diff --git a/application/admin/controller/salary/CustomDetail.php b/application/admin/controller/salary/CustomDetail.php new file mode 100644 index 0000000..a93b948 --- /dev/null +++ b/application/admin/controller/salary/CustomDetail.php @@ -0,0 +1,347 @@ +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->isSuperAdmin()) { + $admins = Db::name('admin')->where('id', $this->auth->id)->field('id,nickname')->select(); + } else{ + $admins = Db::name('admin')->field('id,nickname')->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->isSuperAdmin()) { + $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']; + } + + $querySettleData = Db::name('salary_settle') + ->whereIn('target_admin_id', $adminIds) + ->where('salary_month', '=', $month) + ->field('id,target_admin_id,settle_status') + ->select(); + $querySettleRes = []; + foreach ($querySettleData as $querySettleDatum) { + $targetAdminId = $querySettleDatum['target_admin_id']; + $querySettleRes[$targetAdminId] = $querySettleDatum['settle_status']; + } + + + 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'] = $querySettleRes[$adminId] ?? 0; + } + + $res = array_values($res); + + $result = array("total" => count($res), "rows" => $res); + + + return json($result); + } + + $this->assignconfig("manage", $this->auth->check('salary/custom_detail/edit')); + $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); + + $this->success(); + } + + + public function edit() + { + + 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 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_settle') + ->where('salary_month', $month) + ->where('target_admin_id', $targetAdminId) + ->field('target_admin_id,salary_month')->find(); + if (!empty($settle)) { + Db::name('salary_settle') + ->where('salary_month', $month) + ->where('target_admin_id', $targetAdminId) + ->save([ + 'settle_status' => 1, + ]); + $this->success(); + } + + Db::name('salary_settle')->insert([ + 'target_admin_id' => $targetAdminId, + 'salary_month' => $month, + 'settle_status' => 1, + ]); + + $this->success(); + } + + +} diff --git a/application/admin/view/salary/custom_detail/add.html b/application/admin/view/salary/custom_detail/add.html new file mode 100644 index 0000000..25e6326 --- /dev/null +++ b/application/admin/view/salary/custom_detail/add.html @@ -0,0 +1,36 @@ +
+ +
+ +
+ +
+
+
+ +
+ +
+
+ {foreach $groupedItems as $groupName => $group} +
+ +
+ + {foreach $group as $index => $item} + {if $index % 2 == 0}
{/if} + +
+ +
+ {if $index % 2 == 1 || $index == count($group) - 1}
{/if} + {/foreach} + {/foreach} + + +
diff --git a/application/admin/view/salary/custom_detail/edit.html b/application/admin/view/salary/custom_detail/edit.html new file mode 100644 index 0000000..5a2ad7b --- /dev/null +++ b/application/admin/view/salary/custom_detail/edit.html @@ -0,0 +1,38 @@ +
+ +
+ +
+ +
+
+
+ +
+ +
+
+ {foreach $groupedItems as $groupName => $group} +
+ +
+ + {foreach $group as $index => $item} + {if $index % 2 == 0}
{/if} + +
+ +
+ {if $index % 2 == 1 || $index == count($group) - 1}
{/if} + {/foreach} + {/foreach} + + + + +
diff --git a/application/admin/view/salary/custom_detail/index.html b/application/admin/view/salary/custom_detail/index.html new file mode 100644 index 0000000..0f97a48 --- /dev/null +++ b/application/admin/view/salary/custom_detail/index.html @@ -0,0 +1,66 @@ +
+ {:build_heading()} + + + + + +
+
+
+
+
+ + {:__('Add')} + + + + + +
+ +
+
+
+ +
+
+
+ + diff --git a/public/assets/js/backend/salary/custom_detail.js b/public/assets/js/backend/salary/custom_detail.js new file mode 100644 index 0000000..0cc7872 --- /dev/null +++ b/public/assets/js/backend/salary/custom_detail.js @@ -0,0 +1,127 @@ +define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { + + var Controller = { + index: function () { +// 初始化表格参数配置 + Table.api.init({ + extend: { + index_url: 'salary/custom_detail/index' + location.search, + add_url: 'salary/custom_detail/add', + // del_url: 'oa/schedule/del', + // multi_url: 'oa/schedule/multi', + // import_url: 'oa/schedule/import', + table: 'salary', + } + }); + + var table = $("#table"); + + var defaultColumnArr = []; + defaultColumnArr.push({ + "title":"用户名", + "field":"name", + },{ + "title":"用户id", + "field":"target_admin_id", + "visible":false + }); + + + // 获取后端传入的字段定义(JSON 字符串) + + var columnJson = $('#salaryitem').val(); + var rawColumns = JSON.parse(columnJson); + rawColumns.forEach(function (item) { + // 可以加入格式化、样式控制等逻辑 + defaultColumnArr.push({ + field: item.id, + title: item.name, + }); + }); + + defaultColumnArr.push({ + field: 'operate', + title: __('Operate'), + table: table, + buttons: [ + { + name: 'edit', + text: '编辑', + title: '编辑', + classname: 'btn btn-xs btn-success btn-dialog', + icon: 'fa fa-edit', + url: function (row) { + // 注意这里拼接 admin_id 和 month + return 'salary/custom_detail/edit?target_admin_id=' + row.target_admin_id + '&month=' + row.month; + }, + extend: 'data-area=\'["800px", "600px"]\'', + visible:function(row){ + if (!Config.manage) { + return false; + } + return true; + } + }, + { + name: 'complete', + text:"结算", + title:"结算", + extend: 'data-toggle="tooltip" data-container="body"', + classname: 'btn btn-xs btn-success btn-magic btn-ajax', + icon: 'fa fa-cny', + url: function (row) { + // 注意这里拼接 admin_id 和 month + return 'salary/detail/settle?target_admin_id=' + row.target_admin_id + '&month=' + row.month; + }, + confirm: '确认结算?', + refresh: true, + success: function (data, ret) { + $("#table").bootstrapTable('refresh'); + return false; + }, + error: function (data, ret) { + Layer.alert(ret.msg); + return false; + }, + visible: function (row) { + if (!Config.manage) { + return false; + } + //返回true时按钮显示,返回false隐藏 + if (row.settle_status != 1) { + return true; + } + return false; + } + }, + ], + events: Table.api.events.operate, + formatter: Table.api.formatter.operate + }); + + + // 初始化表格 + table.bootstrapTable({ + url: $.fn.bootstrapTable.defaults.extend.index_url, + columns: defaultColumnArr, + searchFormVisible: true, + searchFormTemplate: 'customformtpl', + }); + + // 为表格绑定事件 + Table.api.bindevent(table); + }, + add: function () { + Controller.api.bindevent(); + }, + edit: function () { + Controller.api.bindevent(); + }, + api: { + bindevent: function () { + Form.api.bindevent($("form[role=form]")); + } + } + }; + return Controller; +});