import{r as b,aI as D,aJ as U}from"./index-CYy8zBsj.js";function I(t,e,p,r,s){const c=(t??"").trim().toLowerCase(),T=(e??"").trim().toLowerCase(),g=c.length>0&&T.length>0?c===T:s==="INTRA_STATE",l=p??[],u=l.find(f=>{var n;return((n=f.name)==null?void 0:n.toUpperCase())==="IGST"}),o=l.find(f=>{var n;return((n=f.name)==null?void 0:n.toUpperCase())==="CGST"}),a=l.find(f=>{var n;return((n=f.name)==null?void 0:n.toUpperCase())==="SGST"});return g&&o&&a?{isSameState:!0,effectiveRate:o.percentage+a.percentage}:!g&&u?{isSameState:!1,effectiveRate:u.percentage}:{isSameState:g,effectiveRate:r}}function q(t,e){return t.discountType==="AMOUNT"?Math.min(Math.max(0,t.discount||0),e):e*Math.min(Math.max(0,t.discount||0),100)/100}function B(t,e){var T,g,l,u;const p=((T=t.selectedBatch)==null?void 0:T.taxRateDtoList)??[];let r=((g=p.find(o=>{var a;return((a=o.name)==null?void 0:a.toUpperCase())==="CGST"}))==null?void 0:g.percentage)??0,s=((l=p.find(o=>{var a;return((a=o.name)==null?void 0:a.toUpperCase())==="SGST"}))==null?void 0:l.percentage)??0,c=((u=p.find(o=>{var a;return((a=o.name)==null?void 0:a.toUpperCase())==="IGST"}))==null?void 0:u.percentage)??0;return r===0&&s===0&&c===0&&(r=t.cgstRate??0,s=t.sgstRate??0,c=t.igstRate??0),r===0&&s===0&&c===0&&e.effectiveRate>0&&(e.isSameState?(r=e.effectiveRate/2,s=e.effectiveRate/2):c=e.effectiveRate),{cgstRate:r,sgstRate:s,igstRate:c}}function w(t,e,p){return b.useMemo(()=>{const r=t.taxMode==="WITHOUT_GST";let s=0,c=0,T=0,g=0,l=0,u=0,o=0,a=!0,f=0;t.items.forEach(i=>{var m,A,G,M;const h=i.quantity*i.unitPrice,L=((m=i.selectedBatch)==null?void 0:m.basePurchasePrice)??0;f+=L*i.quantity;const v=q(i,h),S=Math.max(0,h-v),d=I((A=t.dispatchAddress)==null?void 0:A.state,(G=t.shippingAddress)==null?void 0:G.state,(M=i.selectedBatch)==null?void 0:M.taxRateDtoList,i.gstRate,t.transactionType);if(a=d.isSameState,!r){const R=B(i,d);d.isSameState?(l+=S*R.cgstRate/100,u+=S*R.sgstRate/100):o+=S*(R.igstRate||d.effectiveRate)/100;const P=d.isSameState?R.cgstRate+R.sgstRate:R.igstRate||d.effectiveRate;c+=S*P/100}s+=S,T+=v,g+=i.quantity});const n=D(e);r||(l+=n.cgstTotal,u+=n.sgstTotal,o+=n.igstTotal,c+=n.taxTotal);const x=p.filter(i=>i.isChargeable).reduce((i,h)=>i+U(h.charges??[],s),0),C=U(e,s),y=s+c+x+C;return{subTotal:s,taxTotal:c,totalDiscount:T,serviceChargesTotal:x,directChargesTotal:C,grandTotal:y,totalQty:g,cgstTotal:l,sgstTotal:u,igstTotal:o,isSameState:a,totalPurchaseAmount:f}},[t.items,t.dispatchAddress,t.shippingAddress,t.transactionType,t.taxMode,e,p])}export{I as a,q as g,B as r,w as u};
