FinancialCancel.php 4.8 KB

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