diff --git a/app/admin/controller/store_product/StoreProductController.php b/app/admin/controller/store_product/StoreProductController.php index d7fb722f..c7d01048 100644 --- a/app/admin/controller/store_product/StoreProductController.php +++ b/app/admin/controller/store_product/StoreProductController.php @@ -6,7 +6,9 @@ namespace app\admin\controller\store_product; use app\admin\controller\BaseAdminController; use app\admin\lists\store_product\StoreProductLists; use app\admin\logic\store_product\StoreProductLogic; +use app\admin\logic\warehouse_product\WarehouseProductLogic; use app\admin\validate\store_product\StoreProductValidate; +use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_product\StoreProduct; use app\common\model\warehouse_product_storege\WarehouseProductStorege; use Webman\RedisQueue\Redis; @@ -113,16 +115,41 @@ class StoreProductController extends BaseAdminController if ($count == 1) { $store_id = $store_arr[0]; foreach ($product_arr as $key => $arr) { - $find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray(); - StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id); + // $find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray(); + $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, + ]; + WarehouseProductLogic::add($data); + 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) { - $find = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray(); - StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_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, + ]; + WarehouseProductLogic::add($data); + 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]); } } diff --git a/app/admin/controller/warehouse_product/WarehouseProductController.php b/app/admin/controller/warehouse_product/WarehouseProductController.php index 42d89d7c..4f1fdeb3 100644 --- a/app/admin/controller/warehouse_product/WarehouseProductController.php +++ b/app/admin/controller/warehouse_product/WarehouseProductController.php @@ -40,6 +40,7 @@ class WarehouseProductController extends BaseAdminController { $params = (new WarehouseProductValidate())->post()->goCheck('add'); $params['admin_id']=$this->adminId; + $params['store_id']=0; $result = WarehouseProductLogic::add($params); if (WarehouseProductLogic::hasError()) { return $this->fail(WarehouseProductLogic::getError()); @@ -103,12 +104,12 @@ class WarehouseProductController extends BaseAdminController * 确认操作 */ public function enter(){ - $id=$this->request->post('id'); - $result = WarehouseProductLogic::enter($id); - if (true === $result) { - return $this->success('编辑成功', [], 1, 1); - } - return $this->fail(WarehouseProductLogic::getError()); - return $this->success($result); + // $id=$this->request->post('id'); + // $result = WarehouseProductLogic::enter($id); + // if (true === $result) { + // return $this->success('编辑成功', [], 1, 1); + // } + // return $this->fail(WarehouseProductLogic::getError()); + return $this->success(''); } } \ No newline at end of file diff --git a/app/admin/logic/store_product/StoreProductLogic.php b/app/admin/logic/store_product/StoreProductLogic.php index a0ab807f..cf07e3b4 100644 --- a/app/admin/logic/store_product/StoreProductLogic.php +++ b/app/admin/logic/store_product/StoreProductLogic.php @@ -46,7 +46,7 @@ class StoreProductLogic extends BaseLogic 'bar_code' => $params['bar_code'] ?? '', 'cate_id' => $params['cate_id'], 'unit' => $params['unit'], - 'stock' => $params['stock'], + 'stock' => 0, 'price' => $params['price'], 'vip_price' => $params['vip_price'], 'cost' => $params['cost'], @@ -73,7 +73,7 @@ class StoreProductLogic extends BaseLogic "cost" => $params['cost'], "purchase" => $params['purchase'], "unit" => $params["unit"], - "stock" => $params['stock'], + "stock" => 0, "product_id" => $res['id'], "unique" => setUnique($res['id'], '', 0), 'sales' => 0, @@ -446,9 +446,9 @@ 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); - } + // if ($product_arr['stock'] > 0) { + // self::storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id); + // } Db::commit(); return true; } catch (\Exception $e) { @@ -460,9 +460,9 @@ class StoreProductLogic extends BaseLogic //更新门店库存 Db::startTrans(); try { - if ($product_arr['stock'] > 0) { - self::storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id); - } + // if ($product_arr['stock'] > 0) { + // self::storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id); + // } Db::commit(); return true; } catch (\Exception $e) { @@ -497,9 +497,9 @@ class StoreProductLogic extends BaseLogic 'stock' => 0, ]; StoreBranchProductExchange::create($product); - if ($product_arr['stock'] > 0) { - self::storage($find, $store_id, $admin_id, $product_arr,$warehouse_id); - } + // if ($product_arr['stock'] > 0) { + // self::storage($find, $store_id, $admin_id, $product_arr,$warehouse_id); + // } Db::commit(); return true; } catch (\Exception $e) { @@ -510,9 +510,9 @@ class StoreProductLogic extends BaseLogic } else { Db::startTrans(); try { - if ($product_arr['stock'] > 0) { - self::storage($find, $store_id, $admin_id, $product_arr,2,$warehouse_id); - } + // if ($product_arr['stock'] > 0) { + // self::storage($find, $store_id, $admin_id, $product_arr,2,$warehouse_id); + // } Db::commit(); return true; } catch (\Exception $e) { diff --git a/app/admin/logic/warehouse_product/WarehouseProductLogic.php b/app/admin/logic/warehouse_product/WarehouseProductLogic.php index 25f6b2d0..7aef771d 100644 --- a/app/admin/logic/warehouse_product/WarehouseProductLogic.php +++ b/app/admin/logic/warehouse_product/WarehouseProductLogic.php @@ -11,6 +11,7 @@ use app\common\model\system_store_storage\SystemStoreStorage; use app\common\model\warehouse_product_storege\WarehouseProductStorege; use support\Log; use think\facade\Db; +use support\exception\BusinessException; /** @@ -33,20 +34,46 @@ class WarehouseProductLogic extends BaseLogic { // Db::startTrans(); // try { + $before_nums=0; + $after_nums=0; + $storege = WarehouseProductStorege::where('warehouse_id', $params['warehouse_id'])->where('product_id', $params['product_id'])->find(); + if($storege){ + if ($params['financial_pm'] == 0) { + $after_nums=$storege['nums']-$params['nums']; + if($after_nums<0){ + throw new BusinessException('库存不足'); + return false; + } + WarehouseProductStorege::where('id', $storege['id'])->dec('nums', $params['nums'])->update(); + } else { + $after_nums=$storege['nums']+$params['nums']; + WarehouseProductStorege::where('id', $storege['id'])->inc('nums', $params['nums'])->update(); + } + $before_nums=$storege['nums']; + }else{ + WarehouseProductStorege::create([ + 'warehouse_id' => $params['warehouse_id'], + 'product_id' => $params['product_id'], + 'nums' => $params['nums'], + ]); + } + $batch_count = WarehouseProduct::where(['product_id' => $params['product_id'], 'warehouse_id' => $params['warehouse_id'], 'financial_pm' => $params['financial_pm'], 'store_id' => $params['store_id']])->count(); $data = [ 'warehouse_id' => $params['warehouse_id'], 'store_id' => $params['store_id'] ?? 0, 'product_id' => $params['product_id'], 'financial_pm' => $params['financial_pm'], - 'batch' => 0, + 'batch' => $batch_count+1, 'nums' => $params['nums'], + 'before_nums' => $before_nums, + 'after_nums' => $after_nums, 'price' => $params['price'] ?? '', 'purchase' => $params['purchase'] ?? '', 'cost' => $params['cost'] ?? '', 'total_price' => $params['total_price'] ?? '', 'admin_id' => $params['admin_id'], 'code' => $params['code'] ?? '', - 'status' => $params['status'] ?? 0, + 'status' => 1, 'mark' => $params['mark'] ?? '', ]; if (isset($params['manufacture']) && $params['manufacture'] != '') { @@ -56,7 +83,8 @@ class WarehouseProductLogic extends BaseLogic $data['expiration_date'] = strtotime($params['expiration_date']); } $res = WarehouseProduct::create($data); - self::enter($res['id'], $params['financial_pm']); + + // self::enter($res['id'], $params['financial_pm']); // Db::commit(); return $res; // } catch (\Exception $e) { @@ -108,38 +136,6 @@ class WarehouseProductLogic extends BaseLogic } } - /** - * @notes 确认商品仓储信息 - * @param array $params - * @author admin - * @date 2024/07/31 16:55 - */ - public static function enter($id, $financial_pm = 0) - { - - $find = WarehouseProduct::where('id', $id)->find(); - $find->status = 1; - $find->batch = WarehouseProduct::where(['product_id' => $find['product_id'], 'warehouse_id' => $find['warehouse_id'], 'financial_pm' => $financial_pm, 'store_id' => $find['store_id']])->count(); - $find->save(); - - $storege = WarehouseProductStorege::where('warehouse_id', $find['warehouse_id'])->where('product_id', $find['product_id'])->find(); - - if ($storege) { - if ($financial_pm == 0) { - WarehouseProductStorege::where('id',$storege['id'])->dec('nums', $find['nums'])->update(); - } else { - WarehouseProductStorege::where('id',$storege['id'])->inc('nums', $find['nums'])->update(); - } - } else { - WarehouseProductStorege::create([ - 'warehouse_id' => $find['warehouse_id'], - 'product_id' => $find['product_id'], - 'nums' => $find['nums'], - ]); - } - } - - /** * @notes 删除商品仓储信息 * @param array $params @@ -157,7 +153,7 @@ class WarehouseProductLogic extends BaseLogic } elseif ($res['financial_pm'] == 0) { $find = SystemStoreStorage::where(['outbound_id' => $res['id']])->find(); if ($find) { - if($find['status']==1){ + if ($find['status'] == 1) { $stock = StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->value('stock'); if ($stock < $res['nums']) { self::setError('商品库存不足,无法退回');