dsh-workbench-salary 0.4.15 → 0.4.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +1 -1
- package/lib/domain/salary.js +81 -1
- package/lib/index.js +7 -8
- package/package.json +3 -3
package/lib/client.js
CHANGED
|
@@ -4237,7 +4237,7 @@ var GLYPHS = require_icons2().ICONS;
|
|
|
4237
4237
|
ensureStyles();
|
|
4238
4238
|
host.registerAddon({
|
|
4239
4239
|
id: "wb-addon-salary",
|
|
4240
|
-
version: "0.4.
|
|
4240
|
+
version: "0.4.17",
|
|
4241
4241
|
icon: GLYPHS["wallet"],
|
|
4242
4242
|
views: [
|
|
4243
4243
|
{ id: "stats", icon: tabIcon("stats"), title: { zh: "\u7EDF\u8BA1", en: "Stats" }, component: withProps(require_stats(), "\u7EDF\u8BA1") },
|
package/lib/domain/salary.js
CHANGED
|
@@ -176,6 +176,86 @@ export function createSalary({ db, host, log }) {
|
|
|
176
176
|
return { deleted: true, id: row.id };
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
// 备份导入:按 id UPSERT,id 对不上再用自然键(发薪日 + 所属期 + 应发 + 实发 + 备注)兜底。
|
|
180
|
+
// 三档策略与 docs/specs/15-数据备份.md 一致;软删的行不参与匹配,也不会被复活。
|
|
181
|
+
const IMPORT_POLICIES = ["newest", "skip", "overwrite"];
|
|
182
|
+
const IMPORT_KEYS = MONEY_FIELDS.map((f) => f.column);
|
|
183
|
+
|
|
184
|
+
function importRow(raw) {
|
|
185
|
+
const payDate = requireDate(raw.payDate, "发薪日期");
|
|
186
|
+
const cycle = normalizeCycle(raw.cycle);
|
|
187
|
+
const employmentType = normalizeEmploymentType(raw.employmentType);
|
|
188
|
+
const period = incomePeriod(raw.incomePeriod, payDate, cycle);
|
|
189
|
+
const money = moneyInput(raw);
|
|
190
|
+
const gross = hasValue(raw.grossPay) ? money.grossPay : derivedTotals(money).gross;
|
|
191
|
+
const net = hasValue(raw.netPay) ? money.netPay : round2(Math.max(gross - sumKeys(money, SOCIAL_KEYS) - money.incomeTax - money.postTaxDeduction, 0));
|
|
192
|
+
const note = String(raw.note === undefined ? "" : raw.note).trim();
|
|
193
|
+
const stamp = {
|
|
194
|
+
created: Number(raw.createdAt) > 0 ? Number(raw.createdAt) : now(),
|
|
195
|
+
updated: Number(raw.updatedAt) > 0 ? Number(raw.updatedAt) : now(),
|
|
196
|
+
rev: Number(raw.rev) > 0 ? Number(raw.rev) : 1,
|
|
197
|
+
};
|
|
198
|
+
const backupId = typeof raw.id === "string" ? raw.id.trim() : "";
|
|
199
|
+
let local = backupId ? one("SELECT * FROM salary_record WHERE id=?", backupId) : null;
|
|
200
|
+
if (!local) {
|
|
201
|
+
local = one(
|
|
202
|
+
`SELECT * FROM salary_record WHERE deleted_at IS NULL AND pay_date=? AND IFNULL(income_period,'')=?
|
|
203
|
+
AND ABS(gross_pay-?)<0.005 AND ABS(net_pay-?)<0.005 AND note=? ORDER BY created_at LIMIT 1`,
|
|
204
|
+
payDate,
|
|
205
|
+
period || "",
|
|
206
|
+
gross,
|
|
207
|
+
net,
|
|
208
|
+
note,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
const values = MONEY_FIELDS.map((f) => (f.key === "grossPay" ? gross : f.key === "netPay" ? net : money[f.key]));
|
|
212
|
+
return {
|
|
213
|
+
local,
|
|
214
|
+
stamp,
|
|
215
|
+
row: [payDate, period, cycle, employmentType, ...values, note],
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function importRecords(records, policy) {
|
|
220
|
+
const mode = IMPORT_POLICIES.includes(policy) ? policy : "newest";
|
|
221
|
+
const report = { ok: true, imported: 0, skipped: 0, failed: 0, policy: mode };
|
|
222
|
+
const list = Array.isArray(records) ? records : [];
|
|
223
|
+
const columns = ["pay_date", "income_period", "cycle", "employment_type", ...IMPORT_KEYS, "note"];
|
|
224
|
+
const assignments = columns.map((column) => column + "=excluded." + column).join(", ");
|
|
225
|
+
for (const raw of list) {
|
|
226
|
+
try {
|
|
227
|
+
if (!raw || typeof raw !== "object") throw new Error("工资记录格式不正确");
|
|
228
|
+
const { local, stamp, row } = importRow(raw);
|
|
229
|
+
if (local && local.deleted_at !== null && local.deleted_at !== undefined) {
|
|
230
|
+
report.skipped += 1;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (local) {
|
|
234
|
+
const outdated = mode === "newest" && Number(local.updated_at) >= stamp.updated;
|
|
235
|
+
if (mode === "skip" || outdated) {
|
|
236
|
+
report.skipped += 1;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const id = local ? local.id : (typeof raw.id === "string" && raw.id.trim() ? raw.id.trim() : newId("sal"));
|
|
241
|
+
run(
|
|
242
|
+
`INSERT INTO salary_record (id, ${columns.join(", ")}, created_at, updated_at, rev) VALUES (${Array.from({ length: columns.length + 4 }, () => "?").join(", ")})
|
|
243
|
+
ON CONFLICT(id) DO UPDATE SET ${assignments}, created_at=excluded.created_at, updated_at=excluded.updated_at, rev=excluded.rev, deleted_at=NULL`,
|
|
244
|
+
id,
|
|
245
|
+
...row,
|
|
246
|
+
stamp.created,
|
|
247
|
+
stamp.updated,
|
|
248
|
+
stamp.rev,
|
|
249
|
+
);
|
|
250
|
+
report.imported += 1;
|
|
251
|
+
} catch {
|
|
252
|
+
report.failed += 1;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
report.ok = report.failed === 0;
|
|
256
|
+
return report;
|
|
257
|
+
}
|
|
258
|
+
|
|
179
259
|
function breakdown(rows) {
|
|
180
260
|
const fields = {};
|
|
181
261
|
for (const field of MONEY_FIELDS) fields[field.key] = round2(rows.reduce((sum, r) => sum + (Number(r[field.key]) || 0), 0));
|
|
@@ -335,5 +415,5 @@ export function createSalary({ db, host, log }) {
|
|
|
335
415
|
}
|
|
336
416
|
|
|
337
417
|
|
|
338
|
-
return { status, list, get, create, update, remove, stats, dump, record };
|
|
418
|
+
return { status, list, get, create, update, remove, stats, dump, record, importRecords };
|
|
339
419
|
}
|
package/lib/index.js
CHANGED
|
@@ -34,7 +34,10 @@ export function apply(ctx) {
|
|
|
34
34
|
// 更新来源:宿主「插件」页按它查新版本并安装(只认官方 npm registry)。
|
|
35
35
|
update: { source: "npm", package: "dsh-workbench-salary" },
|
|
36
36
|
category: "finance",
|
|
37
|
-
|
|
37
|
+
// 工资记录随备份迁移(同步清单为空:不上云)。
|
|
38
|
+
tables: [
|
|
39
|
+
{ name: "salary_record", persistence: "persistent" },
|
|
40
|
+
],
|
|
38
41
|
sync: { tables: [] },
|
|
39
42
|
views: VIEWS,
|
|
40
43
|
capability: {
|
|
@@ -67,18 +70,14 @@ export function apply(ctx) {
|
|
|
67
70
|
],
|
|
68
71
|
},
|
|
69
72
|
exportData: () => ({ records: engine.dump() }),
|
|
70
|
-
importData: (payload) => {
|
|
73
|
+
importData: (payload, _ctx, options) => {
|
|
71
74
|
let data = payload;
|
|
72
75
|
if (typeof data === "string") {
|
|
73
76
|
try { data = JSON.parse(data); } catch { data = {}; }
|
|
74
77
|
}
|
|
75
78
|
const records = data && Array.isArray(data.records) ? data.records : [];
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
for (const item of records) {
|
|
79
|
-
try { engine.create({ ...item, incomePeriod: item.incomePeriod ?? null }); imported += 1; } catch { failed += 1; }
|
|
80
|
-
}
|
|
81
|
-
return { imported, failed };
|
|
79
|
+
// 逐行 UPSERT:同一份备份导入两次不应再长出重复行(importRecords 内部按 policy 处理)。
|
|
80
|
+
return engine.importRecords(records, options && options.policy);
|
|
82
81
|
},
|
|
83
82
|
setup,
|
|
84
83
|
}), "salary-domain");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workbench-salary",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.17",
|
|
4
4
|
"description": "个人工作台工资插件:按发薪日期记录工资构成、扣款与实发",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"addon": {
|
|
17
17
|
"id": "wb-addon-salary",
|
|
18
18
|
"title": "工资",
|
|
19
|
-
"version": "0.4.
|
|
19
|
+
"version": "0.4.17",
|
|
20
20
|
"minShell": "0.8.0",
|
|
21
21
|
"tables": [
|
|
22
22
|
"salary_record"
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"dsh-workbench-host": "^0.
|
|
42
|
+
"dsh-workbench-host": "^0.28.0"
|
|
43
43
|
},
|
|
44
44
|
"dsh": {
|
|
45
45
|
"bundle": {
|