Model_ck.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. //$this->logic_ding->sendToDing("2库存验证数据【".$number."】".json_encode($num)."库存数量:".($wh+$shopwh)."仓库的占单模式".$warehouse['zdms']."$".json_encode($save));
  148. if(isset($save[0]) && $warehouse['zdms'] == 2)
  149. {
  150. 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;
  151. }
  152. if($yz == 0)
  153. {
  154. $z = $this->get_whlabel_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  155. return $z;
  156. }
  157. }
  158. else//订单状态不符合
  159. {
  160. return array('t'=>0,'whlabel'=>$whlabel,'fpdata'=>$fpdata);exit;
  161. }
  162. }
  163. public function get_whlabel_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata)
  164. {
  165. $fhw = '|';
  166. $fpdata = explode(';',trim($fpdata,';'));
  167. $wcfpd = '';
  168. $wlw = $this->whlabel->find_all("zd = '".$number."'");
  169. if($state == '207' || $state == '209')
  170. {
  171. $x = 0;
  172. $pp = explode('|',trim($whlabel,'|'));
  173. $wzdid = '';$szdid = '';$wfcid = array();
  174. foreach ($pp as $va)
  175. {
  176. $fhid = '';$DNOTO = 0;
  177. $num = explode('-',$va);
  178. if(isset($num[2]) && $num[2] == "DNOTO")
  179. {
  180. $DNOTO = 1;
  181. }
  182. $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 = '')");//通用
  183. $shopwhlabel = $this->whlabel->find_all("state = '0' and number = '$num[0]' and warehouse = '".$warehouse['id']."' and (zd = '".$number."' or zd = '') and shop like '%,".$shop.",%'");//专属
  184. if(count($shopwhlabel) > 0 && $DNOTO < 1)//如果有专属库存
  185. {
  186. if(count($shopwhlabel) >= $num[1])
  187. {
  188. for($i=0;$i<$num[1];$i++)
  189. {
  190. $fhid .= $shopwhlabel[$i]['id'].$warehouse['hz'].'~';
  191. //$this->whlabel->save(array('zd'=>$number),$shopwhlabel[$i]['id']);//专属 ~符号这里改验货也需要改
  192. $wzdid .= 'id != '.$shopwhlabel[$i]['id'].' and ';
  193. $wfcid[] = $shopwhlabel[$i]['id'];
  194. }
  195. }
  196. else
  197. {
  198. for($i=0;$i<count($shopwhlabel);$i++)
  199. {
  200. $fhid .= $shopwhlabel[$i]['id'].$warehouse['hz'].'~';
  201. //$this->whlabel->save(array('zd'=>$number),$shopwhlabel[$i]['id']);//扣减专属
  202. $wzdid .= 'id != '.$shopwhlabel[$i]['id'].' and ';
  203. $wfcid[] = $shopwhlabel[$i]['id'];
  204. }
  205. for($i=0;$i<$num[1]-count($shopwhlabel);$i++)
  206. {
  207. if(isset($whlabel[$i]))
  208. {
  209. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  210. //$this->whlabel->save(array('zd'=>$number),$whlabel[$i]['id']);//扣减通用
  211. $wzdid .= 'id != '.$whlabel[$i]['id'].' and ';
  212. $wfcid[] = $whlabel[$i]['id'];
  213. }
  214. }
  215. }
  216. $fhw .= $num[0].'-'.$num[1].'-'.rtrim($fhid,'~')."|";
  217. $cffpt = explode('|',trim($fpdata[$x],'|'));
  218. $cffpt[9] = rtrim($fhid,'~');
  219. $wcfpd .= implode("|",$cffpt).';';
  220. }
  221. else if(count($whlabel) >= $num[1] && $DNOTO < 1)
  222. {
  223. for($i=0;$i<$num[1];$i++)
  224. {
  225. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  226. //$this->whlabel->save(array('zd'=>$number),$whlabel[$i]['id']);//通用
  227. $wzdid .= 'id != '.$whlabel[$i]['id'].' and ';
  228. $wfcid[] = $whlabel[$i]['id'];
  229. }
  230. $fhw .= $num[0].'-'.$num[1].'-'.rtrim($fhid,'~')."|";
  231. $cffpt = explode('|',trim($fpdata[$x],'|'));
  232. $cffpt[9] = rtrim($fhid,'~');
  233. $wcfpd .= implode("|",$cffpt).';';
  234. }
  235. else
  236. {
  237. $fhw .= $va."|";
  238. $wcfpd .= $fpdata[$x].';';
  239. }
  240. $x++;
  241. }
  242. }
  243. $this->db->trans_begin();
  244. if(isset($wlw[0]))
  245. {
  246. foreach ($wlw as $v)
  247. {
  248. $this->whlabel->save(array('zd'=>''),$v['id']);//专属
  249. }
  250. }
  251. if($wzdid != '')
  252. {
  253. foreach ($wfcid as $v)
  254. {
  255. $this->whlabel->save(array('zd'=>$number),$v);
  256. }
  257. }
  258. if ($this->db->trans_status() === FALSE)
  259. {
  260. $this->db->trans_rollback();
  261. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','success'=>false)));
  262. }
  263. else
  264. {
  265. $this->db->trans_commit();
  266. return array('t'=>0,'whlabel'=>$fhw,'fpdata'=>$wcfpd);
  267. }
  268. }
  269. public function get_specialstock($state,$warehouse,$shop,$number,$whlabel,$fpdata,$yz=0)
  270. {
  271. if(!$whlabel || $whlabel == '|' || stripos($whlabel,$warehouse['hz']) === false)
  272. {
  273. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'请先添加特殊产品信息!','success'=>false)));exit;
  274. }
  275. //先预先判断开始,否则清除数据后造成库存错误
  276. if($state == '207' || $state == '209')//待发货状态
  277. {
  278. $pp = explode('|',trim($whlabel,'|'));
  279. $x = 0;$save = array();
  280. foreach ($pp as $va)
  281. {
  282. $x++;
  283. $num = explode('-',$va);
  284. if(stripos($va,$warehouse['hz']) !== false)
  285. {
  286. $wh = $this->specialstock->find_count("state = '0' and number = '$num[0]' and (zd = '$number' or zd = '')");//通用
  287. if($wh < $num[1])
  288. {
  289. $save[] = $x-1;continue;//提前先加X不然出错,所以要减1
  290. }
  291. }
  292. }
  293. if(isset($save[0]))
  294. {
  295. return array('t'=>1,'m'=>json_encode(array('t'=>2,'msg'=>"红色商品名库存不够扣减或不允许占单",'error'=>$save,'success'=>false)));exit;
  296. }
  297. if($yz == 0)
  298. {
  299. $z = $this->get_specialstock_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  300. return $z;
  301. }
  302. }
  303. else//订单状态不符合
  304. {
  305. return array('t'=>0,'whlabel'=>$whlabel,'fpdata'=>$fpdata);exit;
  306. }
  307. }
  308. public function get_specialstock_zd($state,$warehouse,$shop,$number,$whlabel,$fpdata)
  309. {
  310. $fpdata = explode(';',trim($fpdata,';'));
  311. $wcfpd = '';
  312. $this->db->trans_begin();
  313. $wl = $this->whlabel->find_all("zd = '".$number."'");
  314. if($wl)
  315. {
  316. foreach ($wl as $v)
  317. {
  318. $this->whlabel->save(array('zd'=>''),$v['id']);//专属
  319. }
  320. }
  321. if($state == '207' || $state == '209')
  322. {
  323. $fhw = array();$x = 0;$wzdid = '';
  324. $pp = explode('|',trim($whlabel,'|'));
  325. foreach ($pp as $va)
  326. {
  327. $num = explode('-',$va);
  328. if(stripos($va,$warehouse['hz']) !== false)
  329. {
  330. $fhid = '';
  331. $num = explode('-',$va);
  332. $whlabel = $this->specialstock->find_all("state = '0' and number = '$num[0]' and zd = ''");//通用
  333. $fgzid = explode('~',trim($num[2],'~'));
  334. foreach ($fgzid as $vid)
  335. {
  336. $xzkc = $this->specialstock->read(rtrim($vid,$warehouse['hz']));//选择的这个库存
  337. if($xzkc['zd'] != '')
  338. {
  339. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'所选库存已被占单!请重新打开选择页面','success'=>false)));exit;
  340. }
  341. }
  342. for($i=0;$i<$num[1];$i++)
  343. {
  344. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  345. $this->specialstock->save(array('zd'=>$number,'zdlist'=>date('Y-m-d H:i:s',time())),$whlabel[$i]['id']);//通用
  346. $wzdid .= 'id != '.$whlabel[$i]['id'].' and ';
  347. }
  348. $fhw[] = $num[0].'-'.$num[1].'-'.rtrim($fhid,'~');
  349. $cffpt = explode('|',trim($fpdata[$x],'|'));
  350. $cffpt[9] = rtrim($fhid,'~');
  351. $wcfpd .= implode("|",$cffpt).';';
  352. }
  353. else
  354. {
  355. $fhw[] = $va;
  356. $wcfpd .= $fpdata[$x].';';
  357. }
  358. $x++;
  359. }
  360. $fhw = '|'.implode("|",$fhw).'|';
  361. }
  362. if ($this->db->trans_status() === TRUE)
  363. {
  364. $this->db->trans_commit();
  365. return array('t'=>0,'whlabel'=>$fhw,'fpdata'=>$wcfpd);exit;
  366. }
  367. else
  368. {
  369. $this->db->trans_rollback();
  370. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','success'=>false)));exit;
  371. }
  372. }
  373. //预先判断仓开始
  374. public function get_yx_whlabel($state,$warehouse,$shop,$number,$whlabel,$fpdata)//通用仓
  375. {
  376. $z = $this->get_whlabel($state,$warehouse,$shop,$number,$whlabel,$fpdata);
  377. return $z;
  378. }
  379. public function get_yx_specialstock($state,$warehouse,$shop,$number,$whlabel,$fpdata)//特殊仓
  380. {
  381. $pp = explode('|',trim($whlabel,'|'));
  382. $fpdata = explode(';',trim($fpdata,';'));
  383. $wcfpd = '';$fhw = array();
  384. $x = 0;$save = array();
  385. $kytskc = array();
  386. foreach ($pp as $k=>$va)
  387. {
  388. $num = explode('-',$va);
  389. $wh = $this->specialstock->find_count("state = '0' and number = '$num[0]' and zd = ''");
  390. if(!isset($num[1]))
  391. {
  392. $num[1] = 1;
  393. }
  394. if($wh >= $num[1])
  395. {
  396. $kytskc[] = $num[0].'-'.$num[1].'-z';//有可占用的
  397. }
  398. else
  399. {
  400. $kytskc[] = $num[0].'-'.$num[1].'-0';
  401. }
  402. }
  403. $y = 0;
  404. $this->db->trans_begin();
  405. foreach ($kytskc as $va)
  406. {
  407. $num = explode('-',$va);
  408. if(stripos($va,$warehouse['hz']) !== false)
  409. {
  410. $whlabel = $this->specialstock->find_all("state = '0' and number = '$num[0]' and zd = ''");//通用
  411. $fhid = '';
  412. for($i=0;$i<$num[1];$i++)
  413. {
  414. $fhid .= $whlabel[$i]['id'].$warehouse['hz'].'~';
  415. $this->specialstock->save(array('zd'=>$number,'zdlist'=>date('Y-m-d H:i:s',time())),$whlabel[$i]['id']);//通用
  416. }
  417. $fhw[] = $num[0].'-'.$num[1].'-'.rtrim($fhid,'~');
  418. $cffpt = explode('|',trim($fpdata[$x],'|'));
  419. $cffpt[9] = rtrim($fhid,'~');
  420. $wcfpd .= implode("|",$cffpt).';';
  421. $y++;
  422. }
  423. else
  424. {
  425. $fhw[] = $num[0].'-'.$num[1].'-0';
  426. $wcfpd .= $fpdata[$x].';';
  427. }
  428. $x++;
  429. }
  430. if($this->db->trans_status() === TRUE)
  431. {
  432. $this->db->trans_commit();
  433. return array('t'=>0,'whlabel'=>"|".implode("|",$fhw)."|",'fpdata'=>$wcfpd);exit;
  434. }
  435. else
  436. {
  437. $this->db->trans_rollback();
  438. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','success'=>false)));exit;
  439. }
  440. }
  441. //预先判断仓结束
  442. public function get_fl($hz,$whlabel,$fpdata)//特殊仓
  443. {
  444. $sfxh = 0;
  445. if($hz != '')
  446. {
  447. $num = explode('|',trim($whlabel,'|'));
  448. $bh = substr_count($whlabel,$hz);
  449. if($bh == count($num))
  450. {
  451. $sfxh = 1;//有现货
  452. }
  453. }
  454. $f = explode(';',trim($fpdata,';'));
  455. //$fl = substr_count($fpdata,'Bob');//暂时不用这个,防止产品出现大小写不一致
  456. $i = 0;
  457. foreach ($f as $v)
  458. {
  459. if(stripos($fpdata,'bob') !== false && (stripos($v,'bob') !== false || stripos($v,'wig') === false))
  460. {
  461. $i++;
  462. }
  463. }
  464. if(count($f) == $i)
  465. {
  466. $type = $this->typeclass->get_type('37','BOBTYPE');
  467. }
  468. else if(stripos($fpdata,'wig') !== false)
  469. {
  470. $type = $this->typeclass->get_type('37','WIGTYPE');
  471. }
  472. else
  473. {
  474. $type = $this->typeclass->get_type('37','OTHERTYPE');
  475. }
  476. $ckfl = $type['id'];
  477. return array('sfxh'=>$sfxh,'ckfl'=>$ckfl);
  478. }
  479. } //end class