feat: 修改了库存统计和负库存更新逻辑

This commit is contained in:
mkm 2024-09-01 18:04:07 +08:00
parent 97c4cf60d5
commit 14ffe62487

View File

@ -56,8 +56,8 @@ class WarehouseLogic extends BaseLogic
$topData[] = [ $topData[] = [
'title' => '总商品库存', 'title' => '总商品库存',
'desc' => '平台统计商品总库存、含门店仓库', 'desc' => '平台统计商品总库存、含门店仓库',
'total_money' => bcadd($warehouseProductStorege['nums'],$storeBranchProduct['stock'],2), 'total_money' => bcadd($warehouseProductStorege['nums'], $storeBranchProduct['stock'], 2),
'cash_title' => bcadd($warehouseProductStorege['total_price'],$storeBranchProduct['total_price'],2), 'cash_title' => bcadd($warehouseProductStorege['total_price'], $storeBranchProduct['total_price'], 2),
'value' => [], 'value' => [],
'type' => 1, 'type' => 1,
]; ];
@ -193,14 +193,14 @@ class WarehouseLogic extends BaseLogic
$list = StoreProduct::where('stock', '<', 0)->page($parmas['page_no'], 15)->select()->toArray(); $list = StoreProduct::where('stock', '<', 0)->page($parmas['page_no'], 15)->select()->toArray();
$count = StoreProduct::where('stock', '<', 0)->count(); $count = StoreProduct::where('stock', '<', 0)->count();
} elseif ($parmas['type'] == 2) { } elseif ($parmas['type'] == 2) {
$where[]=['stock','<',0]; $where[] = ['stock', '<', 0];
if(isset($parmas['store_id']) && $parmas['store_id'] > 0){ if (isset($parmas['store_id']) && $parmas['store_id'] > 0) {
$where[]=['store_id','=',$parmas['store_id']]; $where[] = ['store_id', '=', $parmas['store_id']];
} }
$store_arr=getenv('NO_STORE_STATISTICS'); $store_arr = getenv('NO_STORE_STATISTICS');
if($store_arr){ if ($store_arr) {
$store_arr=explode(',',$store_arr); $store_arr = explode(',', $store_arr);
$where[]=['store_id','not in',$store_arr]; $where[] = ['store_id', 'not in', $store_arr];
} }
$list = StoreBranchProduct::where($where)->page($parmas['page_no'], 15)->select() $list = StoreBranchProduct::where($where)->page($parmas['page_no'], 15)->select()
->each(function ($item) { ->each(function ($item) {
@ -220,60 +220,55 @@ class WarehouseLogic extends BaseLogic
$count = WarehouseProductStorege::where('nums', '<', 0)->count(); $count = WarehouseProductStorege::where('nums', '<', 0)->count();
} }
return ['lists' => $list, 'count' => $count]; return ['lists' => $list, 'count' => $count];
} }
/** /**
* 负库存更新归0 * 负库存更新归0
*/ */
public static function updateNegativeZero($parmas) public static function updateNegativeZero($parmas)
{ {
if ($parmas['type'] == 1) { if ($parmas['type'] == 1) {
$res = StoreProduct::where('id',$parmas['id'])->update(['stock'=>0]); $res = StoreProduct::where('id', $parmas['id'])->update(['stock' => 0]);
} elseif ($parmas['type'] == 2) { } elseif ($parmas['type'] == 2) {
$res=StoreBranchProduct::where('id',$parmas['id'])->update(['stock'=>0]); $res = StoreBranchProduct::where('id', $parmas['id'])->update(['stock' => 0]);
} elseif ($parmas['type'] == 3) { } elseif ($parmas['type'] == 3) {
$res = WarehouseProductStorege::where('id',$parmas['id'])->update(['nums'=>0]); $res = WarehouseProductStorege::where('id', $parmas['id'])->update(['nums' => 0]);
} }
return $res; return $res;
} }
public static function stockProductPrice($parmas){ public static function stockProductPrice($parmas)
$arr1=WarehouseProductStorege::where('nums','>',0)->select(); {
foreach ($arr1 as $k=>$v){ $arr1 = WarehouseProductStorege::where('nums', '>', 0)->select();
$find=StoreProduct::where('id',$v['product_id'])->find(); foreach ($arr1 as $k => $v) {
if($find&& $find['price']>0){ $find = StoreProduct::where('id', $v['product_id'])->find();
$total_price=bcmul($find['price'],$v['nums'],2); if ($find && $find['price'] > 0) {
$price=$find['price']; $total_price = bcmul($find['price'], $v['nums'], 2);
}else{ $price = $find['price'];
$total_price=0; } else {
$price=0; $total_price = 0;
$price = 0;
} }
WarehouseProductStorege::where('id',$v['id'])->update(['price'=>$price,'total_price'=>$total_price]); WarehouseProductStorege::where('id', $v['id'])->update(['price' => $price, 'total_price' => $total_price]);
} }
$arr2=StoreBranchProduct::where('stock','>',0)->select(); $arr2 = StoreBranchProduct::where('stock', '>', 0)->select();
foreach ($arr2 as $k=>$v){ foreach ($arr2 as $k => $v) {
if($v['price']>0){ if ($v['price'] > 0) {
$total_price=bcmul($v['price'],$v['stock'],2); $total_price = bcmul($v['price'], $v['stock'], 2);
}else{ } else {
$total_price=0; $total_price = 0;
} }
StoreBranchProduct::where('id',$v['id'])->update(['total_price'=>$total_price]); StoreBranchProduct::where('id', $v['id'])->update(['total_price' => $total_price]);
} }
$arr3=WarehouseProductStorege::where('nums','>',0)->field('product_id,sum(nums) as nums')->group('product_id')->select(); $arr3 = StoreProduct::where('stock', '>=', 0)->select();
foreach ($arr3 as $k=>$v){ foreach ($arr3 as $k => $v) {
StoreProduct::where('id',$v['product_id'])->update(['stock'=>$v['nums']]); $stock = StoreBranchProduct::where('product_id', $v['id'])->where('stock', '>', 0)->sum('stock');
} $nums = WarehouseProductStorege::where('nums', '>', 0)->where('product_id', $v['id'])->sum('nums');
$arr4=StoreBranchProduct::where('stock','>',0)->field('product_id,sum(stock) as stock')->group('product_id')->order('stock desc')->select(); $stock2 = bcadd($stock, $nums, 2);
foreach ($arr4 as $k=>$v){ bcmul($v['purchase'], $stock2, 2);
$find=StoreProduct::where('id',$v['product_id'])->find(); StoreProduct::where('id', $v['id'])->update(['stock' => $stock2, 'total_price' => $v]);
if($find){
$stock=bcadd($find['stock'],$v['stock'],2);
$find->total_price=bcmul($find['purchase'],$v['stock'],2);
$find->stock=$stock;
$find->save();
}
} }
return true; return true;
} }
} }