Model_ck.php 14 KB

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