FinancialCancel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\Model;
  4. class FinancialCancel extends Base
  5. {
  6. protected $schema = [
  7. 'id' => 'bigint',
  8. 'item_id' => 'bigint',
  9. 'type' => 'tinyint',
  10. 'remark' => 'varchar',
  11. 'apply_id' => 'int',
  12. 'apply_name' => 'varchar',
  13. 'status' => 'tinyint',
  14. 'addtime' => 'datetime',
  15. 'updatetime' => 'datetime'
  16. ];
  17. public static $status=[
  18. '1'=>'待审批',
  19. '2'=>'审批通过',
  20. '3'=>'审批驳回'
  21. ];
  22. public function FinancialManager(){
  23. return $this->hasOne(FinancialManager::class,'item_id','id');
  24. }
  25. public function FinancialTz(){
  26. return $this->hasOne(FinancialTz::class,'item_id','id');
  27. }
  28. public static function onAfterInsert(Model $model): void
  29. {
  30. try {
  31. if($model['type']==1){
  32. FinancialCancel::cancelManager($model['item_id'],$model);
  33. }else{
  34. FinancialCancel::cancelTz($model['item_id'],$model);
  35. }
  36. } catch (\Exception $e) {
  37. throw new \Exception($e->getMessage());
  38. }
  39. }
  40. public static function cancelManager($managerId,$cancelInfo){
  41. $ManagerInfo=FinancialManager::with(['ProductRela'=>['Product']])->where('id',$managerId)->findOrEmpty();
  42. if($ManagerInfo->isEmpty()) throw new \Exception('数据不存在');
  43. if($ManagerInfo->status==3){
  44. $IFz=self::checkTz($managerId);
  45. if(!$IFz) throw new \Exception('计提数据未处理');
  46. }
  47. if($ManagerInfo->ProductRela->isEmpty()) throw new \Exception('商品不存在');
  48. $ProductInfo=$ManagerInfo->ProductRela;
  49. $up=[];
  50. $ProductInfo->each(function ($item) use (&$up){
  51. $info = FinancialProducts::where('id',$item->product_id)->findOrEmpty();
  52. if($info->isEmpty()) throw new \Exception($item->goodName.'商品不存在');
  53. if($item->status==1){
  54. if($item->type==1)$up[]=ProductStock::AddSingleStock($info,$item->num);
  55. if($item->type==2)$up[]=ProductStock::SubSingleStock($info,$item->num);
  56. }
  57. });
  58. if(empty($up)) throw new \Exception('库存修改数据不存在');
  59. $log=[];
  60. $type=[1=>8,2=>7];
  61. foreach ($up as $k=>$v){
  62. $log[]=[
  63. "type"=>$type[$v['type']],
  64. 'order_item_id'=>$cancelInfo->id,
  65. 'product_id'=>$v->product_id,
  66. 'num'=>$v->num,
  67. "fz_date"=>$v->fz_date,
  68. 'unit_price'=>$v->unit_price,
  69. 'rate'=>$v->rate,
  70. 'apply_id'=>$cancelInfo->apply_id,
  71. 'apply_name'=>$cancelInfo->apply_name,
  72. ];
  73. }
  74. (new ProductOnlog)->saveAll($log);
  75. $ManagerInfo->status=5; // 5 取消
  76. $ManagerInfo->save();
  77. }
  78. public static function checkTz($managerId){
  79. $ManagerInfo=FinancialTz::where('manager_id',$managerId)->findOrEmpty();
  80. if($ManagerInfo->isEmpty()) throw new \Exception($managerId.'计提数据不存在');
  81. if($ManagerInfo->status!=4) return false;
  82. return true;
  83. }
  84. public static function cancelTz($tzId,$cancelInfo){
  85. $TzInfo=FinancialTz::with(['ProductTz'=>['Product']])->where('id',$tzId)->findOrEmpty();
  86. if($TzInfo->isEmpty()) throw new \Exception('计提数据不存在');
  87. $status = $TzInfo->status;
  88. $ProductInfo=$TzInfo->ProductTz;
  89. $up=[];
  90. if($status==2){
  91. $ProductInfo->each(function ($item) use (&$up,$status){
  92. $info = FinancialProducts::where('id',$item->product_id)->findOrEmpty();
  93. if($item->type==2)$up[]=ProductStock::AddSingleStock($info,$item->num);
  94. if($item->type==1)$up[]=ProductStock::SubSingleStock($info,$item->num);
  95. });
  96. if(empty($up)) throw new \Exception('库存修改数据不存在');
  97. $log=[];
  98. $type=[1=>8,2=>7];
  99. foreach ($up as $k=>$v){
  100. $log[]=[
  101. "type"=>$type[$v['type']],
  102. 'order_item_id'=>$cancelInfo->id,
  103. 'product_id'=>$v->product_id,
  104. 'num'=>$v->num,
  105. "fz_date"=>$v->fz_date,
  106. 'unit_price'=>$v->unit_price,
  107. 'rate'=>$v->rate,
  108. ];
  109. }
  110. (new ProductOnlog)->saveAll($log);
  111. }
  112. $TzInfo->status=4;
  113. $TzInfo->save();
  114. }
  115. }