Files
weiguang-jewelry-ledger/新账系统/web/dist/assets/OrderEditPage-8Ao6T3C2.js
T

1 line
16 KiB
JavaScript
Raw Blame History

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