goodsModel.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. class goodsClassModel extends Model
  3. {
  4. //$lx=0入库,$lx=1出
  5. public function crkstate($zt, $lx=0)
  6. {
  7. $arrs = array('入','出');
  8. $ztna = array('待'.$arrs[$lx].'库','已'.$arrs[$lx].'库','已部分'.$arrs[$lx].'库');
  9. $ztnc = array('red','green','#ff6600');
  10. return '<font color="'.$ztnc[$zt].'">'.$ztna[$zt].'</font>';
  11. }
  12. //设置库存
  13. public function setstock($id='', $lsx='1')
  14. {
  15. $where = '';
  16. if($id!=''){
  17. $id = c('check')->onlynumber($id);
  18. $where=' and `aid` in('.$id.')';
  19. }
  20. $sql = 'SELECT sum(count)stock,aid FROM `[Q]goodss` where `status`=1 '.$where.' GROUP BY aid';
  21. if($id=='')$this->update('stock=0','id>0');
  22. $rows= $this->db->getall($sql);
  23. foreach($rows as $k=>$rs){
  24. $this->update('`stock`='.$rs['stock'].'', $rs['aid']);
  25. }
  26. }
  27. //根据仓库日期获取库存
  28. public function getstock($id='', $dt='')
  29. {
  30. $where= '';
  31. if($id!=''){
  32. $id = c('check')->onlynumber($id);
  33. $where='`aid` in('.$id.') and ';
  34. }
  35. if($dt!='')$where.="`applydt`<='$dt' and "; //日期
  36. $sql = 'SELECT sum(count)stock,`aid`,`depotid` FROM `[Q]goodss` where '.$where.' `status`=1 GROUP BY `aid`,`depotid`';
  37. $rows= $this->db->getall($sql);
  38. $arra = array();
  39. foreach($rows as $k=>$rs){
  40. $aid = $rs['aid'];
  41. $arra[$aid][$rs['depotid']] = $rs['stock'];
  42. if(!isset($arra[$aid][0])) $arra[$aid][0]= 0;
  43. $arra[$aid][0]+=floatval($rs['stock']);
  44. }
  45. return $arra;
  46. }
  47. //待出入库数量
  48. public function getdaishu()
  49. {
  50. $where = m('admin')->getcompanywhere(1);
  51. return $this->db->rows('`[Q]goodm`','`status`=1 and `state`<>1 '.$where.'');
  52. }
  53. //判断是否存在相同库存
  54. public function existsgoods($rs, $id=0)
  55. {
  56. $where = "`id`<>".$id." and `typeid`=".$rs['typeid']." and `name`='".$rs['name']."' and ifnull(`guige`,'')='".$rs['guige']."' and ifnull(`xinghao`,'')='".$rs['xinghao']."'";
  57. $to = $this->rows($where);
  58. return $to>0;
  59. }
  60. public function getgoodstype($lx=0)
  61. {
  62. $dbs = m('option');
  63. $num = 'goodstype';
  64. if(ISMORECOM && $cnum=m('admin')->getcompanynum())$num.='_'.$cnum.'';
  65. $rowss = $dbs->getdata($num);
  66. $rows = array();
  67. $str1 = ' &nbsp; &nbsp; ';
  68. if($lx==1)$str1=' ';
  69. foreach($rowss as $k=>$rs){
  70. $rows[] = array(
  71. 'name' => $rs['name'],
  72. 'value' => $rs['id'],
  73. );
  74. $rowsa = $dbs->getdata($rs['id']);
  75. if($rowsa)foreach($rowsa as $k1=>$rs1){
  76. $rows[] = array(
  77. 'name' => ''.$str1.'├'.$rs1['name'],
  78. 'value' => $rs1['id'],
  79. );
  80. }
  81. }
  82. return $rows;
  83. }
  84. /**
  85. * $lx=0默认,1领用销售,2采购,3调拨
  86. */
  87. public function getgoodsdata($lx=0)
  88. {
  89. $typeid = $this->rock->get('selvalue');
  90. $limit = (int)$this->rock->get('limit', '10');
  91. $page = (int)$this->rock->get('page', '1');
  92. $key = $this->rock->get('key');
  93. $where = '1=1';
  94. if(!isempt($typeid)){
  95. $alltpeid = m('option')->getalldownid($typeid);
  96. $where = 'a.`typeid` in('.$alltpeid.')';
  97. }
  98. $stockarr = array();
  99. if($lx==3){
  100. $ckid = $this->rock->get('ckid');
  101. $aids = '0';
  102. if(!isempt($ckid)){
  103. $rowss= $this->db->getall("select `aid`,sum(`count`)as counts from `[Q]goodss` where `depotid`='$ckid' group by `aid`");
  104. foreach($rowss as $k=>$rs){
  105. $aids.=','.$rs['aid'].'';
  106. $stockarr[$rs['aid']] = $rs['counts'];
  107. }
  108. }
  109. $where.= ' and a.`id` in('.$aids.')';
  110. }
  111. $where .= m('admin')->getcompanywhere(1,'a.');
  112. if($key){
  113. $key= $this->rock->jm->base64decode($key);
  114. $where.= " and (a.`name` like '%$key%' or a.`num` like '%$key%' or a.`xinghao` like '%$key%' or a.`guige` like '%$key%')";
  115. }
  116. $rowss = $this->db->getall('select SQL_CALC_FOUND_ROWS a.`id`,a.`name`,a.`xinghao`,a.`num`,a.`guige`,a.`stock`,a.`price`,a.`unit`,b.`name` as `typename` from `[Q]goods` a left join `[Q]option` b on a.`typeid`=b.`id` where '.$where.' limit '.(($page-1)*$limit).','.$limit.'');
  117. $totalCount = $this->db->found_rows();
  118. $rows = array();
  119. foreach($rowss as $k=>$rs){
  120. $name = $rs['name'];
  121. if(!isempt($rs['xinghao']))$name.='('.$rs['xinghao'].')';
  122. $stock = $rs['stock'];
  123. if($lx==3){
  124. $stock = arrvalue($stockarr,$rs['id'],'0');
  125. }
  126. $baar = array(
  127. 'name' => $name,
  128. 'value' => $rs['id'],
  129. 'num' => $rs['num'],
  130. 'price' => $rs['price'],
  131. 'unit' => $rs['unit'],
  132. 'stock' => $stock,
  133. 'subname' => $rs['num'].' '.$rs['typename'],
  134. );
  135. if(($lx==1 || $lx==3) && $stock<='0'){
  136. $baar['disabled']= true;//领用没有库存了
  137. $baar['subname'].= ' 无库存';
  138. }
  139. $rows[] = $baar;
  140. }
  141. if($lx==0)return $rows;
  142. $selectdata = $this->getgoodstype(1);
  143. return array(
  144. 'rows' => $rows,
  145. 'selectdata'=>$selectdata,
  146. 'totalCount'=>$totalCount,
  147. 'page' => $page,
  148. 'limit' => $limit,
  149. );
  150. }
  151. /**
  152. * 主表goodm部分出入库状态更新
  153. */
  154. public function upstatem($ids='')
  155. {
  156. $dbm = m('goodm');
  157. $where= '';
  158. if($ids!='')$where="`id` in($ids) and ";
  159. $rows = $dbm->getall(''.$where.'`status`=1 and `state` in(0,2)');
  160. foreach($rows as $k=>$rs){
  161. $id = $rs['id'];
  162. $state = $rs['state'];
  163. $rsone = $this->db->getone('[Q]goodn','`mid`='.$id.'','sum(`count`)count,sum(`couns`)couns');
  164. $count = floatval($rsone['count']);
  165. $couns = floatval($rsone['couns']);
  166. if($couns>=$count){
  167. $zt = 1;
  168. }else if($couns==0){
  169. $zt = 0;
  170. }else{
  171. $zt = 2;
  172. }
  173. if($state!=$zt)$dbm->update('`state`='.$zt.'', $id);
  174. }
  175. }
  176. /**
  177. * 供应商列表
  178. */
  179. public function getgys()
  180. {
  181. $where = m('admin')->getcompanywhere(1);
  182. $arows = m('customer')->getall('`status`=1 and `isgys`=1 '.$where.'','id as value,name');
  183. return $arows;
  184. }
  185. /**
  186. * 获取仓库下拉框
  187. */
  188. public function godepotarr()
  189. {
  190. $where = m('admin')->getcompanywhere(1);
  191. $depotarr = m('godepot')->getall('1=1'.$where.'','id,depotname as name,depotnum','`sort`');
  192. $rows = array();
  193. foreach($depotarr as $k=>$rs){
  194. $rows[] = array(
  195. 'name' => ''.$rs['depotnum'].'.'.$rs['name'].'',
  196. 'value' => $rs['id'],
  197. );
  198. }
  199. return $rows;
  200. }
  201. /**
  202. * 根据主表Id获取申请物品信息, $glx 0原始数组,1字符串
  203. */
  204. public function getgoodninfo($mid, $glx=0, $mgx=5)
  205. {
  206. $rows = $this->db->getall("select a.`count`,a.couns,a.`price`,b.`unit`,b.`num`,b.`name`,b.`guige`,b.`xinghao`,a.`lygh` from `[Q]goodn` a left join `[Q]goods` b on a.`aid`=b.`id` where a.`mid`='$mid' order by a.`sort`");
  207. $str = '';
  208. if($glx==1){
  209. foreach($rows as $k1=>$rs1){
  210. if($k1>$mgx)break;
  211. $str.=''.$rs1['name'].'';
  212. if(!isempt($rs1['xinghao']))$str.='('.$rs1['xinghao'].')';
  213. $str .=':'.$rs1['count'].''.$rs1['unit'].'';
  214. if($rs1['lygh']=='1')$str.='<font color=blue>(需归还)</font>';
  215. if($rs1['lygh']=='2')$str.='<font color=green>(已归还)</font>';
  216. $str.=';';
  217. }
  218. return $str;
  219. }
  220. return $rows;
  221. }
  222. /**
  223. * 获取分类名称
  224. */
  225. private $typenamearr= array();
  226. public function gettypename($tid)
  227. {
  228. if(isset($this->typenamearr[$tid])){
  229. return $this->typenamearr[$tid];
  230. }else{
  231. $one = $this->db->getone('[Q]option',$tid);
  232. $varr= '';
  233. if($one){
  234. $varr = $one['name'];
  235. if(!isempt($one['pid']) && $one['pid']){
  236. $one = $this->db->getone('[Q]option',$one['pid']);
  237. if($one && !contain($one['num'],'goodstype')){
  238. $varr = $one['name'].'/'.$varr.'';
  239. }
  240. }
  241. }
  242. $this->typenamearr[$tid] = $varr;
  243. return $varr;
  244. }
  245. }
  246. /**
  247. * 直接操作出入库
  248. */
  249. public function chukuopts($mid, $mknum)
  250. {
  251. $isru = m('option')->getval('wpautostock');
  252. if($isru!='1')return;
  253. $barr = $this->chukuopt($mid);
  254. if(!$barr['success'])m('log')->addlogs('直接出入库', $mknum.'('.$mid.'):'.$barr['msg'], 2);
  255. }
  256. public function chukuopt($mid, $depotid=0)
  257. {
  258. $mrs = m('goodm')->getone("`id`='$mid' and `status`=1");
  259. if(!$mrs)return returnerror('该单据还未审核完成,不能出入库操作');
  260. $comid = $mrs['comid'];
  261. if($depotid==0){
  262. $where = '1=1';
  263. if(ISMORECOM){
  264. $where = 'comid='.$comid.'';
  265. }
  266. $grs = m('godepot')->getone($where);
  267. if(!$grs)return returnerror('没有创建仓库');
  268. $depotid = $grs['id'];
  269. }
  270. $mtype = (int)$mrs['type']; //3就是调拨
  271. $typv = (int)$mrs['type'];
  272. $typa = explode(',', '1,0,1,0,0,0');
  273. $kina = explode(',', '0,0,1,3,1,4');
  274. if(!isset($typa[$typv]) || !isset($kina[$typv]))return returnerror('为设置出入库类型');
  275. $type = $typa[$typv];
  276. $kind = $kina[$typv];
  277. //if($mtype==3 && $depotid==$mrs['custid'])return returnerror('调拨出入库仓库不能相同');
  278. $ndbs = m('goodn');
  279. //读取已入库数量
  280. $arwos = $ndbs->getall('`mid`='.$mid.' and `couns`<`count`');
  281. if(!$arwos)return returnerror('子表没用可出入库得');
  282. $arr['applydt'] = $this->rock->date;
  283. $arr['type'] = $type;
  284. $arr['kind'] = $kind;
  285. $arr['depotid'] = $depotid;
  286. $arr['explain'] = '';
  287. $arr['uid'] = $this->adminid;
  288. $arr['optid'] = $this->adminid;
  289. $arr['optdt'] = $this->rock->now;
  290. $arr['comid'] = $comid;
  291. $arr['optname'] = $this->adminname;
  292. $arr['status'] = 1;
  293. $arr['mid'] = $mid;
  294. $aid = '0';
  295. foreach($arwos as $k1=>$rs1){
  296. $count = floatval($rs1['count']) - floatval($rs1['couns']);
  297. if($count<=0)continue;
  298. $arr['type'] = $type;
  299. $arr['depotid'] = $depotid;
  300. $arr['aid'] = $rs1['aid'];
  301. $arr['count'] = $count;
  302. if($type==1)$arr['count'] = 0 - $arr['count'];//出库为负数
  303. $ussid = $this->db->record('[Q]goodss', $arr);
  304. if($ussid){
  305. $ndbs->update('`couns`=`count`', $rs1['id']);
  306. }
  307. if($mtype==3){
  308. $arr['depotid'] = $mrs['custid']; //仓库
  309. $arr['type'] = 1; //出库
  310. $arr['count'] = 0 - $count;
  311. $this->db->record('[Q]goodss', $arr);
  312. }
  313. $aid.=','.$rs1['aid'].'';
  314. }
  315. if($aid!='0')$this->setstock($aid);
  316. $this->upstatem($mid);
  317. return returnsuccess();
  318. }
  319. }
粤ICP备19079148号