InvoiceOrder.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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->inv_status=2;
  84. $cgdinfo->save();
  85. }else throw new \Exception("关联金额{$fee}大于采购单{$code}未开票金额{$cgdinfo->winv_fee}");
  86. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  87. if(!$payinfo->isEmpty()){
  88. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  89. Log::info('InvoiceOrder跟后数据orderIn:pay' . $pay->winv_fee . '----' . $fee);
  90. if(!$pay->isEmpty() && $pay->winv_fee>=strval($fee)){
  91. $pay->winv_fee=bcsub($pay->winv_fee,strval($fee),2);
  92. $pay->inv_fee = bcadd($pay->inv_fee,strval($fee),2);
  93. $pay->inv_status=2;
  94. $pay->save();
  95. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}未开票金额{$pay->winv_fee}");
  96. }
  97. }
  98. //流程未结束驳回中断
  99. public static function orderOut($code,$fee){
  100. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  101. if(!$cgdinfo->isEmpty()){
  102. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  103. $cgdinfo->save();
  104. }
  105. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  106. if(!$payinfo->isEmpty()){
  107. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  108. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  109. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  110. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  111. $pay->inv_status=$pay->ainv_fee==0 && $pay->inv_fee==0?1:2;
  112. $pay->save();
  113. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
  114. }
  115. }
  116. public static function orderFinish($code,$fee){
  117. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  118. if(!$cgdinfo->isEmpty()){
  119. $cgdinfo->ainv_fee = bcadd($cgdinfo->ainv_fee,strval($fee),2);
  120. $cgdinfo->inv_status = $cgdinfo->ainv_fee==$cgdinfo->totalPrice-$cgdinfo->inv_tag_fe&& $cgdinfo->winv_fee==0 ?3:2;
  121. $cgdinfo->save();
  122. }
  123. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  124. if(!$payinfo->isEmpty()){
  125. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  126. if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
  127. $pay->ainv_fee=bcadd($pay->ainv_fee,strval($fee),2);
  128. $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
  129. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
  130. $pay->save();
  131. }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
  132. }
  133. }
  134. public static function orderCancel($code,$fee){
  135. $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
  136. if(!$cgdinfo->isEmpty()){
  137. $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
  138. $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,strval($fee),2);
  139. $cgdinfo->inv_status = $cgdinfo->ainv_fee==0 && $cgdinfo->winv_fee==$cgdinfo->totalPrice-$cgdinfo->inv_tag_fee?1:2;
  140. $cgdinfo->save();
  141. }
  142. $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  143. if(!$payinfo->isEmpty()){
  144. $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
  145. if(!$pay->isEmpty()){
  146. $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
  147. $pay->ainv_fee = bcsub($pay->ainv_fee,strval($fee),2);
  148. $pay->inv_status = $pay->ainv_fee==0 && $pay->inv_fee==0?1:2;
  149. $pay->save();
  150. }
  151. }
  152. }
  153. public static function CheckOrder($code,&$data){
  154. $InvoiceOrder = InvoiceOrder::with(['ItemInfo'])->where(['code'=>$code,'status'=>[1,2]])->select();
  155. if(!$InvoiceOrder->isEmpty()){
  156. $hpinfo=[];
  157. foreach ($InvoiceOrder as $item){
  158. if(isset($hpinfo[$item->ItemInfo->invoiceCode]))continue;
  159. $hpinfo[$item->ItemInfo->invoiceCode] = $item->ItemInfo->invoiceCode;
  160. $hp = PayInvoice::with(['invoice'])->where(['hpNo'=>$item->ItemInfo->invoiceCode])->findOrEmpty();
  161. $data[]=['type'=>'发票池关联采购单','username'=> $hp['apply_name'],'orderCode'=>$hp['hpNo'],'companyName'=>$hp['companyName']];
  162. }
  163. }
  164. }
  165. }