Model_ck.php 15 KB

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