Buy.php 43 KB

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