目录重组:后端按core/domains分领域存放(仅移动与更新引用)
- 50个后端模块用git mv移入core/与domains/<领域>/,入口server.js、launch.js、restore.js及server/原位不动 - 所有require路径由脚本按新旧位置统一换算(后端、server/、测试) - 新增test/app-paths.test.js,锁定默认数据目录、public目录与飞书配置文件的绝对位置 - 维护与部署.md补充目录说明并更新模块路径 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
95c481c74a
commit
9ec37196d4
172 files changed
+458
-384
No files matched your search
@@ -0,0 +1,210 @@
|
||||
'use strict';
|
||||
// 客户寄存料是保管克重,不是店铺自有料,不计采购、资金或利润。
|
||||
const { randomUUID } = require('node:crypto');
|
||||
function initialize(l) {
|
||||
l.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS customer_material_deposits(id TEXT PRIMARY KEY,number TEXT UNIQUE NOT NULL,date TEXT NOT NULL,mg INTEGER NOT NULL CHECK(mg>0),memo TEXT NOT NULL,actor TEXT NOT NULL,actor_id TEXT NOT NULL,created TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS customer_material_events(id TEXT PRIMARY KEY,deposit_id TEXT NOT NULL REFERENCES customer_material_deposits(id),recovery_id TEXT NOT NULL REFERENCES recoveries(id),date TEXT NOT NULL,delta_mg INTEGER NOT NULL CHECK(delta_mg<>0),memo TEXT NOT NULL,actor TEXT NOT NULL,created TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS customer_material_origins(recovery_id TEXT PRIMARY KEY REFERENCES recoveries(id),deposit_id TEXT UNIQUE NOT NULL REFERENCES customer_material_deposits(id),mg INTEGER NOT NULL CHECK(mg>=0));
|
||||
CREATE INDEX IF NOT EXISTS ix_customer_material_events_deposit ON customer_material_events(deposit_id,recovery_id);
|
||||
`);
|
||||
}
|
||||
function active(actor, env) {
|
||||
if (!actor || !['owner', 'assistant'].includes(actor.role)) env.fail('请登录后登记客户寄存料', 403);
|
||||
}
|
||||
function record(l, id, env) {
|
||||
const row = l.one('SELECT * FROM customer_material_deposits WHERE id=?', id);
|
||||
if (!row) env.fail('这笔客户寄存料不存在,请重新选择', 404);
|
||||
return row;
|
||||
}
|
||||
function remaining(l, id, exclude = null) {
|
||||
return l.one(
|
||||
'SELECT mg+(SELECT COALESCE(SUM(delta_mg),0) FROM customer_material_events WHERE deposit_id=d.id AND (? IS NULL OR recovery_id<>?)) AS value FROM customer_material_deposits d WHERE id=?',
|
||||
exclude,
|
||||
exclude,
|
||||
id,
|
||||
)?.value;
|
||||
}
|
||||
function detail(l, id, env) {
|
||||
const row = record(l, id, env),
|
||||
left = remaining(l, id),
|
||||
origin = l.one('SELECT * FROM customer_material_origins WHERE deposit_id=?', id),
|
||||
mg = origin?.mg ?? row.mg;
|
||||
return {
|
||||
id: row.id,
|
||||
number: row.number,
|
||||
date: row.date,
|
||||
mg,
|
||||
remainingMg: left,
|
||||
usedMg: mg - left,
|
||||
...(origin ? { sourceRecoveryId: origin.recovery_id, voided: mg === 0 } : {}),
|
||||
memo: row.memo,
|
||||
actor: row.actor,
|
||||
created: row.created,
|
||||
events: l.rows(
|
||||
'SELECT date,delta_mg AS deltaMg,memo,actor,created FROM customer_material_events WHERE deposit_id=? ORDER BY created,rowid',
|
||||
id,
|
||||
),
|
||||
};
|
||||
}
|
||||
function list(l, query, actor, env) {
|
||||
active(actor, env);
|
||||
const q = env.text(query.q ?? '', '查询', 100, true),
|
||||
rows = l
|
||||
.rows('SELECT id FROM customer_material_deposits ORDER BY date DESC,created DESC,rowid DESC')
|
||||
.map(r => detail(l, r.id, env))
|
||||
.filter(r => !q || [r.number, r.memo, r.actor].some(v => v.includes(q)));
|
||||
return {
|
||||
rows,
|
||||
totalMg: rows.reduce((n, r) => n + r.mg, 0),
|
||||
remainingMg: rows.reduce((n, r) => n + r.remainingMg, 0),
|
||||
usedMg: rows.reduce((n, r) => n + r.usedMg, 0),
|
||||
};
|
||||
}
|
||||
function create(l, input, actor, env) {
|
||||
active(actor, env);
|
||||
const date = env.day(input.date ?? env.today()),
|
||||
mg = env.weight(input.grams, '寄存克重'),
|
||||
memo = env.text(input.memo, '备忘', 240);
|
||||
if (date > env.today()) env.fail('寄存日期不能晚于今天');
|
||||
if (mg <= 0) env.fail('寄存克重须大于0');
|
||||
if (input.verified !== true) env.fail('请确认寄存克重和备忘');
|
||||
const seq = l.meta('customer-material-sequence', 0) + 1;
|
||||
l.setMeta('customer-material-sequence', seq);
|
||||
const id = randomUUID(),
|
||||
number = 'JC' + date.replaceAll('-', '') + String(seq).padStart(4, '0');
|
||||
l.run(
|
||||
'INSERT INTO customer_material_deposits VALUES (?,?,?,?,?,?,?,?)',
|
||||
id,
|
||||
number,
|
||||
date,
|
||||
mg,
|
||||
memo,
|
||||
actor.name,
|
||||
actor.id,
|
||||
env.now(),
|
||||
);
|
||||
l.audit('customer-material:' + id, '登记客户寄存料', actor, {
|
||||
number,
|
||||
date,
|
||||
mg,
|
||||
memo,
|
||||
notice: '只登记保管克重,不计购料、收付、成本或利润',
|
||||
});
|
||||
return detail(l, id, env);
|
||||
}
|
||||
function resolve(l, input, env) {
|
||||
if (input.old_source !== 'deposit') {
|
||||
if (input.old_source !== undefined && input.old_source !== '' && input.old_source !== 'incoming')
|
||||
env.fail('请选择有效旧料来源');
|
||||
return null;
|
||||
}
|
||||
if (input.mode !== 'exchange') env.fail('客户寄存料请从置换入口使用');
|
||||
const row = record(l, input.deposit_id, env),
|
||||
mg = env.weight(input.deposit_weight, '本次使用寄存克重');
|
||||
if (mg <= 0) env.fail('使用寄存克重须大于0');
|
||||
return { id: row.id, number: row.number, mg };
|
||||
}
|
||||
function usages(q) {
|
||||
const map = new Map();
|
||||
for (const part of q?.exchangeItems ?? (q ? [q] : [])) {
|
||||
const d = part.customerDeposit;
|
||||
if (d) map.set(d.id, (map.get(d.id) || 0) + d.mg);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
function validate(l, q, env, { orderId = null, recoveryId = null } = {}) {
|
||||
const exclude = recoveryId ?? (orderId ? l.one('SELECT id FROM recoveries WHERE order_id=?', orderId)?.id : null);
|
||||
for (const [id, mg] of usages(q)) {
|
||||
const row = record(l, id, env);
|
||||
if (exclude && l.one('SELECT 1 FROM customer_material_origins WHERE recovery_id=? AND deposit_id=?', exclude, id))
|
||||
env.fail('不能在原置换单使用本单产生的寄存余料,请另开置换单', 409);
|
||||
if (row.date > q.date) env.fail('寄存料日期晚于置换日期,请核对', 409);
|
||||
if (mg > remaining(l, id, exclude)) env.fail(row.number + ' 的寄存余料不足,请刷新后减少使用克重', 409);
|
||||
}
|
||||
}
|
||||
function apply(l, id, q, actor, env) {
|
||||
validate(l, q, env, { recoveryId: id });
|
||||
const target = usages(q),
|
||||
old = new Map(
|
||||
l
|
||||
.rows(
|
||||
'SELECT deposit_id,SUM(-delta_mg) AS mg FROM customer_material_events WHERE recovery_id=? AND deposit_id NOT IN (SELECT deposit_id FROM customer_material_origins WHERE recovery_id=?) GROUP BY deposit_id',
|
||||
id,
|
||||
id,
|
||||
)
|
||||
.map(r => [r.deposit_id, r.mg]),
|
||||
);
|
||||
for (const depositId of new Set([...old.keys(), ...target.keys()])) {
|
||||
const delta = (old.get(depositId) || 0) - (target.get(depositId) || 0);
|
||||
if (!delta) continue;
|
||||
l.run(
|
||||
'INSERT INTO customer_material_events VALUES (?,?,?,?,?,?,?,?)',
|
||||
randomUUID(),
|
||||
depositId,
|
||||
id,
|
||||
env.today(),
|
||||
delta,
|
||||
delta > 0 ? '置换取消/更正退回寄存料' : '置换使用寄存料',
|
||||
actor.name,
|
||||
env.now(),
|
||||
);
|
||||
}
|
||||
syncSurplus(l, id, q, actor, env);
|
||||
}
|
||||
// 一张置换单的寄存余料汇总为一笔,保留来源和更正流水。
|
||||
function validateSurplus(l, id, q, env) {
|
||||
const origin = l.one('SELECT * FROM customer_material_origins WHERE recovery_id=?', id);
|
||||
if (!origin) return;
|
||||
const target = q?.depositMg ?? 0,
|
||||
used = origin.mg - remaining(l, origin.deposit_id);
|
||||
const rows = l.rows(
|
||||
'SELECT r.id,r.number,r.date,SUM(-e.delta_mg) AS mg FROM customer_material_events e JOIN recoveries r ON r.id=e.recovery_id WHERE e.deposit_id=? AND e.recovery_id<>? GROUP BY r.id HAVING SUM(-e.delta_mg)>0',
|
||||
origin.deposit_id,
|
||||
id,
|
||||
);
|
||||
if (target < used || (q && rows.some(r => r.date < q.date)))
|
||||
env.fail('本单寄存余料已被置换使用,请先更正用料订单:' + rows.map(r => r.number).join('、'), 409);
|
||||
}
|
||||
function syncSurplus(l, id, q, actor, env) {
|
||||
validateSurplus(l, id, q, env);
|
||||
const target = q?.depositMg ?? 0,
|
||||
origin = l.one('SELECT * FROM customer_material_origins WHERE recovery_id=?', id);
|
||||
const source = l.one('SELECT number FROM recoveries WHERE id=?', id),
|
||||
memo = q
|
||||
? (
|
||||
'置换余料 · ' +
|
||||
source.number +
|
||||
' · ' +
|
||||
q.customer +
|
||||
';' +
|
||||
(q.exchangeItems ?? [q])
|
||||
.map(p => p.surplusMemo || '')
|
||||
.filter(Boolean)
|
||||
.join(';')
|
||||
).slice(0, 240)
|
||||
: '';
|
||||
if (!origin) {
|
||||
if (target) {
|
||||
const made = create(l, { date: q.date, grams: (target / 1000).toFixed(3), memo, verified: true }, actor, env);
|
||||
l.run('INSERT INTO customer_material_origins VALUES (?,?,?)', id, made.id, target);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const delta = target - origin.mg;
|
||||
if (delta)
|
||||
l.run(
|
||||
'INSERT INTO customer_material_events VALUES (?,?,?,?,?,?,?,?)',
|
||||
randomUUID(),
|
||||
origin.deposit_id,
|
||||
id,
|
||||
env.today(),
|
||||
delta,
|
||||
q ? '原置换单更正寄存余料' : '原置换单取消,撤回寄存余料',
|
||||
actor.name,
|
||||
env.now(),
|
||||
);
|
||||
l.run('UPDATE customer_material_origins SET mg=? WHERE recovery_id=?', target, id);
|
||||
if (q) l.run('UPDATE customer_material_deposits SET date=?,memo=? WHERE id=?', q.date, memo, origin.deposit_id);
|
||||
}
|
||||
module.exports = { initialize, list, create, resolve, validate, apply, usages, validateSurplus };
|
||||
Reference in new issue
Block a user