开单成本改为勾选并支持按账号配置默认开单人

This commit is contained in:
summer committed 2026-11-19 02:23:28 +08:00
1 parent 8cac61deb3
commit 6047cc360b
17 files changed
+278 -40

No files matched your search

+9 -6
View File
@@ -5,6 +5,7 @@ const { randomUUID, createHash } = require('node:crypto');
const fs = require('node:fs');
const path = require('node:path');
const Calc = require('./public/calculations');
const FeeEstimates=require('./public/fee-estimates');
const CreditCardFees=require('./public/credit-card-fees');
const CreditCardRates=require('./credit-card-rates');
const ShopSettings=require('./shop-settings');
@@ -151,7 +152,7 @@ class Ledger {
`);
// 增量升级旧库,不改写历史订单金额、单号和成本。
const columns=this.rows('PRAGMA table_info(orders)').map(r=>r.name);
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'",internal_note:"TEXT NOT NULL DEFAULT ''"})){
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'",internal_note:"TEXT NOT NULL DEFAULT ''",fee_cost_mode:"TEXT NOT NULL DEFAULT 'legacy'"})){
if(!columns.includes(name))this.db.exec(`ALTER TABLE orders ADD COLUMN ${name} ${definition}`);
}
// 只加可空分项,不把历史原人数归给任一助理,也不改历史版本及录入信息。
@@ -473,6 +474,7 @@ class Ledger {
const fees=this.validateFees(input.fee_items??prior?.fee_items?.map(r=>({...r,amount:r.amount===null?'':(r.amount/100).toFixed(2)})));
const feesDate=fees.length?day(input.fees_date||date,date):'';
const account=paymentAccount(input.payment_account||prior?.payment_account||'助理微信');
const feeCostMode=prior?.fee_cost_mode||pick(input.fee_cost_mode??'legacy',['legacy','checkboxes'],'费用录入方式');
const newNumber=!prior?.paper_number;
const autoNumber=prior?.paper_date&&prior.paper_number.match(/^\d{8}-?(\d+)$/);
const paper=newNumber?this.paperNumber(date,true):autoNumber?date.replaceAll('-','')+autoNumber[1]:prior.paper_number;
@@ -486,10 +488,10 @@ class Ledger {
this.run('INSERT INTO orders(id,number,date,customer,salesperson,note,internal_note,paper_number,lines,discount,total,status,created) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)',id,number,...common,'draft',now());this.assignMember('order',id,member,{fresh:true});this.enqueue('order:'+id);
CreditCardRates.capture(this,'order',id);
}
this.run('UPDATE orders SET paper_date=?,payment_account=?,fee_items=?,fees_date=?,rounding_mode=? WHERE id=?',newNumber||autoNumber?date:prior?.paper_date||'',account,JSON.stringify(fees),feesDate,roundingMode,id);
this.run('UPDATE orders SET paper_date=?,payment_account=?,fee_items=?,fees_date=?,rounding_mode=?,fee_cost_mode=? WHERE id=?',newNumber||autoNumber?date:prior?.paper_date||'',account,JSON.stringify(fees),feesDate,roundingMode,feeCostMode,id);
this.audit(id,'保存草稿',actor,{revision:this.rawOrder(id).revision});return {id};
}
validateCompletedCosts(lines,fees){
validateCompletedCosts(lines,fees,{checkboxCosts=false}={}){
const missingCosts=lines.flatMap((line,i)=>{
if(line.cost!=null)return [];
const missing=line.costParts?Object.entries(Calc.costLabels).filter(([key])=>line.costParts[key]==null&&!(key==='material'&&line.costMaterial?.mode==='pool')).map(([,label])=>label):['货品成本'];
@@ -499,6 +501,7 @@ class Ledger {
const missingFees=fees.filter(f=>f.amount==null).map(f=>f.note);
for(const [i,line] of lines.entries())for(const charge of line.charges||[]){
if(!charge.id)continue;
if(checkboxCosts&&FeeEstimates.checkboxCategory(charge.name))continue;
const shipping=['快递','快递费','运费'].includes(charge.name),key=shipping?'shipping':'charge:'+charge.id;
if(!fees.some(f=>f.key===key))missingFees.push(`商品${i+1}的${charge.name}`);
}
@@ -521,7 +524,7 @@ class Ledger {
if(!Array.isArray(fees))fail('直接费用项目无效');
// 保存时暂用 estimated 保留显式填写的0,随后确认实际费用时统一转为 recorded。
const feeInputs=fees.map(f=>({...f,status:'estimated'}));
this.validateCompletedCosts(lines,this.validateFees(feeInputs));
this.validateCompletedCosts(lines,this.validateFees(feeInputs),{checkboxCosts:(prior?.fee_cost_mode||input.fee_cost_mode)==='checkboxes'});
const {id}=this.saveOrder({...input,fee_items:feeInputs},actor);
const o=this.rawOrder(id);
this.action(id,'confirm',{date:o.date},actor,NEW_ORDER_ACTION);
@@ -772,7 +775,7 @@ class Ledger {
if(count!==line.stockCount){line.stockCount=count;countsChanged=true;}
}
if(countsChanged){this.run('UPDATE orders SET lines=? WHERE id=?',JSON.stringify(o.lines),id);this.validateCountInventory();}
this.validateCompletedCosts(o.lines,o.fee_items);
this.validateCompletedCosts(o.lines,o.fee_items,{checkboxCosts:o.fee_cost_mode==='checkboxes'});
// 老版本无 status 的费用可能已经入账,只有明确待核实的项目才能在此新增支出。
const fees=o.pendingFees;
if(fees.length)this.recordFees(o,fees,o.fees_date||date,actor,context);
@@ -781,7 +784,7 @@ class Ledger {
} else if(action==='deliver'){
if(o.status!=='confirmed')fail('订单不在待交付状态',409);if(o.balance!==0)fail('未收齐客户款,不能确认交付');if(o.goodsCost===null&&!o.materialCostPending)fail('请先补齐货品成本,不能把空白当作0');
OrderAfterSales.validateStoredLots(this,o,actor,this.businessEnv());
this.validateCompletedCosts(o.lines,o.fee_items);
this.validateCompletedCosts(o.lines,o.fee_items,{checkboxCosts:o.fee_cost_mode==='checkboxes'});
if(o.pendingFees.length)fail('请先核实附加成本,没有发生的费用填0,不能把预估当作实际费用');
if(input.verified!==true)fail('请确认已复称、收款核实并实际交给客户/快递');
if(o.payments.some(p=>p.date>date)||o.expenses.some(e=>e.date>date))fail('交付日期不能早于本单已登记的收款或费用日期');