Refund.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. $this->order_table = 'shop/' . $type . '_order';
  11. $this->goods_table = 'shop/' . $type . '_order_goods';
  12. $this->refund_table = 'shop/' . $type . '_order_refund';
  13. if ($type == 'buy') {
  14. $this->lib = 'mshop/lib/' . $type;
  15. } else {
  16. $this->lib = 'shop/lib/' . $type;
  17. }
  18. return $this;
  19. }
  20. # 获取退款记录
  21. public function getList($type, $id, $order_id)
  22. {
  23. $order = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
  24. $where['order_id'] = $order_id;
  25. $data = Dever::db($this->refund_table)->select_page($where);
  26. if ($data) {
  27. $goods_status = Dever::db($this->goods_table)->config['status'];
  28. $refund_status = Dever::db($this->refund_table)->config['status'];
  29. $refund_process = Dever::db($this->refund_table)->config['process'];
  30. foreach ($data as $k => $v) {
  31. $data[$k]['cdate'] = date('Y-m-d H:i', $v['cdate']);
  32. if ($v['order_goods_id'] != -1 && $order['price'] > $v['cash']) {
  33. $data[$k]['goods'] = Dever::db($this->goods_table)->getDataByIds(array('ids' => $v['order_goods_id']));
  34. } else {
  35. $data[$k]['goods'] = Dever::db($this->goods_table)->getDataByIds(array('order_id' => $v['order_id']));
  36. }
  37. $data[$k]['status_name'] = $refund_status[$v['status']];
  38. $data[$k]['process_name'] = $refund_process[$v['process']];
  39. if ($data[$k]['goods']) {
  40. foreach ($data[$k]['goods'] as $k1 => $v1) {
  41. $data[$k]['goods'][$k1]['info'] = Dever::load('goods/lib/info')->getPayInfo($v1['goods_id'], $v1['sku_id']);
  42. $data[$k]['goods'][$k1]['status_name'] = $goods_status[$v1['status']];
  43. }
  44. }
  45. }
  46. }
  47. return $data;
  48. }
  49. # 更新退款记录
  50. public function up($order_id, $order_goods_id, $status, $price, $p_price, $num = false, $desc = '', $pic = '', $process = 1)
  51. {
  52. $data['order_id'] = $order_id;
  53. if (!$order_goods_id) {
  54. $where['order_id'] = $order_id;
  55. $goods = Dever::db($this->goods_table)->getIds($where);
  56. if ($goods) {
  57. $order_goods_id = implode(',', array_keys($goods));
  58. }
  59. $data['type'] = 1;
  60. } else {
  61. $data['type'] = 2;
  62. }
  63. $data['order_goods_id'] = $order_goods_id;
  64. $info = Dever::db($this->refund_table)->find($data);
  65. $data['status'] = $status;
  66. $data['cash'] = $price;
  67. if ($p_price) {
  68. $data['p_cash'] = $p_price;
  69. }
  70. if ($num) {
  71. $data['num'] = $num;
  72. }
  73. $data['desc'] = $desc;
  74. $data['pic'] = $pic;
  75. $data['process'] = $process;
  76. if ($info) {
  77. $data['where_id'] = $info['id'];
  78. $state = Dever::db($this->refund_table)->update($data);
  79. if ($state) {
  80. $data['id'] = $info['id'];
  81. return $data;
  82. }
  83. } else {
  84. $state = Dever::db($this->refund_table)->insert($data);
  85. if ($state) {
  86. $data['id'] = $state;
  87. return $data;
  88. }
  89. }
  90. return false;
  91. }
  92. # 申请退款
  93. public function apply($type, $id, $order_id, $order_goods_id, $status = 1, $num = 0, $desc = '', $pic = '', $process = 2)
  94. {
  95. if ($status != 1 && $status != 2 && $status != 3) {
  96. Dever::alert('当前订单状态不允许退货退款');
  97. }
  98. $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
  99. if (isset($data['withdraw']) && $data['withdraw'] == 2) {
  100. Dever::alert('当前订单状态不允许退货退款');
  101. }
  102. $auth = false;
  103. if ($this->type == 'sell' && $type == 1) {
  104. if ($data['status'] == 2) {
  105. $auth = true;
  106. }
  107. } else {
  108. $config = array(2,3,4,5,6);
  109. if (in_array($data['status'], $config)) {
  110. $auth = true;
  111. }
  112. }
  113. if ($auth) {
  114. if ($order_goods_id) {
  115. $info = Dever::db($this->goods_table)->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'status' => 1));
  116. if ($info) {
  117. if (!$num) {
  118. $num = $info['num'];
  119. }
  120. Dever::db($this->goods_table)->update(array('where_id' => $info['id'], 'status' => 2));
  121. $info['total_price'] = $info['price'] * $num;
  122. if (isset($info['coupon_cash']) && $info['coupon_cash']) {
  123. $cash = $info['total_price'] - $info['coupon_cash'];
  124. } else {
  125. $cash = $info['total_price'];
  126. }
  127. if (isset($info['p_price'])) {
  128. $p_cash = $info['p_price'] * $num;
  129. } else {
  130. $p_cash = 0;
  131. }
  132. } else {
  133. Dever::alert('您没有权限操作');
  134. }
  135. } else {
  136. if (!$num) {
  137. $num = $data['num'];
  138. }
  139. $cash = $data['price'] - $data['refund_cash'];
  140. if (isset($data['p_price']) && isset($data['refund_p_cash'])) {
  141. $p_cash = $data['p_price'] - $data['refund_p_cash'];
  142. } else {
  143. $p_cash = 0;
  144. }
  145. if (isset($data['ps_cash']) && $data['ps_cash'] > 0 && $data['status'] >= 3) {
  146. # 已发货不退配送费
  147. $cash -= $data['ps_cash'];
  148. }
  149. }
  150. if ($cash > 0) {
  151. $log = $this->up($data['id'], $order_goods_id, $status, $cash, $p_cash, $num, $desc, $pic, $process);
  152. if (!$log) {
  153. Dever::alert('退款失败');
  154. }
  155. if ($this->type == 'buy') {
  156. # 如果是采购单,这里要把库存先减掉
  157. if ($data['status'] == 5 || $data['status'] == 6) {
  158. $oper_data = Dever::db($this->goods_table)->getDataByIds(array('ids' => $log['order_goods_id']));
  159. Dever::load('shop/lib/goods')->oper($data, 2, 1, $oper_data);
  160. }
  161. if ($data['source_id'] && $data['source_id'] > 0) {
  162. $data['num'] = $num;
  163. Dever::load('cash/lib/order')->up($data, 2, 1, $log);
  164. } else {
  165. $data['num'] = $num;
  166. Dever::load('cash/lib/order')->up($data, 1, 3);
  167. }
  168. # 发消息给供应商
  169. if ($data['source_id'] && Dever::project('message')) {
  170. $msg = '您的订单'.$data['order_num'].',有新的商品问题。';
  171. $msg_param['type'] = 1;
  172. $msg_param['id'] = $data['id'];
  173. $msg_param = Dever::json_encode($msg_param);
  174. $pid = $data['source_type'] + 1;
  175. Dever::load('message/lib/data')->push(-1, $data['source_id'], '退货与报损通知', $msg, 2, $pid, false, $msg_param);
  176. }
  177. }
  178. $update = array();
  179. $update['where_id'] = $data['id'];
  180. $update['refund_cash'] = $data['refund_cash'] + $cash;
  181. if (isset($data['refund_p_cash'])) {
  182. $update['refund_p_cash'] = $data['refund_p_cash'] + $p_cash;
  183. }
  184. $update['refund_status'] = 2;
  185. Dever::db($this->order_table)->update($update);
  186. if ($id && $process == 2) {
  187. $this->action($log, $process, $data);
  188. }
  189. } else {
  190. Dever::alert('您没有可以退款的金额');
  191. }
  192. } else {
  193. Dever::alert('您没有权限操作');
  194. }
  195. return 'reload';
  196. }
  197. # 确认退款
  198. public function action($info, $process, $order = false, $cash = true)
  199. {
  200. if (!is_array($info)) {
  201. $info = Dever::db($this->refund_table)->find($info);
  202. }
  203. if (!$info) {
  204. Dever::alert('您没有权限操作');
  205. }
  206. if (!$order) {
  207. $order = Dever::db($this->order_table)->find($info['order_id']);
  208. }
  209. if (!$order) {
  210. Dever::alert('您没有权限操作');
  211. }
  212. if (isset($order['withdraw']) && $order['withdraw'] == 2) {
  213. Dever::alert('当前订单状态不允许退货退款');
  214. }
  215. $state = Dever::db($this->refund_table)->update(array('where_id' => $info['id'], 'process' => $process));
  216. if (!$state) {
  217. Dever::alert('操作失败');
  218. }
  219. $update = array();
  220. if ($process == 3) {
  221. if ($info['num'] && $info['num'] > 0) {
  222. //$update['num'] = $order['num'] + $info['num'];
  223. }
  224. $update['refund_cash'] = $order['refund_cash'] - $info['cash'];
  225. if (isset($order['refund_p_cash'])) {
  226. $update['refund_p_cash'] = $order['refund_p_cash'] - $info['p_cash'];
  227. }
  228. $update['where_id'] = $order['id'];
  229. $update['refund_status'] = 1;
  230. $state = Dever::db($this->order_table)->update($update);
  231. if ($info['type'] == 2) {
  232. Dever::db($this->goods_table)->update(array('where_id' => $info['order_goods_id'], 'status' => 1));
  233. }
  234. if ($cash && $this->type == 'buy' && $order['source_id'] && $order['source_id'] > 0) {
  235. $order['num'] = $info['num'];
  236. Dever::load('cash/lib/order')->up($order, 2, 3, $info);
  237. }
  238. } else {
  239. if ($info['type'] == 2) {
  240. $state = Dever::db($this->goods_table)->update(array('where_id' => $info['order_goods_id'], 'status' => 3));
  241. if ($state) {
  242. $this->notice($state, $order, $info);
  243. # 检查这个订单下的商品是不是都退了
  244. $total = Dever::db($this->goods_table)->getTotal(array('order_id' => $order['id'], 'status' => '1,2'));
  245. if ($total <= 0) {
  246. $update['operdate'] = time();
  247. $update['status'] = 8;
  248. } elseif ($order['status'] == 5) {
  249. $update['status'] = 6;
  250. }
  251. if ($update) {
  252. $update['where_id'] = $order['id'];
  253. $update['refund_status'] = 1;
  254. $state = Dever::db($this->order_table)->update($update);
  255. }
  256. }
  257. } else {
  258. $update['where_id'] = $order['id'];
  259. $update['operdate'] = time();
  260. if (isset($order['ps_cash']) && $order['ps_cash'] > 0 && $order['status'] < 3) {
  261. # 配送费退掉
  262. //$update['ps_cash'] = 0;
  263. }
  264. $update['status'] = 8;
  265. $state = Dever::db($this->order_table)->update($update);
  266. if ($state) {
  267. $this->notice($state, $order, $info);
  268. if (isset($order['user_coupon_id']) && $order['user_coupon_id']) {
  269. # 还原优惠券
  270. Dever::db('shop/user_coupon')->update(array('where_id' => $order['user_coupon_id'], 'status' => 1));
  271. }
  272. }
  273. }
  274. if ($cash && $this->type == 'buy' && $order['source_id'] && $order['source_id'] > 0) {
  275. $order['num'] = $info['num'];
  276. if (isset($update['status'])) {
  277. $order['status'] = $update['status'];
  278. }
  279. Dever::load('cash/lib/order')->up($order, 2, 2, $info);
  280. }
  281. }
  282. $data = Dever::db($this->goods_table)->getDataByIds(array('ids' => $info['order_goods_id']));
  283. if ($this->type == 'sell' && $process == 2) {
  284. # 如果是销售单,退款后要减少销量
  285. Dever::load('shop/lib/goods')->oper($order, 2, 2, $data);
  286. } elseif ($this->type == 'buy' && $process == 3 && ($order['status'] == 5 || $order['status'] == 6)) {
  287. # 如果是采购单,退款后要恢复库存
  288. Dever::load('shop/lib/goods')->oper($order, 1, 1, $data);
  289. }
  290. return 'reload';
  291. }
  292. # 获取部分退款的详情
  293. public function getInfo($type, $id, $order_id, $order_goods_id)
  294. {
  295. $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
  296. $info = Dever::db($this->goods_table)->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'status' => 1));
  297. if ($info) {
  298. if ($type > 10) {
  299. $info['price'] = $info['p_price'];
  300. }
  301. if (isset($info['coupon_cash']) && $info['coupon_cash']) {
  302. $info['tui_price'] = ($info['price'] * $info['num']) - $info['coupon_cash'];
  303. } else {
  304. $info['tui_price'] = $info['price'] * $info['num'];
  305. }
  306. $info['tui_one_price'] = $info['price'];
  307. $info['goods'] = Dever::db('goods/info')->find($info['goods_id']);
  308. }
  309. return $info;
  310. }
  311. # 退款通知
  312. public function notice($state, $data, $info)
  313. {
  314. if ($state && isset($data['uid']) && $data['uid'] > 0) {
  315. $refund_cash = $info['cash'];
  316. $refund_order_id = $info['id'];
  317. $shop = Dever::db('shop/info')->one($data['shop_id']);
  318. $msg_param['type'] = 1;//消息类型1是订单消息
  319. $msg_param['id'] = $data['id'];
  320. $msg_param['name'] = $shop['name'];
  321. $msg_param = Dever::json_encode($msg_param);
  322. $msg = '您有一笔订单已退款,退款将在3个工作日内返回原支付账户';
  323. Dever::load('message/lib/data')->push(-1, $data['uid'], '退款成功通知', $msg, 2, 1, false, $msg_param);
  324. # 退款到原支付账户 待处理
  325. $param = array
  326. (
  327. 'project_id' => 1,
  328. 'channel_id' => 1,
  329. 'system_source' => 5,
  330. 'order_id' => $data['order_num'],
  331. 'refund_cash' => $refund_cash,
  332. 'refund_order_id' => $refund_order_id
  333. );
  334. $result = Dever::load('pay/api.refund', $param);
  335. if (!$result) {
  336. # 退款失败,抛出错误
  337. throw new \Exception('退款失败');
  338. Dever::alert('退款失败');
  339. }
  340. if (Dever::load('wechat_applet')) {
  341. $config = Dever::db($this->order_table)->config;
  342. $user = Dever::db('passport/wechat')->one(array('uid' => $data['uid'], 'system_id' => 1, 'system_source' => 5));
  343. if ($user && $user['openid']) {
  344. $send = array
  345. (
  346. 'character_string1' => array
  347. (
  348. 'value' => $data['order_num'],
  349. ),
  350. 'amount3' => array
  351. (
  352. 'value' => $refund_cash . '元',
  353. ),
  354. 'thing7' => array
  355. (
  356. 'value' => '您的退款已到账,请查看~',
  357. ),
  358. );
  359. $send = Dever::json_encode($send);
  360. Dever::load('wechat_applet/subscribe')->sendOne('order_refund', 1, $user['openid'], 'pages/app/order/order?id=' . $data['id'], $send, Dever::config('base')->wechat_applet);
  361. }
  362. }
  363. }
  364. }
  365. }