Files
weiguang-jewelry-ledger/演示/周转金/contract.md
T

12 KiB
Raw Blame History

当天订单对账演示接口契约

2026-11-02 当前页面新增并只使用 report.daily 的当日对账投影:{date,overview:{orderCount,salesCents,orderCostCents,profitCents},members:[{member,name,date,openingCents,orderCount,salesCents,orderCostCents,profitCents,selfCollectedCents,ownerCollectedCents,otherCollectedCents,ownerAdvancedCents,selfPaidCents,otherAdvancedCents,calculatedToOwnerCents,toOwnerCents,reviewRequired,incomeDetails,channelTotals,costDetails,costsByPayer}]}。金额仍为整数分;收入明细保留orderId、customer、collector、channel、amountCents和self/owner/other分类,成本明细保留orderId、customer、kind、payer及amountCents,两者合计均严格核对。只取所选日期归属订单,真实净利润包含负数;助理公式为麦麦垫付成本+当天净利润-麦麦代收款。亏损日toOwnerCents为null待复核;正常日负值表示麦麦应返/核对。初期余额只作参考。UI支持收入按钮展开客户和渠道,移除转账及累计现金结清流程。旧transfers及旧报告/API仅兼容保留,daily不依赖、不修改或新增转账。原料池FIFO、日加权及刷卡6‰算法不变;全套38/38通过,仅本地修改,未提交或推送。

以下为早期引擎与兼容字段契约,累计转账字段不用于当前日报页面

本目录是独立方案演示,不接正式账本。所有金额对外输入为元(数字或十进制字符串),报告金额统一为整数分;重量输入为克,报告为整数毫克。UI 只格式化金额和重量,不计算结算。

HTTP

  • GET /api/demo/state?date=2026-11-01:返回完整演示数据和该日累计报告。
  • GET /api/demo/report?date=2026-11-01:仅返回该日报告。
  • GET /api/demo/purchase-preview?grams=10&total=10000:返回 {weightMg:10000,totalCents:1000000,unitPriceCents:100000,unitPriceYuanPerGram:1000};单价单位为分/克或元/克,只作展示。
  • POST /api/demo/opening:{member, amount, date?}。只修改成员初始周转金金额;日期固定为演示起始日,传入其他日期会拒绝。
  • POST /api/demo/purchase:{date,payer,grams,total,note?}。购入共用料池,款项已由付款人付出。
  • POST /api/demo/order:{date,member,customer,grams,sale,collector,channel,expenseAmount?,expensePayer?,materialSource?,directMaterialCost?,directMaterialPayer?,note?,allowLoss?}。只登记已交付、货款全部实收的普通销售。materialSource 为 pool(默认)或 direct;direct 为本单自行配货,材料成本当日本人或代付人已付,克重不扣共用池。channel 为 wechat、alipay、cash、bank 或 credit_card;信用卡自动由 owner 收款,手续费 6‰ 由 owner 支付。allowLoss:true 可明确登记亏损待复核单;不自动反向支付利润。
  • POST /api/demo/transfer:{date,from,to,amount,note?,idempotencyKey?}。只允许当前日期这对成员的已形成待转方向、且不超过待转额;登记才改变现金。支持部分转账。相同 idempotencyKey 及相同内容返回原结果,不重复记账;已结清后再次全额转账报错。
  • POST /api/demo/reset:{}。重置到默认示例,已有模拟记录清空。
  • POST /api/demo/scenario:{name:"complex"} 或 {scenario:"complex"} 载入第二个虚构场景;{name:"default"} 等于重置。

所有写入默认响应当前操作日期的完整 state。/reset 与 /scenario 返回场景默认日期。可带 requestId 字段作为表单级幂等键(除 reset/scenario/opening 外);相同内容仅执行一次。Host 限定 localhost/127.0.0.1,Origin 限定同源;仅监听 127.0.0.1:3102。API 错误不修改任何状态。

成功:{ok:true,data:...}。失败:{ok:false,error:{code,message,field?}},状态码 400(校验)、409(结算冲突)、403(Host/Origin)、404(不存在)、405(方法)、413(超64KB)、415(非JSON)。

