PresaleOrderRefund.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\presale\model;
  13. use app\model\member\MemberAccount;
  14. use app\model\BaseModel;
  15. use addon\coupon\model\Coupon;
  16. use app\model\system\Pay;
  17. /**
  18. * 订单退款
  19. *
  20. * @author Administrator
  21. *
  22. */
  23. class PresaleOrderRefund extends BaseModel
  24. {
  25. //已申请退款
  26. const REFUND_APPLY = 1;
  27. //已完成
  28. const REFUND_COMPLETE = 2;
  29. //已拒绝
  30. const REFUND_REFUSE = -1;
  31. /**
  32. * 订单退款状态
  33. * @var unknown
  34. */
  35. public $order_refund_status = [
  36. self::REFUND_APPLY => [
  37. 'status' => self::REFUND_APPLY,
  38. 'name' => '申请退款',
  39. ],
  40. self::REFUND_COMPLETE => [
  41. 'status' => self::REFUND_COMPLETE,
  42. 'name' => '退款成功',
  43. ],
  44. self::REFUND_REFUSE => [
  45. 'status' => self::REFUND_REFUSE,
  46. 'name' => '退款拒绝',
  47. ]
  48. ];
  49. /*************************************************************************** 用户申请退款操作(start)**********************************/
  50. /**
  51. * 用户申请退款
  52. * @param $data
  53. * @return array
  54. */
  55. public function applyRefund($data)
  56. {
  57. $order_info = model('promotion_presale_order')->getInfo(
  58. [
  59. [ 'id', '=', $data[ 'id' ] ],
  60. [ 'site_id', '=', $data[ 'site_id' ] ]
  61. ],
  62. 'pay_end_time,order_status,refund_status,member_id,balance_final_money,pay_final_money'
  63. );
  64. if (empty($order_info) || $order_info[ 'member_id' ] != $data[ 'member_id' ]) {
  65. return $this->error('', '非法请求!');
  66. }
  67. //不能重复申请退款
  68. if ($order_info[ 'refund_status' ] == self::REFUND_APPLY) {
  69. return $this->error('', '请不要重复申请退款');
  70. }
  71. //支付尾款了不能申请退款
  72. if ($order_info[ 'order_status' ] != PresaleOrderCommon::WAIT_FINAL_PAY) {
  73. return $this->error();
  74. }
  75. $pay_model = new Pay();
  76. $refund_no = $pay_model->createRefundNo();
  77. if ($order_info[ 'refund_status' ] == self::REFUND_REFUSE) {
  78. $order_data = [
  79. 'refund_status' => self::REFUND_APPLY,
  80. 'refund_status_name' => $this->order_refund_status[ self::REFUND_APPLY ][ 'name' ],
  81. ];
  82. } else {
  83. $order_data = [
  84. 'deposit_refund_no' => $refund_no,
  85. 'refund_status' => self::REFUND_APPLY,
  86. 'refund_status_name' => $this->order_refund_status[ self::REFUND_APPLY ][ 'name' ],
  87. 'apply_refund_time' => time(),
  88. ];
  89. }
  90. $res = model('promotion_presale_order')->update($order_data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  91. return $this->success($res);
  92. }
  93. /**
  94. * 用户取消申请退款
  95. * @param array $condition
  96. */
  97. public function cancelRefund($condition = [])
  98. {
  99. $order_info = model('promotion_presale_order')->getInfo(
  100. $condition,
  101. 'pay_end_time,order_status,refund_status,'
  102. );
  103. if (empty($order_info)) {
  104. return $this->error('', '非法请求!');
  105. }
  106. //不能重复申请退款
  107. if ($order_info[ 'refund_status' ] != self::REFUND_APPLY) {
  108. return $this->error('', '已退款或已取消');
  109. }
  110. $order_data = [
  111. 'deposit_refund_no' => '',
  112. 'refund_status' => '',
  113. 'refund_status_name' => '',
  114. 'apply_refund_time' => '',
  115. ];
  116. $res = model('promotion_presale_order')->update($order_data, $condition);
  117. return $this->success($res);
  118. }
  119. /**
  120. * 拒绝退款
  121. * @param $data
  122. * @return array
  123. */
  124. public function refuseRefund($data)
  125. {
  126. $order_info = model('promotion_presale_order')->getInfo(
  127. [
  128. [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ]
  129. ]
  130. , 'refund_status'
  131. );
  132. if ($order_info[ 'refund_status' ] == self::REFUND_REFUSE) {
  133. return $this->success();
  134. }
  135. $order_data = [
  136. 'refund_status' => self::REFUND_REFUSE,
  137. 'refund_status_name' => $this->order_refund_status[ self::REFUND_REFUSE ][ 'name' ],
  138. 'refuse_reason' => $data[ 'refuse_reason' ],
  139. 'refund_time' => time()
  140. ];
  141. $res = model('promotion_presale_order')->update($order_data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  142. return $this->success($res);
  143. }
  144. /**
  145. * 同意退款
  146. * @param $data
  147. * @return array
  148. */
  149. public function agreeRefund($data)
  150. {
  151. $order_info = model('promotion_presale_order')->getInfo(
  152. [
  153. [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ]
  154. ]
  155. );
  156. if ($order_info[ 'refund_status' ] == self::REFUND_COMPLETE) {
  157. return $this->success();
  158. }
  159. if ($order_info[ 'order_status' ] == 1 && $order_info[ 'pay_end_time' ] < time()) {
  160. $pay_model = new Pay();
  161. $refund_no = $pay_model->createRefundNo();
  162. }
  163. model('promotion_presale_order')->startTrans();
  164. try {
  165. //增加库存
  166. // model('promotion_presale')->setInc([['presale_id', '=', $order_info['presale_id']]], 'presale_stock', $order_info['num']);
  167. //增加门店库存
  168. if ($order_info[ 'delivery_store_id' ] > 0) {
  169. $presale_order_common = new PresaleOrderCommon();
  170. $store_data = [
  171. 'delivery_store_id' => $order_info[ 'delivery_store_id' ],
  172. 'num' => $order_info[ 'num' ],
  173. 'sku_id' => $order_info[ 'sku_id' ]
  174. ];
  175. $store_result = $presale_order_common->incStoreGoodsStock($store_data);
  176. if ($store_result[ 'code' ] < 0) {
  177. model('promotion_presale_order')->rollback();
  178. return $store_result;
  179. }
  180. }
  181. //返还店铺优惠券
  182. $coupon_id = $order_info[ "coupon_id" ];
  183. if ($coupon_id > 0) {
  184. $coupon_model = new Coupon();
  185. $coupon_model->refundCoupon($coupon_id, $order_info[ "member_id" ]);
  186. }
  187. //平台优惠券
  188. //平台余额 退还余额(定金余额)
  189. $balance_money = 0;
  190. $deposit_money = 0;//退款定金金额(余额支付+现金支付)
  191. if ($order_info[ "balance_deposit_money" ] > 0) {
  192. $balance_money += $order_info[ "balance_deposit_money" ];
  193. $deposit_money += $order_info[ "balance_deposit_money" ];
  194. }
  195. if ($order_info[ "balance_final_money" ] > 0) {
  196. $balance_money += $order_info[ "balance_final_money" ];
  197. }
  198. if ($balance_money > 0) {
  199. $member_account_model = new MemberAccount();
  200. $member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], "balance", $balance_money, "presale_deposit_refund", "余额返还", "订单关闭返还");
  201. }
  202. //现金原路退还
  203. if ($order_info[ "pay_deposit_money" ] > 0) {
  204. $pay_model = new Pay();
  205. $refund_result = $pay_model->refund($order_info[ "deposit_refund_no" ], $order_info[ 'pay_deposit_money' ], $order_info[ "deposit_out_trade_no" ], '', $order_info[ 'pay_deposit_money' ], $order_info[ "site_id" ], 1);
  206. if ($refund_result[ "code" ] < 0) {
  207. model('promotion_presale_order')->rollback();
  208. return $refund_result;
  209. }
  210. }
  211. $order_common = new PresaleOrderCommon();
  212. //修改订单退款状态
  213. $order_data = [
  214. 'order_status' => $order_common::ORDER_CLOSE,
  215. 'order_status_name' => $order_common->order_status[ $order_common::ORDER_CLOSE ][ 'name' ],
  216. 'order_status_action' => json_encode($order_common->order_status[ $order_common::ORDER_CLOSE ], JSON_UNESCAPED_UNICODE),
  217. 'refund_status' => self::REFUND_COMPLETE,
  218. 'refund_status_name' => $this->order_refund_status[ self::REFUND_COMPLETE ][ 'name' ],
  219. // 'refund_money' => $balance_money + $order_info["pay_deposit_money"],
  220. 'refund_money' => $deposit_money + $order_info[ "pay_deposit_money" ],
  221. 'refund_time' => time(),
  222. ];
  223. if (isset($refund_no) && !empty($refund_no)) {
  224. $order_data[ 'deposit_refund_no' ] = $refund_no;
  225. }
  226. model('promotion_presale_order')->update($order_data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  227. //减少预约数量
  228. model('promotion_presale')->setDec([ [ 'presale_id', '=', $order_info[ 'presale_id' ] ] ], 'sale_num', $order_info[ 'num' ]);
  229. model('promotion_presale_order')->commit();
  230. return $this->success();
  231. } catch (\Exception $e) {
  232. model('promotion_presale_order')->rollback();
  233. return $this->error('', $e->getMessage());
  234. }
  235. }
  236. /*************************************************************************** 用户申请退款操作(end)**********************************/
  237. /*************************************************************************** 订单退款相关操作(start)**********************************/
  238. /**
  239. * 订单退款
  240. * @param $data
  241. * @return array|mixed|void
  242. */
  243. public function refundPresaleOrder($order_no, $is_deposit_back, $refund_money_type)
  244. {
  245. //$refund_money_type 1原路退款 2线下退款 3退款到余额
  246. //$is_deposit_back 1退定金 2不退定金
  247. $is_offline = 1;
  248. //是否线下退款
  249. if ($refund_money_type == 2) {
  250. $is_offline = 2;
  251. }
  252. $order_info = model('promotion_presale_order')->getInfo(
  253. [
  254. [ 'order_no', '=', $order_no ]
  255. ]
  256. );
  257. $order_goods_info = model('order_goods')->getInfo(
  258. [
  259. [ 'order_no', '=', $order_no ]
  260. ],
  261. 'refund_action_time'
  262. );
  263. if (empty($order_info)) {
  264. return $this->success();
  265. }
  266. if ($order_info[ 'refund_status' ] == self::REFUND_COMPLETE) {
  267. return $this->success();
  268. }
  269. model('promotion_presale_order')->startTrans();
  270. try {
  271. //修改订单退款状态
  272. $pay_model = new Pay();
  273. $deposit_refund_no = $pay_model->createRefundNo();
  274. $final_refund_no = $pay_model->createRefundNo();
  275. $balance_money = 0;
  276. if ($refund_money_type == 1) {//原路退款
  277. //在线支付定金原路退还
  278. if ($order_info[ "pay_deposit_money" ] > 0 && $is_deposit_back == 1 && $is_offline == 1) {
  279. $pay_model = new Pay();
  280. $refund_result = $pay_model->refund($deposit_refund_no, $order_info[ 'pay_deposit_money' ], $order_info[ "deposit_out_trade_no" ], '', $order_info[ 'pay_deposit_money' ], $order_info[ "site_id" ], 1);
  281. if ($refund_result[ "code" ] < 0) {
  282. model('promotion_presale_order')->rollback();
  283. return $refund_result;
  284. }
  285. }
  286. //在线支付尾款原路退还
  287. if ($order_info[ "pay_final_money" ] > 0 && $is_offline == 1) {
  288. $pay_model = new Pay();
  289. $refund_result = $pay_model->refund($final_refund_no, $order_info[ 'pay_final_money' ], $order_info[ "final_out_trade_no" ], '', $order_info[ 'pay_final_money' ], $order_info[ "site_id" ], 1);
  290. if ($refund_result[ "code" ] < 0) {
  291. model('promotion_presale_order')->rollback();
  292. return $refund_result;
  293. }
  294. }
  295. //余额支付定金原路退还
  296. if ($order_info[ "balance_deposit_money" ] > 0 && $is_deposit_back == 1) {
  297. $balance_money += $order_info[ "balance_deposit_money" ];
  298. }
  299. //余额支付尾款原路退还
  300. if ($order_info[ "balance_final_money" ] > 0) {
  301. $balance_money += $order_info[ "balance_final_money" ];
  302. }
  303. if ($balance_money > 0 && $is_offline == 1) {
  304. $member_account_model = new MemberAccount();
  305. $member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], "balance", $balance_money, "presale_refund", "余额返还", "订单关闭返还");
  306. }
  307. } elseif ($refund_money_type == 3) {//退款到余额
  308. $balance_money += $order_info[ 'final_money' ];
  309. if ($is_deposit_back == 1) {
  310. $balance_money += $order_info[ 'presale_deposit_money' ];
  311. }
  312. $member_account_model = new MemberAccount();
  313. $member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ "member_id" ], "balance", $balance_money, "presale_refund", "余额返还", "订单关闭返还");
  314. }
  315. $refund_money = 0;
  316. $refund_money += $order_info[ 'final_money' ];
  317. if ($is_deposit_back == 1) {
  318. $refund_money += $order_info[ 'presale_deposit_money' ];
  319. }
  320. $order_common = new PresaleOrderCommon();
  321. $order_data = [
  322. 'order_status' => $order_common::ORDER_CLOSE,
  323. 'order_status_name' => $order_common->order_status[ $order_common::ORDER_CLOSE ][ 'name' ],
  324. 'order_status_action' => json_encode($order_common->order_status[ $order_common::ORDER_CLOSE ], JSON_UNESCAPED_UNICODE),
  325. 'deposit_refund_no' => $deposit_refund_no,
  326. 'final_refund_no' => $final_refund_no,
  327. 'refund_status' => self::REFUND_COMPLETE,
  328. 'refund_status_name' => $this->order_refund_status[ self::REFUND_COMPLETE ][ 'name' ],
  329. // 'refund_money' => $order_info["pay_deposit_money"] + $order_info["pay_final_money"] + $balance_money,
  330. 'refund_money' => $refund_money,
  331. 'refuse_time' => time(),
  332. 'apply_refund_time' => $order_goods_info[ 'refund_action_time' ]
  333. ];
  334. model('promotion_presale_order')->update($order_data, [ [ 'order_no', '=', $order_no ] ]);
  335. model('promotion_presale_order')->commit();
  336. return $this->success();
  337. } catch (\Exception $e) {
  338. model('promotion_presale_order')->rollback();
  339. return $this->error('', $e->getMessage());
  340. }
  341. }
  342. /*************************************************************************** 订单退款相关操作(end)**********************************/
  343. }