Memberrefund.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /*
  3. * 订单退款
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Memberrefund extends BaseMember {
  21. public function initialize() {
  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. $refundreturn_model = model('refundreturn');
  34. $condition = array();
  35. $reason_list = $refundreturn_model->getReasonList($condition); //退款退货原因
  36. View::assign('reason_list', $reason_list);
  37. $order_id = intval(input('param.order_id'));
  38. $goods_id = intval(input('param.goods_id')); //订单商品表编号
  39. if ($order_id < 1 || $goods_id < 1) {//参数验证
  40. $this->error(lang('param_error'), (string) url('Memberorder/index'));
  41. }
  42. $condition = array();
  43. $condition[] = array('buyer_id', '=', session('member_id'));
  44. $condition[] = array('order_id', '=', $order_id);
  45. $order = $refundreturn_model->getRightOrderList($condition, $goods_id);
  46. $order_id = $order['order_id'];
  47. $order_amount = $order['order_amount']; //订单金额
  48. $order_refund_amount = $order['refund_amount']; //订单退款金额
  49. $goods_list = $order['goods_list'];
  50. $goods = $goods_list[0];
  51. $goods_pay_price = $goods['goods_pay_price']; //商品实际成交价
  52. $goods_pay_price -= $order['presell_deposit_amount'];
  53. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  54. $goods_pay_price = $order_amount - $order_refund_amount;
  55. $goods['goods_pay_price'] = $goods_pay_price;
  56. }
  57. View::assign('goods', $goods);
  58. View::assign('order', $order);
  59. View::assign('store', $order['extend_store']);
  60. View::assign('order_common', $order['extend_order_common']);
  61. View::assign('goods_list', $order['goods_list']);
  62. $goods_id = $goods['rec_id'];
  63. $condition = array();
  64. $condition[] = array('buyer_id', '=', $order['buyer_id']);
  65. $condition[] = array('order_id', '=', $order['order_id']);
  66. $condition[] = array('order_goods_id', '=', $goods_id);
  67. $condition[] = array('refund_state', '<', '3');
  68. $refund_list = $refundreturn_model->getRefundreturnList($condition);
  69. $refund = array();
  70. if (!empty($refund_list) && is_array($refund_list)) {
  71. $refund = $refund_list[0];
  72. }
  73. $refund_state = $refundreturn_model->getRefundState($order); //根据订单状态判断是否可以退款退货
  74. if ((isset($refund['refund_id']) && $refund['refund_id'] > 0) || $refund_state != 1) {//检查订单状态,防止页面刷新不及时造成数据错误
  75. $this->error(lang('param_error'), (string) url('Memberorder/index'));
  76. }
  77. if (request()->isPost() && $goods_id > 0) {
  78. $refund_array = array();
  79. $refund_amount = floatval(input('post.refund_amount')); //退款金额
  80. if (($refund_amount < 0) || ($refund_amount > $goods_pay_price)) {
  81. $refund_amount = $goods_pay_price;
  82. }
  83. $goods_num = intval(input('post.goods_num')); //退货数量
  84. if (($goods_num < 0) || ($goods_num > $goods['goods_num'])) {
  85. $goods_num = 1;
  86. }
  87. $refund_array['reason_info'] = '';
  88. $reason_id = intval(input('post.reason_id')); //退货退款原因
  89. $refund_array['reason_id'] = $reason_id;
  90. $reason_array = array();
  91. $reason_array['reason_info'] = lang('other');
  92. $reason_list[0] = $reason_array;
  93. if (!empty($reason_list[$reason_id])) {
  94. $reason_array = $reason_list[$reason_id];
  95. $refund_array['reason_info'] = $reason_array['reason_info'];
  96. }
  97. $pic_array = array();
  98. $pic_array['buyer'] = $this->upload_pic(); //上传凭证
  99. $info = serialize($pic_array);
  100. $refund_array['pic_info'] = $info;
  101. $trade_model = model('trade');
  102. $order_shipped = $trade_model->getOrderState('order_shipped'); //订单状态30:已发货
  103. if ($order['order_state'] == $order_shipped) {
  104. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  105. } else {
  106. $refund_array['order_lock'] = '1';
  107. }
  108. $refund_array['refund_type'] = input('post.refund_type'); //类型:1为退款,2为退货
  109. $show_url = (string) url('Memberreturn/index');
  110. $refund_array['return_type'] = '2'; //退货类型:1为不用退货,2为需要退货
  111. if ($refund_array['refund_type'] != '2') {
  112. $refund_array['refund_type'] = '1';
  113. $refund_array['return_type'] = '1';
  114. $show_url = (string) url('Memberrefund/index');
  115. }
  116. $refund_array['seller_state'] = '1'; //状态:1为待审核,2为同意,3为不同意
  117. $refund_array['refund_amount'] = ds_price_format($refund_amount);
  118. $refund_array['goods_num'] = $goods_num;
  119. $refund_array['buyer_message'] = input('post.buyer_message');
  120. $refund_array['add_time'] = TIMESTAMP;
  121. $state = $refundreturn_model->addRefundreturn($refund_array, $order, $goods);
  122. if ($state) {
  123. if ($order['order_state'] == $order_shipped) {
  124. $refundreturn_model->editOrderLock($order_id);
  125. }
  126. //自提点订单锁定
  127. $chain_order_model=model('chain_order');
  128. $chain_order_model->editChainOrderLock($order_id);
  129. $this->success(lang('ds_common_save_succ'), $show_url);
  130. } else {
  131. $this->error(lang('ds_common_save_fail'));
  132. }
  133. } else {
  134. /* 设置买家当前菜单 */
  135. $this->setMemberCurMenu('member_refund');
  136. /* 设置买家当前栏目 */
  137. $this->setMemberCurItem('my_address_edit');
  138. return View::fetch($this->template_dir . 'add_refund');
  139. }
  140. }
  141. /**
  142. * 添加全部退款即取消订单
  143. *
  144. */
  145. public function add_refund_all() {
  146. $trade_model = model('trade');
  147. $refundreturn_model = model('refundreturn');
  148. $order_id = intval(input('param.order_id'));
  149. $condition = array();
  150. $condition[] = array('buyer_id', '=', session('member_id'));
  151. $condition[] = array('order_id', '=', $order_id);
  152. $order = $refundreturn_model->getRightOrderList($condition);
  153. View::assign('order', $order);
  154. View::assign('store', $order['extend_store']);
  155. View::assign('order_common', $order['extend_order_common']);
  156. View::assign('goods_list', $order['goods_list']);
  157. $order_amount = $order['order_amount']; //订单金额
  158. $order_amount -= $order['presell_deposit_amount'];
  159. $condition = array();
  160. $condition[] = array('buyer_id', '=', $order['buyer_id']);
  161. $condition[] = array('order_id', '=', $order['order_id']);
  162. $condition[] = array('goods_id', '=', '0');
  163. $condition[] = array('refund_state', '<', '3');
  164. $refund_list = $refundreturn_model->getRefundreturnList($condition);
  165. $refund = array();
  166. if (!empty($refund_list) && is_array($refund_list)) {
  167. $refund = $refund_list[0];
  168. }
  169. $order_paid = $trade_model->getOrderState('order_paid'); //订单状态20:已付款
  170. $payment_code = $order['payment_code']; //支付方式
  171. if ((isset($refund['refund_id']) && $refund['refund_id'] > 0) || !in_array($order['order_state'],[ORDER_STATE_PAY,ORDER_STATE_PICKUP]) || $payment_code == 'offline') {//检查订单状态,防止页面刷新不及时造成数据错误
  172. $this->error(lang('param_error'), 'home/memberrefund/index');
  173. }
  174. if (!request()->isPost()) {
  175. /* 设置买家当前菜单 */
  176. $this->setMemberCurMenu('member_refund');
  177. /* 设置买家当前栏目 */
  178. $this->setMemberCurItem('my_address_edit');
  179. return View::fetch($this->template_dir . 'add_refund_all');
  180. } else {
  181. $refund_array = array();
  182. $refund_array['refund_type'] = '1'; //类型:1为退款,2为退货
  183. $refund_array['seller_state'] = '1'; //状态:1为待审核,2为同意,3为不同意
  184. $refund_array['order_lock'] = '2'; //锁定类型:1为不用锁定,2为需要锁定
  185. $refund_array['goods_id'] = '0';
  186. $refund_array['order_goods_id'] = '0';
  187. $refund_array['reason_id'] = '0';
  188. $refund_array['reason_info'] = lang('refund_notice4');
  189. $refund_array['goods_name'] = lang('all_orders_refunded');
  190. $refund_array['refund_amount'] = ds_price_format($order_amount);
  191. $refund_array['buyer_message'] = input('post.buyer_message');
  192. $refund_array['add_time'] = TIMESTAMP;
  193. $pic_array = array();
  194. $pic_array['buyer'] = $this->upload_pic(); //上传凭证
  195. $info = serialize($pic_array);
  196. $refund_array['pic_info'] = $info;
  197. $state = $refundreturn_model->addRefundreturn($refund_array, $order);
  198. if ($state) {
  199. $refundreturn_model->editOrderLock($order_id);
  200. //自提点订单锁定
  201. $chain_order_model=model('chain_order');
  202. $chain_order_model->editChainOrderLock($order_id);
  203. $this->success(lang('ds_common_save_succ'), 'Memberrefund/index');
  204. } else {
  205. $this->error(lang('ds_common_save_fail'));
  206. }
  207. }
  208. }
  209. /**
  210. * 退款记录列表页
  211. *
  212. */
  213. public function index() {
  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. $refundreturn_model = model('refundreturn');
  256. $condition = array();
  257. $condition[] = array('buyer_id', '=', session('member_id'));
  258. $condition[] = array('refund_id', '=', intval(input('param.refund_id')));
  259. $condition[] = array('refund_type', '=', '1'); //类型:1为退款,2为退货
  260. $refund = $refundreturn_model->getRefundreturnInfo($condition);
  261. View::assign('refund', $refund);
  262. $info['buyer'] = array();
  263. if (!empty($refund['pic_info'])) {
  264. $info = unserialize($refund['pic_info']);
  265. }
  266. $pic_list = empty($info['buyer']) ? '' : $info['buyer'];
  267. View::assign('pic_list', $pic_list);
  268. $condition = array();
  269. $condition[] = array('order_id', '=', $refund['order_id']);
  270. $order = $refundreturn_model->getRightOrderList($condition, $refund['order_goods_id']);
  271. View::assign('order', $order);
  272. View::assign('store', $order['extend_store']);
  273. View::assign('order_common', $order['extend_order_common']);
  274. View::assign('goods_list', $order['goods_list']);
  275. /* 设置买家当前菜单 */
  276. $this->setMemberCurMenu('member_refund');
  277. /* 设置买家当前栏目 */
  278. $this->setMemberCurItem('my_address_edit');
  279. return View::fetch($this->template_dir . 'view');
  280. }
  281. /**
  282. * 上传凭证
  283. *
  284. */
  285. private function upload_pic() {
  286. $refund_pic = array();
  287. $refund_pic[1] = 'refund_pic1';
  288. $refund_pic[2] = 'refund_pic2';
  289. $refund_pic[3] = 'refund_pic3';
  290. $pic_array = array();
  291. $count = 1;
  292. foreach ($refund_pic as $pic) {
  293. if (!empty($_FILES[$pic]['name'])) {
  294. $res = ds_upload_pic(ATTACH_PATH . DIRECTORY_SEPARATOR . 'refund', $pic);
  295. if ($res['code']) {
  296. $pic_array[$count] = $res['data']['file_name'];
  297. } else {
  298. $pic_array[$count] = '';
  299. }
  300. $count++;
  301. }
  302. }
  303. return $pic_array;
  304. }
  305. function getRefundStateArray($type = 'all') {
  306. $state_array = array(
  307. '1' => lang('refund_state_confirm'),
  308. '2' => lang('refund_state_yes'),
  309. '3' => lang('refund_state_no')
  310. ); //卖家处理状态:1为待审核,2为同意,3为不同意
  311. View::assign('state_array', $state_array);
  312. $admin_array = array(
  313. '1' => lang('in_processing'),
  314. '2' => lang('to_be_processed'),
  315. '3' => lang('has_been_completed'),
  316. '4' => lang('refund_state_no')
  317. ); //确认状态:1为买家或卖家处理中,2为待平台管理员处理,3为退款退货已完成
  318. View::assign('admin_array', $admin_array);
  319. $state_data = array(
  320. 'seller' => $state_array,
  321. 'admin' => $admin_array
  322. );
  323. if ($type == 'all') {
  324. return $state_data; //返回所有
  325. }
  326. return $state_data[$type];
  327. }
  328. /**
  329. * 栏目菜单
  330. */
  331. function getMemberItemList() {
  332. $item_list = array(
  333. array(
  334. 'name' => 'buyer_refund',
  335. 'text' => lang('ds_member_path_buyer_refund'),
  336. 'url' => (string) url('Memberrefund/index'),
  337. ),
  338. array(
  339. 'name' => 'buyer_return',
  340. 'text' => lang('ds_member_path_buyer_return'),
  341. 'url' => (string) url('Memberreturn/index'),
  342. ),
  343. array(
  344. 'name' => 'buyer_vr_refund',
  345. 'text' => lang('refund_virtual_currency_code'),
  346. 'url' => (string) url('Membervrrefund/index'),
  347. ),
  348. );
  349. return $item_list;
  350. }
  351. }