Model_whlabel.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Model_Whlabel extends Lin_Model
  3. {
  4. function __construct(){
  5. parent::__construct();
  6. $this->load->database();
  7. $this->table = 'whlabel';
  8. $this->load_table('whlabel');
  9. }
  10. public function get_supplier($supplier)
  11. {
  12. return $this->find("supplier = '$supplier'");
  13. }
  14. public function get_sku($sku)
  15. {
  16. return $this->find("sku = '$sku'");
  17. }
  18. public function get_cpid($cpid)
  19. {
  20. return $this->find("cpid = '$cpid'");
  21. }
  22. public function get_features($features)
  23. {
  24. return $this->find("features = '$features'");
  25. }
  26. public function get_warehousesku($warehouse,$sku)
  27. {
  28. return $this->find("warehouse = '$warehouse' and sku = '$sku'");
  29. }
  30. public function get_enter($enter)
  31. {
  32. return $this->find("enter = '$enter'");
  33. }
  34. public function get_number($number)
  35. {
  36. return $this->find("number = '$number'");
  37. }
  38. public function get_cxzd($number,$zd)
  39. {
  40. return $this->find("number = '$number' and zd = '$zd'");
  41. }
  42. public function get_numberout($number,$warehouse)
  43. {
  44. return $this->find("state = '0' and number = '$number' and warehouse = '$warehouse'");
  45. }
  46. public function get_labelnumberout($shop,$number,$warehouse)
  47. {
  48. return $this->find("state = '0' and shop = '$shop' and number = '$number' and warehouse = '$warehouse' and zd = ''");
  49. }
  50. public function get_labelnumberout_purchase($shop,$number,$warehouse,$purchase)
  51. {
  52. return $this->find("state = '0' and shop = '$shop' and number = '$number' and warehouse = '$warehouse' and purchase = '$purchase' and zd = ''");
  53. }
  54. public function get_labelnumberout_purchase_nopur($shop,$number,$warehouse)
  55. {
  56. return $this->find("state = '0' and shop = '$shop' and number = '$number' and warehouse = '$warehouse' and zd = ''");
  57. }
  58. public function get_numberret($number,$warehouse,$orderinfo,$waybill)
  59. {
  60. return $this->find("state = 1 and number = '$number' and warehouse = '$warehouse' and orderinfo = '$orderinfo' and waybill = '$waybill'");
  61. }
  62. public function get_label($label)
  63. {
  64. return $this->find("label = '$label'");
  65. }
  66. public function get_title($title)
  67. {
  68. return $this->find("title like '%$title%'");
  69. }
  70. public function get_kcyz($number,$warehouse)//库存验证 检查库存是否充足----//这个判断和出库不一样!
  71. {
  72. $save = array();$x=0;
  73. $pp = explode('|',trim($number,'|'));
  74. foreach ($pp as $va)
  75. {
  76. $ckcg = 0;
  77. $num = explode('-',$va);
  78. for($i=0;$i<$num[1];$i++)
  79. {
  80. $whlabel = $this->get_numberout($num[0],$warehouse);
  81. if(!$whlabel)
  82. {
  83. $ckcg++;
  84. }
  85. }
  86. if($ckcg > 0)
  87. {
  88. $save[] = $x;
  89. }
  90. $x++;
  91. }
  92. if(count($save) > 0)
  93. {
  94. return $save;
  95. }
  96. else
  97. {
  98. return 1;
  99. }
  100. }
  101. public function get_kc($oldstate,$state,$zd,$ys,$warehouse,$whlabel,$features,$fpdatatitle,$number,$shop,$oldwarehouse=0,$oldwhlabel='')//库存验证 提交订单时验证
  102. {
  103. //smt:edit-bcbz-readonly-customersmt_medit,dlz:edit-readonly-customer_medit
  104. //$aa = $this->whlabel->find_all("zd = '$number'");//过一天删除这个条件,过度用,下面aa也删除
  105. //if($oldwarehouse == $warehouse && $oldwhlabel == $whlabel && $state == $oldstate)//如果没有改变仓库和产品和状态直接返回通过
  106. //{
  107. //return array('t'=>0);exit;
  108. //}
  109. if(!$whlabel || $whlabel == '|')
  110. {
  111. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'请先添加产品信息!','oldwarehouse'=>$oldwarehouse-1,'success'=>false)));exit;
  112. }
  113. //先预先判断开始,否则清除数据后造成库存错误
  114. if($zd == '1' && ($state == '207' || $state == '209'))//待发货状态
  115. {
  116. $pp = explode('|',trim($whlabel,'|'));
  117. $features = explode('|',rtrim($features,'|'));
  118. $x = 0;$save = array();
  119. //合并重复项开始
  120. /** //已在SKU编辑中去重了 待观察这个可以去掉
  121. $hbdata = array();
  122. foreach ($pp as $va)
  123. {
  124. $num = explode('-',$va);
  125. if(!isset($hbdata[$num[0]]))
  126. {
  127. $hbdata[$num[0]] = array($num[0],$num[1]);
  128. }
  129. else
  130. {
  131. $hbdata[$num[0]][1] += $num[1];
  132. }
  133. }
  134. $pp = array();
  135. foreach ($hbdata as $va)
  136. {
  137. $pp[] = $va[0].'-'.$va[1];
  138. }
  139. **/
  140. //合并重复项结束
  141. foreach ($pp as $va)
  142. {
  143. $num = explode('-',$va);
  144. $wh = $this->whlabel->find_count("state = '0' and number = '$num[0]' and warehouse = '$warehouse' and (zd = '$number' or zd = '') and (shop IS NULL or shop = '')");//通用
  145. $shopwh = $this->whlabel->find_count("state = '0' and number = '$num[0]' and warehouse = '$warehouse' and (zd = '$number' or zd = '') and shop like '%,".$shop.",%'");//专属
  146. $x++;
  147. if($wh+$shopwh < $num[1])
  148. {
  149. $save[] = $x-1;continue;//提前先加X不然出错,所以要减1
  150. }
  151. }
  152. if(isset($save[0]) && $ys == '0')//非预设仓库提醒
  153. {
  154. return array('t'=>1,'m'=>json_encode(array('t'=>2,'msg'=>"红色商品名库存匹配错误或库存不够扣减",'oldwarehouse'=>$oldwarehouse-1,'error'=>$save,'success'=>false)));exit;
  155. }
  156. }
  157. if($zd == '1' && $state != '207' && $state != '209')//待发货状态
  158. {
  159. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'此状订单状态无法占用库存','success'=>false)));exit;
  160. }
  161. $this->db->trans_begin();
  162. //预先判断结束
  163. //if($oldwarehouse != $warehouse || $oldwhlabel != $whlabel || $state != $oldstate)
  164. //{
  165. $wl = $this->whlabel->find_all("zd = '$number'");//查找是否有占用库存情况
  166. if($wl)//如果有那么清除所有占用
  167. {
  168. $i = 0;
  169. foreach ($wl as $v)
  170. {
  171. if($v['sku'] == 'Preset' && $v['state'] == '9')
  172. {
  173. $this->whlabel->remove($v['id']);//删除预设
  174. $i++;
  175. }
  176. else
  177. {
  178. $this->whlabel->save(array('zd'=>''),$v['id']);//专属
  179. $i++;
  180. }
  181. }
  182. if($i != count($wl))
  183. {
  184. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>"库存操作失败,请重试",'oldwarehouse'=>$oldwarehouse-1,'success'=>false)));exit;
  185. }
  186. }
  187. if(isset($save[0]) && $ys == '1' && ($state == '207' || $state == '209'))//预设库存增加库存
  188. {
  189. foreach ($save as $v)
  190. {
  191. $time = time();
  192. $pp[$v] = explode('-',$pp[$v]);
  193. for($i=0;$i<$pp[$v][1];$i++)
  194. {
  195. $this->whlabel->insert(array('purchase'=>1,'shop'=>'','sku'=>'Preset','state'=>9,'number'=>$pp[$v][0],'features'=>$features[$v],'title'=>$fpdatatitle[$v],'label'=>$time.rand(1000,9999),'enter'=>$time,'time'=>$time,'retreatwarehouse'=>$warehouse,'warehouse'=>$warehouse,'zd'=>$number));
  196. }
  197. unset($pp[$v]);
  198. }
  199. }
  200. //}
  201. if ($this->db->trans_status() === TRUE)
  202. {
  203. $this->db->trans_commit();
  204. }
  205. else
  206. {
  207. $this->db->trans_rollback();
  208. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','oldwarehouse'=>$oldwarehouse-1,'success'=>false)));exit;
  209. }
  210. $this->db->trans_begin();
  211. if($zd == '1' && isset($pp[0]) && ($state == '207' || $state == '209'))
  212. {
  213. //$pp = explode('|',trim($whlabel,'|'));//预先判断已有这一步
  214. foreach ($pp as $va)
  215. {
  216. $num = explode('-',$va);
  217. $whlabel = $this->whlabel->find_all("state = '0' and number = '$num[0]' and warehouse = '$warehouse' and zd = '' and (shop IS NULL or shop = '')");//通用
  218. $shopwhlabel = $this->whlabel->find_all("state = '0' and number = '$num[0]' and warehouse = '$warehouse' and zd = '' and shop like '%,".$shop.",%'");//专属
  219. if(count($shopwhlabel) > 0)//如果有专属库存
  220. {
  221. if(count($shopwhlabel) >= $num[1])
  222. {
  223. for($i=0;$i<$num[1];$i++)
  224. {
  225. $this->whlabel->save(array('zd'=>$number),$shopwhlabel[$i]['id']);//专属
  226. }
  227. }
  228. else
  229. {
  230. for($i=0;$i<count($shopwhlabel);$i++)
  231. {
  232. $this->whlabel->save(array('zd'=>$number),$shopwhlabel[$i]['id']);//扣减专属
  233. }
  234. for($i=0;$i<$num[1]-count($shopwhlabel);$i++)
  235. {
  236. $this->whlabel->save(array('zd'=>$number),$whlabel[$i]['id']);//扣减通用
  237. }
  238. }
  239. }
  240. else
  241. {
  242. for($i=0;$i<$num[1];$i++)
  243. {
  244. $this->whlabel->save(array('zd'=>$number),$whlabel[$i]['id']);//通用
  245. }
  246. }
  247. }
  248. }
  249. if ($this->db->trans_status() === TRUE)
  250. {
  251. $this->db->trans_commit();
  252. return array('t'=>0);
  253. }
  254. else
  255. {
  256. $this->db->trans_rollback();
  257. return array('t'=>1,'m'=>json_encode(array('t'=>1,'msg'=>'库存操作错误,请重试!','oldwarehouse'=>$oldwarehouse-1,'success'=>false)));
  258. }
  259. }
  260. } //end class