Refund.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\OrderRefundService;
  4. use think\db\Query;
  5. use think\Model;
  6. /**
  7. * 短信验证码
  8. * @property Orders orders
  9. * @property User user
  10. * @property int refund_status
  11. * @method static static|Query FilterRefund()
  12. */
  13. class Refund Extends Model
  14. {
  15. protected $name='order_info_refund';
  16. const REFUND_ING=10;
  17. const REFUND_PASS=20;
  18. const REFUND_REJECT=30;
  19. const REFUND_CANCEL=40;
  20. public static $refundStatus=[
  21. self::REFUND_ING=>'申请处理中',
  22. self::REFUND_PASS=>'申请通过',
  23. self::REFUND_REJECT=>'申请驳回',
  24. self::REFUND_CANCEL=>'已取消',
  25. ];
  26. /**
  27. * @return string[]
  28. */
  29. public static function getRefundStatus(): array
  30. {
  31. return self::$refundStatus;
  32. }
  33. const REFUND_TYPE_MONEY=1;
  34. const REFUND_TYPE_ALL=2;
  35. const REFUND_TYPE_GOODS=3;
  36. public static $refundTypes=[
  37. self::REFUND_TYPE_MONEY=>'仅退款',
  38. self::REFUND_TYPE_ALL=>'退款退货',
  39. self::REFUND_TYPE_GOODS=>'仅退货',
  40. ];
  41. public static $refundTypeGoods=[self::REFUND_TYPE_ALL,self::REFUND_TYPE_GOODS];
  42. public static $refundTypeMoney=[self::REFUND_TYPE_MONEY,self::REFUND_TYPE_ALL];
  43. public static $reasons=[
  44. 1=>'质量问题',
  45. 2=>'卖家发错货',
  46. ];
  47. /**
  48. * @return string[]
  49. */
  50. public static function getReasons(): array
  51. {
  52. $reasons=self::$reasons;
  53. $arr=[];
  54. foreach ($reasons as $key=>$value){
  55. $arr[$key]=compact('key','value');
  56. }
  57. return $arr;
  58. }
  59. /**
  60. * @return string[]
  61. */
  62. public static function getRefundTypes(): array
  63. {
  64. return self::$refundTypes;
  65. }
  66. /**
  67. * @return int[]
  68. */
  69. public static function getRefundTypeGoods(): array
  70. {
  71. return self::$refundTypeGoods;
  72. }
  73. /**
  74. * @return int[]
  75. */
  76. public static function getRefundTypeMoney(): array
  77. {
  78. return self::$refundTypeMoney;
  79. }
  80. protected $autoWriteTimestamp=true;
  81. public function orderInfo(){
  82. return $this->belongsTo(OrderInfo::class);
  83. }
  84. public function orders(){
  85. return $this->belongsTo(Orders::class,'order_id');
  86. }
  87. public function user(){
  88. return $this->belongsTo(User::class);
  89. }
  90. public function allowCancel(){
  91. return in_array($this['refund_status'],[
  92. self::REFUND_ING,
  93. ]);
  94. }
  95. public function makeCancel(){
  96. $this['refund_status']=self::REFUND_CANCEL;
  97. $this->save();
  98. OrderInfo::where('id',$this['order_info_id'])->update([
  99. 'refund_id'=>null,
  100. ]);
  101. }
  102. public function allowAudit(){
  103. return $this['refund_status']==self::REFUND_ING;
  104. }
  105. #退款相关
  106. /**
  107. * 退款金额
  108. */
  109. public function getRefundAmount(){
  110. return $this['amount'];
  111. }
  112. public function refundResult($succ,$remark=''){
  113. if(is_bool($succ)){
  114. $this['pay_status']=$succ?1:2;
  115. }else{
  116. $this['pay_status']=$succ;
  117. }
  118. $this['pay_remark']=$remark;
  119. $this->save();
  120. }
  121. public function isRefundMoney(){
  122. return in_array($this['refund_type'],self::getRefundTypeMoney());
  123. }
  124. #end
  125. public function makeAudit($pass){
  126. $this['refund_status']=$pass?self::REFUND_PASS:self::REFUND_REJECT;
  127. $refundMoney=false;
  128. if($pass){
  129. #如果退货
  130. if(in_array($this['refund_type'],self::getRefundTypeGoods())){
  131. $this->orderInfo()->update(['is_return_goods'=>1]);
  132. }
  133. #如果退款,执行退款
  134. if(in_array($this['refund_type'],self::getRefundTypeMoney())){
  135. $this['order_no']=order_no('TK');
  136. $refundMoney=true;
  137. }
  138. }
  139. if(!$this->save()){
  140. throw_user('保存失败');
  141. }
  142. if($refundMoney){
  143. $payment=$this->orders->payment??null;
  144. if($payment){
  145. $refund=new OrderRefundService();
  146. $refund->setPayment($payment);
  147. $refund->setRefund($this);
  148. $refund->setBody("订单[{$this->orders->order_no}]退款");
  149. $refund->pay();
  150. }
  151. }
  152. SiteMsg::sendMsg(
  153. $pass?SiteMsg::TYPE_ORDER_REFUND_PASS:SiteMsg::TYPE_ORDER_REFUND_REJECT,
  154. $this->user
  155. );
  156. }
  157. /**
  158. * 售后中及已售后的
  159. */
  160. public function scopeFilterRefund(Query $query){
  161. $query->whereBetween("{$this->getTable()}.refund_status",[self::REFUND_ING,self::REFUND_PASS]);
  162. }
  163. protected static function init()
  164. {
  165. self::beforeInsert(function (self $refund){
  166. $refund['refund_status']=self::REFUND_ING;
  167. });
  168. self::afterUpdate(function (self $refund){
  169. if($refund->refund_status==self::REFUND_PASS && $refund->isRefundMoney()){
  170. Transaction::addTransaction($refund);
  171. }
  172. });
  173. }
  174. }