Statistics.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Achievement;
  4. use addons\qingdong\model\Achievement as AchievementModel;
  5. use addons\qingdong\model\ContractRatio;
  6. use addons\qingdong\model\Receivables;
  7. use addons\qingdong\model\Contract;
  8. use addons\qingdong\model\Leads;
  9. use addons\qingdong\model\Customer;
  10. use addons\qingdong\model\ReceivablesPlan;
  11. use addons\qingdong\model\Record;
  12. use addons\qingdong\model\Staff;
  13. /**
  14. * 统计接口
  15. */
  16. class Statistics extends StaffApi {
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. /**
  20. *成交排行
  21. */
  22. public function contractRanding() {
  23. // $date = input('date', date('Y-m'));
  24. $type = input('type', 0);//0 本人及下属 1 仅本人 2 仅下属 3公司
  25. $status=input('status',1);//1 合同 2 回款
  26. $times=input('times','');
  27. $times=explode(',',$times);
  28. if(empty($times)){
  29. $this->error('参数不能为空');
  30. }
  31. $startDate=date("Y-m",strtotime($times[0]));
  32. $endDate=date("Y-m",strtotime($times[1]));
  33. $between=[$times[0],$times[1]];
  34. if($status == 1){
  35. $contracts = Contract::where([
  36. 'order_date' => ['between', $between],
  37. 'check_status' => 2,
  38. ])->group('owner_staff_id')->field('owner_staff_id,sum(money) as money')->order('money desc')->select();
  39. $list = [];
  40. foreach ($contracts as $v) {
  41. $list[$v['owner_staff_id']] = $v['money'];
  42. }
  43. }else{
  44. $receivables = Receivables::where([
  45. 'return_time' => ['between', $between],
  46. 'check_status' => 2,
  47. ])->group('owner_staff_id')->field('owner_staff_id,sum(money) as money')->order('money desc')->select();
  48. $list = [];
  49. foreach ($receivables as $v) {
  50. $list[$v['owner_staff_id']] = $v['money'];
  51. }
  52. }
  53. $data = [];
  54. $staffs = Staff::getList();
  55. foreach ($staffs as $v) {
  56. if (isset($list[$v['id']])) {
  57. $data[$v['id']] = $list[$v['id']];
  58. } else {
  59. $data[$v['id']] = 0;
  60. }
  61. }
  62. arsort($data);
  63. $staffs = Staff::getKeyList();
  64. $result = [];
  65. $i = 1;
  66. $oneMoney = 0;
  67. if ($type == 1) {//本人
  68. $showStaffIds = [$this->auth->id];
  69. } elseif ($type == 2) {//下属
  70. $showStaffIds = Staff::getLowerStaffId();
  71. } elseif ($type == 3) {//公司
  72. $showStaffIds = Staff::getLowerStaffId();
  73. } else {//全部
  74. $showStaffIds = Staff::getMyStaffIds();
  75. }
  76. foreach ($data as $id => $money) {
  77. if ($i == 1) {
  78. $oneMoney = $money;
  79. }
  80. $val = $staffs[$id];
  81. $val['money'] = $money;
  82. $val['ratio'] = ($money && $oneMoney) ? sprintf("%.2f", $money / $oneMoney * 100) : 0;
  83. $val['rank'] = $i;
  84. $i++;
  85. if(in_array($id,$showStaffIds)){
  86. $result[] = $val;
  87. }
  88. }
  89. if (count($result) >= 10) {
  90. $top = array_slice($result, 0, 3);
  91. $bottom = array_slice($result, -3, 3);
  92. $middle = array_slice($result, 3, 4);
  93. $result = array_merge($top, $bottom, $middle);
  94. }
  95. if ($type == 1) {//本人
  96. $showStaffIds = [$this->auth->id];
  97. //业绩目标完成情况
  98. $data = Achievement::getStaffAchievement($startDate,$endDate,$status);
  99. } elseif ($type == 2) {//下属
  100. $showStaffIds = Staff::getLowerStaffId();
  101. //业绩目标完成情况
  102. $data = Achievement::getStaffAchievement($startDate,$endDate,$status);
  103. } elseif ($type == 3) {//公司
  104. $showStaffIds = Staff::getLowerStaffId();
  105. //业绩目标完成情况
  106. $data = Achievement::getCompanyAchievement($startDate,$endDate,$status);
  107. } else {//全部
  108. $showStaffIds = Staff::getMyStaffIds();
  109. //业绩目标完成情况
  110. $data = Achievement::getStaffAchievement($startDate,$endDate,$status);
  111. }
  112. $achievement=$data['achievement']??0;
  113. if($status == 1){
  114. $totalMoneys = Contract::where([
  115. 'order_date' => ['between', $between],
  116. 'check_status' => 2,
  117. 'owner_staff_id' => ['in',$showStaffIds],
  118. ])->sum('money');
  119. }else{
  120. $totalMoneys = Receivables::where([
  121. 'return_time' => ['between', $between],
  122. 'check_status' => 2,
  123. 'owner_staff_id' => ['in',$showStaffIds],
  124. ])->sum('money');
  125. }
  126. $ratio = ($totalMoneys && intval($achievement)) ? (intval($achievement) == 0) ? 100 : round($totalMoneys / $achievement * 100, 2) : 0;
  127. $data = [
  128. 'achievement' => $achievement,
  129. 'contract_moneys' => $totalMoneys,
  130. 'completion_status' => ($ratio >= 100) ? 1 : 0,//完成状态
  131. 'ratio' => $ratio,
  132. 'ranking' => $result
  133. ];
  134. $this->success('请求成功', $data);
  135. }
  136. /**
  137. * 合同排行 机型排行
  138. */
  139. public function contractRanking() {
  140. $date = input('date', date('Y-m'));
  141. $type = input('type', 0);//0 本人及下属 1 仅本人 2 仅下属
  142. //月底
  143. $endDate = date('Y-m-d', strtotime('+1 month', strtotime($date . '-1')) - 1);
  144. $contracts = Contract::where([
  145. 'order_date' => ['between', [$date . '-1', $endDate]],
  146. 'check_status' => 2,
  147. ])->group('owner_staff_id')->field('owner_staff_id,count(*) as contract_number')->order('contract_number desc')->select();
  148. $list = [];
  149. foreach ($contracts as $v) {
  150. $list[$v['owner_staff_id']] = $v['contract_number'];
  151. }
  152. $contracts = $list;
  153. $data = [];
  154. $staffs = Staff::getList();
  155. foreach ($staffs as $v) {
  156. if (isset($contracts[$v['id']])) {
  157. $data[$v['id']] = $contracts[$v['id']];
  158. } else {
  159. $data[$v['id']] = 0;
  160. }
  161. }
  162. arsort($data);
  163. $staffs = Staff::getKeyList();
  164. $result = [];
  165. $i = 1;
  166. $oneNumber = 0;
  167. if ($type == 1) {//本人
  168. $showStaffIds = [$this->auth->id];
  169. } elseif ($type == 2) {//下属
  170. $showStaffIds = Staff::getLowerStaffId();
  171. } else {//全部
  172. $showStaffIds = Staff::getMyStaffIds();
  173. }
  174. foreach ($data as $id => $number) {
  175. if ($i == 1) {
  176. $oneNumber = $number;
  177. }
  178. $val = $staffs[$id];
  179. $val['number'] = $number;
  180. $val['ratio'] = $oneNumber ? sprintf("%.2f", $number / $oneNumber * 100) : 0;
  181. $val['rank'] = $i;
  182. $i++;
  183. if(in_array($id,$showStaffIds)){
  184. $result[] = $val;
  185. }
  186. }
  187. if (count($result) >= 10) {
  188. $top = array_slice($result, 0, 3);
  189. $bottom = array_slice($result, -3, 3);
  190. $middle = array_slice($result, 3, 4);
  191. $result = array_merge($top, $bottom, $middle);
  192. }
  193. $this->success('请求成功', $result);
  194. }
  195. /**
  196. * 回款统计
  197. */
  198. public function receivablesStatistics() {
  199. $type = input('type',0); //0:全部 1:我负责的 2:下属负责的
  200. switch($type){
  201. case 1:
  202. $wheres['owner_staff_id'] = $this->auth->id;
  203. break;
  204. case 2:
  205. $wheres['owner_staff_id'] = array('in',Staff::getLowerStaffId());
  206. break;
  207. default:
  208. $wheres['owner_staff_id'] = array('in',Staff::getMyStaffIds());
  209. break;
  210. }
  211. $contract_moneys = Contract::where(['owner_staff_id' => $this->auth->id, 'check_status' => 2])->sum('money');
  212. $receivables_moneys = Receivables::where($wheres)->where([
  213. 'check_status' => 2,
  214. ])->sum('money');
  215. $plan_moneys = ReceivablesPlan::where($wheres)->where([
  216. 'status' => 0,
  217. ])->sum('money');
  218. $no_moneys = $contract_moneys - $receivables_moneys;
  219. $startDate = date('Y-01');
  220. $endDate = date('Y-m');
  221. $plans = ReceivablesPlan::where($wheres)->group('dates')->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_date),"%Y-%m") as dates')->select();
  222. $plans_list = [];
  223. foreach ($plans as $item) {
  224. $plans_list[$item['dates']] = $item['money'];
  225. }
  226. $receivables = Receivables::where($wheres)->group('dates')->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"%Y-%m") as dates')->select();
  227. $receivables_list = [];
  228. foreach ($receivables as $item) {
  229. $receivables_list[$item['dates']] = $item['money'];
  230. }
  231. $data = [];
  232. for ($startDate; strtotime($startDate) <= strtotime($endDate); $startDate = date('Y-m', strtotime($startDate . ' +1 month'))) {
  233. $row = [
  234. 'date' => $startDate,
  235. 'plan' => $plans_list[$startDate] ?? 0,
  236. 'receivables' => $receivables_list[$startDate] ?? 0,
  237. ];
  238. $row['no'] = ($row['plan'] - $row['receivables']) > 0 ? $row['plan'] - $row['receivables'] : 0;
  239. $data[] = $row;
  240. }
  241. $this->success('请求成功', [
  242. 'contract_moneys' => $contract_moneys,
  243. 'receivables_moneys' => $receivables_moneys,
  244. 'plan_moneys' => $plan_moneys,
  245. 'no_moneys' => $no_moneys,
  246. 'data' => $data
  247. ]);
  248. }
  249. /**
  250. * 所属员工回款统计
  251. */
  252. public function staffReceivablesStatistics() {
  253. $ids = Staff::getMyStaffIds();
  254. $contract_moneys = Contract::where([
  255. 'owner_staff_id' => ['in', $ids],
  256. 'check_status' => 2,
  257. ])->sum('money');
  258. $receivables_moneys = Receivables::where([
  259. 'owner_staff_id' => ['in', $ids],
  260. 'check_status' => 2,
  261. ])->sum('money');
  262. $plan_moneys = ReceivablesPlan::where([
  263. 'owner_staff_id' => ['in', $ids],
  264. 'status' => 0,
  265. ])->sum('money');
  266. $no_moneys = $contract_moneys - $receivables_moneys;
  267. $contracts = Contract::where([
  268. 'owner_staff_id' => [
  269. 'in',
  270. $ids
  271. ],
  272. 'check_status' => 2,
  273. ])->group('owner_staff_id')->field('sum(money) as money,owner_staff_id')->select();
  274. $contracts_list = [];
  275. foreach ($contracts as $item) {
  276. $contracts_list[$item['owner_staff_id']] = $item['money'];
  277. }
  278. $plans = ReceivablesPlan::where([
  279. 'owner_staff_id' => [
  280. 'in',
  281. $ids
  282. ],
  283. 'status' => 0,
  284. ])->group('owner_staff_id')->field('sum(money) as money,owner_staff_id')->select();
  285. $plans_list = [];
  286. foreach ($plans as $item) {
  287. $plans_list[$item['owner_staff_id']] = $item['money'];
  288. }
  289. $receivables = Receivables::where([
  290. 'owner_staff_id' => [
  291. 'in',
  292. $ids
  293. ],
  294. 'check_status' => 2,
  295. ])->group('owner_staff_id')->field('sum(money) as money,owner_staff_id')->select();
  296. $receivables_list = [];
  297. foreach ($receivables as $item) {
  298. $receivables_list[$item['owner_staff_id']] = $item['money'];
  299. }
  300. $data = [];
  301. $ids = Staff::getMyStaffIds();
  302. $staffs = Staff::where(['id' => ['in', $ids]])->field('id,name')->select();
  303. foreach ($staffs as $v) {
  304. $row = [
  305. 'id' => $v['id'],
  306. 'name' => $v['name'],
  307. 'contracts' => $contracts_list[$v['id']] ?? 0,
  308. 'plan' => $plans_list[$v['id']] ?? 0,
  309. 'receivables' => $receivables_list[$v['id']] ?? 0,
  310. ];
  311. $row['no'] = ($row['contracts'] - $row['receivables']) > 0 ? $row['contracts'] - $row['receivables'] : 0;
  312. $data[] = $row;
  313. }
  314. $this->success('请求成功', [
  315. 'contract_moneys' => $contract_moneys,
  316. 'receivables_moneys' => $receivables_moneys,
  317. 'plan_moneys' => $plan_moneys,
  318. 'no_moneys' => $no_moneys,
  319. 'data' => $data
  320. ]);
  321. }
  322. /**
  323. * 我的业绩目标完成度
  324. */
  325. public function staffAchievementStatistics() {
  326. $year = input('year', date('Y'));
  327. $type = input('type', 0);
  328. if ($type == 1) {//本人
  329. $showStaffIds = [$this->auth->id];
  330. $yearAchievement = Achievement::getStaffYearAchievement($year);
  331. } elseif ($type == 2) {//下属
  332. $showStaffIds = Staff::getLowerStaffId();
  333. $yearAchievement = Achievement::getTeamYearAchievement($year);
  334. } elseif ($type == 3) {//公司
  335. $showStaffIds = Staff::getLowerStaffId();
  336. $yearAchievement = Achievement::getCompanyYearAchievement($year);
  337. } else {//全部
  338. $showStaffIds = Staff::getMyStaffIds();
  339. $yearAchievement = Achievement::getTeamYearAchievement($year);
  340. }
  341. $contracts=ContractRatio::where([
  342. 'contract.check_status'=>2, 'contract_ratio.staff_id' => ['in',$showStaffIds],
  343. 'contract.order_date'=>['like', $year . '%']
  344. ])->with(['contract'])->select();
  345. $contracts=collection($contracts)->toArray();
  346. $contractData=[];
  347. foreach ($contracts as $v) {
  348. $order_date = $v['contract']['order_date'];
  349. $month = date('Y-m', strtotime($order_date));
  350. $contractData[$month]['money'][] = $v['ratio_money'];
  351. $contractData[$month]['contract_id'][] = $v['contract_id'];
  352. }
  353. $contracts_list = [];
  354. foreach ($contractData as $month=>$v) {
  355. $contracts_list[$month] = ['money'=>array_sum($v['money']),'count'=>count($v['contract_id'])];
  356. }
  357. $data = [];
  358. foreach ($yearAchievement as $k => $v) {
  359. if ($month = Achievement::getFieldMonth($k)) {
  360. $month = $year . '-' . $month;
  361. $row = [
  362. 'month' => $month,
  363. 'achievement' => $v,
  364. 'money' => isset($contracts_list[$month]) ? $contracts_list[$month]['money'] : 0,
  365. 'count' => isset($contracts_list[$month]) ? $contracts_list[$month]['count'] : 0,
  366. ];
  367. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  368. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  369. $data[] = $row;
  370. }
  371. }
  372. $this->success('请求成功', $data);
  373. }
  374. /**
  375. * 回款数据排行
  376. */
  377. public function receivablesRanking() {
  378. $year = input('year', date('Y'));
  379. $type = input('type', 0);
  380. if ($type == 1) {//本人
  381. $showStaffIds = [$this->auth->id];
  382. } elseif ($type == 2) {//下属
  383. $showStaffIds = Staff::getLowerStaffId();
  384. } else {//全部
  385. $showStaffIds = Staff::getMyStaffIds();
  386. }
  387. $receivables = Receivables::where([
  388. 'check_status' => 2,
  389. 'return_time' => ['like', $year . '%'],
  390. ])->group('owner_staff_id')->with(['staff'])->order('money desc')->field('owner_staff_id,sum(money) as money,count(*) as count')->select();
  391. $receivablesData = [];
  392. $list = [];
  393. $i=1;
  394. foreach ($receivables as $k => $v) {
  395. $v['ranking'] = $i;
  396. if(in_array($v['owner_staff_id'],$showStaffIds)){
  397. $i++;
  398. $receivablesData[] = $v;
  399. }
  400. $list[$v['owner_staff_id']] = $v['money'];
  401. }
  402. $contracts = $list;
  403. $data = [];
  404. $staffs = Staff::getList();
  405. foreach ($staffs as $v) {
  406. if (isset($contracts[$v['id']])) {
  407. $data[$v['id']] = $contracts[$v['id']];
  408. } else {
  409. $data[$v['id']] = 0;
  410. }
  411. }
  412. arsort($data);
  413. $staffs = Staff::getKeyList();
  414. $result = [];
  415. $i = 1;
  416. $oneNumber = 0;
  417. foreach ($data as $id => $number) {
  418. if ($i == 1) {
  419. $oneNumber = $number;
  420. }
  421. $val = $staffs[$id];
  422. $val['number'] = $number;
  423. $val['ratio'] = $oneNumber ? sprintf("%.2f", $number / $oneNumber * 100) : 0;
  424. $val['rank'] = $i;
  425. if(in_array($id,$showStaffIds)){
  426. $result[] = $val;
  427. $i++;
  428. }
  429. }
  430. if (count($result) >= 10) {
  431. $top = array_slice($result, 0, 3);
  432. $bottom = array_slice($result, -3, 3);
  433. $middle = array_slice($result, 3, 4);
  434. $result = array_merge($top, $bottom, $middle);
  435. }
  436. $this->success('请求成功', ['ranking' => $result, 'data' => $receivablesData]);
  437. }
  438. /**
  439. * 成交数据排行
  440. */
  441. public function contractMoneyRanking() {
  442. $date = input('year', date('Y'));
  443. $type = input('type', 0);
  444. if ($type == 1) {//本人
  445. $showStaffIds = [$this->auth->id];
  446. } elseif ($type == 2) {//下属
  447. $showStaffIds = Staff::getLowerStaffId();
  448. } else {//全部
  449. $showStaffIds = Staff::getMyStaffIds();
  450. }
  451. $contracts=ContractRatio::where([
  452. 'contract.check_status'=>2, 'contract_ratio.staff_id' => ['in',$showStaffIds],
  453. 'contract.order_date'=>['like', $date . '%']
  454. ])->with(['contract','staff'])->select();
  455. $contracts=collection($contracts)->toArray();
  456. $contractData=[];
  457. foreach ($contracts as $v) {
  458. $contractData[$v['staff_id']]['money'][] = $v['ratio_money'];
  459. $contractData[$v['staff_id']]['contract_id'][] = $v['contract_id'];
  460. $contractData[$v['staff_id']]['staff'] = $v['staff'];
  461. $contractData[$v['staff_id']]['staff_id'] = $v['staff_id'];
  462. }
  463. $receivables = [];
  464. foreach ($contractData as $v) {
  465. $receivables[] = [
  466. 'money'=>array_sum($v['money']),
  467. 'count'=>count($v['contract_id']),
  468. 'staff'=>$v['staff'],
  469. 'owner_staff_id'=>$v['staff_id'],
  470. ];
  471. }
  472. $this->arraySortByOneField($receivables,'money',SORT_DESC);
  473. $receivablesData = [];
  474. $list = [];
  475. foreach ($receivables as $k => $v) {
  476. $v['ranking'] = $k + 1;
  477. $v['unit_price'] = ($v['money'] && $v['count']) ? sprintf("%.2f", $v['money'] / $v['count']) : 0;
  478. if(in_array($v['owner_staff_id'],$showStaffIds)){
  479. $receivablesData[] = $v;
  480. }
  481. $list[$v['owner_staff_id']] = $v['money'];
  482. }
  483. $contracts = $list;
  484. $data = [];
  485. $staffs = Staff::getList();
  486. foreach ($staffs as $v) {
  487. if (isset($contracts[$v['id']])) {
  488. $data[$v['id']] = $contracts[$v['id']];
  489. } else {
  490. $data[$v['id']] = 0;
  491. }
  492. }
  493. arsort($data);
  494. $staffs = Staff::getKeyList();
  495. $result = [];
  496. $i = 1;
  497. $oneNumber = 0;
  498. foreach ($data as $id => $number) {
  499. if ($i == 1) {
  500. $oneNumber = $number;
  501. }
  502. $val = $staffs[$id];
  503. $val['number'] = $number;
  504. $val['ratio'] = $oneNumber ? sprintf("%.2f", $number / $oneNumber * 100) : 0;
  505. $val['rank'] = $i;
  506. $i++;
  507. if(in_array($id,$showStaffIds)){
  508. $result[] = $val;
  509. }
  510. }
  511. if (count($result) >= 10) {
  512. $top = array_slice($result, 0, 3);
  513. $bottom = array_slice($result, -3, 3);
  514. $middle = array_slice($result, 3, 4);
  515. $result = array_merge($top, $bottom, $middle);
  516. }
  517. $this->success('请求成功', ['ranking' => $result, 'data' => $receivablesData]);
  518. }
  519. /**
  520. * 新增排行
  521. */
  522. public function newRanking() {
  523. $year = input('date', date('Y'));
  524. $between = [strtotime($year . '-01-01'), strtotime($year.'-1-1 +1 year')];
  525. $betweenC = [date('Y-m-d 00:00:00',strtotime($year . '-01-01')), date('Y-m-d 23:59:59',strtotime($year.'-1-1 +1 year'))];
  526. $customers = Customer::where([
  527. 'createtime' => [
  528. 'between',
  529. $between
  530. ],
  531. 'owner_staff_id' => $this->auth->id
  532. ])->group('month')->field("FROM_UNIXTIME(createtime,'%Y-%m') as month,count(*) as count")->select();
  533. $customers_list = [];
  534. foreach ($customers as $v) {
  535. $customers_list[$v['month']] = $v['count'];
  536. }
  537. $contracts = Contract::where([
  538. 'order_date' => [
  539. 'between',
  540. $betweenC
  541. ],
  542. 'owner_staff_id' => $this->auth->id,
  543. 'check_status'=>2
  544. ])->group('month')->field('FROM_UNIXTIME(createtime,"%Y-%m") as month,count(*) as count')->select();
  545. $contracts_list = [];
  546. foreach ($contracts as $v) {
  547. $contracts_list[$v['month']] = $v['count'];
  548. }
  549. $leads = Leads::where([
  550. 'createtime' => [
  551. 'between',
  552. $between
  553. ],
  554. 'owner_staff_id' => $this->auth->id
  555. ])->group('month')->field('FROM_UNIXTIME(createtime,"%Y-%m") as month,count(*) as count')->select();
  556. $leads_list = [];
  557. foreach ($leads as $v) {
  558. $leads_list[$v['month']] = $v['count'];
  559. }
  560. $data = [];
  561. for ($i = 1; $i <= 12; $i++) {
  562. $month = date('Y-m', strtotime($year . '-' . $i));
  563. $data[] = [
  564. 'month' => $month,
  565. 'customers' => $customers_list[$month] ?? 0,
  566. 'contracts' => $contracts_list[$month] ?? 0,
  567. 'leads' => $leads_list[$month] ?? 0,
  568. ];
  569. }
  570. $this->success('请求成功', $data);
  571. }
  572. /**
  573. * 获取团队新增统计
  574. */
  575. public function addCustomerStatistics() {
  576. $date = input('date', date('Y-m'));
  577. if(strlen($date) == 4){
  578. $between = [strtotime($date . '-01-01'), strtotime($date.'-1-1 +1 year') - 1];
  579. }else{
  580. $between = [strtotime($date), strtotime('+1 month', strtotime($date)) - 1];
  581. }
  582. $ids = Staff::getMyStaffIds();
  583. $staffs = Staff::where(['id' => ['in', $ids]])->field('id,name,post,img')->select();
  584. $c = Customer::where([
  585. 'createtime' => [
  586. 'between',
  587. $between
  588. ],
  589. 'owner_staff_id' => ['in', $ids]
  590. ])->group('owner_staff_id')->order('count desc')->field("owner_staff_id,count(*) as count")->select();
  591. $customers = [];
  592. foreach ($c as $v) {
  593. $customers[$v['owner_staff_id']] = $v['count'];
  594. }
  595. $l = Leads::where([
  596. 'createtime' => [
  597. 'between',
  598. $between
  599. ],
  600. 'owner_staff_id' => ['in', $ids]
  601. ])->group('owner_staff_id')->field("owner_staff_id,count(*) as count")->select();
  602. $leads = [];
  603. foreach ($l as $v) {
  604. $leads[$v['owner_staff_id']] = $v['count'];
  605. }
  606. $t = Contract::where([
  607. 'createtime' => [
  608. 'between',
  609. $between
  610. ],
  611. 'owner_staff_id' => ['in', $ids]
  612. ])->group('owner_staff_id')->field("owner_staff_id,count(*) as count")->select();
  613. $contracts = [];
  614. foreach ($t as $v) {
  615. $contracts[$v['owner_staff_id']] = $v['count'];
  616. }
  617. $total=[];
  618. $total[0]=[
  619. 'name'=>'数据汇总',
  620. 'id'=>0,
  621. 'leads'=>0,
  622. 'customers'=>0,
  623. 'contracts'=>0,
  624. ];
  625. $sort=[];
  626. foreach ($staffs as $k => $v) {
  627. $v['leads'] = $leads[$v['id']] ?? 0;
  628. $v['customers'] = $customers[$v['id']] ?? 0;
  629. $v['contracts'] = $contracts[$v['id']] ?? 0;
  630. $sort[$v['id']] = $v['leads'] + $v['customers'] + $v['contracts'];
  631. $total[0]['leads'] += $v['leads'];
  632. $total[0]['customers'] += $v['customers'];
  633. $total[0]['contracts'] += $v['contracts'];
  634. $staffs[$v['id']] = $v;
  635. }
  636. arsort($sort);
  637. $result=[];
  638. foreach ($sort as $id=>$v){
  639. $result[]=$staffs[$id];
  640. }
  641. $staffs=array_merge($total,$result);
  642. $this->success('请求成功',$staffs);
  643. }
  644. /**
  645. * 客户新增排行
  646. */
  647. public function newCustomer() {
  648. $date = input('date', date('Y-m'));
  649. $type = input('type', 0);
  650. if(strlen($date) == 4){
  651. $between = [strtotime($date . '-01-01'), strtotime($date.'-1-1 +1 year') - 1];
  652. }else{
  653. $between = [strtotime($date), strtotime('+1 month', strtotime($date)) - 1];
  654. }
  655. if ($type == 1) {//本人
  656. $ids = [$this->auth->id];
  657. } elseif ($type == 2) {//下属
  658. $ids = Staff::getLowerStaffId();
  659. } else {//全部
  660. $ids = Staff::getMyStaffIds();
  661. }
  662. $c = Customer::where([
  663. 'createtime' => [
  664. 'between',
  665. $between
  666. ],
  667. 'owner_staff_id' => ['in', $ids]
  668. ])->group('owner_staff_id')->order('count desc')->field("owner_staff_id,count(*) as count")->select();
  669. $customers = [];
  670. foreach ($c as $v) {
  671. $customers[$v['owner_staff_id']] = $v['count'];
  672. }
  673. arsort($customers);
  674. $staffs = Staff::getKeyList();
  675. $result = [];
  676. $i = 1;
  677. $data=[];
  678. foreach ($customers as $id => $number) {
  679. $val = $staffs[$id];
  680. $val['number'] = $number;
  681. $val['rank'] = $i;
  682. $data[]=[
  683. 'name'=>$val['name'],
  684. 'number'=>$number
  685. ];
  686. $i++;
  687. $result[] = $val;
  688. }
  689. $this->success('请求成功',['ranking' => $result, 'data' => $data]);
  690. }
  691. /**
  692. * 客户跟进排行
  693. */
  694. public function newRecord() {
  695. $date = input('date', date('Y-m'));
  696. $type = input('type', 0);
  697. if(strlen($date) == 4){
  698. $between = [strtotime($date . '-01-01'), strtotime($date.'-1-1 +1 year') - 1];
  699. }else{
  700. $between = [strtotime($date), strtotime('+1 month', strtotime($date)) - 1];
  701. }
  702. if ($type == 1) {//本人
  703. $ids = [$this->auth->id];
  704. } elseif ($type == 2) {//下属
  705. $ids = Staff::getLowerStaffId();
  706. } else {//全部
  707. $ids = Staff::getMyStaffIds();
  708. }
  709. //跟进次数
  710. $r= Record::where([
  711. 'create_staff_id' => ['in',$ids],
  712. 'relation_type' =>1,
  713. 'createtime' => [
  714. 'between',
  715. $between
  716. ]
  717. ])->field("create_staff_id,count(*) as count")->group('create_staff_id')->select();
  718. $records = [];
  719. foreach ($r as $v) {
  720. $records[$v['create_staff_id']] = $v['count'];
  721. }
  722. arsort($records);
  723. $staffs = Staff::getKeyList();
  724. $result = [];
  725. $data = [];
  726. $i = 1;
  727. foreach ($records as $id => $number) {
  728. $val = $staffs[$id];
  729. $val['number'] = $number;
  730. $val['rank'] = $i;
  731. $data[]=[
  732. 'name'=>$val['name'],
  733. 'number'=>$number
  734. ];
  735. $i++;
  736. $result[] = $val;
  737. }
  738. $this->success('请求成功',['ranking' => $result, 'data' => $data]);
  739. }
  740. /**
  741. * 获取业绩详情
  742. */
  743. public function getAchievementDetail()
  744. {
  745. $year = input('year', date('Y'));
  746. $status = input('status', 1);
  747. $staff_id = input('staff_id', '');
  748. if (empty($staff_id)) {
  749. $this->error('员工不能为空');
  750. }
  751. $ids = $staff_id;
  752. $achievements = AchievementModel::where(['type' => 3, 'obj_id' => ['in', $ids], 'year' => $year, 'status' => $status])
  753. ->field('sum(january) as january,sum(february) as february,sum(march) as march,sum(april) as april,sum(may) as may,sum(june) as june,sum(july) as july,sum(august) as august,sum(september) as september,sum(october) as october,sum(november) as november,sum(december) as december,sum(yeartarget) as yeartarget')->find()->toArray();
  754. if ($status == 1) {//合同金额
  755. $contracts = ContractRatio::where([
  756. 'contract.check_status' => 2, 'contract_ratio.staff_id' => ['in', $ids],
  757. 'contract.order_date' => ['like', $year . '%']
  758. ])->with(['contract'])->select();
  759. $contracts = collection($contracts)->toArray();
  760. $contractData = [];
  761. foreach ($contracts as $v) {
  762. $order_date = $v['contract']['order_date'];
  763. $month = date('Y-m', strtotime($order_date));
  764. $contractData[$month]['money'][] = $v['ratio_money'];
  765. $contractData[$month]['contract_id'][] = $v['contract_id'];
  766. }
  767. $list = [];
  768. foreach ($contractData as $month => $v) {
  769. $list[$month] = ['money' => array_sum($v['money']), 'count' => count($v['contract_id'])];
  770. }
  771. } else {
  772. $receivables = Receivables::where([
  773. 'owner_staff_id' => ['in', $ids],
  774. 'check_status' => 2,
  775. 'return_time' => ['like', $year . '%'],
  776. ])->group('month')
  777. ->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"%Y-%m") as month,count(*) as count')->select();
  778. $list = [];
  779. foreach ($receivables as $v) {
  780. $list[$v['month']] = $v;
  781. }
  782. }
  783. $data = [];
  784. $echartdata = [];
  785. foreach ($achievements as $k => $v) {
  786. if ($month = AchievementModel::getFieldMonth($k)) {
  787. $month = $year . '-' . $month;
  788. $row = [
  789. 'month' => $month,
  790. 'achievement' => $v ?? 0,
  791. 'money' => isset($list[$month]) ? $list[$month]['money'] : 0,
  792. 'count' => isset($list[$month]) ? $list[$month]['count'] : 0,
  793. ];
  794. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  795. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  796. $echartdata['month'][] = $row['month'];
  797. $echartdata['achievement'][] = $row['achievement']??0;
  798. $echartdata['money'][] = $row['money'];
  799. $data[] = $row;
  800. }
  801. }
  802. $money = isset($echartdata['money']) ? array_sum($echartdata['money']) : 0;
  803. $ratio = ($money && intval($achievements['yeartarget'])) ? sprintf("%.2f", $money / $achievements['yeartarget'] * 100) : 0;
  804. $yeartarget = [
  805. 'year' => $year,
  806. 'money' => $money,
  807. 'achievement' => $achievements['yeartarget'] ?? 0,
  808. 'ratio' => $ratio
  809. ];
  810. $this->success('请求成功', ['data' => $data, 'echartdata' => $echartdata, 'yeartarget' => $yeartarget]);
  811. }
  812. /**
  813. * 排序
  814. * @param $data
  815. * @param $field
  816. * @param int $sort
  817. * @return mixed
  818. */
  819. public function arraySortByOneField($data, $field, $sort = SORT_ASC)
  820. {
  821. $field = array_column($data, $field);
  822. array_multisort($field, $sort, $data);
  823. return $data;
  824. }
  825. }