From f485c956df1b32d932a041414ba4379eea161040 Mon Sep 17 00:00:00 2001 From: lewis <604446095@qq.com> Date: Sat, 11 Jan 2025 16:15:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=95=86=E5=93=81=E8=B0=83?= =?UTF-8?q?=E6=8B=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StoreBranchProductLists.php | 14 +- .../InventoryTransferLogic.php | 138 ++++++++++-------- .../InventoryTransferValidate.php | 8 +- 3 files changed, 87 insertions(+), 73 deletions(-) diff --git a/app/admin/lists/store_branch_product/StoreBranchProductLists.php b/app/admin/lists/store_branch_product/StoreBranchProductLists.php index 3feb38d73..adf157c90 100644 --- a/app/admin/lists/store_branch_product/StoreBranchProductLists.php +++ b/app/admin/lists/store_branch_product/StoreBranchProductLists.php @@ -82,8 +82,12 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI if ($where) { $this->searchWhere[] = $where; } - return StoreBranchProduct::where($this->searchWhere) - ->field(['id', 'store_id', 'product_id', 'image', 'store_name', 'store_info', 'cate_id', 'price', 'sales', 'stock', 'unit', 'cost', 'purchase', 'status', 'batch', 'vip_price','bar_code', 'manufacturer_information','total_price']) + $query = StoreBranchProduct::where($this->searchWhere); + if (isset($this->params['low_stock']) && $this->params['low_stock'] == 1) { + $query->where('stock', '<=', 'low_stock'); + } + return $query + ->field(['id', 'store_id', 'product_id', 'image', 'store_name', 'store_info', 'cate_id', 'price', 'sales', 'stock', 'low_stock', 'unit', 'cost', 'purchase', 'status', 'batch', 'vip_price','bar_code', 'manufacturer_information','total_price']) ->when(!empty($this->adminInfo['store_id']), function ($query) { $query->where('store_id', $this->adminInfo['store_id']); }) @@ -111,7 +115,11 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI */ public function count(): int { - return StoreBranchProduct::where($this->searchWhere) + $query = StoreBranchProduct::where($this->searchWhere); + if (isset($this->params['low_stock']) && $this->params['low_stock'] == 1) { + $query->where('stock', '<=', 'low_stock'); + } + return $query ->when(!empty($this->adminInfo['store_id']), function ($query) { $query->where('store_id', $this->adminInfo['store_id']); }) diff --git a/app/admin/logic/inventory_transfer/InventoryTransferLogic.php b/app/admin/logic/inventory_transfer/InventoryTransferLogic.php index 736fc4b46..f8cc56ffb 100644 --- a/app/admin/logic/inventory_transfer/InventoryTransferLogic.php +++ b/app/admin/logic/inventory_transfer/InventoryTransferLogic.php @@ -6,6 +6,7 @@ use app\admin\logic\warehouse_product\WarehouseProductLogic; use app\common\model\inventory_transfer\InventoryTransfer; use app\common\logic\BaseLogic; use app\common\model\store_branch_product\StoreBranchProduct; +use app\common\model\store_product\StoreProduct; use app\common\model\warehouse_product_storege\WarehouseProductStorege; use support\exception\BusinessException; use think\facade\Db; @@ -29,75 +30,84 @@ class InventoryTransferLogic extends BaseLogic */ public static function add(array $params,$admin_id=0): bool { - $one_before_nums = 0; - $one_after_nums = 0; - $two_before_nums = 0; - $two_after_nums = 0; - if($params['one_type']==1){ - $stock = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['one_id'])->value('stock'); - if ($stock < $params['nums']) { - throw new BusinessException('调拨数量不能大于当前门店库存'); + if (empty($params['product_arr'])) { + throw new BusinessException('请选择商品'); + } + $productIds = array_column($params['product_arr'], 'product_id'); + if ($params['one_type'] == 1) { + $outProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['one_id'])->field('id,product_id,stock')->select()->toArray(); + } else { + $outProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['one_id'])->field('id,product_id,nums stock')->select()->toArray(); + } + $outProducts = reset_index($outProducts, 'product_id'); + if ($params['two_type'] == 1) { + $inProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['two_id'])->field('id,product_id,stock')->select()->toArray(); + } else { + $inProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['two_id'])->field('id,product_id,nums stock')->select()->toArray(); + } + $inProducts = reset_index($inProducts, 'product_id'); + $insert = []; + foreach ($params['product_arr'] as $v) { + $outProduct = !empty($outProducts[$v['product_id']]) ? $outProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']]; + $inProduct = !empty($inProducts[$v['product_id']]) ? $inProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']]; + if ($outProduct['stock'] < $v['nums']) { + throw new BusinessException("出库商品 {$outProduct['product_id']} 调拨数量不能大于当前仓库库存"); } - if($params['two_type']==1){ - $stock_two = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->value('stock'); - }elseif($params['two_type']==2){ - $stock_two = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->value('nums'); - } - $one_before_nums = $stock; - $one_after_nums = bcsub($stock, $params['nums']); - - $two_before_nums = $stock_two; - $two_after_nums = bcadd($stock_two, $params['nums']); - }elseif($params['one_type']==2){ - $stock = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->value('nums'); - if ($stock < $params['nums']) { - throw new BusinessException('调拨数量不能大于当前仓库库存'); - } - if($params['two_type']==1){ - $stock_two = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->value('stock'); - }elseif($params['two_type']==2){ - $stock_two = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->value('nums'); - } - $one_before_nums = $stock; - $one_after_nums = bcsub($stock, $params['nums']); - - $two_before_nums = $stock_two; - $two_after_nums = bcadd($stock_two, $params['nums']); - }else{ - throw new BusinessException('调拨类型错误'); + $insert[] = [ + 'product_id' => $v['product_id'], + 'nums' => $v['nums'], + 'one_before_nums' => $outProduct['stock'], + 'one_after_nums' => bcsub($outProduct['stock'], $v['nums']), + 'two_before_nums' => $inProduct['stock'], + 'two_after_nums' => bcadd($inProduct['stock'], $v['nums']), + 'one_type' => $params['one_type'], + 'two_type' => $params['two_type'], + 'one_id' => $params['one_id'], + 'two_id' => $params['two_id'], + 'create_time' => time(), + ]; } Db::startTrans(); try { - InventoryTransfer::create([ - 'product_id' => $params['product_id'], - 'nums' => $params['nums'], - 'one_before_nums' => $one_before_nums, - 'one_after_nums' => $one_after_nums, - 'two_before_nums' => $two_before_nums, - 'two_after_nums' => $two_after_nums, - 'one_type' => $params['one_type'], - 'two_type' => $params['two_type'], - 'one_id' => $params['one_id'], - 'two_id' => $params['two_id'] - ]); - if($params['one_type']==1){ - $find=StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['one_id'])->find(); - $find->save(['stock' =>bcsub( $find['stock'],$params['nums'],2)]); - SqlChannelLog('StoreBranchProduct', $find['id'], $params['nums'], -1, Request()->url(),$admin_id); - } elseif ($params['one_type'] == 2) { - $find=WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->find(); - $find->save(['nums' =>bcsub( $find['nums'],$params['nums'],2)]); - SqlChannelLog('WarehouseProductStorege', $find['id'], $params['nums'], -1, Request()->url(),$admin_id); - } - if($params['two_type']==1){ - $find=StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->find(); - $find->save(['stock' =>bcadd( $find['stock'],$params['nums'],2)]); - SqlChannelLog('StoreBranchProduct', $find['id'], $params['nums'], 1, Request()->url(),$admin_id); - } elseif ($params['two_type'] == 2) { - $find=WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->find(); - $find->save(['nums' =>bcadd( $find['nums'],$params['nums'],2)]); - SqlChannelLog('WarehouseProductStorege', $find['id'], $params['nums'], 1, Request()->url(),$admin_id); + InventoryTransfer::insertAll($insert); + foreach ($insert as $v) { + if($params['one_type']==1){ + $find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['one_id'])->find(); + $find->save(['stock' =>bcsub( $find['stock'],$v['nums'],2)]); + SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], -1, Request()->url(),$admin_id); + } elseif ($params['one_type'] == 2) { + $find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['one_id'])->find(); + $find->save(['nums' =>bcsub( $find['nums'],$v['nums'],2)]); + SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], -1, Request()->url(),$admin_id); + } + if($params['two_type']==1){ + $find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['two_id'])->find(); + if (empty($find)) { + $storeProduct = StoreProduct::field('top_cate_id,two_cate_id,cate_id,store_name,image,price,vip_price,cost,purchase,keyword,bar_code,store_info,rose,product_type,unit,batch,store_batch,label_id,is_lack,manufacturer_information')->where('id', $v['product_id'])->find()->toArray(); + $find = new StoreBranchProduct(); + $find->product_id = $v['product_id']; + $find->store_id = $params['two_id']; + $find->stock = $v['nums']; + $find->setAttrs($storeProduct); + $find->save(); + } else { + $find->save(['stock' =>bcadd( $find['stock'],$v['nums'],2)]); + } + SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], 1, Request()->url(),$admin_id); + } elseif ($params['two_type'] == 2) { + $find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['two_id'])->find(); + if (empty($find)) { + $find = new WarehouseProductStorege(); + $find->warehouse_id = $params['two_id']; + $find->product_id = $v['product_id']; + $find->nums = $v['nums']; + $find->save(); + } else { + $find->save(['nums' =>bcadd( $find['nums'],$v['nums'],2)]); + } + SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], 1, Request()->url(),$admin_id); + } } Db::commit(); return true; diff --git a/app/admin/validate/inventory_transfer/InventoryTransferValidate.php b/app/admin/validate/inventory_transfer/InventoryTransferValidate.php index dd55ec814..dcf398e94 100644 --- a/app/admin/validate/inventory_transfer/InventoryTransferValidate.php +++ b/app/admin/validate/inventory_transfer/InventoryTransferValidate.php @@ -20,8 +20,6 @@ class InventoryTransferValidate extends BaseValidate */ protected $rule = [ 'id' => 'require', - 'product_id' => 'require', - 'nums' => 'require', 'type' => 'require', 'one_id' => 'require', 'two_id' => 'require', @@ -34,8 +32,6 @@ class InventoryTransferValidate extends BaseValidate */ protected $field = [ 'id' => 'id', - 'product_id' => '商品', - 'nums' => '数量', 'type' => '1商户2仓库', 'one_id' => '转出id', 'two_id' => '转入id', @@ -50,7 +46,7 @@ class InventoryTransferValidate extends BaseValidate */ public function sceneAdd() { - return $this->only(['product_id','nums','one_id','two_id']); + return $this->only(['one_id','two_id']); } @@ -62,7 +58,7 @@ class InventoryTransferValidate extends BaseValidate */ public function sceneEdit() { - return $this->only(['id','product_id','nums','type','one_id','two_id']); + return $this->only(['id','type','one_id','two_id']); }