人数记录增加两位助理的独立统计
This commit is contained in:
1 parent
499b84a7a6
commit
46530c29b8
7 files changed
+209
-51
No files matched your search
+27
-11
@@ -21,6 +21,13 @@ const now = () => new Date().toISOString();
|
||||
const sum = (rows, key) => rows.reduce((s,r)=>s+(typeof key==='function'?key(r):r[key]),0);
|
||||
function fail(message, status=400) { const e=new Error(message); e.status=status; throw e; }
|
||||
function text(value, name, max=100, optional=false) { const s=String(value??'').trim(); if ((!s&&!optional)||s.length>max) fail(`${name}请填写${optional?'不超过':'1~'}${max}字`); return s; }
|
||||
function peopleNumber(value,name,optional=false){
|
||||
if(optional&&(value===null||typeof value==='string'&&!value.trim()))return null;
|
||||
const raw=['number','string'].includes(typeof value)?String(value).trim():'';
|
||||
if(!/^\d+$/.test(raw))fail(`${name}须为0~1000000的整数`);
|
||||
const count=Number(raw);if(!Number.isSafeInteger(count)||count>1000000)fail(`${name}须为0~1000000的整数`);
|
||||
return count;
|
||||
}
|
||||
function fixed(value, places, name, negative=false) {
|
||||
const s=String(value??'').trim(); const re=new RegExp(`^${negative?'-?':''}\\d+(?:\\.\\d{1,${places}})?$`);
|
||||
if(!re.test(s)) fail(`${name}须为${negative?'可带负号的':''}数字,最多${places}位小数`);
|
||||
@@ -71,6 +78,10 @@ class Ledger {
|
||||
for(const [name,definition] of Object.entries({paper_date:"TEXT NOT NULL DEFAULT ''",payment_account:"TEXT NOT NULL DEFAULT '助理微信'",fee_items:"TEXT NOT NULL DEFAULT '[]'",fees_date:"TEXT NOT NULL DEFAULT ''",rounding_mode:"TEXT NOT NULL DEFAULT 'cent'"})){
|
||||
if(!columns.includes(name))this.db.exec(`ALTER TABLE orders ADD COLUMN ${name} ${definition}`);
|
||||
}
|
||||
// 只加可空分项,不把历史原人数归给任一助理,也不改历史版本及录入信息。
|
||||
const peopleColumns=this.rows('PRAGMA table_info(daily_people_counts)').map(row=>row.name);
|
||||
for(const name of ['assistant1_count','assistant2_count'])if(!peopleColumns.includes(name))
|
||||
this.db.exec(`ALTER TABLE daily_people_counts ADD COLUMN ${name} INTEGER CHECK(${name} IS NULL OR (typeof(${name})='integer' AND ${name}>=0 AND ${name}<=1000000))`);
|
||||
PriceLocks.initialize(this);
|
||||
MaterialPools.initialize(this);
|
||||
OrderTemplates.initialize(this);
|
||||
@@ -499,28 +510,33 @@ class Ledger {
|
||||
return {after_sale_id:id,kind:'afterSale'};
|
||||
}
|
||||
peopleCount(date){
|
||||
day(date);const record=this.one('SELECT count,revision FROM daily_people_counts WHERE date=?',date);
|
||||
return {date,count:record?.count??null,revision:record?.revision??0};
|
||||
day(date);const record=this.one('SELECT count,assistant1_count,assistant2_count,revision FROM daily_people_counts WHERE date=?',date);
|
||||
return {date,count:record?.count??null,assistant1Count:record?.assistant1_count??null,assistant2Count:record?.assistant2_count??null,revision:record?.revision??0};
|
||||
}
|
||||
peopleCountSummary(date,from=date){
|
||||
day(date);day(from);if(from>date)fail('开始日期不能晚于结束日期');
|
||||
const totals=this.one('SELECT COALESCE(SUM(count),0) AS total,COUNT(*) AS recordedDays FROM daily_people_counts WHERE date>=? AND date<=?',from,date);
|
||||
return {from,date,total:totals.total,recordedDays:totals.recordedDays};
|
||||
const totals=this.one(`SELECT COALESCE(SUM(count),0) AS total,COUNT(*) AS recordedDays,
|
||||
COALESCE(SUM(assistant1_count),0) AS assistant1Total,COUNT(assistant1_count) AS assistant1Days,
|
||||
COALESCE(SUM(assistant2_count),0) AS assistant2Total,COUNT(assistant2_count) AS assistant2Days
|
||||
FROM daily_people_counts WHERE date>=? AND date<=?`,from,date);
|
||||
return {from,date,total:totals.total,recordedDays:totals.recordedDays,
|
||||
assistant1:{total:totals.assistant1Total,recordedDays:totals.assistant1Days},assistant2:{total:totals.assistant2Total,recordedDays:totals.assistant2Days}};
|
||||
}
|
||||
savePeopleCount(input,actor){
|
||||
if(!actor||!['owner','assistant'].includes(actor.role))fail('请使用麦麦或助理账号登记人数',403);
|
||||
if(!input||typeof input!=='object'||Array.isArray(input))fail('人数记录格式无效');
|
||||
const date=day(input.date),raw=['number','string'].includes(typeof input.count)?String(input.count).trim():'';
|
||||
if(!/^\d+$/.test(raw))fail('人数须为0~1000000的整数');
|
||||
const count=Number(raw);if(!Number.isSafeInteger(count)||count>1000000)fail('人数须为0~1000000的整数');
|
||||
const date=day(input.date),count=peopleNumber(input.count,'人数');
|
||||
if(!Number.isSafeInteger(input.revision)||input.revision<0)fail('人数记录版本无效,请刷新后再保存');
|
||||
const previous=this.one('SELECT count,revision FROM daily_people_counts WHERE date=?',date);
|
||||
const previous=this.one('SELECT count,assistant1_count,assistant2_count,revision FROM daily_people_counts WHERE date=?',date);
|
||||
if(input.revision!==(previous?.revision??0))fail('该日人数已被更新,请刷新后核对再保存',409);
|
||||
// 旧页面或旧中断操作只有 count;缺省时保留分项,显式 null/空白才清为未记录。
|
||||
const assistant1Count=Object.hasOwn(input,'assistant1Count')?peopleNumber(input.assistant1Count,'助理1号人数',true):previous?.assistant1_count??null;
|
||||
const assistant2Count=Object.hasOwn(input,'assistant2Count')?peopleNumber(input.assistant2Count,'助理2号人数',true):previous?.assistant2_count??null;
|
||||
const revision=input.revision+1,timestamp=now();
|
||||
const changed=this.run('INSERT INTO daily_people_counts(date,count,revision,actor,created,updated) VALUES (?,?,?,?,?,?) ON CONFLICT(date) DO UPDATE SET count=excluded.count,revision=excluded.revision,actor=excluded.actor,updated=excluded.updated WHERE daily_people_counts.revision=?',date,count,revision,actor.name,timestamp,timestamp,input.revision);
|
||||
const changed=this.run('INSERT INTO daily_people_counts(date,count,assistant1_count,assistant2_count,revision,actor,created,updated) VALUES (?,?,?,?,?,?,?,?) ON CONFLICT(date) DO UPDATE SET count=excluded.count,assistant1_count=excluded.assistant1_count,assistant2_count=excluded.assistant2_count,revision=excluded.revision,actor=excluded.actor,updated=excluded.updated WHERE daily_people_counts.revision=?',date,count,assistant1Count,assistant2Count,revision,actor.name,timestamp,timestamp,input.revision);
|
||||
if(changed.changes!==1)fail('该日人数已被更新,请刷新后核对再保存',409);
|
||||
this.audit('people-count:'+date,previous?'更新人数':'登记人数',actor,{date,count,previousCount:previous?.count??null,revision});
|
||||
return {date,count,revision};
|
||||
this.audit('people-count:'+date,previous?'更新人数':'登记人数',actor,{date,count,assistant1Count,assistant2Count,previousCount:previous?.count??null,previousAssistant1Count:previous?.assistant1_count??null,previousAssistant2Count:previous?.assistant2_count??null,revision});
|
||||
return {date,count,assistant1Count,assistant2Count,revision};
|
||||
}
|
||||
orderBook(query={}){return orderBook(this,query,{today:today(),validateDay:day,accounts:ACCOUNTS});}
|
||||
orderTemplates(){return OrderTemplates.list(this);}
|
||||
|
||||
Reference in new issue
Block a user