import{a as d}from"./formatters-jmx2pV9o.js";const _=()=>{var a,e;return((e=(a=window.crypto)==null?void 0:a.randomUUID)==null?void 0:e.call(a))||`${Date.now()}-${Math.random().toString(16).slice(2)}`},b=[{value:"",label:"Tous les statuts"},{value:"draft",label:"Brouillon"},{value:"validated",label:"Validée"},{value:"cancelled",label:"Annulée"}],o=(a={})=>({local_id:a.local_id||_(),id:a.id||null,account_id:a.account_id||"",account_code:a.account_code||"",account_label:a.account_label||"",account_is_auxiliary:a.account_is_auxiliary||!1,account_search:a.account_code&&a.account_label?`${a.account_code} · ${a.account_label}`:a.account_search||"",account_balance:Number(a.account_balance||0),auxiliary_account_id:a.auxiliary_account_id||"",auxiliary_ref_code:a.auxiliary_ref_code||"",auxiliary_name:a.auxiliary_name||"",auxiliary_search:a.auxiliary_ref_code&&a.auxiliary_name?`${a.auxiliary_ref_code} · ${a.auxiliary_name}`:a.auxiliary_search||"",label:a.label||"",debit:a.debit?String(Number(a.debit).toFixed(2)):"",credit:a.credit?String(Number(a.credit).toFixed(2)):"",due_date:a.due_date||""}),u=(a,e={})=>{var r;return{company_id:a||"",journal_id:e.journal_id||"",entry_date:e.entry_date||new Date().toISOString().slice(0,10),piece_ref:e.piece_ref||"",label:e.label||"",attachment_url:e.attachment_url||"",lines:(r=e.lines)!=null&&r.length?e.lines.map(t=>o(t)):[o(),o()]}},m=(a=null)=>{var e;return{name:(a==null?void 0:a.name)||"",journal_id:(a==null?void 0:a.journal_id)||"",lines:(e=a==null?void 0:a.lines)!=null&&e.length?a.lines.map(r=>o(r)):[o(),o()]}},f=a=>u(a.company_id,{journal_id:a.journal_id,entry_date:a.entry_date,piece_ref:a.piece_ref,label:a.label,attachment_url:a.attachment_url||"",lines:a.lines||[]}),x=(a,e)=>{var r;return u(e,{journal_id:(a==null?void 0:a.journal_id)||"",piece_ref:"",label:a!=null&&a.name?`Depuis modèle · ${a.name}`:"",lines:((r=a==null?void 0:a.lines)==null?void 0:r.map(t=>({...t,debit:t.debit||"",credit:t.credit||""})))||[]})},y=(a,e)=>u(e,{journal_id:a.journal_id,entry_date:new Date().toISOString().slice(0,10),piece_ref:a.piece_ref?`COP-${a.piece_ref}`:"",label:`${a.label} · Copie`,lines:a.lines||[]}),n=a=>{if(a==null||a==="")return 0;if(typeof a=="number")return Number(a.toFixed(2));const e=String(a).trim().replace(/\s+/g,"").replace(",",".");if(!e)return 0;const r=Number.parseFloat(e);return Number.isFinite(r)&&r>=0?Number(r.toFixed(2)):0},g=a=>{const e=a.reduce((c,i)=>c+n(i.debit),0),r=a.reduce((c,i)=>c+n(i.credit),0),t=Number((e-r).toFixed(2));return{totalDebit:Number(e.toFixed(2)),totalCredit:Number(r.toFixed(2)),difference:t,isBalanced:t===0,formattedTotalDebit:d(e),formattedTotalCredit:d(r),formattedDifference:d(t)}},h=a=>({company_id:a.company_id,journal_id:Number(a.journal_id),entry_date:a.entry_date,piece_ref:a.piece_ref.trim(),label:a.label.trim(),attachment_url:a.attachment_url||null,lines:a.lines.map(e=>({account_id:Number(e.account_id),auxiliary_account_id:e.auxiliary_account_id?Number(e.auxiliary_account_id):null,label:e.label.trim(),debit:n(e.debit),credit:n(e.credit),due_date:e.due_date||null}))}),N=a=>({name:a.name.trim(),journal_id:Number(a.journal_id),lines:a.lines.map(e=>({account_id:Number(e.account_id),auxiliary_account_id:e.auxiliary_account_id?Number(e.auxiliary_account_id):null,label:e.label.trim(),debit:n(e.debit),credit:n(e.credit)}))}),j=a=>a==="validated"?"border-accent2/30 bg-accent2/10 text-accent2":a==="cancelled"?"border-danger/30 bg-danger/10 text-danger":"border-gold/30 bg-gold/10 text-gold",T=a=>{if(!a)return"—";const e=new Date(a).getTime()-Date.now(),r=Math.round(e/1e3),t=Math.abs(r),c=Math.round(t/60),i=Math.round(t/3600),l=Math.round(t/86400);return t<60?"À l'instant":c<60?`Il y a ${c} min`:i<24?`Il y a ${i} h`:`Il y a ${l} j`},p=a=>[["NumeroPiece","NumeroEcriture","Date","Journal","Libelle","DebitTotal","CreditTotal","Statut"],...a.map(r=>[r.piece_ref||"",r.entry_number||"",r.entry_date||"",r.journal_code||"",r.label||"",r.total_debit||"",r.total_credit||"",r.status_label||""])].map(r=>r.map(t=>`"${String(t??"").replaceAll('"','""')}"`).join(";")).join(`
`);export{b as E,g as a,p as b,u as c,y as d,x as e,o as f,j as g,T as h,m as i,N as j,f as m,h as s};
