InvoiceOrder.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. Log::info('InvoiceOrder数据:onBeforeDelete:'.json_encode($model->id,JSON_UNESCAPED_UNICODE));
  71. $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
  72. Log::info('InvoiceOrder数据:onBeforeDelete'.json_encode($info->toArray(),JSON_UNESCAPED_UNICODE));
  73. if($info->isEmpty())return ;
  74. if(in_array($info->status,[1,2])) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  75. Log::info('InvoiceOrder数据:onBeforeDelete:'.InvoiceOrder::getLastsql());
  76. }
  77. //订单关联初始化金额
  78. public static function orderIn($code,$fee){
  79. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  80. Log::info('InvoiceOrder跟后数据orderIn:cgd:'. $cgdinfo->winv_fee."----".$fee);
  81. if(!$cgdinfo->isEmpty() && $cgdinfo->winv_fee>=strval($fee)){
  82. $cgdinfo->winv_fee = bcsub($cgdinfo->winv_fee,strval($fee),2);
  83. $cgdinfo->save();
  84. }else throw new \Exception("关联金额{$fee}大于采购单{$code}未开票金额{$cgdinfo->winv_fee}");
  85. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  86. if(!$payinfo->isEmpty()){
  87. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  88. Log::info('InvoiceOrder跟后数据orderIn:pay' . $pay->winv_fee . '----' . $fee);
  89. if(!$pay->isEmpty() && $pay->winv_fee>=strval($fee)){
  90. $pay->winv_fee=bcsub($pay->winv_fee,strval($fee),2);
  91. $pay->inv_fee = bcadd($pay->inv_fee,strval($fee),2);
  92. $pay->save();
  93. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}未开票金额{$pay->winv_fee}");
  94. }
  95. }
  96. //流程未结束驳回中断
  97. public static function orderOut($code,$fee){
  98. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  99. if(!$cgdinfo->isEmpty()){
  100. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  101. $cgdinfo->save();
  102. }
  103. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  104. if(!$payinfo->isEmpty()){
  105. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  106. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  107. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  108. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  109. $pay->save();
  110. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
  111. }
  112. }
  113. public static function orderFinish($code,$fee){
  114. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  115. if(!$cgdinfo->isEmpty()){
  116. $cgdinfo->ainv_fee = bcadd($cgdinfo->ainv_fee,strval($fee),2);
  117. $cgdinfo->inv_status = $cgdinfo->ainv_fee==$cgdinfo->totalPrice&& $cgdinfo->winv_fee==0 ?3:2;
  118. $cgdinfo->save();
  119. }
  120. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  121. if(!$payinfo->isEmpty()){
  122. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  123. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  124. $pay->ainv_fee=bcadd($pay->ainv_fee,strval($fee),2);
  125. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  126. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
  127. $pay->save();
  128. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
  129. }
  130. }
  131. public static function orderCancel($code,$fee){
  132. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  133. if(!$cgdinfo->isEmpty()){
  134. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  135. $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,strval($fee),2);
  136. $cgdinfo->inv_status = $cgdinfo->ainv_fee==0 && $cgdinfo->winv_fee==$cgdinfo->totalPrice?1:2;
  137. $cgdinfo->save();
  138. }
  139. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  140. if(!$payinfo->isEmpty()){
  141. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  142. if(!$pay->isEmpty()){
  143. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  144. $pay->ainv_fee = bcsub($pay->ainv_fee,strval($fee),2);
  145. $pay->inv_status = $pay->ainv_fee==0?1:2;
  146. $pay->save();
  147. }
  148. }
  149. }
  150. }