diff --git a/app/Models/BagOrderModel.php b/app/Models/BagOrderModel.php index f56023e..ead93ca 100644 --- a/app/Models/BagOrderModel.php +++ b/app/Models/BagOrderModel.php @@ -10,9 +10,26 @@ class BagOrderModel extends Model protected $primaryKey = 'bo_idx'; protected $returnType = 'object'; protected $useTimestamps = false; + + /** + * 동일 발주 UUID에 대해 bo_version이 최대인 행만 (수정으로 생긴 이전 버전 행은 목록·이력에서 제외). + * DB에는 버전별 행이 그대로 남고, 조회 시에만 필터한다. + */ + public function whereLatestHead(int $lgIdx): self + { + $lg = (int) $lgIdx; + + return $this->where( + "(bo_uuid, bo_version) IN (SELECT bo_uuid, MAX(bo_version) FROM {$this->table} WHERE bo_lg_idx = {$lg} GROUP BY bo_uuid)", + null, + false + ); + } + protected $allowedFields = [ 'bo_uuid', 'bo_version', 'bo_lg_idx', 'bo_gugun_code', 'bo_dong_code', 'bo_company_idx', 'bo_agency_idx', 'bo_fee_rate', 'bo_order_date', + 'bo_bag_types', 'bo_unit_prices', 'bo_qty_boxes', 'bo_lot_no', 'bo_hash', 'bo_status', 'bo_orderer_idx', 'bo_regdate', 'bo_moddate', ];