1import{r as m,j as e}from"./vendor-react-DIA7vK4g.js";import{el as A,bb as d,bd as h,be as x,bg as u,bm as b,aa as L,X as B,R as I,S as T,bY as F,bZ as E,b_ as j,b$ as n,c0 as M,c1 as r,t as H,v as J,bF as Z,ba as $,b9 as R}from"./index-DtUVjOr4.js";import{C as z,a as q,b as Q}from"./collapsible-3B_X3N32.js";import{c as D}from"./export-utils-uEaPs01O.js";import{f,a as y,c as w}from"./billing-formatters-uNs-HNzw.js";import{P as X}from"./play-VbpcOJIM.js";import{H as Y}from"./hash-Cx5p7-Gk.js";import"./vendor-docs-BA9xu9ak.js";import"./vendor-utils-DWHjTIse.js";import"./vendor-supabase-_bOAMmkC.js";import"./index-C-uNlZii.js";const K=_=>{const[p,c]=_.split("-");return`${["Jan","Fev","Mar","Abr","Mai","Jun","Jul","Ago","Set","Out","Nov","Dez"][Number(c)-1]}/${p}`};function ie(){const[_,p]=m.useState(!1),[c,C]=m.useState(null),[v,G]=m.useState(new Set),O=async()=>{p(!0);try{let s=[],t=0;for(;;){const{data:o,error:i}=await $.from("invoices").select("id, invoice_number, omie_lancamento_id, omie_synced_at, created_at, due_date, total_amount, status, gateway, payment_method, customers(company_name, document)").not("omie_lancamento_id","is",null).not("omie_synced_at","is",null).order("omie_synced_at",{ascending:!1}).range(t,t+1e3-1);if(i)throw i;if(!o||o.length===0||(s=s.concat(o),o.length<1e3))break;t+=1e3}const l=s.map(o=>{var i,S;return{id:o.id,invoice_number:o.invoice_number,omie_lancamento_id:o.omie_lancamento_id,omie_synced_at:o.omie_synced_at,created_at:o.created_at,due_date:o.due_date,total_amount:o.total_amount,status:o.status,gateway:o.gateway,payment_method:o.payment_method,customer_name:((i=o.customers)==null?void 0:i.company_name)??null,customer_doc:((S=o.customers)==null?void 0:S.document)??null}});C(l),R.success(`Relatório gerado: ${l.length} faturas`)}catch(a){console.error(a),R.error("Erro: "+(a.message||"desconhecido"))}finally{p(!1)}},N=m.useMemo(()=>{if(!c)return[];const a=new Map;for(const s of c){const t=s.omie_synced_at.slice(0,7);a.has(t)||a.set(t,{month:t,label:K(t),count:0,total:0,invoices:[]});const l=a.get(t);l.count++,l.total+=Number(s.total_amount||0),l.invoices.push(s)}return Array.from(a.values()).sort((s,t)=>t.month.localeCompare(s.month))},[c]),g=m.useMemo(()=>c?{count:c.length,total:c.reduce((a,s)=>a+Number(s.total_amount||0),0),months:N.length}:{count:0,total:0,months:0},[c,N]),P=a=>{G(s=>{const t=new Set(s);return t.has(a)?t.delete(a):t.add(a),t})},V=a=>{const s=[{header:"Data Geração Omie",accessor:t=>y(t.omie_synced_at)},{header:"Nº Fatura",accessor:"invoice_number"},{header:"ID Omie",accessor:"omie_lancamento_id"},{header:"Cliente",accessor:"customer_name"},{header:"CNPJ/CPF",accessor:"customer_doc"},{header:"Vencimento",accessor:t=>w(t.due_date)},{header:"Valor",accessor:t=>Number(t.total_amount||0).toFixed(2).replace(".",",")},{header:"Status",accessor:"status"},{header:"Gateway",accessor:"gateway"},{header:"Método",accessor:"payment_method"}];D(a.invoices,s,`faturas-omie-${a.month}`)},k=()=>{if(!c)return;D(c,[{header:"Mês Geração",accessor:s=>s.omie_synced_at.slice(0,7)},{header:"Data Geração Omie",accessor:s=>y(s.omie_synced_at)},{header:"Nº Fatura",accessor:"invoice_number"},{header:"ID Omie",accessor:"omie_lancamento_id"},{header:"Cliente",accessor:"customer_name"},{header:"CNPJ/CPF",accessor:"customer_doc"},{header:"Vencimento",accessor:s=>w(s.due_date)},{header:"Valor",accessor:s=>Number(s.total_amount||0).toFixed(2).replace(".",",")},{header:"Status",accessor:"status"},{header:"Gateway",accessor:"gateway"},{header:"Método",accessor:"payment_method"}],"faturas-omie-completo")};return e.jsxs("div",{className:"container mx-auto py-6 px-4 max-w-7xl",children:[e.jsx(A,{title:"Faturas Geradas no Omie",backTo:"/bi-relatorios",backLabel:"B.I e Relatórios"}),e.jsxs(d,{className:"mb-6",children:[e.jsx(h,{children:e.jsx(x,{className:"text-base",children:"Sobre este relatório"})}),e.jsxs(u,{className:"space-y-3 text-sm text-muted-foreground",children:[e.jsxs("p",{children:["Lista todas as faturas com lançamento criado no Omie, agrupadas pelo ",e.jsx("strong",{children:"mês de geração"})," (data em que foram efetivamente sincronizadas com o Omie â campo ",e.jsx("code",{children:"omie_synced_at"}),"), independente da data de vencimento."]}),e.jsxs(b,{onClick:O,disabled:_,children:[_?e.jsx(L,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(X,{className:"h-4 w-4 mr-2"}),"Gerar Relatório"]})]})]}),c&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4 mb-6",children:[e.jsxs(d,{children:[e.jsxs(h,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium",children:"Total de Faturas"}),e.jsx(B,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(u,{children:e.jsx("div",{className:"text-2xl font-bold",children:g.count.toLocaleStr
1ing("pt-BR")})})]}),e.jsxs(d,{children:[e.jsxs(h,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium",children:"Valor Total"}),e.jsx(I,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(u,{children:e.jsx("div",{className:"text-2xl font-bold",children:f(g.total)})})]}),e.jsxs(d,{children:[e.jsxs(h,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium",children:"Meses"}),e.jsx(Y,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(u,{children:e.jsx("div",{className:"text-2xl font-bold",children:g.months})})]})]}),e.jsx("div",{className:"flex justify-end mb-4",children:e.jsxs(b,{variant:"outline",onClick:k,children:[e.jsx(T,{className:"h-4 w-4 mr-2"})," Exportar tudo"]})}),e.jsxs(d,{children:[e.jsx(h,{children:e.jsx(x,{className:"text-base",children:"Resumo Mensal"})}),e.jsx(u,{className:"p-0",children:e.jsxs(F,{children:[e.jsx(E,{children:e.jsxs(j,{children:[e.jsx(n,{className:"w-12"}),e.jsx(n,{children:"Mês"}),e.jsx(n,{className:"text-right",children:"Qtd Faturas"}),e.jsx(n,{className:"text-right",children:"Valor Total"}),e.jsx(n,{className:"text-right",children:"Ticket Médio"}),e.jsx(n,{className:"w-32"})]})}),e.jsx(M,{children:N.map(a=>e.jsx(z,{asChild:!0,open:v.has(a.month),onOpenChange:()=>P(a.month),children:e.jsxs(e.Fragment,{children:[e.jsx(q,{asChild:!0,children:e.jsxs(j,{className:"cursor-pointer hover:bg-muted/50",children:[e.jsx(r,{children:v.has(a.month)?e.jsx(H,{className:"h-4 w-4"}):e.jsx(J,{className:"h-4 w-4"})}),e.jsx(r,{className:"font-medium",children:a.label}),e.jsx(r,{className:"text-right",children:a.count.toLocaleString("pt-BR")}),e.jsx(r,{className:"text-right font-medium",children:f(a.total)}),e.jsx(r,{className:"text-right text-muted-foreground",children:f(a.total/a.count)}),e.jsx(r,{className:"text-right",children:e.jsxs(b,{size:"sm",variant:"ghost",onClick:s=>{s.stopPropagation(),V(a)},children:[e.jsx(T,{className:"h-3 w-3 mr-1"})," CSV"]})})]})}),e.jsx(Q,{asChild:!0,children:e.jsx(j,{children:e.jsx(r,{colSpan:6,className:"p-0 bg-muted/30",children:e.jsx("div",{className:"p-4",children:e.jsxs(F,{children:[e.jsx(E,{children:e.jsxs(j,{children:[e.jsx(n,{children:"Data Geração"}),e.jsx(n,{children:"Nº Fatura"}),e.jsx(n,{children:"ID Omie"}),e.jsx(n,{children:"Cliente"}),e.jsx(n,{children:"Vencimento"}),e.jsx(n,{className:"text-right",children:"Valor"}),e.jsx(n,{children:"Status"})]})}),e.jsx(M,{children:a.invoices.map(s=>e.jsxs(j,{children:[e.jsx(r,{className:"text-xs",children:y(s.omie_synced_at)}),e.jsx(r,{className:"font-mono text-xs",children:s.invoice_number||"â"}),e.jsx(r,{className:"font-mono text-xs",children:s.omie_lancamento_id}),e.jsx(r,{className:"text-xs",children:s.customer_name||"â"}),e.jsx(r,{className:"text-xs",children:w(s.due_date)}),e.jsx(r,{className:"text-right text-xs",children:f(Number(s.total_amount||0))}),e.jsx(r,{children:e.jsx(Z,{variant:"outline",className:"text-xs",children:s.status})})]},s.id))})]})})})})})]})},a.month))})]})})]})]})]})}export{ie 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.