liu
昨天 f7e06aaee32674791f63d6e5111c0d0a3f4de4a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { HrmSalaryCalculationApi } from '#/api/hrm/salary/calculation';
 
import { ref } from 'vue';
 
import { Page } from '#/packages/effects/common-ui/src';
import { downloadFileFromBlobPart } from '#/packages/utils/src';
 
import { message, Modal, Form, FormItem, DatePicker, TreeSelect, Table, InputNumber, Alert, Tag } from 'ant-design-vue';
 
import { DICT_TYPE } from '#/packages/constants/src';
import { getDictLabel } from '#/packages/effects/hooks/src';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  calculateSalary,
  confirmSalary,
  deleteSalaryCalculation,
  exportSalaryCalculation,
  getSalaryCalculationPage,
  previewSalaryCalculation,
  revokeSalaryCalculation,
  saveSalaryCalculation,
} from '#/api/hrm/salary/calculation';
import { getDeptList } from '#/api/system/dept';
import { handleTree } from '#/packages/utils/src';
import { $t } from '#/locales';
 
import { useGridColumns, useGridFormSchema, usePreviewColumns } from './data';
 
// 核算弹窗相关
const calculateVisible = ref(false);
const calculatePeriod = ref<string>();
const calculateDeptId = ref<number>();
const deptTreeData = ref<any[]>([]);
const previewLoading = ref(false);
const previewData = ref<HrmSalaryCalculationApi.SalaryCalculation[]>([]);
const submitLoading = ref(false);
const calculateLoading = ref(false);
 
// 打开核算弹窗
async function openCalculateModal() {
  const formValues = await gridApi.formApi.getValues();
  calculatePeriod.value = formValues.period;
  calculateDeptId.value = formValues.deptId;
  previewData.value = [];
  // 加载部门树
  const data = await getDeptList();
  deptTreeData.value = handleTree(data);
  calculateVisible.value = true;
}
 
/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}
 
/** 执行薪酬核算(自动计算并保存) */
async function handleCalculate() {
  if (!calculatePeriod.value) {
    message.warning('请选择核算周期');
    return;
  }
  calculateLoading.value = true;
  try {
    await calculateSalary({
      period: calculatePeriod.value,
      deptId: calculateDeptId.value,
    });
    message.success('薪酬核算执行成功');
    calculateVisible.value = false;
    handleRefresh();
  } finally {
    calculateLoading.value = false;
  }
}
 
/** 预览核算数据 */
async function handlePreview() {
  if (!calculatePeriod.value) {
    message.warning('请选择核算周期');
    return;
  }
  previewLoading.value = true;
  try {
    previewData.value = await previewSalaryCalculation({
      period: calculatePeriod.value,
      deptId: calculateDeptId.value,
    });
    if (previewData.value.length === 0) {
      message.info('该条件下没有需要核算的员工');
    }
  } finally {
    previewLoading.value = false;
  }
}
 
/** 计算实发工资 */
function calculateActualSalary(record: HrmSalaryCalculationApi.SalaryCalculation) {
  const income = (record.baseSalary || 0) +
    (record.performanceSalary || 0) +
    (record.overtimePay || 0) +
    (record.mealAllowance || 0) +
    (record.transportAllowance || 0) +
    (record.otherAllowance || 0);
  const deduction = (record.socialSecurityDeduction || 0) +
    (record.housingFundDeduction || 0) +
    (record.taxDeduction || 0) +
    (record.otherDeduction || 0) +
    (record.leaveDeduction || 0);
  return income - deduction;
}
 
/** 更新行数据并计算实发工资 */
function updateFieldValue(record: HrmSalaryCalculationApi.SalaryCalculation, field: keyof HrmSalaryCalculationApi.SalaryCalculation, value: number) {
  (record as any)[field] = value;
  record.actualSalary = calculateActualSalary(record);
}
 
/** 提交薪酬核算(手动调整后保存) */
async function handleSubmit() {
  if (!calculatePeriod.value) {
    message.warning('请选择核算周期');
    return;
  }
  if (previewData.value.length === 0) {
    message.warning('请先预览核算数据');
    return;
  }
  submitLoading.value = true;
  try {
    await saveSalaryCalculation({
      period: calculatePeriod.value,
      list: previewData.value,
    });
    message.success('核算保存成功');
    calculateVisible.value = false;
    handleRefresh();
  } finally {
    submitLoading.value = false;
  }
}
 
/** 取消弹窗 */
function handleCancel() {
  calculateVisible.value = false;
  previewData.value = [];
}
 
