crmModel.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. class crmClassModel extends Model
  3. {
  4. public function initModel()
  5. {
  6. $this->settable('customer');
  7. }
  8. //读取我的客户和共享给我的
  9. public function getmycust($uid=0, $id=0)
  10. {
  11. if(isempt($id))$id = 0;
  12. if($uid==0)$uid=$this->adminid;
  13. $s = $this->rock->dbinstr('shateid', $uid);
  14. $rows = $this->getrows("`status`=1 and ((`uid`='$uid') or (`id`=$id) or (".$s."))",'id as value,name,id,unitname as subname','`optdt` desc');
  15. return $rows;
  16. }
  17. //读取所有客户
  18. public function custdata()
  19. {
  20. $where = m('admin')->getcompanywhere(3);
  21. $rows = $this->getrows("`status`=1 ".$where."",'id as value,name,id,unitname as subname','`optdt` desc');
  22. return $rows;
  23. }
  24. //读取我的销售机会
  25. public function getmysale($uid, $id=0)
  26. {
  27. $where = '`uid`='.$uid.' and `state` in(0,1) and (`htid`=0 or `htid`='.$id.')';
  28. $rows = m('custsale')->getrows($where, 'id,custid,custname,money,laiyuan');
  29. return $rows;
  30. }
  31. //读取我的合同
  32. public function getmyract($uid, $id=0, $type=2)
  33. {
  34. $where = '';
  35. if($type==0 || $type==1)$where='and `type`='.$type.'';
  36. $where = '`uid`='.$uid.' '.$where.' and (`isover`=0 or `id`='.$id.')';
  37. $rows = m('custract')->getrows($where, 'id,custid,custname,money,num');
  38. return $rows;
  39. }
  40. //更新合同状态
  41. public function ractmoney($htid)
  42. {
  43. if(isempt($htid))return false;
  44. if(!is_array($htid)){
  45. $ors = $this->db->getone('[Q]custract','id='.$htid.'','money,moneys,ispay,id,isover');
  46. }else{
  47. $ors = $htid;
  48. }
  49. if(!$ors)return false;
  50. $zmoney = $ors['money']; $moneys = $ors['moneys'];
  51. $oispay = $ors['ispay'];
  52. $htid = $ors['id'];
  53. $money = $this->db->getmou('[Q]custfina','sum(money)','htid='.$htid.' and `ispay`=1');
  54. $moneyy = $this->getmoneys($htid); //已创建收付款单金额
  55. $symon = $zmoney - $money;
  56. $ispay = 0;
  57. $isover = 0;
  58. if($symon<=0){
  59. $ispay = 1;
  60. }else if($money>0){
  61. $ispay = 2;
  62. }
  63. if($moneyy>=$zmoney)$isover = 1;
  64. if($ispay != $oispay || $symon!= $moneys || $isover != $ors['isover']){
  65. $this->db->update('[Q]custract','`ispay`='.$ispay.',`moneys`='.$symon.',`isover`='.$isover.'', $htid);
  66. }
  67. return array($ispay, $symon);
  68. }
  69. public function getmoneys($htid, $id=0)
  70. {
  71. $moneys = floatval($this->db->getmou('[Q]custfina','sum(money)','`htid`='.$htid.' and `id`<>'.$id.''));
  72. return $moneys;
  73. }
  74. /**
  75. * 对应人统计金额
  76. */
  77. public function moneytotal($uid, $month)
  78. {
  79. $uid = (int)$uid;
  80. $sql = "SELECT uid,type,ispay,sum(money)money,count(1)stotal FROM `[Q]custfina` where `uid`='$uid' and `dt` like '$month%' GROUP BY type,ispay";
  81. $farr = explode(',', 'shou_moneyd,shou_moneyz,shou_moneys,shou_moneyn,shou_shu,fu_moneyd,fu_moneyz,fu_moneys,fu_moneyn,fu_shu');
  82. foreach($farr as $f)$$f= 0;
  83. $rows = $this->db->getall($sql);
  84. foreach($rows as $k=>$rs){
  85. $type = $rs['type']; $ispay = $rs['ispay'];
  86. $money = floatval($rs['money']);
  87. $stotal = floatval($rs['stotal']);
  88. if($type==0){
  89. if($ispay==1){
  90. $shou_moneys += $money;
  91. }else{
  92. $shou_moneyd += $money;
  93. }
  94. $shou_shu += $stotal;
  95. $shou_moneyz += $money;
  96. }else{
  97. if($ispay==1){
  98. $fu_moneys += $money;
  99. }else{
  100. $fu_moneyd += $money;
  101. }
  102. $fu_shu += $stotal;
  103. $fu_moneyz += $money;
  104. }
  105. }
  106. //当月已收付
  107. $sql = "SELECT type,sum(money)money FROM `[Q]custfina` where `uid`='$uid' and `ispay`=1 and `paydt` like '$month%' GROUP BY type";
  108. $rows = $this->db->getall($sql);
  109. foreach($rows as $k=>$rs){
  110. if($rs['type']==0)$shou_moneyn = $rs['money']+0;
  111. if($rs['type']==1)$fu_moneyn = $rs['money']+0;
  112. }
  113. $arr = array();
  114. foreach($farr as $f)$arr[$f] = $$f;
  115. return $arr;
  116. }
  117. //客户转移
  118. public function movetouser($uid, $sid, $toid)
  119. {
  120. $rows = $this->getrows("`id` in($sid)",'id,uid,name');
  121. $toname = m('admin')->getmou('name',"`id`='$toid'");
  122. if(isempt($toname))return false;
  123. foreach($rows as $k=>$rs){
  124. $id = $rs['id'];
  125. $uarr = array();
  126. $uarr['uid'] = $toid;
  127. $uarr['optname']= $toname;
  128. $nowid = (int)$rs['uid'];
  129. if($nowid==0)$nowid = $uid;
  130. $this->update(array('uid'=>$toid,'suoname'=>$toname), $id);
  131. m('custract')->update($uarr, "`uid`='$nowid' and `custid`='$id'");
  132. m('custsale')->update($uarr, "`uid`='$nowid' and `custid`='$id'"); //销售机会
  133. m('goodm')->update($uarr, "`uid`='$nowid' and `custid`='$id' and `type`=2"); //销售的
  134. m('custplan')->update($uarr, "`uid`='$nowid' and `custid`='$id'"); //跟进计划
  135. $uarr['ismove']=1;
  136. m('custfina')->update($uarr, "`uid`='$nowid' and `custid`='$id'");
  137. }
  138. }
  139. //客户统计
  140. public function custtotal($ids='')
  141. {
  142. $wher = '';
  143. $whe2 = '';
  144. $ustr = '`moneyz`=0,`moneyd`=0,`htshu`=0';
  145. if($ids!=''){
  146. $wher=' and `custid` in('.$ids.')';
  147. $whe2=' and a.`id` in('.$ids.')';
  148. $this->update($ustr,'id in('.$ids.')');
  149. }else{
  150. $this->update($ustr,'id>0');
  151. }
  152. $rows = $this->db->getall('SELECT custid,sum(money)as moneys,ispay FROM `[Q]custfina` where `type`=0 '.$wher.' GROUP BY custid,ispay');
  153. $arr = array();
  154. foreach($rows as $k=>$rs){
  155. $custid = $rs['custid'];
  156. if(!isset($arr[$custid]))$arr[$custid] = array(0,0,0);
  157. $arr[$custid][0]+=$rs['moneys'];
  158. if($rs['ispay']==0)$arr[$custid][1]+=$rs['moneys'];
  159. }
  160. foreach($arr as $id=>$rs){
  161. $uarr['moneyz'] = $rs[0];
  162. $uarr['moneyd'] = $rs[1];
  163. $this->update($uarr, $id);
  164. }
  165. $rows = $this->db->getall('SELECT custid,count(1)htshu FROM `[Q]custract` where id>0 '.$wher.' GROUP BY custid');
  166. foreach($rows as $k=>$rs){
  167. $custid = $rs['custid'];
  168. $this->update('htshu='.$rs['htshu'].'', $custid);
  169. }
  170. $rows= $this->db->getall('select b.name,a.id from `[Q]customer` a left join `[Q]admin` b on a.`uid`=b.`id` where a.`uid`>0 '.$whe2.' and (a.`suoname`<>b.`name` or a.`suoname` is null)');
  171. foreach($rows as $k=>$rs){
  172. $this->update("`suoname`='".$rs['name']."'", $rs['id']);
  173. }
  174. $this->update('`suoname`=null','`uid`=0');
  175. }
  176. //合同状态金额更新
  177. public function custractupzt($htid='')
  178. {
  179. $where1= $where2= '';
  180. if(!isempt($htid)){
  181. $where1="and `htid` in($htid)";
  182. $where2="and `id` in($htid)";
  183. }
  184. $this->db->update('[Q]custract','`ispay`=0,`isover`=0,`moneys`=money','`id`>0 '.$where2.'');
  185. $rows = $this->db->getall('SELECT `htid` FROM `[Q]custfina` where `htid`>0 '.$where1.' GROUP BY htid');
  186. foreach($rows as $k=>$rs){
  187. $htid = $rs['htid'];
  188. $this->ractmoney($htid);
  189. }
  190. //更新收付款单
  191. $rows = $this->db->getall('SELECT a.id,a.htid,a.htnum,b.num FROM `[Q]custfina` a left join `[Q]custract` b on a.htid=b.id where a.htid>0 and a.htnum<>b.num ');
  192. foreach($rows as $k=>$rs){
  193. $htid = $rs['htid'];
  194. if(isempt($rs['num']))$htid = 0;
  195. $this->db->record('[Q]custfina', array(
  196. 'htid' => $htid,
  197. 'htnum' => $rs['num'],
  198. ), $rs['id']);
  199. }
  200. }
  201. /**
  202. * 跟进名称读取客户档案
  203. */
  204. public function getcustomer($name)
  205. {
  206. if(isempt($name))return false;
  207. $rs = $this->getone("(`name`='$name' or `unitname`='$name')");
  208. return $rs;
  209. }
  210. /**
  211. * 销售单是收款状态
  212. */
  213. public function xiaozhuantai($rs, $lx=0, $csid=0)
  214. {
  215. $str = '';
  216. $wshou1 = 0;
  217. if($rs['status']=='5')return ($lx==0)?'作废了':0;
  218. if($rs['custractid']=='0'){
  219. $finrows = $this->db->getall('select * from `[Q]custfina` where `htid`=-'.$rs['id'].' and `id`<>'.$csid.'');
  220. $shou = 0;
  221. $shou1 = 0;//已创建金额
  222. $ispay = '0';
  223. foreach($finrows as $k1=>$rs1){
  224. if($rs1['ispay']=='1')$shou+=floatval($rs1['money']);
  225. $shou1+=floatval($rs1['money']);
  226. }
  227. $wshou = floatval($rs['money'])-$shou;
  228. $wshou1 = floatval($rs['money'])-$shou1;
  229. if($wshou<0)$wshou = 0;
  230. if($wshou1<=0){
  231. $wshou1 = 0;//未创建
  232. $ispay = '1';
  233. }
  234. if($wshou==0){
  235. $str = '<font color=green>已全部收款</font>';
  236. }else{
  237. $str = '待收<font color=#ff6600>'.$wshou.'</font>';
  238. }
  239. if($ispay!=$rs['ispay'])$this->db->update('[Q]goodm','`ispay`='.$ispay.'', '`id`='.$rs['id'].'');
  240. }
  241. if($lx==1)return $wshou1;
  242. return $str;
  243. }
  244. }
粤ICP备19079148号