feat: 修改了商品库存管理逻辑,增加了库存导入功能,支持单门店和多门店商品库存的增加,并优化了库存日志记录。
This commit is contained in:
parent
bc2ed0635a
commit
22ed5fefdd
app/admin
controller
lists/warehouse_product
logic/store_product
@ -101,5 +101,83 @@ class StoreProductController extends BaseAdminController
|
|||||||
public function import()
|
public function import()
|
||||||
{
|
{
|
||||||
return $this->fail('接口已关闭');
|
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\OrderDetail;
|
||||||
use app\common\service\xlsx\WarehouseOrdeRentry;
|
use app\common\service\xlsx\WarehouseOrdeRentry;
|
||||||
use support\exception\BusinessException;
|
use support\exception\BusinessException;
|
||||||
|
use support\Log;
|
||||||
use think\facade\Db;
|
use think\facade\Db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,54 +109,28 @@ class WarehouseOrderController extends BaseAdminController
|
|||||||
'status' => 1,
|
'status' => 1,
|
||||||
'admin_id' => $this->adminId,
|
'admin_id' => $this->adminId,
|
||||||
];
|
];
|
||||||
$find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
$storeProduct = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
|
||||||
if ($arr['stock'] == 0) {
|
if ($arr['stock'] == 0) {
|
||||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $storeProduct);
|
||||||
} else {
|
} else {
|
||||||
$data['total_price'] = bcmul($arr['stock'], $find['purchase'], 2);
|
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
|
||||||
$data['purchase'] = $find['purchase'];
|
$data['purchase'] = $storeProduct['purchase'];
|
||||||
$data['oid'] = $res['id'];
|
$data['oid'] = $res['id'];
|
||||||
WarehouseProductLogic::add($data);
|
WarehouseProductLogic::add($data);
|
||||||
$find = StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->find();
|
$find = StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->find();
|
||||||
if ($find) {
|
if ($find) {
|
||||||
StoreBranchProduct::where('id', $find['id'])->inc('stock', $arr['stock'])->update();
|
StoreBranchProduct::where('id', $find['id'])->inc('stock', $arr['stock'])->update();
|
||||||
} else {
|
} else {
|
||||||
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
|
$ordinary=StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $storeProduct);
|
||||||
StoreBranchProduct::where('product_id', $arr['id'])->where('store_id', $store_id)->inc('stock', $arr['stock'])->update();
|
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();
|
$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']]);
|
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();
|
Db::commit();
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
|
@ -33,7 +33,7 @@ class WarehouseProductLists extends BaseAdminDataLists implements ListsSearchInt
|
|||||||
public function setSearch(): array
|
public function setSearch(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'=' => ['warehouse_id', 'financial_pm', 'store_id','oid'],
|
'=' => ['warehouse_id', 'financial_pm', 'store_id','oid','supplier_id'],
|
||||||
'between_time' => 'create_time'
|
'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();
|
$store_find = StoreBranchProduct::where(['product_id' => $product_arr['id'], 'store_id' => $store_id])->findOrEmpty()->toArray();
|
||||||
if ($find && !$store_find) {
|
if ($find && !$store_find) {
|
||||||
@ -439,32 +439,14 @@ class StoreProductLogic extends BaseLogic
|
|||||||
'bar_code' => $attr_value['bar_code']
|
'bar_code' => $attr_value['bar_code']
|
||||||
];
|
];
|
||||||
StoreBranchProductAttrValue::create($arr);
|
StoreBranchProductAttrValue::create($arr);
|
||||||
// if ($product_arr['stock'] > 0) {
|
|
||||||
// self::storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id);
|
|
||||||
// }
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
return true;
|
return $branch;
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
Db::rollback();
|
Db::rollback();
|
||||||
Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile());
|
Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile());
|
||||||
return false;
|
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