import { Divide, Multiply, Subtract } from '../../shared/math-operations'; import { PrintTypesEnum } from '../../enums/code-enums'; import { BillStatusEnum } from '../../enums/enums'; import { PrintSharedService } from '../../shared/shared-print.service'; import { MyDate } from '../../utils/my-date'; import { TrUtils } from '../../utils/tr-utils'; import { PurchaseOrderTotalsService } from './purchase-order-totals.service'; export class PurchaseOrderPrintService { //Prepare Pdf Data For Purchase Order And Bill static GetDataForPurchaseOrderPrint(argPurchaseOrderData: any, EntityData: any, image: any, VendorData: any, TaxCodes: any, IsFormPO: boolean) { let PurchaseOrderData: any = TrUtils.Stringify(argPurchaseOrderData); let PurchaseOrderPDFData: any = {}; PurchaseOrderPDFData.Settings = TrUtils.Stringify(argPurchaseOrderData.Settings); PurchaseOrderPDFData.SType = TrUtils.Stringify(argPurchaseOrderData.SType); PurchaseOrderPDFData.Adjust = argPurchaseOrderData.Adjust; // let IsTaxable: boolean = (PurchaseOrderData.Tax === 3) ? true : false; let IsTaxable: boolean = true; // PurchaseOrderPDFData.ShowTaxColumn = (PurchaseOrderData.Tax === 3) ? true : false; // PurchaseOrderPDFData.HasTaxIncluded = (PurchaseOrderData.Tax === 2) ? true : false; // PurchaseOrderPDFData.HideDiscountColumn = (PurchaseOrderData.Tax === 2) ? true : false; PurchaseOrderPDFData.ShowTaxColumn = true; PurchaseOrderPDFData.HasTaxIncluded = false; PurchaseOrderPDFData.HideDiscountColumn = false; let SType: any; if (!TrUtils.IsNull(argPurchaseOrderData.SType)) { SType = argPurchaseOrderData.SType } // PurchaseOrderPDFData.HasNoTax = (PurchaseOrderData.Tax === 1) ? true : false; PurchaseOrderPDFData.HasNoTax = false; PurchaseOrderPDFData.ShowIGST = (TrUtils.IsNull(SType) || (SType === 'Intra')) ? false : true; if (IsFormPO) { PurchaseOrderPDFData.Id = PurchaseOrderData.Code; PurchaseOrderPDFData.CrDate = MyDate.ConvertUTCDateToReadable(PurchaseOrderData.CrDate); PurchaseOrderPDFData.DeDate = MyDate.ConvertUTCDateToReadable(PurchaseOrderData.DeDate); } else { PurchaseOrderPDFData.Id = PurchaseOrderData.BNo; PurchaseOrderPDFData.CrDate = MyDate.ConvertUTCDateToReadable(PurchaseOrderData.CrDate); PurchaseOrderPDFData.DeDate = MyDate.ConvertUTCDateToReadable(PurchaseOrderData.DeDate); } PurchaseOrderPDFData.Entity = PrintSharedService.GetFormattedEntityDataForPrint(EntityData, PrintTypesEnum.PurOrder, PurchaseOrderData); PurchaseOrderPDFData.Items = TrUtils.Stringify(PurchaseOrderData.Items); PurchaseOrderPDFData.Ops = TrUtils.Stringify(PurchaseOrderData.Ops); if (!TrUtils.IsNull(PurchaseOrderPDFData.Ops)) { PurchaseOrderPDFData.Ops = this.GetLaborPrintInfo(PurchaseOrderPDFData.Ops, TaxCodes, 'AT', PurchaseOrderPDFData.Entity.DecimalsNumber); } // PurchaseOrderPDFData.Items = this.GetPartsDescription(PurchaseOrderPDFData.Items); PurchaseOrderPDFData.Items = this.GetQtyWithUoM(PurchaseOrderPDFData.Items); let DetailedTotalsInfo: any; if (IsFormPO) { DetailedTotalsInfo = PurchaseOrderTotalsService.GetTotalsValue(PurchaseOrderPDFData.Ops, PurchaseOrderPDFData.Items, IsTaxable, true, TaxCodes, PurchaseOrderPDFData.Tax, true, PurchaseOrderPDFData.HasNoTax, false, 0, false, PurchaseOrderPDFData.Adjust, PurchaseOrderPDFData.Entity); } else { DetailedTotalsInfo = PurchaseOrderTotalsService.GetTotalsValue(PurchaseOrderPDFData.Ops, PurchaseOrderPDFData.Items, IsTaxable, true, TaxCodes, PurchaseOrderPDFData.Tax, true, PurchaseOrderPDFData.HasNoTax, true, PurchaseOrderData.Disc, PurchaseOrderData.Perc, PurchaseOrderPDFData.Adjust, PurchaseOrderPDFData.Entity); } PurchaseOrderPDFData = this.ConcatTotalsInfo(PurchaseOrderPDFData, DetailedTotalsInfo); PurchaseOrderPDFData = PrintSharedService.GetEntityHeaderStyles(PurchaseOrderPDFData, EntityData, image); if (IsFormPO) { PurchaseOrderPDFData.HeaderName = 'Purchase Order'; } else { if (argPurchaseOrderData.Expense) { PurchaseOrderPDFData.HeaderName = 'Expense'; } else { let PaymentData = this.GetPaymentsTotal(PurchaseOrderData.Credits, PurchaseOrderData.Payments, PurchaseOrderData.Total, PurchaseOrderData.Sts, PurchaseOrderPDFData.Entity.DecimalsNumber); PurchaseOrderPDFData.HeaderName = 'Bill'; PurchaseOrderPDFData.Due = PaymentData.Due; PurchaseOrderPDFData.Paid = PaymentData.Paid; } } PurchaseOrderPDFData.Vendor = this.GetVendorDataForPrint(TrUtils.Stringify(VendorData)); PurchaseOrderPDFData = this.FixPriceValuesForPrint(PurchaseOrderPDFData, TaxCodes, PurchaseOrderPDFData.Entity.DecimalsNumber); return PurchaseOrderPDFData; } static GetLaborPrintInfo(LaborList: any, TaxCodes: any, LineTotal: string, DecimalsNumber: number) { LaborList.forEach((Labor: any) => { Labor.HSN = Labor.SAC; if (TrUtils.IsNull(Labor.Qty)) { Labor.Qty = 1; } Labor.UnCo = TrUtils.FixPriceValue(Labor.UnCo, DecimalsNumber); // Labor.TaxName = this.getTaxName(Labor.TCode, TaxCodes); if (!TrUtils.IsEmpty(Labor.Perc)) { Labor.DiscPerc = Labor.Perc; } else { Labor.DiscPerc = Labor.Perc; Labor.DiscPerc = Divide(Multiply(100, Labor.Disc), Labor.UnCo); } Labor.TCode = this.GetTCodeFromTaxes(Labor); Labor.CGSTAmt = TrUtils.FixPriceValue(this.GetTaxAmountFromTaxes(Labor, 'CGST', Labor.CGST), DecimalsNumber); Labor.SGSTAmt = TrUtils.FixPriceValue(this.GetTaxAmountFromTaxes(Labor, 'SGST', Labor.SGST), DecimalsNumber); Labor.IGSTAmt = TrUtils.FixPriceValue(this.GetTaxAmountFromTaxes(Labor, 'IGST', Labor.IGST), DecimalsNumber); Labor.QtyAndUoM = Labor.Qty.toString(); Labor.CGSTPerc = this.GetTaxRateFromTaxes(Labor, 'CGST', PrintSharedService.GetCGSTValueBasedOnTaxCode(Labor.TCode, TaxCodes)); Labor.SGSTPerc = this.GetTaxRateFromTaxes(Labor, 'SGST', PrintSharedService.GetSGSTValueBasedOnTaxCode(Labor.TCode, TaxCodes)); Labor.IGSTPerc = this.GetTaxRateFromTaxes(Labor, 'IGST', PrintSharedService.GetIGSTValueBasedOnTaxCode(Labor.TCode, TaxCodes)); // console.log('hello', TrUtils.Stringify(Labor)); // if (LineTotal === 'AT') { // Labor.LineTotal = TrUtils.FixPriceValue(Labor.AfterLaborTax); // } else { // Labor.LineTotal = TrUtils.FixPriceValue(subtraction(Labor.Amt, Labor.Disc)); // } }); return LaborList; } static GetPaymentsTotal(Credits: any, BillPayments: any, Total: number, BillStatus: string, DecimalsNumber: number) { const payments = Array.isArray(BillPayments) ? BillPayments : []; const credits = Array.isArray(Credits) ? Credits : []; const paymentsTotal = payments.reduce((total: number, payment: any) => total + TrUtils.SetValueToZeroIfNull(payment?.Paid), 0); const creditsTotal = credits.reduce((total: number, credit: any) => total + TrUtils.SetValueToZeroIfNull(credit?.Amt), 0); const paidTotal = paymentsTotal + creditsTotal; const isPaid = BillStatus === BillStatusEnum.Paid; let due = isPaid ? 0 : TrUtils.FixedTo(Subtract(Total, paidTotal), DecimalsNumber); if (due < 0) { due = 0; } const data: any = { Due: due, Paid: TrUtils.FixedTo(paidTotal, DecimalsNumber) }; if (due === 0) { data.Sts = BillStatusEnum.Closed; } return data; } static GetVendorDataForPrint(argVendorData: any) { if (TrUtils.IsNull(argVendorData)) { return null; } let VendorPDFData: any = {}; let VendorData: any = TrUtils.Stringify(argVendorData); // VendorPDFData.VendorType = VendorData.Type; // if (VendorData.Type === CustomerTypesEnum.Individual) { VendorPDFData.Name = VendorData.Name; VendorPDFData.Contact = [ { Type: 'M', No: VendorData.Ph } ] // } else { // VendorPDFData.Name = VendorData.CName; // VendorPDFData.Contact = [ // { Type: 'M', No: VendorData.MPhone } // ] // } VendorPDFData.Address = []; if (!TrUtils.IsNull(VendorData.Adrs)) { VendorPDFData.Address.push(VendorData.Adrs.A1); if (!TrUtils.IsEmpty(VendorData.Adrs.A2)) { VendorPDFData.Address.push(VendorData.Adrs.A2); } VendorPDFData.Address.push(VendorData.Adrs.Ct); } return VendorPDFData; } static GetQtyWithUoM(Items: any) { Items.forEach((Part: any) => { if (TrUtils.IsNull(Part.Ind)) { if (!TrUtils.IsZero(Part.Qty) && !TrUtils.CheckInvalidSelect(Part.UoM)) { Part.QtyAndUoM = Part.Qty + ' ' + Part.UoM; } } else { if (TrUtils.IsNull(Part.OpCode)) { if (!TrUtils.IsZero(Part.Qty) && !TrUtils.CheckInvalidSelect(Part.UoM)) { Part.QtyAndUoM = Part.Qty + ' ' + Part.UoM; } } } }); return Items; } static ConcatTotalsInfo(PurchaseOrderPDFData: any, DetailedTotalsInfo: any) { PurchaseOrderPDFData = TrUtils.ConcatObjects(PurchaseOrderPDFData, DetailedTotalsInfo); return PurchaseOrderPDFData; } static FixPriceValuesForPrint(PrintData: any, TaxCodes: any, DecimalsNumber: number) { PrintData.ShowDetailedPartTaxInfo = false; PrintData.Items.forEach((Part: any) => { if (TrUtils.IsNull(Part.UnCo)) { Part.UnCo = Part.UnPr; } Part.UnCo = TrUtils.FixPriceValue(Part.UnCo, DecimalsNumber); Part.DiscountedPrice = TrUtils.FixPriceValue(Part.DiscountedPrice, DecimalsNumber); Part.AfterTax = TrUtils.FixPriceValue(Part.AfterTax, DecimalsNumber); Part.TCode = this.GetTCodeFromTaxes(Part); Part.CGSTAmt = TrUtils.FixPriceValue(this.GetTaxAmountFromTaxes(Part, 'CGST', Part.CGST), DecimalsNumber); Part.IGSTAmt = TrUtils.FixPriceValue(this.GetTaxAmountFromTaxes(Part, 'IGST', Part.IGST), DecimalsNumber); Part.SGSTAmt = TrUtils.FixPriceValue(this.GetTaxAmountFromTaxes(Part, 'SGST', Part.SGST), DecimalsNumber); if (!TrUtils.CheckInvalidSelect(Part.TCode)) { PrintData.ShowDetailedPartTaxInfo = true; } Part.CGSTPerc = this.GetTaxRateFromTaxes(Part, 'CGST', PrintSharedService.GetCGSTValueBasedOnTaxCode(Part.TCode, TaxCodes)); Part.SGSTPerc = this.GetTaxRateFromTaxes(Part, 'SGST', PrintSharedService.GetSGSTValueBasedOnTaxCode(Part.TCode, TaxCodes)); Part.IGSTPerc = this.GetTaxRateFromTaxes(Part, 'IGST', PrintSharedService.GetIGSTValueBasedOnTaxCode(Part.TCode, TaxCodes)); }); PrintData.PartsDiscTotal = TrUtils.FixPriceValue(PrintData.PartsDiscTotal, DecimalsNumber); PrintData.PartsTotalAfterDisc = TrUtils.FixPriceValue(PrintData.PartsTotalAfterDisc, DecimalsNumber); PrintData.PartsTotalBeforeDisc = TrUtils.FixPriceValue(PrintData.PartsTotalBeforeDisc, DecimalsNumber); PrintData.TaxTotal = TrUtils.FixPriceValue(PrintData.TaxTotal, DecimalsNumber); PrintData.PartCGST = TrUtils.FixPriceValue(PrintData.PartCGST, DecimalsNumber); PrintData.PartIGST = TrUtils.FixPriceValue(PrintData.PartIGST, DecimalsNumber); PrintData.PartSGST = TrUtils.FixPriceValue(PrintData.PartSGST, DecimalsNumber); PrintData.PartAfterTax = TrUtils.FixPriceValue(PrintData.PartAfterTax, DecimalsNumber); PrintData.TotalRoundedBy = TrUtils.FixPriceValue(PrintData.TotalRoundedBy, DecimalsNumber); PrintData.RoundedTotal = TrUtils.FixPriceValue(PrintData.RoundedTotal, DecimalsNumber); return PrintData; } static GetTaxAmountFromTaxes(item: any, taxCode: string, fallbackAmount: any = 0) { const taxes = Array.isArray(item?.Taxes) ? item.Taxes : []; const matchedTaxes = taxes.filter((tax: any) => tax?.Code === taxCode); if (matchedTaxes.length !== 0) { return matchedTaxes.reduce((total: number, tax: any) => total + Number(tax?.Amt || 0), 0); } return TrUtils.SetValueToZeroIfNull(fallbackAmount); } static GetTaxRateFromTaxes(item: any, taxCode: string, fallbackRate: any = 0) { const taxes = Array.isArray(item?.Taxes) ? item.Taxes : []; const matchedTaxes = taxes.filter((tax: any) => tax?.Code === taxCode); if (matchedTaxes.length !== 0) { return matchedTaxes.reduce((total: number, tax: any) => total + Number(tax?.Rate || 0), 0); } return TrUtils.SetValueToZeroIfNull(fallbackRate); } static GetTCodeFromTaxes(item: any) { const taxes = Array.isArray(item?.Taxes) ? item.Taxes : []; const matchedTax = taxes.find((tax: any) => !TrUtils.IsNull(tax?.TaxCodeId)); if (!TrUtils.IsNull(matchedTax?.TaxCodeId)) { return matchedTax.TaxCodeId; } return item?.TCode; } }