39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use CodeIgniter\Model;
|
||
|
|
|
||
|
|
class BagInventoryModel extends Model
|
||
|
|
{
|
||
|
|
protected $table = 'bag_inventory';
|
||
|
|
protected $primaryKey = 'bi_idx';
|
||
|
|
protected $returnType = 'object';
|
||
|
|
protected $useTimestamps = false;
|
||
|
|
protected $allowedFields = [
|
||
|
|
'bi_lg_idx', 'bi_bag_code', 'bi_bag_name', 'bi_qty', 'bi_updated_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 재고 증감 (upsert)
|
||
|
|
*/
|
||
|
|
public function adjustQty(int $lgIdx, string $bagCode, string $bagName, int $delta): void
|
||
|
|
{
|
||
|
|
$existing = $this->where('bi_lg_idx', $lgIdx)->where('bi_bag_code', $bagCode)->first();
|
||
|
|
if ($existing) {
|
||
|
|
$this->update($existing->bi_idx, [
|
||
|
|
'bi_qty' => max(0, (int) $existing->bi_qty + $delta),
|
||
|
|
'bi_updated_at' => date('Y-m-d H:i:s'),
|
||
|
|
]);
|
||
|
|
} else {
|
||
|
|
$this->insert([
|
||
|
|
'bi_lg_idx' => $lgIdx,
|
||
|
|
'bi_bag_code' => $bagCode,
|
||
|
|
'bi_bag_name' => $bagName,
|
||
|
|
'bi_qty' => max(0, $delta),
|
||
|
|
'bi_updated_at'=> date('Y-m-d H:i:s'),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|