Achievement.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller\qingdongams\statistic;
  3. use addons\qingdongams\model\Contract;
  4. use addons\qingdongams\model\ContractRatio;
  5. use addons\qingdongams\model\Receivables;
  6. use app\common\controller\Backend;
  7. use addons\qingdongams\model\Achievement as AchievementModel;
  8. use addons\qingdongams\model\Staff;
  9. /**
  10. * 业绩统计
  11. */
  12. class Achievement extends Backend
  13. {
  14. /**
  15. * 业绩目标完成情况
  16. */
  17. public function index()
  18. {
  19. $row = input('row/a');
  20. if (isset($row['year']) && $row['year']) {
  21. $year = $row['year'];
  22. } else {
  23. $year = date('Y');
  24. }
  25. if (isset($row['status']) && $row['status']) {
  26. $status=$row['status'];
  27. }else{
  28. $status=1;
  29. }
  30. if (isset($row['type']) && $row['type']) {
  31. $type=$row['type'];
  32. }else{
  33. $type=0;
  34. }
  35. $staff = Staff::info();
  36. if ($type == 1) {//本人
  37. $ids = [$staff->id];
  38. } elseif ($type == 2) {//下属
  39. $ids = Staff::getLowerStaffId();
  40. } else {//全部
  41. $ids = Staff::getMyStaffIds();
  42. }
  43. if (isset($row['staff_id']) && $row['staff_id']) {
  44. $ids=$staff_id=$row['staff_id'];
  45. }else{
  46. $staff_id='';
  47. }
  48. $achievements=AchievementModel::where(['type'=>3,'obj_id'=>['in',$ids],'year'=>$year,'status'=>$status])
  49. ->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();
  50. if($status == 1){//合同金额
  51. $contracts=ContractRatio::where([
  52. 'contract.check_status'=>2, 'contract_ratio.staff_id' => ['in',$ids],
  53. 'contract.order_date'=>['like', $year . '%']
  54. ])->with(['contract'])->select();
  55. $contracts=collection($contracts)->toArray();
  56. $contractData=[];
  57. foreach ($contracts as $v) {
  58. $order_date = $v['contract']['order_date'];
  59. $month = date('Y-m', strtotime($order_date));
  60. $contractData[$month]['money'][] = $v['ratio_money'];
  61. $contractData[$month]['contract_id'][] = $v['contract_id'];
  62. }
  63. $list = [];
  64. foreach ($contractData as $month=>$v) {
  65. $list[$month] = ['money'=>array_sum($v['money']),'count'=>count($v['contract_id'])];
  66. }
  67. }else{
  68. $receivables = Receivables::where([
  69. 'owner_staff_id' => ['in',$ids],
  70. 'check_status' => 2,
  71. 'return_time' => ['like', $year . '%'],
  72. ])->group('month')
  73. ->field('sum(money) as money,FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),"%Y-%m") as month,count(*) as count')->select();
  74. $list = [];
  75. foreach ($receivables as $v) {
  76. $list[$v['month']] = $v;
  77. }
  78. }
  79. $data = [];
  80. $echartdata=[];
  81. foreach ($achievements as $k => $v) {
  82. if ($month = AchievementModel::getFieldMonth($k)) {
  83. $month = $year . '-' . $month;
  84. $row = [
  85. 'month' => $month,
  86. 'achievement' => $v,
  87. 'money' => isset($list[$month]) ? $list[$month]['money'] : 0,
  88. 'count' => isset($list[$month]) ? $list[$month]['count'] : 0,
  89. ];
  90. $row['ratio'] = ($row['money'] && intval($row['achievement'])) ? sprintf("%.2f", $row['money'] / $row['achievement'] * 100) : 0;
  91. $row['unit_price'] = ($row['money'] && $row['count']) ? sprintf("%.2f", $row['money'] / $row['count']) : 0;
  92. $echartdata['month'][] = $row['month'];
  93. $echartdata['achievement'][] = $row['achievement'];
  94. $echartdata['money'][] = $row['money'];
  95. $data[] = $row;
  96. }
  97. }
  98. $this->view->assign([
  99. 'data' => $data,
  100. 'echartdata' => $echartdata,
  101. 'status' => $status,
  102. 'year' => $year,
  103. 'type' => $type,
  104. 'staff_id' => $staff_id,
  105. ]);
  106. return $this->view->fetch();
  107. }
  108. }