Statistics.php 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Achievement;
  4. use addons\qingdongams\model\AreaManagement;
  5. use addons\qingdongams\model\ConsumeDetail;
  6. use addons\qingdongams\model\ContractRatio;
  7. use addons\qingdongams\model\Field;
  8. use addons\qingdongams\model\FormApproval;
  9. use addons\qingdongams\model\PartsStockReload;
  10. use addons\qingdongams\model\Receivables;
  11. use addons\qingdongams\model\Contract;
  12. use addons\qingdongams\model\Event;
  13. use addons\qingdongams\model\Leads;
  14. use addons\qingdongams\model\Customer;
  15. use addons\qingdongams\model\ReceivablesPlan;
  16. use addons\qingdongams\model\Record;
  17. use addons\qingdongams\model\SeaOperation;
  18. use addons\qingdongams\model\Staff;
  19. use addons\qingdongams\model\Workorder;
  20. use addons\qingdongams\model\StaffSignIn;
  21. use addons\qingdongams\model\Product;
  22. use addons\qingdongams\model\Business;
  23. use think\Exception;
  24. /**
  25. * 统计接口
  26. */
  27. class Statistics extends StaffApi {
  28. protected $noNeedLogin = [];
  29. protected $noNeedRight = [];
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. try{
  34. \think\Db::execute("SET @@sql_mode='';");
  35. }catch (Exception $e){
  36. }
  37. }
  38. /**
  39. *成交排行
  40. */
  41. public function contractRanding() {
  42. $date = input('date', date('Y-m'));
  43. $type = input('type', 0);//0 本人及下属 1 仅本人 2 仅下属 4公司
  44. //月底
  45. $endDate = date('Y-m-d', strtotime('+1 month', strtotime(date($date . '-1'))) - 1);
  46. $contracts = Contract::where([
  47. 'order_date' => ['between', [$date . '-1', $endDate]],
  48. 'check_status' => 2,
  49. ])->group('owner_staff_id')->field('owner_staff_id,sum(money) as money')->order('money desc')->select();
  50. $list = [];
  51. foreach ($contracts as $v) {
  52. $list[$v['owner_staff_id']] = $v['money'];
  53. }
  54. $contracts = $list;
  55. $data = [];
  56. $staffs = Staff::getList();
  57. foreach ($staffs as $v) {
  58. if (isset($contracts[$v['id']])) {
  59. $data[$v['id']] = $contracts[$v['id']];
  60. } else {
  61. $data[$v['id']] = 0;
  62. }
  63. }
  64. arsort($data);
  65. $staffs = Staff::getKeyList();
  66. $result = [];
  67. $i = 1;
  68. $oneMoney = 0;
  69. if ($type == 1) {//本人
  70. $showStaffIds = [$this->auth->id];
  71. } elseif ($type == 2) {//下属
  72. $showStaffIds = Staff::getLowerStaffId();
  73. } elseif ($type == 3) {//公司
  74. $showStaffIds = Staff::getLowerStaffId();
  75. } else {//全部
  76. $showStaffIds = Staff::getMyStaffIds();
  77. }
  78. foreach ($data as $id => $money) {
  79. if ($i == 1) {
  80. $oneMoney = $money;
  81. }
  82. $val = $staffs[$id];
  83. $val['money'] = $money;
  84. $val['ratio'] = ($money && $oneMoney) ? sprintf("%.2f", $money / $oneMoney * 100) : 0;
  85. $val['rank'] = $i;
  86. $i++;
  87. if(in_array($id,$showStaffIds)){
  88. $result[] = $val;
  89. }
  90. }
  91. if (count($result) >= 10) {
  92. $top = array_slice($result, 0, 3);
  93. $bottom = array_slice($result, -3, 3);
  94. $middle = array_slice($result, 3, 4);
  95. $result = array_merge($top, $bottom, $middle);
  96. }
  97. if ($type == 1) {//本人
  98. $showStaffIds = [$this->auth->id];
  99. //业绩目标完成情况
  100. $achievement = Achievement::getStaffAchievement($date);
  101. } elseif ($type == 2) {//下属
  102. $showStaffIds = Staff::getLowerStaffId();
  103. //业绩目标完成情况
  104. $achievement = Achievement::getTeamAchievement($date);
  105. } elseif ($type == 3) {//公司
  106. $showStaffIds = Staff::getLowerStaffId();
  107. //业绩目标完成情况
  108. $achievement = Achievement::getCompanyAchievement($date);
  109. } else {//全部
  110. $showStaffIds = Staff::getMyStaffIds();
  111. //业绩目标完成情况
  112. $achievement = Achievement::getTeamAchievement($date);
  113. }
  114. $contractMoneys = Contract::where([
  115. 'order_date' => ['between', [$date . '-1', $endDate]],
  116. 'check_status' => 2,
  117. 'owner_staff_id' => ['in',$showStaffIds],
  118. ])->sum('money');
  119. $ratio = ($contractMoneys && intval($achievement)) ? round($contractMoneys / $achievement * 100, 2) : 0;
  120. $data = [
  121. 'achievement' => $achievement,
  122. 'contract_moneys' => $contractMoneys,
  123. 'completion_status' => ($ratio >= 100) ? 1 : 0,//完成状态
  124. 'ratio' => $ratio,
  125. 'ranking' => $result
  126. ];
  127. $this->success('请求成功', $data);
  128. }
  129. //合同排行 机型排行
  130. public function contractRanking() {
  131. $date = input('date', date('Y-m'));
  132. $type = input('type', 0);//0 本人及下属 1 仅本人 2 仅下属
  133. //月底
  134. $endDate = date('Y-m-d', strtotime('+1 month', strtotime($date . '-1')) - 1);
  135. $contracts = Contract::where([
  136. 'order_date' => ['between', [$date . '-1', $endDate]],
  137. 'check_status' => 2,
  138. ])->group('owner_staff_id')->field('owner_staff_id,count(*) as contract_number')->order('contract_number desc')->select();
  139. $list = [];
  140. foreach ($contracts as $v) {
  141. $list[$v['owner_staff_id']] = $v['contract_number'];
  142. }
  143. $contracts = $list;
  144. $data = [];
  145. $staffs = Staff::getList();
  146. foreach ($staffs as $v) {
  147. if (isset($contracts[$v['id']])) {
  148. $data[$v['id']] = $contracts[$v['id']];
  149. } else {
  150. $data[$v['id']] = 0;
  151. }
  152. }
  153. arsort($data);
  154. $staffs = Staff::getKeyList();
  155. $result = [];
  156. $i = 1;
  157. $oneNumber = 0;
  158. if ($type == 1) {//本人
  159. $showStaffIds = [$this->auth->id];
  160. } elseif ($type == 2) {//下属
  161. $showStaffIds = Staff::getLowerStaffId();
  162. } else {//全部
  163. $showStaffIds = Staff::getMyStaffIds();
  164. }
  165. foreach ($data as $id => $number) {
  166. if ($i == 1) {
  167. $oneNumber = $number;
  168. }
  169. $val = $staffs[$id];
  170. $val['number'] = $number;
  171. $val['ratio'] = $oneNumber ? sprintf("%.2f", $number / $oneNumber * 100) : 0;
  172. $val['rank'] = $i;
  173. $i++;
  174. if(in_array($id,$showStaffIds)){
  175. $result[] = $val;
  176. }
  177. }
  178. if (count($result) >= 10) {
  179. $top = array_slice($result, 0, 3);
  180. $bottom = array_slice($result, -3, 3);
  181. $middle = array_slice($result, 3, 4);
  182. $result = array_merge($top, $bottom, $middle);
  183. }
  184. $this->success('请求成功', $result);
  185. }
  186. //回款统计
  187. public function receivablesStatistics() {
  188. $contract_moneys = Contract::where(['owner_staff_id' => $this->auth->id, 'check_status' => 2])->sum('money');
  189. $receivables_moneys = Receivables::where([
  190. 'owner_staff_id' => $this->auth->id,
  191. 'check_status' => 2,
  192. ])->sum('money');
  193. $plan_moneys = ReceivablesPlan::where([
  194. 'owner_staff_id' => $this->auth->id,
  195. 'status' => 0,
  196. ])->sum('money');
  197. $no_moneys = $contract_moneys - $receivables_moneys;
  198. $startDate = date('Y-m', strtotime($this->auth->createtime));
  199. $endDate = date('Y-m');
  200. $plans = ReceivablesPlan::where([
  201. 'owner_staff_id' => $this->auth->id,
  202. ])->group('dates')->field('id,sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_date),"%Y-%m") as dates')->select();
  203. $plans_list = [];
  204. foreach ($plans as $item) {
  205. $plans_list[$item['dates']] = $item['money'];
  206. }
  207. $receivables = Receivables::where([
  208. 'owner_staff_id' => $this->auth->id,
  209. ])->group('dates')->field('id,sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"%Y-%m") as dates')->select();
  210. $receivables_list = [];
  211. foreach ($receivables as $item) {
  212. $receivables_list[$item['dates']] = $item['money'];
  213. }
  214. $data = [];
  215. for ($startDate; strtotime($startDate) <= strtotime($endDate); $startDate = date('Y-m', strtotime($startDate . ' +1 month'))) {
  216. $row = [
  217. 'date' => $startDate,
  218. 'plan' => $plans_list[$startDate] ?? 0,
  219. 'receivables' => $receivables_list[$startDate] ?? 0,
  220. ];
  221. $row['no'] = ($row['plan'] - $row['receivables']) > 0 ? $row['plan'] - $row['receivables'] : 0;
  222. $data[] = $row;
  223. }
  224. $this->success('请求成功', [
  225. 'contract_moneys' => $contract_moneys,
  226. 'receivables_moneys' => $receivables_moneys,
  227. 'plan_moneys' => $plan_moneys,
  228. 'no_moneys' => $no_moneys,
  229. 'data' => $data
  230. ]);
  231. }
  232. //所属员工回款统计
  233. public function staffReceivablesStatistics() {
  234. $ids = Staff::getMyStaffIds();
  235. $contract_moneys = Contract::where([
  236. 'owner_staff_id' => ['in', $ids],
  237. 'check_status' => 2,
  238. ])->sum('money');
  239. $receivables_moneys = Receivables::where([
  240. 'owner_staff_id' => ['in', $ids],
  241. 'check_status' => 2,
  242. ])->sum('money');
  243. $plan_moneys = ReceivablesPlan::where([
  244. 'owner_staff_id' => ['in', $ids],
  245. 'status' => 0,
  246. ])->sum('money');
  247. $no_moneys = $contract_moneys - $receivables_moneys;
  248. //$startDate = date('Y-m', $this->auth->createtime);
  249. //$endDate = date('Y-m');
  250. $contracts = Contract::where([
  251. 'owner_staff_id' => [
  252. 'in',
  253. $ids
  254. ],
  255. 'check_status' => 2,
  256. ])->group('owner_staff_id')->field('id,sum(money) as money,owner_staff_id')->select();
  257. $contracts_list = [];
  258. foreach ($contracts as $item) {
  259. $contracts_list[$item['owner_staff_id']] = $item['money'];
  260. }
  261. $plans = ReceivablesPlan::where([
  262. 'owner_staff_id' => [
  263. 'in',
  264. $ids
  265. ],
  266. 'status' => 0,
  267. ])->group('owner_staff_id')->field('id,sum(money) as money,owner_staff_id')->select();
  268. $plans_list = [];
  269. foreach ($plans as $item) {
  270. $plans_list[$item['owner_staff_id']] = $item['money'];
  271. }
  272. $receivables = Receivables::where([
  273. 'owner_staff_id' => [
  274. 'in',
  275. $ids
  276. ],
  277. 'check_status' => 2,
  278. ])->group('owner_staff_id')->field('id,sum(money) as money,owner_staff_id')->select();
  279. $receivables_list = [];
  280. foreach ($receivables as $item) {
  281. $receivables_list[$item['owner_staff_id']] = $item['money'];
  282. }
  283. $data = [];
  284. $ids = Staff::getMyStaffIds();
  285. $staffs = Staff::where(['id' => ['in', $ids],'status'=>1])->field('id,name')->select();
  286. foreach ($staffs as $v) {
  287. $row = [
  288. 'id' => $v['id'],
  289. 'name' => $v['name'],
  290. 'contracts' => $contracts_list[$v['id']] ?? 0,
  291. 'plan' => $plans_list[$v['id']] ?? 0,
  292. 'receivables' => $receivables_list[$v['id']] ?? 0,
  293. ];
  294. $row['no'] = ($row['contracts'] - $row['receivables']) > 0 ? $row['contracts'] - $row['receivables'] : 0;
  295. $data[] = $row;
  296. }
  297. $this->success('请求成功', [
  298. 'contract_moneys' => $contract_moneys,
  299. 'receivables_moneys' => $receivables_moneys,
  300. 'plan_moneys' => $plan_moneys,
  301. 'no_moneys' => $no_moneys,
  302. 'data' => $data
  303. ]);
  304. }
  305. //我的业绩目标完成度
  306. public function staffAchievementStatistics() {
  307. $year = input('year', date('Y'));
  308. $type = input('type', 0);
  309. if ($type == 1) {//本人
  310. $showStaffIds = [$this->auth->id];
  311. $yearAchievement = Achievement::getStaffYearAchievement($year);
  312. } elseif ($type == 2) {//下属
  313. $showStaffIds = Staff::getLowerStaffId();
  314. $yearAchievement = Achievement::getTeamYearAchievement($year);
  315. } elseif ($type == 3) {//公司
  316. $showStaffIds = Staff::getLowerStaffId();
  317. $yearAchievement = Achievement::getCompanyYearAchievement($year);
  318. } else {//全部
  319. $showStaffIds = Staff::getMyStaffIds();
  320. $yearAchievement = Achievement::getTeamYearAchievement($year);
  321. }
  322. $contracts = Contract::where([
  323. 'owner_staff_id' => ['in',$showStaffIds],
  324. 'check_status' => 2,
  325. 'order_date' => ['like', $year . '%'],
  326. ])->group('month')->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),"%Y-%m") as month,count(*) as count')->select();
  327. $contracts_list = [];
  328. foreach ($contracts as $v) {
  329. $contracts_list[$v['month']] = $v;
  330. }
  331. $data = [];
  332. foreach ($yearAchievement as $k => $v) {
  333. if ($month = Achievement::getFieldMonth($k)) {
  334. $month = $year . '-' . $month;
  335. $row = [
  336. 'month' => $month,
  337. 'achievement' => $v,
  338. 'money' => isset($contracts_list[$month]) ? $contracts_list[$month]['money'] : 0,
  339. 'count' => isset($contracts_list[$month]) ? $contracts_list[$month]['count'] : 0,
  340. ];
  341. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  342. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  343. $data[] = $row;
  344. }
  345. }
  346. $this->success('请求成功', $data);
  347. }
  348. //回款数据排行
  349. public function receivablesRanking() {
  350. $year = input('year', date('Y'));
  351. $type = input('type', 0);
  352. if ($type == 1) {//本人
  353. $showStaffIds = [$this->auth->id];
  354. } elseif ($type == 2) {//下属
  355. $showStaffIds = Staff::getLowerStaffId();
  356. } else {//全部
  357. $showStaffIds = Staff::getMyStaffIds();
  358. }
  359. $receivables = Receivables::where([
  360. 'check_status' => 2,
  361. 'return_time' => ['like', $year . '%']
  362. ])->group('owner_staff_id')->with(['staff'])->order('money desc')->field('owner_staff_id,sum(money) as money,count(*) as count')->select();
  363. $receivablesData = [];
  364. $list = [];
  365. foreach ($receivables as $k => $v) {
  366. $v['ranking'] = $k + 1;
  367. if(in_array($v['owner_staff_id'],$showStaffIds)){
  368. $receivablesData[] = $v;
  369. }
  370. $list[$v['owner_staff_id']] = $v['money'];
  371. }
  372. $contracts = $list;
  373. $data = [];
  374. $staffs = Staff::getList();
  375. foreach ($staffs as $v) {
  376. if (isset($contracts[$v['id']])) {
  377. $data[$v['id']] = $contracts[$v['id']];
  378. } else {
  379. $data[$v['id']] = 0;
  380. }
  381. }
  382. arsort($data);
  383. $staffs = Staff::getKeyList();
  384. $result = [];
  385. $i = 1;
  386. $oneNumber = 0;
  387. foreach ($data as $id => $number) {
  388. if ($i == 1) {
  389. $oneNumber = $number;
  390. }
  391. $val = $staffs[$id];
  392. $val['number'] = $number;
  393. $val['ratio'] = $oneNumber ? sprintf("%.2f", $number / $oneNumber * 100) : 0;
  394. $val['rank'] = $i;
  395. $i++;
  396. if(in_array($id,$showStaffIds)){
  397. $result[] = $val;
  398. }
  399. }
  400. if (count($result) >= 10) {
  401. $top = array_slice($result, 0, 3);
  402. $bottom = array_slice($result, -3, 3);
  403. $middle = array_slice($result, 3, 4);
  404. $result = array_merge($top, $bottom, $middle);
  405. }
  406. $this->success('请求成功', ['ranking' => $result, 'data' => $receivablesData]);
  407. }
  408. //成交数据排行
  409. public function contractMoneyRanking() {
  410. $date = input('year', date('Y'));
  411. $type = input('type', 0);
  412. if ($type == 1) {//本人
  413. $showStaffIds = [$this->auth->id];
  414. } elseif ($type == 2) {//下属
  415. $showStaffIds = Staff::getLowerStaffId();
  416. } else {//全部
  417. $showStaffIds = Staff::getMyStaffIds();
  418. }
  419. $receivables = Contract::where([
  420. 'check_status' => 2,
  421. 'order_date' => ['like', $date . '%']
  422. ])->group('owner_staff_id')->with(['staff'])->order('money desc')->field('owner_staff_id,sum(money) as money,count(*) as count')->select();
  423. $receivablesData = [];
  424. $list = [];
  425. foreach ($receivables as $k => $v) {
  426. $v['ranking'] = $k + 1;
  427. $v['unit_price'] = ($v['money'] && $v['count']) ? sprintf("%.2f", $v['money'] / $v['count']) : 0;
  428. if(in_array($v['owner_staff_id'],$showStaffIds)){
  429. $receivablesData[] = $v;
  430. }
  431. $list[$v['owner_staff_id']] = $v['money'];
  432. }
  433. $contracts = $list;
  434. $data = [];
  435. $staffs = Staff::getList();
  436. foreach ($staffs as $v) {
  437. if (isset($contracts[$v['id']])) {
  438. $data[$v['id']] = $contracts[$v['id']];
  439. } else {
  440. $data[$v['id']] = 0;
  441. }
  442. }
  443. arsort($data);
  444. $staffs = Staff::getKeyList();
  445. $result = [];
  446. $i = 1;
  447. $oneNumber = 0;
  448. foreach ($data as $id => $number) {
  449. if ($i == 1) {
  450. $oneNumber = $number;
  451. }
  452. $val = $staffs[$id];
  453. $val['number'] = $number;
  454. $val['ratio'] = $oneNumber ? sprintf("%.2f", $number / $oneNumber * 100) : 0;
  455. $val['rank'] = $i;
  456. $i++;
  457. if(in_array($id,$showStaffIds)){
  458. $result[] = $val;
  459. }
  460. }
  461. if (count($result) >= 10) {
  462. $top = array_slice($result, 0, 3);
  463. $bottom = array_slice($result, -3, 3);
  464. $middle = array_slice($result, 3, 4);
  465. $result = array_merge($top, $bottom, $middle);
  466. }
  467. $this->success('请求成功', ['ranking' => $result, 'data' => $receivablesData]);
  468. }
  469. //新增排行
  470. public function newRanking() {
  471. $year = input('date', date('Y'));
  472. $between = [strtotime($year . '-01-01'), strtotime($year.'-1-1 +1 year')];
  473. $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'))];
  474. $customers = Customer::where([
  475. 'createtime' => [
  476. 'between',
  477. $between
  478. ],
  479. 'create_staff_id' => $this->auth->id
  480. ])->group('month')->field("id,FROM_UNIXTIME(createtime,'%Y-%m') as month,count(*) as count")->select();
  481. $customers_list = [];
  482. foreach ($customers as $v) {
  483. $customers_list[$v['month']] = $v['count'];
  484. }
  485. $contracts = Contract::where([
  486. 'order_date' => [
  487. 'between',
  488. $betweenC
  489. ],
  490. 'create_staff_id' => $this->auth->id
  491. ])->group('month')->field('id,FROM_UNIXTIME(createtime,"%Y-%m") as month,count(*) as count')->select();
  492. $contracts_list = [];
  493. foreach ($contracts as $v) {
  494. $contracts_list[$v['month']] = $v['count'];
  495. }
  496. $leads = Leads::where([
  497. 'createtime' => [
  498. 'between',
  499. $between
  500. ],
  501. 'create_staff_id' => $this->auth->id
  502. ])->group('month')->field('id,FROM_UNIXTIME(createtime,"%Y-%m") as month,count(*) as count')->select();
  503. $leads_list = [];
  504. foreach ($leads as $v) {
  505. $leads_list[$v['month']] = $v['count'];
  506. }
  507. $data = [];
  508. for ($i = 1; $i <= 12; $i++) {
  509. $month = date('Y-m', strtotime($year . '-' . $i));
  510. $data[] = [
  511. 'month' => $month,
  512. 'customers' => $customers_list[$month] ?? 0,
  513. 'contracts' => $contracts_list[$month] ?? 0,
  514. 'leads' => $leads_list[$month] ?? 0,
  515. ];
  516. }
  517. $this->success('请求成功', $data);
  518. }
  519. //获取团队新增统计
  520. public function addCustomerStatistics() {
  521. $date = input('date', date('Y-m'));
  522. if(strlen($date) == 4){
  523. $between = [strtotime($date . '-01-01'), strtotime($date.'-1-1 +1 year') - 1];
  524. $betweenC = [date('Y-m-d 00:00:00',strtotime($date . '-01-01')), date('Y-m-d 23:59:59',strtotime($date.'-1-1 +1 year') - 1)];
  525. }else{
  526. $between = [strtotime($date), strtotime('+1 month', strtotime($date)) - 1];
  527. $betweenC = [date('Y-m-d 00:00:00',strtotime($date)), date('Y-m-d 23:59:59',strtotime('+1 month', strtotime($date)) - 1)];
  528. }
  529. $ids = Staff::getMyStaffIds();
  530. $staffs = Staff::where(['id' => ['in', $ids]])->field('id,name,post,img')->select();
  531. $c = Customer::where([
  532. 'createtime' => [
  533. 'between',
  534. $between
  535. ],
  536. 'create_staff_id' => ['in', $ids]
  537. ])->group('create_staff_id')->order('count desc')->field("id,create_staff_id,count(*) as count")->select();
  538. $customers = [];
  539. foreach ($c as $v) {
  540. $customers[$v['create_staff_id']] = $v['count'];
  541. }
  542. $l = Leads::where([
  543. 'createtime' => [
  544. 'between',
  545. $between
  546. ],
  547. 'create_staff_id' => ['in', $ids]
  548. ])->group('create_staff_id')->field("id,create_staff_id,count(*) as count")->select();
  549. $leads = [];
  550. foreach ($l as $v) {
  551. $leads[$v['create_staff_id']] = $v['count'];
  552. }
  553. $t = Contract::where([
  554. 'order_date' => [
  555. 'between',
  556. $betweenC
  557. ],
  558. 'create_staff_id' => ['in', $ids]
  559. ])->group('create_staff_id')->field("id,create_staff_id,count(*) as count")->select();
  560. $contracts = [];
  561. foreach ($t as $v) {
  562. $contracts[$v['create_staff_id']] = $v['count'];
  563. }
  564. $total=[];
  565. $total[0]=[
  566. 'name'=>'数据汇总',
  567. 'id'=>0,
  568. 'leads'=>0,
  569. 'customers'=>0,
  570. 'contracts'=>0,
  571. ];
  572. $sort=[];
  573. foreach ($staffs as $k => $v) {
  574. $v['leads'] = $leads[$v['id']] ?? 0;
  575. $v['customers'] = $customers[$v['id']] ?? 0;
  576. $v['contracts'] = $contracts[$v['id']] ?? 0;
  577. $sort[$v['id']] = $v['leads'] + $v['customers'] + $v['contracts'];
  578. $total[0]['leads'] += $v['leads'];
  579. $total[0]['customers'] += $v['customers'];
  580. $total[0]['contracts'] += $v['contracts'];
  581. $staffs[$v['id']] = $v;
  582. }
  583. arsort($sort);
  584. $result=[];
  585. foreach ($sort as $id=>$v){
  586. $result[]=$staffs[$id];
  587. }
  588. $staffs=array_merge($total,$result);
  589. $this->success('请求成功',$staffs);
  590. }
  591. //客户分析-客户数分析
  592. public function getCustomerCount()
  593. {
  594. $times = input('times');
  595. $department_id = input('department_id');
  596. $staff_id = input('staff_id');
  597. if (empty($times)) {
  598. $this->error('日期不能为空');
  599. }
  600. $times = setTimes($times, 'time');
  601. $where = [];
  602. $where['createtime'] = ['between', $times];
  603. if ($department_id) {
  604. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  605. $where['create_staff_id'] = ['in', $l_ids];
  606. }
  607. if ($staff_id) {
  608. $where['create_staff_id'] = $staff_id;
  609. }
  610. $result = Customer::where($where)
  611. ->field('count(*) as count,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')
  612. ->group('time')->select();
  613. $trade_result = Customer::where($where)->where(['contract_status'=>1])
  614. ->field('count(*) as count,trade_date')
  615. ->group('trade_date')->select();
  616. $addCount = [];
  617. foreach ($result as $v) {
  618. $addCount[$v['time']] = $v['count'];
  619. }
  620. $tradeCount = [];
  621. foreach ($trade_result as $v) {
  622. $tradeCount[$v['trade_date']] = $v['count'];
  623. }
  624. $dateList = getDateList($times[0], $times[1]);
  625. $result=[];
  626. foreach ($dateList as $date) {
  627. $result['date'][]=$date;
  628. $result['add_count'][]=$addCount[$date] ?? 0;
  629. $result['trade_count'][]=$tradeCount[$date] ?? 0;
  630. }
  631. $this->success('请求成功', $result);
  632. }
  633. //客户分析-老客跟进分析
  634. public function getCustomerSignIn()
  635. {
  636. $times = input('times');
  637. $department_id = input('department_id');
  638. $staff_id = input('staff_id');
  639. if (empty($times)) {
  640. $this->error('日期不能为空');
  641. }
  642. $times = setTimes($times, 'time');
  643. $where = [];
  644. $where['createtime'] = ['between', $times];
  645. if ($department_id) {
  646. $l_ids = Staff::getDepartmentStaffIds($department_id);
  647. $where['create_staff_id'] = ['in', $l_ids];
  648. }
  649. if ($staff_id) {
  650. $where['create_staff_id'] = $staff_id;
  651. }
  652. $result = StaffSignIn::where($where)
  653. ->field('count(*) as count,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')
  654. ->group('time,customer_id')->select();
  655. $trade_result = StaffSignIn::where($where)->where(['customer_status'=>1])//成交
  656. ->field('count(*) as count,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')
  657. ->group('time,customer_id')->select();
  658. $addCount = [];
  659. foreach ($result as $v) {
  660. $addCount[$v['time']] = $v['count'];
  661. }
  662. $tradeCount = [];
  663. foreach ($trade_result as $v) {
  664. $tradeCount[$v['time']] = $v['count'];
  665. }
  666. $dateList = getDateList($times[0], $times[1]);
  667. $result=[];
  668. foreach ($dateList as $date) {
  669. $result['date'][]=$date;
  670. $result['count'][]=$addCount[$date] ?? 0;
  671. $result['trade_count'][]=$tradeCount[$date] ?? 0;
  672. }
  673. $this->success('请求成功', $result);
  674. }
  675. //客户分析-客户转化率分析
  676. public function getCustomerTradeRate()
  677. {
  678. $times = input('times');
  679. $department_id = input('department_id');
  680. $staff_id = input('staff_id');
  681. if (empty($times)) {
  682. $this->error('日期不能为空');
  683. }
  684. $times = setTimes($times, 'time');
  685. $where = [];
  686. $where['createtime'] = ['between', $times];
  687. if ($department_id) {
  688. $l_ids = Staff::getDepartmentStaffIds($department_id);
  689. $where['create_staff_id'] = ['in', $l_ids];
  690. }
  691. if ($staff_id) {
  692. $where['create_staff_id'] = $staff_id;
  693. }
  694. $result = Customer::where($where)->count();
  695. //成交
  696. $trade_result = Customer::where($where)->where(['contract_status' => 1])->count();
  697. $rate = getRatio($trade_result, $result);
  698. $this->success('请求成功', ['addCount' => $result, 'tradeCount' => $trade_result, 'rate' => $rate]);
  699. }
  700. //客户分析-客户售后满意度分析
  701. public function getWorkorderSatisfied()
  702. {
  703. $times = input('times');
  704. $department_id = input('department_id');
  705. $staff_id = input('staff_id');
  706. if (empty($times)) {
  707. $this->error('日期不能为空');
  708. }
  709. $times = setTimes($times, 'time');
  710. $where = [];
  711. $where['createtime'] = ['between', $times];
  712. if ($department_id) {
  713. $l_ids = Staff::getDepartmentStaffIds($department_id);
  714. $where['owner_staff_id'] = ['in', $l_ids];//完成人
  715. }
  716. if ($staff_id) {
  717. $where['owner_staff_id'] = $staff_id;//完成人
  718. }
  719. $result = Workorder::where($where)->where(['status'=>3])->count();
  720. //成交
  721. $satisfied_result = Workorder::where($where)->where(['status' => 3,'comment_score'=>['in',[3,4,5]]])->count();
  722. $rate = getRatio($satisfied_result, $result);
  723. $this->success('请求成功', ['addCount' => $result, 'satisfiedCount' => $satisfied_result, 'rate' => $rate]);
  724. }
  725. //客户分析-客户成交周期分析
  726. public function getCustomerTrade()
  727. {
  728. $times = input('times');
  729. $department_id = input('department_id');
  730. $staff_id = input('staff_id');
  731. if (empty($times)) {
  732. $this->error('日期不能为空');
  733. }
  734. $times = setTimes($times, 'date');
  735. $where = [];
  736. $where['trade_date'] = ['between', $times];
  737. if ($department_id) {
  738. $l_ids = Staff::getDepartmentStaffIds($department_id);
  739. $where['create_staff_id'] = ['in', $l_ids];
  740. }
  741. if ($staff_id) {
  742. $where['create_staff_id'] = $staff_id;
  743. }
  744. $result = Customer::where($where)->where(['contract_status'=>1])//成交
  745. ->field('count(*) as count,trade_date')
  746. ->group('trade_date')->select();
  747. $date_result = Customer::where($where)->where(['contract_status' => 1])//成交
  748. ->field('trade_date,createtime')->select();
  749. $data=[];
  750. foreach ($date_result as $v){
  751. $data[$v['trade_date']][]=intval((strtotime($v['trade_date'])-strtotime($v['createtime']))/86400);
  752. }
  753. foreach ($data as $k=>$v) {
  754. $data[$k] = $v ? intval(array_sum($v) / count($v)) : [];
  755. }
  756. $addCount = [];
  757. foreach ($result as $v) {
  758. $addCount[$v['trade_date']] = $v['count'];
  759. }
  760. $dateList = getDateList(strtotime($times[0]), strtotime($times[1])+86400-1);
  761. $result=[];
  762. foreach ($dateList as $date) {
  763. $result['date'][]=$date;
  764. $result['count'][]=$addCount[$date] ?? 0;
  765. $result['number'][]=$data[$date] ?? 0;
  766. }
  767. $this->success('请求成功', $result);
  768. }
  769. //售后分析-工单数分析
  770. public function getWorkorderCount(){
  771. $times = input('times');
  772. $department_id = input('department_id');
  773. $staff_id = input('staff_id');
  774. $workorder_type = input('workorder_type');
  775. if (empty($times)) {
  776. $this->error('日期不能为空');
  777. }
  778. $times = setTimes($times, 'time');
  779. $where = [];
  780. $where['createtime'] = ['between', $times];
  781. if ($department_id) {
  782. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  783. $where['create_staff_id'] = ['in', $l_ids];
  784. }
  785. if ($staff_id) {
  786. $where['create_staff_id'] = $staff_id;
  787. }
  788. if ($workorder_type) {
  789. $where['workorder_type'] = $workorder_type;
  790. }
  791. $result = Workorder::where($where)//新增工单
  792. ->field('count(*) as count,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')
  793. ->group('time')->select();
  794. unset($where['createtime']);
  795. $where['end_time'] = ['between', $times];
  796. $satisfied_result = Workorder::where($where)->where(['status' => 3])//完成工单
  797. ->field('count(*) as count,FROM_UNIXTIME(end_time,"%Y-%m-%d") as time')->select();
  798. $addCount = [];
  799. foreach ($result as $v) {
  800. $addCount[$v['time']] = $v['count'];
  801. }
  802. $satisfiedCount = [];
  803. foreach ($satisfied_result as $v) {
  804. $satisfiedCount[$v['time']] = $v['count'];
  805. }
  806. $dateList = getDateList($times[0], $times[1]);
  807. $result=[];
  808. foreach ($dateList as $date) {
  809. $result['date'][]=$date;
  810. $result['count'][]=$addCount[$date] ?? 0;
  811. $result['satisfied'][]=$satisfiedCount[$date] ?? 0;
  812. }
  813. $this->success('请求成功', $result);
  814. }
  815. //售后分析-工单金额分析
  816. public function getWorkorderMoney(){
  817. $times = input('times');
  818. $department_id = input('department_id');
  819. $staff_id = input('staff_id');
  820. $workorder_type = input('workorder_type');
  821. if (empty($times)) {
  822. $this->error('日期不能为空');
  823. }
  824. $times = setTimes($times, 'time');
  825. $where = [];
  826. $where['end_time'] = ['between', $times];
  827. if ($department_id) {
  828. $l_ids = Staff::getDepartmentStaffIds($department_id);
  829. $where['create_staff_id'] = ['in', $l_ids];
  830. }
  831. if ($staff_id) {
  832. $where['create_staff_id'] = $staff_id;
  833. }
  834. if ($workorder_type) {
  835. $where['workorder_type'] = $workorder_type;
  836. }
  837. $result = Workorder::where($where)->where(['status' => 3])//完成工单
  838. ->field('count(*) as count,FROM_UNIXTIME(end_time,"%Y-%m-%d") as time')->select();
  839. $money_result = Workorder::where($where)->where(['status' => 3])//工单金额
  840. ->field('sum(money) as money,FROM_UNIXTIME(end_time,"%Y-%m-%d") as time')->select();
  841. $addCount = [];
  842. foreach ($result as $v) {
  843. $addCount[$v['time']] = $v['count'];
  844. }
  845. $moneyCount = [];
  846. foreach ($money_result as $v) {
  847. $moneyCount[$v['time']] = $v['money'];
  848. }
  849. $dateList = getDateList($times[0], $times[1]);
  850. $result=[];
  851. foreach ($dateList as $date) {
  852. $result['date'][]=$date;
  853. $result['count'][]=$addCount[$date] ?? 0;
  854. $result['money'][]=$moneyCount[$date] ?? 0;
  855. }
  856. $this->success('请求成功', $result);
  857. }
  858. //销售分析-销售漏斗分析
  859. public function getStaffCustomer()
  860. {
  861. $times = input('times');
  862. $department_id = input('department_id');
  863. $staff_id = input('staff_id');
  864. if (empty($times)) {
  865. $this->error('日期不能为空');
  866. }
  867. $times = setTimes($times, 'time');
  868. $where = [];
  869. $where['createtime'] = ['between', $times];
  870. if ($department_id) {
  871. $l_ids = Staff::getOneGroupStaffIds($department_id);
  872. $where['owner_staff_id'] = ['in', $l_ids];
  873. }
  874. if ($staff_id) {
  875. $where['owner_staff_id'] = $staff_id;
  876. }
  877. $status=Field::where(['type'=>'跟进状态'])->column('field');
  878. $result = Customer::where($where)//成交
  879. ->field('count(*) as count,follow')
  880. ->group('follow')->select();
  881. $addCount = [];
  882. foreach ($result as $v) {
  883. $addCount[$v['follow']] = $v['count'];
  884. }
  885. $customerList = [];
  886. foreach ($status as $follow) {
  887. $customerList[] = [
  888. 'name' => $follow,
  889. 'value' => $addCount[$follow] ?? 0,
  890. ];
  891. }
  892. $this->success('请求成功', $customerList);
  893. }
  894. //产品分析-产品销售分析
  895. public function getProductCount(){
  896. $times = input('times');
  897. $department_id = input('department_id');
  898. $staff_id = input('staff_id');
  899. if (empty($times)) {
  900. $this->error('日期不能为空');
  901. }
  902. $times = setTimes($times, 'date');
  903. $where = [];
  904. $where['start_time'] = ['between', $times];
  905. if ($department_id) {
  906. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  907. $where['owner_staff_id'] = ['in', $l_ids];
  908. }
  909. if ($staff_id) {
  910. $where['owner_staff_id'] = $staff_id;
  911. }
  912. $result=Contract::where($where)->field('count(*) as count,start_time')->group('start_time')->select();
  913. $addCount = [];
  914. foreach ($result as $v) {
  915. $addCount[$v['start_time']] = $v['count'];
  916. }
  917. $dateList = getDateList(strtotime($times[0]), strtotime($times[1]));
  918. $result=[];
  919. foreach ($dateList as $date) {
  920. $result['date'][]=$date;
  921. $result['count'][]=$addCount[$date] ?? 0;
  922. }
  923. $this->success('请求成功', $result);
  924. }
  925. //产品分析-部件问题分析
  926. public function getProductParts()
  927. {
  928. $times = input('times');
  929. $department_id = input('department_id');
  930. $staff_id = input('staff_id');
  931. if (empty($times)) {
  932. $this->error('日期不能为空');
  933. }
  934. $times = setTimes($times, 'time');
  935. $where = [];
  936. $where['w.createtime'] = ['between', $times];
  937. if ($department_id) {
  938. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  939. $where['create_staff_id'] = ['in', $l_ids];
  940. }
  941. if ($staff_id) {
  942. $where['create_staff_id'] = $staff_id;
  943. }
  944. $result = Workorder::where($where)->alias('w')->where(['workorder_type'=>'上门维修'])
  945. ->join('qingdongams_customer_product c', 'c.id=w.customer_product_id')
  946. ->field('count(*) as count,product_id')->select();
  947. $result=collection($result)->toArray();
  948. $addCount = [];
  949. foreach ($result as $v) {
  950. $addCount[$v['product_id']] = $v['count'];
  951. }
  952. $sum=array_sum($addCount);
  953. $product = Product::where([])->select();
  954. $result=[];
  955. foreach ($product as $item) {
  956. $result['name'][]=$item['name'];
  957. $result['count'][]=$addCount[$item['id']] ?? 0;
  958. $result['ratio'][]=getRatio($addCount[$item['id']] ?? 0,$sum);
  959. }
  960. $this->success('请求成功', $result?:null);
  961. }
  962. //员工业绩分析-合同数分析
  963. public function getContractCount()
  964. {
  965. $times = input('times');
  966. $department_id = input('department_id');
  967. $staff_id = input('staff_id');
  968. if (empty($times)) {
  969. $this->error('日期不能为空');
  970. }
  971. $times = setTimes($times, 'date');
  972. $where = [];
  973. $where['order_date'] = ['between', $times];
  974. if ($department_id) {
  975. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  976. $where['create_staff_id'] = ['in', $l_ids];
  977. }
  978. if ($staff_id) {
  979. $where['create_staff_id'] = $staff_id;
  980. }
  981. $result=Contract::where($where)->field('count(*) as count,order_date')->group('order_date')->select();
  982. $addCount = [];
  983. foreach ($result as $v) {
  984. $addCount[$v['order_date']] = $v['count'];
  985. }
  986. $dateList = getDateList(strtotime($times[0]), strtotime($times[1]));
  987. $result=[];
  988. foreach ($dateList as $date) {
  989. $result['date'][]=$date;
  990. $result['count'][]=$addCount[$date] ?? 0;
  991. }
  992. $this->success('请求成功', $result);
  993. }
  994. //员工业绩分析-合同金额分析
  995. public function getContractMoney()
  996. {
  997. $times = input('times');
  998. $department_id = input('department_id');
  999. $staff_id = input('staff_id');
  1000. if (empty($times)) {
  1001. $this->error('日期不能为空');
  1002. }
  1003. $times = setTimes($times, 'date');
  1004. $where = [];
  1005. $where['order_date'] = ['between', $times];
  1006. if ($department_id) {
  1007. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  1008. $where['create_staff_id'] = ['in', $l_ids];
  1009. }
  1010. if ($staff_id) {
  1011. $where['create_staff_id'] = $staff_id;
  1012. }
  1013. $result=Contract::where($where)->field('sum(money) as count,order_date')->group('order_date')->select();
  1014. $addCount = [];
  1015. foreach ($result as $v) {
  1016. $addCount[$v['order_date']] = $v['count'];
  1017. }
  1018. $dateList = getDateList(strtotime($times[0]), strtotime($times[1]));
  1019. $result=[];
  1020. foreach ($dateList as $date) {
  1021. $result['date'][]=$date;
  1022. $result['count'][]=$addCount[$date] ?? 0;
  1023. }
  1024. $this->success('请求成功', $result);
  1025. }
  1026. //员工业绩分析-回款金额分析
  1027. public function getReceivablesMoney()
  1028. {
  1029. $times = input('times');
  1030. $department_id = input('department_id');
  1031. $staff_id = input('staff_id');
  1032. if (empty($times)) {
  1033. $this->error('日期不能为空');
  1034. }
  1035. $times = setTimes($times, 'date');
  1036. $where = [];
  1037. $where['return_time'] = ['between', $times];
  1038. if ($department_id) {
  1039. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  1040. $where['create_staff_id'] = ['in', $l_ids];
  1041. }
  1042. if ($staff_id) {
  1043. $where['create_staff_id'] = $staff_id;
  1044. }
  1045. $result=Receivables::where($where)->field('sum(money) as count,return_time')
  1046. ->group('return_time')->select();
  1047. $addCount = [];
  1048. foreach ($result as $v) {
  1049. $addCount[$v['return_time']] = $v['count'];
  1050. }
  1051. $dateList = getDateList(strtotime($times[0]), strtotime($times[1]));
  1052. $result=[];
  1053. foreach ($dateList as $date) {
  1054. $result['date'][]=$date;
  1055. $result['money'][]=intval($addCount[$date] ?? 0);
  1056. }
  1057. $this->success('请求成功', $result);
  1058. }
  1059. //员工业绩分析-合同汇总表
  1060. public function getContractSummary()
  1061. {
  1062. $times = input('times');
  1063. $department_id = input('department_id');
  1064. $staff_id = input('staff_id');
  1065. if (empty($times)) {
  1066. $this->error('日期不能为空');
  1067. }
  1068. $times = setTimes($times, 'date');
  1069. $where = [];
  1070. $where['order_date'] = ['between', $times];
  1071. if ($department_id) {
  1072. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  1073. $where['create_staff_id'] = ['in', $l_ids];
  1074. }
  1075. if ($staff_id) {
  1076. $where['create_staff_id'] = $staff_id;
  1077. }
  1078. $result = Contract::where($where)->field('count(*) as count,order_date')
  1079. ->group('order_date')->select();
  1080. $contract_result = Contract::where($where)->field('sum(money) as count,order_date')
  1081. ->group('order_date')->select();
  1082. unset($where['order_date']);
  1083. $where['return_time'] = ['between', $times];
  1084. $receivables_result=Receivables::where($where)->field('sum(money) as count,return_time')
  1085. ->group('return_time')->select();
  1086. $addCount = [];
  1087. foreach ($result as $v) {
  1088. $addCount[$v['order_date']] = $v['count'];
  1089. }
  1090. $contractCount = [];
  1091. foreach ($contract_result as $v) {
  1092. $contractCount[$v['order_date']] = $v['count'];
  1093. }
  1094. $receivablesCount = [];
  1095. foreach ($receivables_result as $v) {
  1096. $receivablesCount[$v['return_time']] = $v['count'];
  1097. }
  1098. $dateList = getDateList(strtotime($times[0]), strtotime($times[1]));
  1099. $contractList = [];
  1100. foreach ($dateList as $date) {
  1101. $contractList[] = [
  1102. 'date' => $date,
  1103. 'count' => $addCount[$date] ?? 0,
  1104. 'contract_money' => $contractCount[$date] ?? 0,
  1105. 'receivables_money' => $receivablesCount[$date] ?? 0,
  1106. ];
  1107. }
  1108. $this->success('请求成功', $contractList);
  1109. }
  1110. //业绩目标完成分析-合同金额分析
  1111. public function getContractTarget(){
  1112. $year = input('year', date('Y'));
  1113. $team_id = input('team_id');
  1114. $staff_id = input('staff_id');
  1115. if (empty($year)) {
  1116. $this->error('日期不能为空');
  1117. }
  1118. $where = ['check_status'=>2];
  1119. $where['order_date'] = ['like', $year . '%'];
  1120. //默认公司
  1121. $yearAchievement = Achievement::getCompanyYearAchievement($year,Achievement::CONTRACT_TYPE);
  1122. if ($team_id) {//获取团队下成员
  1123. $staff=Staff::where([])->find();
  1124. $l_ids = Staff::getStaffLowerIds($staff);
  1125. $where['owner_staff_id'] = ['in', $l_ids];
  1126. $yearAchievement = Achievement::getTeamYearAchievement($year,Achievement::CONTRACT_TYPE,$team_id);
  1127. }
  1128. if ($staff_id) {
  1129. $where['owner_staff_id'] = $staff_id;
  1130. $yearAchievement = Achievement::getStaffYearAchievement($year,Achievement::CONTRACT_TYPE,$staff_id);
  1131. }
  1132. $contracts = Contract::where($where)->group('month')
  1133. ->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),"%Y-%m") as month,count(*) as count')->select();
  1134. $contracts_list = [];
  1135. foreach ($contracts as $v) {
  1136. $contracts_list[$v['month']] = $v;
  1137. }
  1138. $data = [];
  1139. foreach ($yearAchievement as $k => $v) {
  1140. if ($month = Achievement::getFieldMonth($k)) {
  1141. $month = $year . '-' . $month;
  1142. $row = [
  1143. 'month' => $month,
  1144. 'achievement' => $v,
  1145. 'money' => isset($contracts_list[$month]) ? $contracts_list[$month]['money'] : 0,
  1146. 'count' => isset($contracts_list[$month]) ? $contracts_list[$month]['count'] : 0,
  1147. ];
  1148. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  1149. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  1150. $data['month'][] = $row['month'];
  1151. $data['achievement'][] = $row['achievement'];
  1152. $data['money'][] = $row['money'];
  1153. $data['ratio'][] = $row['ratio'];
  1154. }
  1155. }
  1156. $this->success('请求成功', $data);
  1157. }
  1158. //业绩目标完成分析-回款金额分析
  1159. public function getReceivablesTarget(){
  1160. $year = input('year', date('Y'));
  1161. $team_id = input('team_id');
  1162. $staff_id = input('staff_id');
  1163. if (empty($year)) {
  1164. $this->error('日期不能为空');
  1165. }
  1166. $where = ['check_status'=>2];
  1167. $where['return_time'] = ['like', $year . '%'];
  1168. //默认公司
  1169. $yearAchievement = Achievement::getCompanyYearAchievement($year,Achievement::RECEIVABLES_TYPE);
  1170. if ($team_id) {//获取团队下成员
  1171. $staff=Staff::where([])->find();
  1172. $l_ids = Staff::getStaffLowerIds($staff);
  1173. $where['owner_staff_id'] = ['in', $l_ids];
  1174. $yearAchievement = Achievement::getTeamYearAchievement($year,Achievement::RECEIVABLES_TYPE,$team_id);
  1175. }
  1176. if ($staff_id) {
  1177. $where['owner_staff_id'] = $staff_id;
  1178. $yearAchievement = Achievement::getStaffYearAchievement($year,Achievement::RECEIVABLES_TYPE,$staff_id);
  1179. }
  1180. $receivables = Receivables::where($where)->group('month')
  1181. ->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"%Y-%m") as month,count(*) as count')->select();
  1182. $receivables_list = [];
  1183. foreach ($receivables as $v) {
  1184. $receivables_list[$v['month']] = $v;
  1185. }
  1186. $data = [];
  1187. foreach ($yearAchievement as $k => $v) {
  1188. if ($month = Achievement::getFieldMonth($k)) {
  1189. $month = $year . '-' . $month;
  1190. $row = [
  1191. 'month' => $month,
  1192. 'achievement' => $v,
  1193. 'money' => isset($receivables_list[$month]) ? $receivables_list[$month]['money'] : 0,
  1194. 'count' => isset($receivables_list[$month]) ? $receivables_list[$month]['count'] : 0,
  1195. ];
  1196. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  1197. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  1198. $data['month'][] = $row['month'];
  1199. $data['achievement'][] = $row['achievement'];
  1200. $data['money'][] = $row['money'];
  1201. $data['ratio'][] = $row['ratio'];
  1202. }
  1203. }
  1204. $this->success('请求成功', $data);
  1205. }
  1206. //进销存销售分析-销售汇总分析
  1207. public function getSalesSummary(){
  1208. $times = input('times');
  1209. $department_id = input('department_id');
  1210. $staff_id = input('staff_id');
  1211. if (empty($times)) {
  1212. $this->error('日期不能为空');
  1213. }
  1214. $times = setTimes($times, 'date');
  1215. $where = [];
  1216. $where['order_date'] = ['between', $times];
  1217. if ($department_id) {
  1218. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  1219. $where['create_staff_id'] = ['in', $l_ids];
  1220. }
  1221. if ($staff_id) {
  1222. $where['create_staff_id'] = $staff_id;
  1223. }
  1224. $result = Contract::where($where)->field('count(*) as count,order_date')
  1225. ->group('order_date')->select();
  1226. $contract_result = Contract::where($where)->field('sum(money) as count,order_date')
  1227. ->group('order_date')->select();
  1228. $addCount = [];
  1229. foreach ($result as $v) {
  1230. $addCount[$v['order_date']] = $v['count'];
  1231. }
  1232. $contractCount = [];
  1233. foreach ($contract_result as $v) {
  1234. $contractCount[$v['order_date']] = $v['count'];
  1235. }
  1236. $dateList = getDateList(strtotime($times[0]), strtotime($times[1]));
  1237. $result=[];
  1238. foreach ($dateList as $date) {
  1239. $result['date'][]=$date;
  1240. $result['count'][]=intval($addCount[$date] ?? 0);
  1241. $result['contract_money'][]=$contractCount[$date] ?? 0;
  1242. }
  1243. $this->success('请求成功', $result);
  1244. }
  1245. //进销存销售分析-产品销售分析
  1246. public function getProductSummary(){
  1247. $times = input('times');
  1248. $department_id = input('department_id');
  1249. $staff_id = input('staff_id');
  1250. if (empty($times)) {
  1251. $this->error('日期不能为空');
  1252. }
  1253. $times = setTimes($times, 'date');
  1254. $where = [];
  1255. // $where['createtime'] = ['between', $times];
  1256. if ($department_id) {
  1257. $l_ids = Staff::getOneDepartmentStaffList($department_id);
  1258. $where['create_staff_id'] = ['in', $l_ids];
  1259. }
  1260. if ($staff_id) {
  1261. $where['create_staff_id'] = $staff_id;
  1262. }
  1263. $result = Contract::where($where) ->alias('w')
  1264. ->join('qingdongams_contract_product c', 'w.id=c.contract_id')
  1265. ->field('count(*) as count,sum(number) as number,product_id,sum(money) as money')->group('product_id')->where(['check_status'=>2])->select();
  1266. $result=collection($result)->toArray();
  1267. $addCount = [];
  1268. foreach ($result as $v) {
  1269. $addCount[$v['product_id']] = [
  1270. 'count'=>$v['count'],
  1271. 'number'=>$v['number'],
  1272. 'money'=>$v['money'],
  1273. ];
  1274. }
  1275. $sum=array_sum($addCount);
  1276. $product = Product::where([])->select();
  1277. $data = [];
  1278. foreach ($product as $item) {
  1279. $data[] = [
  1280. 'name' => $item['name'],
  1281. 'count' => $addCount[$item['id']]['count'] ?? 0,
  1282. 'number' => $addCount[$item['id']]['number'] ?? 0,
  1283. 'money' => $addCount[$item['id']]['money'] ?? 0,
  1284. ];
  1285. }
  1286. $this->success('请求成功', $data);
  1287. }
  1288. //获取任务统计
  1289. public function getEventStatistics(){
  1290. $khfwsm= Event::where(['event_type'=>['like',"客户服务_上门%"]])->group('status')->column('count(*) as number','status');
  1291. $khfwdh= Event::where(['event_type'=>"客户服务_电话"])->group('status')->column('count(*) as number','status');
  1292. $khfwwx= Event::where(['event_type'=>"客户服务_微信"])->group('status')->column('count(*) as number','status');
  1293. $shfwsm= Event::where(['event_type'=>['like',"售后服务_上门%"]])->group('status')->column('count(*) as number','status');
  1294. $shfwdh= Event::where(['event_type'=>"售后服务_电话"])->group('status')->column('count(*) as number','status');
  1295. $shfwfcwx= Event::where(['event_type'=>"售后服务_返厂维修"])->group('status')->column('count(*) as number','status');
  1296. $data = [
  1297. [
  1298. 'name' => '客户服务-上门',
  1299. 'data' => [
  1300. [
  1301. 'key' => '未开始',
  1302. 'value' => ($khfwsm[0] ?? 0) . '',
  1303. ],
  1304. [
  1305. 'key' => '进行中',
  1306. 'value' => ($khfwsm[1] ?? 0) . '',
  1307. ],
  1308. [
  1309. 'key' => '已结束',
  1310. 'value' => ($khfwsm[2] ?? 0) . '',
  1311. ],
  1312. [
  1313. 'key' => '任务结束',
  1314. 'value' => ($khfwsm[3] ?? 0) . '',
  1315. ],
  1316. ]
  1317. ],
  1318. [
  1319. 'name' => '客户服务-电话',
  1320. 'data' => [
  1321. [
  1322. 'key' => '未开始',
  1323. 'value' => ($khfwdh[0] ?? 0) . '',
  1324. ],
  1325. [
  1326. 'key' => '进行中',
  1327. 'value' => ($khfwdh[1] ?? 0) . '',
  1328. ],
  1329. [
  1330. 'key' => '已结束',
  1331. 'value' => ($khfwdh[2] ?? 0) . '',
  1332. ],
  1333. [
  1334. 'key' => '任务结束',
  1335. 'value' => ($khfwdh[3] ?? 0) . '',
  1336. ],
  1337. ]
  1338. ],
  1339. [
  1340. 'name' => '客户服务-微信',
  1341. 'data' => [
  1342. [
  1343. 'key' => '未开始',
  1344. 'value' => ($khfwwx[0] ?? 0) . '',
  1345. ],
  1346. [
  1347. 'key' => '进行中',
  1348. 'value' => ($khfwwx[1] ?? 0) . '',
  1349. ],
  1350. [
  1351. 'key' => '已结束',
  1352. 'value' => ($khfwwx[2] ?? 0) . '',
  1353. ],
  1354. [
  1355. 'key' => '任务结束',
  1356. 'value' => ($khfwwx[3] ?? 0) . '',
  1357. ],
  1358. ]
  1359. ],
  1360. [
  1361. 'name' => '售后服务-上门',
  1362. 'data' => [
  1363. [
  1364. 'key' => '未开始',
  1365. 'value' => ($shfwsm[0] ?? 0) . '',
  1366. ],
  1367. [
  1368. 'key' => '进行中',
  1369. 'value' => ($shfwsm[1] ?? 0) . '',
  1370. ],
  1371. [
  1372. 'key' => '已结束',
  1373. 'value' => ($shfwsm[2] ?? 0) . '',
  1374. ],
  1375. [
  1376. 'key' => '任务结束',
  1377. 'value' => ($shfwsm[3] ?? 0) . '',
  1378. ],
  1379. ]
  1380. ],
  1381. [
  1382. 'name' => '售后服务-电话',
  1383. 'data' => [
  1384. [
  1385. 'key' => '未开始',
  1386. 'value' => ($shfwdh[0] ?? 0) . '',
  1387. ],
  1388. [
  1389. 'key' => '进行中',
  1390. 'value' => ($shfwdh[1] ?? 0) . '',
  1391. ],
  1392. [
  1393. 'key' => '已结束',
  1394. 'value' => ($shfwdh[2] ?? 0) . '',
  1395. ],
  1396. [
  1397. 'key' => '任务结束',
  1398. 'value' => ($shfwdh[3] ?? 0) . '',
  1399. ],
  1400. ]
  1401. ],
  1402. [
  1403. 'name' => '售后服务-返厂维修',
  1404. 'data' => [
  1405. [
  1406. 'key' => '未开始',
  1407. 'value' => ($shfwfcwx[0] ?? 0) . '',
  1408. ],
  1409. [
  1410. 'key' => '进行中',
  1411. 'value' => ($shfwfcwx[1] ?? 0) . '',
  1412. ],
  1413. [
  1414. 'key' => '已结束',
  1415. 'value' => ($shfwfcwx[2] ?? 0) . '',
  1416. ],
  1417. [
  1418. 'key' => '任务结束',
  1419. 'value' => ($shfwfcwx[3] ?? 0) . '',
  1420. ],
  1421. ]
  1422. ],
  1423. ];
  1424. $this->success('请求成功',$data);
  1425. }
  1426. //获取客户统计
  1427. public function getCustomerStatistics(){
  1428. $data= Customer::where([])->group('management_id,type')
  1429. ->field('count(*) as number,type,management_id')->select();
  1430. $managements=AreaManagement::where([])->column('name','id');
  1431. $result=[];
  1432. $customer=[];
  1433. foreach ($data as $v){
  1434. $customer[$v['management_id']][$v['type']]=$v['number'];
  1435. }
  1436. foreach ($customer as $mid=>$v){
  1437. if(!isset($managements[$mid])){
  1438. continue;
  1439. }
  1440. $result[]=[
  1441. 'name'=>$managements[$mid],
  1442. 'data' => [
  1443. [
  1444. 'key' => 'A类客户',
  1445. 'value' => ($v['A'] ?? 0) . '',
  1446. ],
  1447. [
  1448. 'key' => 'B类客户',
  1449. 'value' => ($v['B'] ?? 0) . '',
  1450. ],
  1451. [
  1452. 'key' => 'C类客户',
  1453. 'value' => ($v['C'] ?? 0) . '',
  1454. ],
  1455. [
  1456. 'key' => 'D类客户',
  1457. 'value' => ($v['D'] ?? 0) . '',
  1458. ],
  1459. ]
  1460. ];
  1461. }
  1462. $this->success('请求成功',$result);
  1463. }
  1464. //获取审批统计
  1465. public function getApprovalStatistics(){
  1466. $form= FormApproval::where([])->column('name','id');
  1467. $data=[];
  1468. foreach ($form as $id=>$name) {
  1469. $value=[];
  1470. $value['name']=$name;
  1471. $approval=\addons\qingdongams\model\Approval::where(['formapproval_id'=>$id])->group('check_status')->column('count(*) as number','check_status');
  1472. $value['data']=[
  1473. ['key'=>'待审核','value'=>($approval[1]??0).''],
  1474. ['key'=>'审核通过','value'=>($approval[2]??0).''],
  1475. ['key'=>'审核拒绝','value'=>($approval[3]??0).''],
  1476. ['key'=>'撤销审核','value'=>($approval[4]??0).''],
  1477. ['key'=>'终止审批','value'=>($approval[9]??0).''],
  1478. ];
  1479. $data[]=$value;
  1480. }
  1481. $this->success('请求成功',$data);
  1482. }
  1483. //获取费用统计
  1484. public function getConsumeStatistics()
  1485. {
  1486. $consume_types=ConsumeDetail::where([])->group('consume_type')->column('consume_type');
  1487. $data=[];
  1488. foreach ($consume_types as $name){
  1489. $value=[];
  1490. $value['name']=$name;
  1491. $consume_ids=ConsumeDetail::where(['consume_type'=>$name])->column('consume_id');
  1492. $consume=\addons\qingdongams\model\Consume::where(['id'=>['in',$consume_ids]])->group('check_status')->column('count(*) as number','check_status');
  1493. $value['data']=[
  1494. ['key'=>'待审核','value'=>($consume[0]??0).''],
  1495. ['key'=>'审核通过','value'=>($consume[2]??0).''],
  1496. ['key'=>'审核拒绝','value'=>($consume[3]??0).''],
  1497. ['key'=>'撤销审核','value'=>($consume[4]??0).''],
  1498. ['key'=>'终止审批','value'=>($consume[9]??0).''],
  1499. ];
  1500. $data[]=$value;
  1501. }
  1502. $this->success('请求成功',$data);
  1503. }
  1504. //获取出纳统计
  1505. public function getCashierStatistics()
  1506. {
  1507. $consume_types=ConsumeDetail::where([])->group('consume_type')->column('consume_type');
  1508. $data=[];
  1509. foreach ($consume_types as $name){
  1510. $value=[];
  1511. $value['name']=$name;
  1512. $consume_ids=ConsumeDetail::where(['consume_type'=>$name])->column('consume_id');
  1513. $consume=\addons\qingdongams\model\Consume::where(['id'=>['in',$consume_ids],'check_status'=>2])->group('is_cashier')->column('count(*) as number','is_cashier');
  1514. $value['data']=[
  1515. ['key'=>'未打款','value'=>($consume[0]??0).''],
  1516. ['key'=>'已打款','value'=>($consume[1]??0).''],
  1517. ];
  1518. $data[]=$value;
  1519. }
  1520. $this->success('请求成功',$data);
  1521. }
  1522. //获取合同统计
  1523. public function getContractStatistics()
  1524. {
  1525. $contract= Contract::where([])->group('check_status')->column('count(*) as number','check_status');
  1526. $data = [
  1527. [
  1528. 'name' => '合同',
  1529. 'data' => [
  1530. ['key'=>'待审核','value'=>($contract[0]??0).''],
  1531. ['key'=>'审核通过','value'=>($contract[2]??0).''],
  1532. ['key'=>'审核拒绝','value'=>($contract[3]??0).''],
  1533. ['key'=>'撤销审核','value'=>($contract[4]??0).''],
  1534. ['key'=>'终止审批','value'=>($contract[9]??0).''],
  1535. ]
  1536. ],
  1537. ];
  1538. $this->success('请求成功',$data);
  1539. }
  1540. //获取报价单拿统计
  1541. public function getQuoteStatistics()
  1542. {
  1543. $quote= \addons\qingdongams\model\Quote::where([])->group('check_status')->column('count(*) as number','check_status');
  1544. $data = [
  1545. [
  1546. 'name' => '报价单',
  1547. 'data' => [
  1548. ['key'=>'待审核','value'=>($quote[0]??0).''],
  1549. ['key'=>'审核通过','value'=>($quote[2]??0).''],
  1550. ['key'=>'审核拒绝','value'=>($quote[3]??0).''],
  1551. ['key'=>'撤销审核','value'=>($quote[4]??0).''],
  1552. ['key'=>'终止审批','value'=>($quote[9]??0).''],
  1553. ]
  1554. ],
  1555. ];
  1556. $this->success('请求成功',$data);
  1557. }
  1558. //获取备件统计
  1559. public function getPartsStatistics()
  1560. {
  1561. $ru= PartsStockReload::where(['type'=>1])->group('check_status')->column('count(*) as number','check_status');
  1562. $chu= PartsStockReload::where(['type'=>2])->group('check_status')->column('count(*) as number','check_status');
  1563. $data = [
  1564. [
  1565. 'name' => '入库审核',
  1566. 'data' => [
  1567. ['key'=>'待审核','value'=>($ru[0]??0).''],
  1568. ['key'=>'审核通过','value'=>($ru[2]??0).''],
  1569. ['key'=>'审核拒绝','value'=>($ru[3]??0).''],
  1570. ['key'=>'撤销审核','value'=>($ru[4]??0).''],
  1571. ['key'=>'终止审批','value'=>($ru[9]??0).''],
  1572. ]
  1573. ],
  1574. [
  1575. 'name' => '出库审核',
  1576. 'data' => [
  1577. ['key'=>'待审核','value'=>($chu[0]??0).''],
  1578. ['key'=>'审核通过','value'=>($chu[2]??0).''],
  1579. ['key'=>'审核拒绝','value'=>($chu[3]??0).''],
  1580. ['key'=>'撤销审核','value'=>($chu[4]??0).''],
  1581. ['key'=>'终止审批','value'=>($chu[9]??0).''],
  1582. ]
  1583. ],
  1584. ];
  1585. $this->success('请求成功',$data);
  1586. }
  1587. //获取工单统计
  1588. public function getWorkorderStatistics()
  1589. {
  1590. $workorder_types= Workorder::where([])->group('workorder_type')->column('workorder_type');
  1591. $data=[];
  1592. foreach ($workorder_types as $name) {
  1593. if(empty($name)){
  1594. continue;
  1595. }
  1596. $value=[];
  1597. $value['name']=$name;
  1598. $workorder=Workorder::where(['workorder_type'=>$name])->group('check_status')->column('count(*) as number','check_status');
  1599. $value['data']=[
  1600. ['key'=>'待审核','value'=>($workorder[0]??0).''],
  1601. ['key'=>'审核通过','value'=>($workorder[2]??0).''],
  1602. ['key'=>'审核拒绝','value'=>($workorder[3]??0).''],
  1603. ['key'=>'撤销审核','value'=>($workorder[4]??0).''],
  1604. ['key'=>'终止审批','value'=>($workorder[9]??0).''],
  1605. ];
  1606. $data[]=$value;
  1607. }
  1608. $this->success('请求成功',$data);
  1609. }
  1610. /**
  1611. * 客户新增排行
  1612. */
  1613. public function newCustomer() {
  1614. $date = input('date', date('Y-m'));
  1615. $type = input('type', 0);
  1616. if(strlen($date) == 4){
  1617. $between = [strtotime($date . '-01-01'), strtotime($date.'-1-1 +1 year') - 1];
  1618. }else{
  1619. $between = [strtotime($date), strtotime('+1 month', strtotime($date)) - 1];
  1620. }
  1621. if ($type == 1) {//本人
  1622. $ids = [$this->auth->id];
  1623. } elseif ($type == 2) {//下属
  1624. $ids = Staff::getLowerStaffId();
  1625. } else {//全部
  1626. $ids = Staff::getMyStaffIds();
  1627. }
  1628. $c = Customer::where([
  1629. 'createtime' => [
  1630. 'between',
  1631. $between
  1632. ],
  1633. 'owner_staff_id' => ['in', $ids]
  1634. ])->group('owner_staff_id')->order('count desc')->field("owner_staff_id,count(*) as count")->select();
  1635. $customers = [];
  1636. foreach ($c as $v) {
  1637. $customers[$v['owner_staff_id']] = $v['count'];
  1638. }
  1639. arsort($customers);
  1640. $staffs = Staff::getKeyList();
  1641. $result = [];
  1642. $i = 1;
  1643. $data=[];
  1644. foreach ($customers as $id => $number) {
  1645. $val = $staffs[$id];
  1646. $val['number'] = $number;
  1647. $val['rank'] = $i;
  1648. $data[]=[
  1649. 'name'=>$val['name'],
  1650. 'number'=>$number
  1651. ];
  1652. $i++;
  1653. $result[] = $val;
  1654. }
  1655. $this->success('请求成功',['ranking' => $result, 'data' => $data]);
  1656. }
  1657. /**
  1658. * 客户跟进排行
  1659. */
  1660. public function newRecord() {
  1661. $date = input('date', date('Y-m'));
  1662. $type = input('type', 0);
  1663. if(strlen($date) == 4){
  1664. $between = [strtotime($date . '-01-01'), strtotime($date.'-1-1 +1 year') - 1];
  1665. }else{
  1666. $between = [strtotime($date), strtotime('+1 month', strtotime($date)) - 1];
  1667. }
  1668. if ($type == 1) {//本人
  1669. $ids = [$this->auth->id];
  1670. } elseif ($type == 2) {//下属
  1671. $ids = Staff::getLowerStaffId();
  1672. } else {//全部
  1673. $ids = Staff::getMyStaffIds();
  1674. }
  1675. //跟进次数
  1676. $r= Record::where([
  1677. 'create_staff_id' => ['in',$ids],
  1678. 'relation_type' =>1,
  1679. 'createtime' => [
  1680. 'between',
  1681. $between
  1682. ]
  1683. ])->field("create_staff_id,count(*) as count")->group('create_staff_id')->select();
  1684. $records = [];
  1685. foreach ($r as $v) {
  1686. $records[$v['create_staff_id']] = $v['count'];
  1687. }
  1688. arsort($records);
  1689. $staffs = Staff::getKeyList();
  1690. $result = [];
  1691. $data = [];
  1692. $i = 1;
  1693. foreach ($records as $id => $number) {
  1694. $val = $staffs[$id];
  1695. $val['number'] = $number;
  1696. $val['rank'] = $i;
  1697. $data[]=[
  1698. 'name'=>$val['name'],
  1699. 'number'=>$number
  1700. ];
  1701. $i++;
  1702. $result[] = $val;
  1703. }
  1704. $this->success('请求成功',['ranking' => $result, 'data' => $data]);
  1705. }
  1706. /**
  1707. * 获取业绩详情
  1708. */
  1709. public function getAchievementDetail()
  1710. {
  1711. $year = input('year', date('Y'));
  1712. $status = input('status', 1);
  1713. $staff_id = input('staff_id', '');
  1714. $type = input('type', 3);//3员工 2团队
  1715. if (empty($staff_id)) {
  1716. $this->error('员工不能为空');
  1717. }
  1718. if($type == 2){
  1719. $staff_id = $this->auth->id;
  1720. $ids = Staff::getMyStaffIds();
  1721. }else{
  1722. $ids = $staff_id;
  1723. }
  1724. $achievements = Achievement::where(['type' => $type, 'obj_id' => ['in', $staff_id],
  1725. 'year' => $year, 'status' => $status])
  1726. ->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();
  1727. if ($status == 1) {//合同金额
  1728. $contracts = ContractRatio::where([
  1729. 'contract.check_status' => 2, 'contract_ratio.staff_id' => ['in', $ids],
  1730. 'contract.order_date' => ['like', $year . '%']
  1731. ])->with(['contract'])->select();
  1732. $contracts = collection($contracts)->toArray();
  1733. $contractData = [];
  1734. foreach ($contracts as $v) {
  1735. $order_date = $v['contract']['order_date'];
  1736. $month = date('Y-m', strtotime($order_date));
  1737. $contractData[$month]['money'][] = $v['ratio_money'];
  1738. $contractData[$month]['contract_id'][] = $v['contract_id'];
  1739. }
  1740. $list = [];
  1741. foreach ($contractData as $month => $v) {
  1742. $list[$month] = ['money' => array_sum($v['money']), 'count' => count($v['contract_id'])];
  1743. }
  1744. } else if ($status == 2) {
  1745. $receivables = Receivables::where([
  1746. 'owner_staff_id' => ['in', $ids],
  1747. 'check_status' => 2,
  1748. 'return_time' => ['like', $year . '%'],
  1749. ])->group('month')
  1750. ->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"%Y-%m") as month,count(*) as count')->select();
  1751. $list = [];
  1752. foreach ($receivables as $v) {
  1753. $list[$v['month']] = $v;
  1754. }
  1755. }elseif ($status == 3) {//客户
  1756. $where['createtime'] = ['between',[strtotime($year.'-01-01'),
  1757. strtotime($year.'-12-31')]];
  1758. $where['create_staff_id'] = ['in',$ids];
  1759. $moneys = Customer::where($where)
  1760. ->field('count(*) as count,FROM_UNIXTIME(createtime,"%Y-%m") as month')
  1761. ->group('month')->select();
  1762. $list = [];
  1763. foreach ($moneys as $v) {
  1764. $list[$v['month']] = $v;
  1765. }
  1766. } elseif ($status == 4) {//拜访
  1767. $where['createtime'] = ['between',[strtotime($year.'-01-01'),
  1768. strtotime($year.'-12-31')]];
  1769. $where['staff_id'] = ['in',$staff_id];
  1770. $where['type'] = 3;
  1771. $where['status'] = 2;
  1772. $moneys = Event::where($where)->field('count(*) as count,FROM_UNIXTIME(actual_end_time,"%Y-%m") as month')
  1773. ->group('month')->select();
  1774. $list = [];
  1775. foreach ($moneys as $v) {
  1776. $list[$v['month']] = $v;
  1777. }
  1778. }
  1779. $data = [];
  1780. $echartdata = [];
  1781. foreach ($achievements as $k => $v) {
  1782. if ($month = Achievement::getFieldMonth($k)) {
  1783. $month = $year . '-' . $month;
  1784. $row = [
  1785. 'month' => $month,
  1786. 'achievement' => $v ?? 0,
  1787. 'money' => isset($list[$month]) ? $list[$month]['money'] : 0,
  1788. 'count' => isset($list[$month]) ? $list[$month]['count'] : 0,
  1789. ];
  1790. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  1791. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  1792. $echartdata['month'][] = $row['month'];
  1793. $echartdata['achievement'][] = $row['achievement']??0;
  1794. $echartdata['money'][] = $row['money'];
  1795. $data[] = $row;
  1796. }
  1797. }
  1798. $money = isset($echartdata['money']) ? array_sum($echartdata['money']) : 0;
  1799. $ratio = ($money && intval($achievements['yeartarget'])) ? sprintf("%.2f", $money / $achievements['yeartarget'] * 100) : 0;
  1800. $yeartarget = [
  1801. 'year' => $year,
  1802. 'money' => $money,
  1803. 'achievement' => $achievements['yeartarget'] ?? 0,
  1804. 'ratio' => $ratio
  1805. ];
  1806. $this->success('请求成功', ['data' => $data, 'echartdata' => $echartdata, 'yeartarget' => $yeartarget]);
  1807. }
  1808. //客户统计
  1809. public function customerStatistics(){
  1810. $times = input('times');
  1811. $type = input('type', 2);//2下属 1本人 0团队
  1812. if (empty($times)) {
  1813. $this->error('筛选时间不能为空');
  1814. }
  1815. $times = setTimes($times, 'datetime');
  1816. $startTime = strtotime($times[0]);
  1817. $endTime = strtotime($times[1]);
  1818. $where = ['createtime' => ['between', [$startTime, $endTime]]];
  1819. $whereR = ['createtime' => ['between', [$startTime, $endTime]]];
  1820. if ($type == 1) {
  1821. $where['owner_staff_id'] = $this->auth->id;
  1822. $whereR['create_staff_id'] = $this->auth->id;
  1823. } elseif ($type == 2) {
  1824. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  1825. $whereR['create_staff_id'] = ['in', Staff::getLowerStaffId()];
  1826. } else {
  1827. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  1828. $whereR['create_staff_id'] = ['in', Staff::getMyStaffIds()];
  1829. }
  1830. //新增客户数
  1831. $customer = Customer::where($where)->count();
  1832. //跟进客户数
  1833. $record = Record::where($whereR)->where(['relation_type'=>1,'follow_type' => ['neq', '其它']])->count();
  1834. //客户成交数
  1835. $customer_complate = Customer::where($where)->where(['contract_status' => 1])->count();
  1836. //客户未成交数
  1837. $customer_nocomplate = Customer::where($where)->where(['contract_status' => 0])->count();
  1838. //进入公海客户数
  1839. $seain = SeaOperation::where($where)->where(['type'=>1])->count();
  1840. //公海领取客户数
  1841. $seaout = SeaOperation::where($where)->where(['type'=>0])->count();
  1842. $this->success('请求成功', [
  1843. 'customer' => $customer,
  1844. 'record' => $record,
  1845. 'customer_complate' => $customer_complate,
  1846. 'customer_nocomplate' => $customer_nocomplate,
  1847. 'seain' => $seain,
  1848. 'seaout' => $seaout,]);
  1849. }
  1850. //商机统计
  1851. public function businessStatistics(){
  1852. $times = input('times');
  1853. $type = input('type', 2);//2下属 1本人 0团队
  1854. if (empty($times)) {
  1855. $this->error('筛选时间不能为空');
  1856. }
  1857. $times = setTimes($times, 'datetime');
  1858. $startTime = strtotime($times[0]);
  1859. $endTime = strtotime($times[1]);
  1860. $where = ['createtime' => ['between', [$startTime, $endTime]]];
  1861. if ($type == 1) {
  1862. $where['owner_staff_id'] = $this->auth->id;
  1863. } elseif ($type == 2) {
  1864. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  1865. } else {
  1866. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  1867. }
  1868. //商机总金额
  1869. $business_money = Business::where($where)->sum('money');
  1870. //商机总数
  1871. $business_num = Business::where($where)->count();
  1872. //赢单金额
  1873. $business_complate_money = Business::where($where)->where(['contract_status' => 1])->sum('money');
  1874. //输单交金额
  1875. $business_nocomplate_money = Business::where($where)->where(['contract_status' => 0])->sum('money');
  1876. //赢单数
  1877. $business_complate = Business::where($where)->where(['contract_status' => 1])->count();
  1878. //输单数
  1879. $business_nocomplate = Business::where($where)->where(['contract_status' => 0])->count();
  1880. $this->success('请求成功', [
  1881. 'business_money' => $business_money,
  1882. 'business_num' => $business_num,
  1883. 'business_complate_money' => $business_complate_money,
  1884. 'business_nocomplate_money' => $business_nocomplate_money,
  1885. 'business_complate' => $business_complate,
  1886. 'business_nocomplate' => $business_nocomplate,]);
  1887. }
  1888. //合同统计
  1889. public function contractStatistics(){
  1890. $times = input('times');
  1891. $type = input('type', 2);//2下属 1本人 0团队
  1892. if (empty($times)) {
  1893. $this->error('筛选时间不能为空');
  1894. }
  1895. if($times == 'thisyear' || $times == 'lastyear' || $times == 'nextyear'){
  1896. $timeinfo ='%Y-%m';
  1897. }else{
  1898. $timeinfo ='%Y-%m-%d';
  1899. }
  1900. $times = setTimes($times, 'datetime');
  1901. $startTime = strtotime($times[0]);
  1902. $endTime = strtotime($times[1]);
  1903. $where = ['order_date' => ['between', [date('Y-m-d 00:00:00',$startTime), date('Y-m-d 23:59:59',$endTime)]]];
  1904. $whereR = ['return_time' => ['between', [date('Y-m-d 00:00:00',$startTime), date('Y-m-d 23:59:59',$endTime)]]];
  1905. if ($type == 1) {
  1906. $where['owner_staff_id'] = $this->auth->id;
  1907. $whereR['owner_staff_id'] = $this->auth->id;
  1908. } elseif ($type == 2) {
  1909. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  1910. $whereR['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  1911. } else {
  1912. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  1913. $whereR['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  1914. }
  1915. $where['check_status']= 2;
  1916. //合同总金额
  1917. $contract_money = Contract::where($where)->sum('money');
  1918. //签约合同数
  1919. $contract_num = Contract::where($where)->count();
  1920. //回款总金额
  1921. $receivables_money = Receivables::where($whereR)->count();
  1922. //未回款总金额
  1923. $no_money = $contract_money -$receivables_money;
  1924. if($no_money <= 0){
  1925. $no_money = 0;
  1926. }
  1927. $receivables_nomoney = $no_money;
  1928. //已到期的合同
  1929. $contract_over = Contract::where($where)->where(['end_time' => array('gt',date('Y-m-d'))])->sum('money');
  1930. //即将到期的合同
  1931. $contract_now = Contract::where($where)->where(['end_time' => array('elt',date('Y-m-d'))])->count();
  1932. $contract = Contract::where($where)->group('month')->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),"'.$timeinfo.'") as month')->select();
  1933. $contracts_list = [];
  1934. foreach ($contract as $v) {
  1935. $contracts_list[$v['month']] = $v['money'];
  1936. }
  1937. $receivables = Receivables::where($whereR)->where([
  1938. 'check_status' => 2,
  1939. ])->group('dates')->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"'.$timeinfo.'") as dates')->select();
  1940. $receivables_list = [];
  1941. foreach ($receivables as $v) {
  1942. $receivables_list[$v['dates']] = $v['money'];
  1943. }
  1944. $data = [];
  1945. foreach ($contracts_list as $k => $v) {
  1946. $row = array(
  1947. 'month'=>$k,
  1948. 'contract'=>$v,
  1949. 'receivables'=>isset($receivables_list[$k]) ? $receivables_list[$k] : 0,
  1950. );
  1951. $data[] = $row;
  1952. }
  1953. $this->success('请求成功', [
  1954. 'contract_money' => $contract_money,
  1955. 'receivables_money' => $receivables_money,
  1956. 'receivables_nomoney' => $receivables_nomoney,
  1957. 'contract_num' => $contract_num,
  1958. 'contract_over' => $contract_over,
  1959. 'contract_now' => $contract_now,
  1960. 'contract' => $data,]);
  1961. }
  1962. }