Outerrors.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. header("Access-Control-Allow-Origin: *");
  3. class Outerrors extends Start_Controller{
  4. public function __construct(){
  5. parent::__construct();
  6. $this->load->library('session');
  7. $this->load->_model('Model_outerrors','outerrors');
  8. $this->load->_model("Model_logic_order","logic_order");
  9. $this->load->_model("Model_check",'check');
  10. $this->load->_model("Model_express","express");
  11. $this->load->_model("Model_warehouse","warehouse");
  12. $this->load->_model('Model_whlabel','whlabel');
  13. $this->load->_model("Model_logic_express","logic_express");
  14. $this->load->_model("Model_ck",'ck');
  15. $this->load->_model("Model_shop","shop");
  16. $this->load->_model('Model_specialstock','specialstock');
  17. $this->load->_model('Model_fullorder','fullorder');
  18. $this->load->_model('Model_fullordersmt','fullordersmt');
  19. $this->load->_model('Model_fullordertt','fullordertt');
  20. $this->load->_model('Model_fullorderamz','fullorderamz');
  21. $this->load->_model('Model_dhl','dhl');
  22. $this->load->_model('Model_dhlhz','dhlhz');
  23. $this->load->_model('Model_typeclass','typeclass');
  24. $this->load->_model("Model_logic_ding",'logic_ding');
  25. }
  26. //定义方法的调用规则 获取URI第二段值
  27. public function _remap($arg,$arg_array)
  28. {
  29. if($arg == 'scan')//出库页
  30. {
  31. $this->_scan();
  32. }
  33. else if($arg == 'operate')//出库列表页
  34. {
  35. $this->_operate();
  36. }
  37. else if($arg == 'excel'){
  38. $this->_excel();
  39. }
  40. else
  41. {
  42. $this->_index();
  43. }
  44. }
  45. private function _index()
  46. {
  47. if($this->input->method() === 'post'){
  48. $page = $this->input->post('page',true);
  49. $perpage = $this->input->post('perpage',true);
  50. $number = $this->input->post('number',true);
  51. $timetk = $this->input->post('timetk',true);
  52. $timetj = $this->input->post('timetj',true);
  53. $order_status = $this->input->post('order_status',true);
  54. $timetk = strtotime($timetk);
  55. $timetj = strtotime($timetj);
  56. $where = "";
  57. if($number){
  58. $where .= " number = '$number' ";
  59. }
  60. if(empty($where)){
  61. $where = " scan_time > '$timetk' and scan_time < '$timetj' ";
  62. }else{
  63. $where .= " and scan_time > '$timetk' and scan_time < '$timetj' ";
  64. }
  65. if($order_status >= 0){
  66. $where .= " and order_status = '$order_status' ";
  67. }
  68. //数据排序
  69. $order_str = "order_status asc, id desc";
  70. if(empty($page))
  71. {
  72. $start = 0;
  73. $perpage = 1;
  74. }
  75. else
  76. {
  77. $start = ($page - 1)*$perpage;
  78. }
  79. $list = $this->outerrors->find_all($where,"*",$order_str,$start,$perpage);
  80. $info_list = [];
  81. foreach($list as $k => $v){
  82. $str = "";
  83. $str .= "<p ><b style='font-size:18px !important' data-id='".$v['id']."' data-type='1' data-operate='".$v['dy_operate']."' onclick='dycz(this)'>店员备注</b></p>";
  84. if(!empty($v['dy_status']) ){
  85. $str .= "<p><b style='font-size:18px !important;background-color: #FFC125;color: #000;' data-id='".$v['id']."' data-type='2' data-operate='".$v['ck_operate']."' onclick='dycz(this)'>仓库备注</b></p>";
  86. }
  87. $dy_operate_str = "";
  88. if(!empty($v['dy_status'])){
  89. if($v['order_status'] == 0){
  90. if(mb_stripos($v['dy_operate'],'发走') !== false){
  91. $dy_operate_str = "<p style='color:red;font-weight:bold;'>".$v['dy_operate']."</p>";
  92. }else{
  93. $dy_operate_str = $v['dy_operate'];
  94. }
  95. }else{
  96. $dy_operate_str = $v['dy_operate'];
  97. }
  98. }
  99. $info_list[] = [
  100. 'id' => $v['id'],
  101. 'scan_time' => date('Y-m-d H:i:s',$v['scan_time']),
  102. 'number' => $v['number'],
  103. 'waybill' => $v['waybill'],
  104. 'order_status'=>empty($v['order_status'])?'未出库':date("Y-m-d H:i:s",$v['library_time'])."出库",
  105. 'dy_status'=>empty($v['dy_status'])?'':$dy_operate_str,
  106. 'ck_status' => empty($v['ck_status'])?'':$v['ck_operate'],
  107. 'remark'=>empty($v['remark'])?'':$v['remark'],
  108. 'opreate'=>$str,
  109. ];
  110. }
  111. $total = $this->outerrors->find_count($where);
  112. $pagenum = ceil($total/$perpage);
  113. $over = $total-($start+$perpage);
  114. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  115. echo json_encode($rows);exit;
  116. }
  117. $this->_Template('outerrors',$this->data);
  118. }
  119. private function _operate(){
  120. if($this->input->method() === 'post'){
  121. $id = $this->input->post('id',true);
  122. $type = $this->input->post('operate_type',true);
  123. $remark = $this->input->post('operate_remark',true);
  124. if($type == 1){
  125. $arr = [
  126. 'dy_status'=>1,
  127. 'dy_operate'=>$remark,
  128. 'dy_time'=>time(),
  129. ];
  130. }else if($type == 2){
  131. $arr = [
  132. 'ck_status'=>1,
  133. 'ck_operate'=>$remark,
  134. 'ck_time'=>time(),
  135. ];
  136. }else{
  137. echo json_encode(['code'=>-1,'msg'=>'执行类型异常']);exit;
  138. }
  139. if($this->outerrors->save($arr,$id)){
  140. echo json_encode(['code'=>1,'msg'=>'操作成功']);exit;
  141. }else{
  142. echo json_encode(['code'=>-1,'msg'=>'操作失败']);exit;
  143. }
  144. }
  145. echo json_encode(['code'=>-1,'msg'=>'参数异常']);exit;
  146. }
  147. //excel 下载
  148. private function _excel(){
  149. if($this->input->method() === 'get'){
  150. $excel = $this->input->get('excel',true);
  151. if($excel == 2){
  152. $number = $this->input->get('number',true);
  153. $timetk = $this->input->get('timetk',true);
  154. $timetj = $this->input->get('timetj',true);
  155. $timetk = strtotime($timetk);
  156. $timetj = strtotime($timetj);
  157. $where = "";
  158. if($number){
  159. $where .= " number = '$number' ";
  160. }
  161. if(empty($where)){
  162. $where = " scan_time > '$timetk' and scan_time < '$timetj' ";
  163. }else{
  164. $where .= " and scan_time > '$timetk' and scan_time < '$timetj' ";
  165. }
  166. $list = $this->outerrors->find_all($where,"*");
  167. $info_list = [];
  168. foreach($list as $k => $v){
  169. $info_list[] = [
  170. 'id' => $v['id'],
  171. 'number' => $v['number'],
  172. 'dy_operate'=>$v['dy_operate'],
  173. 'ck_operate' => $v['ck_operate'],
  174. 'remark'=>empty($v['remark'])?'':$v['remark'],
  175. 'scan_time' => date('Y-m-d H:i:s',$v['scan_time']),
  176. 'dy_time'=>empty($v['dy_time'])?'':date('Y-m-d H:i:s',$v['dy_time']),
  177. 'ck_time'=>empty($v['ck_time'])?'':date('Y-m-d H:i:s',$v['ck_time']),
  178. ];
  179. }
  180. $filename = date("Y-m-d")."扫不出库记录.xls";
  181. $str = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">
  182. <head>
  183. <!--[if gte mso 9]><xml>
  184. <x:ExcelWorkbook>
  185. <x:ExcelWorksheets>
  186. <x:ExcelWorksheet>
  187. <x:Name>EXCEL</x:Name>
  188. <x:WorksheetOptions>
  189. <x:Print>
  190. <x:ValidPrinterInfo />
  191. </x:Print>
  192. </x:WorksheetOptions>
  193. </x:ExcelWorksheet>
  194. </x:ExcelWorksheets>
  195. </x:ExcelWorkbook>
  196. </xml>
  197. <![endif]-->
  198. </head><body>";
  199. $str .= "<table border=1 style='font-family: Microsoft Yahei;font-size: 13px;'>";
  200. $str .= "<tr><td>订单编码</td><td>店员操作</td><td>仓库操作</td><td>不能出库原因</td><td>扫描时间</td><td>店员操作时间</td><td>仓库操作时间</td></tr>";
  201. foreach ($info_list as $key=>$value)
  202. {
  203. $str .= "<tr>";
  204. $str .= "<td>". $value['number']. "</td>";
  205. $str .= "<td>". $value['dy_operate']. "</td>";
  206. $str .= "<td>". $value['ck_operate']. "</td>";
  207. $str .= "<td>". $value['remark']. "</td>";
  208. $str .= "<td>". $value['scan_time']. "</td>";
  209. $str .= "<td>". $value['dy_time']. "</td>";
  210. $str .= "<td>". $value['ck_time']. "</td>";
  211. $str .= "</tr>";
  212. }
  213. $str .= "</table></body></html>";
  214. header( "Content-Type: application/vnd.ms-excel; name='excel'" );
  215. header( "Content-type: application/octet-stream" );
  216. header( "Content-Disposition: attachment; filename=".$filename );
  217. header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  218. header( "Pragma: no-cache" );
  219. header( "Expires: 0" );
  220. exit($str);
  221. }
  222. }
  223. }
  224. //扫描未出库的单子
  225. private function _scan(){
  226. if($this->input->method() === 'post'){
  227. $waybill = $this->input->post('waybill',true);
  228. if(empty($waybill)){
  229. echo json_encode(['code'=>-1,'msg'=>'快递单号不能为空']);exit;
  230. }
  231. $waybill = trim($waybill);
  232. if(substr($waybill,0,3) == '788' && substr($waybill,-4,4) == '0430')//Fedex联邦
  233. {
  234. $waybill = substr($waybill,0,12);
  235. }
  236. if(strlen($waybill) == '34')//联邦杭州超长运单截取
  237. {
  238. $str = substr($waybill, 0,3);
  239. if($str == "420"){
  240. $waybill = substr($waybill,12);
  241. }else{
  242. $waybill = substr($waybill,22,12);
  243. }
  244. }
  245. if(!$waybill)
  246. {
  247. echo json_encode(array('msg'=>'出库信息不能为空!','success'=>false));exit;
  248. }
  249. if(strlen($waybill) == 30)
  250. {
  251. $waybill = substr($waybill, 8);
  252. }
  253. $this->logic_ding->sendToDing("扫描未出库单子的数据".json_encode([
  254. 'waybill' => $waybill,
  255. ],JSON_UNESCAPED_UNICODE));
  256. $info = $this->logic_order->getInfo("waybill = '$waybill'");
  257. $outtype = 1;
  258. if(empty($info)){
  259. $outtype = 2;
  260. $info = $this->logic_order->getInfo("number = '$waybill'");
  261. }
  262. if(empty($info)){
  263. $info = $this->logic_order->getInfo("oldwaybill like '%/".$waybill."%'");
  264. if(!empty($info)){
  265. $arr = [
  266. 'number'=>$info['number'],
  267. 'waybill'=>$waybill,//扫描单号
  268. 'plat'=>$info['lv_platform'],
  269. 'shop'=>$info['shop'],
  270. 'remark'=>"无此数据",
  271. 'scan_time'=>time(),
  272. ];
  273. if($this->outerrors->insert($arr)){
  274. echo json_encode(['code'=>1,'msg'=>'扫描成功']);exit;
  275. }else{
  276. echo json_encode(['code'=>-1,'msg'=>'操作失败']);exit;
  277. }
  278. }
  279. }
  280. if(empty($info)){
  281. echo json_encode(['code'=>-1,'msg'=>'未发现和 '.$waybill.' 关联的订单信息']);exit;
  282. }
  283. $r_info = $this->outerrors->find("waybill = '$waybill'");
  284. if(!empty($r_info)){
  285. echo json_encode(['code'=>-1,'msg'=>''.$waybill.' 的相关信息已录入,记录单号为:'.$r_info['number']]);exit;
  286. }
  287. $r = $this->_outerrorinfo($info,$outtype);
  288. if($r['success']){
  289. echo json_encode(['code'=>-1,'msg'=>$r['msg']]);exit;
  290. }
  291. $arr = [
  292. 'number'=>$info['number'],
  293. 'waybill'=>$waybill,//扫描单号
  294. 'plat'=>$info['lv_platform'],
  295. 'shop'=>$info['shop'],
  296. 'remark'=>$r['msg'],
  297. 'scan_time'=>time(),
  298. ];
  299. if($this->outerrors->insert($arr)){
  300. echo json_encode(['code'=>1,'msg'=>'扫描成功']);exit;
  301. }else{
  302. echo json_encode(['code'=>-1,'msg'=>'操作失败']);exit;
  303. }
  304. }
  305. $this->_Template('outerrors_scan',$this->data);
  306. }
  307. //出库错误的原因返回
  308. private function _outerrorinfo($info,$outtype){
  309. $time = time();
  310. $y = $info;
  311. $fullorder_name = $y['lv_platform'];
  312. if($y['mergeid'] == '1')
  313. {
  314. $y = $this->$fullorder_name->read($y['merge']);
  315. }
  316. // 66 云途(欧洲) 67 云途(欧洲)-带保险 如果是欧洲云途 需要检擦出库时间是否超过15天
  317. if(in_array($y['express'],[66,67,82])){
  318. if((time() - $y['printtime']) >= (14*24*3600)){
  319. return array('msg'=>'运单超时!请联系梦体处理!','success'=>false);
  320. //echo json_encode(array('msg'=>'运单超时!请联系梦体处理!','success'=>false));exit;
  321. }
  322. }
  323. // 78 dpd英国 和 79dpd欧盟
  324. if(in_array($y['express'],[78,79,84])){
  325. $r = $this->check->checkOverdue($y['name'],$y['number'],$y['address']);
  326. if(!$r){
  327. return array('msg'=>'同一个客户3天内发货超过2包,不可出库!请联系店员做申报信息修改!'.$y['name']."-".$y['number']."-".$r*1,'success'=>false);
  328. //echo json_encode(array('msg'=>'同一个客户3天内发货超过2包,不可出库!请联系店员做申报信息修改!'.$y['name']."-".$y['number']."-".$r*1,'success'=>false));exit;
  329. }
  330. }
  331. $express = $this->express->read($y['express']);
  332. if($y['state'] == 216 && $fullorder_name == 'fullordersmt')
  333. {
  334. $y['librarynot'] = '客户已取消!';
  335. }
  336. if($y['print'] != 3 || $y['libraryconfirm'] == 1 || $y['state'] == 214 || $y['state'] == 217 || $y['state'] == 283 || $y['state'] == 206 || $y['state'] == 205 || $y['state'] == 204 || $y['state'] == 210 || $y['state'] == 211 || $y['state'] == 212 || ($y['state'] == 216 && $fullorder_name == 'fullordersmt'))//独立站216先关了
  337. {
  338. return array('msg'=>'禁止出库!'.$y['librarynot'],'success'=>false);
  339. //echo json_encode(array('msg'=>'禁止出库!'.$y['librarynot'],'success'=>false));exit;
  340. }
  341. if($y['library'] > 1)
  342. {
  343. return array('msg'=>'此数据 '.date('Y-m-d H:i:s',$y['librarytime']).' 已操作出库','success'=>false);
  344. // echo json_encode(array('msg'=>'此数据 '.date('Y-m-d H:i:s',$y['librarytime']).' 已操作','success'=>false));exit;
  345. }
  346. if($express['out'] != 3)
  347. {
  348. if($express['out'] != $outtype)
  349. {
  350. $smlx = ($express['out']=='1')?'运单':'发货单';
  351. return array('msg'=>'扫描类型错误!此单需要扫描'.$smlx,'success'=>false);
  352. //echo json_encode(array('msg'=>'扫描类型错误!此单需要扫描'.$smlx,'success'=>false));exit;
  353. }
  354. }
  355. if($y['type'] != 7 && $y['waybill'] != '' && $y['printtype'] == '1')
  356. {
  357. $r = $this->logic_express->ydCheckById($y['waybill'],$y['express']);
  358. if(!$r){
  359. return array('msg'=>'出库物流和系统不匹配,请联系梦体解决'.$y['express'],'success'=>false);
  360. //echo json_encode(array('msg'=>'出库物流和系统不匹配,请联系梦体解决'.$y['express'],'success'=>false));exit;
  361. }
  362. }
  363. $express_info = $this->express->read($y['express']);
  364. if(empty($express_info)){
  365. return array('msg'=>'快递信息异常,请联系技术!','success'=>false);
  366. }
  367. if(((time() - $y['printtime']) > (15*24*3600) || $y['printtime'] < 1590211800) && $y['express'] == '24' && $y['type'] != 6)//杭州店铺除外
  368. {
  369. //echo json_encode(array('yd'=>1,'id'=>$y['id'],'express'=>$y['express'],'name'=>$fullorder_name,'success'=>true));exit;
  370. }
  371. if((time() - $y['printtime']) > (6*24*3600) && $y['express'] == '3' && $y['type'] != 6)//杭州店铺除外
  372. {
  373. //echo json_encode(array('yd'=>1,'id'=>$y['id'],'express'=>$y['express'],'name'=>$fullorder_name,'success'=>true));exit;
  374. return array('msg'=>'此运单过期无法出库,请联系成会处理!','success'=>false);
  375. }
  376. if((time() - $y['printtime']) > (7*24*3600) && $y['express'] == '37' && $y['type'] != 6)//杭州店铺除外
  377. {
  378. //echo json_encode(array('yd'=>1,'id'=>$y['id'],'express'=>$y['express'],'name'=>$fullorder_name,'success'=>true));exit;
  379. }
  380. $this->db->trans_begin();
  381. $warehouse = $this->warehouse->read($y['type']);
  382. $bdck = $warehouse['bdck'];
  383. if($warehouse['zd'] == '1')
  384. {
  385. if($warehouse['bdck'] == '')
  386. {
  387. $this->db->trans_rollback();
  388. return array('msg'=>'仓库设置错误!','success'=>false);
  389. }
  390. $wh = $this->_fout_db($y,$warehouse,$bdck);
  391. if($wh['fh'] == 2)
  392. {
  393. $kcyz = $this->ck->get_kc($y['state'],$warehouse,$y['shop'],$y['number'],$y['whlabel'],$y['fpdata']);
  394. if($kcyz['t'] == '0')
  395. {
  396. if($kcyz['fpdata'] != '')
  397. {
  398. $x['whlabel'] = $kcyz['whlabel'];
  399. $x['fpdata'] = $kcyz['fpdata'];
  400. $y['whlabel'] = $x['whlabel'];
  401. $this->$fullorder_name->save($x,$y['id']);
  402. }
  403. }
  404. $wh = $this->_fout_db($y,$warehouse,$bdck);
  405. if($wh['fh'] == 2)
  406. {
  407. $this->db->trans_rollback();
  408. return array('msg'=>'库存占用量与实际出库量不符!','success'=>false);
  409. }
  410. }
  411. foreach ($wh['sj'] as $v)
  412. {
  413. $this->$bdck->save(array('state'=>1,'cktype'=>1,'orderinfo'=>$y['orderinfo'],'waybill'=>$y['waybill'],'outk'=>$time,'time'=>$time),$v['id']);
  414. }
  415. }
  416. $shop = $this->shop->read($y['shop']);
  417. if($express['printcode'] == "DHLUSA" && $y['waybill'] != '')
  418. {
  419. $type = $this->typeclass->read($shop['type']);
  420. $y['shoptype'] = $type['title'];
  421. $d = $this->dhl->get_data_9610($y);
  422. if($d != 1)
  423. {
  424. $this->db->trans_rollback();
  425. return array('msg'=>'9610失败,'.$d,'success'=>false);
  426. }
  427. }
  428. if($fullorder_name == 'fullordersmt')
  429. {
  430. $stt = ($y['waybill']!='')?209:$y['state'];
  431. $post = array('library'=>2,'state'=>$stt,'librarytime'=>$time,'dbapi'=>0);
  432. }
  433. else if(($fullorder_name == 'fullordertt' || $fullorder_name == 'fullorderxw' || $fullorder_name == 'fullorder' || $fullorder_name = 'fullorderamz') && $y['source'] == 1)
  434. {
  435. $post = array('library'=>2,'state'=>216,'librarytime'=>$time,'dbapi'=>0);
  436. }
  437. else
  438. {
  439. $post = array('library'=>2,'librarytime'=>$time,'dbapi'=>0);
  440. }
  441. if($outtype == 2 && $y['printnumber'] > 1)
  442. {
  443. $p = json_encode($post);
  444. $this->db->trans_rollback();
  445. return array('msg'=>'该单共打印过'.$y['printnumber'].'次,请注意核对当前发货单是否是最新发货单!','outnumber'=>$y['id'],'post'=>$p,'fullorder_name'=>$fullorder_name,'success'=>false);
  446. }
  447. $this->db->trans_rollback();
  448. return array('msg'=>'此单允许出库 !!!','success'=>true);
  449. //$this->$fullorder_name->save($post,$y['id']);
  450. }
  451. private function _fout_db($y,$warehouse,$bdck)//对比占单是否正确
  452. {
  453. $wh = $this->$bdck->find_all("zd = '".$y['number']."' and warehouse = '".$y['type']."' and (state = '0' or state = '9')");
  454. $cpsl = 0;
  455. $pp = explode('|',trim($y['whlabel'],'|'));
  456. foreach ($pp as $va)
  457. {
  458. $num = explode('-',$va);
  459. if(isset($num[2]))
  460. {
  461. if(stripos($num[2],$warehouse['hz']) !== false)
  462. {
  463. $cpsl += $num[1];
  464. }
  465. }
  466. }
  467. if(count($wh) == $cpsl)
  468. {
  469. return array('fh'=>1,'sj'=>$wh);
  470. }
  471. else
  472. {
  473. return array('fh'=>2);
  474. }
  475. }
  476. }