Memberrefund.php 16 KB

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