Buy.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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) {
  317. if ($status == 2) {
  318. }
  319. $update['status'] = 2;
  320. $update['where_id'] = $order['id'];
  321. Dever::db('shop/buy_order')->update($update);
  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. return 'ok';
  370. } else {
  371. Dever::alert('您没有权限');
  372. }
  373. }
  374. # 取消订单
  375. public function cancel($id, $order_id)
  376. {
  377. $data = $this->getView($id, $order_id, false);
  378. if ($data['status'] == 1) {
  379. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  380. return 'ok';
  381. } else {
  382. Dever::alert('当前订单状态不允许取消');
  383. }
  384. }
  385. # 查看订单状态
  386. public function orderStatus($id)
  387. {
  388. $config = Dever::db('shop/buy_order')->config;
  389. $info = Dever::db('shop/buy_order')->one($id);
  390. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
  391. $status = $config['status'][$info['status']];
  392. if ($tk) {
  393. $status = '申请退款中';
  394. }
  395. return $status;
  396. }
  397. # 后台审核退款
  398. public function refund_commit_api()
  399. {
  400. $id = Dever::input('id');
  401. $process = Dever::input('process');
  402. return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
  403. }
  404. # 货物未发出,仅退款
  405. public function refund_apply_commit_api()
  406. {
  407. $order_id = Dever::input('order_id');
  408. $type = Dever::input('type');
  409. $type_id = Dever::input('type_id');
  410. $desc = Dever::input('desc');
  411. $pic = Dever::input('pic');
  412. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, 3, 0, $desc, $pic, 2);
  413. }
  414. # 货物已收到 全部退款 报损 需要审核
  415. public function refund_apply_info_commit_api()
  416. {
  417. $order_id = Dever::input('order_id');
  418. $status = Dever::input('status');
  419. $desc = Dever::input('desc');
  420. $pic = Dever::input('pic');
  421. $type = Dever::input('type');
  422. $type_id = Dever::input('type_id');
  423. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, $status, 0, $desc, $pic, 1);
  424. }
  425. # 部分退款 报损
  426. public function refund_apply_info_one_commit_api()
  427. {
  428. $order_id = Dever::input('order_id');
  429. $order_goods_id = Dever::input('order_goods_id');
  430. $num = Dever::input('num', 0);
  431. $status = Dever::input('status');
  432. $desc = Dever::input('desc');
  433. $pic = Dever::input('pic');
  434. $type = Dever::input('type');
  435. $type_id = Dever::input('type_id');
  436. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
  437. }
  438. # 展示订单详情
  439. public function show()
  440. {
  441. $id = Dever::input('order_id');
  442. $type = Dever::input('type', 1);
  443. $config = Dever::db('shop/buy_order')->config;
  444. $info = Dever::db('shop/buy_order')->one($id);
  445. $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
  446. $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
  447. $status = $config['status'][$info['status']];
  448. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  449. $cdate = date('Y-m-d H:i', $info['cdate']);
  450. $opertime = date('Y-m-d H:i', $info['operdate']);
  451. $html = '<div class="layui-col-md12"><div class="layui-card"><div class="layui-card-header">基本信息</div><div class="layui-card-body">';
  452. $html .= '<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  453. if ($type == 2) {
  454. $info['price'] = $info['p_price'];
  455. }
  456. $html .= '<tr>
  457. <td>基本信息</td>
  458. <td>'.$this->table(false, array(array('订单号:' . $info['order_num'], '金额:' . $info['price'], '数量:' . $info['num'], '状态:' . $status))).'</td>
  459. </tr>';
  460. if ($type == 1 && $tk && $tk['process'] == 1) {
  461. $status = '申请' . $tk_status[$tk['status']];
  462. }
  463. if ($type == 1) {
  464. $time = array('下单时间:' . $cdate, '订货时间:' . $opertime);
  465. } else {
  466. $time = array('订货时间:' . $opertime);
  467. }
  468. $html .= '<tr>
  469. <td>订单时间</td>
  470. <td>'.$this->table(false, array($time)).'</td>
  471. </tr>';
  472. if ($info['type'] == 1) {
  473. $type_info = Dever::db('shop/info')->find($info['type_id']);
  474. } elseif ($info['type'] == 2) {
  475. $type_info = Dever::db('store/info')->find($info['type_id']);
  476. } elseif ($info['type'] == 3) {
  477. $type_info = Dever::db('store/info')->find($info['type_id']);
  478. }
  479. $html .= '<tr>
  480. <td>配送信息</td>
  481. <td>'.$this->table(false, array(array($type_info['name'] . ',' . $type_info['truename'] . ',' . $type_info['mobile'] . ',' . $type_info['address']))).'</td>
  482. </tr>';
  483. if ($type == 1 && $info['source_id'] > 0) {
  484. if ($info['source_type'] == 2) {
  485. # 仓库
  486. $source_info = Dever::db('store/info')->find($info['source_id']);
  487. } elseif ($info['source_type'] == 3) {
  488. # 工厂
  489. $source_info = Dever::db('factory/info')->find($info['source_id']);
  490. }
  491. $html .= '<tr>
  492. <td>供应商信息</td>
  493. <td>'.$this->table(false, array(array($source_info['name'] . ',' . $source_info['truename'] . ',' . $source_info['mobile']))).'</td>
  494. </tr>';
  495. }
  496. if ($info['info']) {
  497. $html .= '<tr>
  498. <td>订单备注</td>
  499. <td>'.$this->table(false, array(array($info['info']))).'</td>
  500. </tr>';
  501. }
  502. if ($type == 2) {
  503. $config = Dever::load('factory/admin/auth.config');
  504. $button = array();
  505. if ($info['status'] < 5) {
  506. $button[] = '<button class="layui-btn layui-btn-primary" onclick="$(\'#print_'.$info['id'].'\').click()">打印货单</button>';
  507. }
  508. if ($info['status'] == 3) {
  509. $cancel = Dever::url('lib/buy.refund_apply_commit?json=1', 'mshop');
  510. $button[] = '<button class="layui-btn layui-btn-primary" onclick="$(\'#send_'.$info['id'].'\').click()">配送发货</button>';
  511. $button[] = '<button class="layui-btn layui-btn-primary" onclick="cancel('.$info['id'].','.$info['source_id'].', \''.$cancel.'\')">取消订单</button>';
  512. }
  513. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  514. $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系平台</button>';
  515. $html .= '<tr>
  516. <td>功能按钮</td>
  517. <td>'.$this->table(false, array($button)).'</td>
  518. </tr>';
  519. }
  520. $html .= '</tbody></table></div></div>';
  521. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  522. if (!$info['refund_cash']) {
  523. $info['refund_cash'] = 0;
  524. }
  525. if ($info['refund_cash'] > 0 && $tk) {
  526. if ($tk['process'] == 1) {
  527. $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>';
  528. } else {
  529. $process = $tk_process[$tk['process']];
  530. }
  531. $desc = array();
  532. $desc['退款原因'] = $tk['desc'];
  533. $desc['退款图片'] = '暂无';
  534. if ($tk['pic']) {
  535. $desc['退款图片'] = '';
  536. $pic = explode(',', $tk['pic']);
  537. foreach ($pic as $k => $v) {
  538. $desc['退款图片'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  539. }
  540. }
  541. $tk['desc'] = '<a href="javascript:showAlert($(\'#desc_content\').html());">查看</a><span id="desc_content" style="display:none">'.Dever::table($desc).'</span>';
  542. $head = array('退款金额', '数量', '退款进度', '退款原因');
  543. $body = array(array($info['refund_cash'], $info['num'], $process, $tk['desc']));
  544. $html .= '<div class="layui-card"><div class="layui-card-header">退款信息</div><div class="layui-card-body">' . $this->table($head, $body) . '</div></div>';
  545. }
  546. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  547. if ($info['ps_info']) {
  548. $info['ps_info']['service_name'] = '供应商自送';
  549. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  550. if ($info['ps_info']['ydate']) {
  551. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  552. }
  553. $status = Dever::db('shop/buy_order_ps')->config['status'];
  554. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  555. if ($info['ps_info']['service_id'] > 0) {
  556. //暂时还没有
  557. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  558. $info['ps_info']['service_name'] = $service['name'];
  559. }
  560. $head = array('名称', '单号', '费用', '配货员', '更多');
  561. $table = array();
  562. $table['打包员'] = $info['ps_info']['dby'];
  563. $table['数量'] = $info['ps_info']['num'];
  564. $table['重量'] = $info['ps_info']['zl'];
  565. $table['体积'] = $info['ps_info']['tj'];
  566. $table['发货时间'] = $info['ps_info']['cdate'];
  567. if ($info['ps_info']['ydate']) {
  568. $table['收货时间'] = $info['ps_info']['ydate'];
  569. }
  570. $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>'));
  571. $html .= '<div class="layui-card"><div class="layui-card-header">物流信息</div><div class="layui-card-body">' . $this->table($head, $body) . '</div></div>';
  572. }
  573. $body = array();
  574. $body_total = array();
  575. $body_total['price'] = 0;
  576. $body_total['num'] = 0;
  577. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  578. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  579. $refund_body = array();
  580. $refund_body_total = array();
  581. $refund_body_total['price'] = 0;
  582. $refund_body_total['num'] = 0;
  583. foreach ($goods as $k => $v) {
  584. if ($type == 2) {
  585. $v['price'] = $v['p_price'];
  586. }
  587. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  588. $status = $goods_status[$v['status']];
  589. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  590. if ($tk && $tk['process'] == 1) {
  591. if ($type == 1) {
  592. $status = '申请' . $tk_status[$tk['status']];
  593. } else {
  594. $status = '已上报';
  595. }
  596. if ($type == 1) {
  597. $desc = array();
  598. $desc['退款原因'] = $tk['desc'];
  599. $desc['退款图片'] = '暂无';
  600. if ($tk['pic']) {
  601. $desc['退款图片'] = '';
  602. $pic = explode(',', $tk['pic']);
  603. foreach ($pic as $k => $v) {
  604. $desc['退款图片'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  605. }
  606. }
  607. $status .= '&nbsp;&nbsp;<a href="javascript:showAlert($(\'#desc_content\').html());">查看原因</a><span id="desc_content" style="display:none">'.Dever::table($desc).'</span>';
  608. $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>';
  609. }
  610. } elseif ($type > 1 && (!$tk || ($tk && $tk['process'] == 3))) {
  611. $tui = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  612. if ($info['status'] == 3) {
  613. $status = '<a href="javascript:;" onclick="tui('.$v['id'].','.$info['id'].','.$info['source_id'].', 2, \''.$tui.'\')" class="layui-btn">缺货退款</a>';
  614. } elseif ($info['status'] == 4) {
  615. $status = '<a href="javascript:;" onclick="tui('.$v['id'].','.$info['id'].','.$info['source_id'].', 1, \''.$tui.'\')" class="layui-btn">报损退款</a>';
  616. }
  617. }
  618. if (isset($goods_info['sku'])) {
  619. $sku = '[' . $goods_info['sku']['string'] . ']';
  620. } else {
  621. $sku = '';
  622. }
  623. if ($tk && $v['status'] == 3) {
  624. $refund_body[] = array
  625. (
  626. $goods_info['name'] . ' ' . $sku,
  627. $v['price'],
  628. $v['num'],
  629. $tk_status[$tk['status']],
  630. );
  631. $refund_body_total['price'] += $v['price'];
  632. $refund_body_total['num'] += $v['num'];
  633. } else {
  634. $body[] = array
  635. (
  636. $goods_info['name'] . ' ' . $sku,
  637. $v['price'],
  638. $v['num'],
  639. $status,
  640. );
  641. $body_total['price'] += $v['price'];
  642. $body_total['num'] += $v['num'];
  643. }
  644. }
  645. if ($body) {
  646. $head = array('名称', '金额', '数量', '状态');
  647. $body[] = array
  648. (
  649. '合计',
  650. $body_total['price'],
  651. $body_total['num'],
  652. '-'
  653. );
  654. $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>';
  655. }
  656. if ($refund_body) {
  657. $head = array('名称', '金额', '数量', '状态');
  658. $refund_body[] = array
  659. (
  660. '合计',
  661. $refund_body_total['price'],
  662. $refund_body_total['num'],
  663. '-'
  664. );
  665. $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>';
  666. }
  667. $html .= '</div>';
  668. return $html;
  669. }
  670. private function table($head, $data)
  671. {
  672. $html = '';
  673. if ($head) {
  674. $html = '<table class="layui-table">';
  675. $html .= '<thead><tr>';
  676. foreach ($head as $k => $v) {
  677. $html .= '<th>'.$v.'</th>';
  678. }
  679. $html .= '</tr></thead>';
  680. $html .= '<tbody>';
  681. foreach ($data as $k => $v) {
  682. $html .= '<tr>';
  683. foreach ($v as $k1 => $v1) {
  684. $html .= '<td>'.$v1.'</td>';
  685. }
  686. $html .= '</tr>';
  687. }
  688. $html .= '</tbody>';
  689. $html .= '</table>';
  690. } else {
  691. foreach ($data as $k => $v) {
  692. $html .= '';
  693. foreach ($v as $k1 => $v1) {
  694. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  695. }
  696. $html .= '';
  697. }
  698. }
  699. return $html;
  700. }
  701. # 发货
  702. /*
  703. public function send_commit()
  704. {
  705. $order_id = Dever::input('order_id');
  706. $order = Dever::db('shop/buy_order')->find($order_id);
  707. if (!$order || ($order && $order['status'] != 3)) {
  708. Dever::alert('当前订单状态无法发货');
  709. }
  710. $state = Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 4));
  711. if ($state) {
  712. $this->updatePs($order, 1);
  713. }
  714. return 'reload';
  715. }*/
  716. # 采购订单审核 拆单
  717. public function audit_commit()
  718. {
  719. $order_id = Dever::input('order_id');
  720. $order = Dever::db('shop/buy_order')->find($order_id);
  721. if (!$order || ($order && $order['status'] != 2)) {
  722. Dever::alert('当前订单状态无法拆单');
  723. }
  724. if ($order['type'] != 1) {
  725. Dever::alert('这不是门店的订单');
  726. }
  727. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
  728. $shop = Dever::db('shop/info')->find($order['type_id']);
  729. # 获取门店分配的仓库和工厂
  730. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  731. $store = array();
  732. $distance = array();
  733. # 按照距离排序
  734. foreach ($shop_store as $k => $v) {
  735. $info = Dever::db('store/info')->find($v['store_id']);
  736. if ($info && $info['status'] == 1) {
  737. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  738. $store[] = $info;
  739. }
  740. }
  741. if (!$store) {
  742. Dever::alert('未分配仓库');
  743. }
  744. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  745. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  746. $factory = array();
  747. $distance = array();
  748. # 按照距离排序
  749. foreach ($shop_factory as $k => $v) {
  750. $info = Dever::db('factory/info')->find($v['factory_id']);
  751. if ($info && $info['status'] == 1) {
  752. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  753. $factory[] = $info;
  754. }
  755. }
  756. if (!$factory) {
  757. Dever::alert('未分配工厂');
  758. }
  759. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  760. $this->order_num = 0;
  761. $this->order_id = array();
  762. $delete = array();
  763. $yes = array();
  764. foreach ($goods as $k => $v) {
  765. # 开始找仓库
  766. $find = $this->store($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $store, $v);
  767. if (!$find[1]) {
  768. # 仓库没有,找厂家
  769. if ($factory) {
  770. $find = $this->factory($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $factory, $v);
  771. }
  772. }
  773. if ($find[1]) {
  774. $delete[$v['id']] = $v;
  775. } else {
  776. $yes[$v['id']] = $v;
  777. }
  778. }
  779. if ($this->order_num > 0) {
  780. if ($this->order_num == 1) {
  781. Dever::db('shop/buy_order')->delete($find[2][0]);
  782. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $find[2][0]));
  783. $p_price = 0;
  784. foreach ($goods as $k => $v) {
  785. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $order['id'], 'goods_id' => $v['goods_id']));
  786. if ($goods_info) {
  787. Dever::db('shop/buy_order_goods')->update(array('where_id' => $goods_info['id'], 'set_p_price' => $v['p_price']));
  788. $p_price += $v['p_price'];
  789. }
  790. }
  791. 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()));
  792. Dever::db('shop/buy_order_goods')->delete(array('order_id' => $find[2][0]));
  793. unset($this->order_id[$find[2][0]]);
  794. $this->order_id[$order['id']] = array($find[0], $find[1][0]);
  795. } else {
  796. if (!$yes) {
  797. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  798. } else {
  799. $price = 0;
  800. $num = 0;
  801. foreach ($delete as $k => $v) {
  802. Dever::db('shop/buy_order_goods')->delete(array('where_id' => $v['id']));
  803. $price += $v['price'];
  804. $num += $v['num'];
  805. }
  806. $price = $order['price'] - $price;
  807. $num = $order['num'] - $num;
  808. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $price, 'num' => $num));
  809. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  810. }
  811. }
  812. } else {
  813. # 没有找到,退款
  814. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  815. }
  816. if ($this->order_id) {
  817. foreach ($this->order_id as $k => $v) {
  818. $this->sendMsg($v[0], $v[1], $k);
  819. }
  820. }
  821. return 'ok';
  822. }
  823. private function store($order_id, $order_num, $shop, $store, $goods, $state = 2)
  824. {
  825. $find = array();
  826. $type_id = array();
  827. $id = array();
  828. if (!$store) {
  829. return array(2, $type_id, $id);
  830. }
  831. foreach ($store as $k => $v) {
  832. $where['store_id'] = $v['id'];
  833. $where['goods_id'] = $goods['goods_id'];
  834. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  835. $sku = Dever::db('store/goods_sku')->getOne($where);
  836. if ($sku) {
  837. if ($goods['num'] <= $sku['total']) {
  838. # 如果库存符合,直接下单即可,生成仓库订单
  839. $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']);
  840. if ($state) {
  841. $type_id[] = $where['store_id'];
  842. $id[] = $state;
  843. $this->order_id[$state] = array(2, $where['store_id']);
  844. break;
  845. }
  846. } elseif ($state == 1 && $sku['total'] > 0) {
  847. # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
  848. $goods['num'] = $goods['num'] - $sku['total'];
  849. # 生成仓库订单
  850. $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']);
  851. if (!$state) {
  852. $goods['num'] = $goods['num'] + $sku['total'];
  853. } else {
  854. $type_id[] = $where['store_id'];
  855. $id[] = $state;
  856. $this->order_id[$state] = array(2, $where['store_id']);
  857. }
  858. continue;
  859. } else {
  860. # 直接切换到下一个仓库处理
  861. continue;
  862. }
  863. }
  864. }
  865. return array(2, $type_id, $id);
  866. }
  867. private function factory($order_id, $order_num, $shop, $factory, $goods)
  868. {
  869. $find = array();
  870. $type_id = array();
  871. $id = array();
  872. if (!$factory) {
  873. return array(3, $type_id, $id);
  874. }
  875. foreach ($factory as $k => $v) {
  876. $where['factory_id'] = $v['id'];
  877. $where['goods_id'] = $goods['goods_id'];
  878. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  879. $sku = Dever::db('factory/goods_sku')->getOne($where);
  880. if ($sku) {
  881. $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']);
  882. if ($state) {
  883. $type_id[] = $where['factory_id'];
  884. $id[] = $state;
  885. $this->order_id[$state] = array(3, $where['factory_id']);
  886. break;
  887. }
  888. } else {
  889. continue;
  890. }
  891. }
  892. return array(3, $type_id, $id);
  893. }
  894. # 发送消息
  895. private function sendMsg($source_type, $source_id, $id)
  896. {
  897. if (Dever::project('message')) {
  898. $msg_param['type'] = 1;//消息类型1是订单消息
  899. $msg_param['id'] = $id;
  900. $msg_param['source_id'] = $source_id;
  901. $msg_param['source_type'] = $source_type;
  902. $msg_param = Dever::json_encode($msg_param);
  903. $project = $source_type + 1;
  904. $msg = '您有一个新的订货单,请及时处理。';
  905. Dever::load('message/lib/data')->push(-1, $source_id, '订货通知', $msg, 2, $project, false, $msg_param);
  906. }
  907. }
  908. # 生成拆单的订单
  909. private function createOrder($order_id, $order_num, $type, $type_id, $source_type, $source_id, $price, $p_price, $num, $goods_id, $sku_id)
  910. {
  911. $where['parent_order_id'] = $order_id;
  912. $where['type'] = $type;
  913. $where['type_id'] = $type_id;
  914. $where['source_type'] = $source_type;
  915. $where['source_id'] = $source_id;
  916. $where['r'] = $goods_id;
  917. $info = Dever::db('shop/buy_order')->find($where);
  918. if (!$info) {
  919. $this->order_num++;
  920. $data = $where;
  921. $data['name'] = $order_id;
  922. $data['order_num'] = $order_num;
  923. $data['operdate'] = time();
  924. $data['status'] = 3;
  925. $data['price'] = $price;
  926. $data['p_price'] = $p_price;
  927. $data['num'] = $num;
  928. $id = $state = Dever::db('shop/buy_order')->insert($data);
  929. } else {
  930. $id = $info['id'];
  931. $data['where_id'] = $id;
  932. $data['price'] = $info['price'] + $price;
  933. $data['p_price'] = $info['p_price'] + $p_price;
  934. $data['num'] = $info['num'] + $num;
  935. $state = Dever::db('shop/buy_order')->update($data);
  936. }
  937. if ($state && $id > 0) {
  938. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $id));
  939. if (!$goods_info) {
  940. $goods['order_id'] = $id;
  941. $goods['goods_id'] = $goods_id;
  942. $goods['sku_id'] = $sku_id;
  943. $goods['price'] = $price;
  944. $goods['p_price'] = $p_price;
  945. $goods['num'] = $num;
  946. Dever::db('shop/buy_order_goods')->insert($goods);
  947. }
  948. //$state = $this->updateSell($source_type, $source_id, $goods_id, $sku_id, $num);
  949. return $id;
  950. }
  951. return false;
  952. }
  953. # 减少库存,增加销量
  954. public function updateSell($source_type, $source_id, $goods_id, $sku_id, $num)
  955. {
  956. if ($source_type == 2) {
  957. $data['store_id'] = $source_id;
  958. $data['goods_id'] = $goods_id;
  959. $data['sku_id'] = $sku_id;
  960. $data['num'] = $num;
  961. return Dever::load('store/lib/goods')->oper($source_id, 1, 2, array($data));
  962. } else {
  963. $up = array();
  964. $up['where_factory_id'] = $source_id;
  965. $up['where_goods_id'] = $goods_id;
  966. $up['sell_num'] = $num;
  967. $state = Dever::db('factory/goods')->incSell($up);
  968. if ($state) {
  969. if ($sku_id > 0) {
  970. } else {
  971. $sku_id = -1;
  972. }
  973. $upSku = $up;
  974. $upSku['where_sku_id'] = $sku_id;
  975. $state = Dever::db('factory/goods_sku')->incSell($upSku);
  976. }
  977. return $state;
  978. }
  979. }
  980. }