Sellerrefund.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace app\api\controller;
  3. use GatewayWorker\Lib\Db;
  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 Sellerrefund extends MobileSeller
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerrefund.lang.php');
  23. }
  24. /**
  25. * @api {POST} api/Sellerrefund/refund_list 退款记录列表页
  26. * @apiVersion 1.0.0
  27. * @apiGroup Sellerrefund
  28. *
  29. * @apiHeader {String} X-DS-KEY 卖家授权token
  30. *
  31. * @apiParam {Int} refund_type 类型 1退款 2退货
  32. * @apiParam {String} key 搜索关键词
  33. * @apiParam {String} type 搜索字段名 order_sn订单号 refund_sn退款单号 buyer_name买家用户名
  34. * @apiParam {String} add_time_from 开始日期 YYYY-MM-DD
  35. * @apiParam {String} add_time_to 结束日期 YYYY-MM-DD
  36. * @apiParam {Int} state 卖家处理状态:1:待审核,2:同意,3:不同意
  37. * @apiParam {Int} lock 订单锁定类型:1:不用锁定,2:需要锁定
  38. * @apiParam {String} page 页码
  39. * @apiParam {String} pagesize 每页显示数量
  40. *
  41. * @apiSuccess {String} code 返回码,10000为成功
  42. * @apiSuccess {String} message 返回消息
  43. * @apiSuccess {Object} result 返回数据
  44. * @apiSuccess {Object[]} result.refund_list 退款记录列表 (返回字段参考refundreturn表)
  45. * @apiSuccess {Object[]} result.refund_list.goods_list 退款商品列表 (返回字段参考ordergoods表)
  46. * @apiSuccess {String} result.refund_list.goods_list.goods_img_480 商品图片完整路径
  47. * @apiSuccess {String} result.refund_list.seller_state_text 卖家处理状态描述
  48. * @apiSuccess {String} result.refund_list.refund_state_text 管理员处理状态描述
  49. * @apiSuccess {String} result.refund_list.delay_time 延期时间
  50. * @apiSuccess {Int} result.page_total 总页数
  51. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  52. */
  53. public function refund_list()
  54. {
  55. $refund_type = intval(input('refund_type'));
  56. if (!in_array($refund_type, array(1, 2))) {
  57. $refund_type = 1;
  58. }
  59. $refundreturn_model = model('refundreturn');
  60. $condition = array();
  61. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  62. $condition[] = array('refund_type', '=', $refund_type); //类型:1为退款,2为退货
  63. $keyword_type = array('order_sn', 'refund_sn', 'buyer_name');
  64. $key = input('key');
  65. $type = input('type');
  66. if (trim($key) != '' && in_array($type, $keyword_type)) {
  67. $condition[] = array($type, 'like', '%' . $key . '%');
  68. }
  69. $add_time_from = input('add_time_from');
  70. $add_time_to = input('add_time_to');
  71. if (trim($add_time_from) != '' || trim($add_time_to) != '') {
  72. if ($add_time_from !== false || $add_time_to !== false) {
  73. $condition[] = array('add_time', 'between time', array($add_time_from, $add_time_to));
  74. }
  75. }
  76. $seller_state = intval(input('state'));
  77. if ($seller_state > 0) {
  78. $condition[] = array('seller_state', '=', $seller_state);
  79. }
  80. $order_lock = intval(input('lock'));
  81. if ($order_lock) {
  82. $condition[] = array('order_lock', '=', $order_lock);
  83. }
  84. $refund_list = $refundreturn_model->getRefundList($condition, 10);
  85. $mobile_page = $refundreturn_model->page_info;
  86. $order_model = model('order');
  87. $trade_model = model('trade');
  88. $return_delay = $trade_model->getMaxDay('return_delay'); //发货默认5天后才能选择没收到
  89. foreach ($refund_list as $k => $v) {
  90. $goods_list = array();
  91. if ($v['goods_id'] > 0) {
  92. $goods = array();
  93. $goods['goods_id'] = $v['goods_id'];
  94. $goods['goods_name'] = $v['goods_name'];
  95. $goods['goods_img_480'] = goods_thumb($v, 480);
  96. $goods_list[] = $goods;
  97. } else {
  98. $condition = array();
  99. $condition[] = array('order_id', '=', $v['order_id']);
  100. $order_goods_list = $order_model->getOrdergoodsList($condition);
  101. foreach ($order_goods_list as $key => $value) {
  102. $goods = array();
  103. $goods['goods_id'] = $value['goods_id'];
  104. $goods['goods_name'] = $value['goods_name'];
  105. //$goods['goods_spec'] = $value['goods_spec'];
  106. $goods['goods_img_480'] = goods_thumb($value, 480);
  107. $goods_list[] = $goods;
  108. }
  109. }
  110. $refund_list[$k]['goods_list'] = $goods_list;
  111. $refund_state_array = $this->getRefundStateArray();
  112. $refund_list[$k]['seller_state_text'] = $refund_state_array['state_array'][$v['seller_state']];
  113. $refund_list[$k]['refund_state_text'] = $refund_state_array['admin_array'][$v['refund_state']];
  114. $refund_list[$k]['delay_time'] = TIMESTAMP - $v['delay_time'] - 60 * 60 * 24 * $return_delay;
  115. }
  116. $result = array_merge(array('refund_list' => $refund_list), mobile_page($mobile_page));
  117. ds_json_encode(10000, '', $result);
  118. }
  119. /**
  120. * @api {POST} api/Sellerrefund/edit 退款审核页
  121. * @apiVersion 1.0.0
  122. * @apiGroup Sellerrefund
  123. *
  124. * @apiHeader {String} X-DS-KEY 卖家授权token
  125. *
  126. * @apiParam {Int} refund_id 退款ID
  127. * @apiParam {Int} refund_type 类型 1退款 2退货
  128. * @apiParam {String} seller_message 卖家备注
  129. * @apiParam {Int} seller_state 卖家处理状态:1:待审核,2:同意,3:不同意
  130. *
  131. * @apiSuccess {String} code 返回码,10000为成功
  132. * @apiSuccess {String} message 返回消息
  133. * @apiSuccess {Object} result 返回数据
  134. */
  135. public function edit()
  136. {
  137. $refundreturn_model = model('refundreturn');
  138. $condition = array();
  139. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  140. $condition[] = array('refund_id', '=', intval(input('param.refund_id')));
  141. $refund = $refundreturn_model->getRefundreturnInfo($condition);
  142. if ($refund['seller_state'] != '1') {//检查状态,防止页面刷新不及时造成数据错误
  143. ds_json_encode(10001, lang('param_error'));
  144. }
  145. $order_id = $refund['order_id'];
  146. $refund_array = array();
  147. $refund_array['seller_time'] = TIMESTAMP;
  148. $refund_array['seller_state'] = input('post.seller_state'); //卖家处理状态:1为待审核,2为同意,3为不同意
  149. $refund_array['seller_message'] = input('post.seller_message');
  150. if ($refund_array['seller_state'] == '3') {
  151. $refund_array['refund_state'] = '3'; //状态:1为处理中,2为待管理员处理,3为已完成
  152. } else {
  153. $refund_array['seller_state'] = '2';
  154. $refund_array['refund_state'] = '2';
  155. }
  156. if ($refund['refund_type'] == 2 && $refund_array['seller_state'] == '2') {
  157. $return_type = intval(input('post.return_type'));
  158. $refund_array['return_type'] = $return_type;
  159. if (!in_array($return_type, array(1, 2))) {
  160. $refund_array['return_type'] = 2;
  161. }
  162. }
  163. $state = $refundreturn_model->editRefundreturn($condition, $refund_array);
  164. if ($state) {
  165. $sellerState = input('post.seller_state');
  166. if($sellerState == 2){
  167. //退款成功
  168. $order_model = model('order');
  169. $orderInfo = $order_model->getOrderInfo([['order_id', '=', $refund['order_id']]]);
  170. if ($orderInfo['card_id'] != 0 && $orderInfo['deduction_amount'] > 0) {
  171. //去退金额
  172. $cardModel = \think\facade\Db::name('sub_card');
  173. $cardInfo = $cardModel->where([['id', '=', $orderInfo['card_id']]])->find();
  174. if (!empty($cardInfo)) {
  175. $relBalance = $cardInfo['balance'] + $orderInfo['deduction_amount'];
  176. $updateCard = [
  177. 'balance' => $relBalance,
  178. 'freeze' => $cardInfo['freeze'] - $orderInfo['deduction_amount'],
  179. ];
  180. if ($relBalance > 0) {
  181. $updateCard['card_status'] = 2;
  182. }
  183. $res = $cardModel->where([['id', '=', $orderInfo['card_id']]])->update($updateCard);
  184. if ($res) {
  185. //删除消费记录
  186. $expenseModel = \think\facade\Db::name('sub_card_expense');
  187. $expenseModel->where([
  188. ['card_id', '=', $orderInfo['card_id']],
  189. ['order_id', '=', $refund['order_id']]
  190. ])->update(['expense_status' => 2]);
  191. }
  192. }
  193. }
  194. }
  195. if ($refund_array['seller_state'] == '3') {
  196. if ($refund['order_lock'] == '2') {
  197. $refundreturn_model->editOrderUnlock($order_id); //订单解锁
  198. }
  199. //自提点订单解锁
  200. $chain_order_model = model('chain_order');
  201. $chain_order_model->editChainOrderUnlock($order_id);
  202. }
  203. $this->recordSellerlog(lang('refund_processing') . $refund['refund_sn']);
  204. // 发送买家消息
  205. $param = array();
  206. $param['code'] = 'refund_return_notice';
  207. $param['member_id'] = $refund['buyer_id'];
  208. //阿里短信参数
  209. $param['ali_param'] = array(
  210. 'refund_sn' => $refund['refund_sn']
  211. );
  212. $param['ten_param'] = array(
  213. $refund['refund_sn']
  214. );
  215. $param['param'] = array_merge($param['ali_param'], array(
  216. 'refund_url' => HOME_SITE_URL . '/Memberrefund/view/?refund_id=' . $refund['refund_id'],
  217. ));
  218. //微信模板消息
  219. $param['weixin_param'] = array(
  220. 'url' => config('ds_config.h5_site_url') . '/member/' . ($refund['refund_type'] == 1 ? 'refund' : 'return') . '_view?refund_id=' . $refund['refund_id'],
  221. 'data' => array(
  222. "keyword1" => array(
  223. "value" => $refund['order_sn'],
  224. "color" => "#333"
  225. ),
  226. "keyword2" => array(
  227. "value" => $refund['refund_amount'],
  228. "color" => "#333"
  229. )
  230. ),
  231. );
  232. \mall\queue\QueueClient::push('sendMemberMsg', $param);
  233. ds_json_encode(10000, lang('ds_common_op_succ'));
  234. } else {
  235. ds_json_encode(10001, lang('ds_common_op_fail'));
  236. }
  237. }
  238. /**
  239. * @api {POST} api/Sellerrefund/get_refund_info 退款记录查看页
  240. * @apiVersion 1.0.0
  241. * @apiGroup Sellerrefund
  242. *
  243. * @apiHeader {String} X-DS-KEY 卖家授权token
  244. *
  245. * @apiParam {Int} refund_id 退款ID
  246. *
  247. * @apiSuccess {String} code 返回码,10000为成功
  248. * @apiSuccess {String} message 返回消息
  249. * @apiSuccess {Object} result 返回数据
  250. * @apiSuccess {String[]} result.pic_list 退货凭证列表
  251. * @apiSuccess {Object} result.refund 退货信息 (返回字段参考refundreturn)
  252. */
  253. public function get_refund_info()
  254. {
  255. $refundreturn_model = model('refundreturn');
  256. $condition = array();
  257. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  258. $condition[] = array('refund_id', '=', intval(input('param.refund_id')));
  259. $refund = $refundreturn_model->getRefundreturnInfo($condition);
  260. $info['buyer'] = array();
  261. if (!empty($refund['pic_info'])) {
  262. $info = unserialize($refund['pic_info']);
  263. }
  264. $pic_list = array();
  265. if (is_array($info['buyer'])) {
  266. foreach ($info['buyer'] as $k => $v) {
  267. if (!empty($v)) {
  268. $pic_list[] = ds_get_pic(ATTACH_PATH . '/refund', $v);
  269. }
  270. }
  271. }
  272. ds_json_encode(10000, '', array('refund' => $refund, 'pic_list' => $pic_list));
  273. }
  274. /**
  275. * @api {POST} api/Sellerrefund/receive 收货
  276. * @apiVersion 1.0.0
  277. * @apiGroup Sellerrefund
  278. *
  279. * @apiHeader {String} X-DS-KEY 卖家授权token
  280. *
  281. * @apiParam {Int} refund_id 退款ID
  282. * @apiParam {Int} return_type 物流状态 3:延迟收货(发货需要超过5天) 4:已收货
  283. *
  284. * @apiSuccess {String} code 返回码,10000为成功
  285. * @apiSuccess {String} message 返回消息
  286. * @apiSuccess {Object} result 返回数据
  287. */
  288. public function receive()
  289. {
  290. $refundreturn_model = model('refundreturn');
  291. $trade_model = model('trade');
  292. $condition = array();
  293. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  294. $condition[] = array('refund_id', '=', intval(input('param.return_id')));
  295. $return = $refundreturn_model->getRefundreturnInfo($condition);
  296. $return_delay = $trade_model->getMaxDay('return_delay'); //发货默认5天后才能选择没收到
  297. $delay_time = TIMESTAMP - $return['delay_time'] - 60 * 60 * 24 * $return_delay;
  298. if ($return['seller_state'] != '2' || $return['goods_state'] != '2') {//检查状态,防止页面刷新不及时造成数据错误
  299. ds_json_encode(10001, lang('param_error'));
  300. }
  301. $refund_array = array();
  302. if (input('post.return_type') == '3' && $delay_time > 0) {
  303. $refund_array['goods_state'] = '3';
  304. } else {
  305. $refund_array['receive_time'] = TIMESTAMP;
  306. $refund_array['receive_message'] = lang('confirm_receipt_goods_completed');
  307. $refund_array['refund_state'] = '2'; //状态:1为处理中,2为待管理员处理,3为已完成
  308. $refund_array['goods_state'] = '4';
  309. }
  310. $state = $refundreturn_model->editRefundreturn($condition, $refund_array);
  311. if ($state) {
  312. $this->recordSellerlog(lang('confirm_receipt_goods_returned') . $return['refund_sn']);
  313. // 发送买家消息
  314. $param = array();
  315. $param['code'] = 'refund_return_notice';
  316. $param['member_id'] = $return['buyer_id'];
  317. //阿里短信参数
  318. $param['ali_param'] = array(
  319. 'refund_sn' => $return['refund_sn']
  320. );
  321. $param['ten_param'] = array(
  322. $return['refund_sn']
  323. );
  324. $param['param'] = array_merge($param['ali_param'], array(
  325. 'refund_url' => HOME_SITE_URL . '/Memberreturn/view?return_id=' . $return['refund_id'],
  326. ));
  327. //微信模板消息
  328. $param['weixin_param'] = array(
  329. 'url' => config('ds_config.h5_site_url') . '/member/return_view?refund_id=' . $return['refund_id'],
  330. 'data' => array(
  331. "keyword1" => array(
  332. "value" => $return['order_sn'],
  333. "color" => "#333"
  334. ),
  335. "keyword2" => array(
  336. "value" => $return['refund_amount'],
  337. "color" => "#333"
  338. )
  339. ),
  340. );
  341. \mall\queue\QueueClient::push('sendMemberMsg', $param);
  342. ds_json_encode(10000, lang('ds_common_op_succ'));
  343. } else {
  344. ds_json_encode(10001, lang('ds_common_op_fail'));
  345. }
  346. }
  347. function getRefundStateArray()
  348. {
  349. $state_array = array(
  350. '1' => lang('refund_state_confirm'),
  351. '2' => lang('refund_state_yes'),
  352. '3' => lang('refund_state_no')
  353. ); //卖家处理状态:1为待审核,2为同意,3为不同意
  354. $admin_array = array(
  355. '1' => lang('in_processing'),
  356. '2' => lang('to_processed'),
  357. '3' => lang('has_been_completed'),
  358. '4' => lang('refund_state_no')
  359. ); //确认状态:1为买家或卖家处理中,2为待平台管理员处理,3为退款退货已完成
  360. return array('state_array' => $state_array, 'admin_array' => $admin_array,);
  361. }
  362. /**
  363. * @api {POST} api/Sellerrefund/search_deliver 物流跟踪
  364. * @apiVersion 1.0.0
  365. * @apiGroup Sellerrefund
  366. *
  367. * @apiHeader {String} X-DS-KEY 卖家授权token
  368. *
  369. * @apiParam {String} refund_id 退款id
  370. *
  371. * @apiSuccess {String} code 返回码,10000为成功
  372. * @apiSuccess {String} message 返回消息
  373. * @apiSuccess {Object} result 返回数据
  374. * @apiSuccess {String} result.express_name 物流公司名称
  375. * @apiSuccess {String} result.shipping_code 物流单号
  376. * @apiSuccess {Object[]} result.deliver_info 物流数据
  377. * @apiSuccess {String} result.deliver_info.context 内容
  378. * @apiSuccess {String} result.deliver_info.time 时间
  379. */
  380. public function search_deliver()
  381. {
  382. $refund_id = intval(input('post.refund_id'));
  383. if ($refund_id <= 0) {
  384. ds_json_encode(10001, lang('param_error'));
  385. }
  386. $refundreturn_model = model('refundreturn');
  387. $condition = array();
  388. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  389. $condition[] = array('refund_id', '=', $refund_id);
  390. $return = $refundreturn_model->getRefundreturnInfo($condition);
  391. if (empty($return)) {
  392. ds_json_encode(10001, lang('refund_not_exist'));
  393. }
  394. if (!$return['express_id'] || !$return['invoice_no']) {
  395. ds_json_encode(10001, lang('deliver_not_exist'));
  396. }
  397. $express = rkcache('express', true);
  398. if (isset($express[$return['express_id']])) {
  399. $express_code = $express[$return['express_id']]['express_code'];
  400. $express_name = $express[$return['express_id']]['express_name'];
  401. $deliver_info = $this->_get_express($express_code, $return['invoice_no']);
  402. } else {
  403. $express_code = '';
  404. $express_name = '';
  405. $deliver_info = array();
  406. }
  407. ds_json_encode(10000, '', array('express_name' => $express_name, 'shipping_code' => $return['invoice_no'], 'deliver_info' => $deliver_info));
  408. }
  409. /**
  410. * 从第三方取快递信息
  411. *
  412. */
  413. public function _get_express($express_code, $shipping_code)
  414. {
  415. $result = model('express')->queryExpress($express_code, $shipping_code);
  416. if ($result['Success'] != true) {
  417. ds_json_encode(10001, lang('deliver_search_fail'));
  418. }
  419. $content['Traces'] = array_reverse($result['Traces']);
  420. $output = array();
  421. if (is_array($content['Traces'])) {
  422. foreach ($content['Traces'] as $k => $v) {
  423. if ($v['AcceptTime'] == '')
  424. continue;
  425. //$output[] = $v['time'] . '&nbsp;&nbsp;' . $v['context'];
  426. $output[$k]['AcceptTime'] = $v['AcceptTime'];
  427. $output[$k]['AcceptStation'] = $v['AcceptStation'];
  428. }
  429. }
  430. if (empty($output))
  431. ds_json_encode(10001, lang('deliver_not_exist'));
  432. return $output;
  433. }
  434. }