'bigint',// 'checkCode' =>'varchar',//盘点编号 'product_id' =>'bigint',//财务商品id 'check_num' =>'decimal',//盘点数量 'diff_num' =>'decimal',//盈亏数量 'stock_num'=>'decimal',//盘点库存数量' 'unit_price'=>'decimal',//单价 'is_diff' =>'tinyint',//1盘盈0盘亏 'check_type' =>'tinyint',//1数据盘点2调整单 'fz_date'=>'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{ $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)){ ProductStock::create([ 'product_id'=>$product->id, 'residue_stock'=>$model->diff_num, 'total_stock'=>$model->diff_num, ]); }else{ if($product->residue_stock!= bcsub($model->check_num,$model->diff_num,8)) throw new \think\Exception('盘点数量与库存数量不匹配'); $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){ ProductStock::create([ 'product_id'=>$new->id, 'residue_stock'=>$model->diff_num, 'total_stock'=>$model->diff_num, ]); }else throw new \think\Exception('新增商品失败'); $id = $new->id; } }else{ if($product->residue_stock!=bcsub($model->check_num,$model->diff_num,8)) throw new \think\Exception("盘点数量与库存数量不匹配"); $up=$product->ProductStock->save(['residue_stock'=>bcadd($product->residue_stock,$model->diff_num,8)]); 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)->save($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); } } }