Refund.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Refund
  5. {
  6. # 设置订单的类型
  7. public function set($type)
  8. {
  9. $this->type = $type;
  10. if ($type == 'dh') {
  11. $this->order_table = 'agent/dh_order';
  12. $this->goods_table = 'agent/dh_order_goods';
  13. $this->refund_table = 'agent/dh_order_refund';
  14. $this->lib = 'agent/lib/dhorder';
  15. } elseif ($type == 'card') {
  16. $this->order_table = 'card/order';
  17. $this->goods_table = 'card/order_card';
  18. $this->refund_table = 'shop/order_refund';
  19. $this->lib = 'card/lib/buy';
  20. } else {
  21. $this->order_table = 'shop/' . $type . '_order';
  22. $this->goods_table = 'shop/' . $type . '_order_goods';
  23. $this->refund_table = 'shop/' . $type . '_order_refund';
  24. if ($type == 'buy') {
  25. $this->lib = 'mshop/lib/' . $type;
  26. } else {
  27. $this->lib = 'shop/lib/' . $type;
  28. }
  29. }
  30. return $this;
  31. }
  32. # 获取退款记录
  33. public function getList($type, $id, $order_id)
  34. {
  35. $order = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
  36. $where['order_id'] = $order_id;
  37. $data = Dever::db($this->refund_table)->select_page($where);
  38. if ($data) {
  39. $goods_status = Dever::db($this->goods_table)->config['status'];
  40. $refund_status = Dever::db($this->refund_table)->config['status'];
  41. $refund_process = Dever::db($this->refund_table)->config['process'];
  42. foreach ($data as $k => $v) {
  43. if ($v['cash'] <= 0 && $v['re_cash'] > 0) {
  44. $data[$k]['cash'] = $v['cash'] = $v['re_cash'];
  45. }
  46. $data[$k]['cdate'] = date('Y-m-d H:i', $v['cdate']);
  47. if ($v['order_goods_id'] != -1 && $v['order_goods_id']) {
  48. $data[$k]['goods'] = Dever::db($this->goods_table)->getDataByIds(array('ids' => $v['order_goods_id']));
  49. } else {
  50. $data[$k]['goods'] = Dever::db($this->goods_table)->getDataByIds(array('order_id' => $v['order_id']));
  51. }
  52. $data[$k]['status_name'] = $refund_status[$v['status']];
  53. $data[$k]['process_name'] = $refund_process[$v['process']];
  54. if ($data[$k]['goods']) {
  55. foreach ($data[$k]['goods'] as $k1 => $v1) {
  56. $data[$k]['goods'][$k1]['info'] = Dever::load('goods/lib/info')->getPayInfo($v1['goods_id'], $v1['sku_id']);
  57. $data[$k]['goods'][$k1]['status_name'] = $goods_status[$v1['status']];
  58. }
  59. }
  60. }
  61. }
  62. return $data;
  63. }
  64. # 更新退款记录
  65. public function up($order_id, $order_goods_id, $status, $price, $p_price, $num = false, $desc = '', $pic = '', $process = 1, $order = array(), $re_cash = 0)
  66. {
  67. $data['order_id'] = $order_id;
  68. if (!$order_goods_id) {
  69. $where['order_id'] = $order_id;
  70. $goods = Dever::db($this->goods_table)->getIds($where);
  71. if ($goods) {
  72. $order_goods_id = implode(',', array_keys($goods));
  73. }
  74. $data['type'] = 1;
  75. } else {
  76. $data['type'] = 2;
  77. }
  78. $data['order_goods_id'] = $order_goods_id;
  79. $info = Dever::db($this->refund_table)->find($data);
  80. $data['status'] = $status;
  81. $data['cash'] = $price;
  82. if ($p_price) {
  83. $data['p_cash'] = $p_price;
  84. }
  85. $data['re_cash'] = $re_cash;
  86. if ($num) {
  87. $data['num'] = $num;
  88. }
  89. $data['desc'] = $desc;
  90. $data['pic'] = $pic;
  91. $data['process'] = $process;
  92. if ($order && isset($order['parent_type']) && $order['parent_type'] > 1) {
  93. $data['parent_type'] = $order['parent_type'];
  94. $data['parent_order_id'] = $order['parent_order_id'];
  95. }
  96. if ($info) {
  97. $data['where_id'] = $info['id'];
  98. $state = Dever::db($this->refund_table)->update($data);
  99. if ($state) {
  100. $data['id'] = $info['id'];
  101. return $data;
  102. }
  103. } else {
  104. $state = Dever::db($this->refund_table)->insert($data);
  105. if ($state) {
  106. $data['id'] = $state;
  107. return $data;
  108. }
  109. }
  110. return false;
  111. }
  112. # 申请退款
  113. public function apply($type, $id, $order_id, $order_goods_id, $status = 1, $num = 0, $desc = '', $pic = '', $process = 2, $buy_status = false)
  114. {
  115. if ($status != 1 && $status != 2 && $status != 3) {
  116. Dever::alert('当前订单状态不允许退货退款');
  117. }
  118. $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
  119. if (isset($data['withdraw']) && $data['withdraw'] == 2) {
  120. Dever::alert('当前订单状态不允许退货退款');
  121. }
  122. $auth = false;
  123. if ($this->type == 'sell' && $type == 1) {
  124. if ($data['status'] == 2) {
  125. $auth = true;
  126. }
  127. } else {
  128. $config = array(2,3,4,5,6);
  129. if (in_array($data['status'], $config)) {
  130. $auth = true;
  131. }
  132. }
  133. if ($auth) {
  134. $re_cash = 0;
  135. if ($order_goods_id) {
  136. $info = Dever::db($this->goods_table)->find(array('id' => $order_goods_id, 'order_id' => $data['id']));
  137. if ($info) {
  138. Dever::db($this->goods_table)->update(array('where_id' => $info['id'], 'status' => 2));
  139. if (!$num) {
  140. $num = $info['num'];
  141. }
  142. $info['total_price'] = $info['price'] * $num;
  143. if (isset($info['discount_cash']) && $info['discount_cash']) {
  144. $info['total_price'] = $info['total_price'] - $info['discount_cash'];
  145. }
  146. if (isset($info['coupon_cash']) && $info['coupon_cash']) {
  147. $info['total_price'] = $info['total_price'] - $info['coupon_cash'];
  148. }
  149. if (isset($data['card_code_cash']) && $data['card_code_cash']) {
  150. $kou = $data['card_code_cash'] + $data['wallet_cash'];
  151. if ($kou > 0) {
  152. if ($info['total_price'] >= $kou) {
  153. $info['total_price'] = $info['total_price'] - $kou;
  154. } else {
  155. $re_cash = $info['total_price'];
  156. $info['total_price'] = 0;
  157. }
  158. }
  159. }
  160. if (isset($data['record_cash']) && $data['record_cash']) {
  161. if ($info['total_price'] >= $data['record_cash']) {
  162. $info['total_price'] = $info['total_price'] - $data['record_cash'];
  163. } else {
  164. $re_cash = $info['total_price'];
  165. $info['total_price'] = 0;
  166. }
  167. }
  168. $cash = $info['total_price'];
  169. if (isset($info['p_price'])) {
  170. $p_cash = $info['p_price'] * $num;
  171. } else {
  172. $p_cash = 0;
  173. }
  174. } else {
  175. Dever::alert('您没有权限操作1');
  176. }
  177. } else {
  178. Dever::db($this->goods_table)->updates(array('option_order_id' => $data['id'], 'option_status' => 1, 'set_status' => 2));
  179. if (!$num) {
  180. $num = $data['num'];
  181. }
  182. if (!$data['refund_cash']) {
  183. $data['refund_cash'] = 0;
  184. }
  185. if ($data['price'] <= 0 && isset($data['p_price'])) {
  186. $data['price'] = $data['p_price'];
  187. }
  188. $cash = $data['price'] - $data['refund_cash'];
  189. $num = $data['num'] - $data['refund_num'];
  190. if (isset($data['p_price']) && isset($data['refund_p_cash'])) {
  191. $p_cash = $data['p_price'] - $data['refund_p_cash'];
  192. } else {
  193. $p_cash = 0;
  194. }
  195. if (isset($data['ps_cash']) && $data['ps_cash'] > 0 && $data['status'] >= 3) {
  196. # 已发货不退配送费
  197. $cash -= $data['ps_cash'];
  198. }
  199. #新加
  200. if(isset($data['record_cash']) && $data['record_cash']>0){
  201. $re_cash = $data['record_cash'];
  202. }
  203. }
  204. $cash = round($cash, 2);
  205. if ($cash >= 0) {
  206. $log = $this->up($data['id'], $order_goods_id, $status, $cash, $p_cash, $num, $desc, $pic, $process, $data, $re_cash);
  207. if (!$log) {
  208. Dever::alert('退款失败');
  209. }
  210. if ($this->type == 'buy') {
  211. # 如果是采购单,这里要把库存先减掉
  212. if ($data['status'] == 5 || $data['status'] == 6) {
  213. $oper_data = Dever::db($this->goods_table)->getDataByIds(array('ids' => $log['order_goods_id']));
  214. Dever::load('shop/lib/goods')->oper($data, 2, 1, $oper_data);
  215. }
  216. if ($data['source_id'] && $data['source_id'] > 0) {
  217. $data['num'] = $num;
  218. Dever::load('cash/lib/order')->up($data, 2, 1, $log);
  219. } else {
  220. $data['num'] = $num;
  221. Dever::load('cash/lib/order')->up($data, 1, 3);
  222. }
  223. # 发消息给供应商
  224. if ($data['source_id'] && Dever::project('message')) {
  225. $msg = '您的订单'.$data['order_num'].',有新的商品问题。';
  226. $msg_param['type'] = 1;
  227. $msg_param['id'] = $data['id'];
  228. $msg_param = Dever::json_encode($msg_param);
  229. $pid = $data['source_type'] + 1;
  230. Dever::load('message/lib/data')->push(-1, $data['source_id'], '退货与报损通知', $msg, 2, $pid, false, $msg_param);
  231. }
  232. }
  233. if (!$data['refund_cash']) {
  234. $data['refund_cash'] = 0;
  235. }
  236. if (isset($data['refund_p_cash']) && !$data['refund_p_cash']) {
  237. $data['refund_p_cash'] = 0;
  238. }
  239. $update = array();
  240. $update['where_id'] = $data['id'];
  241. $update['refund_cash'] = $data['refund_cash'] + $cash + $re_cash;
  242. $update['refund_num'] = $data['refund_num'] + $num;
  243. if (isset($data['refund_p_cash'])) {
  244. $update['refund_p_cash'] = $data['refund_p_cash'] + $p_cash;
  245. }
  246. $update['refund_status'] = 2;
  247. Dever::db($this->order_table)->update($update);
  248. if ($id && $process == 2) {
  249. $this->action($log, $process, $data, true, $buy_status);
  250. }
  251. } else {
  252. Dever::alert('您没有可以退款的金额');
  253. }
  254. } else {
  255. Dever::alert('您没有权限操作2');
  256. }
  257. return 'reload';
  258. }
  259. # 确认退款
  260. public function action($info, $process, $order = false, $cash = true, $buy_status = false)
  261. {
  262. if (!is_array($info)) {
  263. $info = Dever::db($this->refund_table)->find($info);
  264. }
  265. if (!$info) {
  266. Dever::alert('您没有权限操作3');
  267. }
  268. if (!$order) {
  269. $order = Dever::db($this->order_table)->find($info['order_id']);
  270. }
  271. if (!$order) {
  272. Dever::alert('您没有权限操作4');
  273. }
  274. if (isset($order['withdraw']) && $order['withdraw'] == 2) {
  275. Dever::alert('当前订单状态不允许退货退款');
  276. }
  277. $state = Dever::db($this->refund_table)->update(array('where_id' => $info['id'], 'process' => $process));
  278. if (!$state) {
  279. Dever::alert('操作失败');
  280. }
  281. $update = $data = array();
  282. if ($process == 3) {
  283. if ($info['num'] && $info['num'] > 0) {
  284. //$update['num'] = $order['num'] + $info['num'];
  285. }
  286. $update['refund_num'] = $order['refund_num'] - $info['num'];
  287. $update['refund_cash'] = $order['refund_cash'] - $info['cash'];
  288. if (isset($order['refund_p_cash'])) {
  289. $update['refund_p_cash'] = $order['refund_p_cash'] - $info['p_cash'];
  290. }
  291. $update['where_id'] = $order['id'];
  292. if ($update['refund_cash'] <= 0) {
  293. $update['refund_status'] = 1;
  294. } else {
  295. $update['refund_status'] = 3;
  296. }
  297. # 检测是否还有别的退款申请
  298. $refund_data = Dever::db($this->refund_table)->find(array('order_id' => $order['id'], 'process' => 1));
  299. if ($refund_data) {
  300. $update['refund_status'] = 2;
  301. }
  302. $state = Dever::db($this->order_table)->update($update);
  303. if ($info['type'] == 2) {
  304. Dever::db($this->goods_table)->update(array('where_id' => $info['order_goods_id'], 'status' => 1));
  305. } else {
  306. Dever::db($this->goods_table)->updates(array('option_order_id' => $order['id'], 'option_status' => 2, 'set_status' => 1));
  307. }
  308. if ($cash && $this->type == 'buy' && $order['source_id'] && $order['source_id'] > 0) {
  309. $order['num'] = $info['num'];
  310. Dever::load('cash/lib/order')->up($order, 2, 3, $info);
  311. }
  312. } else {
  313. if ($info['type'] == 2) {
  314. if ($info['order_goods_id']) {
  315. $data = Dever::db($this->goods_table)->getDataByIds(array('ids' => $info['order_goods_id'], 'order_id' => $info['order_id'], 'clear' => true));
  316. }
  317. $state = Dever::db($this->goods_table)->update(array('where_id' => $info['order_goods_id'], 'status' => 3));
  318. if ($state) {
  319. $this->notice($state, $order, $info);
  320. # 检查这个订单下的商品是不是都退了
  321. $total = Dever::db($this->goods_table)->getTotal(array('order_id' => $order['id'], 'status' => '1,2'));
  322. if ($total <= 0) {
  323. $update['operdate'] = time();
  324. $update['status'] = 8;
  325. $update['refund_status'] = 4;
  326. } elseif ($order['status'] == 5) {
  327. $update['status'] = 6;
  328. $update['refund_status'] = 3;
  329. } else {
  330. $update['refund_status'] = 3;
  331. }
  332. if ($update['refund_status'] == 3) {
  333. $refund_data = Dever::db($this->refund_table)->find(array('order_id' => $order['id'], 'process' => 1));
  334. if ($refund_data) {
  335. $update['refund_status'] = 2;
  336. }
  337. }
  338. if ($update) {
  339. $update['where_id'] = $order['id'];
  340. $state = Dever::db($this->order_table)->update($update);
  341. }
  342. if ($buy_status != -1) {
  343. if (isset($order['parent_type']) && $order['parent_type'] > 1 && isset($order['parent_order_id']) && $order['parent_order_id']) {
  344. # 把关联订单也退掉
  345. $goods_info = Dever::db($this->goods_table)->find($info['order_goods_id']);
  346. if ($goods_info) {
  347. if ($order['parent_type'] == 2) {
  348. $sell_order_goods = Dever::db('shop/sell_order_goods')->find(array('order_id' => $order['parent_order_id'], 'goods_id' => $goods_info['goods_id'], 'sku_id' => $goods_info['sku_id']));
  349. } elseif ($order['parent_type'] == 3) {
  350. $dh_order_goods = Dever::db('agent/dh_order_goods')->find(array('order_id' => $order['parent_order_id'], 'goods_id' => $goods_info['goods_id'], 'sku_id' => $goods_info['sku_id']));
  351. }
  352. }
  353. } elseif (isset($order['shop_type']) && $order['shop_type'] == 2) {
  354. # 把关联订单也退掉
  355. $buy_order = Dever::db('shop/buy_order')->select(array('parent_type' => 2, 'parent_order_id' => $info['order_id']));
  356. if ($buy_order) {
  357. $goods_info = Dever::db($this->goods_table)->find($info['order_goods_id']);
  358. foreach ($buy_order as $k => $v) {
  359. $temp = Dever::db('shop/buy_order_goods')->find(array('order_id' => $v['id'], 'goods_id' => $goods_info['goods_id'], 'sku_id' => $goods_info['sku_id']));
  360. if ($temp) {
  361. $temp['type'] = $v['type'];
  362. $temp['type_id'] = $v['type_id'];
  363. $buy_order_goods = $temp;
  364. }
  365. }
  366. }
  367. } elseif (isset($order['mid']) && $order['mid']) {
  368. # 把关联订单也退掉
  369. $buy_order = Dever::db('shop/buy_order')->select(array('parent_type' => 3, 'parent_order_id' => $info['order_id']));
  370. if ($buy_order) {
  371. $goods_info = Dever::db($this->goods_table)->find($info['order_goods_id']);
  372. foreach ($buy_order as $k => $v) {
  373. $temp = Dever::db('shop/buy_order_goods')->find(array('order_id' => $v['id'], 'goods_id' => $goods_info['goods_id'], 'sku_id' => $goods_info['sku_id']));
  374. if ($temp) {
  375. $temp['type'] = $v['type'];
  376. $temp['type_id'] = $v['type_id'];
  377. $buy_order_goods = $temp;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. } else {
  385. $update['where_id'] = $order['id'];
  386. $update['operdate'] = time();
  387. if (isset($order['ps_cash']) && $order['ps_cash'] > 0 && $order['status'] < 3) {
  388. # 配送费退掉
  389. //$update['ps_cash'] = 0;
  390. }
  391. $update['status'] = 8;
  392. $update['refund_status'] = 4;
  393. $state = Dever::db($this->order_table)->update($update);
  394. if ($state) {
  395. $data = Dever::db($this->goods_table)->getDataByIds(array('order_id' => $order['id'], 'status' => 2, 'clear' => true));
  396. Dever::db($this->goods_table)->updates(array('option_order_id' => $order['id'], 'option_status' => 2, 'set_status' => 3));
  397. $this->notice($state, $order, $info);
  398. if (isset($order['user_coupon_id']) && $order['user_coupon_id']) {
  399. # 还原优惠券
  400. Dever::db('shop/user_coupon')->update(array('where_id' => $order['user_coupon_id'], 'status' => 1));
  401. }
  402. }
  403. }
  404. $re_cash = $info['re_cash'];
  405. if (isset($order['card_code_id']) && $order['card_code_id'] && $order['card_code_cash'] > 0) {
  406. # 还原礼品卡
  407. if ($re_cash > 0) {
  408. $card_cash = $re_cash;
  409. } else {
  410. $card_cash = $order['card_code_cash'];
  411. }
  412. if ($re_cash > 0) {
  413. $card_code_cash = $order['card_code_cash'] - $re_cash;
  414. $order['card_code_cash'] = $re_cash;
  415. Dever::db($this->order_table)->update(array('where_id' => $order['id'], 'card_code_cash' => $card_code_cash, 'clear' => true));
  416. }
  417. Dever::db('card/code')->decUseCash(array('where_id' => $order['card_code_id'], 'set_use_cash' => $order['card_code_cash']));
  418. # 生成结算单
  419. $code = Dever::db('card/code')->find($order['card_code_id']);
  420. if ($code) {
  421. $card_info = Dever::db('card/info')->find($code['card_id']);
  422. if ($card_info) {
  423. if ($card_info['price'] == $card_info['value']) {
  424. $price = $card_cash;
  425. } else {
  426. $zhe = round($card_info['price']/$card_info['value'], 2);
  427. $price = round($card_cash*$zhe, 2);
  428. }
  429. $rorder = $order;
  430. $rinfo = $info;
  431. $rorder['type'] = 1;
  432. $rorder['type_id'] = $rorder['shop_id'];
  433. $rorder['source_type'] = 4;
  434. $rorder['source_id'] = -1;
  435. $rorder['num'] = $info['num'];
  436. $rinfo['cash'] = $rorder['price'] = $price;
  437. $rinfo['p_cash'] = $rorder['p_price'] = $card_cash;
  438. if (isset($update['status'])) {
  439. $rorder['status'] = $update['status'];
  440. }
  441. Dever::load('cash/lib/order')->up($rorder, 2, 2, $rinfo, 2);
  442. }
  443. }
  444. } elseif (isset($order['wallet_cash']) && $order['wallet_cash'] && $order['wallet_cash'] > 0) {
  445. # 还原钱包
  446. if ($re_cash > 0) {
  447. $wallet_cash = $re_cash;
  448. } else {
  449. $wallet_cash = $order['wallet_cash'];
  450. }
  451. if ($re_cash > 0) {
  452. $wallet_cash = $order['wallet_cash'] - $re_cash;
  453. $order['wallet_cash'] = $re_cash;
  454. Dever::db($this->order_table)->update(array('where_id' => $order['id'], 'wallet_cash' => $wallet_cash, 'clear' => true));
  455. }
  456. Dever::load('shop/lib/money')->up($order['uid'], 4, $wallet_cash, $order['id'], '商品退款', 2);
  457. $rorder = $order;
  458. $rinfo = $info;
  459. $rorder['type'] = 1;
  460. $rorder['type_id'] = $rorder['shop_id'];
  461. $rorder['source_type'] = 4;
  462. $rorder['source_id'] = -1;
  463. $rorder['num'] = $info['num'];
  464. $rinfo['cash'] = $rorder['price'] = $wallet_cash;
  465. $rinfo['p_cash'] = $rorder['p_price'] = $order['wallet_cash'];
  466. if (isset($update['status'])) {
  467. $rorder['status'] = $update['status'];
  468. }
  469. Dever::load('cash/lib/order')->up($rorder, 2, 2, $rinfo, 3);
  470. }elseif (isset($order['record_cash']) && $order['record_cash'] && $order['record_cash'] > 0) {
  471. # 还原资金账户
  472. if ($re_cash > 0) {
  473. $record_cash = $re_cash;
  474. } else {
  475. $record_cash = $order['record_cash'];
  476. }
  477. if ($re_cash > 0) {
  478. $record_cash = $order['record_cash'] - $re_cash;
  479. $order['record_cash'] = $re_cash;
  480. Dever::db($this->order_table)->update(array('where_id' => $order['id'], 'record_cash' => $record_cash, 'clear' => true));
  481. }
  482. $record = Dever::db('shop/record')->find(array('type_id'=>$info['order_id'],'shop_id'=>$order['type_id']));
  483. $shop = Dever::db('shop/info')->find(array('id'=>$order['type_id']));
  484. if($shop && $record){
  485. if(!$shop['price']){
  486. $shop['price'] = 0;
  487. }
  488. $price = $shop['price'] + $info['re_cash'];
  489. $explan = '订单退货返还';
  490. Dever::load('shop/lib/record')->pingtai($shop['id'],$shop['sid'],3,$info['order_id'],$info['re_cash'],$price,$explan);
  491. }
  492. $rorder = $order;
  493. $rinfo = $info;
  494. $rorder['type'] = 1;
  495. $rorder['type_id'] = $rorder['type_id'];
  496. $rorder['source_type'] = 4;
  497. $rorder['source_id'] = -1;
  498. $rorder['num'] = $info['num'];
  499. $rinfo['cash'] = $rorder['price'] = $record_cash;
  500. $rinfo['p_cash'] = $rorder['p_price'] = $order['record_cash'];
  501. if (isset($update['status'])) {
  502. $rorder['status'] = $update['status'];
  503. }
  504. Dever::load('cash/lib/order')->up($rorder, 2, 2, $rinfo, 3);
  505. }
  506. if ($cash && isset($order['source_id']) && $order['source_id'] && $order['source_id'] > 0) {
  507. $order['num'] = $info['num'];
  508. if (isset($update['status'])) {
  509. $order['status'] = $update['status'];
  510. }
  511. Dever::load('cash/lib/order')->up($order, 2, 2, $info);
  512. }
  513. }
  514. if ($this->type == 'sell' && $process == 2) {
  515. # 如果是销售单,退款后要减少销量
  516. Dever::load('shop/lib/goods')->oper($order, 2, 2, $data);
  517. } elseif ($this->type == 'buy' && $process == 3 && ($order['status'] == 5 || $order['status'] == 6)) {
  518. # 如果是采购单,退款后要恢复库存
  519. Dever::load('shop/lib/goods')->oper($order, 1, 1, $data);
  520. } elseif ($this->type == 'buy' && $process == 2 && $order['source_type'] == 2) {
  521. # 如果是采购单,退款后要恢复库存
  522. Dever::load('store/lib/goods')->oper($order['source_id'], 2, 2, $data);
  523. } elseif ($this->type == 'dh' && $process == 2) {
  524. # 如果是兑换单,要恢复
  525. Dever::load('agent/lib/dhorder')->updateSell($order, 2);
  526. }
  527. if (isset($sell_order_goods) && $sell_order_goods) {
  528. Dever::load('shop/lib/refund')->set('sell')->apply(2, $sell_order_goods['shop_id'], $sell_order_goods['order_id'], $sell_order_goods['id'], 2, 0, '退货退款', '', 2, -1);
  529. } elseif (isset($buy_order_goods) && $buy_order_goods) {
  530. Dever::load('shop/lib/refund')->set('buy')->apply($buy_order_goods['type'], $buy_order_goods['type_id'], $buy_order_goods['order_id'], $buy_order_goods['id'], $buy_status, 0, $info['desc'], '', 2, -1);
  531. } elseif (isset($dh_order_goods) && $dh_order_goods) {
  532. Dever::load('shop/lib/refund')->set('dh')->apply(2, $dh_order_goods['mid'], $dh_order_goods['order_id'], $dh_order_goods['id'], 2, 0, '退货退款', '', 2, -1);
  533. }
  534. return 'reload';
  535. }
  536. # 获取部分退款的详情
  537. public function getInfo($type, $id, $order_id, $order_goods_id)
  538. {
  539. $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
  540. $info = Dever::db($this->goods_table)->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'status' => 1));
  541. if ($info) {
  542. if ($type > 10) {
  543. $info['price'] = $info['p_price'];
  544. }
  545. $total = ($info['price'] * $info['num']);
  546. if (isset($info['discount_cash']) && $info['discount_cash']) {
  547. $total = $total - $info['discount_cash'];
  548. }
  549. if (isset($info['coupon_cash']) && $info['coupon_cash']) {
  550. $info['tui_price'] = $total - $info['coupon_cash'];
  551. } else {
  552. $info['tui_price'] = $total;
  553. }
  554. $info['tui_one_price'] = $info['price'];
  555. $info['goods'] = Dever::db('goods/info')->one($info['goods_id']);
  556. }
  557. return $info;
  558. }
  559. # 退款通知
  560. public function notice($state, $data, $info)
  561. {
  562. if (!$state) {
  563. return;
  564. }
  565. if (isset($data['dh_type'])) {
  566. return;
  567. }
  568. if (isset($data['type']) && $data['type'] > 1) {
  569. return;
  570. }
  571. if (isset($data['pay_type']) && $data['pay_type'] > 1) {
  572. return;
  573. }
  574. if (isset($data['parent_type']) && $data['parent_type'] > 1) {
  575. return;
  576. }
  577. if ($info['cash'] <= 0) {
  578. return;
  579. }
  580. $refund_cash = $info['cash'];
  581. $refund_order_id = $info['id'];
  582. # 退款到原支付账户 待处理
  583. if (strstr($data['order_num'], '_')) {
  584. $temp = explode('_', $data['order_num']);
  585. $data['order_num'] = $temp[0];
  586. }
  587. $param = array
  588. (
  589. 'project_id' => 1,
  590. 'channel_id' => 1,
  591. 'system_source' => 5,
  592. 'order_id' => $data['order_num'],
  593. 'refund_cash' => $refund_cash,
  594. 'refund_order_id' => $refund_order_id
  595. );
  596. if ($refund_cash > 0) {
  597. $result = Dever::load('pay/api.refund', $param);
  598. if (!$result) {
  599. # 退款失败,抛出错误
  600. //throw new \Exception('退款失败');
  601. Dever::alert('退款失败,请联系管理员');
  602. }
  603. }
  604. if (isset($data['uid']) && $data['uid'] > 0) {
  605. $shop = Dever::db('shop/info')->one($data['shop_id']);
  606. $msg_param['type'] = 1;//消息类型1是订单消息
  607. $msg_param['id'] = $data['id'];
  608. $msg_param['name'] = $shop['name'];
  609. $msg_param = Dever::json_encode($msg_param);
  610. $msg = '您有一笔订单已退款,退款将在3个工作日内返回原支付账户';
  611. if (Dever::project('message')) {
  612. Dever::load('message/lib/data')->push(-1, $data['uid'], '退款成功通知', $msg, 2, 1, false, $msg_param);
  613. }
  614. if (Dever::project('wechat_applet')) {
  615. $config = Dever::db($this->order_table)->config;
  616. $user = Dever::db('passport/wechat')->one(array('uid' => $data['uid'], 'system_id' => 1, 'system_source' => 5));
  617. if ($user && $user['openid']) {
  618. $send = array
  619. (
  620. 'character_string1' => array
  621. (
  622. 'value' => $data['order_num'],
  623. ),
  624. 'amount3' => array
  625. (
  626. 'value' => $refund_cash . '元',
  627. ),
  628. 'thing7' => array
  629. (
  630. 'value' => '您的退款已到账,请查看~',
  631. ),
  632. );
  633. $send = Dever::json_encode($send);
  634. Dever::load('wechat_applet/subscribe')->sendOne('order_refund', 1, $user['openid'], 'pages/app/order/order?id=' . $data['id'], $send, Dever::config('base')->wechat_applet);
  635. }
  636. }
  637. }
  638. }
  639. }