goodsModel.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. $where = '1=1';
  91. if(!isempt($typeid)){
  92. $alltpeid = m('option')->getalldownid($typeid);
  93. $where = 'a.`typeid` in('.$alltpeid.')';
  94. }
  95. $stockarr = array();
  96. if($lx==3){
  97. $ckid = $this->rock->get('ckid');
  98. $aids = '0';
  99. if(!isempt($ckid)){
  100. $rowss= $this->db->getall("select `aid`,sum(`count`)as counts from `[Q]goodss` where `depotid`='$ckid' group by `aid`");
  101. foreach($rowss as $k=>$rs){
  102. $aids.=','.$rs['aid'].'';
  103. $stockarr[$rs['aid']] = $rs['counts'];
  104. }
  105. }
  106. $where.= ' and a.`id` in('.$aids.')';
  107. }
  108. $where .= m('admin')->getcompanywhere(1,'a.');
  109. $rowss = $this->db->getall('select a.`id`,a.`name`,a.`xinghao`,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.'');
  110. $rows = array();
  111. foreach($rowss as $k=>$rs){
  112. $name = $rs['name'];
  113. if(!isempt($rs['xinghao']))$name.='('.$rs['xinghao'].')';
  114. $stock = $rs['stock'];
  115. if($lx==3){
  116. $stock = arrvalue($stockarr,$rs['id'],'0');
  117. }
  118. $baar = array(
  119. 'name' => $name,
  120. 'value' => $rs['id'],
  121. 'price' => $rs['price'],
  122. 'unit' => $rs['unit'],
  123. 'stock' => $stock,
  124. 'subname' => $rs['typename'],
  125. );
  126. if(($lx==1 || $lx==3) && $stock<='0'){
  127. $baar['disabled']= true;//领用没有库存了
  128. $baar['subname'].= ' 无库存';
  129. }
  130. $rows[] = $baar;
  131. }
  132. if($lx==0)return $rows;
  133. $selectdata = $this->getgoodstype(1);
  134. return array(
  135. 'rows' => $rows,
  136. 'selectdata'=>$selectdata
  137. );
  138. }
  139. /**
  140. * 主表goodm部分出入库状态更新
  141. */
  142. public function upstatem($ids='')
  143. {
  144. $dbm = m('goodm');
  145. $where= '';
  146. if($ids!='')$where="`id` in($ids) and ";
  147. $rows = $dbm->getall(''.$where.'`status`=1 and `state` in(0,2)');
  148. foreach($rows as $k=>$rs){
  149. $id = $rs['id'];
  150. $state = $rs['state'];
  151. $rsone = $this->db->getone('[Q]goodn','`mid`='.$id.'','sum(`count`)count,sum(`couns`)couns');
  152. $count = floatval($rsone['count']);
  153. $couns = floatval($rsone['couns']);
  154. if($couns>=$count){
  155. $zt = 1;
  156. }else if($couns==0){
  157. $zt = 0;
  158. }else{
  159. $zt = 2;
  160. }
  161. if($state!=$zt)$dbm->update('`state`='.$zt.'', $id);
  162. }
  163. }
  164. /**
  165. * 供应商列表
  166. */
  167. public function getgys()
  168. {
  169. $where = m('admin')->getcompanywhere(1);
  170. $arows = m('customer')->getall('`status`=1 and `isgys`=1 '.$where.'','id as value,name');
  171. return $arows;
  172. }
  173. /**
  174. * 获取仓库下拉框
  175. */
  176. public function godepotarr()
  177. {
  178. $where = m('admin')->getcompanywhere(1);
  179. $depotarr = m('godepot')->getall('1=1'.$where.'','id,depotname as name,depotnum','`sort`');
  180. $rows = array();
  181. foreach($depotarr as $k=>$rs){
  182. $rows[] = array(
  183. 'name' => ''.$rs['depotnum'].'.'.$rs['name'].'',
  184. 'value' => $rs['id'],
  185. );
  186. }
  187. return $rows;
  188. }
  189. /**
  190. * 根据主表Id获取申请物品信息, $glx 0原始数组,1字符串
  191. */
  192. public function getgoodninfo($mid, $glx=0, $mgx=5)
  193. {
  194. $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`");
  195. $str = '';
  196. if($glx==1){
  197. foreach($rows as $k1=>$rs1){
  198. if($k1>$mgx)break;
  199. $str.=''.$rs1['name'].'';
  200. if(!isempt($rs1['xinghao']))$str.='('.$rs1['xinghao'].')';
  201. $str .=':'.$rs1['count'].''.$rs1['unit'].'';
  202. if($rs1['lygh']=='1')$str.='<font color=blue>(需归还)</font>';
  203. if($rs1['lygh']=='2')$str.='<font color=green>(已归还)</font>';
  204. $str.=';';
  205. }
  206. return $str;
  207. }
  208. return $rows;
  209. }
  210. /**
  211. * 获取分类名称
  212. */
  213. private $typenamearr= array();
  214. public function gettypename($tid)
  215. {
  216. if(isset($this->typenamearr[$tid])){
  217. return $this->typenamearr[$tid];
  218. }else{
  219. $one = $this->db->getone('[Q]option',$tid);
  220. $varr= '';
  221. if($one){
  222. $varr = $one['name'];
  223. if(!isempt($one['pid']) && $one['pid']){
  224. $one = $this->db->getone('[Q]option',$one['pid']);
  225. if($one && !contain($one['num'],'goodstype')){
  226. $varr = $one['name'].'/'.$varr.'';
  227. }
  228. }
  229. }
  230. $this->typenamearr[$tid] = $varr;
  231. return $varr;
  232. }
  233. }
  234. }
粤ICP备19079148号