crmModel.php 9.2 KB

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