crmModel.php 8.0 KB

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