Model_whlabel.php 8.7 KB

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