단가 조회 공통 로직을 모델로 통합하고 발주·판매·주문·사이트 화면의 단가 계산이 모두 최신 등록 순서(bp_regdate, bp_idx DESC)를 따르도록 맞춘다. Made-with: Cursor
61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class BagPriceModel extends Model
|
|
{
|
|
protected $table = 'bag_price';
|
|
protected $primaryKey = 'bp_idx';
|
|
protected $returnType = 'object';
|
|
protected $useTimestamps = false;
|
|
protected $allowedFields = [
|
|
'bp_lg_idx', 'bp_bag_code', 'bp_bag_name',
|
|
'bp_order_price', 'bp_wholesale', 'bp_consumer',
|
|
'bp_start_date', 'bp_end_date', 'bp_state',
|
|
'bp_regdate', 'bp_moddate', 'bp_reg_mb_idx',
|
|
];
|
|
|
|
/**
|
|
* 같은 봉투코드에 단가 기간이 겹쳐도 "나중 등록 단가"가 우선되도록
|
|
* 활성 단가를 등록일/PK 역순으로 정렬해 봉투코드별 1건만 남긴다.
|
|
*
|
|
* @return array<string, object>
|
|
*/
|
|
public function latestActiveMapByBagCode(int $lgIdx): array
|
|
{
|
|
$rows = $this->where('bp_lg_idx', $lgIdx)
|
|
->where('bp_state', 1)
|
|
->orderBy('bp_regdate', 'DESC')
|
|
->orderBy('bp_idx', 'DESC')
|
|
->findAll();
|
|
|
|
$map = [];
|
|
foreach ($rows as $row) {
|
|
$code = (string) ($row->bp_bag_code ?? '');
|
|
if ($code === '' || isset($map[$code])) {
|
|
continue;
|
|
}
|
|
$map[$code] = $row;
|
|
}
|
|
|
|
return $map;
|
|
}
|
|
|
|
public function latestActiveByBagCode(int $lgIdx, string $bagCode): ?object
|
|
{
|
|
$bagCode = trim($bagCode);
|
|
if ($bagCode === '') {
|
|
return null;
|
|
}
|
|
|
|
return $this->where('bp_lg_idx', $lgIdx)
|
|
->where('bp_bag_code', $bagCode)
|
|
->where('bp_state', 1)
|
|
->orderBy('bp_regdate', 'DESC')
|
|
->orderBy('bp_idx', 'DESC')
|
|
->first();
|
|
}
|
|
}
|