Out.php 16 KB

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