1import{_ as d}from"./FormControl.vue_vue_type_script_setup_true_lang-DQDOazJB.js";import{ad as ke,ag as r,A as m,w as s,a as V,H as o,av as l,b6 as u,n as p,x as g,am as Z,m as z,y as w,ak as i,v as _e,G as c,a8 as ae,ar as x}from"./index-Bjy5XtNX.js";import{L as le}from"./LoadingText-Zmbt-QPh.js";import{_ as Ve}from"./ListView-D_8ab1_t.js";import{D as R}from"./index-B1ZP30gt.js";import{_ as oe}from"./Badge.vue_vue_type_script_setup_true_lang-DPlTgLIq.js";import"./ohash.D__AXeF1-DHF4M4F_.js";import"./Checkbox.vue_vue_type_script_setup_true_lang-DDOWflWq.js";const we={class:"flex flex-col gap-4"},Ce={class:"flex items-center justify-between"},he={class:"flex items-center gap-2"},Ue={class:"flex border-b border-outline-gray-2 gap-4"},Ee=["onClick"],Ie={key:0,class:"font-medium"},Te={key:1},Se={key:2},ze={key:3},Ne={class:"flex flex-col gap-3"},Pe={class:"flex flex-col gap-3"},je={class:"flex justify-end"},$e={class:"max-h-96 overflow-auto divide-y divide-outline-gray-1 rounded border border-outline-gray-1"},Re={class:"flex items-center gap-2"},Oe={class:"font-medium"},Le={class:"flex gap-1"},Ae={class:"grid grid-cols-1 sm:grid-cols-2 gap-3"},De={class:"sm:col-span-2 flex items-end gap-2"},Be={key:0,class:"rounded border border-outline-gray-2 p-6 text-center text-sm text-ink-gray-4"},Fe={key:1,class:"rounded border border-outline-gray-2 overflow-hidden"},qe={class:"w-full text-sm"},Ge={class:"px-3 py-2 font-medium"},Me={class:"px-3 py-2 text-right"},Je={class:"px-3 py-2 text-right text-ink-gray-5"},He={class:"px-3 py-2 text-right"},Qe={class:"px-3 py-2 text-right"},Ke={class:"flex flex-col gap-3"},We={class:"border-t border-outline-gray-1 pt-3"},Xe={class:"flex gap-2 items-end"},rt={__name:"Billing",setup(Ye){const U=i("invoices"),O=i([]),L=i(!1),N=i(""),E=i(!1),I=i(null),y=i({credit_limit:"",topup_amount:"",topup_note:""}),C=i(!1),k=i("");async function P(){L.value=!0,N.value="";try{const a=await(await fetch("/api/billing/credit")).json();if(!a.ok)throw new Error(a.error||"Errore");O.value=a.customers||[]}catch(a){N.value=a.message}finally{L.value=!1}}function ne(a){I.value=a,y.value={credit_limit:a.credit_limit,topup_amount:"",topup_note:""},k.value="",E.value=!0}async function se(){k.value="";const a=parseFloat(y.value.credit_limit);if(isNaN(a)||a<0){k.value="Limite non valido";return}C.value=!0;try{const t=await(await fetch(`/api/billing/credit/${encodeURIComponent(I.value.name)}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({credit_limit:a})})).json();if(!t.ok)throw new Error(t.error||"Errore");E.value=!1,await P()}catch(e){k.value=e.message}finally{C.value=!1}}async function ie(){k.value="";const a=parseFloat(y.value.topup_amount);if(!a||a<=0){k.value="Importo non valido";return}C.value=!0;try{const t=await(await fetch(`/api/billing/credit/${encodeURIComponent(I.value.name)}/topup`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({amount:a,note:y.value.topup_note})})).json();if(!t.ok)throw new Error(t.error||"Errore");y.value.topup_amount="",E.value=!1,await P()}catch(e){k.value=e.message}finally{C.value=!1}}const ee=i([]),A=i([]),D=i([]),B=i(""),F=i(!0),T=i(""),q=i(!1),G=i(""),S=i(!1),h=i(""),M=i(!1),b=i(""),J=i(!1),H=()=>({customer_name:"",customer_type:"Company",tax_id:"",email:"",mobile:"",website:"",customer_group:"",territory:"",address:{line:"",city:"",pincode:"",country:"Italy"}}),n=i(H()),re=[{label:"Azienda",value:"Company"},{label:"Privato",value:"Individual"}],j=i(!1),v=i({customer:"",amount:"",description:"",qty:1}),Q=i(!1),_=i(""),K=i("");async function $(){F.value=!0,T.value="";try{const a=await(await fetch("/api/billing/")).json();if(a.error)throw new Error(a.error);ee.value=a.invoices||[]}catch(a){T.value=a.message}finally{F.value=!1}}async function W(){try{const a=await(await fetch("/api/billing/customers")).json();D.value=a.customers||[],A.value=D.value.map(e=>({label:e.customer_name||e.name,value:e.name}))}catch{A.value=[]}}function ue(){n.value=H(),h.value="",b.value="",S.value=!0}async function de(a){b.value="",h.value=a.name,S.value=!0,n.value=H();try{const e=await(await fetch(`/api/billing/customer/${encodeURIComponent(a.name)}`)).json();if(e.ok){const t=e.customer||{};n.value={customer_name:t.customer_name||"",customer_type:t.customer_type||"Company",tax_id:t.tax_id||"",email:t.email_id||"",mobile:t.mobile_no||"",website:t.website||"",customer_group:t.customer_group||"",territory:t.territory||"",address:{line:"",city:"",pincode:"",country:"Italy"}}}}catch{}}async function me(){if(!n.value.tax_id){b.value="Inserisci la P.IVA";return}J.value=!0,b.value="";try{const a=await(await fetch(`/api/billing/vat-lookup?vat=${encodeURIComponent(n.value.tax_id)}`)).json();if(!a.ok)throw new Error(a.error||"P.IVA non trovata");a.name&&!n.value.customer_name&&(n.value.customer_name=a.name),a.address&&(n.value.address={...n.value.address,...a.address})}catch(a){b.value=a.message}finally{J.value=!1}}async function ve(){if(b.value="",!n.value.customer_name.trim()){b.value="Nome cliente richiesto";return}M.value=!0;try{const a=!!h.value,e=a?`/api/billing/customer/${encodeURIComponent(h.value)}`:"/api/billing/customer",f=await(await fetch(e,{method:a?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n.value)})).json();if(!f.ok)throw new Error(f.error||"Errore");S.value=!1,await W()}catch(a){b.value=a.message}finally{M.value=!1}}async function pe(a){G.value=a.name;try{const t=await(await fetch(`/api/billing/customer/${encodeURIComponent(a.name)}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({disabled:!a.disabled})}
1)).json();if(!t.ok)throw new Error(t.error||"Errore");await W()}catch(e){custErr.value=e.message}finally{G.value=""}}const ce=[{label:"Fattura",key:"name",width:"200px"},{label:"Cliente",key:"customer",width:"200px"},{label:"Totale",key:"_total",width:"120px",align:"right"},{label:"Scaduto",key:"_out",width:"120px",align:"right"},{label:"Stato",key:"_status",width:"120px"},{label:"Data",key:"posting_date",width:"110px"},{label:"",key:"_actions",width:"120px"}],ye=_e(()=>{const a=B.value.trim().toLowerCase();return ee.value.filter(e=>!a||(e.name+" "+(e.customer||"")).toLowerCase().includes(a)).map(e=>({...e,_total:X(e.grand_total,e.currency),_out:X(e.outstanding_amount,e.currency),_status:e.docstatus===0?"Bozza":e.status||""}))});function X(a,e){return a==null?"":new Intl.NumberFormat("it-IT",{style:"currency",currency:e||"EUR"}).format(a)}function Y(a){return X(a,"EUR")}function fe(a){return a==="Bozza"?"gray":a==="Paid"?"green":a==="Overdue"?"red":"blue"}function ge(){v.value={customer:"",amount:"",description:"",qty:1},_.value="",j.value=!0}async function be(){if(_.value="",!v.value.customer){_.value="Seleziona un cliente";return}if(!v.value.amount){_.value="Inserisci un importo";return}Q.value=!0;try{const e=await(await fetch("/api/billing/generate",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({customer:v.value.customer,amount:v.value.amount,description:v.value.description,qty:v.value.qty||1})})).json();if(!e.ok)throw new Error(e.error||"Errore generazione");j.value=!1,await $()}catch(a){_.value=a.message}finally{Q.value=!1}}async function xe(a){K.value=a;try{const t=await(await fetch(`/api/billing/${encodeURIComponent(a)}/submit`,{method:"POST"})).json();if(!t.ok)throw new Error(t.error||"Errore conferma");await $()}catch(e){T.value=e.message}finally{K.value=""}}return ke(()=>{$(),W(),P()}),(a,e)=>(r(),m("div",we,[s("div",Ce,[e[29]||(e[29]=s("div",null,[s("div",{class:"text-base font-semibold"},"Fatturazione"),s("div",{class:"text-xs text-ink-gray-5"},"Fatture hosting su ERP â genera e conferma senza uscire dalla console.")],-1)),s("div",he,[U.value==="invoices"?(r(),m(V,{key:0},[o(l(d),{modelValue:B.value,"onUpdate:modelValue":e[0]||(e[0]=t=>B.value=t),placeholder:"Cerca fatturaâ¦",class:"w-56"},null,8,["modelValue"]),o(l(p),{variant:"subtle",onClick:$},{default:u(()=>[...e[25]||(e[25]=[c("Aggiorna",-1)])]),_:1}),o(l(p),{variant:"subtle",onClick:e[1]||(e[1]=t=>q.value=!0)},{default:u(()=>[...e[26]||(e[26]=[c("Clienti ERP",-1)])]),_:1}),o(l(p),{variant:"solid",onClick:ge},{default:u(()=>[...e[27]||(e[27]=[c("Genera fattura",-1)])]),_:1})],64)):(r(),g(l(p),{key:1,variant:"subtle",onClick:P},{default:u(()=>[...e[28]||(e[28]=[c("Aggiorna",-1)])]),_:1}))])]),s("div",Ue,[(r(),m(V,null,Z([{key:"invoices",label:"Fatture"},{key:"credit",label:"Credito prepagato"}],t=>s("button",{key:t.key,onClick:f=>U.value=t.key,class:ae(["pb-2 text-sm font-medium border-b-2 -mb-px transition-colors",U.value===t.key?"border-ink-blue-5 text-ink-blue-5":"border-transparent text-ink-gray-5 hover:text-ink-gray-7"])},x(t.label),11,Ee)),64))]),U.value==="invoices"?(r(),m(V,{key:0},[F.value?(r(),g(l(le),{key:0})):T.value?(r(),g(l(z),{key:1,message:T.value},null,8,["message"])):(r(),g(l(Ve),{key:2,columns:ce,rows:ye.value,"row-key":"name",options:{selectable:!1,showTooltip:!1,emptyState:{title:"Nessuna fattura",description:"Genera la prima fattura"}}},{cell:u(({column:t,row:f,item:te})=>[t.key==="name"?(r(),m("span",Ie,x(te),1)):t.key==="_status"?(r(),m("span",Te,[o(l(oe),{label:f._status,theme:fe(f._status),variant:"subtle"},null,8,["label","theme"])])):t.key==="_actions"?(r(),m("span",Se,[f.docstatus===0?(r(),g(l(p),{key:0,variant:"subtle",size:"sm",loading:K.value===f.name,onClick:Ze=>xe(f.name)},{default:u(()=>[...e[30]||(e[30]=[c("Conferma",-1)])]),_:1},8,["loading","onClick"])):w("",!0)])):(r(),m("span",ze,x(te),1))]),_:1},8,["rows"])),o(l(R),{modelValue:j.value,"onUpdate:modelValue":e[6]||(e[6]=t=>j.value=t),options:{title:"Genera fattura"}},{"body-content":u(()=>[s("div",Ne,[o(l(d),{type:"select",label:"Cliente",modelValue:v.value.customer,"onUpdate:modelValue":e[2]||(e[2]=t=>v.value.customer=t),options:A.value},null,8,["modelValue","options"]),o(l(d),{type:"number",label:"Importo (EUR)",modelValue:v.value.amount,"onUpdate:modelValue":e[3]||(e[3]=t=>v.value.amount=t),placeholder:"0.00"},null,8,["modelValue"]),o(l(d),{type:"text",label:"Descrizione",modelValue:v.value.description,"onUpdate:modelValue":e[4]||(e[4]=t=>v.value.description=t),placeholder:"Hosting MMOSâ¦"},null,8,["modelValue"]),o(l(d),{type:"number",label:"Quantità ",modelValue:v.value.qty,"onUpdate:modelValue":e[5]||(e[5]=t=>v.value.qty=t)},null,8,["modelValue"]),_.value?(r(),g(l(z),{key:0,message:_.value},null,8,["message"])):w("",!0)])]),actions:u(()=>[o(l(p),{variant:"solid",loading:Q.value,onClick:be},{default:u(()=>[...e[31]||(e[31]=[c("Crea bozza",-1)])]),_:1},8,["loading"])]),_:1},8,["modelValue"]),o(l(R),{modelValue:q.value,"onUpdate:modelValue":e[7]||
1(e[7]=t=>q.value=t),options:{title:"Clienti ERP",size:"lg"}},{"body-content":u(()=>[s("div",Pe,[s("div",je,[o(l(p),{variant:"solid",onClick:ue},{default:u(()=>[...e[32]||(e[32]=[c("Nuovo cliente",-1)])]),_:1})]),s("div",$e,[(r(!0),m(V,null,Z(D.value,t=>(r(),m("div",{key:t.name,class:"flex items-center justify-between px-3 py-2 text-sm"},[s("span",Re,[s("span",Oe,x(t.customer_name||t.name),1),t.disabled?(r(),g(l(oe),{key:0,label:"disabilitato",theme:"gray",variant:"subtle"})):w("",!0)]),s("span",Le,[o(l(p),{variant:"ghost",size:"sm",onClick:f=>de(t)},{default:u(()=>[...e[33]||(e[33]=[c("Modifica",-1)])]),_:1},8,["onClick"]),o(l(p),{variant:"ghost",size:"sm",loading:G.value===t.name,onClick:f=>pe(t)},{default:u(()=>[c(x(t.disabled?"Abilita":"Disabilita"),1)]),_:2},1032,["loading","onClick"])])]))),128))])])]),_:1},8,["modelValue"]),o(l(R),{modelValue:S.value,"onUpdate:modelValue":e[20]||(e[20]=t=>S.value=t),options:{title:h.value?"Scheda cliente":"Nuovo cliente",size:"xl"}},{"body-content":u(()=>[s("div",Ae,[s("div",De,[o(l(d),{type:"text",label:"P.IVA (autocompila da VIES)",modelValue:n.value.tax_id,"onUpdate:modelValue":e[8]||(e[8]=t=>n.value.tax_id=t),placeholder:"IT01234567890",class:"flex-1"},null,8,["modelValue"]),o(l(p),{variant:"subtle",loading:J.value,onClick:me},{default:u(()=>[...e[34]||(e[34]=[c("Cerca P.IVA",-1)])]),_:1},8,["loading"])]),o(l(d),{type:"text",label:"Nome / Ragione sociale",modelValue:n.value.customer_name,"onUpdate:modelValue":e[9]||(e[9]=t=>n.value.customer_name=t)},null,8,["modelValue"]),o(l(d),{type:"select",label:"Tipo",modelValue:n.value.customer_type,"onUpdate:modelValue":e[10]||(e[10]=t=>n.value.customer_type=t),options:re},null,8,["modelValue"]),o(l(d),{type:"email",label:"Email",modelValue:n.value.email,"onUpdate:modelValue":e[11]||(e[11]=t=>n.value.email=t)},null,8,["modelValue"]),o(l(d),{type:"text",label:"Telefono",modelValue:n.value.mobile,"onUpdate:modelValue":e[12]||(e[12]=t=>n.value.mobile=t)},null,8,["modelValue"]),o(l(d),{type:"text",label:"Sito web",modelValue:n.value.website,"onUpdate:modelValue":e[13]||(e[13]=t=>n.value.website=t)},null,8,["modelValue"]),o(l(d),{type:"text",label:"Gruppo cliente",modelValue:n.value.customer_group,"onUpdate:modelValue":e[14]||(e[14]=t=>n.value.customer_group=t),placeholder:"Commercial"},null,8,["modelValue"]),o(l(d),{type:"text",label:"Indirizzo",modelValue:n.value.address.line,"onUpdate:modelValue":e[15]||(e[15]=t=>n.value.address.line=t),class:"sm:col-span-2"},null,8,["modelValue"]),o(l(d),{type:"text",label:"Città ",modelValue:n.value.address.city,"onUpdate:modelValue":e[16]||(e[16]=t=>n.value.address.city=t)},null,8,["modelValue"]),o(l(d),{type:"text",label:"CAP",modelValue:n.value.address.pincode,"onUpdate:modelValue":e[17]||(e[17]=t=>n.value.address.pincode=t)},null,8,["modelValue"]),o(l(d),{type:"text",label:"Paese",modelValue:n.value.address.country,"onUpdate:modelValue":e[18]||(e[18]=t=>n.value.address.country=t)},null,8,["modelValue"]),o(l(d),{type:"text",label:"Territorio",modelValue:n.value.territory,"onUpdate:modelValue":e[19]||(e[19]=t=>n.value.territory=t),placeholder:"Italy"},null,8,["modelValue"]),b.value?(r(),g(l(z),{key:0,message:b.value,class:"sm:col-span-2"},null,8,["message"])):w("",!0)])]),actions:u(()=>[o(l(p),{variant:"solid",loading:M.value,onClick:ve},{default:u(()=>[c(x(h.value?"Salva":"Crea cliente"),1)]),_:1},8,["loading"])]),_:1},8,["modelValue","options"])],64)):w("",!0),U.value==="credit"?(r(),m(V,{key:1},[L.value?(r(),g(l(le),{key:0})):N.value?(r(),g(l(z),{key:1,message:N.value},null,8,["message"])):(r(),m(V,{key:2},[O.value.length?(r(),m("div",Fe,[s("table",qe,[e[36]||(e[36]=s("thead",{class:"bg-surface-gray-1 text-xs text-ink-gray-5"},[s("tr",null,[s("th",{class:"text-left font-medium px-3 py-2"},"Cliente"),s("th",{class:"text-right font-medium px-3 py-2"},"Limite credito"),s("th",{class:"text-right font-medium px-3 py-2"},"In sospeso"),s("th",{class:"text-right font-medium px-3 py-2"},"Disponibile"),s("th",{class:"px-3 py-2"})])],-1)),s("tbody",null,[(r(!0),m(V,null,Z(O.value,t=>(r(),m("tr",{key:t.name,class:"border-t border-outline-gray-1 hover:bg-surface-gray-1/50"},[s("td",Ge,x(t.customer_name),1),s("td",Me,x(Y(t.credit_limit)),1),s("td",Je,x(Y(t.outstanding)),1),s("td",He,[s("span",{class:ae(t.available>0?"text-green-600 font-medium":"text-red-500")},x(Y(t.available)),3)]),s("td",Qe,[o(l(p),{variant:"ghost",size:"sm",onClick:f=>ne(t)},{default:u(()=>[...e[35]||(e[35]=[c("Modifica",-1)])]),_:1},8,["onClick"])])]))),128))])])])):(r(),m("div",Be," Nessun cliente attivo in ERP. "))],64)),o(l(R),{modelValue:E.value,"onUpdate:modelValue":e[24]||(e[24]=t=>E.value=t),options:{title:I.value?`Credito â ${I.value.customer_name}`:"Credito",size:"md"}},{"body-content":u(()=>[s("div",Ke,[o(l(d),{type:"number",label:"Limite credito (â¬)",modelValue:y.value.credit_limit,"onUpdate:modelValue":e[21]||(e[21]=t=>y.value.credit_limit=t),placeholder:"0.00",description:"Importo massimo che il cliente può avere in fatture aperte."},null,8,["modelValue"]),s("div",We,[e[38]||(e[38]=s("div",{class:"text-xs font-medium text-ink-gray-5 mb-2"},"Ricarica credito (Payment Entry)",-1)),s("div",Xe,[o(l(d),{type:"number",label:"Importo (â¬)",modelValue:y.value.topup_amount,"onUpdate:modelValue":e[22]||(e[22]=t=>y.value.topup_amount=t),placeholder:"0.00",class:"flex-1"},null,8,["modelValue"]),o(l(p),{variant:"subtle",loading:C.value,onClick:ie,disabled:!y.value.topup_amount},{default:u(()=>
1[...e[37]||(e[37]=[c("Accredita",-1)])]),_:1},8,["loading","disabled"])]),o(l(d),{type:"text",label:"Nota",modelValue:y.value.topup_note,"onUpdate:modelValue":e[23]||(e[23]=t=>y.value.topup_note=t),placeholder:"Ricarica credito MMOS",class:"mt-2"},null,8,["modelValue"])]),k.value?(r(),g(l(z),{key:0,message:k.value},null,8,["message"])):w("",!0)])]),actions:u(()=>[o(l(p),{variant:"solid",loading:C.value,onClick:se},{default:u(()=>[...e[39]||(e[39]=[c("Salva limite",-1)])]),_:1},8,["loading"])]),_:1},8,["modelValue","options"])],64)):w("",!0)]))}};export{rt as default};
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.