import{n as e}from"./rolldown-runtime-Dl_DZoUm.js";import{a as t,o as n}from"./receipt-BUADJAzs.js";import{n as r,r as i,t as a}from"./receipts-B7E3sgzO.js";import{c as o,l as s,s as c,tr as l,u,v as d}from"./index-D422TGjj.js";import{o as f,t as p}from"./shared-Da74NgmJ.js";import{r as m,t as h}from"./format-u4LYCVYL.js";import{a as g,c as _,d as v,f as y,i as b,l as x,n as S,o as C,p as w,r as T,s as E,t as D,u as O}from"./model-CDW0MnQe.js";import{a as k,i as A,n as j,o as M,r as N,s as P,t as F}from"./BusinessControls-B3wlV0Qu.js";var I=e(l(),1),L=d();function R({kind:e,id:t,children:n}){let r={orders:[`open-order`,`order`],"price-locks":[`open-price-lock`,`lock`],recoveries:[`recovery-open`,`recovery`]}[e];return(0,L.jsx)(F,{type:`button`,className:`link-button`,"data-action":r[0],"data-id":t,onClick:()=>{t&&o(e,{search:`?`+r[1]+`=`+encodeURIComponent(t)})},children:n})}function ee({pool:e,cutover:t}){let n=Object.hasOwn(e,`personalPools`);return(0,L.jsxs)(`section`,{className:`panel material-rolling-current`,"aria-label":n?`当前公共材料`:`当前统一材料`,children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsxs)(`h2`,{children:[e.date,` `,n?`公共材料`:`当前材料`]}),(0,L.jsx)(`span`,{className:`small muted`,children:n?`个人回收料独立在下方展示`:`自 `+t+` 起按日结转`})]}),(0,L.jsxs)(`div`,{className:`material-rolling-totals`,children:[(0,L.jsxs)(`div`,{children:[(0,L.jsx)(`span`,{children:n?`公共料加权料价`:`当前最终加权料价`}),(0,L.jsx)(`strong`,{children:e.averageRate==null?`待补采购`:(0,L.jsxs)(L.Fragment,{children:[`¥`,m(e.averageRate),(0,L.jsx)(`small`,{children:`/克`})]})})]}),(0,L.jsxs)(`div`,{children:[(0,L.jsx)(`span`,{children:n?`公共剩余克重`:`统一剩余克重`}),(0,L.jsxs)(`strong`,{children:[h(e.remainingMg),(0,L.jsx)(`small`,{children:`g`})]})]})]}),n?(0,L.jsxs)(L.Fragment,{children:[(0,L.jsxs)(`p`,{className:`small muted`,children:[`公共料共 `,h(e.totalMg),`g,已用/预留 `,h(e.usedMg),`g;余料成本 `,D(e.remainingCost),`。`,e.shortageMg?`公共料还缺 `+h(e.shortageMg)+`g。`:``]}),(0,L.jsx)(`p`,{className:`small muted`,children:`订单先用归属人的个人回收料,不足由公共料补足;两类来源各自加权、各自保留付款人与余料。`})]}):(0,L.jsxs)(L.Fragment,{children:[!!e.shortageMg&&(0,L.jsxs)(`p`,{className:`danger-text`,children:[`还需补足 `,h(e.shortageMg),`g,今日订单料费与利润暂待补。`]}),(0,L.jsxs)(`details`,{className:`material-rolling-composition`,children:[(0,L.jsx)(`summary`,{children:`查看今天的料价组成`}),(0,L.jsx)(`div`,{className:`material-rolling-parts`,children:[[`昨日以前剩余材料`,e.opening],[`今日购料(`+(e.todayPurchases.count||0)+`笔)`,e.todayPurchases],[`今日退货回料(`+(e.todayReturns.count||0)+`笔)`,e.todayReturns]].map(e=>{let[t,n]=e;return(0,L.jsxs)(`div`,{children:[(0,L.jsx)(`span`,{children:t}),(0,L.jsxs)(`strong`,{children:[h(n.mg),`g`]}),(0,L.jsx)(`span`,{children:D(n.amount)})]},t)})}),(0,L.jsx)(`p`,{className:`small muted`,children:`加权料价=(结转成本+今日采购成本+今日退货回料原金料成本)÷对应克重。退货按原订单金料成本与实际退回克重计入;同日售退净抵销,完整退回记录保留。工费、快递费不加入材料成本。`}),(0,L.jsxs)(`p`,{className:`small muted`,children:[`今天共可计料 `,h(e.totalMg),`g,已耗用 `,h(e.usedMg),`g;剩余材料成本 `,D(e.remainingCost),`。`]})]}),(0,L.jsx)(`p`,{className:`small muted material-rolling-rule`,children:`今日加料只重算今日订单,旧日订单冻结。历史购料录错,提交更正后由店主复核。`})]})]})}function z({pool:e,state:t}){if(!e.personalPools)return null;let n=Object.entries(e.personalPools).filter(([e])=>t.user.role===`owner`||e===f.currentMember(t));return(0,L.jsxs)(`section`,{className:`panel material-personal-pools`,children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsxs)(`h2`,{children:[e.date,` 个人回收料`]}),(0,L.jsx)(`span`,{className:`small muted`,children:`回收归属与公共购料分别核算`})]}),n.map(([n,r])=>{let i=(e.usages||[]).filter(e=>e.active&&!e.anonymous&&e.materialSources?.some(e=>e.kind===`personal_recovery`&&e.member===n));return(0,L.jsxs)(`article`,{className:`material-personal-pool`,children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsxs)(`h3`,{children:[f.memberName(t,n),`个人回收料`]}),(0,L.jsxs)(`strong`,{children:[`余 `,h(r.remainingMg),`g`]})]}),(0,L.jsxs)(`div`,{className:`material-personal-totals`,children:[(0,L.jsxs)(`span`,{children:[`加权料价 `,r.averageRate==null?`待核对`:D(r.averageRate)+`/克`]}),(0,L.jsxs)(`span`,{children:[`已用/预留 `,h(r.usedMg),`g`]}),(0,L.jsxs)(`span`,{children:[`余料成本 `,D(r.remainingCost)]})]}),(0,L.jsxs)(`details`,{children:[(0,L.jsx)(`summary`,{children:`查看回收来源与占用`}),(0,L.jsx)(`ul`,{children:r.purchases?.length?r.purchases.map((e,t)=>(0,L.jsxs)(`li`,{children:[e.sourceKind===`carry`?`以前结转`:e.sourceKind===`return`?`退回原料`:`回收生成`,` ·`,` `,h(e.mg),`g · `,D(e.amount),` `,(e.recoveryId||e.recovery_id)&&(0,L.jsx)(R,{kind:`recoveries`,id:e.recoveryId||e.recovery_id,children:`查看回收原单`})]},e.id||t)):(0,L.jsx)(`li`,{children:`来源随原回收单保留。`})}),i.length?(0,L.jsx)(`ul`,{children:i.map((e,t)=>(0,L.jsxs)(`li`,{children:[e.kind===`priceLock`?`锁价预留`:`订单耗用`,` `,h(e.materialSources.filter(e=>e.kind===`personal_recovery`&&e.member===n).reduce((e,t)=>e+t.quantity,0)),`g ·`,` `,e.kind===`priceLock`&&e.price_lock_id?(0,L.jsx)(R,{kind:`price-locks`,id:e.price_lock_id,children:e.number||`查看锁价单`}):e.order_id?(0,L.jsx)(R,{kind:`orders`,id:e.order_id,children:e.number||`查看订单`}):`原单留档`]},t))}):(0,L.jsx)(`p`,{className:`small muted`,children:`当前没有已用或预留克重。`})]})]},n)}),!n.length&&(0,L.jsx)(`p`,{className:`small muted`,children:`本人当前没有个人回收料,订单不足部分使用公共料。`}),(0,L.jsx)(`p`,{className:`small muted`,children:`回收专用料仅供归属人的订单先用,不与其他人的回收料混用;回收来源请从原回收/置换单更正,已有耗用或锁价预留须先释放。`})]})}function B({p:e,rolling:t,state:n,openDialog:r}){let i=``;if(e.sourceKind&&e.sourceKind!==`purchase`?i=`材料结转记录`:e.recovery_id?i=`随回收/置换原单保留`:f.teamMode(n)&&n.user.role!==`owner`&&!e.canEdit&&!e.canRequestChange&&(i=`共享采购`),i)return(0,L.jsx)(`span`,{className:`small muted`,children:i});let a=S(e,t,n.today)?`申请`:``;return(0,L.jsx)(`div`,{className:`actions`,children:[`edit`,`delete`].map(t=>(0,L.jsxs)(F,{type:`button`,className:t===`edit`?`quiet`:`danger`,"data-id":e.id,"data-action":t+`-material-purchase`,onClick:()=>r({kind:t,id:e.id}),children:[a,t===`edit`?`修改`:`删除`]},t))})}function V({pool:e,state:t,openDialog:n}){if(f.teamMode(t))return(0,L.jsx)(`p`,{className:`small muted`,children:`购料在实际付款当天记支出,未用完的料结转留用;订单按实际耗用自动计算料费,并核算与付款人的往来,销售耗用不再重复记现金支出。成员往来在每日对账核对,旧料款结算只作历史追溯。`});let r=e.settlement;if(!r)return null;let i=r.settlementDue,a=r.pending||i==null,o=f.memberName(t,`owner`),s=f.memberName(t,`assistant`);return(0,L.jsxs)(`div`,{className:`material-pool-settlement`,children:[(0,L.jsxs)(`div`,{className:`material-settlement-line`,children:[(0,L.jsxs)(`strong`,{children:[`本批金料与`,s,`结算`]}),(0,L.jsxs)(`span`,{children:[s,`垫付的剩余料款`,` `,r.assistantInventoryCredit==null?`待补采购`:D(r.assistantInventoryCredit)]}),(0,L.jsx)(`span`,{children:a?`采购未覆盖耗用,结算待补`:i===0?`已结清`:(i<0?o+`待转回`+s:s+`待交`+o)+` `+D(Math.abs(i))}),!a&&i!==0&&(0,L.jsx)(F,{type:`button`,"data-action":`material-pool-settlement`,"data-id":e.id,onClick:()=>n({kind:`settlement`,pool:e}),children:`登记实际交款/返款`})]}),!!e.settlements?.length&&(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsx)(j,{className:`material-detail-table`,columns:[{title:`日期`},{title:`实际交款/返款`},{title:`金额`,headerClassName:`number`},{title:`备注`}],rows:e.settlements.map((e,t)=>({key:t,cellClasses:[void 0,void 0,`number`,void 0],cells:[e.date,e.amount<0?o+` → `+s:s+` → `+o,D(Math.abs(e.amount)),e.note]}))})})]})}function H(e,t){let n=e.allocations||f.memberKeys(t).filter(t=>Number.isSafeInteger(e[`${t}_amount`])).map(t=>({payer:t,amount:e[`${t}_amount`]}));return n.length?n.map(e=>f.memberName(t,e.payer)+` `+D(e.amount)).join(` · `):`付款来源待补齐`}function U({pool:e,rolling:t,state:n,openDialog:r}){let i=b(e),a=(e.usages||[]).filter(e=>!f.teamMode(n)||n.user.role===`owner`||(e.order_id||e.price_lock_id)&&(e.member==null||e.member===f.currentMember(n)));return(0,L.jsxs)(`div`,{className:`material-pool-details`,children:[(0,L.jsxs)(`h3`,{children:[e.date,` 购料明细(按录入先后)`]}),(0,L.jsx)(`p`,{className:`small muted`,children:`单价=本笔购买总价÷购买克重,与当天加权料价分开显示;显示精度不改变记账金额。`}),(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsxs)(M,{className:`material-detail-table material-purchase-table`,children:[(0,L.jsx)(`thead`,{children:(0,L.jsxs)(`tr`,{children:[(0,L.jsx)(`th`,{children:`购料日期`}),(0,L.jsx)(`th`,{className:`number`,children:`购买克重`}),(0,L.jsx)(`th`,{className:`number`,children:`单价(元/克)`}),(0,L.jsx)(`th`,{className:`number`,children:`购买总价`}),(0,L.jsx)(`th`,{children:`付款人`}),(0,L.jsx)(`th`,{children:`备注`}),(0,L.jsx)(`th`,{children:`操作`})]})}),(0,L.jsxs)(`tbody`,{children:[i.map(e=>(0,L.jsxs)(`tr`,{"data-purchase-id":e.id,children:[(0,L.jsx)(`td`,{children:e.date}),(0,L.jsxs)(`td`,{className:`number`,children:[h(e.mg),`g`]}),(0,L.jsx)(`td`,{className:`number`,children:m(e.amount*1e3/e.mg)}),(0,L.jsx)(`td`,{className:`number`,children:D(e.amount)}),(0,L.jsx)(`td`,{children:T(e)?f.memberName(n,T(e)):`待补`}),(0,L.jsx)(`td`,{className:`material-purchase-note`,children:e.note}),(0,L.jsx)(`td`,{children:(0,L.jsx)(B,{p:e,rolling:t,state:n,openDialog:r})})]},e.id)),!i.length&&(0,L.jsx)(`tr`,{children:(0,L.jsx)(`td`,{colSpan:7,children:`尚未登记实际购料。`})})]})]})}),!!e.returns?.length&&(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`h3`,{children:`退货回料追溯`}),(0,L.jsx)(`p`,{className:`small muted`,children:`回料没有新增购料付款。金料按原订单成本计入,工费与快递费不加入料池。`}),(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsx)(j,{className:`material-detail-table material-return-table`,columns:[{title:`回料日期/原订单`},{title:`实际退回克重`,headerClassName:`number`},{title:`原金料成本`,headerClassName:`number`},{title:`计入方式`}],rows:e.returns.map((t,n)=>({key:n,cellClasses:[void 0,`number`,`number`,void 0],cells:[(0,L.jsxs)(L.Fragment,{children:[t.date||e.date,t.order_id&&(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`br`,{}),(0,L.jsx)(R,{kind:`orders`,id:t.order_id,children:t.number||`查看原订单`})]})]}),(0,L.jsxs)(L.Fragment,{children:[h(t.mg??t.grossMg),`g`]}),D(t.amount??t.grossAmount),t.sameDay?`同日售退已净抵销`:`并入回料当天材料`]}))})})]}),(0,L.jsx)(`h3`,{children:`订单耗用与锁价原料明细`}),(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsxs)(M,{className:`material-detail-table`,children:[(0,L.jsx)(`thead`,{children:(0,L.jsxs)(`tr`,{children:[(0,L.jsx)(`th`,{children:`日期/订单或锁价单`}),(0,L.jsx)(`th`,{children:`客户/商品`}),(0,L.jsx)(`th`,{className:`number`,children:`使用克重`}),(0,L.jsx)(`th`,{className:`number`,children:`耗用料费`}),(0,L.jsx)(`th`,{children:`耗用料款归属/状态`})]})}),(0,L.jsxs)(`tbody`,{children:[a.map((e,t)=>(0,L.jsxs)(`tr`,{className:e.active?void 0:`muted`,children:[(0,L.jsxs)(`td`,{children:[e.date,(0,L.jsx)(`br`,{}),(0,L.jsx)(R,{kind:e.kind===`priceLock`?`price-locks`:`orders`,id:e.kind===`priceLock`?e.price_lock_id:e.order_id,children:e.number||(e.kind===`priceLock`?`查看锁价单`:`查看订单`)})]}),(0,L.jsxs)(`td`,{children:[e.customer||`未填称呼`,(0,L.jsxs)(`div`,{className:`small muted`,children:[e.kind===`priceLock`?`锁价原料`:`商品 `+(Number(e.line_index)+1),` · `,e.product]})]}),(0,L.jsxs)(`td`,{className:`number`,children:[h(e.mg),`g`]}),(0,L.jsx)(`td`,{className:`number`,children:e.amount==null?`料费待补`:D(e.amount)}),(0,L.jsx)(`td`,{children:e.active?e.amount==null?`待补采购`:H(e,n):`已释放,不计已用克重`})]},t)),!a.length&&(0,L.jsx)(`tr`,{children:(0,L.jsx)(`td`,{colSpan:5,children:`尚未关联订单或锁价。`})})]})]})}),(0,L.jsx)(V,{pool:e,state:n,openDialog:r})]})}function W({data:e,state:t,openDate:n,toggle:r,openDialog:i}){let a=!!e.rolling?.enabled,o=g(e),s=a?6:9,c=e=>o.reduce((t,n)=>t+(n[e]??0),0),l={pending:`待补采购`,unused:`未使用`,using:`使用中`,exhausted:`已用完`},u=a?[`日期`,`实际购料克重`,`实际购买总价`,`当日耗用克重`,`记录`,`明细`]:[`料价日期`,`购买总克重`,`购买总价`,`加权进货料价`,`已用克重`,`剩余克重`,`付款人`,`状态`,`明细`];return(0,L.jsxs)(L.Fragment,{children:[(a?[e.current]:o).map(e=>(0,L.jsx)(z,{pool:e,state:t},e.id)),!a&&(0,L.jsxs)(L.Fragment,{children:[e.rolling&&(0,L.jsxs)(`section`,{className:`panel material-rolling-start`,children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsx)(`h2`,{children:`合并未用完的材料`}),t.user.role===`owner`&&e.rolling.canEnable&&(0,L.jsx)(F,{type:`button`,className:`primary`,"data-action":`material-rolling-enable-preview`,onClick:()=>i({kind:`enable`}),children:`核对并启用跨日结转`})]}),(0,L.jsx)(`p`,{className:`small muted`,children:`启用后,昨天以前未用完的料合并到今天,只保留一个当前加权料价和剩余克重。今日已有订单按新料价核算,旧日订单不变。`}),(e.rolling.reason||t.user.role!==`owner`)&&(0,L.jsx)(`p`,{className:`small muted`,children:e.rolling.reason||`由店主账号核对后启用。`})]}),(0,L.jsxs)(`div`,{className:`material-pool-summary`,"aria-label":`每日料价合计`,children:[(0,L.jsxs)(`span`,{children:[`累计购料 `,(0,L.jsxs)(`strong`,{children:[h(c(`totalMg`)),`g`]})]}),(0,L.jsxs)(`span`,{children:[`购买总价 `,(0,L.jsx)(`strong`,{children:D(c(`totalAmount`))})]}),(0,L.jsxs)(`span`,{children:[`已用克重 `,(0,L.jsxs)(`strong`,{children:[h(c(`usedMg`)),`g`]})]}),(0,L.jsxs)(`span`,{children:[`剩余克重 `,(0,L.jsxs)(`strong`,{children:[h(c(`remainingMg`)),`g`]})]}),!!e.settlement?.pendingCount&&(0,L.jsx)(`span`,{className:`danger-text`,children:`有每日料价待补采购,结算暂待补`})]})]}),(0,L.jsxs)(`section`,{className:`panel material-pool-list-panel`+(a?` material-rolling-history`:``),children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsx)(`h2`,{children:a?`历史采购与耗用追溯`:`每日料价`}),(0,L.jsxs)(`span`,{className:`small muted`,children:[o.length,` 个日期 · `,a?`历史材料已计入结转`:`克重、料价与状态自动计算`]})]}),(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsxs)(M,{className:`material-pool-table`+(a?` material-rolling-history-table`:``),children:[(0,L.jsx)(`thead`,{children:(0,L.jsx)(`tr`,{children:u.map((e,t)=>(0,L.jsx)(`th`,{className:t>0&&t<(a?4:6)?`number`:void 0,children:e},e))})}),(0,L.jsxs)(`tbody`,{children:[o.map(o=>{let c=o.id===n,u=b(o);return(0,L.jsxs)(I.Fragment,{children:[(0,L.jsxs)(`tr`,{children:[(0,L.jsx)(`td`,{children:(0,L.jsx)(`strong`,{children:o.date})}),(0,L.jsxs)(`td`,{className:`number`,children:[h(a?u.reduce((e,t)=>e+t.mg,0):o.totalMg),`g`]}),(0,L.jsxs)(`td`,{className:`number`,children:[`¥`,m(a?u.reduce((e,t)=>e+t.amount,0):o.totalAmount)]}),!a&&(0,L.jsx)(`td`,{className:`number`,children:o.averageRate==null?`待补采购`:D(o.averageRate)+`/克`}),(0,L.jsxs)(`td`,{className:`number`,children:[h(o.usedMg),`g`]}),a?(0,L.jsx)(`td`,{children:(0,L.jsx)(`span`,{className:`small muted`,children:o.id===e.current.id?`今日记录`:o.dateNumber(o.byPayer?.[e]?.purchased)>0).map(e=>f.memberName(t,e)).join(`、`)||`待补采购`}),(0,L.jsx)(`td`,{children:(0,L.jsx)(`span`,{className:`badge `+(o.status===`pending`?`warn`:``),children:l[o.status]||o.status})})]}),(0,L.jsx)(`td`,{children:(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`toggle-material-pool`,"data-id":o.id,"aria-expanded":c,onClick:()=>r(c?``:o.id),children:c?`收起`:`查看`})})]}),c&&(0,L.jsx)(`tr`,{className:`material-pool-detail-row`,children:(0,L.jsx)(`td`,{colSpan:s,children:(0,L.jsx)(U,{pool:o,rolling:a,state:t,openDialog:i})})})]},o.id)}),!o.length&&(0,L.jsx)(`tr`,{children:(0,L.jsx)(`td`,{colSpan:s,className:`material-pool-empty`,children:`还没有购料或订单耗用记录,登记第一笔购料后自动建立当天料价。`})})]})]})}),(0,L.jsx)(`p`,{className:`small muted material-pool-footnote`,children:a?`历史采购保留日期、克重、金额与真实付款人;当前可用材料统一查看上方卡片。今日购料只重算今日订单,历史录错需店主复核后更正。`:`同一天按购料录入先后,先扣完一笔料款,再扣下一笔;一单耗用多笔购料时,会按实际付款人分别显示。加权料价仅供查看,订单按总价与总克重精确计算到分;同日补购会自动更新关联订单成本。已退货并回到自有库存的克重会恢复到原购料日期的可用量。`})]})]})}function G(e){let t;try{t=JSON.parse(localStorage.getItem(i(e.user.id))||`null`)}catch{t=null}let n=C(t||{date:e.today,payer:f.currentMember(e)});return{...n,date:n.date||e.today}}function K({state:e,value:t,create:n=!1,minDate:r,children:i}){return(0,L.jsx)(`div`,{className:`material-purchase-scroll`,children:(0,L.jsxs)(`div`,{className:`material-purchase-fields`,children:[(0,L.jsxs)(`label`,{children:[`购料日期`,(0,L.jsx)(A,{name:`date`,defaultValue:t.date,type:`date`,required:!0,min:r,max:e.today})]}),(0,L.jsxs)(`label`,{children:[`购买克重(g)`,(0,L.jsx)(A,{name:`grams`,defaultValue:t.grams,type:`number`,required:!0,min:`0.001`,step:`0.001`,inputMode:`decimal`,placeholder:n?`填实际购入克重`:void 0})]}),(0,L.jsxs)(`label`,{children:[`购买总价(元)`,(0,L.jsx)(A,{name:`total`,defaultValue:t.total,type:`number`,min:`0`,step:`0.01`,required:!0,inputMode:`decimal`,placeholder:n?`填实际支付总额`:void 0})]}),(0,L.jsxs)(`label`,{children:[`付款人`,(0,L.jsx)(k,{name:`payer`,defaultValue:t.payer,children:f.memberOptions(e,!1,{selected:t.payer}).map(([e,t])=>(0,L.jsx)(`option`,{value:e,children:t},e))})]}),(0,L.jsxs)(`label`,{children:[`备注(选填)`,(0,L.jsx)(A,{name:`note`,defaultValue:t.note,type:`text`,maxLength:150,placeholder:n?`例如:上午第一笔`:void 0})]}),i]})})}function te({state:e,rolling:t,save:n,blocked:r}){let[a]=(0,I.useState)(()=>G(e)),[o,s]=(0,I.useState)(``);function c(t){localStorage.setItem(i(e.user.id),JSON.stringify(C(Object.fromEntries(new FormData(t)))))}async function l(r){r.preventDefault(),s(``);try{let i=y(Object.fromEntries(new FormData(r.currentTarget)),e);if(t&&i.date!==e.today)throw Error(`跨日结转已启用,新购料请登记今日。历史记录录错请从对应购料提交更正申请。`);await n(`/api/material-purchases`,i)}catch(e){s(e.message)}}return(0,L.jsxs)(`section`,{className:`panel material-purchase-panel`,children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsx)(`h2`,{children:`登记一笔购料`}),(0,L.jsx)(`span`,{className:`small muted`,children:t?`结转余料、今日购料与回料统一加权`:`同一天的多笔购料自动合并加权`})]}),(0,L.jsxs)(`form`,{id:`material-purchase-form`,onInput:e=>c(e.currentTarget),onChange:e=>c(e.currentTarget),onSubmit:l,children:[(0,L.jsx)(K,{state:e,value:a,create:!0,children:(0,L.jsx)(F,{type:`submit`,className:`primary`,disabled:r,children:`保存购料`})}),(0,L.jsx)(`div`,{id:`material-purchase-error`,"aria-live":`polite`,children:o&&(0,L.jsx)(`div`,{className:`error`,children:o})})]}),(0,L.jsx)(`p`,{className:`small muted material-scroll-hint`,children:`左右滑动填写完整购料信息 →`}),(0,L.jsx)(`p`,{className:`small muted material-purchase-hint`,children:`只登记购料与实际付款人,付款当天记支出,未用完的料结转留用。关联订单按实际耗用自动计算料费及与付款人的往来,销售耗用不再重复记现金支出。实物入库仍在「库存」登记。`})]})}function q({state:e,purchase:t,kind:n,rolling:r,actions:i,refresh:a}){let{blocked:o,stale:s,error:c,close:l,submit:u,save:d}=i;if(t.deleted_at)return(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`p`,{children:`这笔购料已删除,原记录与操作历史保留。`}),(0,L.jsx)(F,{className:`primary`,type:`button`,"data-action":`refresh-changed-material-purchase`,onClick:a,children:`关闭并刷新每日购料`})]});if(t.recovery_id)return(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`p`,{children:`这笔购料随回收/置换原单保留,不能在购料页独立修改或删除。`}),(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`close-material-purchase`,onClick:l,children:`关闭`})]});if(t.canEdit===!1&&t.canRequestChange===!1)return(0,L.jsx)(`p`,{children:`这笔共享购料不能由当前账号更正。请由登记人或店主核对处理。`});let p=S(t,r,e.today),m=n===`edit`,g=m?`修改`:`删除`;async function _(i){i.preventDefault();let a=Object.fromEntries(new FormData(i.currentTarget));await u(async()=>{let i=m?{...y(a,e),revision:t.revision}:{revision:t.revision};if(r&&t.date===e.today&&i.date&&i.date!==e.today)throw Error(`今日购料不能直接改为已结转的历史日期。请核对所选日期与原购料凭据。`);if(p){let e=String(a.change_reason||``).trim();if(!e||e.length>200)throw Error(`请填写更正原因,最多200字。`);if(a.change_verified!==`on`)throw Error(`请核对原始购料凭据与本次申请值,再勾选确认。`);Object.assign(i,{action:n,reason:e,verified:!0})}await d(`/api/material-purchases/`+encodeURIComponent(t.id)+`/`+(p?`change`:n),i)})}return(0,L.jsxs)(L.Fragment,{children:[!m&&(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`dl`,{className:`material-purchase-facts`,children:[[`购料日期`,t.date],[`购买克重`,h(t.mg)+`g`],[`购买总价`,D(t.amount)],[`付款人`,f.memberName(e,T(t))]].map(([e,t])=>(0,L.jsxs)(`div`,{children:[(0,L.jsx)(`dt`,{children:e}),(0,L.jsx)(`dd`,{children:t})]},e))}),t.note&&(0,L.jsxs)(`p`,{className:`small muted material-purchase-original-note`,children:[`备注:`,t.note]}),(0,L.jsx)(`p`,{className:`small material-purchase-impact`,children:p?`本次只提交删除申请,待店主核对订单影响并批准后才更新账务。等待期间仍按原购料记账。`:`删除后,这笔购料不再计入实际购料与成本总额,相关订单的料费、利润和成员往来会重新核算。购料不足时,订单会显示“料费待补”;客户售价、收付款和实物库存保持原值。`}),(0,L.jsx)(`p`,{className:`small muted`,children:`原记录与操作历史会保留。`})]}),(0,L.jsxs)(`form`,{id:`material-purchase-`+n+`-form`,"data-id":t.id,"data-revision":t.revision,"data-kind":n,"data-stale":s||void 0,onSubmit:_,children:[(0,L.jsxs)(`fieldset`,{className:`material-form-controls`,style:{display:`contents`},disabled:s,children:[m&&(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(K,{state:e,value:{date:t.date,total:(t.amount/100).toFixed(2),grams:h(t.mg),payer:T(t),note:t.note},minDate:r&&!p?e.today:void 0}),(0,L.jsx)(`p`,{className:`small muted material-scroll-hint`,children:`左右滑动填写完整购料信息 →`}),(0,L.jsx)(`p`,{className:`small muted material-purchase-impact`,children:p?`本次只提交修改申请。店主核对原值、申请值和关联订单影响并批准后,才重新核算;待批期间仍按原记录记账。`:r?`保存今日购料后,只重新核算今日料价与订单。已结转的历史日期请从对应历史购料提交更正申请。`:`保存后重算料价、关联订单的料费、利润与结算。改日期后,订单耗用仍留在原料价日期;购料不足时会显示“料费待补”。`})]}),(p||r)&&(0,L.jsxs)(`div`,{className:`material-purchase-review-fields`,hidden:!p,children:[(0,L.jsxs)(`label`,{children:[`更正原因`,(0,L.jsx)(P,{name:`change_reason`,maxLength:200,rows:2,required:p,placeholder:`写清原记录哪里录错,以及核对依据。`})]}),(0,L.jsxs)(`label`,{className:`check`,children:[(0,L.jsx)(A,{name:`change_verified`,type:`checkbox`,required:p}),`已核对原始购料凭据与本次申请值,提交店主复核后再更正。`]})]})]}),(0,L.jsx)(`div`,{id:`material-purchase-change-error`,"aria-live":`polite`,children:c}),(0,L.jsxs)(`div`,{className:`form-actions`,children:[(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`close-material-purchase`,onClick:l,children:`取消`}),(0,L.jsx)(F,{type:`submit`,className:m?`primary`:`danger`,disabled:o||s,children:p?`提交`+g+`申请`:m?`保存修改`:`确认删除购料`})]})]})]})}function J({state:e,pool:t,actions:n}){let{close:r,blocked:i,save:a,submit:o,error:s}=n,c=t.settlement.settlementDue,l=f.memberName(e,`owner`),u=f.memberName(e,`assistant`);function d(e){e.preventDefault();let n=Object.fromEntries(new FormData(e.currentTarget));o(async()=>{if(n.verified!==`on`)throw Error(`请先核实双方实际转账。`);if(p.cents(n.amount,`实际转账金额`)<=0)throw Error(`实际转账金额须大于0。`);await a(`/api/material-pools/`+encodeURIComponent(t.id)+`/settlement`,{date:n.date,amount:n.amount,direction:n.direction,note:n.note||``,verified:!0,revision:String(t.revision)})})}return(0,L.jsxs)(L.Fragment,{children:[(0,L.jsxs)(`p`,{className:`small muted`,children:[c<0?l+`待转回`+u:u+`待交`+l,` ¥`,m(Math.abs(c)),`。只登记双方实际转账,不会发起付款。`]}),(0,L.jsxs)(`form`,{id:`material-settlement-form`,"data-pool-id":t.id,"data-revision":t.revision,onSubmit:d,children:[(0,L.jsxs)(`div`,{className:`grid three`,children:[(0,L.jsxs)(`label`,{children:[`业务日期`,(0,L.jsx)(A,{name:`date`,defaultValue:e.today,type:`date`,max:e.today,required:!0})]}),(0,L.jsxs)(`label`,{children:[`转款方向`,(0,L.jsxs)(k,{name:`direction`,defaultValue:c<0?`toAssistant`:`toOwner`,children:[(0,L.jsxs)(`option`,{value:`toOwner`,children:[u,` → `,l]}),(0,L.jsxs)(`option`,{value:`toAssistant`,children:[l,` → `,u]})]})]}),(0,L.jsxs)(`label`,{children:[`实际转账金额(元)`,(0,L.jsx)(A,{name:`amount`,defaultValue:(Math.abs(c)/100).toFixed(2),type:`number`,min:`0`,step:`0.01`,required:!0})]})]}),(0,L.jsxs)(`label`,{children:[`凭据/备注(选填)`,(0,L.jsx)(A,{name:`note`,type:`text`,maxLength:150})]}),(0,L.jsxs)(`label`,{className:`check`,children:[(0,L.jsx)(A,{name:`verified`,type:`checkbox`,required:!0}),`已核实双方实际转账。`]}),(0,L.jsx)(`div`,{id:`material-settlement-error`,"aria-live":`polite`,children:s}),(0,L.jsxs)(`div`,{className:`form-actions`,children:[(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`close-material-settlement`,onClick:r,children:`取消`}),(0,L.jsx)(F,{type:`submit`,className:`primary`,disabled:i,children:`保存实际交款`})]})]})]})}function Y(e){let t=new Date(e||``);return Number.isNaN(t.getTime())?`时间未提供`:t.toLocaleString(`zh-CN`)}var X=new WeakMap;function ne({client:e,state:t,refresh:n,open:r}){let[i,a]=(0,I.useState)(()=>X.get(e)?.user===t.user.id?X.get(e).status:`pending`),[o,s]=(0,I.useState)(0),[c,l]=(0,I.useState)(null),[u,d]=(0,I.useState)(``),[f,p]=(0,I.useState)(``),m=[i,o,n].join(`/`),g=u!==m;return(0,I.useEffect)(()=>{let t=!0;return e.api(`/api/material-purchase-changes?`+new URLSearchParams({status:i})).then(x).then(e=>{t&&(l(e),p(``))}).catch(e=>{t&&p(e.message)}).finally(()=>{t&&d(m)}),()=>{t=!1}},[e,t.user.id,i,m]),(0,L.jsxs)(`section`,{className:`panel material-change-panel`,children:[(0,L.jsxs)(`div`,{className:`panel-title`,children:[(0,L.jsx)(`h2`,{children:t.user.role===`owner`?`历史购料更正复核`:`我的历史购料更正`}),(0,L.jsx)(F,{type:`button`,"data-action":`material-change-refresh`,className:`quiet`,onClick:()=>s(o+1),children:`刷新更正申请`})]}),(0,L.jsx)(`p`,{className:`small muted`,children:`历史购料修改或删除先提交申请,店主查看料价和订单影响后批准;待批期间仍按原值记账。`}),(0,L.jsx)(`div`,{id:`material-changes-list`,"aria-live":`polite`,"aria-busy":g,children:f?(0,L.jsxs)(`div`,{className:`error`,children:[f,` `,(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`material-change-refresh`,onClick:()=>s(o+1),children:`重新读取申请`})]}):c?(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`div`,{className:`material-change-tabs`,role:`group`,"aria-label":`购料更正状态`,children:[`pending`,`approved`,`rejected`,`all`].map((n,r)=>(0,L.jsxs)(F,{type:`button`,"data-action":`material-change-filter`,"data-status":n,"aria-pressed":n===i,className:n===i?`primary`:`quiet`,onClick:()=>{X.set(e,{user:t.user.id,status:n}),a(n),s(o+1)},children:[[`待复核`,`已批准`,`已驳回`,`全部`][r],n!==`all`&&`(`+c.counts[n]+`)`]},n))}),c.rows.length?(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsx)(j,{className:`material-change-table`,columns:[{title:`更正内容`},{title:`申请人/原因`},{title:`状态/处理意见`},{title:`操作`}],rows:c.rows.map(e=>({key:e.id,cells:[(0,L.jsxs)(L.Fragment,{children:[e.action===`delete`?`删除历史购料`:`修改历史购料`,e.preview?.before&&(0,L.jsxs)(`div`,{className:`small muted`,children:[e.preview.before.date,` · `,h(e.preview.before.mg),`g · `,D(e.preview.before.amount)]})]}),(0,L.jsxs)(L.Fragment,{children:[e.requester_name,(0,L.jsx)(`div`,{className:`small muted`,children:Y(e.created)}),(0,L.jsx)(`div`,{className:`material-change-text`,children:e.reason})]}),(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`span`,{className:`badge `+(e.status===`pending`?`warn`:``),children:E[e.status]}),e.stale&&e.status===`pending`&&(0,L.jsx)(`div`,{className:`small danger-text`,children:`原记录已变化,需重新核对。`}),e.note&&(0,L.jsx)(`div`,{className:`small material-change-text`,children:e.note}),e.reviewer_name&&(0,L.jsxs)(`div`,{className:`small muted`,children:[e.reviewer_name,` · `,Y(e.reviewed)]})]}),(0,L.jsx)(L.Fragment,{children:(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`material-change-open`,"data-id":e.id,onClick:()=>r(e.id),children:t.user.role===`owner`&&e.status===`pending`?`查看影响并复核`:`查看申请`})})]}))})}):(0,L.jsx)(`p`,{className:`small muted material-rolling-empty`,children:`没有此状态的更正申请。`})]}):`正在读取更正申请…`})]})}function Z({label:e,head:t,rows:n,highlight:r=!0}){return(0,L.jsx)(`div`,{className:`table-wrap`,children:(0,L.jsx)(j,{className:`material-detail-table material-change-comparison`,"aria-label":e,columns:[{title:`核对项目`,rowScope:`row`},{title:t[0]},{title:t[1]}],rows:n.map(([e,t,n])=>({key:e,attributes:{className:r&&t!==n?`material-change-different`:void 0},cells:[e,t,n]}))})})}function Q({preview:e,state:t}){let n=[[`金料及货品成本`,`goodsCost`],[`利润`,`profit`],[`待结净额`,`settlementDue`],[f.memberName(t,`owner`)+`垫资成本`,`ownerCost`],[`订单成交金额`,`total`],[`客户收款净额`,`netReceived`]],r=e=>e==null?`待补齐`:D(e),i=e=>e.allocations.length?e.allocations.map(e=>f.memberName(t,e.payer)+` `+r(e.amount)).join(`;`):e.materialAmount===null?`待补齐后分摊`:`无分摊金额`;return(0,L.jsxs)(`section`,{className:`material-change-impact`,children:[(0,L.jsx)(`h3`,{children:`服务器核算的订单影响`}),(0,L.jsxs)(`p`,{className:`small muted`,children:[e.summary&&(0,L.jsxs)(L.Fragment,{children:[`受影响 `,e.summary.affectedOrderCount,` 单。`,Object.hasOwn(e.summary,`goodsDelta`)&&Object.hasOwn(e.summary,`profitDelta`)&&(0,L.jsxs)(L.Fragment,{children:[`金料成本合计变化 `,D(e.summary.goodsDelta),`,利润合计变化 `,D(e.summary.profitDelta),`。`]})]}),!!e.affectedDates?.length&&`涉及日期:`+e.affectedDates.join(`、`)+`。`]}),e.relatedOrders?.length?e.relatedOrders.map(e=>(0,L.jsxs)(I.Fragment,{children:[(0,L.jsxs)(`h3`,{children:[e.date,` · `,e.number||e.id]}),(0,L.jsx)(Z,{label:`关联订单成本影响`,head:[`变更前`,`批准后`],rows:n.map(([t,n])=>[t,D(e.before[n]),D(e.after[n])])})]},e.id)):(0,L.jsx)(`p`,{className:`small muted`,children:e.summary?.affectedOrderCount?`当前账号没有可查看的关联订单金额明细。`:`本次没有订单金额变化。`}),!!e.relatedPriceLocks?.length&&(0,L.jsxs)(`section`,{className:`price-lock-related-impact`,children:[(0,L.jsxs)(`h3`,{children:[`同时受影响的锁价单(`,e.relatedPriceLocks.length,` 张)`]}),(0,L.jsx)(`p`,{className:`small muted`,children:`以下只调整料池成本和付款归属,客户锁定售价与实际收付款保持原记录。`}),e.relatedPriceLocks.map(e=>{let t=[[`原锁预留料费`,r(e.before.materialAmount),r(e.after.materialAmount)]];for(let[n,i]of[[`usedMaterialAmount`,`交付耗用原锁料费`],[`leftoverCost`,`少重回池成本`]])(Object.hasOwn(e.before,n)||Object.hasOwn(e.after,n))&&t.push([i,...[e.before,e.after].map(e=>Object.hasOwn(e,n)?r(e[n]):`未交付`)]);return t.push([`自动付款归属`,i(e.before),i(e.after)]),(0,L.jsxs)(I.Fragment,{children:[(0,L.jsxs)(`h3`,{children:[e.date,` · `,e.number||e.id,e.customer&&` · `+e.customer]}),(0,L.jsx)(Z,{label:`关联锁价单成本影响`,head:[`变更前`,`批准后`],rows:t,highlight:!1})]},e.id)})]})]})}function re({p:e,state:t,actions:n,reload:r}){let{error:i,close:a,blocked:o,stale:s,submit:c,save:l}=n;return(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`dl`,{className:`material-rolling-facts`,children:[[`切换起始日`,e.date],[`结转克重`,h(e.opening.mg)+`g`],[`结转金料成本`,D(e.opening.amount)],[`今日将重新核算的订单`,e.affectedOrderCount+` 单`]].map(([e,t])=>(0,L.jsxs)(`div`,{children:[(0,L.jsx)(`dt`,{children:e}),(0,L.jsx)(`dd`,{children:t})]},e))}),e.relatedOrders&&(0,L.jsx)(Q,{preview:e,state:t}),(0,L.jsx)(`p`,{className:`small muted`,children:`启用后,结转材料、今日多笔购料和今日退货回料统一加权。今日已有订单按切换后的料价重新核算;旧日订单冻结。真实采购付款与实物库存按原记录保留。`}),e.canEnable?(0,L.jsxs)(`form`,{id:`material-rolling-enable-form`,"data-revision":e.revision,"data-stale":s||void 0,onSubmit:n=>{n.preventDefault();let r=new FormData(n.currentTarget).get(`verified`)===`on`;c(async()=>{if(!r)throw Error(`请先核对结转材料与今日订单影响,再勾选确认。`);if(t.user.role!==`owner`||!e.canEnable)throw Error(`请由店主账号核对后启用跨日结转。`);await l(`/api/material-pools/rolling-enable`,{date:e.date,revision:e.revision,verified:!0})})},children:[(0,L.jsxs)(`label`,{className:`check`,children:[(0,L.jsx)(A,{name:`verified`,type:`checkbox`,required:!0,disabled:o||s}),`已核对结转克重、金料成本及今日订单影响,确认从 `,e.date,` 起启用。`]}),(0,L.jsx)(`div`,{id:`material-rolling-error`,"aria-live":`polite`,children:i}),(0,L.jsxs)(`div`,{className:`form-actions`,children:[(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":`material-rolling-close`,onClick:a,children:`取消`}),(0,L.jsx)(F,{type:`submit`,className:`primary`,disabled:s||o,children:`确认启用跨日结转`})]})]}):(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`p`,{className:`error`,children:e.reason||`暂不能启用,请先核对材料记录。`}),(0,L.jsx)(F,{type:`button`,"data-action":`material-rolling-enable-preview`,className:`quiet`,onClick:r,children:`重新核对`})]})]})}function ie({r:e,state:t,actions:n}){let{blocked:r,stale:i,error:a,close:o,submit:s,save:c}=n,l=e.status===`pending`,u=t.user.role===`owner`,d=l&&u&&e.canApprove&&!e.stale;function p(t,n){let r=new FormData(t),i=String(r.get(`note`)||``).trim();s(async()=>{if(!u||!l)throw Error(`请由店主账号重新读取申请后复核。`);if(i.length>200||n===`reject`&&!i)throw Error(n===`reject`?`请填写驳回意见,最多200字。`:`处理意见最多200字。`);if(n===`approve`&&!d)throw Error(`原购料或关联资料已变化,请重新读取申请后复核。`);if(n===`approve`&&r.get(`verified`)!==`on`)throw Error(`请核对购料凭据与关联订单影响,再勾选确认。`);await c(`/api/material-purchase-changes/`+encodeURIComponent(e.id)+`/`+n,n===`approve`?{revision:e.purchase_revision,verified:!0,note:i}:{note:i})})}let m=[[`购料日期`,e=>e.date],[`购买克重`,e=>h(e.mg)+`g`],[`购买总价`,e=>D(e.amount)],[`实际付款人`,e=>f.memberName(t,e.payer)],[`备注`,e=>e.note||`未填写`]],g=(0,L.jsx)(F,{type:`button`,"data-action":`material-rolling-close`,className:`quiet`,onClick:o,children:`关闭`});return(0,L.jsxs)(L.Fragment,{children:[(0,L.jsxs)(`p`,{className:`small muted`,children:[`申请人:`,e.requester_name,` · `,Y(e.created)]}),(0,L.jsxs)(`p`,{className:`material-change-text`,children:[`申请原因:`,e.reason]}),(0,L.jsx)(Z,{label:`购料原值与申请值`,head:[`原购料记录`,e.preview.after?`批准后记录`:`批准后删除`],rows:m.map(([t,n])=>[t,n(e.preview.before),e.preview.after?n(e.preview.after):`删除此笔购料`])}),(0,L.jsx)(Q,{preview:e.preview,state:t}),l&&(0,L.jsx)(`p`,{className:`note`,children:`申请尚未生效。批准后才更正购料、材料成本及上述订单,原始记录与复核记录保留。`}),e.stale&&l&&(0,L.jsx)(`p`,{className:`error`,children:`购料或关联资料在申请后已有变化,这份申请不能批准。请驳回后按最新记录重新申请。`}),e.note&&(0,L.jsxs)(`p`,{className:`material-change-text`,children:[`处理意见:`,e.note]}),e.reviewer_name&&(0,L.jsxs)(`p`,{className:`small muted`,children:[e.reviewer_name,` · `,Y(e.reviewed)]}),u&&l?(0,L.jsxs)(`form`,{id:`material-change-review-form`,className:`material-change-review-form`,"data-id":e.id,"data-revision":e.purchase_revision,"data-stale":i||void 0,onSubmit:e=>{e.preventDefault(),p(e.currentTarget,`approve`)},children:[(0,L.jsxs)(`label`,{children:[`处理意见(批准时选填,驳回时必填)`,(0,L.jsx)(P,{name:`note`,maxLength:200,rows:2,disabled:r||i})]}),d&&(0,L.jsxs)(`label`,{className:`check`,children:[(0,L.jsx)(A,{type:`checkbox`,name:`verified`,required:!0,disabled:r||i}),`已核对原购料凭据、申请值与关联订单影响,确认批准本次更正。`]}),(0,L.jsx)(`div`,{id:`material-rolling-error`,"aria-live":`polite`,children:a}),(0,L.jsxs)(`div`,{className:`form-actions`,children:[g,(0,L.jsx)(F,{type:`button`,className:`danger`,"data-action":`material-change-reject`,disabled:r||i,onClick:e=>p(e.currentTarget.form,`reject`),children:`驳回申请`}),d&&(0,L.jsx)(F,{type:`submit`,className:`primary`,disabled:r||i,children:`批准购料更正`})]})]}):g]})}function ae({spec:e,client:t,state:n,rolling:r,save:i,close:a,refresh:o,retry:s,reload:c}){let[l,u]=(0,I.useState)(null),[d,f]=(0,I.useState)(``),[p,m]=(0,I.useState)(null);(0,I.useEffect)(()=>{let r=!0;async function i(){switch(e.kind){case`edit`:case`delete`:return{kind:e.kind,p:w(await t.api(`/api/material-purchases/`+encodeURIComponent(e.id)),e.id,n)};case`enable`:return{kind:`enable`,preview:O(await t.api(`/api/material-pools/rolling-preview`),n)};case`review`:return{kind:`review`,change:_(await t.api(`/api/material-purchase-changes/`+encodeURIComponent(e.id)),e.id,n)};case`settlement`:return null}}return i().then(e=>{r&&u(e)}).catch(e=>{r&&f(e.message)}),()=>{r=!1}},[t,e,n]);let h=e.kind===`edit`||e.kind===`delete`,g=e.kind===`settlement`,v=h?`material-purchase`:g?`material-settlement`:`material-rolling`,y=!!t.pending(),b=t.busy||y,x=!!p&&!g&&(y||(h?p.status===409:[403,404,409].includes(p.status??0))),S=(0,L.jsx)(F,{type:`button`,className:`quiet`,"data-action":h?`reload-material-purchase`:e.kind===`enable`?`material-rolling-enable-preview`:`material-change-open`,"data-id":`id`in e?e.id:void 0,"data-kind":h?e.kind:void 0,onClick:c,children:h?`重新读取这笔购料`:e.kind===`enable`?`重新核对`:`重新读取申请`}),C={blocked:b,stale:x,close:a,save:i,error:p&&(0,L.jsxs)(`div`,{className:`error`,children:[p.message,` `,y?(0,L.jsx)(F,{type:`button`,className:`danger`,"data-action":`retry`,disabled:t.busy,onClick:()=>void s().catch(m),children:`恢复原操作`}):(x||!h&&!g)&&S]}),submit:async e=>{if(!(b||x)){m(null);try{await e()}catch(e){m(e)}}}},T=h?e.kind===`edit`?`修改这笔购料`:`删除这笔购料`:e.kind===`settlement`?e.pool.date+` · 本批金料实际交款`:e.kind===`enable`?d?`跨日结转预览未读到`:l?`核对并启用跨日结转`:`核对跨日结转`:l?.kind===`review`?(l.change.action===`delete`?`删除`:`修改`)+`历史购料 · `+E[l.change.status]:d?`更正申请未读到`:`读取购料更正申请`;return(0,L.jsxs)(N,{id:v+`-dialog`,"aria-labelledby":v+`-title`,onCancel:e=>{e.preventDefault(),a()},children:[(0,L.jsx)(F,{type:`button`,className:`dialog-close`,"data-action":h?`close-material-purchase`:g?`close-material-settlement`:`material-rolling-close`,"aria-label":h?`关闭购料`+(e.kind===`delete`?`删除`:`修改`):g?`关闭本批料款结算`:`关闭材料复核`,onClick:a,children:`×`}),(0,L.jsx)(`h2`,{id:v+`-title`,children:T}),d?(0,L.jsxs)(`div`,{className:`error`,children:[d,` `,S]}):e.kind===`settlement`?(0,L.jsx)(J,{pool:e.pool,state:n,actions:C}):l?.kind===`edit`||l?.kind===`delete`?(0,L.jsx)(q,{state:n,purchase:l.p,kind:l.kind,rolling:r,actions:C,refresh:o}):l?.kind===`enable`?(0,L.jsx)(re,{p:l.preview,state:n,actions:C,reload:c}):l?.kind===`review`?(0,L.jsx)(ie,{r:l.change,state:n,actions:C}):(0,L.jsx)(`p`,{className:`small muted`,"aria-live":`polite`,children:h?`正在读取这笔购料…`:e.kind===`enable`?`正在核对结转材料与今日订单…`:`正在读取原购料记录及订单影响…`})]})}var $=new WeakMap;function oe({client:e,state:o,onState:l,onRecoverOther:d}){c(e),u();let f=s(`pool`),[p,m]=(0,I.useState)(()=>f||($.get(e)?.user===o.user.id?$.get(e).date:``)),[h,g]=(0,I.useState)(null),[_,y]=(0,I.useState)(0),[b,x]=(0,I.useState)(null),[S,C]=(0,I.useState)(``),[w,T]=(0,I.useState)(``),[E,D]=(0,I.useState)(``),[O,k]=(0,I.useState)(!1),[A,j]=(0,I.useState)(0),[M,N]=(0,I.useState)(0);function P(t){$.set(e,{user:o.user.id,date:t}),m(t)}let R=(0,I.useRef)(0),z=(0,I.useRef)(!1),B=(0,I.useCallback)(async()=>{let t=++R.current;try{let n=v(await e.api(`/api/material-pools`));z.current&&t===R.current&&(g(n),C(``),y(e=>e+1),D(``),k(!1))}catch(e){throw z.current&&t===R.current&&C(e.message),e}},[e]);(0,I.useEffect)(()=>(z.current=!0,B().catch(()=>{}),()=>{z.current=!1}),[B,o.user.id]);let[V,H]=(0,I.useState)(f);f!==V&&(H(f),m(f),x(null));async function U(){x(null),C(``);try{await B()}catch{}}async function G(t,n,r){let i=t===`/api/material-purchases`,o=t.split(`/`).at(-1),s=o===`settlement`;if(!z.current)return;i&&j(e=>e+1),(i||o===`edit`||o===`delete`)&&P(r.poolId||r.previousPoolId||String(n.date||``)),s&&P(decodeURIComponent(t.split(`/`)[3]));let c=a(t);D(c),T(``),k(!1),x(null),C(``),++R.current;try{let t=await e.api(`/api/state`);if(!z.current)return;l?.(t),await B(),z.current&&T(c)}catch{z.current&&k(!0)}}let K=async(t,r)=>{await G(t,r,t===`/api/material-purchases`?await n(e,t,r,{key:i(o.user.id),value:JSON.stringify(r)}):await e.mutate(t,r))};async function q(){let n=e.pending();if(n&&!r.accepts(n.url)&&d)return d();let i=await e.retryPending();i&&t(i.job.data)&&(r.finish({client:e,state:o,storage:localStorage,job:i.job,result:{...i.result}}),await G(i.job.url,i.job.data,i.result))}function J(t){e.pending()||e.busy?T(`请先恢复上次未确认的操作,再核对材料记录。`):(T(``),N(e=>e+1),x(t))}let Y=!!h?.rolling?.enabled,X=!!e.pending();return(0,L.jsx)(`div`,{className:`business-pages`,children:(0,L.jsxs)(`div`,{id:`main`,children:[X&&!e.busy&&(0,L.jsxs)(`div`,{className:`note`,role:`alert`,children:[`上次提交结果尚未确认,请先恢复原操作。`,` `,(0,L.jsx)(F,{type:`button`,"data-action":`retry`,className:`danger`,onClick:()=>void q().catch(e=>T(e.message)),children:`恢复原操作`})]}),E?(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(`div`,{className:`page-heading`,children:(0,L.jsx)(`h1`,{children:E})}),(0,L.jsxs)(`section`,{className:`panel`,children:[(0,L.jsx)(`p`,{id:`material-saved-status`,"aria-live":`polite`,children:O?E+`,最新材料记录暂未读到。请重新读取查看,不要重复提交。`:`正在读取最新材料记录…`}),(0,L.jsxs)(`div`,{className:`actions`,children:[(0,L.jsx)(F,{type:`button`,"data-action":`refresh-material-pools`,className:`primary`,onClick:()=>void U(),children:`重新读取每日料价`}),E===`购料已保存`&&(0,L.jsx)(F,{type:`button`,"data-action":`new-material-purchase`,onClick:()=>{D(``),U()},children:`继续登记另一笔购料`})]})]})]}):(0,L.jsxs)(L.Fragment,{children:[(0,L.jsxs)(`div`,{className:`page-heading material-heading`,children:[(0,L.jsxs)(`div`,{children:[(0,L.jsx)(`div`,{className:`eyebrow`,children:`DAILY MATERIAL`}),(0,L.jsx)(`h1`,{children:`每日购料`}),(0,L.jsx)(`p`,{children:`购料付款当天记支出,余料结转;订单按实际耗用自动计料费。`})]}),(0,L.jsx)(F,{type:`button`,"data-action":`refresh-material-pools`,onClick:()=>void U(),children:`刷新`})]}),w&&(0,L.jsx)(`div`,{role:`status`,className:`note`,children:w}),Y&&(0,L.jsx)(ee,{pool:h.current,cutover:h.rolling.cutover}),(0,L.jsx)(te,{state:o,rolling:Y,save:K,blocked:e.busy||X},A),(0,L.jsx)(`div`,{id:`material-pools-content`,"aria-live":`polite`,children:S?(0,L.jsxs)(`div`,{className:`error`,children:[S,` `,(0,L.jsx)(F,{type:`button`,"data-action":`refresh-material-pools`,onClick:()=>void U(),children:`重新读取`})]}):h?(0,L.jsxs)(L.Fragment,{children:[(0,L.jsx)(W,{state:o,data:h,openDate:p,toggle:P,openDialog:J}),Y&&(0,L.jsx)(ne,{state:o,client:e,refresh:_,open:e=>J({kind:`review`,id:e})})]}):`正在读取每日料价…`})]}),b&&(0,L.jsx)(ae,{spec:b,client:e,state:o,rolling:Y,save:K,close:()=>x(null),refresh:()=>void U(),reload:()=>N(e=>e+1),retry:q},M)]})})}export{oe as MaterialPoolsPage};