Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace Factory\Admin;
  3. use Dever;
  4. class Order extends Core
  5. {
  6. # 获取概览数据
  7. public function getData()
  8. {
  9. # 待处理订单
  10. $where['status'] = 3;
  11. $where['source_type'] = 3;
  12. $where['source_id'] = $this->id;
  13. # 待处理订单
  14. $data['order_num'] = Dever::db('shop/buy_order')->getOrderNum($where);
  15. # 超时待处理订单:72小时
  16. $where['end_o'] = time() - 72*3600;
  17. $data['cs_order_num'] = Dever::db('shop/buy_order')->getOrderNum($where);
  18. return $data;
  19. }
  20. # 订单列表
  21. public function getList()
  22. {
  23. $data = Dever::load('mshop/lib/buy')->set(13, 1)->getList($this->id);
  24. $html = '';
  25. if ($data && $data['order']) {
  26. $out_status = Dever::db('shop/buy_order')->config['out_status'];
  27. $excel = Dever::input('excel', 1);
  28. if ($excel == 2) {
  29. Dever::load('factory/admin/excel')->out($this->id, $this->user, $data['order']);
  30. die;
  31. return;
  32. }
  33. foreach ($data['order'] as $k => $v) {
  34. if ($v['ps_info']) {
  35. $ps_date = $v['ps_info']['cdate'];
  36. } else {
  37. $ps_date = '-';
  38. }
  39. $view = Dever::url('order_view?type=2&order_id=' . $v['id'] . '&cash=2', 'factory');
  40. $send = Dever::url('order_send?id=' . $v['id'], 'factory');
  41. $button = '<a href="'.$view.'"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">查看详情</button></a>';
  42. if ($v['status'] < 5) {
  43. $print = Dever::url('admin/order.print?id=' . $v['id'] . '&type=1', 'factory');
  44. $button .= '&nbsp;&nbsp;<a href="'.$print.'" id="print_'.$v['id'].'" target="_blank"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">打印备货单</button></a>';
  45. }
  46. if ($v['status'] == 3) {
  47. $button .= '&nbsp;&nbsp;<a href="javascript:;" id="send_'.$v['id'].'" onclick="fastEdit($(this), \''.$send.'\', \'配送发货\', \'\', 1)"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">配送发货</button></a>';
  48. }
  49. if ($v['refund_status'] == 2 && $v['status'] != 8) {
  50. $v['status_name'] .= '[缺货报损中]';
  51. }
  52. $order = array();
  53. if ($v['parent_type'] == 2) {
  54. $order = Dever::db('shop/sell_order')->find($v['parent_order_id']);
  55. if ($order && $order['uid']) {
  56. if ($order['address_id']) {
  57. $order['address'] = Dever::load('passport/address')->getOne($order['uid'], $order['address_id']);
  58. if ($order['address']) {
  59. $v['type_info']['name'] = $order['address']['contact'];
  60. } else {
  61. $user = Dever::db('passport/user')->find($order['uid']);
  62. $v['type_info']['name'] = $user['username'];
  63. }
  64. } else {
  65. $user = Dever::db('passport/user')->find($order['uid']);
  66. $v['type_info']['name'] = $user['username'];
  67. }
  68. }
  69. } elseif ($v['parent_type'] == 3) {
  70. $order = Dever::db('agent/dh_order')->find($v['parent_order_id']);
  71. if ($order && $order['mid']) {
  72. if ($order['address_id']) {
  73. $order['address'] = Dever::load('agent/address')->getOne($order['mid'], $order['address_id']);
  74. if (isset($order['address']['contact']) && $order['address']['contact']) {
  75. $v['type_info']['name'] = $order['address']['contact'];
  76. } else {
  77. $v['type_info']['name'] = '';
  78. }
  79. } else {
  80. $user = Dever::db('agent/member')->find($order['mid']);
  81. $v['type_info']['name'] = $user['name'];
  82. }
  83. }
  84. }
  85. $v['out_status_name'] = $out_status[$v['source_out_status']];
  86. $dao_date = '-';
  87. if ($v['source_out_date']) {
  88. $dao_date = date('Y-m-d H:i', $v['source_out_date']);
  89. }
  90. $html .= '<tr><td>'.$v['order_num'].'</td><td>'.$v['operdate'].'</td><td>'.$v['type_info']['name'].'</td><td>'.$ps_date.'</td><td>'.$v['p_price'].'</td><td>'.$v['status_name'].'</td><td>'.$v['out_status_name'].'</td><td>'.$dao_date.'</td><td>'.$button.'</td></tr>';
  91. }
  92. }
  93. return $html;
  94. }
  95. # 获取列表页搜索信息
  96. public function getSearch()
  97. {
  98. $config = array
  99. (
  100. array('value' => '0', 'name' => '全部'),
  101. array('value' => 3, 'name' => '待生产发货'),
  102. array('value' => 4, 'name' => '已发货'),
  103. array('value' => '5,6', 'name' => '已到货'),
  104. array('value' => '7,8,11', 'name' => '已取消'),
  105. array('value' => 'refund', 'name' => '缺货与报损'),
  106. );
  107. $status = Dever::input('status');
  108. $html = '';
  109. foreach ($config as $k => $v) {
  110. $selected = '';
  111. if ((!$status && $k == 0) || $status == $v['value']) {
  112. $selected = 'selected=""';
  113. }
  114. $html .= '<option value="'.$v['value'].'" '.$selected.'>'.$v['name'].'</option>';
  115. }
  116. return $html;
  117. }
  118. # 获取列表页搜索信息
  119. public function getSearchOutStatus()
  120. {
  121. $config = array
  122. (
  123. array('value' => '0', 'name' => '全部'),
  124. array('value' => 1, 'name' => '未导出'),
  125. array('value' => 2, 'name' => '已导出'),
  126. );
  127. $status = Dever::input('source_out_status');
  128. $html = '';
  129. foreach ($config as $k => $v) {
  130. $selected = '';
  131. if ((!$status && $k == 0) || $status == $v['value']) {
  132. $selected = 'selected=""';
  133. }
  134. $html .= '<option value="'.$v['value'].'" '.$selected.'>'.$v['name'].'</option>';
  135. }
  136. return $html;
  137. }
  138. # 获取列表页搜索信息
  139. public function getService()
  140. {
  141. $service = Dever::db('shop/service')->select();
  142. $html = '';
  143. foreach ($service as $k => $v) {
  144. $selected = '';
  145. if ($k == 0) {
  146. $selected = 'selected=""';
  147. }
  148. $html .= '<option value="'.$v['id'].'" '.$selected.'>'.$v['name'].'</option>';
  149. }
  150. return $html;
  151. }
  152. # 获取基本信息
  153. public function getConfig()
  154. {
  155. $data['search'] = Dever::url('order', 'factory');
  156. # 发货
  157. $data['send'] = Dever::url('admin/order.send', 'factory');
  158. # 退款
  159. $data['tui'] = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  160. # 导入物流单号
  161. $data['import'] = Dever::load('upload/import.url', 'factory/admin/order.import', 10);
  162. return $data;
  163. }
  164. # 导入物流单号
  165. public function import($file)
  166. {
  167. $file = Dever::local($file);
  168. $data = Dever::excelImport($file, 0);
  169. if ($data) {
  170. $psinfo = array();
  171. foreach ($data as $k => $v) {
  172. if ($k >= 2) {
  173. $order_num = $v['A'];
  174. if ($order_num) {
  175. $order = Dever::db('shop/buy_order')->find(array('order_num' => $order_num));
  176. if ($order && $order['status'] == 3) {
  177. if (!isset($psinfo[$order['id']])) {
  178. $psinfo[$order['id']] = array();
  179. }
  180. $psinfo[$order['id']][] = array($v['B'], $v['C'], $v['D']);
  181. }
  182. }
  183. }
  184. }
  185. if ($psinfo) {
  186. foreach ($psinfo as $k => $v) {
  187. $update = array();
  188. $update['order_id'] = $k;
  189. $service = array();
  190. foreach ($v as $k1 => $v1) {
  191. if ($v1[2] && strstr($v1[2], '-')) {
  192. $update['cdate'] = Dever::maketime($v1[2]);
  193. } else {
  194. $update['cdate'] = time();
  195. }
  196. $service_info = Dever::db('shop/service')->find(array('name' => $v1[1], 'clear' => true));
  197. if (!$service_info) {
  198. $service_id = Dever::db('shop/service')->insert(array('name' => $v1[1], 'type' => 1));
  199. } else {
  200. $service_id = $service_info['id'];
  201. }
  202. $service[] = array
  203. (
  204. 'order' => $k1,
  205. 'service_id' => $service_id,
  206. 'order_num' => $v1[0],
  207. );
  208. }
  209. if ($service) {
  210. $update['service'] = $service;
  211. }
  212. $this->send_action($update);
  213. }
  214. }
  215. }
  216. }
  217. # 发货
  218. public function send_api()
  219. {
  220. $input = Dever::input();
  221. $service = array();
  222. $order_id = Dever::input('update_where_id');
  223. foreach ($input as $k => $v) {
  224. if (strpos($k, '_c_') !== false) {
  225. if ($v == 'null') {
  226. $v = '';
  227. }
  228. $temp = explode('_c_', $k);
  229. $k = str_replace('update_', '', $temp[0]);
  230. if (!isset($c[$k])) {
  231. $c[$k] = array();
  232. }
  233. if (strpos($temp[1], '_i_') !== false) {
  234. $temp = explode('_i_', $temp[1]);
  235. if (!isset($c[$k][$temp[1]])) {
  236. $c[$k][$temp[1]] = 0;
  237. } else {
  238. $c[$k][$temp[1]]++;
  239. }
  240. $num = $c[$k][$temp[1]];
  241. $service[$k][$num][$temp[1]] = $v;
  242. }
  243. }
  244. }
  245. $update = array();
  246. if ($service) {
  247. $update = $service;
  248. }
  249. $update['order_id'] = $order_id;
  250. $update['cdate'] = $input['update_cdate'];
  251. /*
  252. $update['service_id'] = $input['update_service_id'];
  253. $update['order_num'] = $input['update_order_num'];
  254. $update['num'] = $input['update_num'];
  255. $update['phy'] = $input['update_phy'];
  256. $update['dby'] = $input['update_dby'];
  257. $update['zl'] = $input['update_zl'];
  258. $update['tj'] = $input['update_tj'];
  259. $update['price'] = $input['update_price'];
  260. */
  261. return $this->send_action($update);
  262. }
  263. private function send_action($update = array())
  264. {
  265. $info = Dever::db('shop/buy_order_ps')->find(array('order_id' => $update['order_id']));
  266. if (isset($update['service'])) {
  267. $update['service'] = Dever::array_encode($update['service']);
  268. }
  269. if ($info) {
  270. $update['where_id'] = $info['id'];
  271. Dever::db('shop/buy_order_ps')->update($update);
  272. } else {
  273. Dever::db('shop/buy_order_ps')->insert($update);
  274. }
  275. Dever::db('shop/buy_order')->update(array('status' => 4, 'where_id' => $update['order_id'],'fadate'=>time()));
  276. # 检测是否是关联订单
  277. $order = Dever::db('shop/buy_order')->find($update['order_id']);
  278. if ($order && $order['parent_type'] > 1) {
  279. $data = Dever::db('shop/buy_order')->find(array('status' => 3, 'parent_type' => $order['parent_type'], 'parent_order_id' => $order['parent_order_id']));
  280. if (!$data) {
  281. # 更改关联订单状态
  282. if ($order['parent_type'] == 2) {
  283. Dever::db('shop/sell_order')->update(array('where_id' => $order['parent_order_id'], 'status' => 4));
  284. $data = Dever::db('shop/buy_order')->getDataToId(array('parent_type' => $order['parent_type'], 'parent_order_id' => $order['parent_order_id']));
  285. $data = array_keys($data);
  286. $ps_info = Dever::db('shop/buy_order_ps')->getData(array('order_id' => $data));
  287. if ($ps_info) {
  288. $sell_order = array('id' => $order['parent_order_id']);
  289. foreach ($ps_info as $k => $v) {
  290. $v['service'] = Dever::array_decode($v['service']);
  291. foreach ($v['service'] as $k1 => $v1) {
  292. if (isset($v1['order_num']) && $v1['order_num']) {
  293. Dever::load('shop/lib/sell')->updatePs($sell_order, 2, $v1['service_id'], $v1['order_num'], $v['cdate']);
  294. }
  295. }
  296. }
  297. }
  298. } elseif ($order['parent_type'] == 3) {
  299. Dever::db('agent/dh_order')->update(array('where_id' => $order['parent_order_id'], 'status' => 4));
  300. $data = Dever::db('shop/buy_order')->getDataToId(array('parent_type' => $order['parent_type'], 'parent_order_id' => $order['parent_order_id']));
  301. $data = array_keys($data);
  302. $ps_info = Dever::db('shop/buy_order_ps')->getData(array('order_id' => $data));
  303. if ($ps_info) {
  304. $dh_order = array('id' => $order['parent_order_id']);
  305. foreach ($ps_info as $k => $v) {
  306. $v['service'] = Dever::array_decode($v['service']);
  307. foreach ($v['service'] as $k1 => $v1) {
  308. if (isset($v1['order_num']) && $v1['order_num']) {
  309. Dever::load('agent/lib/dhorder')->updatePs($dh_order, 2, $v1['service_id'], $v1['order_num'], $v['cdate']);
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. # 给仓库发消息
  318. if (Dever::project('message') && $order && $order['type'] == 2) {
  319. $msg_param['type'] = 3;//消息头类型3是入库订单消息
  320. $msg_param['id'] = $order['id'];
  321. $msg_param = Dever::json_encode($msg_param);
  322. $msg = '您有新的入库订货单已发货,请及时查收后确认。';
  323. Dever::load('message/lib/data')->push(-1, $order['type_id'], '入库订货单通知', $msg, 7, 3, false, $msg_param);
  324. }
  325. return 'reload';
  326. }
  327. # 打印订单单
  328. public function print_api()
  329. {
  330. return Dever::load('mshop/lib/buy')->printer($this->user);
  331. }
  332. # 获取报损信息
  333. public function refund()
  334. {
  335. $data = Dever::load('mshop/lib/buy')->refund_apply_info_one_info();
  336. if (isset($data['tui_price'])) {
  337. $data['tui_price_info'] = '¥' . $data['tui_price'];
  338. } else {
  339. $data['tui_price_info'] = '¥0';
  340. }
  341. return $data;
  342. }
  343. }