Memberrefund.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /*
  3. * 订单退款
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * ============================================================================
  16. * 控制器
  17. */
  18. class Memberrefund extends BaseMember
  19. {
  20. public function initialize()
  21. {
  22. parent::initialize();
  23. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberrefund.lang.php');
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberorder.lang.php');
  25. //向模板页面输出退款退货状态
  26. $this->getRefundStateArray();
  27. }
  28. /**
  29. * 添加订单商品部分退款
  30. *
  31. */
  32. public function add_refund()
  33. {
  34. $refundreturn_model = model('refundreturn');
  35. $condition = array();
  36. $reason_list = $refundreturn_model->getReasonList($condition); //退款退货原因
  37. View::assign('reason_list', $reason_list);
  38. $order_id = intval(input('param.order_id'));
  39. $goods_id = intval(input('param.goods_id')); //订单商品表编号
  40. if ($order_id < 1 || $goods_id < 1) { //参数验证
  41. $this->error(lang('param_error'), (string) url('Memberorder/index'));
  42. }
  43. $condition = array();
  44. $condition[] = array('buyer_id', '=', session('member_id'));
  45. $condition[] = array('order_id', '=', $order_id);
  46. $order = $refundreturn_model->getRightOrderList($condition, $goods_id);
  47. $order_id = $order['order_id'];
  48. $order_amount = $order['order_amount']; //订单金额
  49. $order_refund_amount = $order['refund_amount']; //订单退款金额
  50. $goods_list = $order['goods_list'];
  51. $goods = $goods_list[0];
  52. $goods_pay_price = $goods['goods_pay_price']; //商品实际成交价
  53. $goods_pay_price -= $order['presell_deposit_amount'];
  54. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  55. $goods_pay_price = $order_amount - $order_refund_amount;
  56. $goods['goods_pay_price'] = $goods_pay_price;
  57. }
  58. View::assign('goods', $goods);
  59. View::assign('order', $order);
  60. View::assign('store', $order['extend_store']);
  61. View::assign('order_common', $order['extend_order_common']);
  62. View::assign('goods_list', $order['goods_list']);
  63. $goods_id = $goods['rec_id'];
  64. $condition = array();
  65. $condition[] = array('buyer_id', '=', $order['buyer_id']);
  66. $condition[] = array('order_id', '=', $order['order_id']);
  67. $condition[] = array('order_goods_id', '=', $goods_id);
  68. $condition[] = array('refund_state', '<', '3');
  69. $refund_list = $refundreturn_model->getRefundreturnList($condition);
  70. $refund = array();
  71. if (!empty($refund_list) && is_array($refund_list)) {
  72. $refund = $refund_list[0];
  73. }
  74. $refund_state = $refundreturn_model->getRefundState($order); //根据订单状态判断是否可以退款退货
  75. if ((isset($refund['refund_id']) && $refund['refund_id'] > 0) || $refund_state != 1) { //检查订单状态,防止页面刷新不及时造成数据错误
  76. $this->error(lang('param_error'), (string) url('Memberorder/index'));
  77. }
  78. if (request()->isPost() && $goods_id > 0) {
  79. $refund_array = array();
  80. $refund_amount = floatval(input('post.refund_amount')); //退款金额
  81. if (($refund_amount < 0) || ($refund_amount > $goods_pay_price)) {
  82. $refund_amount = $goods_pay_price;
  83. }
  84. $goods_num = intval(input('post.goods_num')); //退货数量
  85. if (($goods_num < 0) || ($goods_num > $goods['goods_num'])) {
  86. $goods_num = 1;
  87. }
  88. $refund_array['reason_info'] = '';
  89. $reason_id = intval(input('post.reason_id')); //退货退款原因
  90. $refund_array['reason_id'] = $reason_id;
  91. $reason_array = array();
  92. $reason_array['reason_info'] = lang('other');
  93. $reason_list[0] = $reason_array;
  94. if (!empty($reason_list[$reason_id])) {
  95. $reason_array = $reason_list[$reason_id];
  96. $refund_array['reason_info'] = $reason_array['reason_info'];
  97. }
  98. $pic_array = array();
  99. $pic_array['buyer'] = $this->upload_pic(); //上传凭证
  100. $info = serialize($pic_array);
  101. $refund_array['pic_info'] = $info;
  102. $trade_model = model('trade');
  103. $order_shipped = $trade_model->getOrderState('order_shipped'); //订单状态30:已发货
  104. if ($order['order_state'] == $order_shipped) {
  105. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  106. } else {
  107. $refund_array['order_lock'] = '1';
  108. }
  109. $refund_array['refund_type'] = input('post.refund_type'); //类型:1为退款,2为退货
  110. $show_url = (string) url('Memberreturn/index');
  111. $refund_array['return_type'] = '2'; //退货类型:1为不用退货,2为需要退货
  112. if ($refund_array['refund_type'] != '2') {
  113. $refund_array['refund_type'] = '1';
  114. $refund_array['return_type'] = '1';
  115. $show_url = (string) url('Memberrefund/index');
  116. }
  117. $refund_array['seller_state'] = '1'; //状态:1为待审核,2为同意,3为不同意
  118. $refund_array['refund_amount'] = ds_price_format($refund_amount);
  119. $refund_array['goods_num'] = $goods_num;
  120. $refund_array['buyer_message'] = input('post.buyer_message');
  121. $refund_array['add_time'] = TIMESTAMP;
  122. $state = $refundreturn_model->addRefundreturn($refund_array, $order, $goods);
  123. if ($state) {
  124. if ($order['order_state'] == $order_shipped) {
  125. $refundreturn_model->editOrderLock($order_id);
  126. }
  127. //自提点订单锁定
  128. $chain_order_model = model('chain_order');
  129. $chain_order_model->editChainOrderLock($order_id);
  130. $this->success(lang('ds_common_save_succ'), $show_url);
  131. } else {
  132. $this->error(lang('ds_common_save_fail'));
  133. }
  134. } else {
  135. /* 设置买家当前菜单 */
  136. $this->setMemberCurMenu('member_refund');
  137. /* 设置买家当前栏目 */
  138. $this->setMemberCurItem('my_address_edit');
  139. return View::fetch($this->template_dir . 'add_refund');
  140. }
  141. }
  142. /**
  143. * 添加全部退款即取消订单
  144. *
  145. */
  146. public function add_refund_all()
  147. {
  148. $trade_model = model('trade');
  149. $refundreturn_model = model('refundreturn');
  150. $order_id = intval(input('param.order_id'));
  151. $condition = array();
  152. $condition[] = array('buyer_id', '=', session('member_id'));
  153. $condition[] = array('order_id', '=', $order_id);
  154. $order = $refundreturn_model->getRightOrderList($condition);
  155. View::assign('order', $order);
  156. View::assign('store', $order['extend_store']);
  157. View::assign('order_common', $order['extend_order_common']);
  158. View::assign('goods_list', $order['goods_list']);
  159. $order_amount = $order['order_amount']; //订单金额
  160. $order_amount -= $order['presell_deposit_amount'];
  161. $condition = array();
  162. $condition[] = array('buyer_id', '=', $order['buyer_id']);
  163. $condition[] = array('order_id', '=', $order['order_id']);
  164. $condition[] = array('goods_id', '=', '0');
  165. $condition[] = array('refund_state', '<', '3');
  166. $refund_list = $refundreturn_model->getRefundreturnList($condition);
  167. $refund = array();
  168. if (!empty($refund_list) && is_array($refund_list)) {
  169. $refund = $refund_list[0];
  170. }
  171. $order_paid = $trade_model->getOrderState('order_paid'); //订单状态20:已付款
  172. $payment_code = $order['payment_code']; //支付方式
  173. if ((isset($refund['refund_id']) && $refund['refund_id'] > 0) || !in_array($order['order_state'], [ORDER_STATE_PAY, ORDER_STATE_PICKUP]) || $payment_code == 'offline') { //检查订单状态,防止页面刷新不及时造成数据错误
  174. $this->error(lang('param_error'), 'home/memberrefund/index');
  175. }
  176. if (!request()->isPost()) {
  177. /* 设置买家当前菜单 */
  178. $this->setMemberCurMenu('member_refund');
  179. /* 设置买家当前栏目 */
  180. $this->setMemberCurItem('my_address_edit');
  181. return View::fetch($this->template_dir . 'add_refund_all');
  182. } else {
  183. $refund_array = array();
  184. $refund_array['refund_type'] = '1'; //类型:1为退款,2为退货
  185. $refund_array['seller_state'] = '1'; //状态:1为待审核,2为同意,3为不同意
  186. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  187. $refund_array['goods_id'] = '0';
  188. $refund_array['order_goods_id'] = '0';
  189. $refund_array['reason_id'] = '0';
  190. $refund_array['reason_info'] = lang('refund_notice4');
  191. $refund_array['goods_name'] = lang('all_orders_refunded');
  192. $refund_array['refund_amount'] = ds_price_format($order_amount);
  193. $refund_array['buyer_message'] = input('post.buyer_message');
  194. $refund_array['add_time'] = TIMESTAMP;
  195. $pic_array = array();
  196. $pic_array['buyer'] = $this->upload_pic(); //上传凭证
  197. $info = serialize($pic_array);
  198. $refund_array['pic_info'] = $info;
  199. $state = $refundreturn_model->addRefundreturn($refund_array, $order);
  200. if ($state) {
  201. $refundreturn_model->editOrderLock($order_id);
  202. //自提点订单锁定
  203. $chain_order_model = model('chain_order');
  204. $chain_order_model->editChainOrderLock($order_id);
  205. $this->success(lang('ds_common_save_succ'), 'Memberrefund/index');
  206. } else {
  207. $this->error(lang('ds_common_save_fail'));
  208. }
  209. }
  210. }
  211. /**
  212. * 退款记录列表页
  213. *
  214. */
  215. public function index()
  216. {
  217. $refundreturn_model = model('refundreturn');
  218. $condition = array();
  219. $condition[] = array('buyer_id', '=', session('member_id'));
  220. $condition[] = array('refund_type', '=', '1'); //类型:1为退款,2为退货
  221. $keyword_type = array('order_sn', 'refund_sn', 'goods_name');
  222. $key = input('get.key');
  223. $type = input('get.type');
  224. if (trim($key) != '' && in_array($type, $keyword_type)) {
  225. $condition[] = array($type, 'like', '%' . $key . '%');
  226. }
  227. $add_time_from = input('get.add_time_from');
  228. $add_time_to = input('get.add_time_to');
  229. if (trim($add_time_from) != '') {
  230. $add_time_from = strtotime(trim($add_time_from));
  231. if ($add_time_from !== false) {
  232. $condition[] = array('add_time', '>=', $add_time_from);
  233. }
  234. }
  235. if (trim($add_time_to) != '') {
  236. $add_time_to = strtotime(trim($add_time_to));
  237. if ($add_time_to !== false) {
  238. $add_time_to = $add_time_to + 86399;
  239. $condition[] = array('add_time', '<=', $add_time_to);
  240. }
  241. }
  242. $refund_list = $refundreturn_model->getRefundList($condition, 10);
  243. View::assign('refund_list', $refund_list);
  244. View::assign('show_page', $refundreturn_model->page_info->render());
  245. $store_list = $refundreturn_model->getRefundStoreList($refund_list);
  246. View::assign('store_list', $store_list);
  247. /* 设置买家当前菜单 */
  248. $this->setMemberCurMenu('member_refund');
  249. /* 设置买家当前栏目 */
  250. $this->setMemberCurItem('buyer_refund');
  251. return View::fetch($this->template_dir . 'index');
  252. }
  253. /**
  254. * 退款记录查看
  255. *
  256. */
  257. public function view()
  258. {
  259. $refundreturn_model = model('refundreturn');
  260. $condition = array();
  261. $condition[] = array('buyer_id', '=', session('member_id'));
  262. $condition[] = array('refund_id', '=', intval(input('param.refund_id')));
  263. $condition[] = array('refund_type', '=', '1'); //类型:1为退款,2为退货
  264. $refund = $refundreturn_model->getRefundreturnInfo($condition);
  265. View::assign('refund', $refund);
  266. $info['buyer'] = array();
  267. if (!empty($refund['pic_info'])) {
  268. $info = unserialize($refund['pic_info']);
  269. }
  270. $pic_list = empty($info['buyer']) ? '' : $info['buyer'];
  271. View::assign('pic_list', $pic_list);
  272. $condition = array();
  273. $condition[] = array('order_id', '=', $refund['order_id']);
  274. $order = $refundreturn_model->getRightOrderList($condition, $refund['order_goods_id']);
  275. View::assign('order', $order);
  276. View::assign('store', $order['extend_store']);
  277. View::assign('order_common', $order['extend_order_common']);
  278. View::assign('goods_list', $order['goods_list']);
  279. /* 设置买家当前菜单 */
  280. $this->setMemberCurMenu('member_refund');
  281. /* 设置买家当前栏目 */
  282. $this->setMemberCurItem('my_address_edit');
  283. return View::fetch($this->template_dir . 'view');
  284. }
  285. /**
  286. * 上传凭证
  287. *
  288. */
  289. private function upload_pic()
  290. {
  291. $refund_pic = array();
  292. $refund_pic[1] = 'refund_pic1';
  293. $refund_pic[2] = 'refund_pic2';
  294. $refund_pic[3] = 'refund_pic3';
  295. $pic_array = array();
  296. $count = 1;
  297. foreach ($refund_pic as $pic) {
  298. if (!empty($_FILES[$pic]['name'])) {
  299. $res = ds_upload_pic(ATTACH_PATH . DIRECTORY_SEPARATOR . 'refund', $pic);
  300. if ($res['code']) {
  301. $pic_array[$count] = $res['data']['file_name'];
  302. } else {
  303. $pic_array[$count] = '';
  304. }
  305. $count++;
  306. }
  307. }
  308. return $pic_array;
  309. }
  310. function getRefundStateArray($type = 'all')
  311. {
  312. $state_array = array(
  313. '1' => lang('refund_state_confirm'),
  314. '2' => lang('refund_state_yes'),
  315. '3' => lang('refund_state_no')
  316. ); //卖家处理状态:1为待审核,2为同意,3为不同意
  317. View::assign('state_array', $state_array);
  318. $admin_array = array(
  319. '1' => lang('in_processing'),
  320. '2' => lang('to_be_processed'),
  321. '3' => lang('has_been_completed'),
  322. '4' => lang('refund_state_no')
  323. ); //确认状态:1为买家或卖家处理中,2为待平台管理员处理,3为退款退货已完成
  324. View::assign('admin_array', $admin_array);
  325. $state_data = array(
  326. 'seller' => $state_array,
  327. 'admin' => $admin_array
  328. );
  329. if ($type == 'all') {
  330. return $state_data; //返回所有
  331. }
  332. return $state_data[$type];
  333. }
  334. /**
  335. * 栏目菜单
  336. */
  337. function getMemberItemList()
  338. {
  339. $item_list = array(
  340. array(
  341. 'name' => 'buyer_refund',
  342. 'text' => lang('ds_member_path_buyer_refund'),
  343. 'url' => (string) url('Memberrefund/index'),
  344. ),
  345. array(
  346. 'name' => 'buyer_return',
  347. 'text' => lang('ds_member_path_buyer_return'),
  348. 'url' => (string) url('Memberreturn/index'),
  349. ),
  350. array(
  351. 'name' => 'buyer_vr_refund',
  352. 'text' => lang('refund_virtual_currency_code'),
  353. 'url' => (string) url('Membervrrefund/index'),
  354. ),
  355. );
  356. return $item_list;
  357. }
  358. }