Refund.php 19 KB

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