1 line
16 KiB
JavaScript
1 line
16 KiB
JavaScript
import{n as e}from"./rolldown-runtime-Dl_DZoUm.js";import{n as t,t as n,useRead as r}from"./OrdersPage-DMGBXOU2.js";import{i,t as a}from"./react-1o5M9n0k.js";import{o}from"./shared-DZLPF0cC.js";import{l as s,s as c}from"./index-D-ox0wtk.js";import{t as l}from"./useOrderForm-Boa1k5nR.js";import{i as u,n as d,r as f,t as p}from"./EditFields-DxbwVQgn.js";var m=e(i(),1),h=a();function g(e,t){let n;try{n=JSON.parse(localStorage.getItem(e))}catch{return null}return n?.id===t&&Number.isSafeInteger(n.revision)&&Array.isArray(n.inputs?.lines)?n:null}function _(e,t,n,r){if(e.eligible!==!0)throw Error(e.reason||`这张订单暂不能修改,请重新读取订单后核对。`);if(e.order?.id!==t||!Number.isSafeInteger(e.revision)||!Array.isArray(e.inputs?.lines)||!Array.isArray(e.inputs.fee_items))throw Error(`订单修改资料未完整返回,请重新读取。`);if(e.financialEditable&&e.order.price_lock_id){let t=e.priceLock,n=e.inputs.lines,r=n.length>1?t?.lines:[t],i=new Set;if(!t||t.id!==e.order.price_lock_id||!/^\d{4}-\d{2}-\d{2}$/.test(t.date)||typeof t.financeRequiresApproval!=`boolean`||!Array.isArray(r)||!n.length||r.length!==n.length)throw Error(`锁价日期与成本修改资料未完整返回,请重新读取。`);for(let[t,a]of n.entries()){let o=r[t],s=o?.lineId||o?.id,c=a.price_lock_material||n.length===1&&e.inputs.price_lock_material,l=a.price_lock_extra_material||n.length===1&&e.inputs.price_lock_extra_material;if(![`pool`,`manual`].includes(o?.materialSource)||!Number.isSafeInteger(o.extraMg)||o.extraMg<0||![`pool`,`manual`].includes(c?.source)||o.extraMg>0&&!l||n.length>1&&(!a.lineId||a.lineId!==s||i.has(s)))throw Error(`锁价日期与成本修改资料未完整返回,请重新读取。`);i.add(s)}}let i=n?.revision??e.revision,a=n?.inputs||e.inputs,o=e.order;return{...e,id:t,order:o,originalInputs:e.inputs,user:r.user.id,revision:i,priceLock:e.priceLock||null,sameDayDirectEdit:e.requiresApproval===!1,inputs:{...a,...e.inputs.price_lock_material?{price_lock_material:a.price_lock_material||e.inputs.price_lock_material}:{},...e.inputs.price_lock_extra_material?{price_lock_extra_material:a.price_lock_extra_material||e.inputs.price_lock_extra_material}:{},internal_note:a.internal_note??e.inputs.internal_note??``,id:t,revision:i,paper_number:o.paper_number||``,paper_date:o.paper_date||``,fee_auto_initialized:!0},reason:n?.reason||``,verified:!!n?.verified&&i===e.revision,conflicted:i!==e.revision}}function v({entry:e,read:t,disabled:n}){let r=e.latest?.order;return(0,h.jsxs)(`section`,{className:`panel`,id:`order-edit-conflict`,role:`status`,children:[(0,h.jsx)(`h2`,{children:`订单已有新记录,请先核对`}),(0,h.jsx)(`p`,{children:`你正在填写的内容已保留。核对下方当前订单与表单后,再决定继续使用哪份内容。`}),r?(0,h.jsxs)(`details`,{open:!0,children:[(0,h.jsx)(`summary`,{children:`当前已保存的订单`}),(0,h.jsxs)(`p`,{children:[r.date,` · `,r.customer||`未填客户称呼`,` · 开单人 `,r.salesperson,` · `,r.payment_account,` · 成交 `,o.orderAmount(r.total,r.rounding_mode)]}),(0,h.jsxs)(`table`,{children:[(0,h.jsx)(`thead`,{children:(0,h.jsx)(`tr`,{children:[`商品`,`克重/数量`,`成交金额`,`货品成本`].map(e=>(0,h.jsx)(`th`,{children:e},e))})}),(0,h.jsx)(`tbody`,{children:r.lines.map((e,t)=>(0,h.jsxs)(`tr`,{children:[(0,h.jsx)(`td`,{children:e.name}),(0,h.jsx)(`td`,{children:e.unit===`g`?o.grams(e.quantity)+`g`:e.quantity+`件`}),(0,h.jsxs)(`td`,{children:[`¥`,o.yuan(e.sale)]}),(0,h.jsx)(`td`,{children:e.cost==null?`待补`:`¥`+o.yuan(e.cost)})]},t))})]}),(0,h.jsxs)(`p`,{children:[`实际费用 ¥`,o.yuan(r.directCost),`;客户单备注:`,r.note||`无`]}),r.internal_note&&(0,h.jsxs)(`p`,{children:[`内部备注:`,r.internal_note]})]}):(0,h.jsx)(`p`,{children:`重新读取最新订单后,核对原记录再继续。`}),(0,h.jsxs)(`div`,{className:`actions`,children:[r?(0,h.jsx)(`button`,{disabled:n,"data-action":`keep-order-edit`,onClick:()=>t(`keep`),children:`已核对,保留草稿继续修改`}):(0,h.jsx)(`button`,{disabled:n,"data-action":`reload-order-edit`,onClick:()=>t(`review`),children:`读取最新订单供核对`}),(0,h.jsx)(`button`,{disabled:n,"data-action":`reset-order-edit`,onClick:()=>t(`reset`),children:`改用当前订单内容`})]})]})}function y({client:e,state:r,session:i,refreshState:a,registerLeaveGuard:o,LockEditor:l}){let u=`mm-order-edit-active-`+r.user.id,d=s(`order`)||s(`id`)||localStorage.getItem(u)||``,f=`mm-order-edit-`+r.user.id+`-`+d,[p,y]=(0,m.useState)(null),[x,S]=(0,m.useState)(0),[C,w]=(0,m.useState)(``),[T,E]=(0,m.useState)(!1),[D,O]=(0,m.useState)(!1),k=(0,m.useRef)(!1),A=(0,m.useRef)(0),j=(0,m.useCallback)((t,n,i)=>{k.current&&e.user===r.user.id&&(localStorage.setItem(f,JSON.stringify({id:d,revision:t.revision,inputs:t,reason:n,verified:i})),localStorage.setItem(u,d))},[d,f,u,e,r.user.id]),{checkFailure:M}=t({client:e,state:r,id:d,watch:!!p&&!D,onLost:t=>{k.current=!1,sessionStorage.setItem(n(r.user.id),t),c(`orders`,{replace:!0,fromPage:!0}),a().catch(t=>{e.user===r.user.id&&window.dispatchEvent(new CustomEvent(`order-access-refresh-error`,{detail:t.message}))})}}),N=()=>k.current&&e.user===r.user.id;async function P(t=`open`){if(!d||t!==`open`&&(e.busy||e.pending()||D)||t===`reset`&&!window.confirm(`改用当前已保存的订单内容?这张订单尚未保存的修改草稿将被替换。`))return;let n=++A.current;O(!0);try{let i=await e.api(`/api/orders/`+encodeURIComponent(d)+`/edit`);if(!N()||n!==A.current)return;let a=g(f,d);t===`reset`&&(a=null),t===`keep`&&p?.latest?.revision===i.revision&&a&&(a={...a,verified:!1,revision:i.revision,inputs:{...a.inputs,revision:i.revision}});let o=_(i,d,a,r);(t===`review`||t===`keep`&&i.revision!==p.latest?.revision)&&(o.conflicted=!0,o.verified=!1),o.latest=o.conflicted?i:null,j(o.inputs,o.reason,o.verified),y(o),S(e=>e+1),w(``)}catch(e){N()&&n===A.current&&!await M(e)&&N()&&w(e.message)}finally{N()&&n===A.current&&O(!1)}}let F=(0,m.useEffectEvent)(()=>P());(0,m.useEffect)(()=>(k.current=!0,F(),()=>{k.current=!1,A.current++,localStorage.getItem(u)===d&&localStorage.removeItem(u)}),[d,u]);let I=()=>c(`orders`,{search:`?order=`+encodeURIComponent(d)});async function L(t,n,o,s){try{if(p.conflicted)throw Error(`订单已有新记录,请先读取最新订单并核对,填写内容已经保留。`);if(!n.trim()||n.trim().length>200)throw Error(`请填写1~200字的修改原因。`);if(!o)throw Error(`请先核实本次资料更正,并勾选确认。`);await e.mutate(`/api/orders/`+encodeURIComponent(d)+(s?`/change`:`/edit`),{...t,revision:p.revision,reason:n.trim(),verified:!0})}catch(e){if(!N()||await M(e)||!N())return;let t=e.message.replace(`不要重新开单`,`不要重复保存修改`),r=e.status===403&&i.user.role===`assistant`;if(r||e.status===409){let i=g(f,d),a={...p,inputs:i?.inputs||p.inputs,reason:i?.reason??n,verified:!1,sameDayDirectEdit:!r&&p.sameDayDirectEdit,conflicted:e.status===409,latest:null};r&&(t+=`。填写内容已保留,请重新核对并勾选后提交修改申请。`),j(a.inputs,a.reason,!1),y(a),S(e=>e+1)}w(t);return}if(N()){if(k.current=!1,localStorage.removeItem(f),localStorage.removeItem(u),s)c(`orders`,{search:`?order=`+encodeURIComponent(d),fromPage:!0});else{E(!0);let e=t.date||p.order.date,n=`mm-order-book-`+r.user.id,i={};try{i=JSON.parse(sessionStorage.getItem(n)||`{}`)}catch{}sessionStorage.setItem(n,JSON.stringify({...i,mode:`date`,date:e,q:``,status:``}))}try{await a()}catch(e){w(`修改已保存,最新订单暂未读到。`+e.message)}}}if(T)return(0,h.jsxs)(`div`,{className:`business-pages`,children:[(0,h.jsx)(`h1`,{children:`订单修改已保存`}),(0,h.jsx)(`p`,{children:`已更新原订单,原单号保留。`}),(0,h.jsx)(`button`,{onClick:I,children:`查看修改后的订单`}),(0,h.jsx)(`button`,{onClick:()=>c(`orders`),children:`重新读取订单`}),C&&(0,h.jsx)(`p`,{role:`alert`,children:C})]});if(!p)return(0,h.jsxs)(`div`,{className:`business-pages`,children:[(0,h.jsx)(`h1`,{children:`修改订单`}),(0,h.jsx)(`p`,{children:d?C||`正在读取已记录的订单…`:`请选择要修改的订单。`}),(0,h.jsx)(`button`,{disabled:D,onClick:()=>P(),children:`重新读取订单`}),(0,h.jsx)(`button`,{onClick:()=>c(`orders`),children:`返回订单列表`})]});let R=C&&(0,h.jsxs)(`div`,{role:`alert`,className:`error`,children:[C,p.conflicted&&!p.latest&&(0,h.jsx)(`button`,{type:`button`,"data-action":`reload-order-edit`,disabled:D||e.busy||!!e.pending(),onClick:()=>P(`review`),children:`读取最新订单供核对`})]});return(0,h.jsxs)(`div`,{className:`business-pages`,children:[p.conflicted&&p.latest&&(0,h.jsx)(v,{entry:p,read:P,disabled:D||e.busy||!!e.pending()}),(0,h.jsx)(`fieldset`,{className:`order-editor-body`,disabled:e.busy||!!e.pending(),children:p.priceLock&&p.financialEditable?(0,h.jsx)(l,{client:e,state:r,session:i,entry:p,serverFeedback:R,onDraft:j,onSave:L,onCancel:I,registerLeaveGuard:o},x):(0,h.jsx)(b,{client:e,state:r,session:i,entry:p,serverFeedback:R,onDraft:j,save:L,go:I,blocked:p.conflicted},x)})]})}function b({client:e,state:t,session:n,entry:i,serverFeedback:a,onDraft:s,save:c,go:g,blocked:_}){let{store:v,update:y,ctx:b,loadPools:x,poolError:S}=l({client:e,state:t,session:n,initialDraft:i.inputs,editing:!0}),{form:C,summary:w}=v,T=i.financialEditable,[E,D]=(0,m.useState)(i.reason),[O,k]=(0,m.useState)(i.verified),[A,j]=(0,m.useState)(``),M=r(()=>e.api(`/api/order-note-tags`),t.user.id),N=e=>t=>P(n=>({...n,[e]:t})),P=e=>{k(!1),y(e)};(0,m.useEffect)(()=>{s(v.draft,E,O)},[v.draft,s,E,O]);let F=t.user.role!==`owner`&&(!i.sameDayDirectEdit||!i.directEditDate||i.order.date!==i.directEditDate||(T?C.date:i.order.date)!==i.directEditDate);function I(e){e.preventDefault();try{let n;if(T){o.validateFormFields(C,t),n=o.validateIssueDraft(v.draft,t);let r=Array.from(e.currentTarget.querySelectorAll(`.line-item`));n={...n,lines:n.lines.map((e,t)=>{let n=Array.from(r[t].querySelectorAll(`input[name],select[name]`)).filter(t=>!t.closest(`.charge-row`)&&(t.name!==`stockCount`||Object.hasOwn(e,`stockCount`)));return{...Object.fromEntries(n.map(e=>[e.name,e.value])),...e}})}}else n={customer_type:C.customer_type||`unknown`,...o.validateOrderIdentity({customer:C.customer,salesperson:C.salesperson,note:C.note,internal_note:C.internal_note})};j(``),c(n,E,O,F)}catch(e){j(e.message)}}let L=C.payment.method===`信用卡`,R=(0,h.jsxs)(h.Fragment,{children:[(0,h.jsx)(u,{label:`客户称呼/代号(必填)`,name:`customer`,required:!0,maxLength:60,value:C.customer,onChange:N(`customer`)}),(0,h.jsxs)(`div`,{className:`customer-type-field`,children:[(0,h.jsx)(`span`,{children:`客户类型`}),[[`new`,`新客户`],[`returning`,`老客户`]].map(([e,t])=>(0,h.jsxs)(`label`,{children:[(0,h.jsx)(`input`,{type:`radio`,name:`customer_type`,value:e,checked:C.customer_type===e,onChange:()=>N(`customer_type`)(e)}),t]},e))]}),(0,h.jsx)(u,{label:`开单人(必填)`,name:`salesperson`,required:!0,maxLength:30,value:C.salesperson,onChange:N(`salesperson`)})]}),z=(0,h.jsxs)(h.Fragment,{children:[(0,h.jsxs)(`label`,{children:[`内部备注(客户不可见)`,(0,h.jsx)(`span`,{className:`small muted`,children:`必填,至少3个字符(含#号),最多500字`}),(0,h.jsx)(`textarea`,{name:`internal_note`,rows:3,required:!0,minLength:3,maxLength:500,placeholder:`仅供内部跟进,不会出现在客户销售单或打印件中`,value:C.internal_note,onChange:e=>N(`internal_note`)(e.target.value)})]}),(o.filterNoteTags(M.value?.tags||[],C.internal_note)||[]).map(e=>(0,h.jsx)(`button`,{type:`button`,onClick:()=>N(`internal_note`)(o.insertNoteTag(C.internal_note,e)),children:e},e))]}),B=(0,h.jsxs)(`label`,{children:[`客户单备注`,(0,h.jsx)(`textarea`,{name:`note`,maxLength:240,rows:T?2:3,value:C.note,onChange:e=>N(`note`)(e.target.value)})]});return(0,h.jsxs)(h.Fragment,{children:[(0,h.jsxs)(`div`,{className:`page-heading`,children:[(0,h.jsxs)(`div`,{children:[(0,h.jsx)(`div`,{className:`eyebrow`,children:`EDIT ORDER`}),(0,h.jsx)(`h1`,{id:`order-edit-title`,children:F?`申请修改订单`:`修改订单`}),(0,h.jsxs)(`p`,{children:[i.order.paper_number||i.order.number,` · `,i.order.date]})]}),(0,h.jsx)(`button`,{type:`button`,onClick:g,children:`取消修改`})]}),(0,h.jsx)(`div`,{className:`note`,id:`order-edit-save-intro`,children:(F?`店主复核同意后`:`保存后`)+`,以本页成交合计作为核实后的客户实收金额,并重新核算成本、费用、库存和利润。本次沿用原单号 `+(i.order.paper_number||i.order.number)+`。`}),(0,h.jsxs)(`form`,{id:T?`order-form`:`order-edit-meta-form`,"data-order-edit":T?`true`:void 0,onSubmit:I,children:[T?(0,h.jsxs)(h.Fragment,{children:[(0,h.jsx)(`input`,{type:`hidden`,name:`rounding_mode`,value:C.rounding_mode}),(0,h.jsx)(`input`,{type:`hidden`,name:`orderId`,value:i.id}),(0,h.jsx)(`input`,{type:`hidden`,name:`revision`,value:i.revision}),(0,h.jsxs)(`section`,{className:`panel`,children:[(0,h.jsxs)(`div`,{className:`panel-title`,children:[(0,h.jsx)(`h2`,{children:`客户与单据`}),(0,h.jsx)(`span`,{children:`修改内容单独暂存,可刷新后继续`})]}),(0,h.jsxs)(`div`,{className:`grid four`,children:[(0,h.jsx)(u,{label:`业务日期`,type:`date`,required:!0,max:t.today,name:`date`,value:C.date,onChange:N(`date`)}),(0,h.jsx)(u,{label:`原纸单编号`,name:`paper_number`,value:i.order.paper_number||i.order.number,readOnly:!0}),R,(0,h.jsx)(p,{label:`收款方式`,name:`payment_method`,options:o.paymentMethods(t),value:C.payment.method,onChange:e=>P(t=>({...t,payment:{...t.payment,method:e}}))}),(0,h.jsx)(p,{label:`收款人`,name:`payment_recipient`,disabled:L,value:C.payment.recipient,options:o.recipientOptions(t,C.payment),help:t.accounts?.includes(`麦麦信用卡`)&&(0,h.jsxs)(`small`,{hidden:!L,children:[`信用卡统一由`,o.memberName(t,`owner`),`收款。`]}),onChange:e=>P(t=>({...t,payment:{...t.payment,recipient:e,nonCardRecipient:e}}))})]})]}),(0,h.jsxs)(`section`,{className:`panel`,children:[(0,h.jsx)(`h2`,{children:`商品明细`}),C.lines.map((e,n)=>(0,h.jsx)(f,{index:n,line:e,state:t,ctx:b,form:C,update:P,reloadPools:x,error:j},n)),(0,h.jsx)(`button`,{type:`button`,onClick:()=>P(e=>o.addLine(e,b)),children:`+ 添加商品`}),S&&(0,h.jsx)(`p`,{role:`alert`,children:S})]}),(0,h.jsx)(d,{store:v,state:t,update:P}),(0,h.jsxs)(`section`,{className:`panel`,children:[(0,h.jsxs)(`div`,{className:`grid two`,children:[(0,h.jsx)(u,{label:`整单优惠/抹零(元)`,name:`discount`,value:C.discount,type:`number`,min:`0`,step:`0.01`,required:!0,onChange:N(`discount`)}),B]}),z,(0,h.jsxs)(`p`,{children:[`更正后成交合计/客户实收 `,(0,h.jsx)(`strong`,{children:w.totalText})]})]})]}):(0,h.jsxs)(`section`,{className:`panel`,children:[(0,h.jsxs)(`div`,{className:`grid two`,children:[R,B]}),z]}),(0,h.jsxs)(`section`,{className:`panel`,children:[(0,h.jsx)(`p`,{"data-order-edit-approval-note":!0,hidden:t.user.role!==`assistant`,children:F?`提交的是修改申请。店主复核同意后才生效,填写内容已经保留。`:`今天的订单可直接保存,改为其他日期须店主复核。`}),(0,h.jsxs)(`label`,{children:[`修改原因`,(0,h.jsx)(`textarea`,{name:`edit_reason`,required:!0,rows:2,maxLength:200,placeholder:T?`例如:核对实物后更正克重,或修正收款账户`:`例如:更正客户代号或开单人`,value:E,onChange:e=>D(e.target.value)})]}),(0,h.jsxs)(`label`,{className:`check`,children:[(0,h.jsx)(`input`,{type:`checkbox`,name:`edit_verified`,required:!0,checked:O,onChange:e=>k(e.target.checked)}),T?`已核实本页成交金额等于客户实际已付金额,收款账户、商品、克重及成本与凭据一致。`:`已核实客户称呼、开单人及备注的本次更正。`]}),(0,h.jsxs)(`div`,{className:`form-actions`,children:[(0,h.jsx)(`button`,{type:`button`,onClick:g,children:`取消修改`}),(0,h.jsx)(`button`,{"data-order-edit-submit":!0,disabled:_,className:`primary`,type:`submit`,children:F?`提交修改申请`:`保存修改`})]})]}),(0,h.jsxs)(`div`,{id:`order-error`,"aria-live":`polite`,children:[A&&(0,h.jsx)(`p`,{className:`error`,role:`alert`,children:A}),a]})]})]})}export{y as OrderEditPage}; |