添加商品采购入库

This commit is contained in:
luofei 2023-06-26 14:44:29 +08:00
parent d524565ce1
commit 11ba0dc8d3
4 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,36 @@
<?php
namespace app\common\model\store\product;
use app\common\model\BaseModel;
use app\common\model\system\merchant\Merchant;
class PurchaseRecord extends BaseModel
{
public static function tablePk(): string
{
return 'id';
}
public static function tableName(): string
{
return 'purchase_record';
}
public function product()
{
return $this->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');
}
}

View File

@ -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;
}
}

View File

@ -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('入库成功');
}
}

View File

@ -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');