feat: 修改了商品库存管理的相关逻辑

This commit is contained in:
mkm 2024-08-25 13:56:55 +08:00
parent f0f05332de
commit 9fca148539
12 changed files with 28 additions and 156 deletions

View File

@ -115,6 +115,7 @@ class WarehouseOrderController extends BaseAdminController
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2); $data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
$data['purchase'] = $storeProduct['purchase']; $data['purchase'] = $storeProduct['purchase'];
$data['oid'] = $res['id']; $data['oid'] = $res['id'];
$data['financial_pm'] = 0;
WarehouseProductLogic::add($data); WarehouseProductLogic::add($data);
$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']]);
@ -124,6 +125,7 @@ class WarehouseOrderController extends BaseAdminController
Db::commit(); Db::commit();
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
d($e);
throw new BusinessException($e->getMessage()); throw new BusinessException($e->getMessage());
} }
return $this->success('已导入后台队列,请在门店入库记录中查看', [], 1, 1); return $this->success('已导入后台队列,请在门店入库记录中查看', [], 1, 1);

View File

@ -190,12 +190,16 @@ 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) {
$list = StoreBranchProduct::where('stock', '<', 0)->page($parmas['page_no'], 15)->select() $where[]=['stock','<',0];
if(isset($parmas['store_id']) && $parmas['store_id'] > 0){
$where[]=['store_id','=',$parmas['store_id']];
}
$list = StoreBranchProduct::where($where)->page($parmas['page_no'], 15)->select()
->each(function ($item) { ->each(function ($item) {
$item->remark = SystemStore::where('id', $item['store_id'])->value('name'); $item->remark = SystemStore::where('id', $item['store_id'])->value('name');
}) })
->toArray(); ->toArray();
$count = StoreBranchProduct::where('stock', '<', 0)->count(); $count = StoreBranchProduct::where($where)->count();
} elseif ($parmas['type'] == 3) { } elseif ($parmas['type'] == 3) {
$list = WarehouseProductStorege::where('nums', '<', 0)->page($parmas['page_no'], 15)->select() $list = WarehouseProductStorege::where('nums', '<', 0)->page($parmas['page_no'], 15)->select()
->each(function ($item) { ->each(function ($item) {

View File

@ -6,6 +6,7 @@ use app\admin\logic\store_product\StoreProductLogic;
use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
use Webman\RedisQueue\Redis; use Webman\RedisQueue\Redis;
@ -90,22 +91,21 @@ class StoreBranchProductLogic extends BaseLogic
$find=StoreProduct::where('id', $params['product_id'])->find(); $find=StoreProduct::where('id', $params['product_id'])->find();
if($find){ if($find){
$stock=bcadd($find['stock'],$params['nums'],2); $stock=bcadd($find['stock'],$params['nums'],2);
$find->update(['stock'=>$stock,'total_price'=>bcmul($stock,$find['purchase'],2)]); StoreProduct::where('id', $params['product_id'])->update(['stock'=>$stock,'total_price'=>bcmul($stock,$find['purchase'],2)]);
} }
}else{ }else{
StoreBranchProduct::where('id', $params['id'])->dec('stock',$params['nums'])->update(); StoreBranchProduct::where('id', $params['id'])->dec('stock',$params['nums'])->update();
$find=StoreProduct::where('id', $params['product_id'])->find(); $find=StoreProduct::where('id', $params['product_id'])->find();
if($find){ if($find){
$stock=bcsub($find['stock'],$params['nums'],2); $stock=bcsub($find['stock'],$params['nums'],2);
$find->update(['stock'=>$stock,'total_price'=>bcmul($stock,$find['purchase'],2)]); StoreProduct::where('id', $params['product_id'])->update(['stock'=>$stock,'total_price'=>bcmul($stock,$find['purchase'],2)]);
} }
} }
Db::commit(); Db::commit();
return true; return true;
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
/** /**

View File

@ -74,7 +74,6 @@ class StoreProductLogic extends BaseLogic
"unit" => $params["unit"], "unit" => $params["unit"],
"stock" => 0, "stock" => 0,
"product_id" => $res['id'], "product_id" => $res['id'],
"unique" => setUnique($res['id'], '', 0),
'sales' => 0, 'sales' => 0,
]); ]);
Db::commit(); Db::commit();
@ -320,7 +319,6 @@ class StoreProductLogic extends BaseLogic
$arr = [ $arr = [
'product_id' => $product_arr['id'], 'product_id' => $product_arr['id'],
'store_id' => $store_id, 'store_id' => $store_id,
'unique' => setUnique($branch['id'], '', 0),
'sales' => 0, 'sales' => 0,
'type' => 0, 'type' => 0,
'bar_code' => $find['bar_code'] 'bar_code' => $find['bar_code']

View File

@ -122,6 +122,7 @@ class WarehouseProductLogic extends BaseLogic
// Db::commit(); // Db::commit();
return $res; return $res;
} catch (\Throwable $e) { } catch (\Throwable $e) {
d($e);
throw new BusinessException($e->getMessage()); throw new BusinessException($e->getMessage());
// Db::rollback(); // Db::rollback();
// Log::error($e->getMessage().',file:'.$e->getFile().',line:'.$e->getLine()); // Log::error($e->getMessage().',file:'.$e->getFile().',line:'.$e->getLine());

View File

@ -481,7 +481,6 @@ class OrderLogic extends BaseLogic
$data = []; $data = [];
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$data[$k]['product_id'] = $v['product_id']; $data[$k]['product_id'] = $v['product_id'];
// $unique = StoreProductAttrValue::where('product_id', $v['product_id'])->value('v');
$data[$k]['product_attr_unique'] = ''; $data[$k]['product_attr_unique'] = '';
$data[$k]['cart_num'] = $v['cart_num']; $data[$k]['cart_num'] = $v['cart_num'];
$data[$k]['type'] = ''; $data[$k]['type'] = '';

View File

@ -27,32 +27,12 @@ class StoreProductController extends BaseAdminController
return $this->dataLists(new StoreBranchProductLists()); return $this->dataLists(new StoreBranchProductLists());
} }
// #[ /**
// ApiDoc\Title('添加商品'), * 添加商品
// ApiDoc\url('/store/store_product/storeProduct/add'), */
// ApiDoc\Method('POST'),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(ref: [Definitions::class, "token"]),
// ApiDoc\ResponseSuccess("data", type: "array", children: [
// ['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
// ['name' => 'image', 'desc' => '图片', 'type' => 'string'],
// ['name' => 'store_name', 'desc' => '商品名称', 'type' => 'string'],
// ['name' => 'price', 'desc' => '零售价', 'type' => 'float'],
// ['name' => 'cost', 'desc' => '成本价', 'type' => 'float'],
// ['name' => 'sales', 'desc' => '销量', 'type' => 'int'],
// ['name' => 'stock', 'desc' => '库存', 'type' => 'int'],
// ['name' => 'unit_name', 'desc' => '单位', 'type' => 'string'],
// ['name' => 'cate_name', 'desc' => '分类', 'type' => 'string'],
// ]),
// ]
public function add() public function add()
{ {
$params = (new StoreProductValidate())->post()->goCheck('add'); return $this->fail('暂未开放');
$result = StoreProductLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(StoreProductLogic::getError());
} }
// #[ // #[
@ -152,24 +132,11 @@ class StoreProductController extends BaseAdminController
return $this->success('操作成功', [], 1, 1); return $this->success('操作成功', [], 1, 1);
} }
// #[ /**
// ApiDoc\Title('商品库存增减'), * 商品库存增减
// ApiDoc\url('/store/store_product/storeProduct/stock'), */
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "attrs", type: "array", require: true, desc: "id", children: [
// ['name' => 'unique', 'desc' => '唯一值', 'type' => 'string'],
// ['name' => 'type', 'desc' => '类型1增加2减少', 'type' => 'int'],
// ['name' => 'number', 'desc' => '数量', 'type' => 'int'],
// ]),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(ref: [Definitions::class, "token"]),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
public function stock() public function stock()
{ {
$params = (new StoreProductValidate())->post()->goCheck('stock'); return $this->fail('门店禁止手动增减商品库存');
$params['store_id'] = $this->request->adminInfo['store_id'];
StoreBranchProductLogic::stock($params);
return $this->success('操作成功', [], 1, 1);
} }
} }

