Achievement.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace app\admin\controller\qingdong\department;
  3. use addons\qingdong\model\Achievement as AchievementModel;
  4. use addons\qingdong\model\AchievementRecords;
  5. use addons\qingdong\model\ContractRatio;
  6. use addons\qingdong\model\ExamineRecord;
  7. use addons\qingdong\model\Flow;
  8. use addons\qingdong\model\Receivables;
  9. use app\admin\controller\qingdong\Base;
  10. use addons\qingdong\model\StaffDepartment;
  11. use addons\qingdong\model\Staff;
  12. use think\DB;
  13. use think\Exception;
  14. /**
  15. * 业绩目标
  16. */
  17. class Achievement extends Base
  18. {
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \addons\qingdong\model\Achievement();
  23. }
  24. /**
  25. * 业绩目标列表
  26. * @return string|\think\response\Json
  27. */
  28. public function index()
  29. {
  30. $this->request->filter(['strip_tags']);
  31. if ($this->request->isAjax()) {
  32. $filter = $this->request->get("filter", '');
  33. $filter = (array)json_decode($filter, true);
  34. $year = $filter['year'];
  35. $status = $filter['status'];
  36. $staff_id = $filter['staff_id'] ?? 0;
  37. $where = [];
  38. if ($staff_id) {
  39. $where['obj_id'] = $staff_id;
  40. }else{
  41. $where['obj_id']=['in',Staff::getMyStaffIds()];
  42. }
  43. $achievements=AchievementModel::where(['type'=>3,'year'=>$year,'status'=>$status])
  44. ->where($where)
  45. ->with(['staff'])
  46. ->select();
  47. $achievements=collection($achievements)->toArray();
  48. $ids=[];
  49. foreach ($achievements as $v){
  50. $ids[]=$v['obj_id'];
  51. }
  52. if($status == 1){//合同金额
  53. $contracts=ContractRatio::where([
  54. 'contract.check_status'=>2, 'contract_ratio.staff_id' => ['in',$ids],
  55. 'contract.order_date'=>['like', $year . '%']
  56. ])->with(['contract'])->select();
  57. $contracts=collection($contracts)->toArray();
  58. $contractData=[];
  59. foreach ($contracts as $v) {
  60. $order_date = $v['contract']['order_date'];
  61. $month = date('Y-m', strtotime($order_date));
  62. $contractData[$v['staff_id']][$month]['money'][] = $v['ratio_money'];
  63. }
  64. }else{
  65. $receivables = Receivables::where([
  66. 'owner_staff_id' => ['in',$ids],
  67. 'check_status' => 2,
  68. 'return_time' => ['like', $year . '%'],
  69. ])->select();
  70. $contracts=collection($receivables)->toArray();
  71. $contractData=[];
  72. foreach ($contracts as $v) {
  73. $return_time = $v['return_time'];
  74. $month = date('Y-m', strtotime($return_time));
  75. $contractData[$v['owner_staff_id']][$month]['money'][] = $v['money'];
  76. }
  77. }
  78. $data=[];
  79. foreach ($contractData as $owner_staf_id=>$d){
  80. foreach ($d as $month=>$money){
  81. $data[$owner_staf_id][$month]=array_sum($money['money']);
  82. }
  83. }
  84. foreach ($achievements as $k=>$v) {
  85. $v['january_money'] = $data[$v['obj_id']][$year . '-01'] ?? 0;
  86. $v['february_money'] = $data[$v['obj_id']][$year . '-02'] ?? 0;
  87. $v['march_money'] = $data[$v['obj_id']][$year . '-03'] ?? 0;
  88. $v['april_money'] = $data[$v['obj_id']][$year . '-04'] ?? 0;
  89. $v['may_money'] = $data[$v['obj_id']][$year . '-05'] ?? 0;
  90. $v['june_money'] = $data[$v['obj_id']][$year . '-06'] ?? 0;
  91. $v['july_money'] = $data[$v['obj_id']][$year . '-07'] ?? 0;
  92. $v['august_money'] = $data[$v['obj_id']][$year . '-08'] ?? 0;
  93. $v['september_money'] = $data[$v['obj_id']][$year . '-09'] ?? 0;
  94. $v['october_money'] = $data[$v['obj_id']][$year . '-10'] ?? 0;
  95. $v['november_money'] = $data[$v['obj_id']][$year . '-11'] ?? 0;
  96. $v['december_money'] = $data[$v['obj_id']][$year . '-12'] ?? 0;
  97. $v['year_money'] = $v['january_money'] + $v['february_money'] + $v['march_money'] + $v['april_money'] + $v['may_money'] + $v['june_money'] + $v['july_money'] + $v['august_money'] + $v['september_money'] + $v['october_money'] + $v['november_money'] + $v['december_money'];
  98. $v['january_ratio']= ($v['january_money'] && intval($v['january'])) ?
  99. sprintf("%.2f", $v['january_money'] / $v['january'] * 100) .'%': 0;
  100. $v['february_ratio']= ($v['february_money'] && intval($v['february'])) ?
  101. sprintf("%.2f", $v['february_money'] / $v['february'] * 100) .'%': 0;
  102. $v['march_ratio']= ($v['march_money'] && intval($v['march'])) ?
  103. sprintf("%.2f", $v['march_money'] / $v['march'] * 100) .'%': 0;
  104. $v['april_ratio']= ($v['april_money'] && intval($v['april'])) ?
  105. sprintf("%.2f", $v['april_money'] / $v['april'] * 100) .'%' : 0;
  106. $v['may_ratio']= ($v['may_money'] && intval($v['may'])) ?
  107. sprintf("%.2f", $v['may_money'] / $v['may'] * 100) .'%': 0;
  108. $v['june_ratio']= ($v['june_money'] && intval($v['june'])) ?
  109. sprintf("%.2f", $v['june_money'] / $v['june'] * 100) .'%': 0;
  110. $v['july_ratio']= ($v['july_money'] && intval($v['july'])) ?
  111. sprintf("%.2f", $v['july_money'] / $v['july'] * 100) .'%': 0;
  112. $v['august_ratio']= ($v['august_money'] && intval($v['august'])) ?
  113. sprintf("%.2f", $v['august_money'] / $v['august'] * 100) .'%' : 0;
  114. $v['september_ratio']= ($v['september_money'] && intval($v['september'])) ?
  115. sprintf("%.2f", $v['september_money'] / $v['september'] * 100) .'%' : 0;
  116. $v['october_ratio']= ($v['october_money'] && intval($v['october'])) ?
  117. sprintf("%.2f", $v['october_money'] / $v['october'] * 100) .'%': 0;
  118. $v['november_ratio']= ($v['november_money'] && intval($v['november'])) ?
  119. sprintf("%.2f", $v['november_money'] / $v['november'] * 100) .'%' : 0;
  120. $v['december_ratio']= ($v['december_money'] && intval($v['december'])) ?
  121. sprintf("%.2f", $v['december_money'] / $v['december'] * 100) .'%': 0;
  122. $v['year_ratio']= ($v['year_money'] && intval($v['yeartarget'])) ?
  123. sprintf("%.2f", $v['year_money'] / $v['yeartarget'] * 100) .'%' : 0;
  124. $v['one']=sprintf("%.2f",($v['january'] + $v['february'] + $v['march']));
  125. $v['two']= sprintf("%.2f",($v['april'] + $v['may'] + $v['june']));
  126. $v['three']=sprintf("%.2f",($v['july'] + $v['august'] + $v['september']));
  127. $v['four']=sprintf("%.2f",($v['october'] + $v['november'] + $v['december']));
  128. $v['one_money']=$v['january_money'] + $v['february_money'] + $v['march_money'];
  129. $v['two_money']=$v['april_money'] + $v['may_money'] + $v['june_money'];
  130. $v['three_money']=$v['july_money'] + $v['august_money'] + $v['september_money'];
  131. $v['four_money']=$v['october_money'] + $v['november_money'] + $v['december_money'];
  132. $v['one_ratio']= ($v['one_money'] && $v['one']>0) ?
  133. sprintf("%.2f", $v['one_money'] / $v['one'] * 100) .'%': 0;
  134. $v['two_ratio']= ($v['two_money'] && $v['two']>0) ?
  135. sprintf("%.2f", $v['two_money'] / $v['two'] * 100) .'%': 0;
  136. $v['three_ratio']= ($v['three_money'] && $v['three']>0) ?
  137. sprintf("%.2f", $v['three_money'] / $v['three'] * 100) .'%' : 0;
  138. $v['four_ratio']= ($v['four_money'] && $v['four']>0) ?
  139. sprintf("%.2f", $v['four_money'] / $v['four'] * 100) .'%': 0;
  140. $achievements[$k]=$v;
  141. }
  142. $result = array("total" => count($achievements), "rows" => $achievements);
  143. return json($result);
  144. }
  145. $this->assign('department', StaffDepartment::getList());
  146. $staffs = Staff::where(['id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
  147. $this->assign('staffs', $staffs);
  148. $this->assign('years', getYears());
  149. return $this->view->fetch();
  150. }
  151. /**
  152. * 个人目标
  153. */
  154. public function personal()
  155. {
  156. $this->request->filter(['strip_tags']);
  157. if ($this->request->isAjax()) {
  158. $filter = $this->request->get("filter", '');
  159. $filter = (array)json_decode($filter, true);
  160. $year = $filter['year'];
  161. $status = $filter['status'];
  162. $list = Staff::where(['id'=>$this->_staff->id])->with([
  163. "achievement" => function ($query) use ($year, $status) {
  164. $query->where(['year' => $year, 'status' => $status]);
  165. }
  166. ])->paginate();
  167. $rows = $list->items();
  168. foreach ($rows as $v) {
  169. if (empty($v['achievement'])) {
  170. $v['all'] = 0;
  171. $v['one'] = 0;
  172. $v['two'] = 0;
  173. $v['three'] = 0;
  174. $v['four'] = 0;
  175. } else {
  176. $ach = $v['achievement'];
  177. $v['all'] = round($ach['january'] + $ach['february'] + $ach['march'] + $ach['april'] + $ach['may'] + $ach['june'] + $ach['july'] + $ach['august'] + $ach['september'] + $ach['october'] + $ach['november'] + $ach['december'], 2);
  178. //季度
  179. $v['one'] = round($ach['january'] + $ach['february'] + $ach['march'], 2);
  180. $v['two'] = round($ach['april'] + $ach['may'] + $ach['june'], 2);
  181. $v['three'] = round($ach['july'] + $ach['august'] + $ach['september'], 2);
  182. $v['four'] = round($ach['october'] + $ach['november'] + $ach['december'], 2);
  183. }
  184. $v['year'] = $year;
  185. $v['status'] = $status;
  186. }
  187. $result = array("total" => $list->total(), "rows" => $rows);
  188. return json($result);
  189. }
  190. $this->assign('department', StaffDepartment::getList());
  191. $staffs = Staff::where(['id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
  192. $this->assign('staffs', $staffs);
  193. $this->assign('years', getYears());
  194. return $this->view->fetch();
  195. }
  196. /**
  197. * 业绩目标修改日志
  198. * @return string|\think\response\Json
  199. */
  200. public function records()
  201. {
  202. $this->request->filter(['strip_tags']);
  203. if ($this->request->isAjax()) {
  204. $staff_id=$this->_staff->id;
  205. $list= AchievementRecords::where(['owner_staff_id'=>$staff_id])->with(['staff'])->order('id desc')->paginate();
  206. $result = array("total" => $list->total(), "rows" => $list->items());
  207. return json($result);
  208. }
  209. return $this->view->fetch();
  210. }
  211. /**
  212. * 新建业绩目标
  213. * @param null $ids
  214. * @return string
  215. */
  216. public function add()
  217. {
  218. if ($this->request->isAjax()) {
  219. $params = $this->request->post('row/a');
  220. $params = $this->preExcludeFields($params);
  221. $staff_ids=$params['staff_ids'];
  222. unset($params['staff_ids']);
  223. $staff_ids=explode(',',$staff_ids);
  224. $data=[];
  225. $flow = Flow::getsteplist(Flow::ACHIEVEMENT_STATUS);
  226. foreach ($staff_ids as $sid) {
  227. $params['obj_id'] = $sid;
  228. $params['type'] = 3;
  229. $params['flow_id'] = $flow['flow_id'];
  230. $params['order_id'] = $flow['order_id'];
  231. $params['owner_staff_id']=$this->_staff->id;
  232. if ($flow['status'] == 0) {//发起人自选
  233. if (empty($params['flow_staff_ids'])) {
  234. $this->error('审批人必须选择');
  235. }
  236. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  237. } else {
  238. if (empty($flow['flow_staff_ids'])) {
  239. $this->error('没有直属上级无法审批,请联系管理员!');
  240. }
  241. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  242. }
  243. $data[] = $params;
  244. }
  245. $result = false;
  246. Db::startTrans();
  247. try {
  248. foreach ($data as $params){
  249. $model = new AchievementRecords();
  250. if ($model->save($params) === false) {
  251. throw new Exception('添加失败');
  252. }
  253. $lastId = $model->getLastInsID();
  254. if ($flow['status'] == 1) {//固定审批
  255. //发送审批通知
  256. Flow::sendStepRecord($flow,Flow::ACHIEVEMENT_STATUS, $lastId);
  257. } else {//发起人自选 依次审批
  258. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  259. if ($staff_id) {
  260. ExamineRecord::addExaminse(ExamineRecord::ACHIEVEMENT_TYPE, $lastId, $staff_id);
  261. }
  262. }
  263. }
  264. Db::commit();
  265. } catch (Exception $e) {
  266. Db::rollback();
  267. $this->error($e->getMessage());
  268. }
  269. $this->success('提交成功,等待审核中');
  270. }
  271. $flow=Flow::getsteplist(Flow::ACHIEVEMENT_STATUS);
  272. $this->assign('flow',$flow);
  273. $this->assign('department', StaffDepartment::getList());
  274. $staffs = Staff::where(['id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
  275. $this->assign('staffs', $staffs);
  276. $this->assign('years', getYears());
  277. return $this->view->fetch();
  278. }
  279. /**
  280. * 修改业绩目标
  281. * @param null $ids
  282. * @return string
  283. */
  284. public function edit($ids = null)
  285. {
  286. if ($this->request->isAjax()) {
  287. $params = $this->request->post('row/a');
  288. $params = $this->preExcludeFields($params);
  289. Db::startTrans();
  290. try {
  291. $model = new AchievementRecords();
  292. $flow = Flow::getsteplist(Flow::ACHIEVEMENT_STATUS);
  293. $params['flow_id'] = $flow['flow_id'];
  294. $params['order_id'] = $flow['order_id'];
  295. $params['owner_staff_id']=$this->_staff->id;
  296. if ($flow['status'] == 0) {//发起人自选
  297. if (empty($params['flow_staff_ids'])) {
  298. throw new Exception('审批人必须选择');
  299. }
  300. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  301. } else {
  302. if (empty($flow['flow_staff_ids'])) {
  303. throw new Exception('没有直属上级无法审批,请联系管理员!');
  304. }
  305. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  306. }
  307. if ($model->save($params) === false) {
  308. throw new Exception('添加失败');
  309. }
  310. $lastId = $model->getLastInsID();
  311. if ($flow['status'] == 1) {//固定审批
  312. //发送审批通知
  313. Flow::sendStepRecord($flow,Flow::ACHIEVEMENT_STATUS, $lastId);
  314. } else {//发起人自选 依次审批
  315. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  316. if ($staff_id) {
  317. ExamineRecord::addExaminse(ExamineRecord::ACHIEVEMENT_TYPE, $lastId, $staff_id);
  318. }
  319. }
  320. Db::commit();
  321. } catch (Exception $e) {
  322. Db::rollback();
  323. $this->error($e->getMessage());
  324. }
  325. $this->success('提交成功,等待审核中');
  326. }
  327. $row=AchievementModel::where(['id' => $ids])->with(['staff'])->find();
  328. $flow=Flow::getsteplist(Flow::ACHIEVEMENT_STATUS);
  329. $this->assign('flow',$flow);
  330. $this->assign('row', $row);
  331. return $this->view->fetch();
  332. }
  333. /**
  334. * 获取审批人列表
  335. */
  336. public function getstaff(){
  337. $pageSize = input('pageSize');
  338. $pageNumber = input('pageNumber');
  339. $where = [];
  340. if ($keyValue = $this->request->request("keyValue")) {
  341. $where['id'] = ['in',$keyValue];
  342. }
  343. $name = input('name');
  344. if(!empty($name)){
  345. $where['name'] = ['like','%'.$name.'%'];
  346. }
  347. $staff = Staff::where($where)->field('id,name')->order('id desc')->paginate($pageSize, false, ['page' => $pageNumber]);
  348. return json(['list' => $staff->items(), 'total' => $staff->total()]);
  349. }
  350. /**
  351. * 团队目标
  352. * @param null $ids
  353. * @return string
  354. */
  355. public function edit_personal($ids = null)
  356. {
  357. $year = input('year');
  358. $status = input('status');
  359. if (empty($year) || empty($status)) {
  360. $this->error('参数错误');
  361. }
  362. if ($this->request->isAjax()) {
  363. $params = $this->request->post('row/a');
  364. $achievements = $this->preExcludeFields($params);
  365. $type = 3;//员工
  366. $flow_staff_id = input('flow_staff_ids');
  367. $model = new AchievementRecords();
  368. if (isset($achievements['yeartarget'])) {
  369. unset($achievements['yeartarget']);
  370. }
  371. $id = $this->_staff->id;
  372. $params = [
  373. 'type' => $type,
  374. 'year' => $year,
  375. 'status' => $status,
  376. 'obj_id' => $id,
  377. 'january' => $achievements['january'] ?? 0,
  378. 'february' => $achievements['february'] ?? 0,
  379. 'march' => $achievements['march'] ?? 0,
  380. 'april' => $achievements['april'] ?? 0,
  381. 'june' => $achievements['june'] ?? 0,
  382. 'may' => $achievements['may'] ?? 0,
  383. 'july' => $achievements['july'] ?? 0,
  384. 'august' => $achievements['august'] ?? 0,
  385. 'september' => $achievements['september'] ?? 0,
  386. 'october' => $achievements['october'] ?? 0,
  387. 'november' => $achievements['november'] ?? 0,
  388. 'december' => $achievements['december'] ?? 0,
  389. 'flow_staff_ids' => $flow_staff_id,
  390. 'owner_staff_id'=> $id,
  391. 'check_status' => 0,
  392. 'yeartarget' => array_sum(array_values($achievements)),
  393. 'createtime' => time(),
  394. 'updatetime' => time()
  395. ];
  396. Db::startTrans();
  397. try {
  398. if ($model->insert($params) == false) {
  399. throw new Exception('添加失败');
  400. }
  401. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  402. $lastId = $model->getLastInsID();
  403. if ($staff_id) {
  404. ExamineRecord::addExaminse(ExamineRecord::ACHIEVEMENT_TYPE, $lastId, $staff_id);
  405. }
  406. Db::commit();;
  407. } catch (Exception $e) {
  408. Db::rollback();
  409. $this->error($e->getMessage());
  410. }
  411. $this->success('提交目标成功,等待审核!');
  412. }
  413. $row = Staff::where(['id' => $ids])->with([
  414. "achievement" => function ($query) use ($year, $status) {
  415. $query->where(['year' => $year, 'status' => $status]);
  416. }
  417. ])->find();
  418. $this->assign('staff', Staff::getStaff());
  419. $this->assign('row', $row);
  420. $this->assign('year', $year);
  421. $this->assign('status', $status);
  422. return $this->view->fetch();
  423. }
  424. }