From 11ba0dc8d323b6953e665bd25299d9b0c8b660c8 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Mon, 26 Jun 2023 14:44:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E9=87=87?= =?UTF-8?q?=E8=B4=AD=E5=85=A5=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/store/product/PurchaseRecord.php | 36 ++++++++++++++ .../store/product/ProductRepository.php | 47 +++++++++++++++++++ app/controller/api/server/StoreProduct.php | 13 +++++ route/api.php | 1 + 4 files changed, 97 insertions(+) create mode 100644 app/common/model/store/product/PurchaseRecord.php diff --git a/app/common/model/store/product/PurchaseRecord.php b/app/common/model/store/product/PurchaseRecord.php new file mode 100644 index 00000000..e9ca9e0f --- /dev/null +++ b/app/common/model/store/product/PurchaseRecord.php @@ -0,0 +1,36 @@ +hasOne(Product::class, 'product_id', 'product_id'); + } + + public function supplier() + { + return $this->hasOne(Merchant::class, 'mer_id', 'supplier_mer_id'); + } + + public function merchant() + { + return $this->hasOne(Merchant::class, 'mer_id', 'mer_id'); + } + +} diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index 0c76faf3..77f65452 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -14,6 +14,7 @@ namespace app\common\repositories\store\product; use app\common\model\store\product\ProductLabel; +use app\common\model\store\product\PurchaseRecord; use app\common\model\store\product\Spu; use app\common\model\user\User; use app\common\repositories\community\CommunityRepository; @@ -2252,4 +2253,50 @@ class ProductRepository extends BaseRepository //单次限购 return $data; } + + /** + * 采购入库 + * @param $params + * @return mixed + * @throws \Exception + */ + public function stockIn($params) + { + $product = $this->get($params['product_id']); + if (!$product) { + return app('json')->fail('商品不存在'); + } + Db::startTrans(); + try { + $attrValue = []; + foreach ($params['attrValue'] as $item) { + $attrValue[$item['unique']] = ['price' => $item['price'], 'number' => $item['number']]; + } + $stockIn = 0; + foreach ($product->attrValue as $item) { + $stockIn += $attrValue[$item->unique]['number']; + $item->update(['stock' => $item->stock + $attrValue[$item->unique]['number']], ['unique' => $item->unique]); + } + $model = new PurchaseRecord(); + $data = [ + 'product_id' => $params['product_id'], + 'number' => json_encode($attrValue), + 'mer_id' => $product->mer_id, + 'supplier_mer_id' => 0, + ]; + if (!$model->save($data)) { + throw new \Exception('入库失败'); + } + $product->stock = $stockIn + $product->stock; + if (!$product->save()) { + throw new \Exception('入库失败'); + } + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + throw new \Exception($e->getMessage()); + } + return true; + } + } diff --git a/app/controller/api/server/StoreProduct.php b/app/controller/api/server/StoreProduct.php index de21a91a..32f6e137 100644 --- a/app/controller/api/server/StoreProduct.php +++ b/app/controller/api/server/StoreProduct.php @@ -216,4 +216,17 @@ class StoreProduct extends BaseController $this->repository->update($id, ['is_good' => $is_good]); return app('json')->success('修改成功'); } + + /** + * 商品入库 + * @return \think\response\Json + * @throws \Exception + */ + public function stockIn() + { + $param = $this->request->param(); + $this->repository->stockIn($param); + return app('json')->success('入库成功'); + } + } diff --git a/route/api.php b/route/api.php index 6dabf1d3..144ef46f 100644 --- a/route/api.php +++ b/route/api.php @@ -257,6 +257,7 @@ Route::group('api/', function () { Route::post('product/destory/:id', 'StoreProduct/destory'); Route::post('product/good/:id', 'StoreProduct/updateGood'); Route::get('product/config', 'StoreProduct/config'); + Route::post('product/stockIn', 'StoreProduct/stockIn'); //商品分类 Route::get('category/lst', 'StoreCategory/lst');