Refund.php 19 KB

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