crmModel.php 9.1 KB

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