Refund.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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($status=null)
  12. * @method static static|Query FilterTs()
  13. */
  14. class Refund Extends Model
  15. {
  16. protected $name='order_info_refund';
  17. const REFUND_ING=10;
  18. const REFUND_PASS=20;
  19. const REFUND_REJECT=30;
  20. const REFUND_CANCEL=40;
  21. public static $refundStatus=[
  22. self::REFUND_ING=>'申请处理中',
  23. self::REFUND_PASS=>'申请通过',
  24. self::REFUND_REJECT=>'申请驳回',
  25. self::REFUND_CANCEL=>'已取消',
  26. ];
  27. /**
  28. * @return string[]
  29. */
  30. public static function getRefundStatus(): array
  31. {
  32. return self::$refundStatus;
  33. }
  34. const REFUND_TYPE_MONEY=1;
  35. const REFUND_TYPE_ALL=2;
  36. const REFUND_TYPE_GOODS=3;
  37. const REFUND_TYPE_CANCEL=4;
  38. const REFUND_TYPE_HHBX=5;
  39. public static $refundTypes=[
  40. self::REFUND_TYPE_MONEY=>'仅退款',
  41. self::REFUND_TYPE_ALL=>'退款退货',
  42. self::REFUND_TYPE_GOODS=>'仅退货',
  43. self::REFUND_TYPE_CANCEL=>'取消订单',
  44. self::REFUND_TYPE_HHBX=>'换货保修',
  45. ];
  46. public static $refundTypeGoods=[self::REFUND_TYPE_ALL,self::REFUND_TYPE_GOODS];
  47. public static $refundTypeMoney=[self::REFUND_TYPE_MONEY,self::REFUND_TYPE_CANCEL,self::REFUND_TYPE_ALL];
  48. const REASON_QU=1;
  49. public static $reasons=[
  50. self::REASON_QU=>'质量问题',
  51. 2=>'7天无理由退货(退回运费需客户承担)',
  52. 3=>'协商一致退款',
  53. 4=>'快递运输破损',
  54. 5=>'其他',
  55. ];
  56. const TH_TYPE_NONE=0;
  57. const TH_TYPE_SELF_SEND=1;
  58. const TH_TYPE_SELF_FEE=2;
  59. public static $goodsTypes=[
  60. self::TH_TYPE_NONE=>'无需退货',
  61. self::TH_TYPE_SELF_SEND=>'自行邮寄',
  62. self::TH_TYPE_SELF_FEE=>'拍维修费',
  63. ];
  64. /**
  65. * @return string[]
  66. */
  67. public static function getReasons(): array
  68. {
  69. $reasons=self::$reasons;
  70. $arr=[];
  71. foreach ($reasons as $key=>$value){
  72. $arr[$key]=compact('key','value');
  73. }
  74. return $arr;
  75. }
  76. /**
  77. * @return string[]
  78. */
  79. public static function getRefundBys(): array
  80. {
  81. $obj=self::$goodsTypes;
  82. $arr=[];
  83. foreach ($obj as $key=>$value){
  84. $arr[$key]=compact('key','value');
  85. }
  86. return $arr;
  87. }
  88. /**
  89. * @return string[]
  90. */
  91. public static function getRefundTypes(): array
  92. {
  93. return self::$refundTypes;
  94. }
  95. /**
  96. * @return int[]
  97. */
  98. public static function getRefundTypeGoods(): array
  99. {
  100. return self::$refundTypeGoods;
  101. }
  102. /**
  103. * @return int[]
  104. */
  105. public static function getRefundTypeMoney(): array
  106. {
  107. return self::$refundTypeMoney;
  108. }
  109. protected $autoWriteTimestamp=true;
  110. public function orderInfo(){
  111. return $this->belongsTo(OrderInfo::class);
  112. }
  113. public function orders(){
  114. return $this->belongsTo(Orders::class,'order_id');
  115. }
  116. public function user(){
  117. return $this->belongsTo(User::class);
  118. }
  119. public function allowCancel(){
  120. return in_array($this['refund_status'],[
  121. self::REFUND_ING,
  122. ]);
  123. }
  124. public function makeCancel(){
  125. $this['refund_status']=self::REFUND_CANCEL;
  126. $this->save();
  127. $this->orderInfo()->save([
  128. 'refund_id'=>null,
  129. ]);
  130. }
  131. #根据订单状态选择售后
  132. public static function makeRefundConfig(OrderInfo $orderInfo,$delKeys=true,$inject=true){
  133. $order=$orderInfo->orders;
  134. $refundConfig=[];
  135. #未发货
  136. if($order['status']==$order::S_WAIT_SEND){
  137. $refundConfig['refund_type']=[
  138. self::REFUND_TYPE_CANCEL=>'取消订单',
  139. ];
  140. $refundConfig['type']=[
  141. self::TH_TYPE_NONE=>'无需退货',
  142. ];
  143. $refundConfig['reason']=array_values(Refund::getReasons());
  144. $refundConfig['req_amount']=1;
  145. $refundConfig['req_order']=0;
  146. }
  147. #已发货
  148. elseif(in_array($order['status'],[$order::S_WAIT_REC,$order::S_OVER])){
  149. #七天内可以退货退款
  150. if(time()<$order['send_time']+7*86400){
  151. $refundConfig['refund_type']=[
  152. self::REFUND_TYPE_ALL=>'退款退货',
  153. ];
  154. $refundConfig['type']=[
  155. self::TH_TYPE_SELF_SEND=>'自行邮寄',
  156. ];
  157. $refundConfig['reason']=array_values(Refund::getReasons());
  158. $refundConfig['req_amount']=1;
  159. $refundConfig['req_order']=0;
  160. }else{
  161. $refundConfig['refund_type']=[
  162. self::REFUND_TYPE_HHBX=>'换货保修',
  163. ];
  164. $refundConfig['type']=[
  165. self::TH_TYPE_SELF_FEE=>'拍维修费',
  166. ];
  167. $refundConfig['reason']=array_values(Refund::getReasons());
  168. $refundConfig['req_amount']=0;
  169. $refundConfig['req_order']=1;
  170. }
  171. }
  172. if($delKeys) {
  173. if (!empty($refundConfig['refund_type'])) {
  174. $data = [];
  175. foreach ($refundConfig['refund_type'] as $key => $value) {
  176. $data[] = compact('key', 'value');
  177. }
  178. $refundConfig['refund_type'] = $data;
  179. }
  180. if (!empty($refundConfig['type'])) {
  181. $data = [];
  182. foreach ($refundConfig['type'] as $key => $value) {
  183. $data[] = compact('key', 'value');
  184. }
  185. $refundConfig['type'] = $data;
  186. }
  187. }
  188. if($inject){
  189. $orderInfo['refund_config']=$refundConfig?:null;
  190. }
  191. return $refundConfig;
  192. }
  193. public function allowAudit(){
  194. return $this['refund_status']==self::REFUND_ING;
  195. }
  196. #退款相关
  197. /**
  198. * 退款金额
  199. */
  200. public function getRefundAmount(){
  201. return $this['amount'];
  202. }
  203. public function refundResult($succ,$remark=''){
  204. if(is_bool($succ)){
  205. $this['pay_status']=$succ?1:2;
  206. }else{
  207. $this['pay_status']=$succ;
  208. }
  209. $this['pay_remark']=$remark;
  210. $this->save();
  211. }
  212. public function isRefundMoney(){
  213. return in_array($this['refund_type'],self::getRefundTypeMoney());
  214. }
  215. #end
  216. public function makeAudit($pass){
  217. $this['refund_status']=$pass?self::REFUND_PASS:self::REFUND_REJECT;
  218. $refundMoney=false;
  219. if($pass){
  220. #如果退货
  221. if(in_array($this['refund_type'],self::getRefundTypeGoods())){
  222. $this->orderInfo()->update(['is_return_goods'=>1]);
  223. }
  224. #如果退款,执行退款
  225. if(in_array($this['refund_type'],self::getRefundTypeMoney())){
  226. $this['order_no']=order_no('TK');
  227. $refundMoney=true;
  228. }
  229. }
  230. if(!$this->save()){
  231. throw_user('保存失败');
  232. }
  233. if($refundMoney){
  234. $payment=$this->orders->payment??null;
  235. if($payment){
  236. $refund=new OrderRefundService();
  237. $refund->setPayment($payment);
  238. $refund->setRefund($this);
  239. $refund->setBody("订单[{$this->orders->order_no}]退款");
  240. $refund->pay();
  241. }
  242. }
  243. SiteMsg::sendMsg(
  244. $pass?SiteMsg::TYPE_ORDER_REFUND_PASS:SiteMsg::TYPE_ORDER_REFUND_REJECT,
  245. $this->user
  246. );
  247. #总订单
  248. $order=$this->orders;
  249. if($order && $order->status<Orders::S_OVER){
  250. $has=OrderInfo::where('order_id',$this['order_id'])
  251. ->filterHasUnRefund()
  252. ->find();
  253. if(!$has){
  254. $order->makeCancel();
  255. }
  256. }
  257. }
  258. /**
  259. * 售后中及已售后的
  260. * @param Query $query
  261. * @param null $status 1进行中2已完成
  262. */
  263. public function scopeFilterRefund(Query $query,$status=null){
  264. if(is_null($status)) {
  265. $query->whereBetween("{$this->getTable()}.refund_status", [self::REFUND_ING, self::REFUND_PASS]);
  266. }elseif ($status==1){
  267. $query->where("{$this->getTable()}.refund_status", self::REFUND_ING);
  268. }elseif ($status==2){
  269. $query->where("{$this->getTable()}.refund_status", self::REFUND_PASS);
  270. }
  271. }
  272. /**
  273. * 投诉类型的售后
  274. * @param Query $query
  275. */
  276. public function scopeFilterTs(Query $query){
  277. $query->where("{$this->getTable()}.reason1",self::REASON_QU);
  278. }
  279. protected static function init()
  280. {
  281. self::beforeInsert(function (self $refund){
  282. $refund['refund_status']=self::REFUND_ING;
  283. });
  284. self::afterUpdate(function (self $refund){
  285. if($refund->refund_status==self::REFUND_PASS && $refund->isRefundMoney()){
  286. Transaction::addTransaction($refund);
  287. }
  288. });
  289. }
  290. }