123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace app\cxinv\model;
- use think\Model;class ProductCheck extends Base
- {
- protected $schema = [
- 'id' =>'bigint',
- 'checkCode' =>'varchar',
- 'product_id' =>'bigint',
- 'check_num' =>'decimal',
- 'diff_num' =>'decimal',
- 'stock_num'=>'decimal',
- 'unit_price'=>'decimal',
- 'is_diff' =>'tinyint',
- 'check_type' =>'tinyint',
- 'fz_date'=>'varchar',
- 'status' =>'tinyint',
- 'remark' =>'varchar',
- 'apply_id' =>'int',
- 'apply_name' =>'varchar',
- 'create_time' =>'datetime',
- 'update_time' =>'datetime',
- 'delete_time' =>'datetime',
- ];
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime='delete_time';
- public function product(){
- return $this->belongsTo('FinancialProducts','product_id','id');
- }
- public static function onAfterInsert(Model $model) : void{
- try{
- $product=FinancialProducts::with(['ProductStock'])->findOrEmpty($model->product_id);
- if($product->isEmpty()) throw new \think\Exception('商品不存在');
- $id = $product->id;
- if($model->is_diff==1){
- if(is_null($product->residue_stock)){
- $create=ProductStock::create([
- 'product_id'=>$product->id,
- 'residue_stock'=>$model->check_num,
- 'total_stock'=>$model->check_num,
- ]);
- if($create->isEmpty()) throw new \think\Exception($product->goodName.'库存记录创建失败');
- }else{
- if($product->residue_stock!= $model->stock_num) throw new \think\Exception($product->goodName.'盘点数量与库存数量不匹配');
- $data = $product->toArray();
- unset($data['id']);
- unset($data['create_time']);
- unset($data['update_time']);
- $data['apply_id'] = $model->apply_id;
- $data['apply_name'] = $model->apply_name;
- $new = FinancialProducts::create($data);
- if($new->id>0){
- $create=ProductStock::create([
- 'product_id'=>$new->id,
- 'residue_stock'=>$model->check_num,
- 'total_stock'=>$model->check_num,
- ]);
- if($create->isEmpty()) throw new \think\Exception($product->goodName.'库存记录创建失败');
- }else throw new \think\Exception('复制商品失败');
- $id = $new->id;
- }
- }else{
- if($product->residue_stock!=$model->stock_num) throw new \think\Exception("盘点数量与库存数量不匹配");
- $up=$product->ProductStock->save(['residue_stock'=>$model->check_num]);
- if($up==false) throw new \think\Exception("更新库存失败");
- }
- $data=[
- 'type'=>$model->is_diff==1?3:4,
- 'order_item_id'=>$model->id,
- 'product_id'=>$id,
- 'fz_date'=>$model->fz_date,
- 'num'=>abs($model->diff_num),
- 'unit_price'=>$product->unit_price,
- 'rate'=>$product->cat_tax,
- 'apply_id'=>$model->apply_id,
- 'apply_name'=>$model->apply_name,
- ];
- (new ProductOnlog)->create($data);
- if($model->check_type==2){
- $check=[
- 'checkCode'=>makeNo('CWTZ'),
- 'code'=>$model->checkCode,
- 'itemid'=>$id,
- "type"=>1,
- 'goodNo'=>$product->skuCode,
- 'goodType'=>$product->good_type,
- 'goodName'=>$product->goodName,
- 'spec'=>$product->spec,
- 'unit'=>$product->unit,
- 'company_code'=>$product->buyer_code,
- 'company_name'=>$product->buyer_name,
- 'num'=>$model->diff_num,
- 'price'=>$product->unit_price,
- 'subprice'=>$product->subunit_price,
- 'check_fee'=>$model->diff_fee,
- 'check_uid'=>$model->apply_id,
- 'fz_date'=>$model->fz_date,
- 'check_uname'=>$model->apply_name,
- ];
- FinancialCheck::create($check);
- }
- $model->status=1;
- $model->remark="";
- }catch (\Exception $exception){
- $model->status=2;
- $model->remark=$exception->getMessage();
- }
- $model->save();
- }
- }
|