From 5e9b8d6968e0e90f5278598343ef91acbee25ae9 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Wed, 5 Jul 2023 16:10:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95=E5=95=86=E5=93=81=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E5=85=A5=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/store/product/ProductAttrValue.php | 2 +- .../store/product/ProductRepository.php | 120 +++++++++++++----- app/controller/api/server/StoreProduct.php | 6 +- 3 files changed, 95 insertions(+), 33 deletions(-) diff --git a/app/common/model/store/product/ProductAttrValue.php b/app/common/model/store/product/ProductAttrValue.php index db0ab438..b84c9f5e 100644 --- a/app/common/model/store/product/ProductAttrValue.php +++ b/app/common/model/store/product/ProductAttrValue.php @@ -43,7 +43,7 @@ class ProductAttrValue extends BaseModel public function getDetailAttr($value) { - return json_decode($value); + return json_decode($value, true); } public function product() diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index 46cc4586..e152f831 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -13,6 +13,9 @@ namespace app\common\repositories\store\product; +use app\common\model\store\order\StoreOrder; +use app\common\model\store\order\StoreOrderProduct; +use app\common\model\store\product\ProductAttrValue; use app\common\model\store\product\ProductLabel; use app\common\model\store\product\PurchaseRecord; use app\common\model\store\product\Spu; @@ -2256,40 +2259,76 @@ class ProductRepository extends BaseRepository /** * 采购入库 + * @param $merId * @param $params - * @return mixed + * @return true * @throws \Exception */ - public function stockIn($params) + public function stockIn($merId, $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']]; + $supplierMerId = 0; + if (empty($params['product_id']) && !empty($params['order_product_id'])) { + //有商品无规格或者无商品无规格,导入商品和规格 + $product = Db::name('store_product')->where('old_product_id', $params['order_product_id'])->where('mer_id', $merId)->find(); + if (!empty($product)) { + $unique = $this->importAttrValue($params['order_product_id'], $product, $params['order_unique']); + if (!$unique) { + throw new \Exception('商品规格导入出错'); + } + $attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $product['product_id'])->where('unique', $unique)->find(); + } else { + $productId = $this->import($params['order_product_id'], request()->userInfo(), $params['order_unique']); + $product = $this->get($productId); + $attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $productId)->find(); + } + } else { + //有商品有规格 + $product = $this->get($params['product_id']); + $attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $params['product_id'])->where('unique', $params['unique'])->find(); } - $stockIn = 0; - foreach ($product->attrValue as $item) { - $stockIn += $attrValue[$item->unique]['number']; - $item->update(['stock' => $item->stock + $attrValue[$item->unique]['number']], ['unique' => $item->unique]); + if (!$product || !$attrValue) { + throw new \Exception('商品或规格不存在'); } - $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('入库失败'); + $stockIn = $params['number'] ?? 0; + $price = $params['price'] ?? 0; + if (!empty($params['order_id'])) { + //采购订单导入 + $orderMerId = StoreOrder::where('order_id', $params['order_id'])->value('mer_id'); + $orderProduct = StoreOrderProduct::where('order_id', $params['order_id'])->where('product_id', $params['order_product_id'])->where('product_sku', $params['order_unique'])->find(); + if (empty($orderProduct) || $orderProduct->is_imported == 1) { + throw new \Exception('订单商品不存在或已入库'); + } + $stockIn = $orderProduct['product_num'] ?? 0; + $price = $orderProduct['product_price'] ?? 0; + $supplierMerId = $orderMerId ?? 0; } - $product->stock = $stockIn + $product->stock; - if (!$product->save()) { - throw new \Exception('入库失败'); + if ($stockIn > 0) { + $attrValue->stock = $attrValue->stock + $stockIn; + $attrValue->save(); + $product->stock = $stockIn + $product->stock; + if (!$product->save()) { + throw new \Exception('商品库存保存失败'); + } + $model = new PurchaseRecord(); + $data = [ + 'order_id' => $params['order_id'] ?? 0, + 'order_product_id' => $params['order_product_id'] ?? 0, + 'product_id' => $product->product_id, + 'number' => $stockIn, + 'order_unique' => $params['order_unique'] ?? '', + 'unique' => $attrValue['unique'], + 'price' => $price, + 'mer_id' => $product->mer_id, + 'supplier_mer_id' => $supplierMerId, + ]; + if (!$model->save($data)) { + throw new \Exception('入库失败'); + } + if (!$orderProduct->save(['is_imported' => 1])) { + throw new \Exception('订单商品更新出错'); + } } Db::commit(); } catch (\Exception $e) { @@ -2306,7 +2345,7 @@ class ProductRepository extends BaseRepository * @return bool * @throws \Exception */ - public function import($product_id, $user) + public function import($product_id, $user, $unique = null) { $mer_id = Db::name('store_service')->where('uid', $user['uid'])->where('status', 1)->value('mer_id'); if ($mer_id == 0) { @@ -2314,14 +2353,19 @@ class ProductRepository extends BaseRepository } $find = Db::name('store_product')->where('product_id', $product_id)->find(); if ($find) { - if ($find['product_type'] != 0) { + if (!in_array($find['product_type'], [0, 98])) { throw new \Exception('该商品不是普通商品'); } $exist = Db::name('store_product')->where('old_product_id', $product_id)->where('mer_id', $mer_id)->find(); if ($exist) { throw new \Exception('已经导入过该商品了'); } - $find['attrValue'] = Db::name('store_product_attr_value')->where('product_id', $find['product_id'])->field('image,price,cost,ot_price,svip_price,stock,bar_code,weight,volume')->select(); + $attr = Db::name('store_product_attr')->where('product_id', $find['product_id'])->field('attr_name,attr_values,type')->select(); + foreach ($attr as $item) { + $find['attr'][] = ['attr_name' => $item['attr_name'], 'detail' => explode('-!-', $item['attr_values'])]; + } + $where = empty($unique) ? ['product_id' => $find['product_id']] : ['product_id' => $find['product_id'], 'unique' => $unique]; + $find['attrValue'] = Db::name('store_product_attr_value')->where($where)->field('image,price,cost,ot_price,svip_price,0 as stock,bar_code,weight,volume,detail')->select(); $find['content'] = Db::name('store_product_content')->where('product_id', $find['product_id'])->value('content'); $find['is_show'] = 0; $find['mer_id'] = $mer_id; @@ -2331,7 +2375,6 @@ class ProductRepository extends BaseRepository $find['extend'] = []; $find['param_temp_id'] = []; $find['mer_labels'] = []; - $find['attr'] = []; $find['delivery_way'] = [0 => "2"]; $find["guarantee_template_id"] = ""; $find['product_type'] = 0; @@ -2346,4 +2389,23 @@ class ProductRepository extends BaseRepository return $this->create($find, 0); } + /** + * 仅导入规格值 + * @param $oldProductId + * @param $product + * @param $unique + * @return false + */ + public function importAttrValue($oldProductId, $product, $unique) + { + $product['attrValue'] = ProductAttrValue::where(['product_id' => $oldProductId, 'unique' => $unique])->field('image,price,cost,ot_price,svip_price,0 as stock,bar_code,weight,volume,detail')->select(); + $settleParams = $this->setAttrValue($product, $product['product_id'], 0, 0, $product['mer_id']); + if (!empty($settleParams['attrValue'])) { + if (ProductAttrValue::getInstance()->insert($settleParams['attrValue'][0]) !== false) { + return $settleParams['attrValue'][0]['unique']; + } + } + return false; + } + } diff --git a/app/controller/api/server/StoreProduct.php b/app/controller/api/server/StoreProduct.php index 32f6e137..ba15ae7f 100644 --- a/app/controller/api/server/StoreProduct.php +++ b/app/controller/api/server/StoreProduct.php @@ -219,13 +219,13 @@ class StoreProduct extends BaseController /** * 商品入库 - * @return \think\response\Json + * @return mixed * @throws \Exception */ public function stockIn() { - $param = $this->request->param(); - $this->repository->stockIn($param); + $params = $this->request->param(); + $this->repository->stockIn($this->merId, $params); return app('json')->success('入库成功'); }