123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace app\cxinv\model;
- use think\Model;
- class FinancialCancel extends Base
- {
- protected $schema = [
- 'id' => 'bigint',
- 'item_id' => 'bigint',
- 'type' => 'tinyint', //1 出入库 2计提处理
- 'remark' => 'varchar',
- 'apply_id' => 'int',
- 'apply_name' => 'varchar',
- 'status' => 'tinyint',
- 'addtime' => 'datetime',
- 'updatetime' => 'datetime'
- ];
- protected $createTime = 'addtime';
- protected $updateTime = 'updatetime';
- 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::with(['ProductStock'])->where('id',$item->product_id)->findOrEmpty();
- if($info->isEmpty()) throw new \Exception($item->goodName.'商品不存在');
- if($item->status==1){
- 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"=>$ManagerInfo->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::with(['ProductStock'])->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"=>$TzInfo->fz_date,
- 'unit_price'=>$v['unit_price'],
- 'rate'=>$v['rate'],
- 'apply_id'=>$cancelInfo->apply_id,
- 'apply_name'=>$cancelInfo->apply_name,
- ];
- }
- (new ProductOnlog)->saveAll($log);
- }
- $TzInfo->status=4;
- $TzInfo->save();
- }
- }
|