dsh-workbench-salary 0.2.4 → 0.3.0

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/cordis.patch.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  - insert:
2
2
  - id: workbench
3
- name: dsh-workbench
3
+ name: dsh-workbench-host
4
4
  - id: workbench-salary
5
5
  name: dsh-workbench-salary
package/lib/client.js CHANGED
@@ -1013,7 +1013,10 @@ var require_stats = __commonJS({
1013
1013
  var F = require_fields();
1014
1014
  var yearState = React.useState((/* @__PURE__ */ new Date()).getFullYear());
1015
1015
  var year = yearState[0], setYear = yearState[1];
1016
- var load = C.useLoad(React, api, "salary.stats", { year }, [year]);
1016
+ var basisState = React.useState("arrival");
1017
+ var basis = basisState[0], setBasis = basisState[1];
1018
+ var basisLabel = basis === "income" ? "\u6309\u6240\u5C5E\u6708" : "\u6309\u5230\u8D26\u6708";
1019
+ var load = C.useLoad(React, api, "salary.stats", { year, basis }, [year, basis]);
1017
1020
  var data = load.data;
1018
1021
  var METRICS = [
1019
1022
  { key: "netPay", label: "\u5B9E\u53D1" },
@@ -1266,13 +1269,13 @@ var require_stats = __commonJS({
1266
1269
  }));
1267
1270
  }
1268
1271
  var body;
1269
- if (load.loading) body = h(C.Empty, { h, text: "\u52A0\u8F7D\u4E2D\u2026" });
1270
- else if (load.error) body = h(C.Notice, { h, kind: "error", text: load.error });
1272
+ if (load.error) body = h(C.Notice, { h, kind: "error", text: load.error });
1273
+ else if (load.loading || data && (data.year !== year || data.basis !== basis)) body = h(C.Empty, { h, text: "\u52A0\u8F7D\u4E2D\u2026" });
1271
1274
  else if (!data || data.records === 0) {
1272
1275
  body = h(
1273
1276
  "div",
1274
1277
  { className: "wbsal-empty" },
1275
- h("div", null, year + " \u5E74\u8FD8\u6CA1\u6709\u5DE5\u8D44\u8BB0\u5F55\uFF1B\u6362\u4E2A\u5E74\u4EFD\uFF0C\u6216\u5148\u53BB\u5DE5\u8D44\u8BB0\u5F55\u9875\u8BB0\u4E00\u7B14")
1278
+ h("div", null, year + " \u5E74\u6CA1\u6709\u6309\u5F53\u524D\u53E3\u5F84\u5F52\u5165\u7684\u5DE5\u8D44\u8BB0\u5F55")
1276
1279
  );
1277
1280
  } else {
1278
1281
  var latestBreakdown = data.latestMonth && data.latestMonth.breakdown;
@@ -1350,7 +1353,7 @@ var require_stats = __commonJS({
1350
1353
  "div",
1351
1354
  { className: "wbsal-chart-foot" },
1352
1355
  h("span", { className: "wbsal-legend" }, h("i", null), "\u6709\u8BB0\u5F55\u6708\u4EFD", h("i", { className: "soft" }), "\u5176\u4ED6\u6708\u4EFD"),
1353
- h("span", null, "\u5355\u4F4D\uFF1A\u5143 \xB7 \u6309\u53D1\u85AA\u65E5\u671F\u5F52\u5C5E\u6708\u4EFD \xB7 \u5F53\u524D\u53E3\u5F84 " + metricLabel)
1356
+ h("span", null, "\u5355\u4F4D\uFF1A\u5143 \xB7 " + basisLabel + " \xB7 " + metricLabel)
1354
1357
  )
1355
1358
  ),
1356
1359
  h(
@@ -1448,6 +1451,22 @@ var require_stats = __commonJS({
1448
1451
  h(
1449
1452
  "div",
1450
1453
  { className: "wbsal-toolbar-end" },
1454
+ h(
1455
+ "div",
1456
+ { className: "wbsal-metric", role: "group", "aria-label": "\u5DE5\u8D44\u5F52\u6708\u53E3\u5F84" },
1457
+ [["arrival", "\u6309\u5230\u8D26\u6708"], ["income", "\u6309\u6240\u5C5E\u6708"]].map(function(option) {
1458
+ return h("button", {
1459
+ key: option[0],
1460
+ type: "button",
1461
+ "aria-pressed": basis === option[0],
1462
+ onClick: function() {
1463
+ setBasis(option[0]);
1464
+ setHover(null);
1465
+ setSocialHover(null);
1466
+ }
1467
+ }, option[1]);
1468
+ })
1469
+ ),
1451
1470
  h(
1452
1471
  "div",
1453
1472
  { className: "wbsal-seg" },
@@ -1461,6 +1480,7 @@ var require_stats = __commonJS({
1461
1480
  )
1462
1481
  )
1463
1482
  ),
1483
+ basis === "income" && data && data.year === year && data.basis === basis && data.unassignedRecords > 0 ? h(C.Notice, { h, text: "\u5168\u90E8\u5E74\u4EFD\u5171 " + data.unassignedRecords + " \u7B14\u5DE5\u8D44\u672A\u586B\u5199\u6240\u5C5E\u671F\uFF0C\u672A\u8BA1\u5165\u6240\u5C5E\u6708\u7EDF\u8BA1\u3002" }) : null,
1464
1484
  body
1465
1485
  );
1466
1486
  };
@@ -1936,7 +1956,7 @@ var require_records = __commonJS({
1936
1956
  setOpenId("");
1937
1957
  setMsg(null);
1938
1958
  var payDate = pickDate("monthly");
1939
- setForm({ mode: "create", id: "", payDate, cycle: "monthly", note: F.defaultNote(payDate, "monthly"), values: F.blankValues(), lockGross: false });
1959
+ setForm({ mode: "create", id: "", payDate, incomePeriod: defaultPeriod(payDate, "monthly"), cycle: "monthly", note: F.defaultNote(payDate, "monthly"), values: F.blankValues(), lockGross: false });
1940
1960
  }
1941
1961
  function openEdit(item) {
1942
1962
  var values = F.blankValues();
@@ -1949,7 +1969,10 @@ var require_records = __commonJS({
1949
1969
  setConfirmId("");
1950
1970
  setOpenId("");
1951
1971
  setMsg(null);
1952
- setForm({ mode: "edit", id: item.id, payDate: item.payDate, cycle: item.cycle || "monthly", note: item.note || "", values, lockGross: incomeZero && F.num(item.grossPay) > 0 });
1972
+ setForm({ mode: "edit", id: item.id, payDate: item.payDate, incomePeriod: item.incomePeriod || "", cycle: item.cycle || "monthly", note: item.note || "", values, original: item, lockGross: incomeZero && F.num(item.grossPay) > 0 });
1973
+ }
1974
+ function defaultPeriod(payDate, cycle) {
1975
+ return cycle === "daily" ? payDate : payDate === "" || payDate.startsWith("2000-01-") ? "" : F.incomePeriod(payDate);
1953
1976
  }
1954
1977
  function closeForm() {
1955
1978
  setForm(null);
@@ -1957,14 +1980,18 @@ var require_records = __commonJS({
1957
1980
  }
1958
1981
  function setField(key, value) {
1959
1982
  setForm(function(prev) {
1960
- var next = Object.assign({}, prev, { note: prev.note });
1983
+ var next = Object.assign({}, prev);
1961
1984
  next[key] = value;
1962
- if (key === "payDate" && !prev.cycleLocked) {
1963
- var same = String(value).slice(5) === "15";
1964
- if (same) next.cycle = "monthly";
1985
+ if (key === "incomePeriod") next.periodTouched = true;
1986
+ if (key === "note") next.noteTouched = true;
1987
+ if (prev.mode === "create" && (key === "payDate" || key === "cycle")) {
1988
+ if (!prev.periodTouched) next.incomePeriod = defaultPeriod(next.payDate, next.cycle);
1989
+ else if (key === "cycle" && next.cycle !== prev.cycle && prev.incomePeriod) {
1990
+ next.incomePeriod = next.cycle === "monthly" ? prev.incomePeriod.slice(0, 7) : "";
1991
+ }
1965
1992
  }
1966
- if (key === "payDate" && (!prev.note || prev.note === F.defaultNote(prev.payDate, prev.cycle))) {
1967
- next.note = F.defaultNote(value, next.cycle);
1993
+ if (prev.mode === "create" && !next.noteTouched) {
1994
+ next.note = next.cycle === "monthly" && next.incomePeriod ? F.monthLabel(next.incomePeriod.slice(0, 7)) + "\u5DE5\u8D44" : "";
1968
1995
  }
1969
1996
  return next;
1970
1997
  });
@@ -1989,9 +2016,13 @@ var require_records = __commonJS({
1989
2016
  var payload = {
1990
2017
  payDate: form.payDate,
1991
2018
  cycle: form.cycle,
2019
+ incomePeriod: form.incomePeriod || "",
1992
2020
  note: form.note || ""
1993
2021
  };
1994
- F.KEYS.forEach(function(key) {
2022
+ var moneyChanged = !form.original || F.KEYS.some(function(key) {
2023
+ return F.num(form.values[key]) !== F.num(form.original[key]);
2024
+ });
2025
+ if (moneyChanged) F.KEYS.forEach(function(key) {
1995
2026
  if (key === "grossPay" && form.lockGross) {
1996
2027
  payload.grossPay = F.num(form.values.grossPay);
1997
2028
  return;
@@ -2076,14 +2107,14 @@ var require_records = __commonJS({
2076
2107
  { className: "wbsal-form-meta" },
2077
2108
  h(
2078
2109
  C.Field,
2079
- { h, label: "\u53D1\u85AA\u65E5\u671F", hint: F.incomeLabel(form.payDate) ? "\u5BF9\u5E94 " + F.incomeLabel(form.payDate) + " \u5DE5\u8D44" : "\u6708\u7ED3" },
2080
- h(DP.DatePicker, { React, h, prefix: "wbsal", lang, label: "\u53D1\u85AA\u65E5\u671F", value: form.payDate, onChange: function(next) {
2110
+ { h, label: "\u5230\u8D26\u65E5" },
2111
+ h(DP.DatePicker, { React, h, prefix: "wbsal", lang, label: "\u5230\u8D26\u65E5", value: form.payDate, onChange: function(next) {
2081
2112
  setField("payDate", next);
2082
2113
  } })
2083
2114
  ),
2084
2115
  h(
2085
2116
  C.Field,
2086
- { h, label: "\u7C7B\u578B", hint: "\u6708\u7ED3" },
2117
+ { h, label: "\u7C7B\u578B" },
2087
2118
  h(
2088
2119
  "select",
2089
2120
  { className: "wbsal-input", value: form.cycle, onChange: function(e) {
@@ -2093,6 +2124,22 @@ var require_records = __commonJS({
2093
2124
  h("option", { value: "daily" }, "\u65E5\u7ED3")
2094
2125
  )
2095
2126
  ),
2127
+ h(
2128
+ C.Field,
2129
+ { h, label: "\u5DE5\u8D44\u6240\u5C5E\u671F" },
2130
+ h((form.incomePeriod ? form.incomePeriod.length === 10 : form.cycle === "daily") ? DP.DatePicker : DP.MonthPicker, {
2131
+ React,
2132
+ h,
2133
+ prefix: "wbsal",
2134
+ lang,
2135
+ label: "\u5DE5\u8D44\u6240\u5C5E\u671F",
2136
+ placeholder: "\u672A\u5F52\u5C5E",
2137
+ value: form.incomePeriod || "",
2138
+ onChange: function(next) {
2139
+ setField("incomePeriod", next);
2140
+ }
2141
+ })
2142
+ ),
2096
2143
  h(C.Field, { h, label: "\u5907\u6CE8" }, h("input", { className: "wbsal-input", value: form.note, onChange: function(e) {
2097
2144
  setField("note", e.target.value);
2098
2145
  } }))
@@ -2201,7 +2248,7 @@ var require_records = __commonJS({
2201
2248
  h(
2202
2249
  "div",
2203
2250
  { className: "wbsal-muted" },
2204
- (item.note ? "\u5907\u6CE8\uFF1A" + item.note + " \xB7 " : "") + "\u66F4\u65B0\u4E8E " + C.fmtTime(item.updatedAt, lang)
2251
+ "\u5DE5\u8D44\u6240\u5C5E\u671F\uFF1A" + (item.incomePeriod ? F.monthLabel(item.incomePeriod) : "\u672A\u5F52\u5C5E") + " \xB7 " + (item.note ? "\u5907\u6CE8\uFF1A" + item.note + " \xB7 " : "") + "\u66F4\u65B0\u4E8E " + C.fmtTime(item.updatedAt, lang)
2205
2252
  ),
2206
2253
  h(
2207
2254
  "div",
@@ -2240,7 +2287,12 @@ var require_records = __commonJS({
2240
2287
  }
2241
2288
  },
2242
2289
  h("td", { className: "left" }, h("span", { className: "wbsal-date" }, h("b", null, String(item.payDate).slice(5)), h("span", null, F.weekday(item.payDate)))),
2243
- h("td", { className: "left" }, h(C.Pill, { h, kind: item.cycle === "daily" ? "warn" : "ok" }, item.cycle === "daily" ? "\u65E5\u7ED3" : "\u6708\u7ED3")),
2290
+ h(
2291
+ "td",
2292
+ { className: "left" },
2293
+ h(C.Pill, { h, kind: item.cycle === "daily" ? "warn" : "ok" }, item.cycle === "daily" ? "\u65E5\u7ED3" : "\u6708\u7ED3"),
2294
+ h("div", { className: "wbsal-muted" }, item.incomePeriod ? F.monthLabel(item.incomePeriod) : "\u672A\u5F52\u5C5E")
2295
+ ),
2244
2296
  h("td", null, h("span", { className: "wbsal-net" }, F.money(item.netPay))),
2245
2297
  h("td", { className: "wbsal-opt" }, F.money(item.grossPay)),
2246
2298
  h("td", { className: "wbsal-opt hide-md" }, F.money(F.sumValues(item, F.SOCIAL_KEYS))),
@@ -4249,7 +4301,7 @@ var require_actions = __commonJS({
4249
4301
  ensureStyles();
4250
4302
  host.registerAddon({
4251
4303
  id: "wb-addon-salary",
4252
- version: "0.2.4",
4304
+ version: "0.3.0",
4253
4305
  icon: (init_wallet(), __toCommonJS(wallet_exports)).default,
4254
4306
  views: [
4255
4307
  { id: "stats", icon: tabIcon("stats"), title: { zh: "\u7EDF\u8BA1", en: "Stats" }, component: withProps(require_stats(), "\u7EDF\u8BA1") },
package/lib/db/db.js CHANGED
@@ -12,6 +12,7 @@ const DDL = [
12
12
  `CREATE TABLE IF NOT EXISTS salary_record (
13
13
  id TEXT PRIMARY KEY,
14
14
  pay_date TEXT NOT NULL,
15
+ income_period TEXT,
15
16
  cycle TEXT NOT NULL DEFAULT 'monthly',
16
17
  ${MONEY_FIELDS.map((f) => f.column + " REAL NOT NULL DEFAULT 0").join(",\n ")},
17
18
  note TEXT NOT NULL DEFAULT '',
@@ -26,11 +27,15 @@ const DDL = [
26
27
 
27
28
  export function ensureSchema(db) {
28
29
  for (const sql of DDL) db.exec(sql);
30
+ if (!db.prepare("PRAGMA table_info(salary_record)").all().some((col) => col.name === "income_period")) {
31
+ db.exec("ALTER TABLE salary_record ADD COLUMN income_period TEXT");
32
+ }
33
+ db.exec("CREATE INDEX IF NOT EXISTS idx_salary_income_period ON salary_record(income_period)");
29
34
  }
30
35
 
31
36
  export function salaryRow(r) {
32
37
  if (!r) return null;
33
- const out = { id: r.id, payDate: r.pay_date, cycle: r.cycle };
38
+ const out = { id: r.id, payDate: r.pay_date, incomePeriod: r.income_period ?? null, cycle: r.cycle };
34
39
  for (const field of MONEY_FIELDS) out[field.key] = Number(r[field.column]) || 0;
35
40
  out.note = r.note || "";
36
41
  out.createdAt = r.created_at;
@@ -39,4 +44,4 @@ export function salaryRow(r) {
39
44
  return out;
40
45
  }
41
46
 
42
- export const COLUMNS = ["id", "pay_date", "cycle", ...MONEY_COLUMNS, "note", "created_at", "updated_at", "rev"];
47
+ export const COLUMNS = ["id", "pay_date", "income_period", "cycle", ...MONEY_COLUMNS, "note", "created_at", "updated_at", "rev"];
@@ -1,5 +1,6 @@
1
1
  /** 工资 action:读、写都允许 agent(用户口述后可直接落库);数据页查询仅 UI。 */
2
2
  import { MONEY_FIELDS } from "./model.js";
3
+ import { COLUMNS } from "../db/db.js";
3
4
 
4
5
  const obj = (properties, required = []) => ({ type: "object", properties, required, additionalProperties: false });
5
6
  const str = (description) => ({ type: "string", description });
@@ -56,8 +57,11 @@ export function registerSalaryActions(api, salary) {
56
57
  });
57
58
  action("salary.stats", {
58
59
  description: "只读:年度工资统计(按月聚合的实发/应发/个税/社保、月均、环比、最近月构成)",
59
- input: obj({ year: num("年份 2000–2100,默认今年") }),
60
- execute: (args) => salary.stats({ year: args.year }),
60
+ input: obj({
61
+ year: num("年份 2000–2100,默认今年"),
62
+ basis: { type: "string", enum: ["arrival", "income"], description: "arrival 按到账月(默认);income 按所属月,未归属不计入" },
63
+ }),
64
+ execute: (args) => salary.stats({ year: args.year, basis: args.basis }),
61
65
  });
62
66
  action("salary.recordList", {
63
67
  description: "只读:按日期倒序列出工资记录,可按年、日期区间、日结/月结筛选",
@@ -79,22 +83,24 @@ export function registerSalaryActions(api, salary) {
79
83
  description: "新增一笔工资(会写库,agent 可直接调用):按发薪日期记录日结/月结工资,金额缺省 0,应发与实发默认由明细推导",
80
84
  input: obj({
81
85
  payDate: str("发薪日期 YYYY-MM-DD,必填;月结通常取当月 15 号"),
86
+ incomePeriod: str("工资所属期 YYYY-MM 或 YYYY-MM-DD;空串表示未归属;不传则月结取上月、日结取到账日"),
82
87
  cycle: { type: "string", enum: ["monthly", "daily"], description: "月结 monthly(默认)/ 日结 daily" },
83
88
  ...MONEY_SCHEMA,
84
89
  note: str("备注,可选"),
85
90
  }, ["payDate"]),
86
- execute: (args) => salary.create({ payDate: args.payDate, cycle: args.cycle, note: args.note, ...amountsFrom(args) }),
91
+ execute: (args) => salary.create({ payDate: args.payDate, incomePeriod: args.incomePeriod, cycle: args.cycle, note: args.note, ...amountsFrom(args) }),
87
92
  });
88
93
  action("salary.recordUpdate", {
89
94
  description: "修改一笔工资(会写库;只覆盖传入字段,金额改动后重算应发与实发)",
90
95
  input: obj({
91
96
  id: str("工资记录 id,必填"),
92
97
  payDate: str("发薪日期 YYYY-MM-DD"),
98
+ incomePeriod: str("工资所属期 YYYY-MM 或 YYYY-MM-DD;空串清除,不传保留原值"),
93
99
  cycle: { type: "string", enum: ["monthly", "daily"], description: "月结 / 日结" },
94
100
  ...MONEY_SCHEMA,
95
101
  note: str("备注"),
96
102
  }, ["id"]),
97
- execute: (args) => salary.update({ id: args.id, payDate: args.payDate, cycle: args.cycle, note: args.note, ...amountsFrom(args) }),
103
+ execute: (args) => salary.update({ id: args.id, payDate: args.payDate, incomePeriod: args.incomePeriod, cycle: args.cycle, note: args.note, ...amountsFrom(args) }),
98
104
  });
99
105
  action("salary.recordDelete", {
100
106
  description: "软删除一笔工资(会写库;记录不再出现在列表与统计中)",
@@ -107,7 +113,7 @@ export function registerSalaryActions(api, salary) {
107
113
  execute: () => ({
108
114
  tables: [{
109
115
  name: "salary_record",
110
- columns: ["id", "pay_date", "cycle", ...MONEY_FIELDS.map((f) => f.column), "note", "created_at", "updated_at", "rev"],
116
+ columns: COLUMNS,
111
117
  }],
112
118
  }),
113
119
  });
@@ -125,7 +131,7 @@ export function registerSalaryActions(api, salary) {
125
131
  input: obj({}),
126
132
  execute: () => ({
127
133
  shellVersion: api.shellVersion || "",
128
- domains: [{ id: "wb-addon-salary", version: (api.addon && api.addon.version) || "0.1.0" }],
134
+ domains: [{ id: "wb-addon-salary", version: (api.addon && api.addon.version) || "0.3.0" }],
129
135
  actions: defs.filter((d) => d.name !== "salary.actionSchema").map((d) => ({ ...d, domain: "wb-addon-salary", examples: [] })),
130
136
  }),
131
137
  });
@@ -17,6 +17,21 @@ import {
17
17
  const PAD = (n) => String(n).padStart(2, "0");
18
18
  const MONTH_OF = (date) => Number(String(date).slice(5, 7));
19
19
 
20
+ function incomePeriod(value, payDate, cycle) {
21
+ if (value === null || value === "") return null;
22
+ if (value !== undefined) {
23
+ if (typeof value === "string" && /^\d{4}-\d{2}$/.test(value)) {
24
+ requireDate(`${value}-01`, "工资所属期");
25
+ return value;
26
+ }
27
+ return requireDate(value, "工资所属期");
28
+ }
29
+ if (cycle === "daily") return payDate;
30
+ const year = Number(payDate.slice(0, 4));
31
+ const month = MONTH_OF(payDate);
32
+ return month === 1 ? (year === 2000 ? null : `${year - 1}-12`) : `${year}-${PAD(month - 1)}`;
33
+ }
34
+
20
35
  function yearNumber(value) {
21
36
  const year = Number(value) || new Date(now()).getFullYear();
22
37
  if (!Number.isInteger(year) || year < 2000 || year > 2100) throw new Error("年份必须在 2000–2100 之间");
@@ -49,7 +64,7 @@ export function createSalary({ db, host, log }) {
49
64
  const latest = one("SELECT * FROM salary_record WHERE deleted_at IS NULL ORDER BY pay_date DESC, created_at DESC LIMIT 1");
50
65
  const y = one("SELECT COUNT(*) n, COALESCE(SUM(net_pay),0) net FROM salary_record WHERE deleted_at IS NULL AND substr(pay_date,1,4)=?", String(year));
51
66
  return {
52
- version: "0.1.0",
67
+ version: "0.3.0",
53
68
  records: total,
54
69
  latestDate: latest ? latest.pay_date : "",
55
70
  latestNetPay: latest ? round2(latest.net_pay) : 0,
@@ -92,17 +107,19 @@ export function createSalary({ db, host, log }) {
92
107
  function create(input = {}) {
93
108
  const payDate = requireDate(input.payDate, "发薪日期");
94
109
  const cycle = normalizeCycle(input.cycle);
110
+ const period = incomePeriod(input.incomePeriod, payDate, cycle);
95
111
  const money = moneyInput(input);
96
112
  const gross = hasValue(input.grossPay) ? money.grossPay : derivedTotals(money).gross;
97
113
  const net = hasValue(input.netPay) ? money.netPay : round2(Math.max(gross - sumKeys(money, SOCIAL_KEYS) - money.incomeTax - money.postTaxDeduction, 0));
98
114
  const id = newId("sal");
99
115
  const columns = MONEY_FIELDS.map((f) => f.column);
100
116
  const values = MONEY_FIELDS.map((f) => (f.key === "grossPay" ? gross : f.key === "netPay" ? net : money[f.key]));
101
- const placeholders = Array.from({ length: columns.length + 6 }, () => "?").join(", ");
117
+ const placeholders = Array.from({ length: columns.length + 7 }, () => "?").join(", ");
102
118
  run(
103
- `INSERT INTO salary_record (id, pay_date, cycle, ${columns.join(", ")}, note, created_at, updated_at) VALUES (${placeholders})`,
119
+ `INSERT INTO salary_record (id, pay_date, income_period, cycle, ${columns.join(", ")}, note, created_at, updated_at) VALUES (${placeholders})`,
104
120
  id,
105
121
  payDate,
122
+ period,
106
123
  cycle,
107
124
  ...values,
108
125
  String(input.note === undefined ? "" : input.note).trim(),
@@ -117,6 +134,7 @@ export function createSalary({ db, host, log }) {
117
134
  const row = record(input.id);
118
135
  const patch = {};
119
136
  if (input.payDate !== undefined) patch.payDate = requireDate(input.payDate, "发薪日期");
137
+ if (input.incomePeriod !== undefined) patch.incomePeriod = incomePeriod(input.incomePeriod);
120
138
  if (input.cycle !== undefined) patch.cycle = normalizeCycle(input.cycle);
121
139
  if (input.note !== undefined) patch.note = String(input.note || "").trim();
122
140
  let moneyChanged = false;
@@ -131,8 +149,9 @@ export function createSalary({ db, host, log }) {
131
149
  if (!hasValue(input.netPay) && (moneyChanged || hasValue(input.grossPay))) merged.netPay = netFromGross(merged);
132
150
  const assignments = MONEY_FIELDS.map((f) => f.column + "=?");
133
151
  run(
134
- `UPDATE salary_record SET pay_date=?, cycle=?, ${assignments.join(", ")}, note=?, updated_at=?, rev=rev+1 WHERE id=? AND deleted_at IS NULL`,
152
+ `UPDATE salary_record SET pay_date=?, income_period=?, cycle=?, ${assignments.join(", ")}, note=?, updated_at=?, rev=rev+1 WHERE id=? AND deleted_at IS NULL`,
135
153
  merged.payDate,
154
+ merged.incomePeriod,
136
155
  merged.cycle,
137
156
  ...MONEY_FIELDS.map((f) => merged[f.key]),
138
157
  merged.note,
@@ -157,7 +176,12 @@ export function createSalary({ db, host, log }) {
157
176
 
158
177
  function stats(input = {}) {
159
178
  const year = yearNumber(input.year);
160
- const rows = all("SELECT * FROM salary_record WHERE deleted_at IS NULL AND substr(pay_date,1,4)=? ORDER BY pay_date, created_at", String(year)).map(salaryRow);
179
+ const basis = input.basis ?? "arrival";
180
+ if (basis !== "arrival" && basis !== "income") throw new Error("统计口径须为 arrival 或 income");
181
+ const dateColumn = basis === "income" ? "income_period" : "pay_date";
182
+ const groupDate = (row) => basis === "income" ? row.incomePeriod : row.payDate;
183
+ const rows = all(`SELECT * FROM salary_record WHERE deleted_at IS NULL AND ${dateColumn}>=? AND ${dateColumn}<? ORDER BY ${dateColumn}, pay_date, created_at, id`, `${year}-01`, `${year + 1}-01`).map(salaryRow);
184
+ const unassignedRecords = basis === "income" ? one("SELECT COUNT(*) n FROM salary_record WHERE deleted_at IS NULL AND income_period IS NULL").n : null;
161
185
  const monthly = Array.from({ length: 12 }, (_, i) => ({
162
186
  month: i + 1,
163
187
  period: `${year}-${PAD(i + 1)}`,
@@ -176,7 +200,7 @@ export function createSalary({ db, host, log }) {
176
200
  housingFund: 0,
177
201
  }));
178
202
  for (const row of rows) {
179
- const bucket = monthly[MONTH_OF(row.payDate) - 1];
203
+ const bucket = monthly[MONTH_OF(groupDate(row)) - 1];
180
204
  bucket.records += 1;
181
205
  bucket.basePay = round2(bucket.basePay + row.basePay);
182
206
  bucket.performancePay = round2(bucket.performancePay + row.performancePay);
@@ -208,9 +232,11 @@ export function createSalary({ db, host, log }) {
208
232
  const mom = latestMonth && previousMonth
209
233
  ? { amount: diff, percent: previousMonth.netPay ? round2((diff / previousMonth.netPay) * 100) : null }
210
234
  : null;
211
- const latestRows = latestMonth ? rows.filter((r) => MONTH_OF(r.payDate) === latestMonth.month) : [];
235
+ const latestRows = latestMonth ? rows.filter((r) => MONTH_OF(groupDate(r)) === latestMonth.month) : [];
212
236
  return {
213
237
  year,
238
+ basis,
239
+ unassignedRecords,
214
240
  records: rows.length,
215
241
  months: monthsWithData.length,
216
242
  netTotal,
package/lib/index.js CHANGED
@@ -28,7 +28,7 @@ export function apply(ctx) {
28
28
  id: "wb-addon-salary",
29
29
  title: "工资",
30
30
  description: "按日期记录日结/月结工资:收入项、税前扣款、社保公积金、个税与实发",
31
- version: "0.2.4",
31
+ version: "0.3.0",
32
32
  tables: ["salary_record"],
33
33
  sync: { tables: [] },
34
34
  views: VIEWS,
@@ -45,6 +45,7 @@ export function apply(ctx) {
45
45
  }],
46
46
  fields: [
47
47
  { name: "payDate", type: "string", description: "发薪日期 YYYY-MM-DD;月结通常取当月 15 号" },
48
+ { name: "incomePeriod", type: "string", description: "工资所属期:YYYY-MM 或 YYYY-MM-DD,可独立修改" },
48
49
  { name: "cycle", type: "string", description: "monthly 月结(默认)/ daily 日结" },
49
50
  { name: "netPay", type: "number", description: "实发工资" },
50
51
  { name: "grossPay", type: "number", description: "应发税前工资" },
@@ -55,7 +56,7 @@ export function apply(ctx) {
55
56
  ],
56
57
  notFor: ["不做工资条 OCR", "不做多公司并行核算", "不存姓名等身份信息"],
57
58
  notes: [
58
- "同名日期允许多笔(日结可能一天多笔),统计按发薪日期所属月份聚合。",
59
+ "同日允许多笔;统计可按到账月或所属月聚合,未归属的旧记录仅纳入到账口径。",
59
60
  "金额在本机 SQLite 保存,随宿主备份;不参与云同步。",
60
61
  "agent 可新增、修改、软删除工资记录;删除后不再出现在列表与统计。",
61
62
  ],
@@ -70,7 +71,7 @@ export function apply(ctx) {
70
71
  let imported = 0;
71
72
  let failed = 0;
72
73
  for (const item of records) {
73
- try { engine.create(item); imported += 1; } catch { failed += 1; }
74
+ try { engine.create({ ...item, incomePeriod: item.incomePeriod ?? null }); imported += 1; } catch { failed += 1; }
74
75
  }
75
76
  return { imported, failed };
76
77
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-workbench-salary",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
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.2.4",
19
+ "version": "0.3.0",
20
20
  "minShell": "0.8.0",
21
21
  "tables": [
22
22
  "salary_record"
@@ -43,7 +43,7 @@
43
43
  }
44
44
  },
45
45
  "dependencies": {
46
- "dsh-workbench-host": "^0.8.0"
46
+ "dsh-workbench-host": "^0.8.1"
47
47
  },
48
48
  "dsh": {
49
49
  "bundle": {