/** 确认薪酬核算 */
async function handleConfirm(row: HrmSalaryCalculationApi.SalaryCalculation) {
  await confirmSalary([row.id!]);
  message.success('确认成功');
  handleRefresh();
}
 
/** 批量确认选中的记录 */
async function handleBatchConfirm() {
  const records = gridApi.grid.getCheckboxRecords() as HrmSalaryCalculationApi.SalaryCalculation[];
  if (records.length === 0) {
    message.warning('请选择要确认的记录');
    return;
  }
  const ids = records.map((r) => r.id!);
  await confirmSalary(ids);
  message.success('确认成功');
  handleRefresh();
}
 
/** 批量保存选中的核算记录(仅待确认状态) */
async function handleBatchSave() {
  const records = gridApi.grid.getCheckboxRecords() as HrmSalaryCalculationApi.SalaryCalculation[];
  if (records.length === 0) {
    message.warning('请选择要保存的记录');
    return;
  }
  const pending = records.filter((r) => r.status === 0);
  if (pending.length === 0) {
    message.warning('选中的记录均非待确认状态,无需保存');
    return;
  }
  const first = pending[0];
  if (!first?.period) {
    message.warning('缺少核算周期');
    return;
  }
  await saveSalaryCalculation({ period: first.period, list: pending });
  message.success('批量保存成功');
  handleRefresh();
}
 
/** 撤销确认(已确认 → 待确认,可重新核算) */
async function handleRevoke(row: HrmSalaryCalculationApi.SalaryCalculation) {
  Modal.confirm({
    title: '撤销确认',
    content: `确定撤销【${row.no}】的确认吗?撤销后可重新核算。`,
    okText: '确定',
    cancelText: '取消',
    onOk: async () => {
      await revokeSalaryCalculation([row.id!]);
      message.success('撤销成功');
      handleRefresh();
    },
  });
}
 
/** 删除待确认的核算记录 */
async function handleDelete(row: HrmSalaryCalculationApi.SalaryCalculation) {
  Modal.confirm({
    title: '确认删除',
    content: `确定删除【${row.no}】的核算记录吗?删除后需重新核算。`,
    okText: '确定',
    cancelText: '取消',
    onOk: async () => {
      await deleteSalaryCalculation(row.id!);
      message.success('删除成功');
      handleRefresh();
    },
  });
}
 
/** 导出薪酬核算 */
async function handleExport() {
  const data = await exportSalaryCalculation(await gridApi.formApi.getValues());
  downloadFileFromBlobPart({ fileName: '薪酬核算表.xls', source: data });
}
 
const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    keepSource: true,
    checkboxConfig: {
      highlight: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) =>
          await getSalaryCalculationPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          }),
      },
    },
    pagingConfig: {
      pageSize: 20,
      pageSizes: [20, 50, 100],
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<HrmSalaryCalculationApi.SalaryCalculation>,
});
</script>
 
