Refund.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Refund extends AdminControl
  13. {
  14. const EXPORT_SIZE = 1000;
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/refund.lang.php');
  19. //向模板页面输出退款退货状态
  20. $this->getRefundStateArray();
  21. }
  22. function getRefundStateArray($type = 'all')
  23. {
  24. $state_array = array(
  25. '1' => lang('refund_state_confirm'), '2' => lang('refund_state_yes'), '3' => lang('refund_state_no')
  26. ); //卖家处理状态:1为待审核,2为同意,3为不同意
  27. View::assign('state_array', $state_array);
  28. $admin_array = array(
  29. '1' => lang('admin_state_1'), '2' => lang('admin_state_2'), '3' => lang('admin_state_3'), '4' => lang('refund_state_no')
  30. ); //确认状态:1为买家或卖家处理中,2为待平台管理员处理,3为退款退货已完成
  31. View::assign('admin_array', $admin_array);
  32. $state_data = array(
  33. 'seller' => $state_array, 'admin' => $admin_array
  34. );
  35. if ($type == 'all') {
  36. return $state_data; //返回所有
  37. }
  38. return $state_data[$type];
  39. }
  40. /**
  41. * 待处理列表
  42. */
  43. public function refund_manage()
  44. {
  45. $refundreturn_model = model('refundreturn');
  46. $condition = array();
  47. $condition[] = array('refund_type', '=', 1);
  48. $condition[] = array('refund_state', '=', '2'); //状态:1为处理中,2为待管理员处理,3为已完成
  49. $keyword_type = array('order_sn', 'refund_sn', 'store_name', 'buyer_name', 'goods_name');
  50. $key = input('get.key');
  51. $type = input('get.type');
  52. if (trim($key) != '' && in_array($type, $keyword_type)) {
  53. $condition[] = array($type, 'like', '%' . $key . '%');
  54. }
  55. $add_time_from = input('get.add_time_from');
  56. $add_time_to = input('get.add_time_to');
  57. if (trim($add_time_from) != '') {
  58. $add_time_from = strtotime(trim($add_time_from));
  59. if ($add_time_from !== false) {
  60. $condition[] = array('add_time', '>=', $add_time_from);
  61. }
  62. }
  63. if (trim($add_time_to) != '') {
  64. $add_time_to = strtotime(trim($add_time_to)) + 86399;
  65. if ($add_time_to !== false) {
  66. $condition[] = array('add_time', '<=', $add_time_to);
  67. }
  68. }
  69. $refund_list = $refundreturn_model->getRefundList($condition, 10);
  70. View::assign('show_page', $refundreturn_model->page_info->render());
  71. View::assign('refund_list', $refund_list);
  72. $this->setAdminCurItem('refund_manage');
  73. return View::fetch('refund_manage');
  74. }
  75. /**
  76. * 所有记录
  77. */
  78. public function refund_all()
  79. {
  80. $refundreturn_model = model('refundreturn');
  81. $condition = array();
  82. $condition[] = array('refund_type', '=', 1);
  83. $keyword_type = array('order_sn', 'refund_sn', 'store_name', 'buyer_name', 'goods_name');
  84. $key = input('get.key');
  85. $type = input('get.type');
  86. if (trim($key) != '' && in_array($type, $keyword_type)) {
  87. $condition[] = array($type, 'like', '%' . $key . '%');
  88. }
  89. $add_time_from = input('get.add_time_from');
  90. $add_time_to = input('get.add_time_to');
  91. if (trim($add_time_from) != '') {
  92. $add_time_from = strtotime(trim($add_time_from));
  93. if ($add_time_from !== false) {
  94. $condition[] = array('add_time', '>=', $add_time_from);
  95. }
  96. }
  97. if (trim($add_time_to) != '') {
  98. $add_time_to = strtotime(trim($add_time_to)) + 86399;
  99. if ($add_time_to !== false) {
  100. $condition[] = array('add_time', '<=', $add_time_to);
  101. }
  102. }
  103. $refund_list = $refundreturn_model->getRefundList($condition, 10);
  104. View::assign('show_page', $refundreturn_model->page_info->render());
  105. View::assign('refund_list', $refund_list);
  106. $this->setAdminCurItem('refund_all');
  107. return View::fetch('refund_all');
  108. }
  109. /**
  110. * 退款处理页
  111. *
  112. */
  113. public function edit()
  114. {
  115. $refundreturn_model = model('refundreturn');
  116. $condition = array();
  117. $condition[] = array('refund_id', '=', intval(input('param.refund_id')));
  118. $refund_list = $refundreturn_model->getRefundList(array_merge($condition, array(array('refund_type', '=', 1))));
  119. $refund = $refund_list[0];
  120. //查询交易凭证
  121. $order_model = model('order');
  122. $order = $order_model->getOrderInfo(array('order_id' => $refund['order_id']));
  123. if (request()->isPost()) {
  124. if (!in_array(input('post.refund_state'), [3, 4])) {
  125. $this->error(lang('refund_state_null'));
  126. }
  127. $check = request()->checkToken('__token__');
  128. if (false === $check) {
  129. $this->error('invalid token');
  130. }
  131. if ($refund['refund_state'] != '2') { //检查状态,防止页面刷新不及时造成数据错误
  132. $this->error(lang('ds_common_save_fail'));
  133. }
  134. $order_id = $refund['order_id'];
  135. $refund_array = array();
  136. $refund_array['admin_time'] = TIMESTAMP;
  137. $refund_array['refund_state'] = '4'; //状态:1为处理中,2为待管理员处理,3为已完成
  138. $refund_array['admin_message'] = input('post.admin_message');
  139. if (input('post.refund_state') == '3') {
  140. $trade_no = input('param.trade_no');
  141. if ($trade_no && $trade_no != $order['trade_no']) {
  142. $order_model->editOrder(array('trade_no' => $trade_no), array(
  143. 'order_id' => $order['order_id']
  144. ));
  145. //添加订单日志
  146. $data = array();
  147. $data['order_id'] = $order['order_id'];
  148. $data['log_role'] = 'system';
  149. $data['log_user'] = $this->admin_info['admin_name'];
  150. $data['log_msg'] = '修改支付平台交易号 : ' . $trade_no;
  151. $data['log_orderstate'] = $order['order_state'];
  152. $order_model->addOrderlog($data);
  153. }
  154. $refund_array['refund_state'] = '3';
  155. $res = $refundreturn_model->editOrderRefund($refund);
  156. $state = $res['code'];
  157. if (!$state) {
  158. $this->error($res['msg']);
  159. }
  160. } else {
  161. if ($refund['order_lock'] == '2') {
  162. $state = $refundreturn_model->editOrderUnlock($order_id); //订单解锁
  163. } else {
  164. $state = true;
  165. }
  166. //自提点订单解锁
  167. $chain_order_model = model('chain_order');
  168. $chain_order_model->editChainOrderUnlock($order_id);
  169. }
  170. if ($state) {
  171. $refundreturn_model->editRefundreturn($condition, $refund_array);
  172. // 发送买家消息
  173. $param = array();
  174. $param['code'] = 'refund_return_notice';
  175. $param['member_id'] = $refund['buyer_id'];
  176. //阿里短信参数
  177. $param['ali_param'] = array(
  178. 'refund_sn' => $refund['refund_sn']
  179. );
  180. $param['ten_param'] = array(
  181. $refund['refund_sn']
  182. );
  183. $param['param'] = array_merge($param['ali_param'], array(
  184. 'refund_url' => HOME_SITE_URL . '/memberrefund/view?refund_id=' . $refund['refund_id'],
  185. ));
  186. //微信模板消息
  187. $param['weixin_param'] = array(
  188. 'url' => config('ds_config.h5_site_url') . '/pages/member/refund/RefundView?refund_id=' . $refund['refund_id'],
  189. 'data' => array(
  190. "keyword1" => array(
  191. "value" => $refund['order_sn'],
  192. "color" => "#333"
  193. ),
  194. "keyword2" => array(
  195. "value" => $refund['refund_amount'],
  196. "color" => "#333"
  197. )
  198. ),
  199. );
  200. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendMemberMsg', 'cron_value' => serialize($param)));
  201. $this->log('退款确认,退款编号' . $refund['refund_sn']);
  202. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  203. } else {
  204. $this->error(lang('ds_common_save_fail'));
  205. }
  206. }
  207. View::assign('trade_no', $order['trade_no']);
  208. View::assign('refund', $refund);
  209. $info['buyer'] = array();
  210. if (!empty($refund['pic_info'])) {
  211. $info = unserialize($refund['pic_info']);
  212. }
  213. View::assign('pic_list', $info['buyer']);
  214. return View::fetch('edit');
  215. }
  216. /**
  217. * 退款记录查看页
  218. *
  219. */
  220. public function view()
  221. {
  222. $refundreturn_model = model('refundreturn');
  223. $condition = array();
  224. $condition[] = array('refund_id', '=', intval(input('param.refund_id')));
  225. $condition[] = array('refund_type', '=', 1);
  226. $refund_list = $refundreturn_model->getRefundList($condition);
  227. $refund = $refund_list[0];
  228. View::assign('refund', $refund);
  229. $info['buyer'] = array();
  230. if (!empty($refund['pic_info'])) {
  231. $info = unserialize($refund['pic_info']);
  232. }
  233. View::assign('pic_list', $info['buyer']);
  234. return View::fetch('view');
  235. }
  236. /**
  237. * 退款退货原因
  238. */
  239. public function reason()
  240. {
  241. $refundreturn_model = model('refundreturn');
  242. $condition = array();
  243. $reason_list = $refundreturn_model->getReasonList($condition, 10);
  244. View::assign('reason_list', $reason_list);
  245. View::assign('show_page', $refundreturn_model->page_info->render());
  246. $this->setAdminCurItem('reason');
  247. return View::fetch('reason');
  248. }
  249. /**
  250. * 新增退款退货原因
  251. */
  252. public function add_reason()
  253. {
  254. $refundreturn_model = model('refundreturn');
  255. if (request()->post()) {
  256. $reason_array = array();
  257. $reason_array['reason_info'] = input('post.reason_info');
  258. $reason_array['reason_sort'] = intval(input('post.reason_sort'));
  259. $reason_array['reason_updatetime'] = TIMESTAMP;
  260. $state = $refundreturn_model->addReason($reason_array);
  261. if ($state) {
  262. $this->log('新增退款退货原因,编号' . $state);
  263. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  264. } else {
  265. $this->error(lang('ds_common_save_fail'));
  266. }
  267. }
  268. return View::fetch('add_reason');
  269. }
  270. /**
  271. * 编辑退款退货原因
  272. *
  273. */
  274. public function edit_reason()
  275. {
  276. $refundreturn_model = model('refundreturn');
  277. $condition = array();
  278. $reason_id = intval(input('param.reason_id'));
  279. $condition[] = array('reason_id', '=', $reason_id);
  280. $reason_list = $refundreturn_model->getReasonList($condition);
  281. $reason = $reason_list[$reason_id];
  282. if (request()->post()) {
  283. $reason_array = array();
  284. $reason_array['reason_info'] = input('post.reason_info');
  285. $reason_array['reason_sort'] = intval(input('post.reason_sort'));
  286. $reason_array['reason_updatetime'] = TIMESTAMP;
  287. $state = $refundreturn_model->editReason($condition, $reason_array);
  288. if ($state) {
  289. $this->log('编辑退款退货原因,编号' . $reason_id);
  290. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  291. } else {
  292. $this->error(lang('ds_common_save_fail'));
  293. }
  294. }
  295. View::assign('reason', $reason);
  296. return View::fetch('edit_reason');
  297. }
  298. /**
  299. * 删除退款退货原因
  300. *
  301. */
  302. public function del_reason()
  303. {
  304. $refundreturn_model = model('refundreturn');
  305. $reason_id = input('param.reason_id');
  306. $reason_id_array = ds_delete_param($reason_id);
  307. if ($reason_id_array === FALSE) {
  308. ds_json_encode('10001', lang('param_error'));
  309. }
  310. $condition = array(array('reason_id', 'in', $reason_id_array));
  311. $state = $refundreturn_model->delReason($condition);
  312. if ($state) {
  313. $this->log('删除退款退货原因,编号' . $reason_id);
  314. ds_json_encode('10000', lang('ds_common_del_succ'));
  315. } else {
  316. ds_json_encode('10001', lang('ds_common_del_fail'));
  317. }
  318. }
  319. /**
  320. * 导出
  321. *
  322. */
  323. public function export_step1()
  324. {
  325. $refundreturn_model = model('refundreturn');
  326. $condition = array();
  327. $keyword_type = array('order_sn', 'refund_sn', 'store_name', 'buyer_name', 'goods_name');
  328. $key = input('get.key');
  329. $type = input('get.type');
  330. if (trim($key) != '' && in_array($type, $keyword_type)) {
  331. $condition[] = array($type, 'like', '%' . $key . '%');
  332. }
  333. $add_time_from = input('get.add_time_from');
  334. $add_time_to = input('get.add_time_to');
  335. if (trim($add_time_from) != '') {
  336. $add_time_from = strtotime(trim($add_time_from));
  337. if ($add_time_from !== false) {
  338. $condition[] = array('add_time', '>=', $add_time_from);
  339. }
  340. }
  341. if (trim($add_time_to) != '') {
  342. $add_time_to = strtotime(trim($add_time_to)) + 86399;
  343. if ($add_time_to !== false) {
  344. $condition[] = array('add_time', '<=', $add_time_to);
  345. }
  346. }
  347. if (!is_numeric(input('param.page'))) {
  348. $count = $refundreturn_model->getRefundCount($condition);
  349. $export_list = array();
  350. if ($count > self::EXPORT_SIZE) { //显示下载链接
  351. $page = ceil($count / self::EXPORT_SIZE);
  352. for ($i = 1; $i <= $page; $i++) {
  353. $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
  354. $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
  355. $export_list[$i] = $limit1 . ' ~ ' . $limit2;
  356. }
  357. View::assign('export_list', $export_list);
  358. return View::fetch('/public/excel');
  359. } else { //如果数量小,直接下载
  360. $data = $refundreturn_model->getRefundList(array_merge($condition, array(array('refund_type', '=', 1))), '', '*', 'refund_id desc', self::EXPORT_SIZE);
  361. $this->createExcel($data);
  362. }
  363. } else { //下载
  364. $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
  365. $limit2 = self::EXPORT_SIZE;
  366. $data = $refundreturn_model->getRefundList(array_merge($condition, array(array('refund_type', '=', 1))), $limit2, '*', 'refund_id desc');
  367. $this->createExcel($data);
  368. }
  369. }
  370. /**
  371. * 生成excel
  372. *
  373. * @param array $data
  374. */
  375. private function createExcel($data = array())
  376. {
  377. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/export.lang.php');
  378. $excel_obj = new \excel\Excel();
  379. $excel_data = array();
  380. //设置样式
  381. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  382. //header
  383. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_order_sn'));
  384. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_refund_sn'));
  385. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
  386. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_goods_name'));
  387. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_buyer_name'));
  388. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_tk_order_add_time'));
  389. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('exp_tk_order_refund'));
  390. //data
  391. foreach ((array) $data as $k => $v) {
  392. $tmp = array();
  393. $tmp[] = array('data' => 'DS' . $v['order_sn']);
  394. $tmp[] = array('data' => $v['refund_sn']);
  395. $tmp[] = array('data' => $v['store_name']);
  396. $tmp[] = array('data' => $v['goods_name']);
  397. $tmp[] = array('data' => $v['buyer_name']);
  398. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['add_time']));
  399. $tmp[] = array('format' => 'Number', 'data' => ds_price_format($v['refund_amount']));
  400. $excel_data[] = $tmp;
  401. }
  402. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  403. $excel_obj->addArray($excel_data);
  404. $excel_obj->addWorksheet($excel_obj->charset(lang('exp_tk_refund'), CHARSET));
  405. $excel_obj->generateXML($excel_obj->charset(lang('exp_tk_refund'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
  406. }
  407. /**
  408. * 获取卖家栏目列表,针对控制器下的栏目
  409. */
  410. protected function getAdminItemList()
  411. {
  412. $menu_array = array(
  413. array(
  414. 'name' => 'refund_manage', 'text' => lang('admin_state_2'), 'url' => (string)url('Refund/refund_manage')
  415. ), array(
  416. 'name' => 'refund_all', 'text' => lang('refund_all'), 'url' => (string)url('Refund/refund_all')
  417. ), array(
  418. 'name' => 'reason', 'text' => lang('refund_return_reason'), 'url' => (string)url('Refund/reason')
  419. ),
  420. );
  421. if (request()->action() == 'reason') {
  422. $menu_array[] = [
  423. 'name' => 'add_reason', 'text' => lang('add_reason'), 'url' => "javascript:dsLayerOpen('" . (string)url('Refund/add_reason') . "','" . lang('add_reason') . "')"
  424. ];
  425. }
  426. return $menu_array;
  427. }
  428. }