1import{j as e}from"./vendor-radix-j51FbGWj.js";import{r as u,b as he}from"./vendor-react-CzwWGMlW.js";import{C as F,f as X,g as Z,b as P,I as G,B as v,a as W,L as A,T as pe,e as M,n as T}from"./index-CMgi7Nl8.js";import{D as ge,B as be,C as je,A as fe,e as Ne,f as ye,a as ve,b as we,d as Ce}from"./vendor-charts-CCcLMIy6.js";import{T as ce,a as ie,b as B,c as $,d as oe,e as D}from"./table-DvS7udR6.js";import{aG as Se,X as ne,aF as le,am as se,N as ke,e as De,b0 as Ie,a9 as Ae,bw as Le,a0 as de,a1 as Te,A as $e,aC as Fe,a$ as Pe,b as ze,al as Ee,B as Me}from"./vendor-icons-CaDFxAI1.js";import{S as Y,a as Q,b as K,c as ee,d as H}from"./select-DjZg9c5p.js";import"./vendor-utils-DYobRrOj.js";const L=(s,h={})=>{if(s==null)return"-";const l={style:"currency",currency:"RUB",minimumFractionDigits:h.compact?0:2,maximumFractionDigits:h.compact?0:2};try{return h.compact&&Math.abs(s)>=1e3,new Intl.NumberFormat("ru-RU",l).format(s)}catch{return s}};je.register(fe,Ne,ye,ve,we,Ce);const Be=({data:s})=>{const h=u.useMemo(()=>s!=null&&s.topContractsDebt?{labels:s.topContractsDebt.map(i=>i.contractNumber),datasets:[{data:s.topContractsDebt.map(i=>Math.abs(i.debt)),backgroundColor:s.topContractsDebt.map((i,r)=>{if(i.isNegative){const o=["rgba(40, 167, 69, 0.85)","rgba(32, 150, 60, 0.85)","rgba(25, 135, 50, 0.85)","rgba(20, 120, 40, 0.85)","rgba(15, 105, 30, 0.85)"];return o[r%o.length]}else{const o=["rgba(220, 53, 69, 0.85)","rgba(200, 45, 60, 0.85)","rgba(180, 40, 55, 0.85)","rgba(160, 35, 50, 0.85)","rgba(140, 30, 45, 0.85)"];return o[r%o.length]}}),borderColor:s.topContractsDebt.map((i,r)=>i.isNegative?"rgba(40, 167, 69, 1)":"rgba(220, 53, 69, 1)"),borderWidth:1}]}:null,[s]),l=u.useMemo(()=>s!=null&&s.debtDistribution?{labels:Object.keys(s.debtDistribution),datasets:[{data:Object.values(s.debtDistribution).map(i=>Math.abs(i)),backgroundColor:Object.keys(s.debtDistribution).map((i,r)=>`hsla(${120-120*r/(Object.keys(s.debtDistribution).length-1)}, 70%, 50%, 0.6)`),borderColor:"rgba(0,0,0,0.1)",borderWidth:1}]}:null,[s]),w={responsive:!0,maintainAspectRatio:!1,cutout:"65%",plugins:{legend:{position:"bottom",labels:{boxWidth:12,padding:15,font:{size:12},generateLabels:i=>{const r=i.data;return r.labels.length&&r.datasets.length?r.labels.map((o,p)=>{var f,b;const m=r.datasets[0].data[p],C=(b=(f=s==null?void 0:s.topContractsDebt)==null?void 0:f[p])==null?void 0:b.isNegative;return{text:`${o} (${L(m)}${C?" - пÑедоплаÑа":" - долг"})`,fillStyle:i.data.datasets[0].backgroundColor[p],hidden:isNaN(m)||m===0,index:p}}):[]}}},tooltip:{callbacks:{label:function(i){var p,m;const r=i.raw,o=(m=(p=s==null?void 0:s.topContractsDebt)==null?void 0:p[i.dataIndex])==null?void 0:m.isNegative;return`${i.label}: ${L(r)}${o?" (пÑедоплаÑа)":" (долг)"}`}}}}},j={responsive:!0,maintainAspectRatio:!1,plugins:{legend:{display:!1},tooltip:{callbacks:{label:function(i){return L(i.raw)}}}},scales:{y:{beginAtZero:!0,ticks:{callback:function(i){return L(i)}}}}};return s?e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 mb-6",children:[e.jsxs(F,{children:[e.jsx(X,{children:e.jsx(Z,{className:"text-lg text-muted-foreground font-medium",children:"Топ 5 договоÑов по задолженноÑÑи"})}),e.jsx(P,{children:e.jsx("div",{className:"h-[350px] w-full",children:h&&e.jsx(ge,{data:h,options:w})})})]}),e.jsxs(F,{children:[e.jsx(X,{children:e.jsx(Z,{className:"text-lg text-muted-foreground font-medium",children:"РаÑпÑеделение задолженноÑÑи"})}),e.jsx(P,{children:e.jsx("div",{className:"h-[350px] w-full",children:l&&e.jsx(be,{data:l,options:j})})})]})]}):null},Re=({data:s,activeMonths:h,onMonthClick:l,onAddFilter:w,onRemoveFilter:j,activeFilters:i,searchQuery:r,onSearchChange:o,debtFilter:p,setDebtFilter:m})=>{const[C,f]=u.useState(new Set),b=t=>{const x=new Set(C);x.has(t)?x.delete(t):x.add(t),f(x)},V=t=>new Date(t+"-01").toLocaleString("ru-RU",{month:"short"}).replace(".",""),N=(t,x)=>{if(!x||!t)return t;const k=t.toString().split(new RegExp(`(${x})`,"gi"));return e.jsx(e.Fragment,{children:k.map((n,g)=>n.toLowerCase()===x.toLowerCase()?e.jsx("span",{className:"bg-yellow-200 dark:bg-yellow-800 rounded px-0.5",children:n},g):n)})};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4 items-start sm:items-center justify-between",children:[e.jsxs("div",{className:"relative w-full sm:w-96",children:[e.jsx(Se,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(G,{placeholder:"ÐоиÑк по договоÑÑ Ð¸Ð»Ð¸ конÑÑагенÑÑ...",className:"pl-9",value:r,onChange:t=>o(t.target.value)}),r&&e.jsx("button",{onClick:()=>o(""),className:"absolute right-2.5 top-2.5 text-muted-foreground hover:text-foreground",children:e.jsx(ne,{className:"w-4 h-4"})})]}),e.jsx("div",{className:"flex items-center gap-2 bg-muted/50 p-1 rounded-lg",children:[{id:"all",label:"ÐÑе"},{id:"debt",label:"Ðолжники",icon:le,color:"text-destructive"},{id:"prepayment",label:"ÐÑедоплаÑа",icon:se,color:"text-green-600"},{id:"none",label:"Ðез долга"}].map(t=>e.jsxs(v,{variant:p===t.id?"secondary":"ghost",size:"sm",onClick:()=>m(t.id),className:`h-8 px-3 ${p===t.id?"bg-background shadow-sm":""}`,children:[t.icon&&e.jsx(t.icon,{className:`w-3 h-3 mr-1.5 ${t.color}`}),t.label]},t.id))})]}),i.length>0&&e.jsxs("div",{className:"flex flex-wrap gap-2",children:[i.map((t,x)=>e.jsxs(W,{variant:"secondary",className:"pl-2 pr-1 py-1 flex items-center gap-1",children:[t,e.jsx("button",{onClick:()=>j(t),className:"ml-1 hover:bg-muted rounded-full p-0.5",children:e.jsx(ne,{className:"w-3 h-3"})})]},x)),e.jsx(v,{variant:"link",size:"sm",className:"h-6 px-2 text-xs",onClick:()=>i.forEach(t=>j(t)),children:"ÐÑиÑÑиÑÑ Ð²Ñе"})]})]}),e.jsx("div",{className:"rounded-md border overflow-x-auto",children:e.jsxs(ce,{children:[e.jsx(ie,{children:e.jsxs(B,{className:"bg-muted/50",children:[e.jsx($,{className:"w-[300px]",children:"ÐоговоÑ"}),Array.from(h).map(t=>e.jsx($,{className:"text-center min-w-[80px] px-1 text-xs uppercase text-muted-foreground",children:V(t)},t)),e.jsx($,{className:"text-right w-[150px]",children:"ÐÑог"})]})}),e.jsx(oe,{children:s.length>0?s.map(t=>e.jsxs(he.Fragment,{children:[e.jsxs(B,{className:"group hover:bg-muted/30 transition-colors",children:[e.jsx(D,{className:"font-medium align-top",children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{onClick:()=>b(t.contractId),className:"mt-0.5 text-muted-foreground hover:text-foreground transition-colors",children:C.has(t.contractId)?e.jsx(ke,{className:"w-4 h-4"}):e.jsx(De,{className:"w-4 h-4"})}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-sm font-semibold text-foreground",children:N(t.contract,r)}),e.jsx("span",{className:"text-xs text-muted-foreground",children:N(t.contractNumber,r)})]})]})}),Array.from(h).map(x=>{var n;const k=((n=t.monthlyDebt)==null?void 0:n[x])||0;return e.jsx(D,{className:`text-center p-0 align-top ${k!==0?"cursor-pointer hover:bg-muted/50":""}`,onClick:()=>k!==0&&l(x,t),children:k!==0?e.jsxs("div",{className:"h-full py-3 flex flex-col items-center justify-center gap-0.5",children:[e.jsx("span",{className:"text-xs font-medium",children:L(k,{compact:!0})}),k>0?e.jsx(le,{className:"w-3 h-3 text-destructive"}):e.jsx(se,{className:"w-3 h-3 text-green-600"})]}):e.jsx("div",{className:"py-3 text-center text-muted-foreground
1/30",children:"â"})},x)}),e.jsx(D,{className:"text-right align-top",children:e.jsx("div",{className:"flex flex-col items-end",children:e.jsx("span",{className:`font-bold ${(t.currentDebt||t.beginningDebt)>0?"text-destructive":"text-green-600"}`,children:L(t.currentDebt||t.beginningDebt)})})})]}),C.has(t.contractId)&&e.jsx(B,{className:"bg-muted/20 hover:bg-muted/20",children:e.jsx(D,{colSpan:h.size+2,className:"p-4",children:e.jsxs("div",{className:"flex gap-8 text-sm pl-8",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsxs("span",{className:"text-muted-foreground flex items-center gap-1.5",children:[e.jsx(Ie,{className:"w-3.5 h-3.5"})," ÐокÑпаÑелÑ"]}),e.jsx("span",{className:"font-medium hover:text-primary cursor-pointer border-b border-dashed border-transparent hover:border-primary transition-colors",onClick:()=>w(t.buyer),children:N(t.buyer,r)})]}),e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsxs("span",{className:"text-muted-foreground flex items-center gap-1.5",children:[e.jsx(Ae,{className:"w-3.5 h-3.5"})," ÐÑодавеÑ"]}),e.jsx("span",{className:"font-medium hover:text-primary cursor-pointer border-b border-dashed border-transparent hover:border-primary transition-colors",onClick:()=>w(t.seller),children:N(t.seller,r)})]})]})})})]},t.contractId)):e.jsx(B,{children:e.jsx(D,{colSpan:h.size+2,className:"h-24 text-center text-muted-foreground",children:"ÐиÑего не найдено"})})})]})})]})},_e=({onSubmit:s,onCancel:h,contracts:l,initialData:w=null,isEditMode:j=!1})=>{const[i,r]=u.useState(!1),[o,p]=u.useState(!1),[m,C]=u.useState(w||{contractId:"",date:new Date().toISOString().slice(0,10),amount:"",type:"income",description:""}),[f,b]=u.useState([{contractId:"",amount:"",description:""}]),V=async n=>{n.preventDefault(),r(!0);try{o?await s({type:"group",date:m.date,operationType:m.type,payments:f}):await s({type:"single",...m})}finally{r(!1)}},N=(n,g,d)=>{const R=[...f];R[n][g]=d,b(R)},t=()=>{b([...f,{contractId:"",amount:"",description:""}])},x=n=>{f.length>1&&b(f.filter((g,d)=>d!==n))},k=async n=>{const g=n.target.files[0];if(g)try{const _=(await g.text()).split(` 2`).filter(z=>z.trim()).map(z=>{const[E,O]=z.split(";"),U=l.find(J=>J.number.toLowerCase().includes(E==null?void 0:E.trim().toLowerCase()));return{contractId:(U==null?void 0:U.id)||"",amount:parseFloat(O==null?void 0:O.replace(/\s/g,"").replace(",","."))||"",description:`ÐмпоÑÑ Ð¸Ð· CSV: ${E}`}}).filter(z=>z.contractId);_.length>0&&(b(_),p(!0))}catch(d){console.error(d)}};return e.jsxs(F,{className:"mb-4 border-2 border-primary/10",children:[e.jsx(X,{children:e.jsx(Z,{children:j?"РедакÑиÑование плаÑежа":"ÐовÑй плаÑеж"})}),e.jsx(P,{children:e.jsxs("form",{onSubmit:V,className:"space-y-4",children:[!j&&e.jsxs("div",{className:"flex items-center justify-between mb-4 bg-muted/50 p-3 rounded-md",children:[e.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[e.jsx("input",{type:"checkbox",className:"w-4 h-4 rounded border-gray-300 text-primary focus:ring-primary",checked:o,onChange:n=>p(n.target.checked)}),e.jsx("span",{className:"font-medium",children:"ÐÑÑпповой плаÑеж"})]}),o&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(A,{htmlFor:"csv-upload",className:"cursor-pointer inline-flex items-center gap-2 text-sm text-primary hover:underline",children:[e.jsx(Le,{className:"w-4 h-4"}),"ÐагÑÑзиÑÑ CSV"]}),e.jsx("input",{id:"csv-upload",type:"file",accept:".csv",className:"hidden",onChange:k,onClick:n=>n.target.value=null})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"ÐаÑа опеÑаÑии"}),e.jsx(G,{type:"date",value:m.date,onChange:n=>C({...m,date:n.target.value}),required:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Тип опеÑаÑии"}),e.jsxs(Y,{value:m.type,onValueChange:n=>C({...m,type:n}),children:[e.jsx(Q,{children:e.jsx(K,{})}),e.jsxs(ee,{children:[e.jsx(H,{value:"income",children:"ÐоÑÑÑпление"}),e.jsx(H,{value:"expense",children:"СпиÑание"}),e.jsx(H,{value:"balance",children:"СалÑдо (ÐоÑÑекÑиÑовка)"})]})]})]})]}),o?e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"rounded-md border p-4 bg-muted/20",children:f.map((n,g)=>e.jsxs("div",{className:"flex gap-4 items-end mb-4 last:mb-0",children:[e.jsxs("div",{className:"flex-1 min-w-[200px]",children:[e.jsxs(A,{className:"text-xs text-muted-foreground mb-1 block",children:["ÐÐ¾Ð³Ð¾Ð²Ð¾Ñ #",g+1]}),e.jsxs(Y,{value:n.contractId,onValueChange:d=>N(g,"contractId",d),children:[e.jsx(Q,{children:e.jsx(K,{placeholder:"ÐÑбеÑиÑе договоÑ"})}),e.jsx(ee,{children:l.map(d=>
2e.jsxs(H,{value:d.id,children:[d.number," (",d.source==="fgis"?"ФÐÐС":"Ðн.",")"]},d.id))})]})]}),e.jsxs("div",{className:"w-[150px]",children:[e.jsx(A,{className:"text-xs text-muted-foreground mb-1 block",children:"СÑмма"}),e.jsx(G,{type:"number",step:"0.01",value:n.amount,onChange:d=>N(g,"amount",d.target.value),placeholder:"0.00"})]}),e.jsxs("div",{className:"flex-1",children:[e.jsx(A,{className:"text-xs text-muted-foreground mb-1 block",children:"ÐпиÑание"}),e.jsx(G,{value:n.description,onChange:d=>N(g,"description",d.target.value),placeholder:"ÐÑимеÑание"})]}),e.jsx(v,{type:"button",variant:"ghost",size:"icon",className:"text-destructive hover:text-destructive/90",onClick:()=>x(g),disabled:f.length===1,children:e.jsx(de,{className:"w-4 h-4"})})]},g))}),e.jsxs(v,{type:"button",variant:"outline",onClick:t,className:"w-full border-dashed",children:[e.jsx(Te,{className:"w-4 h-4 mr-2"})," ÐобавиÑÑ ÑÑÑокÑ"]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"ÐоговоÑ"}),e.jsxs(Y,{value:m.contractId,onValueChange:n=>C({...m,contractId:n}),disabled:j,children:[e.jsx(Q,{children:e.jsx(K,{placeholder:"ÐÑбеÑиÑе договоÑ"})}),e.jsx(ee,{children:l.map(n=>e.jsx(H,{value:n.id,children:n.number},n.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"СÑмма"}),e.jsx(G,{type:"number",value:m.amount,onChange:n=>C({...m,amount:n.target.value}),step:"0.01",required:!0})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"ÐпиÑание"}),e.jsx(pe,{value:m.description,onChange:n=>C({...m,description:n.target.value}),rows:3})]})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4 border-t",children:[e.jsx(v,{type:"button",variant:"outline",onClick:h,children:"ÐÑмена"}),e.jsx(v,{type:"submit",disabled:i,children:i?"СоÑ
Ñанение...":j?"ÐбновиÑÑ":"СоздаÑÑ"})]})]})})]})},Oe=({monthData:s,onBack:h,onEdit:l,onDelete:w,onAddPayment:j})=>{if(!s)return null;const i=new Date(s.monthKey+"-01").toLocaleString("ru-RU",{month:"long",year:"numeric"});return e.jsxs("div",{className:"space-y-6 animate-in fade-in slide-in-from-bottom-4 duration-500",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs(v,{variant:"outline",size:"sm",onClick:h,className:"gap-2",children:[e.jsx($e,{className:"w-4 h-4"})," Ðазад"]}),e.jsxs("h2",{className:"text-2xl font-semibold tracking-tight first-letter:capitalize",children:["Ðвижение ÑÑедÑÑв: ",i]})]}),e.jsxs(v,{onClick:j,children:[e.jsx(se,{className:"w-4 h-4 mr-2"})," ÐобавиÑÑ Ð¿Ð»Ð°Ñеж"]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx(F,{className:"bg-muted/30",children:e.jsxs(P,{className:"pt-6",children:[e.jsxs("h3",{className:"font-semibold mb-3 flex items-center gap-2",children:[e.jsx("span",{className:"w-1 h-4 bg-primary rounded-full"}),"ÐнÑоÑмаÑÐ¸Ñ Ð¾ договоÑе"]}),e.jsxs("div",{className:"space-y-2 text-sm",children:[e.jsxs("div",{className:"grid grid-cols-[120px_1fr]",children:[e.jsx("span",{className:"text-muted-foreground",children:"ÐомеÑ:"}),e.jsx("span",{className:"font-medium",children:s.contractNumber})]}),e.jsxs("div",{className:"grid grid-cols-[120px_1fr]",children:[e.jsx("span",{className:"text-muted-foreground",children:"ÐокÑпаÑелÑ:"}),e.jsx("span",{children:s.buyer||"â"})]}),e.jsxs("div",{className:"grid grid-cols-[120px_1fr]",children:[e.jsx("span",{className:"text-muted-foreground",children:"ÐÑодавеÑ:"}),e.jsx("span",{children:s.seller||"â"})]})]})]})}),e.jsx(F,{className:"bg-muted/30",children:e.jsxs(P,{className:"pt-6",children:[e.jsxs("h3",{className:"font-semibold mb-3 flex items-center gap-2",children:[e.jsx("span",{className:"w-1 h-4 bg-primary rounded-full"}),"ÐÐ°Ð»Ð°Ð½Ñ Ð·Ð° пеÑиод"]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex justify-between items-center border-b pb-2",children:[e.jsx("span",{className:"text-muted-foreground",children:"ÐаÑалÑнÑй оÑÑаÑок:"}),e.jsxs("div",{className:"text-right",children:[e.jsx("div",{className:`font-semibold ${s.beginningDebt>0?"text-destructive":"text-green-600"}`,children:L(s.beginningDebt)}),e.jsx(W,{variant:s.beginningDebt>
20?"destructive":"outline",className:"text-[10px] h-5",children:s.beginningDebt>0?"Ðолг":"ÐÑедоплаÑа"})]})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-muted-foreground",children:"ÐонеÑнÑй оÑÑаÑок:"}),e.jsxs("div",{className:"text-right",children:[e.jsx("div",{className:`font-semibold ${s.currentDebt>0?"text-destructive":"text-green-600"}`,children:L(s.currentDebt)}),e.jsx(W,{variant:s.currentDebt>0?"destructive":"outline",className:"text-[10px] h-5",children:s.currentDebt>0?"Ðолг":"ÐÑедоплаÑа"})]})]})]})]})})]}),e.jsx(F,{children:e.jsx(P,{className:"p-0",children:e.jsxs(ce,{children:[e.jsx(ie,{children:e.jsxs(B,{children:[e.jsx($,{className:"w-[120px]",children:"ÐаÑа"}),e.jsx($,{className:"w-[120px] text-center",children:"Тип"}),e.jsx($,{className:"text-right",children:"СÑмма"}),e.jsx($,{children:"ÐпиÑание"}),e.jsx($,{className:"w-[100px] text-right",children:"ÐейÑÑвиÑ"})]})}),e.jsx(oe,{children:s.transactions&&s.transactions.length>0?s.transactions.map(r=>e.jsxs(B,{children:[e.jsx(D,{children:new Date(r.date).toLocaleDateString()}),e.jsx(D,{className:"text-center",children:e.jsx(W,{variant:"secondary",className:r.type==="income"?"bg-green-100 text-green-800 hover:bg-green-200":r.type==="expense"?"bg-blue-100 text-blue-800 hover:bg-blue-200":"bg-gray-100 text-gray-800",children:r.type==="income"?"ÐоÑÑÑпление":r.type==="expense"?"СпиÑание":r.type==="balance"?"СалÑдо":r.type})}),e.jsx(D,{className:"text-right font-mono",children:L(r.amount)}),e.jsx(D,{className:"max-w-[300px] truncate",title:r.description,children:r.description||"â"}),e.jsx(D,{className:"text-right",children:e.jsxs("div",{className:"flex justify-end gap-1",children:[e.jsx(v,{variant:"ghost",size:"icon",className:"h-8 w-8",onClick:()=>l(r),children:e.jsx(Fe,{className:"w-4 h-4"})}),e.jsx(v,{variant:"ghost",size:"icon",className:"h-8 w-8 text-destructive hover:text-destructive",onClick:()=>w(r._id),children:e.jsx(de,{className:"w-4 h-4"})})]})})]},r.id||r._id)):e.jsx(B,{children:e.jsx(D,{colSpan:5,className:"text-center py-8 text-muted-foreground",children:"ÐÐµÑ Ð¾Ð¿ÐµÑаÑий за вÑбÑаннÑй пеÑиод"})})})]})})})]})},Je=({data:s,onUpdate:h})=>{const[l,w]=u.useState(null),[j,i]=u.useState([]),[r,o]=u.useState("table"),[p,m]=u.useState(!0),[C,f]=u.useState(!1),[b,V]=u.useState(null),[N,t]=u.useState(null),[x,k]=u.useState("all"),[n,g]=u.useState(""),[d,R]=u.useState([]),[_,z]=u.useState(""),[E,O]=u.useState(!1);u.useEffect(()=>{s&&w(s),(async()=>{try{const c=await M.get("/api/accounting/contracts/list");i(c.data)}catch(c){console.error("Error fetching contracts:",c)}})()},[s]);const U=u.useMemo(()=>{if(!(l!=null&&l.debtSummary))return new Set;const a=new Set;
2return l.debtSummary.forEach(c=>{Object.entries(c.monthlyDebt||{}).forEach(([y,S])=>{S!==0&&a.add(y)})}),new Set(Array.from(a).sort())},[l]),J=u.useMemo(()=>{if(!(l!=null&&l.debtSummary))return[];let a=l.debtSummary;switch(x){case"debt":a=a.filter(c=>(c.currentDebt||c.beginningDebt)>0);break;case"prepayment":a=a.filter(c=>(c.currentDebt||c.beginningDebt)<0);break;case"none":a=a.filter(c=>(c.currentDebt||c.beginningDebt)===0);break}if(n.trim()){const c=n.toLowerCase();a=a.filter(y=>{var S,I,q,re;return((S=y.contract)==null?void 0:S.toLowerCase().includes(c))||((I=y.contractNumber)==null?void 0:I.toLowerCase().includes(c))||((q=y.buyer)==null?void 0:q.toLowerCase().includes(c))||((re=y.seller)==null?void 0:re.toLowerCase().includes(c))})}return d.length>0&&(a=a.filter(c=>d.some(y=>{var S,I;return((S=c.buyer)==null?void 0:S.toLowerCase().includes(y.toLowerCase()))||((I=c.seller)==null?void 0:I.toLowerCase().includes(y.toLowerCase()))}))),a},[l,x,n,d]),te=async()=>{if(typeof h=="function")await h();else try{const a=await M.get("/api/accounting/finance/data");w(a.data)}catch(a){console.error(a)}},me=(a,c)=>{var S,I,q;const y={monthKey:a,contractId:c.contractId,contractNumber:c.contractNumber,buyer:c.buyer,seller:c.seller,beginningDebt:c.beginningDebt,currentDebt:((S=c.monthlyDebt)==null?void 0:S[a])||0,transactions:((q=(I=l==null?void 0:l.transactions)==null?void 0:I[a])==null?void 0:q[c.contractId])||[]};V(y),o("details")},ue=async a=>{try{a.type==="group"?(await M.post("/api/accounting/finance/payments/bulk",{payments:a.payments,date:a.date,operationType:a.operationType}),T.success(`Ðобавлено ${a.payments.length} плаÑежей`)):N?(await M.put(`/api/accounting/finance/${N._id}`,a),T.success("ÐлаÑеж обновлен")):(await M.post("/api/accounting/finance",a),T.success("ÐлаÑеж Ñоздан")),o(b?"details":"table"),t(null),await te(),b&&o("table")}catch(c){console.error(c),T.error("ÐÑибка ÑоÑ
ÑанениÑ")}},xe=async a=>{if(window.confirm("УдалиÑÑ Ð¿Ð»Ð°Ñеж?"))try{await M.delete(`/api/accounting/finance/${a}`),T.success("ÐлаÑеж Ñдален"),await te(),o("table")}catch{T.error("ÐÑибка ÑдалениÑ")}},ae=async()=>{var a;if(_){O(!0);try{const c={metrics:{balance:(l==null?void 0:l.balance)||0,totalIncome:(l==null?void 0:l.totalIncome)||0,totalDebt:((a=l==null?void 0:l.debtSummary)==null?void 0:a.reduce((S,I)=>S+I.currentDebt,0))||0},topDebtors:(l==null?void 0:l.topContractsDebt)||[]},y=await M.post("/api/ai/analyze-finance",{query:_,context:c});T.success("Ðнализ вÑполнен (Ñм. конÑолÑ)"),console.log(y.data)}catch{T.error("ÐÑибка AI")}finally{O(!1)}}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4",children:[e.jsxs("h2",{className:"text-2xl font-bold tracking-tight flex items-center gap-2",children:[e.jsx(Pe,{className:"w-6 h-6 text-primary"}),"ÐодÑÐ»Ñ ÑинанÑов"]}),e.jsx("div",{className:"flex gap-2 w-full sm:w-auto",children:r==="table"&&e.jsxs(e.Fragment,{children:[e.jsxs(v,{variant:"outline",size:"sm",onClick:()=>m(!p),className:"hidden sm:flex",children:[p?e.jsx(ze,{className:"w-4 h-4 mr-2"}):e.jsx(Ee,{className:"w-4 h-4 mr-2"}),p?"СкÑÑÑÑ Ð³ÑаÑики":"ÐоказаÑÑ Ð³ÑаÑики"]}),e.jsx(v,{onClick:()=>{t(null),o("form")},children:"+ ÐлаÑеж"})]})})]}),r==="form"&&e.jsx(_e,{contracts:j,onSubmit:ue,onCancel:()=>{t(null),o("table")},initialData:N,isEditMode:!!N}),r==="details"&&b&&e.jsx(Oe,{monthData:b,onBack:()=>o("table"),onAddPayment:()=>{t(null),o("form")},onEdit:a=>{t({...a,contractId:a.contractId||b.contractId,amount:a.amount,type:a.type,date:a.date.split("T")[0]}),o("form")},onDelete:xe}),r==="table"&&e.jsxs("div",{className:"space-y-6 animate-in fade-in duration-500",children:[p&&e.jsx(Be,{data:l}),e.jsx(F,{children:e.jsx(P,{className:"p-6",children:e.jsx(Re,{data:J,activeMonths:U,onMonthClick:me,activeFilters:d,onAddFilter:a=>{d.includes(a)||R([...d,a])},onRemoveFilter:a=>R(d.filter(c=>c!==a)),searchQuery:n,onSearchChange:g,debtFilter:x,setDebtFilter:k})})}),e.jsxs(F,{className:"bg-gradient-to-r from-indigo-50 to-blue-50 dark:from-indigo-950/20 dark:to-blue-950/20 border-blue-100 dar
2k:border-blue-900",children:[e.jsx(X,{children:e.jsxs(Z,{className:"flex items-center gap-2 text-base",children:[e.jsx(Me,{className:"w-5 h-5 text-indigo-600"}),"AI ФинанÑовÑй аналиÑик"]})}),e.jsx(P,{children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx(G,{placeholder:"ÐапÑимеÑ: ÐÑо главнÑе должники в ÑÑом меÑÑÑе?",value:_,onChange:a=>z(a.target.value),onKeyDown:a=>a.key==="Enter"&&ae()}),e.jsx(v,{onClick:ae,disabled:E,children:E?"ÐÑмаÑ...":"СпÑоÑиÑÑ"})]})})]})]})]})};export{Je 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.