调试商品库存入库
This commit is contained in:
parent
28280cccd2
commit
5e9b8d6968
@ -43,7 +43,7 @@ class ProductAttrValue extends BaseModel
|
||||
|
||||
public function getDetailAttr($value)
|
||||
{
|
||||
return json_decode($value);
|
||||
return json_decode($value, true);
|
||||
}
|
||||
|
||||
public function product()
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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('入库成功');
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user