<template>
  <Page auto-content-height>
    <!-- 核算弹窗 -->
    <Modal
      v-model:open="calculateVisible"
      title="薪酬核算"
      width="1200px"
      :footer="null"
      :maskClosable="false"
    >
      <Alert
        message="两种核算方式:1. 执行核算 - 自动计算并保存;2. 预览数据 - 手动调整后保存"
        type="info"
        show-icon
        style="margin-bottom: 16px"
      />
 
      <Form layout="inline" class="mb-4">
        <FormItem label="核算周期" required>
          <DatePicker
            v-model:value="calculatePeriod"
            picker="month"
            format="YYYY-MM"
            valueFormat="YYYY-MM"
            placeholder="请选择核算周期"
            style="width: 180px"
          />
        </FormItem>
        <FormItem label="核算部门">
          <TreeSelect
            v-model:value="calculateDeptId"
            :tree-data="deptTreeData"
            :field-names="{ label: 'name', value: 'id', children: 'children' }"
            placeholder="全部部门"
            allow-clear
            tree-default-expand-all
            style="width: 200px"
          />
        </FormItem>
        <FormItem>
          <a-button type="primary" :loading="calculateLoading" @click="handleCalculate">
            执行核算
          </a-button>
        </FormItem>
        <FormItem>
          <a-button :loading="previewLoading" @click="handlePreview">
            预览数据
          </a-button>
        </FormItem>
      </Form>
 
      <Table
        v-if="previewData.length > 0"
        :columns="usePreviewColumns()"
        :data-source="previewData"
        :pagination="{ pageSize: 20, showSizeChanger: true, showTotal: (t) => `共 ${t} 人` }"
        :scroll="{ x: 1800, y: 400 }"
        size="small"
        row-key="userId"
        bordered
        class="mb-4"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'baseSalary'">
            <InputNumber
              v-model:value="record.baseSalary"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'baseSalary', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'performanceSalary'">
            <InputNumber
              v-model:value="record.performanceSalary"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'performanceSalary', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'overtimePay'">
            <InputNumber
              v-model:value="record.overtimePay"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'overtimePay', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'mealAllowance'">
            <InputNumber
              v-model:value="record.mealAllowance"
              :min="0"
              :precision="2"
              size="small"
              style="width: 80px"
              @change="(val: number) => updateFieldValue(record, 'mealAllowance', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'transportAllowance'">
            <InputNumber
              v-model:value="record.transportAllowance"
              :min="0"
              :precision="2"
              size="small"
              style="width: 80px"
              @change="(val: number) => updateFieldValue(record, 'transportAllowance', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'otherAllowance'">
            <InputNumber
              v-model:value="record.otherAllowance"
              :min="0"
              :precision="2"
              size="small"
              style="width: 80px"
              @change="(val: number) => updateFieldValue(record, 'otherAllowance', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'socialSecurityDeduction'">
            <InputNumber
              v-model:value="record.socialSecurityDeduction"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'socialSecurityDeduction', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'housingFundDeduction'">
            <InputNumber
              v-model:value="record.housingFundDeduction"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'housingFundDeduction', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'taxDeduction'">
            <InputNumber
              v-model:value="record.taxDeduction"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'taxDeduction', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'otherDeduction'">
            <InputNumber
              v-model:value="record.otherDeduction"
              :min="0"
              :precision="2"
              size="small"
              style="width: 80px"
              @change="(val: number) => updateFieldValue(record, 'otherDeduction', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'leaveDeduction'">
            <InputNumber
              v-model:value="record.leaveDeduction"
              :min="0"
              :precision="2"
              size="small"
              style="width: 90px"
              @change="(val: number) => updateFieldValue(record, 'leaveDeduction', val)"
            />
          </template>
          <template v-else-if="column.dataIndex === 'actualSalary'">
            <span class="font-bold text-green-600">{{ record.actualSalary?.toFixed(2) }}</span>
          </template>
          <template v-else-if="column.dataIndex === 'workDays'">
            <InputNumber
              v-model:value="record.workDays"
              :min="0"
              :max="31"
              :precision="0"
              size="small"
              style="width: 70px"
            />
          </template>
          <template v-else-if="column.dataIndex === 'overtimeHours'">
            <InputNumber
              v-model:value="record.overtimeHours"
              :min="0"
              :precision="1"
              size="small"
              style="width: 80px"
            />
          </template>
        </template>
      </Table>
 
      <div class="flex justify-end gap-2" v-if="previewData.length > 0">
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" :loading="submitLoading" @click="handleSubmit">
          保存核算
        </a-button>
      </div>
    </Modal>
 
    <Grid table-title="薪酬核算列表">
      <template #toolbar-tools>
        <TableAction
          :actions="[
            {
              label: '核算',
              type: 'primary',
              icon: ACTION_ICON.EDIT,
              auth: ['hrm:salary-calculation:calculate'],
              onClick: openCalculateModal,
            },
            {
              label: '批量确认',
              type: 'primary',
              auth: ['hrm:salary-calculation:confirm'],
              onClick: handleBatchConfirm,
            },
            {
              label: '批量保存',
              type: 'primary',
              auth: ['hrm:salary-calculation:save'],
              onClick: handleBatchSave,
            },
            {
              label: $t('ui.actionTitle.export'),
              type: 'primary',
              icon: ACTION_ICON.DOWNLOAD,
              auth: ['hrm:salary-calculation:export'],
              onClick: handleExport,
            },
          ]"
        />
      </template>
      <template #status="{ row }">
        <Tag :color="row.status === 0 ? 'warning' : row.status === 10 ? 'success' : 'processing'">
          {{ getDictLabel(DICT_TYPE.HRM_SALARY_STATUS, row.status) }}
        </Tag>
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: '确认',
              type: 'link',
              auth: ['hrm:salary-calculation:confirm'],
              ifShow: row.status === 0,
              onClick: handleConfirm.bind(null, row),
            },
            {
              label: '删除',
              type: 'link',
              danger: true,
              auth: ['hrm:salary-calculation:delete'],
              ifShow: row.status === 0,
              onClick: handleDelete.bind(null, row),
            },
            {
              label: '撤销确认',
              type: 'link',
              auth: ['hrm:salary-calculation:confirm'],
              ifShow: row.status === 10,
              onClick: handleRevoke.bind(null, row),
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>