model = new \app\admin\model\salary\Detail; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); $items = Db::name('salary_item')->field('id,attr,type,name')->select(); $salaryItem = []; foreach ($items as $item) { $idAttr[$item['id']] = $item['attr']; if ($item['attr'] == 'JXKHJL') { $salaryItem[] = [ 'id' => 'jx_total', 'name' => '绩效提成', ]; } $salaryItem[] = [ 'id' => 'item_' . $item['id'], 'name' => $item['name'], ]; } $salaryItem[] = [ 'id' => 'total', 'name' => '总计', ]; $month = date('Y-m-01'); if ($this->request->isAjax()) { $filter = $this->request->param('filter'); $filter = json_decode($filter, true); if (!empty($filter['month'])) { $month = $filter['month'] . '-01'; } $res = []; if (!$this->auth->check('salary/custom_detail/edit')) { $admins = Db::name('admin a') ->join('fa_salary_status ss', 'a.id = fa_salary_status.target_admin_id') ->where('create_status', 1) ->where('settle_status', 1) ->where('salary_month', '=', $month) ->where('a.id', $this->auth->id) ->field('a.id,a.nickname,ss.settle_status')->select(); } else{ $admins = Db::name('admin a') ->join('fa_salary_status ss', 'a.id = fa_salary_status.target_admin_id') ->where('create_status', 1) ->where('salary_month', '=', $month) ->field('a.id,a.nickname,ss.settle_status')->select(); } $admins = array_column($admins, NULL, 'id'); $adminIds = array_keys($admins); $adminNames = array_column($admins, 'nickname', 'id'); $builder = $this->model ->where('salary_month', '=', $month); if (!$this->auth->check('salary/custom_detail/edit')) { $builder = $builder->where('target_admin_id', $this->auth->id); } $queryData = $builder->select(); $queryRes = []; foreach ($queryData as $queryDatum) { $targetAdminId = $queryDatum['target_admin_id']; $itemId = $queryDatum['item_id']; $queryRes[$targetAdminId][$itemId] = $queryDatum['item_value']; } foreach ($adminIds as $adminId) { $attrValue = []; $total = 0; foreach ($items as $item) { $res[$adminId]['target_admin_id'] = $adminId; $res[$adminId]['name'] = $adminNames[$adminId]; $res[$adminId]['month'] = $month; $itemKey = 'item_' . $item['id']; $value = 0; if (!empty($queryRes[$adminId][$item['id']])) { $value = $this->clean_number($queryRes[$adminId][$item['id']]); $total = $this->calc_total($total, $item['type'], $queryRes[$adminId][$item['id']]); } $attrValue[$item['attr']] = $value; if ($item['attr'] == 'TD') { $value = $value . '%'; } $res[$adminId][$itemKey] = $value; } $jxTotal = ($attrValue['ZWJX'] - $attrValue['YYCB']) * $attrValue['TD'] / 100; $res[$adminId]['jx_total'] = $this->clean_number($jxTotal); $res[$adminId]['total'] = $this->clean_number($total + $jxTotal); $res[$adminId]['settle_status'] = $admins[$adminId]['settle_status'] ?? 0; } $res = array_values($res); $result = array("total" => count($res), "rows" => $res); return json($result); } $this->assignconfig("edit", $this->auth->check('salary/custom_detail/edit')); $this->assignconfig("copy", $this->auth->check('salary/custom_detail/copy')); $this->assignconfig("settle", $this->auth->check('salary/custom_detail/settle')); $this->view->assign("month", $month); $this->view->assign("salaryitem", json_encode($salaryItem)); return $this->view->fetch(); } public function calc_total($total, $type, $value){ if ($type == 1) { return $total + $value; } if ($type == 2) { return $total - $value; } return $total; } public function clean_number($num): string { return rtrim(rtrim(number_format($num, 10, '.', ''), '0'), '.'); } public function add() { if (false === $this->request->isPost()) { $items = Db::name('salary_item')->field('id,type,name')->select(); $typeMap = [ 1 => '应发工资', 2 => '应扣款项', 3 => '其他' ]; $groupedItems = []; foreach ($items as $item) { $type = $item['type']; $label = $typeMap[$type]; $groupedItems[$label][] = $item; } $this->view->assign('groupedItems', $groupedItems); $this->view->assign('items', $items); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $month = $params['salary_month'] . '-01'; $targetAdminId = $params['target_admin_id']; $salaryDetails = []; foreach ($params as $k => $v) { if (!str_contains($k, 'item')) { continue; } $salaryDetails[] = [ 'target_admin_id' => $targetAdminId, 'salary_month' => $month, 'item_id' => explode('_', $k)[1], 'item_value' => $v, ]; } Db::name('salary_detail') ->where('salary_month', $month) ->where('target_admin_id', $targetAdminId) ->delete(); Db::name('salary_detail') ->insertAll($salaryDetails); Db::name('salary_status')->insert([ 'target_admin_id' => $targetAdminId, 'salary_month' => $month, 'settle_status' => 0, 'create_status' => 1, ]); $this->success(); } public function edit($ids = null) { if (false === $this->request->isPost()) { $targetAdminId = $this->request->get('target_admin_id'); $month = $this->request->get('month'); $row['salary_month'] = $month; $row['target_admin_id'] = $targetAdminId; $queryData = $this->model ->where('salary_month', '=', $month) ->where('target_admin_id', $targetAdminId) ->select(); $items = Db::name('salary_item')->field('id,name,type')->select(); $queryData = array_column($queryData, NULL, 'item_id'); foreach ($items as $item) { $itemKey = 'item_' . $item['id']; $row[$itemKey] = !empty($queryData[$item['id']]) ? $queryData[$item['id']]['item_value'] : 0; } $typeMap = [ 1 => '应发工资', 2 => '应扣款项', 3 => '其他' ]; $groupedItems = []; foreach ($items as $item) { $type = $item['type']; $label = $typeMap[$type]; $groupedItems[$label][] = $item; } $this->view->assign('groupedItems', $groupedItems); $this->view->assign('items', $items); $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $month = $params['salary_month'] . '-01'; $targetAdminId = $params['target_admin_id']; $salaryDetails = []; foreach ($params as $k => $v) { if (!str_contains($k, 'item')) { continue; } $salaryDetails[] = [ 'target_admin_id' => $targetAdminId, 'salary_month' => $month, 'item_id' => explode('_', $k)[1], 'item_value' => $v, ]; } Db::name('salary_detail') ->where('salary_month', $month) ->where('target_admin_id', $targetAdminId) ->delete(); Db::name('salary_detail') ->insertAll($salaryDetails); $this->success(); } public function copy($ids = null) { if (false === $this->request->isPost()) { $targetAdminId = $this->request->get('target_admin_id'); $month = $this->request->get('month'); $row['salary_month'] = $month; $row['target_admin_id'] = $targetAdminId; $queryData = $this->model ->where('salary_month', '=', $month) ->where('target_admin_id', $targetAdminId) ->select(); $items = Db::name('salary_item')->field('id,name,type')->select(); $queryData = array_column($queryData, NULL, 'item_id'); foreach ($items as $item) { $itemKey = 'item_' . $item['id']; $row[$itemKey] = !empty($queryData[$item['id']]) ? $queryData[$item['id']]['item_value'] : 0; } $typeMap = [ 1 => '应发工资', 2 => '应扣款项', 3 => '其他' ]; $groupedItems = []; foreach ($items as $item) { $type = $item['type']; $label = $typeMap[$type]; $groupedItems[$label][] = $item; } $this->view->assign('groupedItems', $groupedItems); $this->view->assign('items', $items); $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $month = $params['salary_month'] . '-01'; $targetAdminId = $params['target_admin_id']; $salaryDetails = []; foreach ($params as $k => $v) { if (!str_contains($k, 'item')) { continue; } $salaryDetails[] = [ 'target_admin_id' => $targetAdminId, 'salary_month' => $month, 'item_id' => explode('_', $k)[1], 'item_value' => $v, ]; } Db::name('salary_detail') ->where('salary_month', $month) ->where('target_admin_id', $targetAdminId) ->delete(); Db::name('salary_detail') ->insertAll($salaryDetails); Db::name('salary_status')->insert([ 'target_admin_id' => $targetAdminId, 'salary_month' => $month, 'settle_status' => 0, 'create_status' => 1, ]); $this->success(); } public function settle() { if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $targetAdminId = $this->request->get('target_admin_id'); $month = $this->request->get('month'); $settle = Db::name('salary_status') ->where('salary_month', $month) ->where('target_admin_id', $targetAdminId) ->field('target_admin_id,salary_month')->find(); if (!empty($settle)) { Db::name('salary_status') ->where('salary_month', $month) ->where('target_admin_id', $targetAdminId) ->update([ 'settle_status' => 1, ]); $this->success(); } Db::name('salary_status')->insert([ 'target_admin_id' => $targetAdminId, 'salary_month' => $month, 'settle_status' => 1, ]); $this->success(); } }