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