Model_ck.php 14 KB

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