krgm.so-manager-dev.com/app/Models/Price.php
kin.rinzen 5c1b33cedc
All checks were successful
Deploy main / deploy (push) Successful in 21s
SWA-67/SWA-80/SWA-89 画面修正
2025-10-01 18:00:19 +09:00

114 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Price extends Model
{
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
const PRICE_MONTH = [
1 => '1ヶ月',
2 => '2ヶ月',
3 => '3ヶ月',
4 => '6ヶ月',
5 => '12ヶ月',
];
protected $table = 'price_a';
protected $primaryKey = 'price_parkplaceid';
protected $fillable = [
'price_parkplaceid',
'prine_name',
'price_month',
'park_id',
'psection_id',
'price_ptypeid',
'user_categoryid',
'pplace_id',
'price'
];
public static function boot()
{
parent::boot();
self::creating(function (Price $model) {
$model->operator_id = Auth::user()->ope_id;
});
}
public static function search($inputs)
{
$query = self::query()
->select(
'price_a.*',
\DB::raw("CONCAT_WS('', usertype.usertype_subject1, usertype.usertype_subject2, usertype.usertype_subject3) as user_category_name")
)
->leftJoin('usertype', 'price_a.user_categoryid', '=', 'usertype.user_categoryid');
// ソート対象カラム
$allowedSortColumns = [
'price_parkplaceid', // 駐車場所ID
'park_id', // 駐輪場ID
'prine_name', // 商品名
'price_month', // 期間
'user_categoryid', // 利用者分類ID
'price', // 駐輪料金(税込)
'psection_id', // 車種区分ID
'price_ptypeid', // 駐輪分類ID
'pplace_id', // 駐車車室ID
];
$sortColumn = $inputs['sort'] ?? '';
$sortType = strtolower($inputs['sort_type'] ?? 'asc');
if (in_array($sortColumn, $allowedSortColumns, true)) {
if (!in_array($sortType, ['asc', 'desc'], true)) {
$sortType = 'asc';
}
$query->orderBy($sortColumn, $sortType);
}
return $inputs['isExport']
? $query->get()
: $query->paginate(\App\Utils::item_per_page ?? 20);
}
public static function getByPk($pk)
{
return self::find($pk);
}
public static function deleteByPk($arr)
{
return self::whereIn('price_parkplaceid', $arr)->delete();
}
//TODO 駐車場所ID not found in database specs
//TODO 駐車車室ID not found in database specs
public function getPark()
{
return $this->belongsTo(Park::class, 'park_id', 'park_id')->first();
}
public function getPSection()
{
return $this->belongsTo(Psection::class, 'psection_id', 'psection_id')->first();
}
public function getPType()
{
return $this->belongsTo(Ptype::class, 'price_ptypeid', 'ptype_id')->first();
}
public function getUserType()
{
return $this->belongsTo(Usertype::class, 'user_categoryid', 'user_categoryid')->first();
}
}