InvoiceOrder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\facade\Log;
  4. use think\Model;use think\model\concern\SoftDelete;
  5. class InvoiceOrder extends Base{
  6. use SoftDelete;
  7. protected $createTime='createTime';
  8. protected $updateTime='updateTime';
  9. protected $autoWriteTimestamp=true;
  10. protected $deleteTime='delete_time';
  11. protected $append=["merge_code","short_name","cat_code","cat_name","tax","inv_good_name"];
  12. public function ItemInfo(){
  13. return $this->hasOne('InvoiceItem','itemId','id');
  14. }
  15. public function getMergeCodeAttr($value,$data){
  16. return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("merge_code","");
  17. }
  18. public function getShortNameAttr($value,$data){
  19. return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("short_name","");
  20. }
  21. public function getCatCodeAttr($value,$data){
  22. return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("cat_code","");
  23. }
  24. public function getCatNameAttr($value,$data){
  25. return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("cat_name","");
  26. }
  27. public function getTaxAttr($value,$data){
  28. return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("tax","");
  29. }
  30. public function getInvGoodNameAttr($value,$data){
  31. return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("inv_good_name","");
  32. }
  33. public static function getNumAttr($val){
  34. return strval(floatval($val));
  35. }
  36. public static function setDiffInfoAttr($val){
  37. return json_encode($val,JSON_UNESCAPED_UNICODE);
  38. }
  39. public static function getDiffInfoAttr($val){
  40. return json_decode($val,true);
  41. }
  42. public static function getOrderInfo($code,$orderCode,$type){
  43. $payNo = PayInvoice::where(['hpNo'=>$code])->value("payNo","");
  44. $info = PayInfo::alias('a')
  45. ->join('cgd_info b','a.cgdNo=b.sequenceNo','left')
  46. ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=2','left')
  47. ->where([['a.status','=',1],['a.is_del','=',0],['a.payNo',"=",$payNo ],['a.cgdNo',"=",$orderCode]])
  48. ->field('b.sequenceNo,b.goodNo,b.goodName,(b.goodNum-thNum) as goodNum,b.goodPrice,b.totalPrice,b.ainv_fee,b.winv_fee,b.apay_fee,b.wpay_fee,
  49. c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name')
  50. ->findOrEmpty();
  51. return $info;
  52. }
  53. //回票流程结束未认证 状态3 退票认证结束 状态4
  54. public static function onAfterWrite( Model $model){
  55. $id= $model->id;
  56. $change = $model->getChangedData();
  57. Log::info('InvoiceOrder跟前数据:'.json_encode($model->id,JSON_UNESCAPED_UNICODE));
  58. $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
  59. Log::info('InvoiceOrder跟后数据:'.json_encode($info->toArray(),JSON_UNESCAPED_UNICODE));
  60. if($info->isEmpty())return ;
  61. if(empty($change)&&in_array($model->status,[1,2])) self::orderIn($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  62. if(!empty($change)&&isset($change['status'])){
  63. if($change['status']==3) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  64. if($change['status']==4) self::orderCancel($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  65. }
  66. Log::info('InvoiceOrder跟后数据:'.InvoiceOrder::getLastsql());
  67. }
  68. public static function onBeforeDelete($model){
  69. $id=$model->id;
  70. $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
  71. if(!$info->isEmpty())return ;
  72. if(in_array($info->status,[1,2])) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  73. }
  74. //订单关联初始化金额
  75. public static function orderIn($code,$fee){
  76. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  77. Log::info('InvoiceOrder跟后数据orderIn:cgd:'. $cgdinfo->winv_fee."----".$fee);
  78. if(!$cgdinfo->isEmpty() && $cgdinfo->winv_fee>=strval($fee)){
  79. $cgdinfo->winv_fee = bcsub($cgdinfo->winv_fee,strval($fee),2);
  80. $cgdinfo->save();
  81. }else throw new \Exception("关联金额{$fee}大于采购单{$code}未开票金额{$cgdinfo->winv_fee}");
  82. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  83. if(!$payinfo->isEmpty()){
  84. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  85. Log::info('InvoiceOrder跟后数据orderIn:pay' . $pay->winv_fee . '----' . $fee);
  86. if(!$pay->isEmpty() && $pay->winv_fee>=strval($fee)){
  87. $pay->winv_fee=bcsub($pay->winv_fee,strval($fee),2);
  88. $pay->inv_fee = bcadd($pay->inv_fee,strval($fee),2);
  89. $pay->save();
  90. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}未开票金额{$pay->winv_fee}");
  91. }
  92. }
  93. //流程未结束驳回中断
  94. public static function orderOut($code,$fee){
  95. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  96. if(!$cgdinfo->isEmpty()){
  97. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  98. $cgdinfo->save();
  99. }
  100. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  101. if(!$payinfo->isEmpty()){
  102. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  103. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  104. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  105. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  106. $pay->save();
  107. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
  108. }
  109. }
  110. public static function orderFinish($code,$fee){
  111. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  112. if(!$cgdinfo->isEmpty()){
  113. $cgdinfo->ainv_fee = bcadd($cgdinfo->ainv_fee,strval($fee),2);
  114. $cgdinfo->inv_status = $cgdinfo->ainv_fee==$cgdinfo->totalPrice&& $cgdinfo->winv_fee==0 ?3:2;
  115. $cgdinfo->save();
  116. }
  117. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  118. if(!$payinfo->isEmpty()){
  119. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  120. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  121. $pay->ainv_fee=bcadd($pay->ainv_fee,strval($fee),2);
  122. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  123. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
  124. $pay->save();
  125. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
  126. }
  127. }
  128. public static function orderCancel($code,$fee){
  129. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  130. if(!$cgdinfo->isEmpty()){
  131. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  132. $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,strval($fee),2);
  133. $cgdinfo->inv_status = $cgdinfo->ainv_fee==0 && $cgdinfo->winv_fee==$cgdinfo->totalPrice?1:2;
  134. $cgdinfo->save();
  135. }
  136. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  137. if(!$payinfo->isEmpty()){
  138. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  139. if(!$pay->isEmpty()){
  140. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  141. $pay->ainv_fee = bcsub($pay->ainv_fee,strval($fee),2);
  142. $pay->inv_status = $pay->ainv_fee==0?1:2;
  143. $pay->save();
  144. }
  145. }
  146. }
  147. }