Out.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. $data['order'] = Dever::db('shop/out_order')->getAll($where);
  65. if ($data['order']) {
  66. foreach ($data['order'] as $k => $v) {
  67. $data['order'][$k] = $this->getInfo($v);
  68. }
  69. }
  70. return $data;
  71. }
  72. # 查看订单详情
  73. public function getView($id, $order_id, $show = true)
  74. {
  75. $where = $this->where($id);
  76. $where['id'] = $order_id;
  77. $result = Dever::db($this->table)->find($where);
  78. if (!$result) {
  79. Dever::alert('订单不存在');
  80. }
  81. if ($show) {
  82. $result = $this->getInfo($result, true);
  83. }
  84. return $result;
  85. }
  86. # 出库下单
  87. public function action($type_id, $name, $num, $goods, $price, $member_id, $out_type, $info, $status = 2)
  88. {
  89. $order_data = $this->where($type_id);
  90. $order_data['name'] = $name;
  91. $order_data['num'] = $num;
  92. $order_data['info'] = $info;
  93. $order_data['out_type'] = $out_type;
  94. $order_data['price'] = $price;
  95. $order_data['member_id'] = $member_id;
  96. $order_data['status'] = $status;
  97. $order_data['order_num'] = $this->getOrderId();
  98. $id = Dever::db('shop/out_order')->insert($order_data);
  99. if (!$id) {
  100. Dever::alert('出库失败');
  101. }
  102. foreach($goods as $k => $v) {
  103. if ($v['ku_state'] == 1) {
  104. $data['order_id'] = $id;
  105. $data['goods_id'] = $v['id'];
  106. $data['sku_id'] = $v['sku_id'];
  107. $data['price'] = $v['price'];
  108. $data['num'] = $v['buy_num'];
  109. $state = Dever::db('shop/out_order_goods')->insert($data);
  110. if ($state) {
  111. # 出库成功 去掉库存
  112. Dever::load('shop/lib/goods')->oper($order_data, 2, 1, array($data));
  113. }
  114. }
  115. }
  116. return array('order_id' => $id);
  117. }
  118. # 生成订单号
  119. public function getOrderId()
  120. {
  121. $where['order_num'] = Dever::order('OUT');
  122. $state = Dever::db('shop/out_order')->one($where);
  123. if (!$state) {
  124. return $where['order_num'];
  125. } else {
  126. return $this->getOrderId();
  127. }
  128. }
  129. # 审核出库单
  130. public function yes_api()
  131. {
  132. $id = Dever::input('id');
  133. $order_id = Dever::input('order_id');
  134. $this->set(2, 1);
  135. $info = $this->getView($id, $order_id, false);
  136. if ($info && $info['status'] == 1) {
  137. $where['where_id'] = $info['id'];
  138. $where['status'] = 2;
  139. $state = Dever::db('shop/out_order')->update($where);
  140. }
  141. return 'ok';
  142. }
  143. # 取消出库单
  144. public function cancel($id, $order_id)
  145. {
  146. $info = $this->getView($id, $order_id, false);
  147. if ($info && $info['status'] == 1) {
  148. $where['where_id'] = $info['id'];
  149. $where['status'] = 3;
  150. $state = Dever::db('shop/out_order')->update($where);
  151. if ($state) {
  152. # 取消成功,恢复库存
  153. $goods = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  154. if ($goods) {
  155. Dever::load('shop/lib/goods')->oper($info, 1, 1, $goods);
  156. }
  157. }
  158. }
  159. return 'ok';
  160. }
  161. # 获取订单详细信息
  162. private function getInfo($info, $view = false)
  163. {
  164. $info['goods'] = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  165. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  166. $type = Dever::db('shop/out_order')->config['config_type'];
  167. $info['type_name'] = $type[$info['out_type']];
  168. $status = Dever::db('shop/out_order')->config['config_status'];
  169. $info['status_name'] = $status[$info['status']];
  170. if ($view || $this->view == 2) {
  171. foreach ($info['goods'] as $k => $v) {
  172. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  173. }
  174. $info['member'] = Dever::db('shop/member')->find($info['member_id']);
  175. }
  176. return $info;
  177. }
  178. # 展示订单详情
  179. public function show()
  180. {
  181. $id = Dever::input('order_id');
  182. $type = Dever::input('type', 1);
  183. $config = Dever::db('shop/out_order')->config;
  184. $info = Dever::db('shop/out_order')->one($id);
  185. $status = $config['config_status'][$info['status']];
  186. $out_type = $config['config_type'][$info['out_type']];
  187. $cdate = date('Y-m-d H:i', $info['cdate']);
  188. $html = '<div class="layui-col-md12"><div class="layui-card"><div class="layui-card-header">基本信息</div><div class="layui-card-body">';
  189. $html .= '<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  190. $html .= '<tr>
  191. <td>订单号</td>
  192. <td>'.$this->table(false, array($info['order_num'])).'</td>
  193. </tr>';
  194. $html .= '<tr>
  195. <td>出库单状态</td>
  196. <td>'.$this->table(false, array($status)).'</td>
  197. </tr>';
  198. $html .= '<tr>
  199. <td>填单时间</td>
  200. <td>'.$this->table(false, array($cdate)).'</td>
  201. </tr>';
  202. if ($info['type'] == 1) {
  203. $type_info = Dever::db('shop/info')->find($info['type_id']);
  204. $member = Dever::db('shop/member')->find($info['member_id']);
  205. } elseif ($info['type'] == 2) {
  206. $type_info = Dever::db('store/info')->find($info['type_id']);
  207. $member = Dever::db('store/member')->find($info['member_id']);
  208. }
  209. if ($type == 1) {
  210. $html .= '<tr>
  211. <td>来源信息</td>
  212. <td>'.$this->table(false, array(array($type_info['name'] . ',' . $type_info['truename'] . ',' . $type_info['mobile'] . ',' . $type_info['address']))).'</td>
  213. </tr>';
  214. }
  215. $html .= '<tr>
  216. <td>操作人</td>
  217. <td>'.$this->table(false, array($member['name'])).'</td>
  218. </tr>';
  219. $html .= '<tr>
  220. <td>出库类别</td>
  221. <td>'.$this->table(false, array($out_type)).'</td>
  222. </tr>';
  223. if ($info['info']) {
  224. $html .= '<tr>
  225. <td>订单备注</td>
  226. <td>'.$this->table(false, array(array($info['info']))).'</td>
  227. </tr>';
  228. }
  229. if ($type == 2) {
  230. $config = Dever::load('store/admin/auth.config');
  231. $button = array();
  232. if ($info['status'] == 1) {
  233. $url = Dever::url('admin/out.cancel?order_id='.$info['id'], 'store');
  234. $button[] = '&nbsp;&nbsp;<a href="javascript:;" onclick="load(\''.$url.'\')"><button type="button" class="layui-btn layui-btn-primary">取消</button></a>';
  235. }
  236. $print = Dever::url('admin/out.print?id=' . $info['id'] . '&type=2', 'store');
  237. $button[] = '<a class="layui-btn layui-btn-primary" href="'.$print.'" target="_blank">打印出库单</a>';
  238. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  239. $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系平台</button>';
  240. $html .= '<tr>
  241. <td>功能按钮</td>
  242. <td>'.$this->table(false, array($button)).'</td>
  243. </tr>';
  244. } elseif ($info['status'] == 1) {
  245. $button = array();
  246. $url = Dever::url('lib/out.yes?id='.$info['type_id'].'&order_id='.$info['id'], 'mshop');
  247. $button[] = '&nbsp;&nbsp;<a href="javascript:;" onclick="load(\''.$url.'\')"><button type="button" class="layui-btn layui-btn-primary">审核</button></a>';
  248. $html .= '<tr>
  249. <td>功能按钮</td>
  250. <td>'.$this->table(false, array($button)).'</td>
  251. </tr>';
  252. }
  253. $html .= '</tbody></table></div></div>';
  254. $body = array();
  255. $body_total = array();
  256. $body_total['price'] = 0;
  257. $body_total['num'] = 0;
  258. $goods = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  259. $goods_status = Dever::db('shop/out_order_goods')->config['status'];
  260. foreach ($goods as $k => $v) {
  261. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  262. $status = $goods_status[$v['status']];
  263. if (isset($goods_info['sku'])) {
  264. $sku = $goods_info['sku']['string'];
  265. } else {
  266. $sku = '无';
  267. }
  268. $d = array
  269. (
  270. $goods_info['name'],
  271. $sku,
  272. $v['price'],
  273. $v['num'],
  274. );
  275. if ($type == 2) {
  276. unset($d[2]);
  277. }
  278. $body[] = $d;
  279. $price = $v['price']*$v['num'];
  280. $body_total['price'] += $price;
  281. $body_total['num'] += $v['num'];
  282. }
  283. if ($body) {
  284. $head = array('商品名称', '商品属性', '商品单价', '商品数量');
  285. $d = array
  286. (
  287. '合计',
  288. '-',
  289. round($body_total['price'], 2),
  290. $body_total['num'],
  291. );
  292. if ($type == 2) {
  293. unset($head[2]);
  294. unset($d[2]);
  295. }
  296. $body[] = $d;
  297. $html .= '<div class="layui-card"><div class="layui-card-header">商品清单</div><div class="layui-card-body" style="max-heights: 500px;overflow: auto;">' . $this->table($head, $body) . '</div></div>';
  298. }
  299. $html .= '</div>';
  300. return '<div class="layui-card-body">' . $html . '</div>';
  301. }
  302. private function table($head, $data)
  303. {
  304. $html = '';
  305. if ($head) {
  306. $html = '<table class="layui-table">';
  307. $html .= '<thead><tr>';
  308. foreach ($head as $k => $v) {
  309. $html .= '<th>'.$v.'</th>';
  310. }
  311. $html .= '</tr></thead>';
  312. $html .= '<tbody>';
  313. foreach ($data as $k => $v) {
  314. $html .= '<tr>';
  315. foreach ($v as $k1 => $v1) {
  316. $html .= '<td>'.$v1.'</td>';
  317. }
  318. $html .= '</tr>';
  319. }
  320. $html .= '</tbody>';
  321. $html .= '</table>';
  322. } else {
  323. foreach ($data as $k => $v) {
  324. $html .= '';
  325. if (is_array($v)) {
  326. foreach ($v as $k1 => $v1) {
  327. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  328. }
  329. } else {
  330. $html .= $v . '&nbsp;&nbsp;&nbsp;&nbsp;';
  331. }
  332. $html .= '';
  333. }
  334. }
  335. return $html;
  336. }
  337. public function printer($user)
  338. {
  339. $id = Dever::input('id');
  340. $type = Dever::input('type', 2);
  341. $factory_config = Dever::db('main/factory_config')->find();
  342. $main_config = Dever::db('main/config')->find();
  343. $config = Dever::db('main/config')->find();
  344. $config = Dever::db('shop/out_order')->config;
  345. $info = Dever::db('shop/out_order')->one($id);
  346. if ($info['type'] == 1) {
  347. $type_info = Dever::db('shop/info')->find($info['type_id']);
  348. $member = Dever::db('shop/member')->find($info['member_id']);
  349. } elseif ($info['type'] == 2) {
  350. $type_info = Dever::db('store/info')->find($info['type_id']);
  351. $member = Dever::db('store/member')->find($info['member_id']);
  352. }
  353. $status = $config['config_status'][$info['status']];
  354. $out_type = $config['config_type'][$info['out_type']];
  355. $cdate = date('Y-m-d H:i', $info['cdate']);
  356. $pdf = Dever::load('pdf/lib/base')->init();
  357. $pdf->hr('-', $main_config['name']);
  358. $pdf->br()->font(20)->center('出库单号:' . $info['order_num']);
  359. $pdf->font(10);
  360. $pdf->br(2);
  361. $pdf->br()->left('仓库名称:' . $type_info['name'], 80)->left('制单人:' . $user['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  362. $pdf->hr();
  363. $pdf->br()->left('出库单状态:' . $status, 140)->left('出库填单时间:' . $cdate, 40);
  364. $pdf->left('出库类别:' . $out_type);
  365. $pdf->left('原因备注:' . $info['info']);
  366. $pdf->hr();
  367. $head = array(array('商品编号', 40), array('商品名称', 70), array('商品属性', 60), array('出库数量', 20));
  368. $data = Dever::db('shop/out_order_goods')->select(array('order_id' => $info['id']));
  369. if ($data) {
  370. $body = array();
  371. $body_total = array();
  372. $body_total['num'] = 0;
  373. foreach ($data as $k => $v) {
  374. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  375. if (isset($goods_info['sku'])) {
  376. $sku = $goods_info['sku']['string'];
  377. } else {
  378. $sku = '';
  379. }
  380. $body[] = array
  381. (
  382. $goods_info['id'],
  383. $goods_info['name'],
  384. $sku,
  385. 'x ' . $v['num'],
  386. );
  387. $body_total['num'] += $v['num'];
  388. }
  389. $pdf->br();
  390. foreach ($head as $k => $v) {
  391. $pdf->left($v[0], $v[1]);
  392. }
  393. foreach ($body as $k => $v) {
  394. $pdf->br();
  395. foreach ($head as $k1 => $v1) {
  396. $pdf->left($v[$k1], $v1[1]);
  397. }
  398. }
  399. $pdf->br();
  400. $pdf->right('共'.$body_total['num'].'件商品');
  401. $pdf->hr();
  402. }
  403. $pdf->br(1);
  404. $pdf->right('如遇任何问题请致电客服');
  405. $pdf->br();
  406. $pdf->font(20);
  407. $pdf->left($main_config['name'], 160);
  408. $pdf->font(10);
  409. $pdf->right('电话:' . $factory_config['phone'], 30);
  410. $pdf->br();
  411. $pdf->left($main_config['site'], 160);
  412. $pdf->font(10);
  413. $pdf->right($main_config['worktime'], 30);
  414. $pdf->out('库存清单');
  415. }
  416. }