TradePool.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\facade\Log;use think\Model;
  4. class TradePool extends Base
  5. {
  6. //设置字段信息
  7. protected $schema = [
  8. 'id' =>'bigint',//
  9. 'tradNo' =>'varchar',//资金编号
  10. 'logNo' =>'varchar',//资金交易编号
  11. 'platform_type' =>'tinyint',//0无属性 1ToB 2ToC
  12. 'customerNo' =>'varchar',//客户公司
  13. 'companyNo' =>'varchar',//业务公司编号
  14. 'trade_time' =>'datetime',//交易时间
  15. 'total_fee' =>'decimal',//总额
  16. 'remark' =>'varchar',//备注
  17. 'tag_type' =>'int',//资金类型 0 认领自己 1平台衍生资金认领
  18. 'relaNo' =>'varchar',//关联平台衍生资金编号
  19. 'is_del' =>'int',//
  20. 'status' =>'varchar',//状态 1待审核2审核通过3 驳回
  21. 'apply_name' =>'varchar',//
  22. 'apply_id' =>'int',//
  23. 'addtime' =>'datetime',//
  24. 'updatetime' =>'datetime',//
  25. ];
  26. protected $createTime='addtime';
  27. protected $updateTime='updatetime';
  28. public function Trade(){
  29. return $this->belongsTo(Trade::class,"tradNo","tradNo")
  30. ->bind(["trade_out","trade_in","trade_bank","companyName","pcompanyNo"=>"companyNo","customerName","pcustomerNo"=>"customerNo","ptotal_fee"=>"total_fee","trade_type","balance","used_fee","poCode","NdCode",'pcCode']);
  31. }
  32. public function Lib(){
  33. return $this->belongsTo(TradeLib::class,"tradNo","tradNo")->bind(['palt_po','skuCode','goodName','goodNum','platPrice','plat_total','tax','cat_code','cat_name','comPrice','com_total']);
  34. }
  35. public static function onAfterInsert(Model $model) : void{
  36. if($model->status==2 && $model->tag_type==0){
  37. self::makeChangeOrder($model->logNo,2);
  38. }
  39. }
  40. public static function onAfterUpdate(Model $model) : void{
  41. $changedData = $model->getChangedData();
  42. if(isset($changedData['status'])&& $model->tag_type==0){
  43. self::makeChangeOrder($model->logNo,$changedData['status']);
  44. }
  45. }
  46. //修改衍生数据
  47. public static function makeChangeOrder($logNo,$status){
  48. $info = self::with(["Trade","Lib"])->where("logNo",$logNo)->findOrEmpty();
  49. Log::info("修改衍生数据".json_encode($info));
  50. if($info->isEmpty()) throw new \Exception("数据不存在");
  51. if($info->trade_type==0) return;
  52. switch ($status){
  53. case 2:
  54. self::createTrade($info);
  55. break;
  56. case 4:
  57. self::returnTrade($info);
  58. break;
  59. default:
  60. throw new \Exception("状态错误");
  61. }
  62. }
  63. public static function createTrade($info){
  64. $assoc = Assoc::alias('a')
  65. ->leftJoin('qrd_info b','a.orderCode = b.sequenceNo')
  66. ->where('a.viceCode',$info->logNo)
  67. ->where(['a.status'=>2,'a.is_del'=>0,"a.type"=>2])
  68. ->where('b.cxCode','<>','')
  69. ->field('a.cancel_fee,a.order_total,b.sequenceNo,b.cxCode')
  70. ->findOrEmpty();
  71. if($assoc->isEmpty()) throw new \Exception('数据不存在');
  72. $qrdArr = QrdInfo::where(['cxCode'=>$assoc->cxCode,'is_comon'=>0,'is_del'=>0])
  73. ->field('totalPrice,wpay_fee,apay_fee,platform_type,companyNo,customerNo,sequenceNo,id')->findOrEmpty();
  74. if($qrdArr->isEmpty()) throw new \Exception($assoc->cxCode.'对应关联销售单数据不存在');
  75. Log::info("订单数据".json_encode($qrdArr,JSON_UNESCAPED_UNICODE));
  76. $orderLv= bcdiv($assoc->cancel_fee,$assoc->order_total,8);
  77. $trade_fee=round(bcmul($qrdArr->totalPrice,$orderLv,8),2);
  78. $temp=['logNo' => makeNo('TRC'),
  79. 'tradNo' => $info->tradNo,
  80. 'platform_type' => $qrdArr->platform_type,
  81. 'companyNo' => $qrdArr->companyNo,
  82. 'customerNo' => $qrdArr->customerNo,
  83. 'apply_id' =>0,
  84. 'apply_name' => 'system',
  85. 'trade_time' => $info->trade_time,
  86. 'total_fee' => $trade_fee,
  87. "tag_type"=>1,//1平台衍生
  88. 'relaNo' => $info->logNo,
  89. 'status' => 2,//2审核通过
  90. ];
  91. $Asoc=[
  92. 'assocNo' =>makeNo('AS'),
  93. 'apply_id' =>0,
  94. 'apply_name' =>'system',
  95. 'type' => 2,
  96. 'orderCode' => $qrdArr->sequenceNo,
  97. 'customerNo' => $qrdArr->customerNo,
  98. 'viceCode' => $temp['logNo'],
  99. 'order_total' =>$qrdArr->totalPrice,
  100. 'vice_total' => $info->plat_total,
  101. 'cancel_fee' =>$trade_fee,
  102. 'status' => 2,//认领通过
  103. ];
  104. if($qrdArr->wpay_fee<$trade_fee) throw new \Exception("{$qrdArr->sequenceNo}待支付金额不足,主单号:{$assoc->cxCode}");
  105. $qrdArr->wpay_fee-= $trade_fee;
  106. $qrdArr->apay_fee+= $trade_fee;
  107. $qrdArr->pay_status= ($qrdArr->wpay_fee==0 &&$qrdArr->pay_fee==0) ?3:2;
  108. $qrdArr->status=1;
  109. $up=$qrdArr->save();
  110. if(!$up) throw new \Exception($qrdArr->sequenceNo.'订单数据修改失败,主单号:'.$assoc->cxCode);
  111. $asse= Assoc::create($Asoc);
  112. if($asse->isEmpty()) throw new \Exception($qrdArr->sequenceNo.'关联数据创建失败,主单号:'.$assoc->cxCode);
  113. $trade= self::create($temp);
  114. if($trade->isEmpty()) throw new \Exception($qrdArr->sequenceNo.'资金数据创建失败,主单号:'.$assoc->cxCode);
  115. $info->relaNo=$trade->logNo;
  116. $info->save();
  117. Log::info('订单数据'.self::getLastSql());
  118. }
  119. public static function returnTrade($info){
  120. $return = TradeReturn::where(["logNo"=>$info->logNo,"status"=>1])->findOrEmpty();
  121. Log::info('订单数据'.json_encode($return->toArray(),JSON_UNESCAPED_UNICODE));
  122. if($return->isEmpty()) throw new \Exception("{$info->logNo}退款记录未找到");
  123. $trade = self::where(["relaNo"=>$info->logNo,"tag_type"=>1,"status"=>2,"tradNo"=>$info->tradNo,"is_del"=>0])->findOrEmpty();
  124. Log::info('订单数据'.json_encode($trade->toArray(),JSON_UNESCAPED_UNICODE));
  125. if($trade->isEmpty()) throw new \Exception("未找到关联资金数据");
  126. $assoc = Assoc::alias('a')
  127. ->leftJoin('qrd_info b','a.orderCode = b.sequenceNo')
  128. ->where('a.viceCode',$trade->logNo)
  129. ->where(['a.status'=>2,'a.is_del'=>0,'a.type'=>2])
  130. ->where('b.cxCode','<>','')
  131. ->field('a.cancel_fee,a.orderCode,a.viceCode,a.id,a.status,b.cxCode')
  132. ->findOrEmpty();
  133. if($assoc->isEmpty()) throw new \Exception($trade->logNo.'关联订单数据不存在');
  134. $qrdArr = QrdInfo::where(['sequenceNo'=>$assoc->orderCode,'is_comon'=>0,'is_del'=>0])
  135. ->field('totalPrice,wpay_fee,pay_fee,apay_fee,inv_status,platform_type,companyNo,customerNo,sequenceNo,id')->findOrEmpty();
  136. if($qrdArr->isEmpty()) throw new \Exception($qrdArr->sequenceNo.'订单数据不存在,主单号:'.$assoc->cxCode);
  137. Assoc::CheckTrad( $assoc->viceCode,4);
  138. $returns=[
  139. 'returnCode'=>makeNo('RTA'),
  140. 'logNo'=>$trade->logNo,
  141. 'companyNo'=>$trade->companyNo,
  142. 'tradNo'=>$trade->tradNo,
  143. 'return_img'=>'',
  144. 'type'=>2,
  145. 'apply_id'=>'0',
  146. 'apply_name'=>'system',
  147. 'return_reason'=>$return->return_reason,
  148. 'remark'=>$return->remark,
  149. 'status'=>1];
  150. $trade->status=4;
  151. $up=$trade->save();
  152. if(!$up) throw new \Exception('资金数据修改失败');
  153. $TradeReturn= TradeReturn::create($returns);
  154. if($TradeReturn->isEmpty()) throw new \Exception('退款记录创建失败');
  155. }
  156. }