View File

@ -11,40 +11,17 @@ use app\store\validate\store_product_attr_value\StoreProductAttrValueValidate;
/** /**
* 商品属性值控制器 * 商品规格列表控制器
* Class StoreProductAttrValueController * Class StoreProductAttrValueController
* @package app\store\controller\store_product_attr_value
*/ */
// #[ApiDoc\title('商品属性值')]
class StoreProductAttrValueController extends BaseAdminController class StoreProductAttrValueController extends BaseAdminController
{ {
// #[ /**
// ApiDoc\Title('商品属性值列表'), * 商品规格列表
// ApiDoc\url('/store/store_product_attr_value/storeProductAttrValue/lists'), * @return mixed
// ApiDoc\Method('GET'), */ public function lists()
// ApiDoc\NotHeaders(),
// ApiDoc\Header(ref: [Definitions::class, "token"]),
// ApiDoc\Query(ref: [Definitions::class, "page"]),
// ApiDoc\Query(name: 'product_id', type: 'int', require: true, desc: '商品ID'),
// ApiDoc\Query(name: 'store_id', type: 'int', require: true, desc: '门店ID'),
// ApiDoc\ResponseSuccess("data", type: "array", children: [
// ['name' => 'count', 'desc' => '总数', 'type' => 'int'],
// ['name' => 'page_no', 'desc' => '页码', 'type' => 'int'],
// ['name' => 'page_size', 'desc' => '每页数量', 'type' => 'int'],
// ['name' => 'extend', 'desc' => '扩展数据', 'type' => 'array'],
// ['name' => 'lists', 'desc' => '列表数据', 'type' => 'array', 'children' => [
// ['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
// ['name' => 'product_id', 'desc' => '商品ID', 'type' => 'int'],
// ['name' => 'stock', 'desc' => '库存', 'type' => 'int'],
// ['name' => 'unique', 'desc' => '唯一值', 'type' => 'string'],
// ['name' => 'sales', 'desc' => '销量', 'type' => 'int'],
// ['name' => 'bar_code', 'desc' => '条码', 'type' => 'string'],
// ['name' => 'image', 'desc' => '规格图片', 'type' => 'string'],
// ]],
// ]),
// ]
public function lists()
{ {
return $this->dataLists(new StoreProductAttrValueLists()); return $this->dataLists(new StoreProductAttrValueLists());
} }

View File

@ -72,7 +72,6 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
foreach ($list as $key => &$item) { foreach ($list as $key => &$item) {
$find = StoreBranchProduct::where(['product_id' => $item['product_id'], 'store_id' => $item['store_id']]) $find = StoreBranchProduct::where(['product_id' => $item['product_id'], 'store_id' => $item['store_id']])
->field('product_id,image,price,cost,store_name,unit,delete_time,vip_price,top_cate_id') ->field('product_id,image,price,cost,store_name,unit,delete_time,vip_price,top_cate_id')
->withTrashed()
->find(); ->find();
if ($find) { if ($find) {
if ($off_activity == 1) { if ($off_activity == 1) {

View File

@ -43,7 +43,7 @@ class StoreProductAttrValueLists extends BaseAdminDataLists implements ListsSear
public function lists(): array public function lists(): array
{ {
return StoreBranchProductAttrValue::with('attr')->where($this->searchWhere) return StoreBranchProductAttrValue::with('attr')->where($this->searchWhere)
->field(['id', 'product_id', 'stock', 'unique', 'sales', 'bar_code' ->field(['id', 'product_id', 'stock','sales', 'bar_code'
]) ])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])

View File

@ -57,7 +57,6 @@ class StoreBranchProductLogic extends BaseLogic
"price" => $data['price'], "price" => $data['price'],
"stock" => $params['stock'], "stock" => $params['stock'],
"product_id" => $res['id'], "product_id" => $res['id'],
"unique" => setUnique($res['id'], '', 0),
'sales' => 0, 'sales' => 0,
]); ]);
StoreCategory::where('id', $params['cate_id'])->inc('three')->update(); StoreCategory::where('id', $params['cate_id'])->inc('three')->update();
@ -149,46 +148,6 @@ class StoreBranchProductLogic extends BaseLogic
} }
} }
/**
* @notes 商品库存
* @param array $params
* @return bool
*/
public static function stock(array $params): bool
{
$attrs = reset_index($params['attrs'], 'unique');
$attrValue = StoreBranchProductAttrValue::where('store_id', $params['store_id'])->whereIn('unique', array_keys($attrs))->select()->toArray();
if (empty($attrValue)) {
throw new \Exception('商品属性不存在');
}
$StoreProduct = StoreBranchProduct::where('store_id', $params['store_id'])->where('product_id', $attrValue[0]['product_id'])->find();
if (empty($StoreProduct)) {
throw new \Exception('商品不存在');
}
self::checkAuth($StoreProduct);
Db::startTrans();
try {
$productStockIn = 0;
foreach ($attrValue as $k => $v) {
$current = $attrs[$v['unique']] ?? [];
if (empty($current) || $v['unique'] != $current['unique']) {
continue;
}
$stock = $current['type'] == 1 ? $v['stock'] + $current['number'] : $v['stock'] - $current['number'];
$stock = max($stock, 0);
$productStockIn = $current['type'] == 1 ? $StoreProduct['stock'] + $current['number'] : $StoreProduct['stock'] - $current['number'];
$productStockIn = max($productStockIn, 0);
StoreBranchProductAttrValue::where('id', $v['id'])->update(['stock' => $stock]);
}
StoreBranchProduct::where('id', $StoreProduct['id'])->update(['stock' => $productStockIn]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new \Exception($e->getMessage());
}
}
public static function checkAuth($product) public static function checkAuth($product)
{ {
if (request()->adminInfo['store_id'] != $product['store_id']) { if (request()->adminInfo['store_id'] != $product['store_id']) {

View File

@ -30,34 +30,6 @@ class StoreProductLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
$data = [
'store_name' => $params['store_name'],
'image' => $params['image'],
'bar_code' => $params['bar_code'] ?? '',
'cate_id' => $params['cate_id'],
'unit' => $params['unit'],
'stock' => $params['stock'],
'cost' => $params['cost'],
'purchase' => $params['purchase'],
'rose' => $params['rose'],
];
$rose_price = bcmul($params['cost'], $params['rose'], 2);
$data['price'] = bcadd($params['cost'], $rose_price, 2);
$res = StoreProduct::create($data);
StoreProductAttrValue::create([
"bar_code" => $params["bar_code"] ?? '',
"image" => $params["image"] ?? '',
"cost" => $params['cost'],
"purchase" => $params['purchase'],
"unit" => $params["unit"],
"price" => $data['price'],
"stock" => $params['stock'],
"product_id" => $res['id'],
"unique" => setUnique($res['id'], '', 0),
'sales' => 0,
]);
StoreCategory::where('id', $params['cate_id'])->inc('three')->update();
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
@ -78,16 +50,10 @@ class StoreProductLogic extends BaseLogic
public static function edit(array $params): bool public static function edit(array $params): bool
{ {
Db::startTrans(); Db::startTrans();
$StoreProduct = StoreProduct::where('id', $params['id'])->find();
try { try {
if ($StoreProduct['cate_id'] != $params['cate_id']) {
StoreCategory::where('id', $params['cate_id'])->inc('three')->update();
StoreCategory::where('id', $StoreProduct['cate_id'])->dec('three')->update();
}
StoreProduct::where('id', $params['id'])->update([ StoreProduct::where('id', $params['id'])->update([
'store_name' => $params['store_name'] 'store_name' => $params['store_name']
]); ]);
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {