import{s as i,b as O}from"./index-BxIwIJfy.js";import{a as U}from"./ReportViewer-BNzhW7G2.js";import{s as f}from"./stimulsoft-B2-Y-rC1.js";import{a as y}from"./vue-core-Di3IuVqe.js";function M(){const A=y(!1),E=y(""),$=y(""),v=y({Entete:[],Lignes:[]}),w=y(null),b=y(null),L=()=>i.state.portailClientDb,k=()=>i.state.portailClientToken,R=()=>({Authorization:`Bearer ${k()}`});async function D({type:n,nDoc:e,libelleDocument:m}){const o={facture:"imprimer-facture",avoir:"imprimer-avoir",devis:"imprimer-devis",bl:"imprimer-bl",bcomc:"imprimer-bcomc"}[n]||"imprimer-facture",[r,d]=await Promise.all([O.get(`${i.lienApi}/portail-client/document-template/${L()}`,{params:{document:m},headers:R()}),O.post(`${i.lienApi}/portail-client/${o}/${L()}/${encodeURIComponent(e)}`,{},{headers:R()})]);return{cheminTemplate:r.data?.chemin,lignes:d.data||[]}}async function N({type:n,nDoc:e,libelleDocument:m,fileName:l}){w.value=e;try{const{cheminTemplate:o,lignes:r}=await D({type:n,nDoc:e,libelleDocument:m});if(!o)throw new Error("Aucun modele de document disponible.");if(!r.length)throw new Error("Aucune donnee pour ce document.");E.value=`${i.lienApi}/${o}`,$.value=l||`${n}_${e}`,v.value={Entete:[],Lignes:r},A.value=!0}finally{w.value=null}}async function P({type:n,nDoc:e,libelleDocument:m,fileName:l}){b.value=e;try{const{cheminTemplate:o,lignes:r}=await D({type:n,nDoc:e,libelleDocument:m});if(!o)throw new Error("Aucun modele de document disponible.");if(!r.length)throw new Error("Aucune donnee pour ce document.");const d=await J(o,r),t=(l||`${n}_${e}.pdf`).endsWith(".pdf")?l||`${n}_${e}.pdf`:`${l||`${n}_${e}`}.pdf`,s=URL.createObjectURL(d),c=document.createElement("a");c.href=s,c.download=t,document.body.appendChild(c),c.click(),document.body.removeChild(c),URL.revokeObjectURL(s)}finally{b.value=null}}function J(n,e){return new Promise(async(m,l)=>{try{const o=`${i.lienApi}/${n}`,r=await fetch(o,{cache:"no-store"});if(!r.ok)throw new Error(`Telechargement du modele : ${r.status}`);const d=await r.text(),t=new f.Stimulsoft.Report.StiReport;t.load(d);const s={...i.state.enreg_societe};s.avecEntete=!0,s.lienApi=i.lienApi,s.cloudId=i.cloudId,s.id_societe=i.id_societe;const c=(p,g)=>{if(!p)return;const h=Array.isArray(g)?g[0]:g;if(!h)return;const B=new Set;for(let a=0;a<p.columns.count;a++){const u=p.columns.getByIndex(a);B.add(u.nameInSource||u.name)}for(const a in h){if(!Object.hasOwn(h,a))continue;const u=h[a];if(Array.isArray(u)||B.has(a))continue;let S="System.String";typeof u=="number"?S=Number.isInteger(u)?"System.Int32":"System.Decimal":typeof u=="boolean"?S="System.Boolean":u instanceof Date&&(S="System.DateTime"),p.columns.add(new f.Stimulsoft.Report.Dictionary.StiDataColumn(a,a,a,S))}},I=U(d,"Lignes",e);c(t.dictionary.dataSources.getByName("Entete"),[]),c(t.dictionary.dataSources.getByName("Lignes"),I),c(t.dictionary.dataSources.getByName("Autres"),[s]);const C=new f.Stimulsoft.System.Data.DataSet("Entete");C.readJson(JSON.stringify({root:[]}));const T=new f.Stimulsoft.System.Data.DataSet("Lignes");T.readJson(JSON.stringify({root:I}));const _=new f.Stimulsoft.System.Data.DataSet("Autres");_.readJson(JSON.stringify({root:[s]})),t.dictionary.databases.clear(),t.regData("Entete","Entete",C),t.regData("Lignes","Lignes",T),t.regData("Autres","Autres",_);const x=t.dictionary.variables.getByName("Len");x&&e.length>0&&(x.valueObject=e.length),t.renderAsync(()=>{t.exportDocumentAsync(p=>{const g=new Blob([new Uint8Array(p)],{type:"application/pdf"});m(g)},f.Stimulsoft.Report.StiExportFormat.Pdf)})}catch(o){l(o)}})}return{ouvrireReport:A,urlFichierRapport:E,nomFichierImpression:$,dataReport:v,apercuEnCours:w,telechargementEnCours:b,apercevoir:N,telecharger:P}}export{M as u};
