feat: 修改了商品库存管理逻辑,增加了库存导入功能,支持单门店和多门店商品库存的增加,并优化了库存日志记录。
This commit is contained in:
parent
bc2ed0635a
commit
22ed5fefdd
@ -101,5 +101,83 @@ class StoreProductController extends BaseAdminController
|
||||
public function import()
|
||||
{
|
||||
return $this->fail('接口已关闭');
|
||||
$product_arr = $this->request->post('product_arr');
|
||||
$store_arr = $this->request->post('store_arr');
|
||||
$stock_type = $this->request->post('stock_type', 1);
|
||||
$warehouse_id = $this->request->post('warehouse_id');
|
||||
$count = count($store_arr);
|
||||
foreach ($product_arr as $key => $arr) {
|
||||
$stock = bcmul($arr['stock'], $count);
|
||||
$nums = WarehouseProductStorege::where('warehouse_id', $warehouse_id)->where('product_id', $arr['id'])->value('nums');
|
||||
if ($nums < $stock) {
|
||||
return $this->fail('商品库存不足');
|
||||
}
|
||||
}
|
||||
if ($count == 1) {
|
||||
$store_id = $store_arr[0];
|
||||
foreach ($product_arr as $key => $arr) {
|
||||
$data = [
|
||||
'warehouse_id' => $warehouse_id,
|
||||
'product_id' => $arr['id'],
|
||||
'store_id' => $store_id,
|
||||
'financial_pm' => 0,
|
||||
'batch' => 1,
|
||||
'nums' => $arr['stock'],
|
||||
'status' => 1,
|
||||
'admin_id' => $this->adminId,
|
||||
];
|
||||
if ($arr['stock'] == 0) {
|
||||
$find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
} else {
|
||||
WarehouseProductLogic::add($data);
|
||||
$find = StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->find();
|
||||
if ($find) {
|
||||
StoreBranchProduct::where('id', $find['id'])->inc('stock', $arr['stock'])->update();
|
||||
} else {
|
||||
$find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->inc('stock', $arr['stock'])->update();
|
||||
}
|
||||
}
|
||||
// StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
|
||||
// Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'stock_type' => $stock_type, 'admin_id' => $this->adminId, 'warehouse_id' => $warehouse_id]);
|
||||
}
|
||||
} else {
|
||||
foreach ($product_arr as $key => $arr) {
|
||||
foreach ($store_arr as $k => $store_id) {
|
||||
$data = [
|
||||
'warehouse_id' => $warehouse_id,
|
||||
'product_id' => $arr['id'],
|
||||
'store_id' => $store_id,
|
||||
'financial_pm' => 0,
|
||||
'batch' => 1,
|
||||
'nums' => $arr['stock'],
|
||||
'status' => 1,
|
||||
'admin_id' => $this->adminId,
|
||||
];
|
||||
if ($arr['stock'] == 0) {
|
||||
$find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
} else {
|
||||
WarehouseProductLogic::add($data);
|
||||
$find = StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->find();
|
||||
if ($find) {
|
||||
StoreBranchProduct::where('id', $find['id'])->inc('stock', $arr['stock'])->update();
|
||||
} else {
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->inc('stock', $arr['stock'])->update();
|
||||
}
|
||||
}
|
||||
|
||||
// $find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
// StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
// Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'stock_type' => $stock_type, 'admin_id' => $this->adminId, 'warehouse_id' => $warehouse_id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success('已导入后台队列,请在门店入库记录中查看', [], 1, 1);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ use app\common\model\warehouse_product_storege\WarehouseProductStorege;
|
||||
use app\common\service\xlsx\OrderDetail;
|
||||
use app\common\service\xlsx\WarehouseOrdeRentry;
|
||||
use support\exception\BusinessException;
|
||||
use support\Log;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
@ -108,54 +109,28 @@ class WarehouseOrderController extends BaseAdminController
|
||||
'status' => 1,
|
||||
'admin_id' => $this->adminId,
|
||||
];
|
||||
$find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
$storeProduct = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
if ($arr['stock'] == 0) {
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $storeProduct);
|
||||
} else {
|
||||
$data['total_price'] = bcmul($arr['stock'], $find['purchase'], 2);
|
||||
$data['purchase'] = $find['purchase'];
|
||||
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
|
||||
$data['purchase'] = $storeProduct['purchase'];
|
||||
$data['oid'] = $res['id'];
|
||||
WarehouseProductLogic::add($data);
|
||||
$find = StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->find();
|
||||
if ($find) {
|
||||
StoreBranchProduct::where('id', $find['id'])->inc('stock', $arr['stock'])->update();
|
||||
} else {
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->inc('stock', $arr['stock'])->update();
|
||||
$ordinary=StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $storeProduct);
|
||||
if(empty($ordinary)){
|
||||
throw new BusinessException('商品不存在');
|
||||
}
|
||||
StoreBranchProduct::where('id', $ordinary['id'])->inc('stock', $arr['stock'])->update();
|
||||
}
|
||||
$finds = WarehouseProduct::where('oid', $res['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
|
||||
WarehouseOrder::where('id', $res['id'])->update(['total_price' => $finds['total_price'], 'nums' => $finds['nums']]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return $this->fail('暂不支持多门店入库');
|
||||
foreach ($product_arr as $key => $arr) {
|
||||
foreach ($store_arr as $k => $store_id) {
|
||||
$data = [
|
||||
'warehouse_id' => $warehouse_id,
|
||||
'product_id' => $arr['id'],
|
||||
'store_id' => $store_id,
|
||||
'financial_pm' => 0,
|
||||
'batch' => 1,
|
||||
'nums' => $arr['stock'],
|
||||
'status' => 1,
|
||||
'admin_id' => $this->adminId,
|
||||
];
|
||||
if ($arr['stock'] == 0) {
|
||||
$find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
} else {
|
||||
WarehouseProductLogic::add($data);
|
||||
$find = StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->find();
|
||||
if ($find) {
|
||||
StoreBranchProduct::where('id', $find['id'])->inc('stock', $arr['stock'])->update();
|
||||
} else {
|
||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
||||
StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->inc('stock', $arr['stock'])->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
|
@ -33,7 +33,7 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
|
||||
public function setSearch(): array
|
||||
{
|
||||
return [
|
||||
'=' => ['warehouse_id', 'financial_pm', 'store_id','oid'],
|
||||
'=' => ['warehouse_id', 'financial_pm', 'store_id','oid','supplier_id'],
|
||||
'between_time' => 'create_time'
|
||||
];
|
||||
}
|
||||
|
@ -395,7 +395,7 @@ class StoreProductLogic extends BaseLogic
|
||||
|
||||
|
||||
/**普通 */
|
||||
public static function ordinary($product_arr, $store_id, $admin_id, $find, $warehouse_id)
|
||||
public static function ordinary($product_arr, $store_id, $admin_id, $find)
|
||||
{
|
||||
$store_find = StoreBranchProduct::where(['product_id' => $product_arr['id'], 'store_id' => $store_id])->findOrEmpty()->toArray();
|
||||
if ($find && !$store_find) {
|
||||
@ -439,32 +439,14 @@ class StoreProductLogic extends BaseLogic
|
||||
'bar_code' => $attr_value['bar_code']
|
||||
];
|
||||
StoreBranchProductAttrValue::create($arr);
|
||||
// if ($product_arr['stock'] > 0) {
|
||||
// self::storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id);
|
||||
// }
|
||||
Db::commit();
|
||||
return true;
|
||||
return $branch;
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// else {
|
||||
// //更新门店库存
|
||||
// Db::startTrans();
|
||||
// try {
|
||||
// // if ($product_arr['stock'] > 0) {
|
||||
// // self::storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id);
|
||||
// // }
|
||||
// Db::commit();
|
||||
// return true;
|
||||
// } catch (\Exception $e) {
|
||||
// Db::rollback();
|
||||
// Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile());
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/**兑换 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user