state.data

{
  version: 1,
  demo: {name: "default", title: "周转金+利润上交+按用料还款", fictional: true, startDate: "2026-11-01", defaultDate: "2026-11-01"},
  members: [{id:"owner",name:"麦麦"},{id:"assistant",name:"助理1"},{id:"assistant2",name:"助理2"}],
  dates: ["2026-11-01"],
  currentDate: "2026-11-01",
  opening: [{member:"owner",amount:1000000,date:"2026-11-01"}, ...],
  purchases: [{id,date,payer,weightMg,totalCents,note}],
  orders: [{id,date,member,customer,weightMg,saleCents,collector,channel,expenseCents,expensePayer,materialSource,directMaterialCostCents,directMaterialPayer,note,allowLoss}],
  transfers: [{id,date,from,to,amountCents,note}],
  report: { /* 下方 */ }
}

report

{
  date: "2026-11-01",
  scenarioName: "default",
  overview: {
    // 以下收入/成本/利润及三个Count是所选日。
    salesCents,materialCostCents,expenseCents,cardFeeCents,orderCostCents,profitCents,orderCount,purchaseCount,transferCount,
    // cumulative 为截至所选日的累计。
    cumulativeSalesCents,cumulativeMaterialCostCents,cumulativeExpenseCents,cumulativeCardFeeCents,cumulativeOrderCostCents,cumulativeProfitCents,cumulativeOrderCount,
    cashCents,unusedMaterialCents,pendingTransferCents
  },
  members: [{
    member:"assistant", name:"助理1", openingCents:1000000,
    // 现金及实收/实付/实转合计截至所选日累计。
    cashCents, collectedCents, purchasePaidCents, directMaterialPaidCents, expensePaidCents, cardFeePaidCents,
    transferInCents, transferOutCents,
    // 本人所选日订单归属数据。
    salesCents,materialCostCents,expenseCents,cardFeeCents,orderCostCents,profitCents,profitDueCents,
    cumulativeSalesCents,cumulativeMaterialCostCents,cumulativeExpenseCents,cumulativeCardFeeCents,cumulativeOrderCostCents,cumulativeProfitCents,cumulativeProfitDueCents,
    unusedMaterialCents, cashPlusUnusedCents,projectedSettledCashCents,
    pendingPayCents,pendingReceiveCents,
    // 仅本人所选日订单:本人自收按渠道、他人代收按成员。
    dailyCollections:[{channel,label,amountCents}],collectedByOthers:[{member,name,amountCents}],
    // 截至所选日此成员实际收款按渠道累计(可包含为别人代收)。
    channelReceipts:[{channel,label,amountCents}],
    cashExplanation:"期初周转金+实际收款-购料-自行配货材料-实付费用-刷卡手续费+已收转账-已付转账",
    expectedSettledAssetsCents, projectedSettledAssetsCents,
    cashStatus:{status:"ok"|"shortfall",shortfallCents,note},
    balanceCheck:{status:"ok"|"review",mathStatus:"ok"|"review",differenceCents,note},
    settlement:{openingPayCents,openingReceiveCents,newPayCents,newReceiveCents,transferredOutCents,transferredInCents,pendingPayCents,pendingReceiveCents}
  }],
  materialPool: {openingWeightMg,openingCostCents,purchaseWeightMg,purchaseCostCents,availableWeightMg,availableCostCents,usedWeightMg,usedCostCents,closingWeightMg,closingCostCents,weightedCostYuanPerGram,weightedUnitPriceCents, funding:[{purchaseId,date,payer,totalCents,consumedCents,remainingCents}]},
  settlements: [{
    pairId:"owner:assistant", memberA:"owner",memberB:"assistant", memberAName:"麦麦",memberBName:"助理1",
    from:"assistant",to:"owner",fromName:"助理1",toName:"麦麦",pendingCents:180000,
    // Signed 以下字段:正数代表 memberA 应向 memberB 转账,负数代表反向。
    openingSignedCents,newSignedCents,transferredSignedCents,pendingSignedCents,
    opening:{from,to,amountCents},newToday:{from,to,amountCents},transferredToday:{from,to,amountCents},
    components:{materialSignedCents,expenseSignedCents,cardFeeSignedCents,collectionSignedCents,profitSignedCents},
    events:[{date,orderId?,transferId?,type,from,to,amountCents,description}],refundAfterRecalculation
  }],
  // 截至所选日全部订单,UI 按 date 与 member 筛选;当前日金额直接取上述统计。
  orders: [{ /* 原订单字段 + */ materialCostCents,cardFeeCents,orderCostCents,profitCents,profitDueCents,reviewRequired,funding:[{purchaseId,payer,amountCents}], formula}],
  transactions:[{id,date,type,member?,from?,to?,amountCents,description}],
  warnings:[{code,message,orderId?}],
  reconciliation:{totalCashCents,totalUnusedCents,totalAssetsCents,openingCents,totalProfitCents,expectedAssetsCents,differenceCents,status,mathStatus,cashShortfallCents}
}

