'bigint', 'item_id' => 'bigint', 'type' => 'tinyint', //1 出入库 2计提处理 'remark' => 'varchar', 'apply_id' => 'int', 'apply_name' => 'varchar', 'status' => 'tinyint', 'addtime' => 'datetime', 'updatetime' => 'datetime' ]; public static $status=[ '1'=>'待审批', '2'=>'审批通过', '3'=>'审批驳回' ]; public function FinancialManager(){ return $this->hasOne(FinancialManager::class,'item_id','id'); } public function FinancialTz(){ return $this->hasOne(FinancialTz::class,'item_id','id'); } public static function onAfterInsert(Model $model): void { try { if($model['type']==1){ FinancialCancel::cancelManager($model['item_id'],$model); }else{ FinancialCancel::cancelTz($model['item_id'],$model); } } catch (\Exception $e) { throw new \Exception($e->getMessage()); } } public static function cancelManager($managerId,$cancelInfo){ $ManagerInfo=FinancialManager::with(['ProductRela'=>['Product']])->where('id',$managerId)->findOrEmpty(); if($ManagerInfo->isEmpty()) throw new \Exception('数据不存在'); if($ManagerInfo->status==3){ $IFz=self::checkTz($managerId); if(!$IFz) throw new \Exception('计提数据未处理'); } if($ManagerInfo->ProductRela->isEmpty()) throw new \Exception('商品不存在'); $ProductInfo=$ManagerInfo->ProductRela; $up=[]; $ProductInfo->each(function ($item) use (&$up){ $info = FinancialProducts::where('id',$item->product_id)->findOrEmpty(); if($info->isEmpty()) throw new \Exception($item->goodName.'商品不存在'); if($item->status==1){ if($item->type==1)$up[]=ProductStock::AddSingleStock($info,$item->num); if($item->type==2){ if($info->residue_stock<$item->num) throw new \Exception($item->goodName.'库存不足'); $up[]=ProductStock::SubSingleStock($info,$item->num); } } }); if(empty($up)) throw new \Exception('库存修改数据不存在'); $log=[]; $type=[1=>8,2=>7]; foreach ($up as $k=>$v){ $log[]=[ "type"=>$type[$v['type']], 'order_item_id'=>$cancelInfo->id, 'product_id'=>$v->product_id, 'num'=>$v->num, "fz_date"=>$v->fz_date, 'unit_price'=>$v->unit_price, 'rate'=>$v->rate, 'apply_id'=>$cancelInfo->apply_id, 'apply_name'=>$cancelInfo->apply_name, ]; } (new ProductOnlog)->saveAll($log); $ManagerInfo->status=5; // 5 取消 $ManagerInfo->save(); } public static function checkTz($managerId){ $ManagerInfo=FinancialTz::where('manager_id',$managerId)->findOrEmpty(); if($ManagerInfo->isEmpty()) throw new \Exception($managerId.'计提数据不存在'); if($ManagerInfo->status!=4) return false; return true; } public static function cancelTz($tzId,$cancelInfo){ $TzInfo=FinancialTz::with(['ProductTz'=>['Product']])->where('id',$tzId)->findOrEmpty(); if($TzInfo->isEmpty()) throw new \Exception('计提数据不存在'); $status = $TzInfo->status; $ProductInfo=$TzInfo->ProductTz; $up=[]; if($status==2){ $ProductInfo->each(function ($item) use (&$up,$status){ $info = FinancialProducts::where('id',$item->product_id)->findOrEmpty(); if($item->type==2)$up[]=ProductStock::AddSingleStock($info,$item->num); if($item->type==1){ if($info->residue_stock<$item->num) throw new \Exception($item->goodName.'库存不足'); $up[]=ProductStock::SubSingleStock($info,$item->num); } }); if(empty($up)) throw new \Exception('库存修改数据不存在'); $log=[]; $type=[1=>8,2=>7]; foreach ($up as $k=>$v){ $log[]=[ "type"=>$type[$v['type']], 'order_item_id'=>$cancelInfo->id, 'product_id'=>$v->product_id, 'num'=>$v->num, "fz_date"=>$v->fz_date, 'unit_price'=>$v->unit_price, 'rate'=>$v->rate, ]; } (new ProductOnlog)->saveAll($log); } $TzInfo->status=4; $TzInfo->save(); } }