Out.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. namespace Mshop\Lib;
  3. use Dever;
  4. class Out
  5. {
  6. # 1是门店,2是仓库
  7. public $type = 1;
  8. # 1是列表,2是详情
  9. public $view = 1;
  10. # 获取配置
  11. public $config = array();
  12. # table
  13. public $table = 'shop/out_order';
  14. public function __construct()
  15. {
  16. $this->config = Dever::db($this->table)->config;
  17. }
  18. # 设置订单的类型
  19. public function set($type, $view)
  20. {
  21. $this->type = $type;
  22. $this->view = $view;
  23. return $this;
  24. }
  25. # 获取公共的where
  26. public function where($id)
  27. {
  28. $where = array();
  29. $where['type'] = $this->type;
  30. $where['type_id'] = $id;
  31. if (!$where) {
  32. Dever::alert('参数错误');
  33. }
  34. return $where;
  35. }
  36. # 采购订单列表
  37. public function getList($id)
  38. {
  39. $where = $this->where($id);
  40. $data['search_value'] = $where;
  41. $data['search_value']['day'] = $day = Dever::input('day');
  42. if ($day) {
  43. $where['start'] = Dever::maketime($day . ' 00:00:00');
  44. $where['end'] = Dever::maketime($day . ' 23:59:59');
  45. }
  46. $result['search_value']['start'] = $start = Dever::input('start');
  47. $result['search_value']['end'] = $end = Dever::input('end');
  48. if ($start && $end) {
  49. $where['start'] = Dever::maketime($start);
  50. $where['end'] = Dever::maketime($end);
  51. }
  52. $order_num = Dever::input('order_num');
  53. if ($order_num) {
  54. $where['order_num'] = $order_num;
  55. }
  56. $status = Dever::input('status');
  57. if ($status) {
  58. $where['status'] = $status;
  59. }
  60. $out_type = Dever::input('out_type');
  61. if ($out_type) {
  62. $where['out_type'] = $out_type;
  63. }
  64. $excel = Dever::input('excel', 1);
  65. if ($excel == 2) {
  66. $data['order'] = Dever::db('shop/out_order')->getData($where);
  67. } else {
  68. $data['order'] = Dever::db('shop/out_order')->getAll($where);
  69. }
  70. if ($data['order']) {
  71. foreach ($data['order'] as $k => $v) {
  72. $data['order'][$k] = $this->getInfo($v);
  73. }
  74. }
  75. return $data;
  76. }
  77. # 查看订单详情
  78. public function getView($id, $order_id, $show = true)
  79. {
  80. $where = $this->where($id);
  81. $where['id'] = $order_id;
  82. $result = Dever::db($this->table)->find($where);
  83. if (!$result) {
  84. Dever::alert('订单不存在');
  85. }
  86. if ($show) {
  87. $result = $this->getInfo($result, true);
  88. }
  89. return $result;
  90. }
  91. # 出库下单
  92. public function action($type_id, $name, $num, $goods, $price, $member_id, $out_type, $info, $status = 2, $area = '')
  93. {
  94. $order_data = $this->where($type_id);
  95. if ($area) {
  96. $order_data['area'] = $area . ',' . $type_id;
  97. }
  98. $order_data['name'] = $name;
  99. $order_data['num'] = $num;
  100. $order_data['info'] = $info;
  101. $order_data['out_type'] = $out_type;
  102. $order_data['price'] = $price;
  103. $order_data['member_id'] = $member_id;
  104. $order_data['status'] = $status;
  105. $order_data['order_num'] = $this->getOrderId();
  106. $id = Dever::db('shop/out_order')->insert($order_data);
  107. if (!$id) {
  108. Dever::alert('出库失败');
  109. }
  110. foreach($goods as $k => $v) {
  111. if ($v['ku_state'] == 1) {
  112. $data['order_id'] = $id;
  113. $data['goods_id'] = $v['id'];
  114. $data['sku_id'] = $v['sku_id'];
  115. $data['price'] = $v['price'];
  116. $data['num'] = $v['buy_num'];
  117. $state = Dever::db('shop/out_order_goods')->insert($data);
  118. if ($state) {
  119. # 出库成功 去掉库存
  120. Dever::load('shop/lib/goods')->oper($order_data, 2, 1, array($data));
  121. }
  122. }
  123. }
  124. return array('order_id' => $id);
  125. }
  126. # 生成订单号
  127. public function getOrderId()
  128. {
  129. $where['order_num'] = Dever::order('OUT');
  130. $state = Dever::db('shop/out_order')->one($where);
  131. if (!$state) {
  132. return $where['order_num'];
  133. } else {
  134. return $this->getOrderId();
  135. }
  136. }
  137. # 审核出库单
  138. public function yes_api()
  139. {
  140. $id = Dever::input('id');
  141. $order_id = Dever::input('order_id');
  142. $type = Dever::input('type', 2);
  143. $this->set($type, 1);
  144. $info = $this->getView($id, $order_id, false);
  145. if ($info && $info['status'] == 1) {
  146. $where['where_id'] = $info['id'];
  147. $where['status'] = 2;
  148. $state = Dever::db('shop/out_order')->update($where);
  149. }
  150. return 'reload';
  151. }
  152. # 取消出库单
  153. public function no()
  154. {
  155. $id = Dever::input('id');
  156. $order_id = Dever::input('order_id');
  157. $type = Dever::input('type', 2);
  158. $this->set($type, 1);
  159. $this->cancel($id, $order_id);
  160. return 'reload';
  161. }
  162. # 取消出库单
  163. public function cancel($id, $order_id)
  164. {
  165. $info = $this->getView($id, $order_id, false);
  166. if ($info && $info['status'] == 1) {
  167. $where['where_id'] = $info['id'];
  168. $where['status'] = 3;
  169. $state = Dever::db('shop/out_order')->update($where);
  170. if ($state) {
  171. # 取消成功,恢复库存
  172. $goods = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  173. if ($goods) {
  174. Dever::load('shop/lib/goods')->oper($info, 1, 1, $goods);
  175. }
  176. }
  177. }
  178. return 'ok';
  179. }
  180. # 获取订单详细信息
  181. private function getInfo($info, $view = false)
  182. {
  183. $info['goods'] = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  184. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  185. $type = Dever::db('shop/out_order')->config['config_type'];
  186. $type = $type();
  187. $info['type_name'] = $type[$info['out_type']]['name'];
  188. $status = Dever::db('shop/out_order')->config['config_status'];
  189. $info['status_name'] = $status[$info['status']];
  190. if ($view || $this->view == 2) {
  191. foreach ($info['goods'] as $k => $v) {
  192. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  193. }
  194. $info['member'] = Dever::db('shop/member')->find($info['member_id']);
  195. }
  196. return $info;
  197. }
  198. # 展示订单详情
  199. public function show()
  200. {
  201. $id = Dever::input('order_id');
  202. $type = Dever::input('type', 1);
  203. $config = Dever::db('shop/out_order')->config;
  204. $info = Dever::db('shop/out_order')->one($id);
  205. $status = $config['config_status'][$info['status']];
  206. $config_type = $config['config_type'];
  207. $config_type = $config_type();
  208. if (isset($config_type[$info['out_type']])) {
  209. $out_type = $config_type[$info['out_type']]['name'];
  210. } else {
  211. $out_type = '其他';
  212. }
  213. $cdate = date('Y-m-d H:i', $info['cdate']);
  214. if ($info['type'] == 1) {
  215. $type_info = Dever::db('shop/info')->find($info['type_id']);
  216. $member = Dever::db('shop/member')->find($info['member_id']);
  217. $name = '门店名称';
  218. } elseif ($info['type'] == 2) {
  219. $type_info = Dever::db('store/info')->find($info['type_id']);
  220. $member = Dever::db('store/member')->find($info['member_id']);
  221. $name = '仓库名称';
  222. }
  223. $result = array();
  224. $result[$info['order_num']] = array
  225. (
  226. 'type' => 'info',
  227. 'content' => array
  228. (
  229. array
  230. (
  231. array($name, $type_info['name']),
  232. array('出库单状态', $status),
  233. array('填单时间', $cdate),
  234. ),
  235. array
  236. (
  237. array('填单人', $member['name']),
  238. array('出库类别', $out_type),
  239. array('原因备注', $info['info']),
  240. ),
  241. )
  242. );
  243. $button = array();
  244. if ($type == 2) {
  245. $config = Dever::load('store/admin/auth.config');
  246. if ($info['status'] == 1) {
  247. $button[] = array
  248. (
  249. 'type' => 'action',
  250. 'link' => Dever::url('admin/out.cancel?order_id='.$info['id'], 'store'),
  251. 'name' => '取消',
  252. );
  253. }
  254. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  255. $button[] = array
  256. (
  257. 'type' => 'link',
  258. 'link' => Dever::url('admin/out.print?id=' . $info['id'] . '&type=2', 'store'),
  259. 'name' => '打印出库单',
  260. );
  261. $button[] = array
  262. (
  263. 'type' => 'link',
  264. 'link' => Dever::url('lib/out.excel_one?show=2&type='.$info['type'].'&id='.$info['id'], 'mshop'),
  265. 'name' => '导出',
  266. );
  267. $button[] = array
  268. (
  269. 'type' => 'alert',
  270. 'content' => $config['phone'],
  271. 'name' => '联系平台',
  272. );
  273. } elseif ($info['status'] == 1) {
  274. $button[] = array
  275. (
  276. 'type' => 'action',
  277. 'link' => Dever::url('lib/out.yes?id='.$info['type_id'].'&order_id='.$info['id'] . '&type=' . $info['type'], 'mshop'),
  278. 'name' => '审核通过',
  279. );
  280. $button[] = array
  281. (
  282. 'type' => 'action',
  283. 'link' => Dever::url('lib/out.no?id='.$info['type_id'].'&order_id='.$info['id'] . '&type=' . $info['type'], 'mshop'),
  284. 'name' => '审核不通过',
  285. );
  286. $button[] = array
  287. (
  288. 'type' => 'link',
  289. 'link' => Dever::url('lib/out.excel_one?type='.$info['type'].'&id='.$info['id'], 'mshop'),
  290. 'name' => '导出',
  291. );
  292. }
  293. $body = array();
  294. $body_total = array();
  295. $body_total['price'] = 0;
  296. $body_total['num'] = 0;
  297. $goods = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  298. $goods_status = Dever::db('shop/out_order_goods')->config['status'];
  299. foreach ($goods as $k => $v) {
  300. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  301. $status = $goods_status[$v['status']];
  302. if (isset($goods_info['sku'])) {
  303. $sku = $goods_info['sku']['string'];
  304. } else {
  305. $sku = '无';
  306. }
  307. $d = array
  308. (
  309. 'pic' => $goods_info['cover'],
  310. 'name' => $goods_info['name'],
  311. 'sku' => $sku,
  312. 'price' => $v['price'],
  313. 'num' => $v['num'],
  314. );
  315. if ($goods_info['price_type'] > 2) {
  316. $d['goods'] = $goods_info['goods'];
  317. }
  318. if ($info['type'] == 2) {
  319. unset($d['price']);
  320. }
  321. $body[] = $d;
  322. $price = $v['price']*$v['num'];
  323. $body_total['price'] += $price;
  324. $body_total['num'] += $v['num'];
  325. }
  326. if ($body) {
  327. if ($info['type'] == 2) {
  328. unset($body_total['price']);
  329. }
  330. $result['出库商品清单'] = array
  331. (
  332. 'type' => 'list',
  333. 'content' => $body,
  334. 'total' => $body_total,
  335. );
  336. }
  337. $head = array
  338. (
  339. 'name' => '基本信息',
  340. 'btn' => $button,
  341. );
  342. $html = Dever::show($head, $result);
  343. return $html;
  344. }
  345. public function printer($user)
  346. {
  347. $id = Dever::input('id');
  348. $type = Dever::input('type', 2);
  349. $factory_config = Dever::db('main/factory_config')->find();
  350. $main_config = Dever::db('main/config')->find();
  351. $config = Dever::db('main/config')->find();
  352. $config = Dever::db('shop/out_order')->config;
  353. $info = Dever::db('shop/out_order')->one($id);
  354. if ($info['type'] == 1) {
  355. $type_info = Dever::db('shop/info')->find($info['type_id']);
  356. $member = Dever::db('shop/member')->find($info['member_id']);
  357. } elseif ($info['type'] == 2) {
  358. $type_info = Dever::db('store/info')->find($info['type_id']);
  359. $member = Dever::db('store/member')->find($info['member_id']);
  360. }
  361. $status = $config['config_status'][$info['status']];
  362. $config_type = $config['config_type'];
  363. $config_type = $config_type();
  364. if (isset($config_type[$info['out_type']])) {
  365. $out_type = $config_type[$info['out_type']]['name'];
  366. } else {
  367. $out_type = '其他';
  368. }
  369. $cdate = date('Y-m-d H:i', $info['cdate']);
  370. $pdf = Dever::load('pdf/lib/base')->init();
  371. $pdf->hr('-', $main_config['name']);
  372. $pdf->br()->font(20)->center('出库单号:' . $info['order_num']);
  373. $pdf->font(10);
  374. $pdf->br(2);
  375. $pdf->br()->left('仓库名称:' . $type_info['name'], 80)->left('制单人:' . $user['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  376. $pdf->hr();
  377. $pdf->br()->left('出库单状态:' . $status, 140)->left('出库填单时间:' . $cdate, 40);
  378. $pdf->left('出库类别:' . $out_type);
  379. $pdf->left('原因备注:' . $info['info']);
  380. $pdf->hr();
  381. $head = array(array('商品编号', 40), array('商品名称', 70), array('商品属性', 60), array('出库数量', 20));
  382. $data = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  383. if ($data) {
  384. $body = array();
  385. $body_total = array();
  386. $body_total['num'] = 0;
  387. foreach ($data as $k => $v) {
  388. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  389. if (isset($goods_info['sku'])) {
  390. $sku = $goods_info['sku']['string'];
  391. } else {
  392. $sku = '';
  393. }
  394. $body[] = array
  395. (
  396. $goods_info['id'],
  397. $goods_info['name'],
  398. $sku,
  399. 'x ' . $v['num'],
  400. );
  401. $body_total['num'] += $v['num'];
  402. }
  403. $pdf->br();
  404. foreach ($head as $k => $v) {
  405. $pdf->left($v[0], $v[1]);
  406. }
  407. foreach ($body as $k => $v) {
  408. $pdf->br();
  409. foreach ($head as $k1 => $v1) {
  410. $pdf->left($v[$k1], $v1[1]);
  411. }
  412. }
  413. $pdf->br();
  414. $pdf->right('共'.$body_total['num'].'件商品');
  415. $pdf->hr();
  416. }
  417. $pdf->br(1);
  418. $pdf->right('如遇任何问题请致电客服');
  419. $pdf->br();
  420. $pdf->font(20);
  421. $pdf->left($main_config['name'], 160);
  422. $pdf->font(10);
  423. $pdf->right('电话:' . $factory_config['phone'], 30);
  424. $pdf->br();
  425. $pdf->left($main_config['site'], 160);
  426. $pdf->font(10);
  427. $pdf->right($main_config['worktime'], 30);
  428. $pdf->out('库存清单');
  429. }
  430. # 导出单个订单
  431. public function excel_one_api()
  432. {
  433. $this->type = Dever::input('type');
  434. $id = Dever::input('id');
  435. $data = Dever::db('shop/out_order')->select($id);
  436. $this->excel($data);
  437. }
  438. # 导出订单
  439. public function excel($data)
  440. {
  441. if (!$data) {
  442. Dever::alert('无导出数据');
  443. }
  444. $file = '出库订单';
  445. $type = Dever::input('search_option_type', $this->type);
  446. $header = array('所属仓库', '出库单号', '商品名称', '商品规格属性', '商品采购价格', '商品出库数量', '采购总价格', '出库类型', '出库状态', '出库时间');
  447. $table = 'store/info';
  448. if ($type == 1) {
  449. $header[0] = '所属门店';
  450. $header[4] = '商品销售价格';
  451. $header[6] = '销售总价格';
  452. $table = 'shop/info';
  453. }
  454. $show = Dever::input('show', 1);
  455. if ($type == 2) {
  456. unset($header[4]);
  457. unset($header[6]);
  458. }
  459. $body = array();
  460. $out_type = Dever::db('shop/out_type')->getAll();
  461. $status = Dever::db('shop/out_order')->config['config_status'];
  462. foreach ($data as $k => $v) {
  463. if (!is_array($v)) {
  464. continue;
  465. }
  466. $type_info = Dever::db($table)->find($v['type_id']);
  467. $goods = Dever::db('shop/out_order_goods')->select(array('order_id' => $v['id']));
  468. if (isset($out_type[$v['out_type']]) && $out_type[$v['out_type']]) {
  469. $out_type_name = $out_type[$v['out_type']]['name'];
  470. } else {
  471. $out_type_name = '其他';
  472. }
  473. $status_name = $status[$v['status']];
  474. $cdate = strstr($v['cdate'], '-') ? $v['cdate'] : date('Y-m-d H:i', $v['cdate']);
  475. foreach ($goods as $k1 => $v1) {
  476. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  477. if (isset($goods_info['sku'])) {
  478. $sku = $goods_info['sku']['string'];
  479. } else {
  480. $sku = '';
  481. }
  482. $d = array
  483. (
  484. $type_info['name'],
  485. $v['order_num'],
  486. $goods_info['name'],
  487. $sku,
  488. $v1['price'],
  489. $v1['num'],
  490. $v1['price']*$v1['num'],
  491. $out_type_name,
  492. $status_name,
  493. $cdate,
  494. );
  495. if ($type == 2) {
  496. unset($d[4]);
  497. unset($d[6]);
  498. }
  499. $body[] = $d;
  500. }
  501. }
  502. Dever::excelExport($body, $header, $file);
  503. }
  504. }