Dashboard.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\admin\controller\qingdong;
  3. use addons\qingdong\model\Staff;
  4. use app\admin\controller\qingdong\Base;
  5. use addons\qingdong\model\Contract;
  6. use addons\qingdong\model\Customer;
  7. use addons\qingdong\model\Field;
  8. use addons\qingdong\model\Receivables;
  9. use addons\qingdong\model\Record;
  10. use addons\qingdong\model\Leads;
  11. use addons\qingdong\model\ExamineRecord;
  12. use think\Config;
  13. /**
  14. * 控制台
  15. * @icon fa fa-dashboard
  16. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  17. */
  18. class Dashboard extends Base {
  19. /**
  20. * 查看
  21. */
  22. public function index() {
  23. //团队统计
  24. $time = strtotime(date('Y-m', strtotime('-1 year')));
  25. $auth = Staff::getMyStaffIds();
  26. $staff_id = implode(',',$auth); //权限
  27. $sources = Field::getField('客户来源');
  28. $customers = Customer::where([
  29. 'owner_staff_id'=>['in',$staff_id],
  30. 'createtime' => [
  31. 'egt',
  32. $time
  33. ]
  34. ])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,source,count(*) as c")->group('source,ctime')->select();
  35. $select = [];
  36. foreach ($customers as $v) {
  37. $select[$v['source']][$v['ctime']] = $v['c'];
  38. }
  39. $toMonthTime = strtotime(date('Y-m'));
  40. $sourcedata = ['客户来源' => ['客户来源']];
  41. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  42. $sourcedata['客户来源'][] = date('Y-m', $time);
  43. foreach ($sources as $v) {
  44. if (empty($sourcedata[$v])) {
  45. $sourcedata[$v][] = $v;
  46. }
  47. $sourcedata[$v][] = intval($select[$v][date('Y-m', $time)] ?? 0);
  48. }
  49. }
  50. //合同金额统计
  51. $time = strtotime(date('Y-m', strtotime('-1 year')));
  52. $contractNums=Contract::where(['order_staff_id'=>['in',$staff_id],'check_status' => 2,'createtime' => ['egt',$time]])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,count(*) as c")->group('ctime')->select();
  53. $contractNums=modify_array($contractNums,'ctime','c');
  54. $contract=Contract::where(['order_staff_id'=>['in',$staff_id],'check_status' => 2,'createtime' => ['egt',$time]])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,sum(money) as s")->group('ctime')->select();
  55. $contract=modify_array($contract,'ctime','s');
  56. //回款
  57. $receivables=Receivables::where(['owner_staff_id'=>['in',$staff_id],'check_status'=>2,'createtime' => ['egt',$time]])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,sum(money) as s")->group("ctime")->select();
  58. $receivables=modify_array($receivables,'ctime','s');
  59. $contactdata=[];
  60. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  61. $contactdata['date'][] = date('Y-m', $time);
  62. $contactdata['num'][] = $contractNums[date('Y-m', $time)]??0;
  63. $contactdata['contract'][] = $contract[date('Y-m', $time)]??0;
  64. $contactdata['receivables'][] = $receivables[date('Y-m', $time)]??0;
  65. }
  66. $where1['owner_staff_id'] =array('in',$staff_id);
  67. $start = strtotime(date('Y-m-d 00:00:00'));
  68. $end = strtotime(date('Y-m-d 23:59:59'));
  69. $where2['createtime']= array(array('egt',$start),array('elt',$end));
  70. //今日新增客户
  71. $todaycontomer = Customer::where(['owner_staff_id'=>['in',$staff_id]])->where($where2)->count();
  72. //今日跟进记录
  73. $todayrecord=Record::where(['create_staff_id'=>$where1['owner_staff_id']])->where($where2)->count();
  74. //今日新增线索
  75. $todaylead = Leads::where($where1)->where($where2)->count();
  76. //今日新增公海
  77. $todaysea = Customer::where('owner_staff_id is null or owner_staff_id =0')->where($where2)->count();
  78. //今日新增合同
  79. $todaycontract = Contract::where($where1)->where($where2)->count();
  80. //今日新增回款
  81. $todayreceivables = Receivables::where($where1)->where($where2)->sum('money');
  82. //今日已审批
  83. $todayexamine = ExamineRecord::where(array('check_staff_id'=>['in',$staff_id],'status'=>1))->where($where2)->count();
  84. //今日待审批
  85. $todaywork =ExamineRecord::where(array('check_staff_id'=>['in',$staff_id],'status'=>0))->where($where2)->count();
  86. //个人统计
  87. $Persontime = strtotime(date('Y-m', strtotime('-1 year')));
  88. $Personauth = Staff::info();
  89. $Personstaff_id = $Personauth->id; //权限
  90. $Personcustomers = Customer::where([
  91. 'owner_staff_id'=>$Personstaff_id,
  92. 'createtime' => [
  93. 'egt',
  94. $Persontime
  95. ]
  96. ])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,source,count(*) as c")->group('source,ctime')->select();
  97. $Personselect = [];
  98. foreach ($Personcustomers as $v) {
  99. $Personselect[$v['source']][$v['ctime']] = $v['c'];
  100. }
  101. $PersontoMonthTime = strtotime(date('Y-m'));
  102. $Personsourcedata = ['客户来源' => ['客户来源']];
  103. for ($Persontime; $Persontime <= $PersontoMonthTime; $Persontime = strtotime('+1 month', $Persontime)) {
  104. $Personsourcedata['客户来源'][] = date('Y-m', $Persontime);
  105. foreach ($sources as $v) {
  106. if (empty($Personsourcedata[$v])) {
  107. $Personsourcedata[$v][] = $v;
  108. }
  109. $Personsourcedata[$v][] = intval($Personselect[$v][date('Y-m', $Persontime)] ?? 0);
  110. }
  111. }
  112. //合同金额统计
  113. $Persontime = strtotime(date('Y-m', strtotime('-1 year')));
  114. $PersoncontractNums=Contract::where(['order_staff_id'=>$Personstaff_id,'check_status' => 2,'createtime' => ['egt',$Persontime]])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,count(*) as c")->group('ctime')->select();
  115. $PersoncontractNums=modify_array($PersoncontractNums,'ctime','c');
  116. $Personcontract=Contract::where(['order_staff_id'=>$Personstaff_id,'check_status' => 2,'createtime' => ['egt',$Persontime]])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,sum(money) as s")->group('ctime')->select();
  117. $Personcontract=modify_array($Personcontract,'ctime','s');
  118. //回款
  119. $Personreceivables=Receivables::where(['owner_staff_id'=>$Personstaff_id,'check_status'=>2,'createtime' => ['egt',$Persontime]])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,sum(money) as s")->group("ctime")->select();
  120. $Personreceivables=modify_array($Personreceivables,'ctime','s');
  121. $Personcontactdata=[];
  122. for ($Persontime; $Persontime <= $PersontoMonthTime; $Persontime = strtotime('+1 month', $Persontime)) {
  123. $Personcontactdata['date'][] = date('Y-m', $Persontime);
  124. $Personcontactdata['num'][] = $PersoncontractNums[date('Y-m', $Persontime)]??0;
  125. $Personcontactdata['contract'][] = $Personcontract[date('Y-m', $Persontime)]??0;
  126. $Personcontactdata['receivables'][] = $Personreceivables[date('Y-m', $Persontime)]??0;
  127. }
  128. $Personwhere1['owner_staff_id'] =$Personstaff_id;
  129. $Personwhere2['createtime']= array(array('egt',$start),array('elt',$end));
  130. //今日新增客户
  131. $Persontodaycontomer = Customer::where(['owner_staff_id'=>$Personstaff_id])->where($Personwhere2)->count();
  132. //今日跟进记录
  133. $Persontodayrecord=Record::where(['create_staff_id'=>$Personstaff_id])->where($Personwhere2)->count();
  134. //今日新增线索
  135. $Persontodaylead = Leads::where($Personwhere1)->where($Personwhere2)->count();
  136. //今日新增公海
  137. $Persontodaysea = Customer::where('owner_staff_id is null or owner_staff_id =0')->where($Personwhere2)->count();
  138. //今日新增合同
  139. $Persontodaycontract = Contract::where($Personwhere1)->where($Personwhere2)->count();
  140. //今日新增回款
  141. $Persontodayreceivables = Receivables::where($Personwhere1)->where($Personwhere2)->sum('money');
  142. //今日已审批
  143. $Persontodayexamine = ExamineRecord::where(array('check_staff_id'=>$Personstaff_id,'status'=>1))->where($Personwhere2)->count();
  144. //今日待审批
  145. $Persontodaywork =ExamineRecord::where(array('check_staff_id'=>$Personstaff_id,'status'=>0))->where($Personwhere2)->count();
  146. $this->view->assign([
  147. 'sourcedata' => array_values($sourcedata),
  148. 'contactdata' => $contactdata,
  149. 'todaycontomer' =>$todaycontomer,
  150. 'todayrecord' =>$todayrecord,
  151. 'todaylead' =>$todaylead,
  152. 'todaysea' =>$todaysea,
  153. 'todaycontract' =>$todaycontract,
  154. 'todayreceivables' =>$todayreceivables,
  155. 'todayexamine' =>$todayexamine,
  156. 'todaywork' =>$todaywork,
  157. 'Personsourcedata' => array_values($Personsourcedata),
  158. 'Personcontactdata' => $Personcontactdata,
  159. 'Persontodaycontomer' =>$Persontodaycontomer,
  160. 'Persontodayrecord' =>$Persontodayrecord,
  161. 'Persontodaylead' =>$Persontodaylead,
  162. 'Persontodaysea' =>$Persontodaysea,
  163. 'Persontodaycontract' =>$Persontodaycontract,
  164. 'Persontodayreceivables' =>$Persontodayreceivables,
  165. 'Persontodayexamine' =>$Persontodayexamine,
  166. 'Persontodaywork' =>$Persontodaywork,
  167. ]);
  168. return $this->view->fetch();
  169. }
  170. }