修改商品调拨

This commit is contained in:
lewis 2025-01-11 16:15:52 +08:00
parent 768068a8ea
commit f485c956df
3 changed files with 87 additions and 73 deletions

View File

@ -82,8 +82,12 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
if ($where) { if ($where) {
$this->searchWhere[] = $where; $this->searchWhere[] = $where;
} }
return StoreBranchProduct::where($this->searchWhere) $query = 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']) 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) { ->when(!empty($this->adminInfo['store_id']), function ($query) {
$query->where('store_id', $this->adminInfo['store_id']); $query->where('store_id', $this->adminInfo['store_id']);
}) })
@ -111,7 +115,11 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
*/ */
public function count(): int 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) { ->when(!empty($this->adminInfo['store_id']), function ($query) {
$query->where('store_id', $this->adminInfo['store_id']); $query->where('store_id', $this->adminInfo['store_id']);
}) })

View File

@ -6,6 +6,7 @@ use app\admin\logic\warehouse_product\WarehouseProductLogic;
use app\common\model\inventory_transfer\InventoryTransfer; use app\common\model\inventory_transfer\InventoryTransfer;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege; use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use support\exception\BusinessException; use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
@ -29,75 +30,84 @@ class InventoryTransferLogic extends BaseLogic
*/ */
public static function add(array $params,$admin_id=0): bool public static function add(array $params,$admin_id=0): bool
{ {
$one_before_nums = 0; if (empty($params['product_arr'])) {
$one_after_nums = 0; throw new BusinessException('请选择商品');
$two_before_nums = 0; }
$two_after_nums = 0; $productIds = array_column($params['product_arr'], 'product_id');
if($params['one_type']==1){ if ($params['one_type'] == 1) {
$stock = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['one_id'])->value('stock'); $outProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['one_id'])->field('id,product_id,stock')->select()->toArray();
if ($stock < $params['nums']) { } else {
throw new BusinessException('调拨数量不能大于当前门店库存'); $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){ $insert[] = [
$stock_two = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->value('stock'); 'product_id' => $v['product_id'],
}elseif($params['two_type']==2){ 'nums' => $v['nums'],
$stock_two = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->value('nums'); 'one_before_nums' => $outProduct['stock'],
} 'one_after_nums' => bcsub($outProduct['stock'], $v['nums']),
$one_before_nums = $stock; 'two_before_nums' => $inProduct['stock'],
$one_after_nums = bcsub($stock, $params['nums']); 'two_after_nums' => bcadd($inProduct['stock'], $v['nums']),
'one_type' => $params['one_type'],
$two_before_nums = $stock_two; 'two_type' => $params['two_type'],
$two_after_nums = bcadd($stock_two, $params['nums']); 'one_id' => $params['one_id'],
}elseif($params['one_type']==2){ 'two_id' => $params['two_id'],
$stock = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->value('nums'); 'create_time' => time(),
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('调拨类型错误');
} }
Db::startTrans(); Db::startTrans();
try { try {
InventoryTransfer::create([ InventoryTransfer::insertAll($insert);
'product_id' => $params['product_id'], foreach ($insert as $v) {
'nums' => $params['nums'], if($params['one_type']==1){
'one_before_nums' => $one_before_nums, $find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['one_id'])->find();
'one_after_nums' => $one_after_nums, $find->save(['stock' =>bcsub( $find['stock'],$v['nums'],2)]);
'two_before_nums' => $two_before_nums, SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
'two_after_nums' => $two_after_nums, } elseif ($params['one_type'] == 2) {
'one_type' => $params['one_type'], $find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['one_id'])->find();
'two_type' => $params['two_type'], $find->save(['nums' =>bcsub( $find['nums'],$v['nums'],2)]);
'one_id' => $params['one_id'], SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
'two_id' => $params['two_id'] }
]); if($params['two_type']==1){
if($params['one_type']==1){ $find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['two_id'])->find();
$find=StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['one_id'])->find(); if (empty($find)) {
$find->save(['stock' =>bcsub( $find['stock'],$params['nums'],2)]); $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();
SqlChannelLog('StoreBranchProduct', $find['id'], $params['nums'], -1, Request()->url(),$admin_id); $find = new StoreBranchProduct();
} elseif ($params['one_type'] == 2) { $find->product_id = $v['product_id'];
$find=WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->find(); $find->store_id = $params['two_id'];
$find->save(['nums' =>bcsub( $find['nums'],$params['nums'],2)]); $find->stock = $v['nums'];
SqlChannelLog('WarehouseProductStorege', $find['id'], $params['nums'], -1, Request()->url(),$admin_id); $find->setAttrs($storeProduct);
} $find->save();
if($params['two_type']==1){ } else {
$find=StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->find(); $find->save(['stock' =>bcadd( $find['stock'],$v['nums'],2)]);
$find->save(['stock' =>bcadd( $find['stock'],$params['nums'],2)]); }
SqlChannelLog('StoreBranchProduct', $find['id'], $params['nums'], 1, Request()->url(),$admin_id); SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
} elseif ($params['two_type'] == 2) { } elseif ($params['two_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->find(); $find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['two_id'])->find();
$find->save(['nums' =>bcadd( $find['nums'],$params['nums'],2)]); if (empty($find)) {
SqlChannelLog('WarehouseProductStorege', $find['id'], $params['nums'], 1, Request()->url(),$admin_id); $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(); Db::commit();
return true; return true;

View File

@ -20,8 +20,6 @@ class InventoryTransferValidate extends BaseValidate
*/ */
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
'product_id' => 'require',
'nums' => 'require',
'type' => 'require', 'type' => 'require',
'one_id' => 'require', 'one_id' => 'require',
'two_id' => 'require', 'two_id' => 'require',
@ -34,8 +32,6 @@ class InventoryTransferValidate extends BaseValidate
*/ */
protected $field = [ protected $field = [
'id' => 'id', 'id' => 'id',
'product_id' => '商品',
'nums' => '数量',
'type' => '1商户2仓库', 'type' => '1商户2仓库',
'one_id' => '转出id', 'one_id' => '转出id',
'two_id' => '转入id', 'two_id' => '转入id',
@ -50,7 +46,7 @@ class InventoryTransferValidate extends BaseValidate
*/ */
public function sceneAdd() 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() public function sceneEdit()
{ {
return $this->only(['id','product_id','nums','type','one_id','two_id']); return $this->only(['id','type','one_id','two_id']);
} }