settlements 固定包含三对成员;某对已结清时 pendingCents 为 0,from/to 为 null。同日新增会先抵扣这对成员的代收/代付,仅在成员对内抵扣,不在三人之间循环轧差。pendingTransferCents 为三对累计待转绝对额之和。

openingSignedCents 是报告日前未结余额;newSignedCents 是当日新产生往来;transferredSignedCents 是当日已转;恒等式 openingSignedCents + newSignedCents - transferredSignedCents === pendingSignedCents。组件是截至报告日累计义务;事件包括独立标记的已登记转账,不会把旧利润重新计入本日新增。

projectedSettledCashCents = cashCents + pendingReceiveCents - pendingPayCents;projectedSettledAssetsCents 还加本人付款未用材料。expectedSettledAssetsCents 对助理固定为期初,对麦麦为期初+全店累计利润。亏损政策未确认时仍保留此正常预期,并用 balanceCheck.status = review 与差额明示异常,不自动重新指定亏损承担方。refundAfterRecalculation 兼容字段仅提示有已转情况下当前方向反转,不据此断言改价原因。

现金可为负以供试算,但不自动补本金或生成贷款。此时 cashStatus.status=shortfall,警示明确资金缺口、需核实另行垫入资金;即使数学资产闭环,member.balanceCheck 与总 reconciliation 的 status 也为 review,mathStatus 单独表示数学闭环是否相符。

计算与边界

  1. 共用池按“昨日期末存料成本+当天全部购料”计算当天加权成本;当天订单按录入序号分摊,最后用尽时精确带走剩余分。跨天保留未用材料重量和成本。
  2. 每笔购料仅在购料时扣一次现金。用料成本从购料付款余额 FIFO 分配到付款人:订单归属人向别人归还已耗用成本;未用料仍是原付款人的资金占用资产,不直接形成别人的欠款。
  3. 销售利润=实收售价-实际用料成本-其他费用-刷卡手续费。助理正利润应交麦麦;麦麦自己的利润留在麦麦名下。亏损仅待复核,不能自动产生麦麦向助理的利润补款。
  4. 代收货款形成收款人欠订单归属人;他人代付材料、费用和手续费形成订单归属人欠付款人。
  5. 已登记转账按独立成员对冲减应转。同日加购或历史日补录会重算该日及后续成本,保留所有已转,可能出现反向待返;不能隐去原已付事实。跨日新购不重算采购日前成本。新登记转账始终按所选日当前待转方向和余额检查。
  6. 非普通已交付收齐销售、预收锁价、分期欠款、退款、退货、回收、置换本次不支持;字段不能使本模型猜测正式账的处理方式。

Node模块

require('./engine.js') 导出 createSeed(name='default')、applyCommand(state,type,payload)、buildReport(state,date)、publicState(state,date)、purchasePreview(payload) 和 DemoError。applyCommand 返回新state,不原地修改;失败抛DemoError(code/status/field),且原状态不变。engine不读写任何文件。

require('./server.js').createDemoServer({stateFile,publicDirectory}) 供隔离测试使用;测试必须传系统临时目录文件及随机端口。命令行启动仅使用上述固定/tmp状态文件及3102。