goodsModel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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!='')$where=' and `aid` in('.$id.')';
  17. $sql = 'SELECT sum(count)stock,aid FROM `[Q]goodss` where `status`=1 '.$where.' GROUP BY aid';
  18. if($id=='')$this->update('stock=0','id>0');
  19. $rows= $this->db->getall($sql);
  20. foreach($rows as $k=>$rs){
  21. $this->update('`stock`='.$rs['stock'].'', $rs['aid']);
  22. }
  23. }
  24. //根据仓库日期获取库存
  25. public function getstock($id='', $dt='')
  26. {
  27. $where= '';
  28. if($id!='')$where='`aid` in('.$id.') and ';
  29. if($dt!='')$where.="`applydt`<='$dt' and "; //日期
  30. $sql = 'SELECT sum(count)stock,`aid`,`depotid` FROM `[Q]goodss` where '.$where.' `status`=1 GROUP BY `aid`,`depotid`';
  31. $rows= $this->db->getall($sql);
  32. $arra = array();
  33. foreach($rows as $k=>$rs){
  34. $aid = $rs['aid'];
  35. $arra[$aid][$rs['depotid']] = $rs['stock'];
  36. if(!isset($arra[$aid][0])) $arra[$aid][0]= 0;
  37. $arra[$aid][0]+=floatval($rs['stock']);
  38. }
  39. return $arra;
  40. }
  41. //待出入库数量
  42. public function getdaishu()
  43. {
  44. $where = m('admin')->getcompanywhere(1);
  45. return $this->db->rows('`[Q]goodm`','`status`=1 and `state`<>1 '.$where.'');
  46. }
  47. //判断是否存在相同库存
  48. public function existsgoods($rs, $id=0)
  49. {
  50. $where = "`id`<>".$id." and `typeid`=".$rs['typeid']." and `name`='".$rs['name']."' and ifnull(`guige`,'')='".$rs['guige']."' and ifnull(`xinghao`,'')='".$rs['xinghao']."'";
  51. $to = $this->rows($where);
  52. return $to>0;
  53. }
  54. public function getgoodstype($lx=0)
  55. {
  56. $dbs = m('option');
  57. $num = 'goodstype';
  58. if(ISMORECOM && $cnum=m('admin')->getcompanynum())$num.='_'.$cnum.'';
  59. $rowss = $dbs->getdata($num);
  60. $rows = array();
  61. $str1 = ' &nbsp; &nbsp; ';
  62. if($lx==1)$str1=' ';
  63. foreach($rowss as $k=>$rs){
  64. $rows[] = array(
  65. 'name' => $rs['name'],
  66. 'value' => $rs['id'],
  67. );
  68. $rowsa = $dbs->getdata($rs['id']);
  69. if($rowsa)foreach($rowsa as $k1=>$rs1){
  70. $rows[] = array(
  71. 'name' => ''.$str1.'├'.$rs1['name'],
  72. 'value' => $rs1['id'],
  73. );
  74. }
  75. }
  76. return $rows;
  77. }
  78. /**
  79. * $lx=0默认,1领用销售,2采购,3调拨
  80. */
  81. public function getgoodsdata($lx=0)
  82. {
  83. $typeid = $this->rock->get('selvalue');
  84. $where = '1=1';
  85. if(!isempt($typeid)){
  86. $alltpeid = m('option')->getalldownid($typeid);
  87. $where = 'a.`typeid` in('.$alltpeid.')';
  88. }
  89. $stockarr = array();
  90. if($lx==3){
  91. $ckid = $this->rock->get('ckid');
  92. $aids = '0';
  93. if(!isempt($ckid)){
  94. $rowss= $this->db->getall("select `aid`,sum(`count`)as counts from `[Q]goodss` where `depotid`='$ckid' group by `aid`");
  95. foreach($rowss as $k=>$rs){
  96. $aids.=','.$rs['aid'].'';
  97. $stockarr[$rs['aid']] = $rs['counts'];
  98. }
  99. }
  100. $where.= ' and a.`id` in('.$aids.')';
  101. }
  102. $where .= m('admin')->getcompanywhere(1,'a.');
  103. $rowss = $this->db->getall('select a.`id`,a.`name`,a.`xinghao`,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.'');
  104. $rows = array();
  105. foreach($rowss as $k=>$rs){
  106. $name = $rs['name'];
  107. if(!isempt($rs['xinghao']))$name.='('.$rs['xinghao'].')';
  108. $stock = $rs['stock'];
  109. if($lx==3){
  110. $stock = arrvalue($stockarr,$rs['id'],'0');
  111. }
  112. $baar = array(
  113. 'name' => $name,
  114. 'value' => $rs['id'],
  115. 'price' => $rs['price'],
  116. 'unit' => $rs['unit'],
  117. 'stock' => $stock,
  118. 'subname' => $rs['typename'],
  119. );
  120. if(($lx==1 || $lx==3) && $stock<='0'){
  121. $baar['disabled']= true;//领用没有库存了
  122. $baar['subname'].= ' 无库存';
  123. }
  124. $rows[] = $baar;
  125. }
  126. if($lx==0)return $rows;
  127. $selectdata = $this->getgoodstype(1);
  128. return array(
  129. 'rows' => $rows,
  130. 'selectdata'=>$selectdata
  131. );
  132. }
  133. /**
  134. * 主表goodm部分出入库状态更新
  135. */
  136. public function upstatem($ids='')
  137. {
  138. $dbm = m('goodm');
  139. $where= '';
  140. if($ids!='')$where="`id` in($ids) and ";
  141. $rows = $dbm->getall(''.$where.'`status`=1 and `state` in(0,2)');
  142. foreach($rows as $k=>$rs){
  143. $id = $rs['id'];
  144. $state = $rs['state'];
  145. $rsone = $this->db->getone('[Q]goodn','`mid`='.$id.'','sum(`count`)count,sum(`couns`)couns');
  146. $count = floatval($rsone['count']);
  147. $couns = floatval($rsone['couns']);
  148. if($couns>=$count){
  149. $zt = 1;
  150. }else if($couns==0){
  151. $zt = 0;
  152. }else{
  153. $zt = 2;
  154. }
  155. if($state!=$zt)$dbm->update('`state`='.$zt.'', $id);
  156. }
  157. }
  158. /**
  159. * 供应商列表
  160. */
  161. public function getgys()
  162. {
  163. $where = m('admin')->getcompanywhere(1);
  164. $arows = m('customer')->getall('`status`=1 and `isgys`=1 '.$where.'','id as value,name');
  165. return $arows;
  166. }
  167. /**
  168. * 获取仓库下拉框
  169. */
  170. public function godepotarr()
  171. {
  172. $where = m('admin')->getcompanywhere(1);
  173. $depotarr = m('godepot')->getall('1=1'.$where.'','id,depotname as name,depotnum','`sort`');
  174. $rows = array();
  175. foreach($depotarr as $k=>$rs){
  176. $rows[] = array(
  177. 'name' => ''.$rs['depotnum'].'.'.$rs['name'].'',
  178. 'value' => $rs['id'],
  179. );
  180. }
  181. return $rows;
  182. }
  183. /**
  184. * 根据主表Id获取申请物品信息, $glx 0原始数组,1字符串
  185. */
  186. public function getgoodninfo($mid, $glx=0, $mgx=5)
  187. {
  188. $rows = $this->db->getall("select a.`count`,a.couns,a.`price`,b.`unit`,b.`num`,b.`name`,b.`guige`,b.`xinghao` from `[Q]goodn` a left join `[Q]goods` b on a.`aid`=b.`id` where a.`mid`='$mid' order by a.`sort`");
  189. $str = '';
  190. if($glx==1){
  191. foreach($rows as $k1=>$rs1){
  192. if($k1>$mgx)break;
  193. $str.=''.$rs1['name'].'';
  194. if(!isempt($rs1['xinghao']))$str.='('.$rs1['xinghao'].')';
  195. $str .=':'.$rs1['count'].''.$rs1['unit'].';';
  196. }
  197. return $str;
  198. }
  199. return $rows;
  200. }
  201. /**
  202. * 获取分类名称
  203. */
  204. private $typenamearr= array();
  205. public function gettypename($tid)
  206. {
  207. if(isset($this->typenamearr[$tid])){
  208. return $this->typenamearr[$tid];
  209. }else{
  210. $one = $this->db->getone('[Q]option',$tid);
  211. $varr= '';
  212. if($one){
  213. $varr = $one['name'];
  214. if(!isempt($one['pid']) && $one['pid']){
  215. $one = $this->db->getone('[Q]option',$one['pid']);
  216. if($one && !contain($one['num'],'goodstype')){
  217. $varr = $one['name'].'/'.$varr.'';
  218. }
  219. }
  220. }
  221. return $varr;
  222. }
  223. }
  224. }