Buy.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. <?php
  2. # 支付
  3. namespace Mshop\Lib;
  4. use Dever;
  5. class Buy
  6. {
  7. # 1是门店,2是仓库,3是工厂,1,2,3是查看采购人的订单,11,12,13是查看供货商的订单
  8. public $type = 1;
  9. # 1是列表,2是详情
  10. public $view = 1;
  11. # 获取配置
  12. public $config = array();
  13. # table
  14. public $table = 'shop/buy_order';
  15. public function __construct()
  16. {
  17. $this->config = Dever::db($this->table)->config;
  18. }
  19. # 设置订单的类型
  20. public function set($type, $view)
  21. {
  22. $this->type = $type;
  23. $this->view = $view;
  24. return $this;
  25. }
  26. # 获取公共的where
  27. public function where($id)
  28. {
  29. $where = array();
  30. if ($this->type > 10) {
  31. $type = $this->type - 10;
  32. $where['source_type'] = $type;
  33. $where['source_id'] = $id;
  34. } else {
  35. $where['type'] = $this->type;
  36. $where['type_id'] = $id;
  37. }
  38. if (!$where) {
  39. Dever::alert('参数错误');
  40. }
  41. return $where;
  42. }
  43. # 获取订单列表
  44. public function getList($id)
  45. {
  46. $result = array();
  47. $where = $this->where($id);
  48. $status = Dever::input('status');
  49. if ($status) {
  50. if ($status == 'refund') {
  51. $where['refund_status'] = 2;
  52. } else {
  53. $where['status'] = $status;
  54. }
  55. }
  56. $order_num = Dever::input('order_num');
  57. if ($order_num) {
  58. $where['order_num'] = $order_num;
  59. }
  60. $refund_status = Dever::input('refund_status');
  61. if ($refund_status) {
  62. $where['refund_status'] = $refund_status;
  63. }
  64. $result['search_value'] = $where;
  65. $result['search_value']['day'] = $day = Dever::input('day');
  66. if ($day) {
  67. if ($this->type > 10) {
  68. $where['start_o'] = Dever::maketime($day . ' 00:00:00');
  69. $where['end_o'] = Dever::maketime($day . ' 23:59:59');
  70. } else {
  71. $where['start'] = Dever::maketime($day . ' 00:00:00');
  72. $where['end'] = Dever::maketime($day . ' 23:59:59');
  73. }
  74. }
  75. $result['search_value']['start'] = $start = Dever::input('start');
  76. $result['search_value']['end'] = $end = Dever::input('end');
  77. if ($start && $end) {
  78. if ($this->type > 10) {
  79. $where['start_o'] = Dever::maketime($start);
  80. $where['end_o'] = Dever::maketime($end);
  81. } else {
  82. $where['start'] = Dever::maketime($start);
  83. $where['end'] = Dever::maketime($end);
  84. }
  85. }
  86. $result['order'] = Dever::db($this->table)->getAll($where);
  87. if ($result['order']) {
  88. foreach ($result['order'] as $k => $v) {
  89. $result['order'][$k] = $this->getInfo($v);
  90. }
  91. }
  92. $result['search'] = array();
  93. $result['search']['status'] = array
  94. (
  95. array('value' => 1, 'name' => '待支付'),
  96. array('value' => 2, 'name' => '待审核'),
  97. array('value' => 3, 'name' => '待发货'),
  98. array('value' => 4, 'name' => '待收货确认'),
  99. array('value' => 5, 'name' => '已完成'),
  100. array('value' => 6, 'name' => '已完成(有退款)'),
  101. array('value' => '7,8,11', 'name' => '已取消'),
  102. );
  103. $result['config'] = Dever::db('main/manage_config')->find();
  104. return $result;
  105. }
  106. # 查看详情
  107. public function getView($id, $order_id, $show = true)
  108. {
  109. $where = $this->where($id);
  110. $where['id'] = $order_id;
  111. $result = Dever::db($this->table)->find($where);
  112. if (!$result) {
  113. Dever::alert('订单不存在');
  114. }
  115. if ($show) {
  116. $result = $this->getInfo($result, true);
  117. }
  118. return $result;
  119. }
  120. # 查看退款详情
  121. public function getRefund($id, $order_id)
  122. {
  123. $data = $this->getView($id, $order_id, false);
  124. $data['refund'] = Dever::db('shop/buy_order_refund')->select(array('order_id' => $info['id']));
  125. return $data;
  126. }
  127. # 获取订单详细信息
  128. public function getInfo($info, $view = false)
  129. {
  130. if ($info['status'] == 1) {
  131. # 15分钟内支付,900秒
  132. $m = 9000;
  133. # 支付倒计时
  134. $info['time'] = time() - $info['cdate'];
  135. if ($info['time'] >= $m) {
  136. # 已过期,自动取消
  137. $info['time'] = -1;
  138. Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
  139. $info['status'] = 11;
  140. } else {
  141. $info['time'] = $m - $info['time'];
  142. }
  143. }
  144. $info['status_name'] = $this->config['status'][$info['status']];
  145. $info['goods'] = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  146. if ($info['type'] == 1) {
  147. $info['type_info'] = Dever::db('shop/info')->getOne($info['type_id']);
  148. } elseif ($info['type'] == 2) {
  149. $info['type_info'] = Dever::db('store/info')->find($info['type_id']);
  150. } elseif ($info['type'] == 3) {
  151. $info['type_info'] = Dever::db('factory/info')->find($info['type_id']);
  152. }
  153. if ($info['source_type'] == 1) {
  154. $info['source_info'] = Dever::db('shop/info')->getOne($info['source_id']);
  155. } elseif ($info['source_type'] == 2) {
  156. $info['source_info'] = Dever::db('store/info')->find($info['source_id']);
  157. } elseif ($info['source_type'] == 3) {
  158. $info['source_info'] = Dever::db('factory/info')->find($info['source_id']);
  159. }
  160. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  161. if ($info['operdate']) {
  162. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  163. }
  164. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  165. $goods_process = Dever::db('shop/buy_order_refund')->config['process'];
  166. if ($view || $this->view == 2) {
  167. foreach ($info['goods'] as $k => $v) {
  168. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  169. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  170. }
  171. } else {
  172. foreach ($info['goods'] as $k => $v) {
  173. $goods = Dever::db('goods/info')->one($v['goods_id']);
  174. $info['goods'][$k]['name'] = $goods['name'];
  175. $info['goods'][$k]['cover'] = $goods['cover'];
  176. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  177. }
  178. }
  179. if (!$info['refund_cash']) {
  180. $info['refund_cash'] = 0;
  181. }
  182. # 配送信息
  183. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  184. if ($info['ps_info']) {
  185. $info['ps_info']['service_name'] = '商家自送';
  186. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  187. if ($info['ps_info']['ydate']) {
  188. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  189. }
  190. $status = Dever::db('shop/buy_order_ps')->config['status'];
  191. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  192. if ($info['ps_info']['service_id'] > 0) {
  193. //暂时还没有
  194. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  195. $info['ps_info']['service_name'] = $service['name'];
  196. }
  197. }
  198. return $info;
  199. }
  200. # 发起支付
  201. public function action($type, $type_id, $name, $num, $goods, $price, $info, $refer = '')
  202. {
  203. $order_data['type'] = $type;
  204. $order_data['type_id'] = $type_id;
  205. $order_data['name'] = $name;
  206. $order_data['num'] = $num;
  207. $order_data['info'] = $info;
  208. $order_data['price'] = $price;
  209. $order_data['order_num'] = $this->getOrderId();
  210. $id = Dever::db('shop/buy_order')->insert($order_data);
  211. if (!$id) {
  212. Dever::alert('支付失败');
  213. }
  214. foreach($goods as $k => $v) {
  215. $data['order_id'] = $id;
  216. $data['goods_id'] = $v['id'];
  217. $data['sku_id'] = $v['sku_id'];
  218. $data['price'] = $v['price'] * $v['buy_num'];
  219. $data['num'] = $v['buy_num'];
  220. Dever::db('shop/buy_order_goods')->insert($data);
  221. }
  222. if ($price <= 0) {
  223. $param['pay_product_id'] = $id;
  224. $param['pay_order_id'] = $order_data['order_num'];
  225. $param['pay_status'] = 2;
  226. $param['pay_cash'] = $price;
  227. $param['pay_msg'] = '';
  228. $this->success($param);
  229. return array
  230. (
  231. 'order' => '',
  232. 'order_num' => $order_data['order_num'],
  233. 'order_id' => $id,
  234. 'type' => 'pay',
  235. );
  236. }
  237. $param = array
  238. (
  239. 'project_id' => 2,
  240. 'channel_id' => 1,
  241. 'system_source' => 5,
  242. 'uid' => -1,
  243. 'name' => $order_data['name'],
  244. 'cash' => $price,
  245. 'product_id' => $id,
  246. 'order_id' => $order_data['order_num'],
  247. 'refer' => $refer,
  248. );
  249. $receipt = Dever::input('receipt');
  250. if ($receipt) {
  251. $param['receipt'] = $receipt;
  252. }
  253. //$result = Dever::load('pay/api.pay', $param);
  254. $result['type'] = 'test';
  255. $result['order'] = '';
  256. $result['order_num'] = $order_data['order_num'];
  257. $result['order_id'] = $id;
  258. return $result;
  259. }
  260. # 再次发起支付
  261. public function raction($id, $refer = '')
  262. {
  263. $order = Dever::db('shop/buy_order')->find($id);
  264. if (!$order) {
  265. Dever::alert('订单信息错误');
  266. }
  267. if ($order['status'] > 1) {
  268. Dever::alert('订单已支付或已取消');
  269. }
  270. $param = array
  271. (
  272. 'project_id' => 2,
  273. 'channel_id' => 1,
  274. 'system_source' => 5,
  275. 'uid' => -1,
  276. 'name' => $order['name'],
  277. 'cash' => $order['price'],
  278. 'product_id' => $id,
  279. 'order_id' => $order['order_num'],
  280. 'refer' => $refer,
  281. );
  282. $receipt = Dever::input('receipt');
  283. if ($receipt) {
  284. $param['receipt'] = $receipt;
  285. }
  286. $result = Dever::load('pay/api.pay', $param);
  287. $result['order_num'] = $order['order_num'];
  288. $result['order_id'] = $id;
  289. return $result;
  290. }
  291. # 支付成功回调 安全加密 设置token
  292. public function success_secure_api_token()
  293. {
  294. $project_id = Dever::input('pay_project_id');
  295. $info = Dever::db('pay/project')->one($project_id);
  296. if ($info) {
  297. return $info['key'];
  298. }
  299. return 'shop_buy_dever_2020';
  300. }
  301. # 支付成功回调 安全加密
  302. public function success_secure_api($param = array())
  303. {
  304. $this->success($param);
  305. }
  306. # 支付成功回调
  307. public function success($param = array())
  308. {
  309. $send = $param ? $param : Dever::preInput('pay_');
  310. $product_id = $send['pay_product_id'];
  311. $order_id = $send['pay_order_id'];
  312. $status = $send['pay_status'];
  313. $cash = $send['pay_cash'];
  314. $msg = $send['pay_msg'];
  315. $order = Dever::db('shop/buy_order')->one(array('id' => $product_id, 'time' => time()));
  316. if ($order && $order['status'] == 1 && $status == 2) {
  317. $update['status'] = 2;
  318. $update['where_id'] = $order['id'];
  319. Dever::db('shop/buy_order')->update($update);
  320. # 生成结算单
  321. Dever::load('cash/lib/order')->up($order, 1, 1);
  322. }
  323. return 'ok';
  324. }
  325. # 生成订单号
  326. public function getOrderId()
  327. {
  328. $where['order_num'] = Dever::order('C');
  329. $state = Dever::db('shop/buy_order')->one($where);
  330. if (!$state) {
  331. return $where['order_num'];
  332. } else {
  333. return $this->getOrderId();
  334. }
  335. }
  336. # 添加配送信息
  337. public function updatePs($order, $status = 1)
  338. {
  339. $where['order_id'] = $order['id'];
  340. $data = Dever::db('shop/buy_order_ps')->find($where);
  341. if ($data) {
  342. $update['where_id'] = $data['id'];
  343. $update['status'] = $status;
  344. $update['ydate'] = time();
  345. Dever::db('shop/buy_order_ps')->update($update);
  346. }
  347. }
  348. # 确认收货
  349. public function finish($id, $order_id)
  350. {
  351. $data = $this->getView($id, $order_id, false);
  352. if ($data['status'] <= 4) {
  353. if ($data['refund_cash'] > 0) {
  354. $status = 6;
  355. } else {
  356. $status = 5;
  357. }
  358. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => $status));
  359. if ($state) {
  360. $this->updatePs($data, 3);
  361. # 增加库存
  362. $where['order_id'] = $data['id'];
  363. $where['status'] = 1;
  364. $goods = Dever::db('shop/buy_order_goods')->select($where);
  365. if ($goods) {
  366. Dever::load('shop/lib/goods')->oper($data, 1, 1, $goods);
  367. }
  368. # 生成结算单
  369. Dever::load('cash/lib/order')->up($data, 1, 2);
  370. }
  371. return 'ok';
  372. } else {
  373. Dever::alert('您没有权限');
  374. }
  375. }
  376. # 取消订单
  377. public function cancel($id, $order_id)
  378. {
  379. $data = $this->getView($id, $order_id, false);
  380. if ($data['status'] == 1) {
  381. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  382. # 生成结算单
  383. if ($state) {
  384. //Dever::load('cash/lib/order')->up($data, 1, 3);
  385. }
  386. return 'ok';
  387. } else {
  388. Dever::alert('当前订单状态不允许取消');
  389. }
  390. }
  391. # 查看订单状态
  392. public function orderStatus($id)
  393. {
  394. $config = Dever::db('shop/buy_order')->config;
  395. $info = Dever::db('shop/buy_order')->one($id);
  396. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
  397. $status = $config['status'][$info['status']];
  398. if ($tk) {
  399. $status = '申请退款中';
  400. }
  401. return $status;
  402. }
  403. # 后台审核退款
  404. public function refund_commit_api()
  405. {
  406. $id = Dever::input('id');
  407. $process = Dever::input('process');
  408. return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
  409. }
  410. # 货物未发出,仅退款
  411. public function refund_apply_commit_api()
  412. {
  413. $order_id = Dever::input('order_id');
  414. $type = Dever::input('type');
  415. $type_id = Dever::input('type_id');
  416. $desc = Dever::input('desc');
  417. $pic = Dever::input('pic');
  418. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, 3, 0, $desc, $pic, 2);
  419. }
  420. # 货物已收到 全部退款 报损 需要审核
  421. public function refund_apply_info_commit_api()
  422. {
  423. $order_id = Dever::input('order_id');
  424. $status = Dever::input('status');
  425. $desc = Dever::input('desc');
  426. $pic = Dever::input('pic');
  427. $type = Dever::input('type');
  428. $type_id = Dever::input('type_id');
  429. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, $status, 0, $desc, $pic, 1);
  430. }
  431. # 部分退款 报损
  432. public function refund_apply_info_one_commit_api()
  433. {
  434. $order_id = Dever::input('order_id');
  435. $order_goods_id = Dever::input('order_goods_id');
  436. $num = Dever::input('num', 0);
  437. $status = Dever::input('status');
  438. $desc = Dever::input('desc');
  439. $pic = Dever::input('pic');
  440. $type = Dever::input('type');
  441. $type_id = Dever::input('type_id');
  442. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
  443. }
  444. # 展示订单详情
  445. public function show()
  446. {
  447. $id = Dever::input('order_id');
  448. $type = Dever::input('type', 1);
  449. $config = Dever::db('shop/buy_order')->config;
  450. $info = Dever::db('shop/buy_order')->one($id);
  451. $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
  452. $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
  453. $status = $config['status'][$info['status']];
  454. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  455. $cdate = date('Y-m-d H:i', $info['cdate']);
  456. $opertime = date('Y-m-d H:i', $info['operdate']);
  457. $html = '<div class="layui-col-md12"><div class="layui-card"><div class="layui-card-header">基本信息</div><div class="layui-card-body">';
  458. $html .= '<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  459. if ($type == 2) {
  460. $info['price'] = $info['p_price'];
  461. }
  462. $html .= '<tr>
  463. <td>基本信息</td>
  464. <td>'.$this->table(false, array(array('订单号:' . $info['order_num'], '金额:' . $info['price'], '数量:' . $info['num'], '状态:' . $status))).'</td>
  465. </tr>';
  466. if ($type == 1 && $tk && $tk['process'] == 1) {
  467. $status = '申请' . $tk_status[$tk['status']];
  468. }
  469. if ($type == 1) {
  470. $time = array('下单时间:' . $cdate, '订货时间:' . $opertime);
  471. } else {
  472. $time = array('订货时间:' . $opertime);
  473. }
  474. $html .= '<tr>
  475. <td>订单时间</td>
  476. <td>'.$this->table(false, array($time)).'</td>
  477. </tr>';
  478. if ($info['type'] == 1) {
  479. $type_info = Dever::db('shop/info')->find($info['type_id']);
  480. } elseif ($info['type'] == 2) {
  481. $type_info = Dever::db('store/info')->find($info['type_id']);
  482. } elseif ($info['type'] == 3) {
  483. $type_info = Dever::db('store/info')->find($info['type_id']);
  484. }
  485. $html .= '<tr>
  486. <td>配送信息</td>
  487. <td>'.$this->table(false, array(array($type_info['name'] . ',' . $type_info['truename'] . ',' . $type_info['mobile'] . ',' . $type_info['address']))).'</td>
  488. </tr>';
  489. if ($type == 1 && $info['source_id'] > 0) {
  490. if ($info['source_type'] == 2) {
  491. # 仓库
  492. $source_info = Dever::db('store/info')->find($info['source_id']);
  493. } elseif ($info['source_type'] == 3) {
  494. # 工厂
  495. $source_info = Dever::db('factory/info')->find($info['source_id']);
  496. }
  497. $html .= '<tr>
  498. <td>供应商信息</td>
  499. <td>'.$this->table(false, array(array($source_info['name'] . ',' . $source_info['truename'] . ',' . $source_info['mobile']))).'</td>
  500. </tr>';
  501. }
  502. if ($info['info']) {
  503. $html .= '<tr>
  504. <td>订单备注</td>
  505. <td>'.$this->table(false, array(array($info['info']))).'</td>
  506. </tr>';
  507. }
  508. if ($type == 2) {
  509. $config = Dever::load('factory/admin/auth.config');
  510. $button = array();
  511. if ($info['status'] < 5) {
  512. $button[] = '<button class="layui-btn layui-btn-primary" onclick="$(\'#print_'.$info['id'].'\').click()">打印货单</button>';
  513. }
  514. if ($info['status'] == 3) {
  515. $cancel = Dever::url('lib/buy.refund_apply_commit?json=1', 'mshop');
  516. $button[] = '<button class="layui-btn layui-btn-primary" onclick="$(\'#send_'.$info['id'].'\').click()">配送发货</button>';
  517. $button[] = '<button class="layui-btn layui-btn-primary" onclick="cancel('.$info['id'].','.$info['source_id'].', \''.$cancel.'\')">取消订单</button>';
  518. }
  519. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  520. $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系平台</button>';
  521. $html .= '<tr>
  522. <td>功能按钮</td>
  523. <td>'.$this->table(false, array($button)).'</td>
  524. </tr>';
  525. }
  526. $html .= '</tbody></table></div></div>';
  527. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  528. if (!$info['refund_cash']) {
  529. $info['refund_cash'] = 0;
  530. }
  531. if ($info['refund_cash'] > 0 && $tk) {
  532. if ($tk['process'] == 1) {
  533. $process = '<a href="javascript:;" onclick="audit('.$tk['id'].', 2, \''.$url.'\')" class="layui-btn">通过</a><a href="javascript:;" onclick="audit('.$tk['id'].', 3, \''.$url.'\')" class="layui-btn layui-btn-danger">驳回</a>';
  534. } else {
  535. $process = $tk_process[$tk['process']];
  536. }
  537. $desc = array();
  538. $desc['退款原因'] = $tk['desc'];
  539. $desc['退款图片'] = '暂无';
  540. if ($tk['pic']) {
  541. $desc['退款图片'] = '';
  542. $pic = explode(',', $tk['pic']);
  543. foreach ($pic as $k => $v) {
  544. $desc['退款图片'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  545. }
  546. }
  547. $tk['desc'] = '<a href="javascript:showAlert($(\'#desc_content\').html());">查看</a><span id="desc_content" style="display:none">'.Dever::table($desc).'</span>';
  548. $head = array('退款金额', '数量', '退款原因', '退款进度');
  549. $body = array(array($info['refund_cash'], $info['num'], $tk['desc'], $process));
  550. $html .= '<div class="layui-card"><div class="layui-card-header">退款信息</div><div class="layui-card-body">' . $this->table($head, $body) . '</div></div>';
  551. }
  552. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  553. if ($info['ps_info']) {
  554. $info['ps_info']['service_name'] = '供应商自送';
  555. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  556. if ($info['ps_info']['ydate']) {
  557. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  558. }
  559. $status = Dever::db('shop/buy_order_ps')->config['status'];
  560. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  561. if ($info['ps_info']['service_id'] > 0) {
  562. //暂时还没有
  563. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  564. $info['ps_info']['service_name'] = $service['name'];
  565. }
  566. $head = array('名称', '单号', '费用', '配货员', '更多');
  567. $table = array();
  568. $table['打包员'] = $info['ps_info']['dby'];
  569. $table['数量'] = $info['ps_info']['num'];
  570. $table['重量'] = $info['ps_info']['zl'];
  571. $table['体积'] = $info['ps_info']['tj'];
  572. $table['发货时间'] = $info['ps_info']['cdate'];
  573. if ($info['ps_info']['ydate']) {
  574. $table['收货时间'] = $info['ps_info']['ydate'];
  575. }
  576. $body = array(array($info['ps_info']['service_name'], $info['ps_info']['order_num'], $info['ps_info']['price'], $info['ps_info']['phy'], '<a href="javascript:showAlert($(\'#refund_content\').html());">查看</a><span id="refund_content" style="display:none">'.Dever::table($table).'</span>'));
  577. $html .= '<div class="layui-card"><div class="layui-card-header">物流信息</div><div class="layui-card-body">' . $this->table($head, $body) . '</div></div>';
  578. }
  579. $body = array();
  580. $body_total = array();
  581. $body_total['price'] = 0;
  582. $body_total['num'] = 0;
  583. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  584. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  585. $refund_body = array();
  586. $refund_body_total = array();
  587. $refund_body_total['price'] = 0;
  588. $refund_body_total['num'] = 0;
  589. foreach ($goods as $k => $v) {
  590. if ($type == 2) {
  591. $v['price'] = $v['p_price'];
  592. }
  593. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  594. $status = $goods_status[$v['status']];
  595. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  596. if ($tk && $tk['process'] == 1) {
  597. if ($type == 1) {
  598. $status = '申请' . $tk_status[$tk['status']];
  599. } else {
  600. $status = '已上报';
  601. }
  602. if ($type == 1) {
  603. $desc = array();
  604. $desc['退款原因'] = $tk['desc'];
  605. $desc['退款图片'] = '暂无';
  606. if ($tk['pic']) {
  607. $desc['退款图片'] = '';
  608. $pic = explode(',', $tk['pic']);
  609. foreach ($pic as $k1 => $v1) {
  610. $desc['退款图片'] .= '<a href="'.$v1.'" target="_blank"><img src="'.$v1.'" width="100"/></a>';
  611. }
  612. }
  613. $status .= '&nbsp;&nbsp;<a href="javascript:showAlert($(\'#desc_content\').html());">查看原因</a><span id="desc_content" style="display:none">'.Dever::table($desc).'</span>';
  614. $status .= '&nbsp;&nbsp;<a href="javascript:;" onclick="audit('.$tk['id'].', 2, \''.$url.'\')" class="layui-btn">通过</a><a href="javascript:;" onclick="audit('.$tk['id'].', 3, \''.$url.'\')" class="layui-btn layui-btn-danger">驳回</a>';
  615. }
  616. } elseif ($type > 1 && (!$tk || ($tk && $tk['process'] == 3))) {
  617. $tui = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  618. if ($info['status'] == 3) {
  619. $status = '<a href="javascript:;" onclick="tui('.$v['id'].','.$info['id'].','.$info['source_id'].', 2, \''.$tui.'\')" class="layui-btn">缺货退款</a>';
  620. } elseif ($info['status'] == 4) {
  621. $status = '<a href="javascript:;" onclick="tui('.$v['id'].','.$info['id'].','.$info['source_id'].', 1, \''.$tui.'\')" class="layui-btn">报损退款</a>';
  622. }
  623. }
  624. if (isset($goods_info['sku'])) {
  625. $sku = '[' . $goods_info['sku']['string'] . ']';
  626. } else {
  627. $sku = '';
  628. }
  629. if ($tk && $v['status'] == 3) {
  630. $refund_body[] = array
  631. (
  632. $goods_info['name'] . ' ' . $sku,
  633. $v['price'],
  634. $v['num'],
  635. $tk_status[$tk['status']],
  636. );
  637. $refund_body_total['price'] += $v['price'];
  638. $refund_body_total['num'] += $v['num'];
  639. } else {
  640. $body[] = array
  641. (
  642. $goods_info['name'] . ' ' . $sku,
  643. $v['price'],
  644. $v['num'],
  645. $status,
  646. );
  647. $body_total['price'] += $v['price'];
  648. $body_total['num'] += $v['num'];
  649. }
  650. }
  651. if ($body) {
  652. $head = array('名称', '金额', '数量', '状态');
  653. $body[] = array
  654. (
  655. '合计',
  656. $body_total['price'],
  657. $body_total['num'],
  658. '-'
  659. );
  660. $html .= '<div class="layui-card"><div class="layui-card-header">商品清单</div><div class="layui-card-body" style="max-height: 500px;overflow: auto;">' . $this->table($head, $body) . '</div></div>';
  661. }
  662. if ($refund_body) {
  663. $head = array('名称', '金额', '数量', '状态');
  664. $refund_body[] = array
  665. (
  666. '合计',
  667. $refund_body_total['price'],
  668. $refund_body_total['num'],
  669. '-'
  670. );
  671. $html .= '<div class="layui-card"><div class="layui-card-header">缺货与报损商品</div><div class="layui-card-body" style="max-height: 300px;overflow: auto;">' . $this->table($head, $refund_body) . '</div></div>';
  672. }
  673. if ($type == 1) {
  674. # 门店结算单
  675. $cash_order = Dever::db('cash/order')->select(array('type' => $info['type'], 'type_id' => $info['type_id'], 'source_order_id' => $info['id']));
  676. } elseif ($type == 2) {
  677. # 工厂结算单
  678. }
  679. $html .= '</div>';
  680. return '<div class="layui-card-body">' . $html . '</div>';
  681. }
  682. private function table($head, $data)
  683. {
  684. $html = '';
  685. if ($head) {
  686. $html = '<table class="layui-table">';
  687. $html .= '<thead><tr>';
  688. foreach ($head as $k => $v) {
  689. $html .= '<th>'.$v.'</th>';
  690. }
  691. $html .= '</tr></thead>';
  692. $html .= '<tbody>';
  693. foreach ($data as $k => $v) {
  694. $html .= '<tr>';
  695. foreach ($v as $k1 => $v1) {
  696. $html .= '<td>'.$v1.'</td>';
  697. }
  698. $html .= '</tr>';
  699. }
  700. $html .= '</tbody>';
  701. $html .= '</table>';
  702. } else {
  703. foreach ($data as $k => $v) {
  704. $html .= '';
  705. foreach ($v as $k1 => $v1) {
  706. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  707. }
  708. $html .= '';
  709. }
  710. }
  711. return $html;
  712. }
  713. # 发货
  714. /*
  715. public function send_commit()
  716. {
  717. $order_id = Dever::input('order_id');
  718. $order = Dever::db('shop/buy_order')->find($order_id);
  719. if (!$order || ($order && $order['status'] != 3)) {
  720. Dever::alert('当前订单状态无法发货');
  721. }
  722. $state = Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 4));
  723. if ($state) {
  724. $this->updatePs($order, 1);
  725. }
  726. return 'reload';
  727. }*/
  728. # 采购订单审核 拆单
  729. public function audit_commit()
  730. {
  731. $order_id = Dever::input('order_id');
  732. $order = Dever::db('shop/buy_order')->find($order_id);
  733. if (!$order || ($order && $order['status'] != 2)) {
  734. Dever::alert('当前订单状态无法拆单');
  735. }
  736. if ($order['type'] != 1) {
  737. Dever::alert('这不是门店的订单');
  738. }
  739. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
  740. $shop = Dever::db('shop/info')->find($order['type_id']);
  741. # 获取门店分配的仓库和工厂
  742. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  743. $store = array();
  744. $distance = array();
  745. # 按照距离排序
  746. foreach ($shop_store as $k => $v) {
  747. $info = Dever::db('store/info')->find($v['store_id']);
  748. if ($info && $info['status'] == 1) {
  749. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  750. $store[] = $info;
  751. }
  752. }
  753. if (!$store) {
  754. Dever::alert('未分配仓库');
  755. }
  756. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  757. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  758. $factory = array();
  759. $distance = array();
  760. # 按照距离排序
  761. foreach ($shop_factory as $k => $v) {
  762. $info = Dever::db('factory/info')->find($v['factory_id']);
  763. if ($info && $info['status'] == 1) {
  764. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  765. $factory[] = $info;
  766. }
  767. }
  768. if (!$factory) {
  769. Dever::alert('未分配工厂');
  770. }
  771. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  772. $this->order_num = 0;
  773. $this->order_id = array();
  774. $delete = array();
  775. $yes = array();
  776. foreach ($goods as $k => $v) {
  777. # 开始找仓库
  778. $find = $this->store($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $store, $v);
  779. if (!$find[1]) {
  780. # 仓库没有,找厂家
  781. if ($factory) {
  782. $find = $this->factory($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $factory, $v);
  783. }
  784. }
  785. if ($find[1]) {
  786. $delete[$v['id']] = $v;
  787. } else {
  788. $yes[$v['id']] = $v;
  789. }
  790. }
  791. if ($this->order_num > 0) {
  792. if ($this->order_num == 1) {
  793. Dever::db('shop/buy_order')->delete($find[2][0]);
  794. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $find[2][0]));
  795. $p_price = 0;
  796. foreach ($goods as $k => $v) {
  797. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $order['id'], 'goods_id' => $v['goods_id']));
  798. if ($goods_info) {
  799. Dever::db('shop/buy_order_goods')->update(array('where_id' => $goods_info['id'], 'set_p_price' => $v['p_price']));
  800. $p_price += $v['p_price'];
  801. }
  802. }
  803. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 3, 'source_type' => $find[0], 'source_id' => $find[1][0], 'p_price' => $p_price, 'operdate' => time()));
  804. Dever::db('shop/buy_order_goods')->delete(array('order_id' => $find[2][0]));
  805. unset($this->order_id[$find[2][0]]);
  806. $this->order_id[$order['id']] = array($find[0], $find[1][0]);
  807. } else {
  808. if (!$yes) {
  809. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  810. Dever::load('cash/lib/order')->up($order, 1, 3);
  811. } else {
  812. $price = 0;
  813. $num = 0;
  814. foreach ($delete as $k => $v) {
  815. Dever::db('shop/buy_order_goods')->delete(array('where_id' => $v['id']));
  816. $price += $v['price'];
  817. $num += $v['num'];
  818. }
  819. $order['price'] = $order['price'] - $price;
  820. $order['num'] = $order['num'] - $num;
  821. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $order['price'], 'num' => $order['num']));
  822. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  823. Dever::load('cash/lib/order')->up($order, 1, 3);
  824. }
  825. }
  826. } else {
  827. # 没有找到,退款
  828. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  829. }
  830. if ($this->order_id) {
  831. foreach ($this->order_id as $k => $v) {
  832. $this->sendMsg($v[0], $v[1], $k);
  833. if ($k != $order['id']) {
  834. $order_info = Dever::db('shop/buy_order')->find($k);
  835. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  836. }
  837. }
  838. }
  839. return 'ok';
  840. }
  841. private function store($order_id, $order_num, $shop, $store, $goods, $state = 2)
  842. {
  843. $find = array();
  844. $type_id = array();
  845. $id = array();
  846. if (!$store) {
  847. return array(2, $type_id, $id);
  848. }
  849. foreach ($store as $k => $v) {
  850. $where['store_id'] = $v['id'];
  851. $where['goods_id'] = $goods['goods_id'];
  852. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  853. $sku = Dever::db('store/goods_sku')->getOne($where);
  854. if ($sku) {
  855. if ($goods['num'] <= $sku['total']) {
  856. # 如果库存符合,直接下单即可,生成仓库订单
  857. $state = $this->createOrder($order_id, $order_num, 1, $shop['id'], 2, $where['store_id'], $goods['price'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
  858. if ($state) {
  859. $type_id[] = $where['store_id'];
  860. $id[] = $state;
  861. $this->order_id[$state] = array(2, $where['store_id']);
  862. break;
  863. }
  864. } elseif ($state == 1 && $sku['total'] > 0) {
  865. # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
  866. $goods['num'] = $goods['num'] - $sku['total'];
  867. # 生成仓库订单
  868. $state = $this->createOrder($order_id, $order_num, 1, $shop['id'], 2, $where['store_id'], $goods['price'], $goods['price'], $sku['total'], $where['goods_id'], $where['sku_id']);
  869. if (!$state) {
  870. $goods['num'] = $goods['num'] + $sku['total'];
  871. } else {
  872. $type_id[] = $where['store_id'];
  873. $id[] = $state;
  874. $this->order_id[$state] = array(2, $where['store_id']);
  875. }
  876. continue;
  877. } else {
  878. # 直接切换到下一个仓库处理
  879. continue;
  880. }
  881. }
  882. }
  883. return array(2, $type_id, $id);
  884. }
  885. private function factory($order_id, $order_num, $shop, $factory, $goods)
  886. {
  887. $find = array();
  888. $type_id = array();
  889. $id = array();
  890. if (!$factory) {
  891. return array(3, $type_id, $id);
  892. }
  893. foreach ($factory as $k => $v) {
  894. $where['factory_id'] = $v['id'];
  895. $where['goods_id'] = $goods['goods_id'];
  896. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  897. $sku = Dever::db('factory/goods_sku')->getOne($where);
  898. if ($sku && $sku['p_price'] > 0) {
  899. $state = $this->createOrder($order_id, $order_num, 1, $shop['id'], 3, $where['factory_id'], $goods['price'], $sku['p_price'], $goods['num'], $where['goods_id'], $where['sku_id']);
  900. if ($state) {
  901. $type_id[] = $where['factory_id'];
  902. $id[] = $state;
  903. $this->order_id[$state] = array(3, $where['factory_id']);
  904. break;
  905. }
  906. } else {
  907. continue;
  908. }
  909. }
  910. return array(3, $type_id, $id);
  911. }
  912. # 发送消息
  913. private function sendMsg($source_type, $source_id, $id)
  914. {
  915. if (Dever::project('message')) {
  916. $msg_param['type'] = 1;//消息类型1是订单消息
  917. $msg_param['id'] = $id;
  918. $msg_param['source_id'] = $source_id;
  919. $msg_param['source_type'] = $source_type;
  920. $msg_param = Dever::json_encode($msg_param);
  921. $project = $source_type + 1;
  922. $msg = '您有一个新的订货单,请及时处理。';
  923. Dever::load('message/lib/data')->push(-1, $source_id, '订货通知', $msg, 2, $project, false, $msg_param);
  924. }
  925. }
  926. # 生成拆单的订单
  927. private function createOrder($order_id, $order_num, $type, $type_id, $source_type, $source_id, $price, $p_price, $num, $goods_id, $sku_id)
  928. {
  929. $where['parent_order_id'] = $order_id;
  930. $where['type'] = $type;
  931. $where['type_id'] = $type_id;
  932. $where['source_type'] = $source_type;
  933. $where['source_id'] = $source_id;
  934. $where['r'] = $goods_id;
  935. $info = Dever::db('shop/buy_order')->find($where);
  936. if (!$info) {
  937. $this->order_num++;
  938. $data = $where;
  939. $data['name'] = $order_id;
  940. $data['order_num'] = $order_num;
  941. $data['operdate'] = time();
  942. $data['status'] = 3;
  943. $data['price'] = $price;
  944. $data['p_price'] = $p_price;
  945. $data['num'] = $num;
  946. $id = $state = Dever::db('shop/buy_order')->insert($data);
  947. } else {
  948. $id = $info['id'];
  949. $data['where_id'] = $id;
  950. $data['price'] = $info['price'] + $price;
  951. $data['p_price'] = $info['p_price'] + $p_price;
  952. $data['num'] = $info['num'] + $num;
  953. $state = Dever::db('shop/buy_order')->update($data);
  954. }
  955. if ($state && $id > 0) {
  956. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $id));
  957. if (!$goods_info) {
  958. $goods['order_id'] = $id;
  959. $goods['goods_id'] = $goods_id;
  960. $goods['sku_id'] = $sku_id;
  961. $goods['price'] = $price;
  962. $goods['p_price'] = $p_price;
  963. $goods['num'] = $num;
  964. Dever::db('shop/buy_order_goods')->insert($goods);
  965. }
  966. //$state = $this->updateSell($source_type, $source_id, $goods_id, $sku_id, $num);
  967. return $id;
  968. }
  969. return false;
  970. }
  971. # 减少库存,增加销量
  972. public function updateSell($source_type, $source_id, $goods_id, $sku_id, $num)
  973. {
  974. if ($source_type == 2) {
  975. $data['store_id'] = $source_id;
  976. $data['goods_id'] = $goods_id;
  977. $data['sku_id'] = $sku_id;
  978. $data['num'] = $num;
  979. return Dever::load('store/lib/goods')->oper($source_id, 1, 2, array($data));
  980. } else {
  981. $up = array();
  982. $up['where_factory_id'] = $source_id;
  983. $up['where_goods_id'] = $goods_id;
  984. $up['sell_num'] = $num;
  985. $state = Dever::db('factory/goods')->incSell($up);
  986. if ($state) {
  987. if ($sku_id > 0) {
  988. } else {
  989. $sku_id = -1;
  990. }
  991. $upSku = $up;
  992. $upSku['where_sku_id'] = $sku_id;
  993. $state = Dever::db('factory/goods_sku')->incSell($upSku);
  994. }
  995. return $state;
  996. }
  997. }
  998. }