修改商品调拨
This commit is contained in:
parent
768068a8ea
commit
f485c956df
@ -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']);
|
||||
})
|
||||
|
@ -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;
|
||||
|
@ -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']);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user