Refund.php 19 KB

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