InvoiceOrder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if($type==1){
  44. $info=InvoiceGood::alias('a')
  45. ->join('qrd_info b','a.orderCode=b.sequenceNo','left')
  46. ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=1','left')
  47. ->where(["invNo"=>$code,"a.orderCode"=>$orderCode])
  48. ->where([['a.goodNum','>',0],['a.is_del','=',0]])
  49. ->field('b.sequenceNo,b.goodNo,b.goodName,a.goodNum,a.goodPrice,a.totalPrice,c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name')
  50. ->findOrEmpty();
  51. } else{
  52. $payNo = PayInvoice::where(['hpNo'=>$code])->value("payNo","");
  53. $info = PayInfo::alias('a')
  54. ->join('cgd_info b','a.cgdNo=b.sequenceNo','left')
  55. ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=2','left')
  56. ->where([['a.status','=',1],['a.is_del','=',0],['a.payNo',"=",$payNo ],['a.cgdNo',"=",$orderCode]])
  57. ->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,
  58. c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name')
  59. ->findOrEmpty();
  60. }
  61. return $info;
  62. }
  63. //回票流程结束未认证 状态3 退票认证结束 状态4
  64. public static function onAfterWrite( Model $model){
  65. $id= $model->id;
  66. $change = $model->getChangedData();
  67. Log::info('InvoiceOrder跟前数据:'.json_encode($model->id,JSON_UNESCAPED_UNICODE));
  68. $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
  69. Log::info('InvoiceOrder跟后数据:'.json_encode($info->toArray(),JSON_UNESCAPED_UNICODE));
  70. if($info->isEmpty())return ;
  71. if(empty($change)&&in_array($model->status,[1,2])) self::orderIn($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  72. if(!empty($change)&&isset($change['status'])){
  73. if($change['status']==3) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  74. if($change['status']==4) self::orderCancel($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  75. }
  76. Log::info('InvoiceOrder跟后数据:'.InvoiceOrder::getLastsql());
  77. }
  78. public static function onBeforeDelete($model){
  79. $id=$model->id;
  80. $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
  81. if(!$info->isEmpty())return ;
  82. if(in_array($info->status,[1,2])) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
  83. }
  84. //订单关联初始化金额
  85. public static function orderIn($code,$fee){
  86. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  87. if(!$cgdinfo->isEmpty() && $cgdinfo->winv_fee>=strval($fee)){
  88. $cgdinfo->winv_fee = bcsub($cgdinfo->winv_fee,strval($fee),2);
  89. $cgdinfo->save();
  90. }
  91. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  92. if(!$payinfo->isEmpty()){
  93. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  94. if(!$pay->isEmpty() && $pay->winv_fee>=strval($fee)){
  95. $pay->winv_fee=bcsub($pay->winv_fee,strval($fee),2);
  96. $pay->inv_fee = bcadd($pay->inv_fee,strval($fee),2);
  97. $pay->save();
  98. }
  99. }
  100. }
  101. //流程未结束驳回中断
  102. public static function orderOut($code,$fee){
  103. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  104. if(!$cgdinfo->isEmpty()){
  105. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  106. $cgdinfo->save();
  107. }
  108. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  109. if(!$payinfo->isEmpty()){
  110. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  111. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  112. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  113. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  114. $pay->save();
  115. }
  116. }
  117. }
  118. public static function orderFinish($code,$fee){
  119. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  120. if(!$cgdinfo->isEmpty()){
  121. $cgdinfo->ainv_fee = bcadd($cgdinfo->ainv_fee,strval($fee),2);
  122. $cgdinfo->inv_status = $cgdinfo->ainv_fee==$cgdinfo->totalPrice&& $cgdinfo->winv_fee==0 ?3:2;
  123. $cgdinfo->save();
  124. }
  125. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  126. if(!$payinfo->isEmpty()){
  127. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  128. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  129. $pay->ainv_fee=bcadd($pay->ainv_fee,strval($fee),2);
  130. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  131. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
  132. $pay->save();
  133. }
  134. }
  135. }
  136. public static function orderCancel($code,$fee){
  137. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  138. if(!$cgdinfo->isEmpty()){
  139. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  140. $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,strval($fee),2);
  141. $cgdinfo->inv_status = $cgdinfo->ainv_fee==0 && $cgdinfo->winv_fee==$cgdinfo->totalPrice?1:2;
  142. $cgdinfo->save();
  143. }
  144. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  145. if(!$payinfo->isEmpty()){
  146. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  147. if(!$pay->isEmpty()){
  148. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  149. $pay->ainv_fee = bcsub($pay->ainv_fee,strval($fee),2);
  150. $pay->inv_status = $pay->ainv_fee==0?1:2;
  151. $pay->save();
  152. }
  153. }
  154. }
  155. }