Refundreturn.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class Refundreturn extends BaseModel {
  17. public $page_info;
  18. /**
  19. * 增加退款退货
  20. * @access public
  21. * @author csdeshang
  22. * @param type $refund_array 退款数组
  23. * @param type $order 排序
  24. * @param type $goods 商品数组
  25. * @return type
  26. */
  27. public function addRefundreturn($refund_array, $order = array(), $goods = array()) {
  28. if (!empty($order) && is_array($order)) {
  29. $refund_array['order_id'] = $order['order_id'];
  30. $refund_array['order_sn'] = $order['order_sn'];
  31. $refund_array['store_id'] = $order['store_id'];
  32. $refund_array['store_name'] = $order['store_name'];
  33. $refund_array['buyer_id'] = $order['buyer_id'];
  34. $refund_array['buyer_name'] = $order['buyer_name'];
  35. }
  36. if (!empty($goods) && is_array($goods)) {
  37. $refund_array['goods_id'] = $goods['goods_id'];
  38. $refund_array['order_goods_id'] = $goods['rec_id'];
  39. $refund_array['order_goods_type'] = $goods['goods_type'];
  40. $refund_array['goods_name'] = $goods['goods_name'];
  41. $refund_array['commis_rate'] = $goods['commis_rate'];
  42. $refund_array['goods_image'] = $goods['goods_image'];
  43. }
  44. $refund_array['refund_sn'] = $this->getRefundsn($refund_array['store_id']);
  45. $refund_id = Db::name('refundreturn')->insertGetId($refund_array);
  46. // 发送商家提醒
  47. $message = array();
  48. if (intval($refund_array['refund_type']) == 1) { // 退款
  49. $message['code'] = 'refund';
  50. } else { // 退货
  51. $message['code'] = 'return';
  52. }
  53. $message['store_id'] = $order['store_id'];
  54. $type = $refund_array['order_lock'] == 2 ? '售前' : '售后';
  55. $message['ali_param'] = array(
  56. 'type' => $type,
  57. 'refund_sn' => $refund_array['refund_sn']
  58. );
  59. $message['ten_param'] = array(
  60. $type,
  61. $refund_array['refund_sn']
  62. );
  63. $message['param'] = $message['ali_param'];
  64. //微信模板消息
  65. $message['weixin_param'] = array(
  66. 'url' => config('ds_config.h5_site_url') . '/seller/refund_form?refund_id=' . $refund_id . '&refund_type=' . $refund_array['refund_type'],
  67. 'data' => array(
  68. "keyword1" => array(
  69. "value" => $refund_array['order_sn'],
  70. "color" => "#333"
  71. ),
  72. "keyword2" => array(
  73. "value" => $refund_array['refund_amount'],
  74. "color" => "#333"
  75. )
  76. ),
  77. );
  78. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendStoremsg','cron_value'=>serialize($message)));
  79. return $refund_id;
  80. }
  81. /**
  82. * 订单锁定
  83. * @access public
  84. * @author csdeshang
  85. * @param type $order_id 订单编号
  86. * @return boolean
  87. */
  88. public function editOrderLock($order_id) {
  89. $order_id = intval($order_id);
  90. if ($order_id > 0) {
  91. $condition = array();
  92. $condition[] = array('order_id', '=', $order_id);
  93. $data = array();
  94. $data['lock_state'] = Db::raw('lock_state+1');
  95. $order_model = model('order');
  96. $result = $order_model->editOrder($data, $condition);
  97. return $result;
  98. }
  99. return false;
  100. }
  101. /**
  102. * 订单解锁
  103. * @access public
  104. * @author csdeshang
  105. * @param type $order_id 订单编号
  106. * @return boolean
  107. */
  108. public function editOrderUnlock($order_id) {
  109. $order_id = intval($order_id);
  110. if ($order_id > 0) {
  111. $condition = array();
  112. $condition[] = array('order_id', '=', $order_id);
  113. $condition[] = array('lock_state', '>=', '1');
  114. $data = array();
  115. $data['lock_state'] = Db::raw('lock_state-1');
  116. $data['delay_time'] = TIMESTAMP;
  117. $order_model = model('order');
  118. $result = $order_model->editOrder($data, $condition);
  119. return $result;
  120. }
  121. return false;
  122. }
  123. /**
  124. *
  125. * 修改记录
  126. * @access public
  127. * @author csdeshang
  128. * @param type $condition 条件
  129. * @param type $data 数据
  130. * @return boolean
  131. */
  132. public function editRefundreturn($condition, $data) {
  133. if (empty($condition)) {
  134. return false;
  135. }
  136. if (is_array($data)) {
  137. $result = Db::name('refundreturn')->where($condition)->update($data);
  138. return $result;
  139. } else {
  140. return false;
  141. }
  142. }
  143. /**
  144. * 退款
  145. * @access public
  146. * @author csdeshang
  147. * @param type $refund 退款
  148. * @return boolean
  149. */
  150. public function refundAmount($order, $refund_amount) {
  151. $order_model = model('order');
  152. //生成out_request_no 支付宝部分退款必传唯一的标识一次退款请求号
  153. $order['out_request_no'] = $order['order_sn'];
  154. $order_amount = $order['order_amount']; //订单金额
  155. $rcb_amount = $order['rcb_amount']; //充值卡支付金额
  156. $pd_amount = $order['pd_amount']; //预存款支付金额
  157. $predeposit_amount = $order_amount - $order['refund_amount'] - $rcb_amount; //可退预存款金额(预存款+在线支付金额) 在线支付可能原路返还
  158. $predeposit_model = model('predeposit');
  159. $not_trade_refund = TRUE; //在线支付 不原路返还
  160. $alipay_payment_list = array('alipay', 'alipay_app', 'alipay_h5');
  161. $wxpay_payment_list = array('wxpay_app', 'wxpay_h5', 'wxpay_jsapi', 'wxpay_minipro', 'wxpay_native');
  162. //未使用预存款支付 以及 充值卡支付的订单 才支持订单原路返还。
  163. if ($predeposit_amount > 0 && (in_array($order['payment_code'], $alipay_payment_list) || in_array($order['payment_code'], $wxpay_payment_list)) && $rcb_amount == 0 && $pd_amount == 0) {
  164. if (in_array($order['payment_code'], $alipay_payment_list)) {
  165. $payment_code = 'alipay';
  166. }
  167. if (in_array($order['payment_code'], $wxpay_payment_list)) {
  168. $payment_code = 'wxpay_native';
  169. }
  170. //调用支付接口处理原路退款
  171. $logic_payment = model('payment', 'logic');
  172. $result = $logic_payment->getPaymentInfo($payment_code);
  173. if (!$result['code']) {
  174. throw new \think\Exception($payment_code . '支付方法未开启', 10006);
  175. }
  176. $main_payment_info = $payment_info = $result['data'];
  177. if($payment_code == 'alipay' && !isset($payment_info['payment_config']['alipay_trade_refund_state'])){
  178. throw new \think\Exception($payment_code . '请配置支付宝支付', 10006);
  179. }
  180. if($payment_code == 'wxpay_native' && !isset($payment_info['payment_config']['wx_trade_refund_state'])){
  181. throw new \think\Exception($payment_code . '请配置微信支付', 10006);
  182. }
  183. //支付宝/微信 未开启原路返回
  184. if (($payment_code == 'alipay' && $payment_info['payment_config']['alipay_trade_refund_state'] == 1) || ($payment_code == 'wxpay_native' && $payment_info['payment_config']['wx_trade_refund_state'] == 1)) {
  185. $result = $logic_payment->getPaymentInfo($order['payment_code']);
  186. if (!$result['code']) {
  187. throw new \think\Exception($order['payment_code'] . '支付方法未开启', 10006);
  188. }
  189. $payment_info = $result['data'];
  190. //原路返还金额
  191. $trade_refund_amount = $refund_amount; //退预存款金额
  192. if ($refund_amount > $predeposit_amount) {
  193. $trade_refund_amount = $predeposit_amount;
  194. }
  195. $payment_info['payment_config']=array_merge($main_payment_info['payment_config'],$payment_info['payment_config']);
  196. $payment_api = new $payment_code($payment_info);
  197. $result = $payment_api->trade_refund($order, $trade_refund_amount);
  198. if (!$result['code']) {
  199. throw new \think\Exception($result['msg'], 10006);
  200. }
  201. $not_trade_refund = FALSE;
  202. }
  203. }
  204. if (($rcb_amount > 0) && ($refund_amount > $predeposit_amount) && $not_trade_refund) {//退充值卡
  205. $log_array = array();
  206. $log_array['member_id'] = $order['buyer_id'];
  207. $log_array['member_name'] = $order['buyer_name'];
  208. $log_array['order_sn'] = $order['order_sn'];
  209. $log_array['amount'] = $refund_amount;
  210. if ($predeposit_amount > 0) {
  211. $log_array['amount'] = $refund_amount - $predeposit_amount;
  212. }
  213. $state = $predeposit_model->changeRcb('refund', $log_array); //增加买家可用充值卡金额
  214. if (!$state) {
  215. throw new \think\Exception('充值卡退回失败', 10006);
  216. }
  217. }
  218. //全部退回预存款
  219. if ($predeposit_amount > 0 && $not_trade_refund) {
  220. $log_array = array();
  221. $log_array['member_id'] = $order['buyer_id'];
  222. $log_array['member_name'] = $order['buyer_name'];
  223. $log_array['order_sn'] = $order['order_sn'];
  224. $log_array['amount'] = $refund_amount; //退预存款金额
  225. if ($refund_amount > $predeposit_amount) {
  226. $log_array['amount'] = $predeposit_amount;
  227. }
  228. $state = $predeposit_model->changePd('refund', $log_array); //增加买家可用预存款金额
  229. if (!$state) {
  230. throw new \think\Exception('预存款退回失败', 10006);
  231. }
  232. }
  233. }
  234. /**
  235. * 平台确认退款处理
  236. * @access public
  237. * @author csdeshang
  238. * @param type $refund 退款
  239. * @return boolean
  240. */
  241. public function editOrderRefund($refund) {
  242. $refund_id = intval($refund['refund_id']);
  243. if ($refund_id > 0) {
  244. $order_id = $refund['order_id']; //订单编号
  245. $field = 'order_id,buyer_id,buyer_name,store_id,order_sn,order_amount,payment_code,order_state,refund_amount,rcb_amount,pd_amount,trade_no';
  246. $order_model = model('order');
  247. try {
  248. Db::startTrans();
  249. $order_model->lock = true;
  250. $order = $order_model->getOrderInfo(array('order_id' => $order_id), array(), $field);
  251. $state = 1;
  252. $this->refundAmount($order, $refund['refund_amount']);
  253. $goods_list = $order_model->getOrdergoodsList(array('order_id' => $order_id));
  254. //库存处理
  255. $data = array();
  256. if ($refund['goods_state'] == '4') {//如果是已收到退货信息,则增加商品库存
  257. $data[$refund['goods_id']] = $refund['goods_num'];
  258. }
  259. $pintuan_list = array(); //需要后续处理的促销活动
  260. foreach ($goods_list as $goods) {
  261. if ($refund['goods_id'] == 0) {//全部退款表示是发货前的退款,则增加商品库存
  262. $data[$goods['goods_id']] = $goods['goods_num'];
  263. }
  264. //如果是拼团
  265. if ($goods['goods_type'] == 6) {
  266. $pintuan_list[] = $goods;
  267. }
  268. }
  269. if (!empty($data)) {
  270. model('goods')->cancelOrderUpdateStorage($data);
  271. }
  272. //拼团退团
  273. if(!empty($pintuan_list)){
  274. foreach($pintuan_list as $goods){
  275. $ppintuangroup_info=Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->lock(true)->find();
  276. if($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state']==1){
  277. if($ppintuangroup_info['pintuangroup_joined']>0){
  278. Db::name('ppintuangroup')->where('pintuangroup_id', $goods['promotions_id'])->dec('pintuangroup_joined')->update();
  279. if($ppintuangroup_info['pintuangroup_joined']==1){
  280. //拼团统计开团数量
  281. $condition=array();
  282. $condition[]=array('pintuan_id','=', $ppintuangroup_info['pintuan_id']);
  283. $condition[]=array('pintuan_count','>', 0);
  284. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  285. }
  286. }
  287. }
  288. }
  289. }
  290. //同意退款之后,订单状态自动设置为已完成 , 因为涉及到月结算 成交的金额减去退款的金额, 交易成功后,买家次月产生的其他退款,由再下月进行结算
  291. if ($state && $order['order_state'] != ORDER_STATE_SUCCESS) {
  292. $update_order['order_state'] = ORDER_STATE_SUCCESS;
  293. $order_model->editOrder($update_order, array('order_id' => $order_id));
  294. //添加订单日志
  295. $data = array();
  296. $data['order_id'] = $order_id;
  297. $data['log_role'] = 'system';
  298. $data['log_msg'] = '平台审核退款,自动确认收货';
  299. $data['log_user'] = '系统';
  300. $data['log_orderstate'] = ORDER_STATE_SUCCESS;
  301. $order_model->addOrderlog($data);
  302. }
  303. if ($state) {
  304. $order_array = array();
  305. $order_amount = $order['order_amount']; //订单金额
  306. $refund_amount = $order['refund_amount'] + $refund['refund_amount']; //退款金额
  307. $order_array['refund_state'] = ($order_amount - $refund_amount) > 0 ? 1 : 2;
  308. $order_array['refund_amount'] = ds_price_format($refund_amount);
  309. $order_array['delay_time'] = TIMESTAMP;
  310. $state = $order_model->editOrder($order_array, array('order_id' => $order_id)); //更新订单退款
  311. if (!$state) {
  312. throw new \think\Exception('订单修改失败', 10006);
  313. }
  314. //修改分销佣金
  315. $condition=array();
  316. $condition[]=array('orderinviter_order_id','=',$order_id);
  317. $condition[]=array('orderinviter_valid','=',0);
  318. $condition[]=array('orderinviter_order_type','=',0);
  319. if($refund['goods_id']){
  320. $condition[]=array('orderinviter_goods_id','=',$refund['goods_id']);
  321. $orderinviter_list=Db::name('orderinviter')->where($condition)->select()->toArray();
  322. foreach($orderinviter_list as $orderinviter_info){
  323. $orderinviter_goods_amount=round($orderinviter_info['orderinviter_goods_amount']-$refund['refund_amount'],2);
  324. $orderinviter_money=round($orderinviter_info['orderinviter_ratio']/100*$orderinviter_goods_amount,2);
  325. Db::name('orderinviter')->where(array(array('orderinviter_id','=',$orderinviter_info['orderinviter_id'])))->update(['orderinviter_goods_amount' => $orderinviter_goods_amount,'orderinviter_money'=>$orderinviter_money]);
  326. }
  327. }else{
  328. $orderinviter_list=Db::name('orderinviter')->where($condition)->select()->toArray();
  329. foreach($orderinviter_list as $orderinviter_info){
  330. $orderinviter_goods_amount=round(($order_amount-$refund_amount)*$orderinviter_info['orderinviter_goods_amount']/$order_amount,2);
  331. $orderinviter_money=round($orderinviter_info['orderinviter_ratio']/100*$orderinviter_goods_amount,2);
  332. Db::name('orderinviter')->where(array(array('orderinviter_id','=',$orderinviter_info['orderinviter_id'])))->update(['orderinviter_goods_amount' => $orderinviter_goods_amount,'orderinviter_money'=>$orderinviter_money]);
  333. }
  334. }
  335. }
  336. if ($state && $refund['order_lock'] == '2') {
  337. $state = $this->editOrderUnlock($order_id); //订单解锁
  338. if (!$state) {
  339. throw new \think\Exception('订单解锁失败', 10006);
  340. }
  341. }
  342. //自提点订单取消
  343. $chain_order_model = model('chain_order');
  344. $chain_order_model->editChainOrderCancel($order_id, 1, (!$refund['order_goods_id'] || $refund['return_type']) ? 1 : 0);
  345. $chain_order_model->editChainOrderUnlock($order_id);
  346. Db::commit();
  347. return ds_callback(true);
  348. } catch (\Exception $e) {
  349. Db::rollback();
  350. return ds_callback(false, $e->getMessage());
  351. }
  352. }
  353. return ds_callback(false, '参数错误');
  354. }
  355. /**
  356. * 增加退款退货原因
  357. * @access public
  358. * @author csdeshang
  359. * @param type $reason_array 原因数组
  360. * @return type
  361. */
  362. public function addReason($reason_array) {
  363. $reason_id = Db::name('refundreason')->insertGetId($reason_array);
  364. return $reason_id;
  365. }
  366. /**
  367. * 修改退款退货原因记录
  368. * @access public
  369. * @author csdeshang
  370. * @param type $condition 条件
  371. * @param type $data 数据
  372. * @return boolean
  373. */
  374. public function editReason($condition, $data) {
  375. if (empty($condition)) {
  376. return false;
  377. }
  378. if (is_array($data)) {
  379. $result = Db::name('refundreason')->where($condition)->update($data);
  380. return $result;
  381. } else {
  382. return false;
  383. }
  384. }
  385. /**
  386. * 删除退款退货原因记录
  387. * @access public
  388. * @author csdeshang
  389. * @param type $condition 条件
  390. * @return boolean
  391. */
  392. public function delReason($condition) {
  393. if (empty($condition)) {
  394. return false;
  395. } else {
  396. $result = Db::name('refundreason')->where($condition)->delete();
  397. return $result;
  398. }
  399. }
  400. /**
  401. * 退款退货原因记录
  402. * @access public
  403. * @author csdeshang
  404. * @param type $condition 条件
  405. * @param type $pagesize 分页
  406. * @param type $limit 限制
  407. * @param type $fields 字段
  408. * @return array
  409. */
  410. public function getReasonList($condition = array(), $pagesize = '', $limit = 0, $fields = '*') {
  411. if ($pagesize) {
  412. $result_paginate = Db::name('refundreason')->field($fields)->where($condition)->order('reason_sort asc,reason_id desc')->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  413. $this->page_info = $result_paginate;
  414. $result = $result_paginate->items();
  415. } else {
  416. $result = Db::name('refundreason')->field($fields)->where($condition)->order('reason_sort asc,reason_id desc')->select()->toArray();
  417. }
  418. $result = ds_change_arraykey($result, 'reason_id');
  419. return $result;
  420. }
  421. /**
  422. * 获取退款退货记录
  423. * @access public
  424. * @author csdeshang
  425. * @param type $condition 条件
  426. * @param type $pagesize 分页
  427. * @param type $fields 字段
  428. * @param type $limit 限制
  429. * @return type
  430. */
  431. public function getRefundreturnList($condition = array(), $pagesize = '', $field = '*', $order = 'refund_id desc', $limit = 0) {
  432. if ($pagesize) {
  433. $result = Db::name('refundreturn')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  434. $this->page_info = $result;
  435. $result = $result->items();
  436. } else {
  437. $result = Db::name('refundreturn')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
  438. }
  439. return $result;
  440. }
  441. /**
  442. * 取退款记录列表
  443. * @access public
  444. * @author csdeshang
  445. * @param array $condition 条件
  446. * @param type $pagesize 分页
  447. * @return type
  448. */
  449. public function getRefundList($condition = array(), $pagesize = '', $field = '*', $order = 'refund_id desc', $limit = 0) {
  450. // if(!isset($condition['refund_type'])){
  451. // $condition['refund_type'] = '1'; //类型:1为退款,2为退货
  452. // }
  453. $result = $this->getRefundreturnList($condition, $pagesize, $field, $order, $limit);
  454. return $result;
  455. }
  456. /**
  457. * 取退货记录
  458. * @access public
  459. * @author csdeshang
  460. * @param array $condition 条件
  461. * @param type $pagesize 分页
  462. * @return type
  463. */
  464. public function getReturnList($condition = array(), $pagesize = '', $field = '*', $order = 'refund_id desc', $limit = 0) {
  465. $condition[] = array('refund_type', '=', '2'); //类型:1为退款,2为退货
  466. $result = $this->getRefundreturnList($condition, $pagesize, $field, $order, $limit);
  467. return $result;
  468. }
  469. /**
  470. * 退款退货申请编号
  471. * @access public
  472. * @author csdeshang
  473. * @param type $store_id 店铺id
  474. * @return string
  475. */
  476. public function getRefundsn($store_id) {
  477. $result = mt_rand(100, 999) . substr(100 + $store_id, -3) . date('ymdHis');
  478. return $result;
  479. }
  480. /**
  481. * 取一条记录
  482. * @access public
  483. * @author csdeshang
  484. * @param type $condition 条件
  485. * @param type $fields 字段
  486. * @return type
  487. */
  488. public function getRefundreturnInfo($condition = array(), $fields = '*') {
  489. return Db::name('refundreturn')->where($condition)->field($fields)->find();
  490. }
  491. /**
  492. * 根据订单取商品的退款退货状态
  493. * @access public
  494. * @author csdeshang
  495. * @param type $order_list 订单列表
  496. * @param type $order_refund 退款订单
  497. * @return string
  498. */
  499. public function getGoodsRefundList($order_list = array(), $order_refund = 0) {
  500. $order_ids = array(); //订单编号数组
  501. $order_ids = array_keys($order_list);
  502. $trade_model = model('trade');
  503. $condition = array();
  504. $condition[] = array('order_id', 'in', $order_ids);
  505. $refund_list = Db::name('refundreturn')->where($condition)->order('refund_id desc')->select()->toArray();
  506. $refund_goods = array(); //已经提交的退款退货商品
  507. if (!empty($refund_list) && is_array($refund_list)) {
  508. foreach ($refund_list as $key => $value) {
  509. $order_id = $value['order_id']; //订单编号
  510. $goods_id = $value['order_goods_id']; //订单商品表编号
  511. if (empty($refund_goods[$order_id][$goods_id])) {
  512. $refund_goods[$order_id][$goods_id] = $value;
  513. if ($order_refund > 0) {//订单下的退款退货所有记录
  514. $order_list[$order_id]['refund_list'] = $refund_goods[$order_id];
  515. }
  516. }
  517. }
  518. }
  519. if (!empty($order_list) && is_array($order_list)) {
  520. foreach ($order_list as $key => $value) {
  521. $order_id = $key;
  522. $goods_list = $value['extend_order_goods']; //订单商品
  523. $order_state = $value['order_state']; //订单状态
  524. $order_paid = $trade_model->getOrderState('order_paid'); //订单状态20:已付款
  525. $payment_code = $value['payment_code']; //支付方式
  526. if (!empty($refund_goods[$order_id][0])) {
  527. $order_list[$order_id]['extend_refund'] = $refund_goods[$order_id][0];
  528. }
  529. if (in_array($order_state, [ORDER_STATE_PAY, ORDER_STATE_PICKUP]) && $payment_code != 'offline') {//已付款未发货的非货到付款订单可以申请取消
  530. $order_list[$order_id]['refund'] = '1';
  531. } elseif ($order_state > ORDER_STATE_PICKUP && !empty($goods_list) && is_array($goods_list)) {//已发货后对商品操作
  532. $refund = $this->getRefundState($value); //根据订单状态判断是否可以退款退货
  533. foreach ($goods_list as $k => $v) {
  534. $goods_id = $v['rec_id']; //订单商品表编号
  535. if ($v['goods_pay_price'] > 0) {//实际支付额大于0的可以退款
  536. $v['refund'] = $refund;
  537. }
  538. if (!empty($refund_goods[$order_id][$goods_id])) {
  539. $seller_state = $refund_goods[$order_id][$goods_id]['seller_state']; //卖家处理状态:1为待审核,2为同意,3为不同意
  540. $refund_state = $refund_goods[$order_id][$goods_id]['refund_state'];
  541. if ($seller_state == 3 || $refund_state == 4) {
  542. $order_list[$order_id]['extend_complain'][$goods_id] = '1'; //不同意可以发起退款投诉
  543. } else {
  544. $v['refund'] = '0'; //已经存在处理中或同意的商品不能再操作
  545. }
  546. $v['extend_refund'] = $refund_goods[$order_id][$goods_id];
  547. } elseif (!empty($refund_goods[$order_id][0])) {
  548. $v['refund'] = '0';
  549. }
  550. $goods_list[$k] = $v;
  551. }
  552. }
  553. $order_list[$order_id]['extend_order_goods'] = $goods_list;
  554. }
  555. }
  556. return $order_list;
  557. }
  558. /**
  559. * 根据订单判断投诉订单商品是否可退款
  560. * @access public
  561. * @author csdeshang
  562. * @param type $order 订单
  563. * @param type $order_goods_id 订单商品id
  564. * @return type
  565. */
  566. public function getComplainRefundList($order, $order_goods_id = 0) {
  567. $list = array();
  568. $refund_list = array(); //已退或处理中商品
  569. $refund_goods = array(); //可退商品
  570. if (!empty($order) && is_array($order)) {
  571. $order_id = $order['order_id'];
  572. $order_list[$order_id] = $order;
  573. $order_list = $this->getGoodsRefundList($order_list);
  574. $order = $order_list[$order_id];
  575. $goods_list = $order['extend_order_goods'];
  576. $order_amount = $order['order_amount']; //订单金额
  577. $order_refund_amount = $order['refund_amount']; //订单退款金额
  578. foreach ($goods_list as $k => $v) {
  579. $goods_id = $v['rec_id']; //订单商品表编号
  580. if ($order_goods_id > 0 && $goods_id != $order_goods_id) {
  581. continue;
  582. }
  583. $v['refund_state'] = 3;
  584. if (!empty($v['extend_refund'])) {
  585. $v['refund_state'] = $v['extend_refund']['seller_state']; //卖家处理状态为3,不同意时能退款
  586. }
  587. if ($v['refund_state'] > 2) {//可退商品
  588. $goods_pay_price = $v['goods_pay_price']; //商品实际成交价
  589. if ($order_amount < ($goods_pay_price + $order_refund_amount)) {
  590. $goods_pay_price = $order_amount - $order_refund_amount;
  591. $v['goods_pay_price'] = $goods_pay_price;
  592. }
  593. $v['goods_refund'] = $v['goods_pay_price'];
  594. $refund_goods[$goods_id] = $v;
  595. } else {//已经存在处理中或同意的商品不能再退款
  596. $refund_list[$goods_id] = $v;
  597. }
  598. }
  599. }
  600. $list = array(
  601. 'refund' => $refund_list,
  602. 'goods' => $refund_goods
  603. );
  604. return $list;
  605. }
  606. /**
  607. * 详细页右侧订单信息
  608. * @access public
  609. * @author csdeshang
  610. * @param type $order_condition 条件
  611. * @param type $order_goods_id 订单商品id
  612. * @return type
  613. */
  614. public function getRightOrderList($order_condition, $order_goods_id = 0) {
  615. $order_model = model('order');
  616. $order_info = $order_model->getOrderInfo($order_condition, array('order_common', 'store'));
  617. $order_id = $order_info['order_id'];
  618. $order_common = $order_info['extend_order_common'];
  619. $order_info['order_common'] = $order_common;
  620. if ($order_common['shipping_express_id'] > 0) {
  621. $express = rkcache('express', true);
  622. if (isset($express[$order_common['shipping_express_id']])) {
  623. $order_info['express_code'] = $express[$order_common['shipping_express_id']]['express_code'];
  624. $order_info['express_name'] = $express[$order_common['shipping_express_id']]['express_name'];
  625. } else {
  626. $order_info['express_code'] = '';
  627. $order_info['express_name'] = '';
  628. }
  629. }
  630. $condition = array();
  631. $condition[] = array('order_id', '=', $order_id);
  632. if ($order_goods_id > 0) {
  633. $condition[] = array('rec_id', '=', $order_goods_id); //订单商品表编号
  634. }
  635. $goods_list = $order_model->getOrdergoodsList($condition);
  636. $order_info['goods_list'] = $goods_list;
  637. return $order_info;
  638. }
  639. /**
  640. * 根据订单状态判断是否可以退款退货
  641. * @access public
  642. * @author csdeshang
  643. * @param type $order 订单
  644. * @return bool
  645. */
  646. public function getRefundState($order) {
  647. $refund = '0'; //默认不允许退款退货
  648. $order_state = $order['order_state']; //订单状态
  649. $trade_model = model('trade');
  650. $order_shipped = $trade_model->getOrderState('order_shipped'); //30:已发货
  651. $order_completed = $trade_model->getOrderState('order_completed'); //40:已收货
  652. switch ($order_state) {
  653. case $order_shipped:
  654. $payment_code = $order['payment_code']; //支付方式
  655. if ($payment_code != 'offline') {//货到付款订单在没确认收货前不能退款退货
  656. $refund = '1';
  657. } else {
  658. $refund = '0';
  659. }
  660. break;
  661. case $order_completed:
  662. $order_refund = $trade_model->getMaxDay('order_refund'); //15:收货完成后可以申请退款退货
  663. $delay_time = $order['delay_time'] + 60 * 60 * 24 * $order_refund;
  664. if ($delay_time > TIMESTAMP) {
  665. $refund = '1';
  666. } else {
  667. $refund = '0';
  668. }
  669. if ($order['ob_no']) {//已结算不可以退款
  670. $refund = '0';
  671. }
  672. break;
  673. default:
  674. $refund = '0';
  675. break;
  676. }
  677. return $refund;
  678. }
  679. /**
  680. * 退货退款数量
  681. * @access public
  682. * @author csdeshang
  683. * @param array $condition 条件
  684. * @return int
  685. */
  686. public function getRefundreturnCount($condition) {
  687. return Db::name('refundreturn')->where($condition)->count();
  688. }
  689. /**
  690. * 取得退款数量
  691. * @access public
  692. * @author csdeshang
  693. * @param array $condition 条件
  694. * @return type
  695. */
  696. public function getRefundCount($condition) {
  697. $condition[] = array('refund_type', '=', 1);
  698. return Db::name('refundreturn')->where($condition)->count();
  699. }
  700. /**
  701. * 取得退款退货数量
  702. * @access public
  703. * @author csdeshang
  704. * @param array $condition 条件
  705. * @return int
  706. */
  707. public function getReturnCount($condition) {
  708. $condition[] = array('refund_type', '=', 2);
  709. return Db::name('refundreturn')->where($condition)->count();
  710. }
  711. /**
  712. * 获得退货退款的店铺列表
  713. * @access public
  714. * @author csdeshang
  715. * @param type $list 店铺列表
  716. * @return array
  717. */
  718. public function getRefundStoreList($list) {
  719. $store_ids = array();
  720. if (!empty($list) && is_array($list)) {
  721. foreach ($list as $key => $value) {
  722. $store_ids[] = $value['store_id']; //店铺编号
  723. }
  724. }
  725. $field = 'store_id,store_name,member_id,member_name,store_qq,store_ww,store_phone';
  726. return model('store')->getStoreMemberIDList($store_ids, $field);
  727. }
  728. }
  729. ?>