Model_ck.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Model_ck extends Lin_Model
  3. {
  4. function __construct(){
  5. parent::__construct();
  6. $this->load_table('setting');
  7. $this->load->_model("Model_logic_ding","logic_ding");
  8. }
  9. public function get_kcyz($state,$warehouse,$shop,$number,$whlabel,$fpdata)//库存验证
  10. {
  11. $save = array();$x=0;
  12. $pp = explode('|',trim($number,'|'));
  13. if($warehouse['zd'] == 1 && $warehouse['bdck'] != '')
  14. {
  15. $n = 'get_'.$warehouse['bdck'];
  16. $y = $this->$n($state,$warehouse,$shop,$number,$whlabel,$fpdata,$yz=1);
  17. return $y;
  18. }
  19. }
  20. public function get_kc($state,$warehouse,$shop,$number,$whlabel,$fpdata)//库存操作开始
  21. {
  22. if(!preg_match('/[0-9]/',$whlabel))
  23. {
  24. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'请先添加产品信息!','success'=>false)));exit;
  25. }
  26. $wl = $this->specialstock->find_all("zd = '$number'");
  27. if($wl)
  28. {
  29. foreach ($wl as $v)
  30. {
  31. $this->specialstock->save(array('zd'=>''),$v['id']);//专属
  32. }
  33. }
  34. //非待发货和客户待收货状态以及仓库不占单
  35. if(($state != '207' && $state != '209') || $warehouse['zd'] == 0)//待发货状态
  36. {
  37. $wlw = $this->whlabel->find_all("zd = '$number'");
  38. if($wlw)
  39. {
  40. foreach ($wlw as $v)
  41. {
  42. $this->whlabel->save(array('zd'=>''),$v['id']);//专属
  43. }
  44. }
  45. }
  46. $fhw = '|';$j = 0;
  47. $fd = explode(';',trim($fpdata,';'));
  48. $wcfpd = '';
  49. $pp = explode('|',trim($whlabel,'|'));
  50. if($warehouse['bdck'] == 'specialstock' && ($state == '207' || $state == '209'))//特殊仓含特殊产品的不处理whlabel和fpdata 但需要状态符合
  51. {
  52. foreach ($pp as $v)
  53. {
  54. if(stripos($v,$warehouse['hz']) === false)
  55. {
  56. $num = explode('-',$v);
  57. $fhw .= $num[0].'-'.$num[1].'-0|';
  58. $cffpt = explode('|',trim($fd[$j],'|'));
  59. $cffpt[9] = 0;
  60. $wcfpd .= implode("|",$cffpt).';';
  61. }
  62. else
  63. {
  64. $fhw .= $v.'|';
  65. $wcfpd .= $fd[$j].';';
  66. }
  67. $j++;
  68. }
  69. }
  70. else
  71. {
  72. foreach ($pp as $v)
  73. {
  74. $num = explode('-',$v);
  75. if(isset($num[2]) && $num[2] == 'DNOTO')//选择不占单的
  76. {
  77. $fhw .= $v.'|';
  78. $wcfpd .= $fd[$j].';';
  79. }
  80. else
  81. {
  82. $fhw .= $num[0].'-'.$num[1].'-0|';
  83. $cffpt = explode('|',trim($fd[$j],'|'));
  84. $cffpt[9] = 0;
  85. $wcfpd .= implode("|",$cffpt).';';
  86. }
  87. $j++;
  88. }
  89. }
  90. $whlabel = $fhw;
  91. $fpdata = $wcfpd;
  92. if($warehouse['zd'] == 1 && $warehouse['bdck'] != '')
  93. {
  94. $n = 'get_'.$warehouse['bdck'];
  95. $z = $this->$n($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  96. return $z;exit;
  97. }
  98. else
  99. {
  100. return array('t'=>0,'whlabel'=>$whlabel,'fpdata'=>$fpdata);exit;
  101. }
  102. }
  103. public function get_whlabel($state,$warehouse,$shop,$number,$whlabel,$fpdata,$yz=0)
  104. {
  105. //smt:edit-bcbz-readonly-customersmt_medit,dlz:edit-readonly-customer_medit
  106. if(!$whlabel || $whlabel == '|')
  107. {
  108. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'请先添加产品信息!','success'=>false)));exit;
  109. }
  110. //先预先判断开始,否则清除数据后造成库存错误
  111. if($state == '207' || $state == '209')//待发货状态
  112. {
  113. $pp = explode('|',trim($whlabel,'|'));
  114. $cxsl = array();//产品数组化提供下面查询
  115. foreach ($pp as $key=>$va)
  116. {
  117. $w = explode('-',$va);
  118. if(!isset($w[2]))
  119. {
  120. $w[2] = 0;
  121. }
  122. $cxsl[$key] = $w;
  123. }
  124. $x = 0;$save = array();
  125. foreach ($pp as $key=>$va)
  126. {
  127. $num = explode('-',$va);
  128. if(!isset($num[2]) || isset($num[2]) && $num[2] != "DNOTO")
  129. {
  130. foreach ($cxsl as $k=>$v)
  131. {
  132. if($v[0] == $num[0] && $num[2] != "DNOTO" && $key != $k)
  133. {
  134. $num[1] += $v[1];//如果有相同产品并且未禁止占单,那么合并占单数量
  135. }
  136. }
  137. }
  138. $wh = $this->whlabel->find_count("state = '0' and number = '$num[0]' and warehouse = '".$warehouse['id']."' and (zd = '$number' or zd = '') and (shop IS NULL or shop = '')");//通用
  139. $shopwh = $this->whlabel->find_count("state = '0' and number = '$num[0]' and warehouse = '".$warehouse['id']."' and (zd = '$number' or zd = '') and shop like '%,".$shop.",%'");//专属
  140. $x++;
  141. if($wh+$shopwh < $num[1] || (isset($num[2]) && $num[2] == "DNOTO" && $warehouse['zdms'] == 2))
  142. {
  143. $this->logic_ding->sendToDing("库存验证数据【".$number."】".json_encode($num)."库存数量:".($wh+$shopwh)."仓库的占单模式".$warehouse['zdms']);
  144. $save[] = $x-1;continue;//提前先加X不然出错,所以要减1
  145. }
  146. }
  147. if(isset($save[0]) && $warehouse['zdms'] == 2)
  148. {
  149. return array('t'=>1,'m'=>json_encode(array('t'=>2,'msg'=>"红色商品名库存不够扣减或不允许占单",'error'=>$save,'hq'=>"state = '0' and number = '$num[0]' and warehouse = '".$warehouse['id']."' and (zd = '$number' or zd = '') and (shop IS NULL or shop = '')",'success'=>false)));exit;
  150. }
  151. if($yz == 0)
  152. {
  153. $z = $this->get_whlabel_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  154. return $z;
  155. }
  156. }
  157. else//订单状态不符合
  158. {
  159. return array('t'=>0,'whlabel'=>$whlabel,'fpdata'=>$fpdata);exit;
  160. }
  161. }
  162. public function get_whlabel_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata)
  163. {
  164. $fhw = '|';
  165. $fpdata = explode(';',trim($fpdata,';'));
  166. $wcfpd = '';
  167. $wlw = $this->whlabel->find_all("zd = '".$number."'");
  168. if($state == '207' || $state == '209')
  169. {
  170. $x = 0;
  171. $pp = explode('|',trim($whlabel,'|'));
  172. $wzdid = '';$szdid = '';$wfcid = array();
  173. foreach ($pp as $va)
  174. {
  175. $fhid = '';$DNOTO = 0;
  176. $num = explode('-',$va);
  177. if(isset($num[2]) && $num[2] == "DNOTO")
  178. {
  179. $DNOTO = 1;
  180. }
  181. $whlabel = $this->whlabel->find_all("state = '0' and number = '$num[0]' and warehouse = '".$warehouse['id']."' and (zd = '".$number."' or zd = '') and (shop IS NULL or shop = '')");//通用
  182. $shopwhlabel = $this->whlabel->find_all("state = '0' and number = '$num[0]' and warehouse = '".$warehouse['id']."' and (zd = '".$number."' or zd = '') and shop like '%,".$shop.",%'");//专属
  183. if(count($shopwhlabel) > 0 && $DNOTO < 1)//如果有专属库存
  184. {
  185. if(count($shopwhlabel) >= $num[1])
  186. {
  187. for($i=0;$i<$num[1];$i++)
  188. {
  189. $fhid .= $shopwhlabel[$i]['id'].$warehouse['hz'].'~';
  190. //$this->whlabel->save(array('zd'=>$number),$shopwhlabel[$i]['id']);//专属 ~符号这里改验货也需要改
  191. $wzdid .= 'id != '.$shopwhlabel[$i]['id'].' and ';
  192. $wfcid[] = $shopwhlabel[$i]['id'];
  193. }
  194. }
  195. else
  196. {
  197. for($i=0;$i<count($shopwhlabel);$i++)
  198. {
  199. $fhid .= $shopwhlabel[$i]['id'].$warehouse['hz'].'~';
  200. //$this->whlabel->save(array('zd'=>$number),$shopwhlabel[$i]['id']);//扣减专属
  201. $wzdid .= 'id != '.$shopwhlabel[$i]['id'].' and ';
  202. $wfcid[] = $shopwhlabel[$i]['id'];
  203. }
  204. for($i=0;$i<$num[1]-count($shopwhlabel);$i++)
  205. {
  206. if(isset($whlabel[$i]))
  207. {
  208. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  209. //$this->whlabel->save(array('zd'=>$number),$whlabel[$i]['id']);//扣减通用
  210. $wzdid .= 'id != '.$whlabel[$i]['id'].' and ';
  211. $wfcid[] = $whlabel[$i]['id'];
  212. }
  213. }
  214. }
  215. $fhw .= $num[0].'-'.$num[1].'-'.rtrim($fhid,'~')."|";
  216. $cffpt = explode('|',trim($fpdata[$x],'|'));
  217. $cffpt[9] = rtrim($fhid,'~');
  218. $wcfpd .= implode("|",$cffpt).';';
  219. }
  220. else if(count($whlabel) >= $num[1] && $DNOTO < 1)
  221. {
  222. for($i=0;$i<$num[1];$i++)
  223. {
  224. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  225. //$this->whlabel->save(array('zd'=>$number),$whlabel[$i]['id']);//通用
  226. $wzdid .= 'id != '.$whlabel[$i]['id'].' and ';
  227. $wfcid[] = $whlabel[$i]['id'];
  228. }
  229. $fhw .= $num[0].'-'.$num[1].'-'.rtrim($fhid,'~')."|";
  230. $cffpt = explode('|',trim($fpdata[$x],'|'));
  231. $cffpt[9] = rtrim($fhid,'~');
  232. $wcfpd .= implode("|",$cffpt).';';
  233. }
  234. else
  235. {
  236. $fhw .= $va."|";
  237. $wcfpd .= $fpdata[$x].';';
  238. }
  239. $x++;
  240. }
  241. }
  242. $this->db->trans_begin();
  243. if(isset($wlw[0]))
  244. {
  245. foreach ($wlw as $v)
  246. {
  247. $this->whlabel->save(array('zd'=>''),$v['id']);//专属
  248. }
  249. }
  250. if($wzdid != '')
  251. {
  252. foreach ($wfcid as $v)
  253. {
  254. $this->whlabel->save(array('zd'=>$number),$v);
  255. }
  256. }
  257. if ($this->db->trans_status() === FALSE)
  258. {
  259. $this->db->trans_rollback();
  260. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','success'=>false)));
  261. }
  262. else
  263. {
  264. $this->db->trans_commit();
  265. return array('t'=>0,'whlabel'=>$fhw,'fpdata'=>$wcfpd);
  266. }
  267. }
  268. public function get_specialstock($state,$warehouse,$shop,$number,$whlabel,$fpdata,$yz=0)
  269. {
  270. if(!$whlabel || $whlabel == '|' || stripos($whlabel,$warehouse['hz']) === false)
  271. {
  272. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'请先添加特殊产品信息!','success'=>false)));exit;
  273. }
  274. //先预先判断开始,否则清除数据后造成库存错误
  275. if($state == '207' || $state == '209')//待发货状态
  276. {
  277. $pp = explode('|',trim($whlabel,'|'));
  278. $x = 0;$save = array();
  279. foreach ($pp as $va)
  280. {
  281. $x++;
  282. $num = explode('-',$va);
  283. if(stripos($va,$warehouse['hz']) !== false)
  284. {
  285. $wh = $this->specialstock->find_count("state = '0' and number = '$num[0]' and (zd = '$number' or zd = '')");//通用
  286. if($wh < $num[1])
  287. {
  288. $save[] = $x-1;continue;//提前先加X不然出错,所以要减1
  289. }
  290. }
  291. }
  292. if(isset($save[0]))
  293. {
  294. return array('t'=>1,'m'=>json_encode(array('t'=>2,'msg'=>"红色商品名库存不够扣减或不允许占单",'error'=>$save,'success'=>false)));exit;
  295. }
  296. if($yz == 0)
  297. {
  298. $z = $this->get_specialstock_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  299. return $z;
  300. }
  301. }
  302. else//订单状态不符合
  303. {
  304. return array('t'=>0,'whlabel'=>$whlabel,'fpdata'=>$fpdata);exit;
  305. }
  306. }
  307. public function get_specialstock_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata)
  308. {
  309. $fpdata = explode(';',trim($fpdata,';'));
  310. $wcfpd = '';
  311. $this->db->trans_begin();
  312. $wl = $this->whlabel->find_all("zd = '".$number."'");
  313. if($wl)
  314. {
  315. foreach ($wl as $v)
  316. {
  317. $this->whlabel->save(array('zd'=>''),$v['id']);//专属
  318. }
  319. }
  320. if($state == '207' || $state == '209')
  321. {
  322. $fhw = array();$x = 0;$wzdid = '';
  323. $pp = explode('|',trim($whlabel,'|'));
  324. foreach ($pp as $va)
  325. {
  326. $num = explode('-',$va);
  327. if(stripos($va,$warehouse['hz']) !== false)
  328. {
  329. $fhid = '';
  330. $num = explode('-',$va);
  331. $whlabel = $this->specialstock->find_all("state = '0' and number = '$num[0]' and zd = ''");//通用
  332. $fgzid = explode('~',trim($num[2],'~'));
  333. foreach ($fgzid as $vid)
  334. {
  335. $xzkc = $this->specialstock->read(rtrim($vid,$warehouse['hz']));//选择的这个库存
  336. if($xzkc['zd'] != '')
  337. {
  338. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'所选库存已被占单!请重新打开选择页面','success'=>false)));exit;
  339. }
  340. }
  341. for($i=0;$i<$num[1];$i++)
  342. {
  343. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  344. $this->specialstock->save(array('zd'=>$number,'zdlist'=>date('Y-m-d H:i:s',time())),$whlabel[$i]['id']);//通用
  345. $wzdid .= 'id != '.$whlabel[$i]['id'].' and ';
  346. }
  347. $fhw[] = $num[0].'-'.$num[1].'-'.rtrim($fhid,'~');
  348. $cffpt = explode('|',trim($fpdata[$x],'|'));
  349. $cffpt[9] = rtrim($fhid,'~');
  350. $wcfpd .= implode("|",$cffpt).';';
  351. }
  352. else
  353. {
  354. $fhw[] = $va;
  355. $wcfpd .= $fpdata[$x].';';
  356. }
  357. $x++;
  358. }
  359. $fhw = '|'.implode("|",$fhw).'|';
  360. }
  361. if ($this->db->trans_status() === TRUE)
  362. {
  363. $this->db->trans_commit();
  364. return array('t'=>0,'whlabel'=>$fhw,'fpdata'=>$wcfpd);exit;
  365. }
  366. else
  367. {
  368. $this->db->trans_rollback();
  369. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','success'=>false)));exit;
  370. }
  371. }
  372. //预先判断仓开始
  373. public function get_yx_whlabel($state,$warehouse,$shop,$number,$whlabel,$fpdata)//通用仓
  374. {
  375. $z = $this->get_whlabel($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  376. return $z;
  377. }
  378. public function get_yx_specialstock($state,$warehouse,$shop,$number,$whlabel,$fpdata)//特殊仓
  379. {
  380. $pp = explode('|',trim($whlabel,'|'));
  381. $fpdata = explode(';',trim($fpdata,';'));
  382. $wcfpd = '';$fhw = array();
  383. $x = 0;$save = array();
  384. $kytskc = array();
  385. foreach ($pp as $k=>$va)
  386. {
  387. $num = explode('-',$va);
  388. $wh = $this->specialstock->find_count("state = '0' and number = '$num[0]' and zd = ''");
  389. if(!isset($num[1]))
  390. {
  391. $num[1] = 1;
  392. }
  393. if($wh >= $num[1])
  394. {
  395. $kytskc[] = $num[0].'-'.$num[1].'-z';//有可占用的
  396. }
  397. else
  398. {
  399. $kytskc[] = $num[0].'-'.$num[1].'-0';
  400. }
  401. }
  402. $y = 0;
  403. $this->db->trans_begin();
  404. foreach ($kytskc as $va)
  405. {
  406. $num = explode('-',$va);
  407. if(stripos($va,$warehouse['hz']) !== false)
  408. {
  409. $whlabel = $this->specialstock->find_all("state = '0' and number = '$num[0]' and zd = ''");//通用
  410. $fhid = '';
  411. for($i=0;$i<$num[1];$i++)
  412. {
  413. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  414. $this->specialstock->save(array('zd'=>$number,'zdlist'=>date('Y-m-d H:i:s',time())),$whlabel[$i]['id']);//通用
  415. }
  416. $fhw[] = $num[0].'-'.$num[1].'-'.rtrim($fhid,'~');
  417. $cffpt = explode('|',trim($fpdata[$x],'|'));
  418. $cffpt[9] = rtrim($fhid,'~');
  419. $wcfpd .= implode("|",$cffpt).';';
  420. $y++;
  421. }
  422. else
  423. {
  424. $fhw[] = $num[0].'-'.$num[1].'-0';
  425. $wcfpd .= $fpdata[$x].';';
  426. }
  427. $x++;
  428. }
  429. if($this->db->trans_status() === TRUE)
  430. {
  431. $this->db->trans_commit();
  432. return array('t'=>0,'whlabel'=>"|".implode("|",$fhw)."|",'fpdata'=>$wcfpd);exit;
  433. }
  434. else
  435. {
  436. $this->db->trans_rollback();
  437. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','success'=>false)));exit;
  438. }
  439. }
  440. //预先判断仓结束
  441. public function get_fl($hz,$whlabel,$fpdata)//特殊仓
  442. {
  443. $sfxh = 0;
  444. if($hz != '')
  445. {
  446. $num = explode('|',trim($whlabel,'|'));
  447. $bh = substr_count($whlabel,$hz);
  448. if($bh == count($num))
  449. {
  450. $sfxh = 1;//有现货
  451. }
  452. }
  453. $f = explode(';',trim($fpdata,';'));
  454. //$fl = substr_count($fpdata,'Bob');//暂时不用这个,防止产品出现大小写不一致
  455. $i = 0;
  456. foreach ($f as $v)
  457. {
  458. if(stripos($fpdata,'bob') !== false && (stripos($v,'bob') !== false || stripos($v,'wig') === false))
  459. {
  460. $i++;
  461. }
  462. }
  463. if(count($f) == $i)
  464. {
  465. $type = $this->typeclass->get_type('37','BOBTYPE');
  466. }
  467. else if(stripos($fpdata,'wig') !== false)
  468. {
  469. $type = $this->typeclass->get_type('37','WIGTYPE');
  470. }
  471. else
  472. {
  473. $type = $this->typeclass->get_type('37','OTHERTYPE');
  474. }
  475. $ckfl = $type['id'];
  476. return array('sfxh'=>$sfxh,'ckfl'=>$ckfl);
  477. }
  478. } //end class