PageSourceSearch

https://vps.lovable.app/assets/ProformaInvoiceModule-Dq0jN5NO.js

js vps.lovable.app collected 2026-10-03 03:46:18 UTC 30,034 bytes, 14 lines download raw bytes

1import{j as e}from"./vendor-ui-CBQCP6gh.js";import{r as j}from"./vendor-react-DfcZEr2I.js";import{g as $,h as se,s as y,B as I,X as he,v as xe,u as pe,C as ee,w as fe}from"./index-Cwf-ABv9.js";import{B as _e}from"./badge-DLZJ1gAn.js";import{a as ae}from"./Index-x-pqp9vo.js";import{D as re,a as ie,b as ne,c as oe}from"./dialog-Pxxuj8iz.js";import{I as i}from"./input-C-yFy7sH.js";import{L as o}from"./label-BoaB4PVQ.js";import{S as M,a as z,b as H,c as V,d as g}from"./select-eBZTo8h_.js";import{T as te}from"./textarea-Cgb1-tzw.js";import{P as Z}from"./plus-B-uf7Nbg.js";import{T as le}from"./dropdown-menu-Dd9CT2Dp.js";import{P as ce}from"./ProformaTemplateRenderer-CUwjb5t6.js";import{D as de}from"./download-45_sJ_lI.js";import ve from"./html2canvas.esm-ClQmKH__.js";import{E as ge}from"./jspdf.es.min-CmZd-r23.js";import{F as je}from"./trending-up-CZzNXXJU.js";import{E as ye}from"./eye-BX8k0jjR.js";import{S as we}from"./square-pen-DLRpPTvX.js";import{A as Ne}from"./arrow-right-Cj0aNaBb.js";import"./vendor-charts-RipllrNb.js";import"./vendor-query-BKnZyYL1.js";import"./index-BdQq_4o_.js";import"./circle-check-DMpE3KMB.js";import"./users-BVjXPYl4.js";import"./book-open-B3auohZU.js";import"./sparkles-B6guJbdn.js";import"./file-stack-CxaFgF_4.js";import"./exportflow-logo-9CsUrxs9.js";import"./package-BVQTrb_d.js";import"./sun-DI1zEr93.js";import"./check-CVGOkdAd.js";import"./clock-CV12RC3U.js";import"./chevron-down-DZd36-US.js";import"./index-Dwra9pae.js";import"./numberToWords-B6tPevwT.js";import"./WatermarkOverlay-Bx69rm5r.js";const me=()=>{const[w,P]=j.useState([]),[u,E]=j.useState(!1),[F,N]=j.useState(0),{toast:p}=$(),{user:f,loading:k,isAuthenticated:a}=se(),l=async(h=!1)=>{try{if(!h&&(!a||!f))return;E(!0);const{data:{session:m}}=await y.auth.getSession();if(!m)throw new Error("No active session");const{data:c,error:C}=await y.from("proforma_invoices").select(`
2          *,
3          party:parties(
4            id,
5            name,
6            email,
7            company_name,
8            gst_number,
9            address
10          )
11        `).order("created_at",{ascending:!1});if(C)throw C;P(c||[]),N(0)}catch(m){if(m.message?.includes("session")&&F<2){N(c=>c+1),setTimeout(()=>l(!0),1e3);return}p({title:"Error",description:`Failed to fetch proforma invoices: ${m.message||"Unknown error"}`,variant:"destructive"})}finally{E(!1)}},s=async h=>{try{if(!f)throw new Error("Not authenticated");const{data:m}=await y.rpc("get_user_firm_id",{user_id:f.id}),{data:c,error:C}=await y.from("proforma_invoices").insert({...h,user_id:f.id,firm_id:m}).select().single();if(C)throw C;return await y.from("ledger_entries").insert({user_id:f.id,firm_id:m,party_id:h.party_id,debit_amount:h.amount,credit_amount:0,balance:h.amount,transaction_type:"debit",transaction_date:h.issue_date,particulars:`Proforma Invoice - ${c.proforma_number}`}),p({title:"Success",description:"Proforma invoice created successfully"}),await l(),c}catch(m){throw p({title:"Error",description:"Failed to create proforma invoice",variant:"destructive"}),m}},n=async(h,m)=>{try{const{error:c}=await y.from("proforma_invoices").update(m).eq("id",h);if(c)throw c;p({title:"Success",description:"Proforma invoice updated successfully"}),await l()}catch(c){throw p({title:"Error",description:"Failed to update proforma invoice",variant:"destructive"}),c}},_=async h=>{try{const{error:m}=await y.from("proforma_invoices").delete().eq("id",h);if(m)throw m;p({title:"Success",description:"Proforma invoice deleted successfully"}),await l()}catch(m){throw p({title:"Error",description:"Failed to delete proforma invoice",variant:"destructive"}),m}},b=async h=>{try{if(!f)throw new Error("Not authenticated");const{data:m}=await y.rpc("get_user_firm_id",{user_id:f.id}),{data:c,error:C}=await y.from("proforma_invoices").select(`
12          *,
13          proforma_line_items(*)
14        `).eq("id",h).single();if(C)throw C;const{data:B,error:R}=await y.from("invoices").insert({user_id:f.id,firm_id:m,party_id:c.party_id,amount:c.amount,status:"draft",issue_date:new Date().toISOString().split("T")[0],due_date:c.due_date,invoice_metadata:c.invoice_metadata,description:c.description,payment_mode:c.payment_mode,payment_terms:c.payment_terms,payment_options:c.payment_options,currency:c.currency,invoice_number:""}).select().single();if(R)throw R;if(c.proforma_line_items&&Array.isArray(c.proforma_line_items)&&c.proforma_line_items.length>0){const q=c.proforma_line_items.map(S=>({invoice_id:B.id,user_id:f.id,firm_id:m,description:S.description,hsn_sac_code:S.hsn_sac_code,unit:S.unit,quantity:S.quantity,rate:S.rate,amount:S.amount,tax_percentage:S.tax_percentage,tax_amount:S.tax_amount,total_amount:S.total_amount})),{error:O}=await y.from("invoice_line_items").insert(q);if(O)throw O}return await y.from("proforma_invoices").update({status:"converted",converted_to_invoice_id:B.id}).eq("id",h),p({title:"Success",description:"Proforma invoice converted to GST invoice successfully"}),await l(),B}catch(m){throw p({title:"Error",description:"Failed to convert proforma 
14invoice",variant:"destructive"}),m}};return j.useEffect(()=>{if(k)return;if(!a||!f){P([]);return}l();const h=y.channel("proforma_invoices_changes").on("postgres_changes",{event:"*",schema:"public",table:"proforma_invoices"},()=>{a&&f&&l()}).subscribe();return()=>{h.unsubscribe()}},[k,a,f?.id]),{proformaInvoices:w,loading:u,createProformaInvoice:s,updateProformaInvoice:n,deleteProformaInvoice:_,convertToInvoice:b,refetch:l}},be=w=>{const[P,u]=j.useState([]),[E,F]=j.useState(!1),{toast:N}=$(),{user:p}=se(),f=async a=>{if(a)try{F(!0);const{data:l,error:s}=await y.from("proforma_line_items").select("*").eq("proforma_id",a).order("created_at",{ascending:!0});if(s)throw s;u(l||[])}catch{N({title:"Error",description:"Failed to fetch proforma line items",variant:"destructive"})}finally{F(!1)}},k=async(a,l)=>{try{if(!p)throw new Error("Not authenticated");const{data:s}=await y.rpc("get_user_firm_id",{user_id:p.id});await y.from("proforma_line_items").delete().eq("proforma_id",a);const n=l.map(b=>{const{id:h,...m}=b;return{...m,proforma_id:a,user_id:p.id,firm_id:s}}),{error:_}=await y.from("proforma_line_items").insert(n);if(_)throw _;N({title:"Success",description:"Proforma line items saved successfully"}),await f(a)}catch(s){throw N({title:"Error",description:"Failed to save proforma line items",variant:"destructive"}),s}};return j.useEffect(()=>{w&&f(w)},[w]),{lineItems:P,loading:E,fetchLineItems:f,saveLineItems:k}},Ce=({isOpen:w,onClose:P,proforma:u})=>{const{parties:E}=ae(),{createProformaInvoice:F,updateProformaInvoice:N}=me(),{lineItems:p,saveLineItems:f}=be(u?.id),{toast:k}=$(),[a,l]=j.useState({party_id:"",amount:0,status:"draft",issue_date:new Date().toISOString().split("T")[0],validity_date:new Date(Date.now()+30*24*60*60*1e3).toISOString().split("T")[0],proforma_number:"",currency:"USD",payment_mode:"credit",payment_terms:"30 days",payment_options:"Bank Transfer"}),[s,n]=j.useState({}),[_,b]=j.useState([]),[h,m]=j.useState(0),[c,C]=j.useState(!1);j.useEffect(()=>{if(!w){C(!1);return}c||(u?(l(u),u.invoice_metadata&&n(u.invoice_metadata),C(!0)):(l({party_id:"",amount:0,status:"draft",issue_date:new Date().toISOString().split("T")[0],validity_date:new Date(Date.now()+30*24*60*60*1e3).toISOString().split("T")[0],proforma_number:"",currency:"USD",payment_mode:"credit",payment_terms:"30 days",payment_options:"Bank Transfer"}),n({}),b([]),m(0),C(!0)))},[w,u?.id]),j.useEffect(()=>{p.length>0&&_.length===0&&w&&b(p)},[p,w]);const B=(t,d)=>t*d,R=(t,d)=>t*d/100,q=(t,d)=>t+d,O=_.reduce((t,d)=>t+d.amount,0),S=_.reduce((t,d)=>t+d.tax_amount,0),W=O+S+h,r=t=>new Intl.NumberFormat("en-US",{minimumFractionDigits:2,maximumFractionDigits:2}).format(t),G=t=>t===0?"":t>0?`+${r(t)}`:r(t),T=(t,d,x)=>{const v=[..._];if(v[t]={...v[t],[d]:x},d==="gross_weight"||d==="rate"){const D=parseFloat(v[t].gross_weight||"0")||0,A=B(D,v[t].rate);v[t].amount=A,v[t].tax_amount=R(A,v[t].tax_percentage),v[t].total_amount=q(A,v[t].tax_amount)}else if(d==="tax_percentage"){const D=R(v[t].amount,x);v[t].tax_amount=D,v[t].total_amount=q(v[t].amount,D)}b(v)},X=()=>{const t={proforma_id:u?.id||"",description:"",hsn_sac_code:"",unit:"PCS",quantity:1,rate:0,amount:0,tax_percentage:0,tax_amount:0,total_amount:0};b([..._,t])},Q=t=>{b(_.filter((d,x)=>x!==t))},L=async t=>{t.preventDefault();try{const{party:d,...x}=a,v={...x,amount:W,invoice_metadata:s};let D;u?.id?(await N(u.id,v),D={...u,...v}):D=await F(v),D&&_.length>0&&await f(D.id,_),k({title:"Success",description:`Proforma invoice ${u?.id?"updated":"created"} successfully`}),P()}catch{}},U=()=>{P()};return e.jsx(re,{open:w,onOpenChange:U,children:e.jsxs(ie,{className:"max-w-6xl max-h-[90vh] overflow-y-auto",children:[e.jsx(ne,{children:e.jsx(oe,{children:u?.id?"Edit Proforma Invoice":"Create New Proforma Invoice"})}),e.jsxs("form",{onSubmit:L,className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(o,{htmlFor:"customer",children:"Customer *"}),e.jsxs(M,{value:a.party_id,onValueChange:t=>l({...a,party_id:t}),required:!0,children:[e.jsx(z,{children:e.jsx(H,{placeholder:"Select customer"})}),e.jsx(V,{children:E.map(t=>e.jsx(g,{value:t.id,children:t.name},t.id))})]})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"currency",children:"Currency *"}),e.jsxs(M,{value:a.currency,onValueChange:t=>l({...a,currency:t}),children:[e.jsx(z,{children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(g,{value:"USD",children:"USD"}),e.jsx(g,{value:"EUR",children:"EUR"}),e.jsx(g,{value:"INR",children:"INR"}),e.jsx(g,{value:"GBP",children:"GBP"})]})]})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"status",children:"Status"}),e.jsxs(M,{value:a.status,onValueChange:t=>l({...a,status:t}),children:[e.jsx(z,{children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(g,{value:"draft",children:"Draft"}),e.jsx(g,{value:"sent",children:"Sent"}),e.jsx(g,{value:"accepted",children:"Accepted"}),e.jsx(g,{value:"expired",children:"Expired"})]})]})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"proforma_number",children:"Invoice Number"}),e.jsx(i,{id:"proforma_number",value:a.proforma_number||"",onChange:t=>l({...a,proforma_number:t.target.value}),placeholder:"PI/0001/25-26"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"issue_date",children:"Issue Date *"}),e.jsx(i,{id:"issue_date",type:"date",value:a.issue_date,onChange:t=>l({...a,issue_date:t.target.value}),required:!0})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"validity_date",children:"Valid Until *"}),e.jsx(i,{id:"validity_date",type:"date",value:a.validity_date,onChange:t=>l({...a,validity_date:t.target.value}),required:!0})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"due_date",children:"Due Date"}),e.jsx(i,{id:"due_date",type:"date",value:a.due_date||"",onChange:t=>l({...a,due_date:t.target.value})})]})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"description",children:"Description"}),e.jsx(te,{id:"description",value:a.description||"",onChange:t=>l({...a,description:t.target.value}),placeholder:"Enter proforma description..."})]}),e.jsxs("div",{className:"border rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Billing & Shipping Details"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(o,{htmlFor:"customer_name",children:"Customer Name"}),e.jsx(i,{id:"customer_name",value:s.customer_name||"",onChange:t=>n({...s,customer_name:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"customer_email",children:"Customer Email"}),e.jsx(i,{id:"customer_email",type:"email",value:s.customer_email||"",onChange:t=>n({...s,customer_email:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"customer_gst_state_code",children:"GST State Code"}),e.jsx(i,{id:"customer_gst_state_code",value:s.customer_gst_state_code||"",onChange:t=>n({...s,customer_gst_state_code:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"mawb_mbl",children:"MAWB/MBL"}),e.jsx(i,{id:"mawb_mbl",value:s.mawb_mbl||"",onChange:t=>n({...s,mawb_mbl:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"hawb_hbl",children:"HAWB/HBL"}),e.jsx(i,{id:"hawb_hbl",value:s.hawb_hbl||"",onChange:t=>n({...s,hawb_hbl:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"shipper_name",children:"Shipper Name"}),e.jsx(i,{id:"shipper_name",value:s.shipper_name||"",onChange:t=>n({...s,shipper_name:t.target.value})})]})]})]}),e.jsxs("div",{className:"border rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Export Details"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(o,{htmlFor:"exporter_ref",children:"Exporter's Reference"}),e.jsx(i,{id:"exporter_ref",value:s.exporter_ref||"",onChange:t=>n({...s,exporter_ref:t.target.value}),placeholder:"EXP/2025/001"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"buyer_order_no",children:"Buyer's Order No. & Date"}),e.jsx(i,{id:"buyer_order_no",value:s.buyer_order_no||"",onChange:t=>n({...s,buyer_order_no:t.target.value}),placeholder:"PO-12345 / 15-01-2025"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"other_reference",children:"Other Reference(s)"}),e.jsx(i,{id:"other_reference",value:s.other_reference||"",onChange:t=>n({...s,other_reference:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"country_of_origin",children:"Country of Origin"}),e.jsx(i,{id:"country_of_origin",value:s.country_of_origin||"INDIA",onChange:t=>n({...s,country_of_origin:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"country_of_destination",children:"Country of Destination"}),e.jsx(i,{id:"country_of_destination",value:s.country_of_destination||"",onChange:t=>n({...s,country_of_destination:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"inco_term",children:"Incoterm / Delivery Terms"}),e.jsx(i,{id:"inco_term",value:s.inco_term||"",onChange:t=>n({...s,inco_term:t.target.value}),placeholder:"FOB, CIF, etc."})]})]})]}),e.jsxs("div",{className:"border rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Shipping & Transport Details"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(o,{htmlFor:"pre_carriage_by",children:"Pre-Carriage By"}),e.jsx(i,{id:"pre_carriage_by",value:s.pre_carriage_by||"",onChange:t=>n({...s,pre_carriage_by:t.target.value}),placeholder:"SEA / AIR"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"place_of_receipt",children:"Place of Receipt"}),e.jsx(i,{id:"place_of_receipt",value:s.place_of_receipt||"",onChange:t=>n({...s,place_of_receipt:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"vessel_flight_no",children:"Vessel / Flight No."}),e.jsx(i,{id:"vessel_flight_no",value:s.vessel_flight_no||s.vessel_details||"",onChange:t=>n({...s,vessel_flight_no:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"port_of_loading",children:"Port of Loading"}),e.jsx(i,{id:"port_of_loading",value:s.port_of_loading||s.pol||"",onChange:t=>n({...s,port_of_loading:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"port_of_discharge",children:"Port of Discharge"}),e.jsx(i,{id:"port_of_discharge",value:s.port_of_discharge||s.pod_fpod||"",onChange:t=>n({...s,port_of_discharge:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"final_destination",children:"Final Destination"}),e.jsx(i,{id:"final_destination",value:s.final_destination||"",onChange:t=>n({...s,final_destination:t.target.value})})]})]})]}),e.jsxs("div",{className:"border rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Packing Details"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(o,{htmlFor:"marks_and_n
14os",children:"Marks & Numbers"}),e.jsx(i,{id:"marks_and_nos",value:s.marks_and_nos||"",onChange:t=>n({...s,marks_and_nos:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"container_no",children:"Container No."}),e.jsx(i,{id:"container_no",value:s.container_no||"",onChange:t=>n({...s,container_no:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"no_of_packages",children:"No. & Kind of Packages"}),e.jsx(i,{id:"no_of_packages",value:s.no_of_packages||"",onChange:t=>n({...s,no_of_packages:t.target.value}),placeholder:"100 CARTONS"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"total_net_weight",children:"Total Net Weight"}),e.jsx(i,{id:"total_net_weight",value:s.total_net_weight||"",onChange:t=>n({...s,total_net_weight:t.target.value}),placeholder:"2,500.00 KGS"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"total_gross_weight",children:"Total Gross Weight"}),e.jsx(i,{id:"total_gross_weight",value:s.total_gross_weight||"",onChange:t=>n({...s,total_gross_weight:t.target.value}),placeholder:"2,750.00 KGS"})]})]})]}),e.jsxs("div",{className:"border rounded-lg p-4",children:[e.jsxs("div",{className:"flex justify-between items-center mb-4",children:[e.jsx("h3",{className:"text-lg font-semibold",children:"Proforma Items"}),e.jsxs(I,{type:"button",onClick:X,variant:"outline",size:"sm",children:[e.jsx(Z,{className:"h-4 w-4 mr-2"}),"Add Item"]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full border-collapse",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b",children:[e.jsx("th",{className:"text-left p-2 min-w-[180px]",children:"Description *"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"HSN/SAC"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"Unit"}),e.jsx("th",{className:"text-left p-2 min-w-[70px]",children:"Qty *"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"Net Wt"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"Gross Wt"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"Rate *"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"Amount"}),e.jsx("th",{className:"text-left p-2 min-w-[60px]",children:"Tax %"}),e.jsx("th",{className:"text-left p-2 min-w-[80px]",children:"Total"}),e.jsx("th",{className:"text-left p-2 w-[50px]"})]})}),e.jsx("tbody",{children:_.map((t,d)=>e.jsxs("tr",{className:"border-b",children:[e.jsx("td",{className:"p-2",children:e.jsx(i,{value:t.description,onChange:x=>T(d,"description",x.target.value),placeholder:"Enter description",required:!0})}),e.jsx("td",{className:"p-2",children:e.jsx(i,{value:t.hsn_sac_code||"",onChange:x=>T(d,"hsn_sac_code",x.target.value),placeholder:"HSN/SAC"})}),e.jsx("td",{className:"p-2",children:e.jsxs(M,{value:t.unit,onValueChange:x=>T(d,"unit",x),children:[e.jsx(z,{children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(g,{value:"PCS",children:"PCS"}),e.jsx(g,{value:"BAGS",children:"BAGS"}),e.jsx(g,{value:"BOXES",children:"BOXES"}),e.jsx(g,{value:"KG",children:"KG"}),e.jsx(g,{value:"MT",children:"MT"}),e.jsx(g,{value:"LITER",children:"LITER"}),e.jsx(g,{value:"HOUR",children:"HOUR"})]})]})}),e.jsx("td",{className:"p-2",children:e.jsx(i,{type:"number",value:t.quantity,onChange:x=>T(d,"quantity",parseFloat(x.target.value)||0),min:"0",step:"0.01",required:!0})}),e.jsx("td",{className:"p-2",children:e.jsx(i,{type:"text",value:t.net_weight||"",onChange:x=>T(d,"net_weight",x.target.value),placeholder:"e.g. 500 KG"})}),e.jsx("td",{className:"p-2",children:e.jsx(i,{type:"number",value:t.gross_weight||"",onChange:x=>T(d,"gross_weight",x.target.value),placeholder:"e.g. 520",min:"0",step:"0.01"})}),e.jsx("td",{className:"p-2",children:e.jsx(i,{type:"number",value:t.rate,onChange:x=>T(d,"rate",parseFloat(x.target.value)||0),min:"0",step:"0.01",required:!0})}),e.jsx("td",{className:"p-2",children:e.jsx("span",{className:"text-sm font-medium",children:r(t.amount)})}),e.jsx("td",{className:"p-2",children:e.jsx(i,{type:"number",value:t.tax_percentage,onChange:x=>T(d,"tax_percentage",parseFloat(x.target.value)||0),min:"0",max:"100",step:"0.01"})}),e.jsx("td",{className:"p-2",children:e.jsx("span",{className:"text-sm font-medium",children:r(t.total_amount)})}
14),e.jsx("td",{className:"p-2",children:e.jsx(I,{type:"button",variant:"outline",size:"sm",onClick:()=>Q(d),className:"text-destructive hover:text-destructive","aria-label":"Remove item",children:e.jsx(le,{className:"h-4 w-4"})})})]},d))})]})})]}),e.jsxs("div",{className:"border rounded-lg p-4",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Summary"}),e.jsxs("div",{className:"space-y-2 max-w-md ml-auto",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"Subtotal:"}),e.jsxs("span",{className:"font-medium",children:[a.currency," ",r(O)]})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{children:"Tax Total:"}),e.jsxs("span",{className:"font-medium",children:[a.currency," ",r(S)]})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{children:"Round Off:"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(i,{type:"number",value:h,onChange:t=>m(parseFloat(t.target.value)||0),step:"0.01",className:"w-20 text-right"}),e.jsx("span",{className:"text-sm",children:G(h)})]})]}),e.jsxs("div",{className:"flex justify-between text-lg font-bold border-t pt-2",children:[e.jsx("span",{children:"Grand Total:"}),e.jsxs("span",{children:[a.currency," ",r(W)]})]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(o,{htmlFor:"payment_mode",children:"Payment Mode"}),e.jsxs(M,{value:a.payment_mode,onValueChange:t=>l({...a,payment_mode:t}),children:[e.jsx(z,{children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(g,{value:"credit",children:"Credit"}),e.jsx(g,{value:"cash",children:"Cash"}),e.jsx(g,{value:"bank_transfer",children:"Bank Transfer"}),e.jsx(g,{value:"cheque",children:"Cheque"}),e.jsx(g,{value:"letter_of_credit",children:"Letter of Credit"})]})]})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"payment_terms",children:"Payment Terms"}),e.jsx(i,{id:"payment_terms",value:a.payment_terms||"",onChange:t=>l({...a,payment_terms:t.target.value}),placeholder:"e.g., 30 days"})]}),e.jsxs("div",{children:[e.jsx(o,{htmlFor:"payment_options",children:"Payment Options"}),e.jsx(i,{id:"payment_options",value:a.payment_options||"",onChange:t=>l({...a,payment_options:t.target.value}),placeholder:"e.g., Bank Transfer"})]}),e.jsxs("div",{className:"md:col-span-3",children:[e.jsx(o,{htmlFor:"payment_details",children:"Payment Details (for template)"}),e.jsx(te,{id:"payment_details",value:s.payment_details||"",onChange:t=>n({...s,payment_details:t.target.value}),placeholder:"Enter payment details that will appear on the invoice template...",rows:3})]})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4 border-t",children:[e.jsx(I,{type:"button",variant:"outline",onClick:U,children:"Cancel"}),e.jsx(I,{type:"submit",children:u?.id?"Update Proforma Invoice":"Create Proforma Invoice"})]})]})]})})},Se=({open:w,onClose:P,proforma:u,customer:E,companyData:F,bankData:N,currentFirm:p,onDownload:f})=>{const[k,a]=j.useState([]),[l,s]=j.useState(!0);j.useEffect(()=>{w&&u&&(async()=>{if(u?.id){s(!0);try{const{data:b,error:h}=await y.from("proforma_line_items").select("*").eq("proforma_id",u.id).order("created_at",{ascending:!0});if(h)throw h;a(b||[])}catch{}finally{s(!1)}}})()},[w,u?.id]);const n=F?.proformaTemplateCode||"modern-blue";return u?e.jsx(re,{open:w,onOpenChange:_=>!_&&P(),children:e.jsxs(ie,{className:"max-w-4xl max-h-[90vh] overflow-hidden flex flex-col",children:[e.jsxs(ne,{className:"flex-shrink-0 flex flex-row items-center justify-between",children:[e.jsxs(oe,{children:["Proforma Invoice Preview - ",u.proforma_number]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(I,{variant:"outline",size:"sm",onClick:f,className:"gap-2",children:[e.jsx(de,{className:"h-4 w-4"}),"Download PDF"]}),e.jsx(I,{variant:"ghost",size:"sm",onClick:P,"aria-label":"Close preview",children:e.jsx(he,{className:"h-4 w-4"})})]})]}),e.jsx("div",{className:"flex-1 overflow-auto bg-muted/30 p-4 rounded-lg",children:l?e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto"}),e.jsx("p",{className:"mt-2 text-muted-foreground",children:"Loading preview..."})]})}):e.jsx("div",{className:"bg-white shadow-lg mx-auto",style:{maxWidth:"210mm"},children:e.jsx(ce,{templateCode:n,proforma:u,customer:E,companyData:F,bankData:{...N,globalBankData:N?.globalBankData},lineItems:k,currentFirm:p})})})]})}):null},dt=()=>{const{proformaInvoices:w,loading:P,deleteProformaInvoice:u,convertToInvoice:E}=me(),{parties:F}=ae(),{companyData:N,bankData:p,globalBankData:f,watermarkData:k}=xe(),[a,l]=j.useState(!1),[s,n]=j.useState(null),[_,b]=j.useState(null),[h,m]=j.useState(!1),{toast:c}=$(),{currentFirm:C}=pe(),B=r=>{switch(r){case"draft":return"secondary";case"sent":return"default";case"accepted":return"default";case"converted":return"secondary";case"expired":return"destructive";default:return"secondary"}},R=r=>{n(r),l(!0)},q=async r=>
14{window.confirm("Are you sure you want to delete this proforma invoice?")&&await u(r)},O=async r=>{if(window.confirm("Are you sure you want to convert this proforma to a GST invoice? This action cannot be undone."))try{await E(r),c({title:"Success",description:"Proforma invoice converted to GST invoice successfully"})}catch{}},S=async r=>{try{m(!0);const{data:G,error:T}=await y.from("proforma_line_items").select("*").eq("proforma_id",r.id).order("created_at",{ascending:!0});if(T)throw T;const X=F.find(Y=>Y.id===r.party_id),Q=N.proformaTemplateCode||"modern-blue",L=document.createElement("div");L.style.position="absolute",L.style.left="-9999px",document.body.appendChild(L);const U=fe(L);U.render(e.jsx(ce,{templateCode:Q,proforma:r,customer:X,companyData:N,bankData:{...p,globalBankData:f},lineItems:G||[],currentFirm:C,watermarkData:k})),await new Promise(Y=>setTimeout(Y,500));const t=await ve(L,{scale:2,useCORS:!0,allowTaint:!0,backgroundColor:"#ffffff"});U.unmount(),document.body.removeChild(L);const d=t.toDataURL("image/png"),x=new ge("p","mm","a4"),v=210,D=295,A=t.height*v/t.width;let K=A,J=0;for(x.addImage(d,"PNG",0,J,v,A),K-=D;K>=0;)J=K-A,x.addPage(),x.addImage(d,"PNG",0,J,v,A),K-=D;const ue=`Proforma_Invoice_${r.proforma_number.replace(/\//g,"_")}_${new Date().toISOString().split("T")[0]}.pdf`;x.save(ue),c({title:"Success",description:"Proforma invoice PDF downloaded successfully"})}catch{c({title:"Error",description:"Failed to generate PDF",variant:"destructive"})}finally{m(!1)}},W=r=>{b(r)};return P?e.jsx("div",{className:"p-6",children:e.jsx("div",{className:"flex items-center justify-center min-h-[400px]",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto"}),e.jsx("p",{className:"mt-2 text-muted-foreground",children:"Loading proforma invoices..."})]})})}):e.jsxs("div",{className:"p-6 space-y-6",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Proforma Invoice"}),e.jsx("p",{className:"text-muted-foreground",children:"Create and manage proforma invoices (estimations)"})]}),e.jsxs(I,{onClick:()=>l(!0),className:"gap-2","data-tour":"create-proforma",children:[e.jsx(Z,{className:"h-4 w-4"}),"New Proforma Invoice"]})]}),e.jsx("div",{className:"grid gap-4","data-tour":"proforma-list",children:w.length===0?e.jsxs(ee,{className:"p-8 text-center",children:[e.jsx(je,{className:"h-12 w-12 text-muted-foreground mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-semibold mb-2",children:"No Proforma Invoices"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Create your first proforma invoice to get started."}),e.jsxs(I,{onClick:()=>l(!0),className:"gap-2",children:[e.jsx(Z,{className:"h-4 w-4"}),"Create Proforma Invoice"]})]}):w.map(r=>e.jsx(ee,{className:"p-6",children:e.jsxs("div",{className:"flex justify-between items-start",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("h3",{className:"font-semibold text-lg",children:r.proforma_number}),e.jsx(_e,{variant:B(r.status),children:r.status})]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[e.jsx("strong",{children:"Customer:"})," ",r.party?.name||"N/A"]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[e.jsx("strong",{children:"Date:"})," ",new Date(r.issue_date).toLocaleDateString()]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[e.jsx("strong",{children:"Valid Until:"})," ",new Date(r.validity_date).toLocaleDateString()]}),e.jsxs("p",{className:"text-lg font-semibold",children:[r.currency," ",Number(r.amount).toFixed(2)]}),r.description&&e.jsx("p",{className:"text-sm text-muted-foreground",children:r.description})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(I,{variant:"outline",size:"sm",onClick:()=>W(r),disabled:h,children:e.jsx(ye,{className:"h-4 w-4"})}),e.jsx(I,{variant:"outline",size:"sm",onClick:()=>S(r),disabled:h,children:e.jsx(de,{className:"h-4 w-4"})}),e.jsx(I,{variant:"outline",size:"sm",onClick:()=>R(r),children:e.jsx(we,{className:"h-4 w-4"})}),r.status!=="converted"&&e.jsxs(I,{variant:"outline",size:"sm",onClick:()=>O(r.id),className:"gap-1",children:[e.jsx(Ne,{className:"h-4 w-4"}),"Convert"]}),e.jsx(I,{variant:"outline",size:"sm",onClick:()=>q(r.id),className:"text-destructive hover:text-destructive",children:e.jsx(le,{className:"h-4 w-4"})})]})]})},r.id))}),e.jsx(Ce,{isOpen:a,onClose:()=>{l(!1),n(null)},proforma:s}),_&&e.jsx(Se,{open:!0,onClose:()=>b(null),proforma:_,customer:F.find(r=>r.id===_.party_id),companyData:N,bankData:p,currentFirm:C,onDownload:()=>S(_)})]})};export{dt 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.