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