1import{j4 as ne,j3 as he,hA as C,gY as n,gZ as t,fI as ve,gL as k,iY as ge,iL as _e,cJ as w,f0 as A}from"./index-DJZVjtmQ.js";import{p as Ce}from"./printPdfBlob-DaaW3xZl.js";import{u as xe}from"./SalesRoot-CtC7qg4y.js";import{u as Pe}from"./useGo-DmtsjHoM.js";import{p as y,f as x,m as _,u as Se,q as ke,e as we}from"./format-LV7yVnC-.js";import{M as ee,C as M}from"./ModalShell-CBLhkN5r.js";import{C as ae,a as je}from"./bits-BHII8dA5.js";import{D as De}from"./DocDetailScreen-BONGQjTk.js";import{C as Ee}from"./CancelModal-1OLuvGKR.js";import{u as Le,C as Ae}from"./ColumnsModal-9eMmN24J.js";import{P as Ne}from"./PdfPreview-kHVmNn5Q.js";import{M as qe}from"./ModalSendDocument-BHmlk3Es.js";import{b as Te,S as Be}from"./generateSalePDF-Dgkqt4VJ.js";import{S as Oe,f as Fe,g as ie,s as Ie}from"./generateShoppingDocument-8nrl3tSI.js";import{s as Me}from"./costLine-B1p3rVBy.js";import{getPurchase as Re,cancelPurchase as Ue,destroyPurchase as ze,acceptPurchase as Ve}from"./utils-CL9OAWTV.js";import{a as le,p as ce}from"./rowAdapter-Cvumy1V9.js";import{l as Ge,i as He,q as Ye}from"./DocumentViewNew-_n7-ezFw.js";import{a as $e}from"./shoppingAdapter-jxBoKgBk.js";import{d as Ke}from"./salesNote-tU_Hk4ex.js";import{getOrderInformationById as Je,getBusinessLogo as We}from"./utils-uCGSxT2Y.js";import"./TaxModal-C4wBwhtz.js";import"./lineMath-CdYEcuNu.js";import"./actiontaxes-aamvIn2G.js";import"./discountChain-KTz5htZG.js";import"./utils-DGFyYG1v.js";import"./SaleRowMobile-DxIy0tsX.js";import"./drawer-Dc7yxaE_.js";import"./ellipsis-DpHaUZKH.js";import"./upload-C10G4aba.js";import"./PickList-DYhka81R.js";import"./useTableColumns-DcUl77VD.js";import"./tablePreferences-DpDzjNmH.js";import"./nativeFiles-BdR1elM3.js";import"./PhoneInputWhatsApp-DTxHmmRA.js";import"./countryCodes-DidX2tuv.js";import"./buildDocumentXlsx-DHEtlmi5.js";import"./xlsx-B4eNT4xg.js";import"./index-BKD8Dact.js";import"./index-BAMY2Nnw.js";import"./utils-CeoKrUbS.js";import"./onlineChannels-cSPEJk7h.js";import"./columnDefaults-CfXk9YuJ.js";import"./index-BdWTPIO3.js";import"./index-BbP3371Q.js";import"./browser-BSX7BJ8Q.js";import"./pdf-1OqqxK52.js";import"./noteHtml-Cqg0UBLS.js";function Xe({kind:e,documentId:l,onClose:j,generate:f}){const{t:a}=ne(),{toast:v}=he(),{idField:P,filePrefix:D,title:r}=Oe[e],[s,m]=C.useState(null);C.useEffect(()=>{l&&Fe(e,l).then(m).catch(()=>v({title:a("No se pudo cargar el documento"),variant:"destructive"}))},[e,l,a,v]);const g=async d=>{await Te(d,(p,E)=>(f||((c,S)=>ie(e,c,S)))(p,E),l,D);const u=await Ie(e,d);if((u==null?void 0:u.code)!==201)throw v({title:a("No se pudo enviar el documento"),variant:"destructive"}),new Error(u==null?void 0:u.message);v({title:a("Documento enviado")})};return n.jsx(qe,{modal:!!l&&!!s,setModal:d=>{d||(m(null),j())},info:s,setInfo:m,onSend:g,idField:P,title:a(r),description:a("Elige cómo enviar el documento al proveedor"),showFileFormatToggle:!0})}const Ze="shopping.purchase.show",oe={item:{label:t("Item")},quantity:{label:t("Cantidad")},unit:{label:t("Unidad")},price:{label:t("Costo x Unidad")},discount:{label:t("Descuento %")},taxes:{label:t("Impuesto %")},deliveryDate:{label:t("Fecha de Entrega")},subTotal:{label:t("SubTotal")},categorySubtotal:{label:t("Subtotal categorÃa")},totales:{label:t("Totales del documento")}};async function te(e,{format:l="pdf",groupByCategory:j=!1}={}){var g,d,u;if(l!=="pdf")return ie("order",e,{format:l});const[f,a,v]=await Promise.all([Je(e),We(),ve()]),P=$e((f==null?void 0:f.data)||{}),D=await Promise.all((P.slots_array||[]).map(async p=>({...p,img:await Ge(p.img)}))),r=P,s={...r,slots_array:He(D)},m=le(r);return Ye(n.jsx(Be,{sale:s,logo:a==null?void 0:a.data,watermark:a==null?void 0:a.watermark,note:Ke(v,"orden"),kind:"Pedido de compra",extra:{partyLabel:"Proveedor",meta:[[k.t("Folio"),r.document_number],[k.t("Fecha"),x(r.document_created)],[k.t("Almacén"),((g=r.inventory_id)==null?void 0:g.label)||r.warehouse||"â"],[k.t("Entrega"),r.delivery_date?x(r.delivery_date):k.t("Sin fecha")]],warehouse:((d=r.inventory_id)==null?void 0:d.label)||r.warehouse,rfc:(u=r.supplier)==null?void 0:u.rfc,pay:ce(r),delivery:r.delivery_date,status:k.t(m),cancelled:m==="Cancelado",shipping:y(r.shipping)}})).toBlob()}async function Xa({params:e}){const l=await Re(e.id);return{purchase:l==null?void 0:l.data}}const Qe="pos-mono",re={1:t("Por semana"),2:t("Por quincena"),3:t("Por mes"),4:t("Por trimestre"),5:t("Anual")},ea=[{id:"c1",label:t("El proveedor no puede surtir"),meta:t("No hay existencia o no llega a tiempo")}
1,{id:"c2",label:t("Error de captura"),meta:t("Se volverá a levantar corregido")},{id:"c3",label:t("Se consiguió mejor precio"),meta:t("Se compra con otro proveedor")},{id:"c4",label:t("Duplicado"),meta:t("Ya existe otro pedido igual")}],aa="margin-top:8px;width:100%;height:56px;padding:0 16px;border:1px solid var(--border);border-radius:14px;background:var(--surface);font-size:17px;color:var(--ink);outline:none",Za=()=>{var H,Y,$,K,J,W,X;const{t:e}=ne(),l=Pe(),j=ge(),f=xe(),{purchase:a}=_e(),{columns:v,toggle:P,cols:D}=Le(Ze,oe),[r,s]=C.useState(null),[m,g]=C.useState(!1),[d,u]=C.useState({payment_type:"2",limit_credit_date:""}),p=a.id,E=le(a),c=E==="Cancelado",S=E==="Recibido",N=!c&&!S,b=a.buy||null,se=((H=a.supplier_id)==null?void 0:H.label)||((Y=a.supplier)==null?void 0:Y.name)||e("Sin proveedor"),R=(($=a.inventory_id)==null?void 0:$.label)||a.warehouse||e("Sin almacén"),de=String(a.payment_type??((K=a.payment_condition)==null?void 0:K.value)??"2")==="1",U=ce(a),q=async o=>{g(!0);try{await o()}catch{f(e("No se pudo completar la acción"),"error")}g(!1)},F=o=>{const i=new FormData;return Object.entries(o).forEach(([B,O])=>i.append(B,O??"")),i},pe=({reason:o})=>q(async()=>{const i=await Ue(F({order_id:p,reason:o}));if(i!=null&&i.code&&i.code>=400){f((i==null?void 0:i.message)||e("No se pudo cancelar el pedido"),"error");return}s(null),f(a.document_number+" "+e("cancelado")+" · "+o),j.revalidate()}),me=()=>q(async()=>{await ze(F({order_id:p})),l("/shopping/purchase",{state:{toast:a.document_number+" "+e("eliminado")}})}),ue=()=>q(async()=>{const o=await Ve(F({order_id:p,...d}));if(o!=null&&o.code&&o.code>=400){f((o==null?void 0:o.message)||e("No se pudo aceptar la compra"),"error");return}s(null),f(a.document_number+" "+e("recibido")),j.revalidate()}),be=()=>l("/shopping/request-orders/create",{state:{quoteOrOrderData:{...a,purchase_id:p}}}),z=C.useCallback(()=>te(p,{format:"pdf"}),[p]),ye=()=>q(async()=>Ce(await z())),h=C.useCallback(()=>s(null),[]),L=a.slots_array||[],V=o=>y(o.value)*Number(o.quantity||0)*(1-(y(o.discount)||0)/100),fe=L.map(o=>{var O,Z,Q;const i=Me(o),B=o.shopping_equivalent_name;return{id:o.slot_id||o.id,qty:ke(o.quantity),name:o.product_name||((O=o.product)==null?void 0:O.label)||o.name||o.description||e("Partida"),sku:o.code||((Z=o.product)==null?void 0:Z.code),unidad:Se(o.unit)||e("Pieza"),unit:_(y(o.value)),disc:y(o.discount),taxes:i.length?i:null,amount:_(V(o)),amountTax:_(y(o.total)),img:o.img||o.image,child:B?e("Comprado por")+" "+B:null,line:{name:((Q=o.product)==null?void 0:Q.label)||o.description,value:y(o.value),quantity:o.quantity,discount:y(o.discount),discount_chain:o.discount_chain,taxes:i}}}),G=y(a.subtotal),I=L.reduce((o,i)=>o+y(i.value)*Number(i.quantity||0),0)-(G||L.reduce((o,i)=>
1o+V(i),0)),T={backTo:"/shopping/purchase",backLabel:t("Compras"),docLabel:t("Pedido de compra"),screenLabel:"Detalle de pedido de compra",partyLabel:"Proveedor",priceHead:t("Costo"),folio:a.document_number,tag:E,actions:[{key:"edit",label:t("Editar"),kind:"primary",hidden:!N,onPick:()=>l(`/shopping/purchase/edit/${p}`)},{key:"receive",label:t("Aceptar compra"),kind:"primary",hidden:!N,onPick:()=>s("receive")},{key:"cols",label:t("Columnas"),desktopOnly:!0,onPick:()=>s("cols")},{key:"print",label:t("Imprimir"),onPick:ye,disabled:m},{key:"pdf",label:t("Ver PDF"),onPick:()=>s("pdf")},{key:"send",label:t("Enviar"),onPick:()=>s("send")},{key:"cancel",label:t("Cancelar"),kind:"danger",hidden:!N,onPick:()=>s("cancel")},{key:"delete",label:t("Eliminar"),kind:"danger",onPick:()=>s("delete")}],states:[{label:t("Pedido"),done:!c,cancelled:c,value:e(c?"Cancelado":"Levantado"),meta:c?a.cancel_reason||e("Documento sin efectos"):[x(a.created_at||a.document_created),R].filter(Boolean).join(" · ")},{label:t("Compromiso"),done:!c&&!!a.delivery_date,cancelled:c,value:c?e("Cancelado"):a.delivery_date?e("Entrega")+" "+x(a.delivery_date):e("Sin fecha de entrega"),meta:c?e("Documento sin efectos"):U,action:t("Poner fecha"),onPick:()=>l(`/shopping/purchase/edit/${p}`),disabled:!N},{label:t("Recepción"),done:S,cancelled:c,value:c?e("Cancelado"):S?e("Recibido")+(b!=null&&b.code?" · "+b.code:""):e("Sin recibir"),meta:e(c?"Documento sin efectos":S?"La compra ya movió inventario y cuentas por pagar":"El pedido no mueve inventario ni cuentas por pagar"),action:t("Convertir a Compra"),onPick:be,disabled:c}],cols:D,itemCount:L.length+" "+(L.length===1?e("partida"):e("partidas")),items:fe,note:a.comments,client:{name:se,rfc:(J=a.supplier)==null?void 0:J.rfc},facts:[{icon:w.calendar,label:t("Fecha"),value:x(a.document_created)||"â"},{icon:w.warehouse,label:t("Almacén"),value:R},{icon:w.card,label:t("Tipo de pago"),value:U+(de&&re[a.collection_period]?" · "+e(re[a.collection_period]):"")},{icon:w.truck,label:t("Entrega"),value:x(a.delivery_date)||e("Sin fecha")},...a.quote_code?[{icon:w.doc,label:t("Cotización"),value:a.quote_code}]:[],...b!=null&&b.code?[{icon:w.cart,label:t("Compra"),value:b.code,href:`/shopping/request-orders/show/${b.id}`}]:[]],totals:[{label:e("Subtotal"),value:_(G)},{label:e("Descuento"),value:I>.004?"â"+_(I):"â",ink:I>.004?"var(--ok)":"var(--ink-4)"},{label:e("Impuestos"),value:_(y(a.taxes??a.tax))}],total:_(y(a.total))};return T.timeline=we({t:e("Pedido de compra creado"),d:[x(a.created_at||a.document_created),(X=(W=a.creator)==null?void 0:W.name)==null?void 0:X.trim()].filter(Boolean).join(" · ")},c?T.states.slice(0,1):T.states.slice(1)),n.jsxs(De,{view:T,children:[n.jsx(Ne,{open:r==="pdf",onClose:h,folio:a.document_number,build:z,onSend:()=>s("send")}),n.jsx(Ee,{open:r==="cancel",onClose:h,folio:a.document_number,busy:m,reasons:ea,confirmLabel:e("Cancelar pedido"),onConfirm:pe}),n.jsx(ee,{open:r==="receive",onClose:h,kicker:e("Aceptar compra")+" "+a.document_number,title:e("¿Cómo se le paga al proveedor?"),width:480,footer:n.jsx(M,{clearLabel:e("Volver"),applyLabel:e(m?"Aceptandoâ¦":"Aceptar compra"),onClear:h,onApply:ue,applyDisabled:m||d.payment_type==="1"&&!d.limit_credit_date}),children:n.jsxs("div",{style:A("padding:24px"),children:[n.jsxs("div",{style:A("display:flex;align-items:center;gap:8px;flex-wrap:wrap"),children:[n.jsx(ae,{active:d.payment_type==="2",onClick:()=>u({payment_type:"2",limit_credit_date:""}),children:e("Un solo pago")}),n.jsx(ae,{active:d.payment_type==="1",onClick:()=>u(o=>({...o,payment_type:"1"})),children:e("Crédito")})]}),d.payment_type==="1"&&n.jsxs(n.Fragment,{children:[n.jsx(je,{style:"margin-top:20px",children:e("Vence el")}),n.jsx("input",{type:"date",value:d.limit_credit_date,onChange:o=>u(i=>({...i,limit_credit_date:o.target.value})),className:Qe,style:A(aa)})]}),n.jsx("div",{style:A("margin-top:18px;font-size:13.5px;line-height:1.5;color:var(--ink-3)"),children:e("Al aceptar se genera la Compra: entra la mercancÃa al almacén y se abre la cuenta por pagar.")})]})}),n.jsx(ee,{open:r==="delete",onClose:h,kicker:e("Eliminar")+" "+a.document_number,title:a.destroy===!1?e("Este pedido ya no se puede borrar"):e("¿Borrar este pedido de compra?"),width:460,footer:a.destroy===!1?n.jsx(M,{clearLabel:e("Volver"),applyLabel:e("Generar devolución"),onClear:h,onApply:()=>l("/shopping/returns/create"+(b!=null&&b.id?"?buy="+b.id:""))}):n.jsx(M,{clearLabel:e("Volver"),applyLabel:e(m?"Eliminandoâ¦":"Eliminar"),onClear:h,onApply:me,applyDisabled:m}),children:n.jsx("div",{style:A("padding:24px;font-size:14px;line-height:1.5;
1color:var(--ink-2)"),children:a.destroy===!1?e("La mercancÃa ya entró al almacén. Para revertirla se genera una devolución de compra."):e("Se borra definitivamente y deja de contar en los reportes. No se puede deshacer.")})}),n.jsx(Ae,{open:r==="cols",onClose:h,columns:v,toggle:P,defaults:oe}),n.jsx(Xe,{kind:"order",documentId:r==="send"?p:null,onClose:h,generate:te})]})};export{Za as default,Xa as loadPurchase};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.