import { ROTypeEnum } from '../enums/code-enums'; import { LaborStatusEnum, PayTypeEnum, ROStatusEnum } from '../enums/enums'; import { Add } from '../shared/math-operations'; import { PrintSharedService } from '../shared/shared-print.service'; import { MyDate } from '../utils/my-date'; import { TrUtils } from '../utils/tr-utils'; import { ROTotalsService } from './ro-totals.service'; export class ROPrintService { static GetTaxAmountFromGroup(item: any, taxGroupKey: string, taxCode: string, fallbackKey: string) { const groupedTaxes = item?.[taxGroupKey]; if (Array.isArray(groupedTaxes)) { const matchedTaxes = groupedTaxes.filter((tax: any) => tax?.Code === taxCode); if (matchedTaxes.length !== 0) { return matchedTaxes.reduce((total: number, tax: any) => Add(total, tax?.Amt), 0); } } return TrUtils.SetValueToZeroIfNull(item?.[fallbackKey]); } static GetTCode(item: any) { const taxGroupKeys: string[] = ['CTaxes', 'ATaxes', 'ETaxes']; for (const taxGroupKey of taxGroupKeys) { const groupedTaxes = item?.[taxGroupKey]; if (!Array.isArray(groupedTaxes)) { continue; } const matchedTax = groupedTaxes.find((tax: any) => !TrUtils.IsNull(tax?.TaxCodeId)); if (!TrUtils.IsNull(matchedTax?.TaxCodeId)) { return matchedTax.TaxCodeId; } } return item?.TCode; } static GetTaxRateFromGroup(item: any, taxGroupKey: string, taxCode: string) { const groupedTaxes = item?.[taxGroupKey]; if (Array.isArray(groupedTaxes)) { const matchedTaxes = groupedTaxes.filter((tax: any) => tax?.Code === taxCode); if (matchedTaxes.length !== 0) { return TrUtils.FixedTo(matchedTaxes.reduce((total: number, tax: any) => Add(total, tax?.Rate), 0)); } } return 0; } static GetTaxRate(item: any, taxCode: string, preferredTaxGroupKey?: string) { const taxGroupKeys: string[] = []; if (typeof preferredTaxGroupKey === 'string' && preferredTaxGroupKey.length !== 0) { taxGroupKeys.push(preferredTaxGroupKey); } ['CTaxes', 'ATaxes', 'ETaxes'].forEach((groupKey: string) => { if (taxGroupKeys.indexOf(groupKey) === -1) { taxGroupKeys.push(groupKey); } }); for (const taxGroupKey of taxGroupKeys) { const rate = this.GetTaxRateFromGroup(item, taxGroupKey, taxCode); if (!TrUtils.IsZero(rate)) { return rate; } } return 0; } static GetRepairOrderPrintInfo(OriginalROData: any, OriginalCustomerData: any, OriginalVehicleData: any, OriginalEntityData: any, image: any, IncludeGST: boolean, ConsolidateGST: boolean, Payee: any, InsCompanyName: any, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, TaxCodes: any, OrderType: any, isReverse:boolean) { let ROPrintData: any = {}; let argROData = TrUtils.Stringify(OriginalROData); argROData.Comps = TrUtils.IsNull(argROData.Comps) ? [] : argROData.Comps; argROData.Ops = TrUtils.IsNull(argROData.Ops) ? [] : argROData.Ops; argROData.Parts = TrUtils.IsNull(argROData.Parts) ? [] : argROData.Parts; argROData.Colli = TrUtils.IsNull(argROData.Colli) ? [] : argROData.Colli; if (!IncludeGST) { argROData.Settings.Tax = 'NO'; } let SType: any if (!TrUtils.IsNull(OriginalROData.SType)) { SType = OriginalROData.SType } ROPrintData = this.GetROBasicDetailsForPrint(ROPrintData, argROData, OriginalVehicleData, OrderType); ROPrintData = this.GetPrintConditionsBasedOnInput(ROPrintData, IncludeGST, ConsolidateGST, Payee, AsCustomerOnly, AsInsuranceOnly, SType); ROPrintData.Entity = PrintSharedService.GetFormattedEntityDataForPrint(OriginalEntityData, "Work Order", OriginalROData); ROPrintData = PrintSharedService.GetEntityHeaderStyles(ROPrintData, OriginalEntityData, image); if (Payee === PayTypeEnum.Insurance) { ROPrintData = this.GetInsDetailsForPrint(ROPrintData, InsCompanyName, argROData); } argROData = this.CheckStatusAndChangeDeclinedItemsToNew(argROData); argROData = this.GetValidItemsForTotals(argROData); argROData.Ops = this.SeparateROLaborByPType(argROData.Ops, Payee, AsCustomerOnly, AsInsuranceOnly); argROData.Parts = this.SeparateROPartsByPType(argROData.Parts, Payee, AsCustomerOnly, AsInsuranceOnly); argROData.Colli = this.SeparateROCollisionsByPType(argROData.Colli, argROData.Ops, argROData.Parts); // argROData = this.ResetDescForPrint(argROData); argROData.Parts = this.ResetQtyAndUoM(argROData.Parts); if (AsInsuranceOnly) { ROPrintData.ShowAccParts = true; } else { ROPrintData.ShowAccParts = this.CheckAndShowItemsAsAR(argROData); } argROData = this.ReverseItemsForROPrint(argROData, isReverse); let finalTotalsData: any = ROTotalsService.GetTotalsValue(argROData, TaxCodes, true, true, true, ROPrintData.Entity.Round, ROPrintData.Entity); finalTotalsData.Settings = TrUtils.Stringify(argROData.Settings); finalTotalsData = this.GetDetailedInfoForItems(finalTotalsData, IncludeGST, ConsolidateGST, AsCustomerOnly, AsInsuranceOnly, TaxCodes, ROPrintData.Entity.DecimalsNumber); if (ConsolidateGST) { finalTotalsData.CustLaborTotalBeforeDisc = finalTotalsData.CustLaborAfterTax; finalTotalsData.CustPartsTotalBeforeDisc = finalTotalsData.CustPartAfterTax; } ROPrintData = TrUtils.ConcatObjects(ROPrintData, finalTotalsData); if (ROPrintData.ShowAccParts) { ROPrintData = this.ConvertItemsAsAR(ROPrintData); } ROPrintData.Colli = this.EmptyInvalidCollisionRepairItems(ROPrintData.Colli); ROPrintData.Comps = TrUtils.Stringify(argROData.Comps); ROPrintData.EstTotal = TrUtils.Stringify(argROData.EstTotal); ROPrintData.Advisor = this.GetEmployeeData(argROData.EmpData); ROPrintData.VehMore = TrUtils.Stringify(argROData.VehMore); ROPrintData.SANotes = TrUtils.Stringify(argROData.SANotes); ROPrintData.OrdType = TrUtils.Stringify(argROData.OrdType); ROPrintData = this.GetLineTotals(ROPrintData, IncludeGST, ConsolidateGST, AsCustomerOnly, AsInsuranceOnly, ROPrintData.Entity.DecimalsNumber); ROPrintData.PrintInfo = this.PrepareFormatForROPrint(ROPrintData, Payee, ROTypeEnum.RepairOrder); ROPrintData = this.CheckAndCombineDescMPNForAR(ROPrintData.Entity.MPN, ROPrintData); ROPrintData.Vehicle = PrintSharedService.GetVehicleFromServiceAndFormatData(OriginalVehicleData); ROPrintData.Customer = PrintSharedService.GetCustomerFromServiceAndFormatData(OriginalCustomerData); if (!TrUtils.IsNull(OriginalROData.SrvLoc)) { let location: any = {}; location.Adrs = PrintSharedService.GetAddress(OriginalROData.SrvLoc.Adrs); location.Name = OriginalROData.SrvLoc.ConName; if (!TrUtils.IsNull(OriginalROData.SrvLoc.ConPh)) { location.Cons = [ { Type: 'M', No: OriginalROData.SrvLoc.ConPh } ]; } ROPrintData.Location = location; } ROPrintData.Consolidate = ConsolidateGST; ROPrintData.ShowDiscountColumn = false; if ((Payee === PayTypeEnum.Customer || AsCustomerOnly) && !ROPrintData.ConsolidateGST) { if (PrintSharedService.CheckItemIndexWithDisc(ROPrintData) !== -1) { ROPrintData.ShowDiscountColumn = (ROPrintData.Settings.DiscLvl === 'ITM' || ROPrintData.Settings.DiscLvl === 'RECITM') ? true : false; } } return ROPrintData; } static GetEmployeeData(empData: any) { let SA: any = {}; if (!TrUtils.IsNull(empData)) { SA.Name = empData.DName; SA.Ph = empData.Cons.Phones.Main; } return SA; } static GetROBasicDetailsForPrint(ROPrintData: any, argROData: any, Product: any, OrderType: any) { ROPrintData.CrDate = MyDate.ConvertUTCDateToReadable(argROData.CrDate); ROPrintData.PrDate = MyDate.ConvertUTCDateToReadable(argROData.PrDate); ROPrintData.MIn = Product.MIn; ROPrintData._id = argROData.Code; ROPrintData.Type = 'Work Order'; ROPrintData.HeaderName = OrderType; return ROPrintData; } static GetPrintConditionsBasedOnInput(ROPrintData: any, IncludeGST: boolean, ConsolidateGST: boolean, Payee: any, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, SType: any) { ROPrintData.Payee = Payee; ROPrintData.AsCustomerOnly = AsCustomerOnly; ROPrintData.AsInsuranceOnly = AsInsuranceOnly; ROPrintData.IncludeGST = IncludeGST; ROPrintData.ConsolidateGST = ConsolidateGST; ROPrintData.ShowIGST = (TrUtils.IsNull(SType) || (SType === 'Intra')) ? false : true; ROPrintData.ShowTaxColumn = (IncludeGST && !ConsolidateGST) ? true : false; // ROPrintData.ShowDiscountColumn = ((Payee === PayTypeEnum.Customer || AsCustomerOnly) && !ConsolidateGST) ? true : false; return ROPrintData; } static GetInsDetailsForPrint(ROPrintData: any, InsCompanyName: any, argROData: any) { if (TrUtils.IsNull(argROData.Ins)) { argROData.Ins = this.GetDefualtInsData(); } ROPrintData.InsComp = InsCompanyName; ROPrintData.SurName = argROData.Ins.SName; ROPrintData.SurEmail = argROData.Ins.SEmail; ROPrintData.SurPhone = argROData.Ins.SPhone; ROPrintData.PolType = argROData.Ins.Type; ROPrintData.PolNo = argROData.Ins.PolNo; return ROPrintData; } static GetDefualtInsData() { let ROInsData: any = { SName: '', SEmail: '', SPhone: '', PolNo: '' }; return ROInsData; } static CheckStatusAndChangeDeclinedItemsToNew(argROData: any) { if (argROData.Sts === ROStatusEnum.New || argROData.Sts === ROStatusEnum.WtngForAppr) { argROData = this.ChangeDeclinedStatusToNew(argROData); } return argROData; } static ChangeDeclinedStatusToNew(argROData: any) { argROData.Ops = this.ChangeDeclinedLaborStatusToNew(argROData.Ops); argROData.Parts = this.ChangeDeclinedPartStatusToNew(argROData.Parts); argROData.Colli = this.ChangeDeclinedColliStatusToNew(argROData.Colli); return argROData; } static ChangeDeclinedLaborStatusToNew(Operations: any) { Operations.forEach((Labor: any) => { if (Labor.Sts === LaborStatusEnum.Declined) { Labor.Sts = LaborStatusEnum.New; } }); return Operations; } static ChangeDeclinedPartStatusToNew(Parts: any) { Parts.forEach((Part: any) => { if (!TrUtils.IsNull(Part.CollId) && Part.Sts === LaborStatusEnum.Declined) { Part.Sts = LaborStatusEnum.New; } }); return Parts; } static ChangeDeclinedColliStatusToNew(Collisions: any) { Collisions.forEach((Collision: any) => { if (Collision.Sts === LaborStatusEnum.Declined) { Collision.Sts = LaborStatusEnum.New; } }); return Collisions; } static GetValidItemsForTotals(RecordData: any) { let RecordStatus: string = RecordData.Sts; RecordData.Ops = this.GetLaborByRecordAndLaborStatus(RecordStatus, RecordData.Ops); RecordData.Parts = this.GetPartsByRecordAndPartStatus(RecordStatus, RecordData.Parts); RecordData.Colli = this.GetCollisionByRecordAndColliStatus(RecordStatus, RecordData.Colli); return RecordData; } static GetLaborByRecordAndLaborStatus(RecordStatus: string, Operations: any) { if (RecordStatus === ROStatusEnum.Cancelled || RecordStatus === ROStatusEnum.Declined) { return Operations; } else { return this.GetValidROLaborListByStatus(Operations); } } static GetValidROLaborListByStatus(Operations: any) { let opCodesList: any[] = Operations.filter((Operation: any) => { if (this.CheckOperationHasActiveStatus(Operation.Sts)) { return Operation; } }); return opCodesList; } static CheckOperationHasActiveStatus(Status: string) { return (Status !== LaborStatusEnum.Cancelled && Status !== LaborStatusEnum.Declined) ? true : false; } static GetPartsByRecordAndPartStatus(RecordStatus: any, Parts: any) { if (RecordStatus === ROStatusEnum.Cancelled || RecordStatus === ROStatusEnum.Declined) { return Parts; } else { return this.GetValidROPartsListByStatus(Parts); } } static GetValidROPartsListByStatus(Parts: any) { let PartsList: any[] = Parts.filter((Part: any) => { if (TrUtils.IsNull(Part.CollId)) { return Part; } else { if (this.CheckOperationHasActiveStatus(Part.Sts)) { return Part; } } }); return PartsList; } static GetCollisionByRecordAndColliStatus(RecordStatus: string, Collisions: any) { if (RecordStatus === ROStatusEnum.Cancelled || RecordStatus === ROStatusEnum.Declined) { return Collisions; } else { return this.GetValidROCollisionListByStatus(Collisions); } } static GetValidROCollisionListByStatus(Collisions: any) { let collList: any[] = Collisions.filter((Collision: any) => { if (this.CheckOperationHasActiveStatus(Collision.Sts)) { return Collision; } }); return collList; } static SeparateROLaborByPType(Labors: any, PType: any, CustomerOnly: boolean, InsuranceOnly: boolean) { let LaborList: any[] = []; LaborList = Labors.filter((Labor: any) => { if (PType === PayTypeEnum.Customer) { return !TrUtils.IsZero(Labor.Pr); } else { return !TrUtils.IsZero(Labor.AssPr); } }); return LaborList; } static SeparateROPartsByPType(Parts: any, PType: any, CustomerOnly: boolean, InsuranceOnly: boolean) { let PartsList: any[] = []; PartsList = Parts.filter((Part: any) => { if (PType === PayTypeEnum.Customer) { return !TrUtils.IsZero(Part.UnPr); } else { return !TrUtils.IsZero(Part.AssPr); } }); return PartsList; } static SeparateROCollisionsByPType(Collisions: any, Ops: any, Parts: any) { let CollisionList: any[] = Collisions.filter((Collision: any) => { let LaborIndex: any = Ops.findIndex((Labor: any) => { return Labor.CollId === Collision._id; }); let PartIndex: any = Parts.findIndex((Part: any) => { return Part.CollId === Collision._id; }); if (LaborIndex !== -1 || PartIndex !== -1) { return Collision; } }); return CollisionList; } static ResetQtyAndUoM(Parts: any) { Parts.forEach((Item: any) => { if (!TrUtils.IsZero(Item.Qty) && !TrUtils.CheckInvalidSelect(Item.UoM)) { Item.QtyAndUoM = Item.Qty + ' ' + Item.UoM; } }); return Parts; } static CheckAndShowItemsAsAR(argROData: any) { let AccPartsIndex: any = argROData.Parts.findIndex((Part: any) => { return !TrUtils.IsNull((Part.CollId)); }); let InsLaborIndex: any = argROData.Ops.findIndex((Labor: any) => { return !TrUtils.IsNull((Labor.CollId)); }); if (AccPartsIndex !== -1 || InsLaborIndex !== -1) { return true; } else { return false; } } static ConvertItemsAsAR(ROPrintData: any) { ROPrintData.Colli.forEach((Collision: any) => { Collision = this.ResetItemsDataById(Collision, ROPrintData.Ops, ROPrintData.Parts); }); ROPrintData.Ops.forEach((Labor: any) => { if (TrUtils.IsNull(Labor.CollId)) { let LaborColli: any = { _id: Labor._id, Desc: Labor.Desc, PBy: Labor.PBy, Op_Id: Labor.Op_Id, Qty: 0, Sts: Labor.Sts }; LaborColli.RRFData = Labor; ROPrintData.Colli.push(LaborColli); } }); ROPrintData.Parts.forEach((Part: any) => { if (TrUtils.IsNull(Part.CollId)) { let LaborIndex: any = ROPrintData.Ops.findIndex((Labor: any) => { return Labor._id === Part.OpId; }); let OpsStatus: string = ROPrintData.Ops[LaborIndex].Sts; let PartColli: any = { _id: Part._id, Desc: Part.Desc, PBy: Part.PBy, Sts: OpsStatus }; PartColli.PartData = Part; ROPrintData.Colli.push(PartColli); } }); ROPrintData.Ops = []; ROPrintData.Parts = []; return ROPrintData; } static ResetItemsDataById(Collision: any, AddedOperations: any[], AddedParts: any[]) { Collision.PartData = TrUtils.IsNull(Collision.PartId) ? null : this.GetPartDataById(Collision.PartId, AddedParts); Collision.RRFData = TrUtils.IsNull(Collision.RRFId) ? null : this.GetLaborDataById(Collision.RRFId, AddedOperations); Collision.PtngData = TrUtils.IsNull(Collision.PainId) ? null : this.GetLaborDataById(Collision.PainId, AddedOperations); Collision.DtngData = TrUtils.IsNull(Collision.RepId) ? null : this.GetLaborDataById(Collision.RepId, AddedOperations); } static GetPartDataById(PartId: any, AddedParts: any[]) { let partIndex: any = AddedParts.findIndex((Part: any) => { return Part._id === PartId; }); return (partIndex !== -1) ? AddedParts[partIndex] : null; } static GetLaborDataById(LaborId: any, AddedOperations: any[]) { let laborIndex: any = AddedOperations.findIndex((Labor: any) => { return Labor._id === LaborId; }); return (laborIndex !== -1) ? AddedOperations[laborIndex] : null; } static EmptyInvalidCollisionRepairItems(Colli: any) { Colli.forEach((CollisionItem: any) => { if (!TrUtils.IsNull(CollisionItem.PartData) && !this.CheckOperationHasActiveStatus(CollisionItem.PartData.Sts)) { CollisionItem.PartData.AssPr = null; CollisionItem.PartData.UnPr = null; CollisionItem.PartData.Disc = 0; CollisionItem.PartData.RecDisc = 0; } if (!TrUtils.IsNull(CollisionItem.RRFData) && !this.CheckOperationHasActiveStatus(CollisionItem.RRFData.Sts)) { CollisionItem.RRFData.AssPr = null; CollisionItem.RRFData.Pr = null; CollisionItem.RRFData.Disc = 0; CollisionItem.RRFData.RecDisc = 0; } if (!TrUtils.IsNull(CollisionItem.DtngData) && !this.CheckOperationHasActiveStatus(CollisionItem.DtngData.Sts)) { CollisionItem.DtngData.AssPr = null; CollisionItem.DtngData.Pr = null; CollisionItem.DtngData.Disc = 0; CollisionItem.DtngData.RecDisc = 0; } if (!TrUtils.IsNull(CollisionItem.PtngData) && !this.CheckOperationHasActiveStatus(CollisionItem.PtngData.Sts)) { CollisionItem.PtngData.AssPr = null; CollisionItem.PtngData.Pr = null; CollisionItem.PtngData.Disc = 0; CollisionItem.PtngData.RecDisc = 0; } }); return Colli; } static GetLineTotals(ROPrintData: any, IncludeGST: boolean, ConsolidateGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, DecimalsNumber: number) { ROPrintData.Ops.forEach((Labor: any) => { Labor.LineTotal = this.GetLaborLineTotal(Labor, IncludeGST, AsCustomerOnly, AsInsuranceOnly, DecimalsNumber); }); ROPrintData.Parts.forEach((Part: any) => { Part.LineTotal = this.GetPartLineTotal(Part, IncludeGST, AsCustomerOnly, AsInsuranceOnly, DecimalsNumber); }); ROPrintData.Colli.forEach((Collision: any) => { let LineTotal: number = 0; if (!TrUtils.IsNull(Collision.PartData)) { LineTotal = Add(LineTotal,this.GetCollisionPartLineTotal(Collision.PartData, ConsolidateGST, AsCustomerOnly, AsInsuranceOnly)); } if (!TrUtils.IsNull(Collision.RRFData)) { LineTotal = Add(LineTotal,this.GetCollisionLaborLineTotal(Collision.RRFData, ConsolidateGST, AsCustomerOnly, AsInsuranceOnly)); } if (!TrUtils.IsNull(Collision.PtngData)) { LineTotal = Add(LineTotal,this.GetCollisionLaborLineTotal(Collision.PtngData, ConsolidateGST, AsCustomerOnly, AsInsuranceOnly)); } if (!TrUtils.IsNull(Collision.DtngData)) { LineTotal = Add(LineTotal,this.GetCollisionLaborLineTotal(Collision.DtngData, ConsolidateGST, AsCustomerOnly, AsInsuranceOnly)); } Collision.LineTotal = TrUtils.FixPriceValue(LineTotal, DecimalsNumber); }); return ROPrintData; } static GetCollisionPartLineTotal(PartData: any, ConsolidateGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean) { let LineTotal: number = 0; if (AsCustomerOnly) { if (ConsolidateGST) { LineTotal = PartData.CustAfterTaxPerItem; } else { LineTotal = PartData.CustAfterPartDisc; } } else if (AsInsuranceOnly) { if (ConsolidateGST) { LineTotal = PartData.InsAfterTaxPerItem; } else { LineTotal = PartData.InsAfterPartDisc; } } else { if (ConsolidateGST) { LineTotal = Add(PartData.InsAfterTaxPerItem , PartData.CustAfterTaxPerItem); } else { LineTotal = Add(PartData.InsAfterPartDisc , PartData.CustAfterPartDisc); } } return Number(LineTotal); } static GetCollisionLaborLineTotal(LaborData: any, ConsolidateGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean) { let LineTotal: number = 0; if (AsCustomerOnly) { if (ConsolidateGST) { LineTotal = LaborData.CustAfterTax; } else { LineTotal = LaborData.CustAfterLaborDisc; } } else if (AsInsuranceOnly) { if (ConsolidateGST) { LineTotal = LaborData.InsAfterTax; } else { LineTotal = LaborData.InsAfterLaborDisc; } } else { if (ConsolidateGST) { LineTotal = Add(LaborData.CustAfterTax , LaborData.InsAfterTax); } else { LineTotal = Add(LaborData.InsAfterLaborDisc , LaborData.CustAfterLaborDisc); } } return Number(LineTotal); } static GetLaborLineTotal(LaborData: any, IncludeGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, DecimalsNumber: number) { let LineTotal: number = 0; if (AsCustomerOnly) { if (IncludeGST) { LineTotal = LaborData.CustAfterTax; } else { LineTotal = LaborData.CustAfterLaborDisc; } } else if (AsInsuranceOnly) { if (IncludeGST) { LineTotal = LaborData.InsAfterTax; } else { LineTotal = LaborData.InsAfterLaborDisc; } } else { if (IncludeGST) { LineTotal = Add(LaborData.CustAfterTax , LaborData.InsAfterTax); } else { LineTotal = Add(LaborData.InsAfterLaborDisc , LaborData.CustAfterLaborDisc); } } return TrUtils.FixPriceValue(LineTotal, DecimalsNumber); } static GetPartLineTotal(PartData: any, IncludeGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, DecimalsNumber: number) { let LineTotal: number = 0; if (AsCustomerOnly) { if (IncludeGST) { LineTotal = PartData.CustAfterTax; } else { LineTotal = PartData.CustAfterPartDisc; } } else if (AsInsuranceOnly) { if (IncludeGST) { LineTotal = PartData.InsAfterTax; } else { LineTotal = PartData.InsAfterPartDisc; } } else { if (IncludeGST) { LineTotal = Add(PartData.InsAfterTax , PartData.CustAfterTax); } else { LineTotal = Add(PartData.InsAfterPartDisc , PartData.CustAfterPartDisc); } } return TrUtils.FixPriceValue(LineTotal, DecimalsNumber); } static ReverseItemsForROPrint(argROData: any, isReverse:boolean) { if(isReverse){ argROData.Comps = TrUtils.IsNull(argROData.Comps) ? [] : argROData.Comps.reverse(); argROData.Ops = TrUtils.IsNull(argROData.Ops) ? [] : argROData.Ops.reverse(); argROData.Parts = TrUtils.IsNull(argROData.Parts) ? [] : argROData.Parts.reverse(); argROData.Colli = TrUtils.IsNull(argROData.Colli) ? [] : argROData.Colli.reverse(); }else{ argROData.Comps = TrUtils.IsNull(argROData.Comps) ? [] : argROData.Comps; argROData.Ops = TrUtils.IsNull(argROData.Ops) ? [] : argROData.Ops; argROData.Parts = TrUtils.IsNull(argROData.Parts) ? [] : argROData.Parts; argROData.Colli = TrUtils.IsNull(argROData.Colli) ? [] : argROData.Colli; } let ReversedLabors: any[] = []; argROData.Comps.forEach((Complaint: any) => { argROData.Ops.forEach((Labor: any) => { if (Labor.CompId === Complaint._id) { ReversedLabors.push(Labor); } }); }); argROData.Ops.forEach((Labor: any) => { if (TrUtils.IsNull(Labor.CompId)) { ReversedLabors.push(Labor); } }); argROData.Ops = ReversedLabors; let ReversedParts: any[] = []; argROData.Ops.forEach((Labor: any) => { argROData.Parts.forEach((Part: any) => { if (TrUtils.IsNull(Part.CollId) && (Part.OpId === Labor._id)) { ReversedParts.push(Part); } }); }); argROData.Parts.forEach((Part: any) => { if (!TrUtils.IsNull(Part.CollId)) { ReversedParts.push(Part); } }); argROData.Parts = ReversedParts; return argROData; } static GetDetailedInfoForItems(ROTotalsData: any, IncludeGST: boolean, ConsolidateGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, TaxCodes: any[], DecimalsNumber: number) { ROTotalsData.Ops = this.GetLaborPrintInfo(ROTotalsData.Ops, ConsolidateGST, IncludeGST, AsCustomerOnly, AsInsuranceOnly, TaxCodes, DecimalsNumber); ROTotalsData.Parts = this.GetItemsPrintInfo(TrUtils.Stringify(ROTotalsData.Parts), ConsolidateGST, IncludeGST, AsCustomerOnly, AsInsuranceOnly, TaxCodes, DecimalsNumber); if (IncludeGST && !ConsolidateGST) { ROTotalsData.ShowDetailedLaborTaxInfo = this.CheckLaborTaxItemIndex(ROTotalsData.Ops) !== -1; ROTotalsData.ShowDetailedPartTaxInfo = this.CheckPartTaxItemIndex(ROTotalsData.Parts) !== -1; } else { ROTotalsData.ShowDetailedLaborTaxInfo = false; ROTotalsData.ShowDetailedPartTaxInfo = false; } return ROTotalsData; } static GetLaborPrintInfo(LaborList: any, ConsolidateGST: boolean, IncludeGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, _TaxCodes: any[], DecimalsNumber:number) { LaborList.forEach((Labor: any) => { Labor.HSN = Labor.SAC; let taxGroupKey = 'CTaxes'; if (AsCustomerOnly) { if (IncludeGST) { if (ConsolidateGST) { Labor.UnPr = TrUtils.FixedTo(Labor.CustAfterTax, DecimalsNumber); } else { Labor.UnPr = TrUtils.FixedTo(Labor.Pr, DecimalsNumber); } Labor.CGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Labor, 'CTaxes', 'CGST', 'CCGST'), DecimalsNumber); Labor.SGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Labor, 'CTaxes', 'SGST', 'CSGST'), DecimalsNumber); Labor.IGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Labor, 'CTaxes', 'IGST', 'CIGST'), DecimalsNumber); } else { Labor.UnPr = TrUtils.FixedTo(Labor.Pr, DecimalsNumber); Labor.CGSTAmt = 0; Labor.SGSTAmt = 0; Labor.IGSTAmt = 0; } } else if (AsInsuranceOnly) { taxGroupKey = 'ATaxes'; if (IncludeGST) { if (ConsolidateGST) { Labor.UnPr = TrUtils.FixedTo(Labor.InsAfterTax, DecimalsNumber); } else { Labor.UnPr = TrUtils.FixedTo(Labor.AssPr, DecimalsNumber); } Labor.CGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Labor, 'ATaxes', 'CGST', 'ACGST'), DecimalsNumber); Labor.SGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Labor, 'ATaxes', 'SGST', 'ASGST'), DecimalsNumber); Labor.IGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Labor, 'ATaxes', 'IGST', 'AIGST'), DecimalsNumber); Labor.AssPr = TrUtils.FixedTo(Labor.AssPr, DecimalsNumber); } else { Labor.UnPr = TrUtils.FixedTo(Labor.AssPr, DecimalsNumber); Labor.AssPr = TrUtils.FixedTo(Labor.AssPr, DecimalsNumber); Labor.CGSTAmt = 0; Labor.SGSTAmt = 0; Labor.IGSTAmt = 0; } } Labor.TCode = this.GetTCode(Labor); Labor.CGSTPerc = this.GetTaxRate(Labor, 'CGST', taxGroupKey); Labor.SGSTPerc = this.GetTaxRate(Labor, 'SGST', taxGroupKey); Labor.IGSTPerc = this.GetTaxRate(Labor, 'IGST', taxGroupKey); }); return LaborList; } static GetItemsPrintInfo(Items: any, ConsolidateGST: boolean, IncludeGST: boolean, AsCustomerOnly: boolean, AsInsuranceOnly: boolean, _TaxCodes: any[], DecimalsNumber: number) { Items.forEach((Item: any) => { let taxGroupKey = 'CTaxes'; if (AsCustomerOnly) { if (IncludeGST) { if (ConsolidateGST) { Item.UnPr = TrUtils.FixedTo(Item.CustAfterTaxPerItem, DecimalsNumber); } else { Item.UnPr = TrUtils.FixedTo(Item.UnPr, DecimalsNumber); } Item.CGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Item, 'CTaxes', 'CGST', 'CCGST'), DecimalsNumber); Item.SGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Item, 'CTaxes', 'SGST', 'CSGST'), DecimalsNumber); Item.IGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Item, 'CTaxes', 'IGST', 'CIGST'), DecimalsNumber); } else { Item.UnPr = TrUtils.FixedTo(Item.UnPr, DecimalsNumber); Item.CGSTAmt = 0; Item.SGSTAmt = 0; Item.IGSTAmt = 0; } } else if (AsInsuranceOnly) { taxGroupKey = 'ATaxes'; if (IncludeGST) { if (ConsolidateGST) { Item.UnPr = TrUtils.FixedTo(Item.InsAfterTaxPerItem, DecimalsNumber); } else { Item.UnPr = TrUtils.FixedTo(Item.AssPr, DecimalsNumber); } Item.CGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Item, 'ATaxes', 'CGST', 'ACGST'), DecimalsNumber); Item.SGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Item, 'ATaxes', 'SGST', 'ASGST'), DecimalsNumber); Item.IGSTAmt = TrUtils.FixedTo(this.GetTaxAmountFromGroup(Item, 'ATaxes', 'IGST', 'AIGST'), DecimalsNumber); Item.AssPr = TrUtils.FixedTo(Item.AssPr, DecimalsNumber); } else { Item.UnPr = TrUtils.FixedTo(Item.AssPr, DecimalsNumber); Item.AssPr = TrUtils.FixedTo(Item.AssPr, DecimalsNumber); Item.CGSTAmt = 0; Item.SGSTAmt = 0; Item.IGSTAmt = 0; } } if (!TrUtils.IsZero(Item.Qty) && !TrUtils.CheckInvalidSelect(Item.UoM)) { Item.QtyAndUoM = Item.Qty + ' ' + Item.UoM; } Item.TCode = this.GetTCode(Item); Item.CGSTPerc = this.GetTaxRate(Item, 'CGST', taxGroupKey); Item.SGSTPerc = this.GetTaxRate(Item, 'SGST', taxGroupKey); Item.IGSTPerc = this.GetTaxRate(Item, 'IGST', taxGroupKey); }); return Items; } static CheckLaborTaxItemIndex(LaborList: any[]) { let TaxIndex: any = LaborList.findIndex((Labor: any) => { if (!TrUtils.IsNull(this.GetTCode(Labor))) { return Labor; } }); return TaxIndex; } static CheckPartTaxItemIndex(PartsList: any[]) { let TaxIndex: any = PartsList.findIndex((Part: any) => { if (!TrUtils.IsNull(this.GetTCode(Part))) { return Part; } }); return TaxIndex; } static PrepareFormatForROPrint(ROPrintData: any, For: string, argROType: any) { let PrintInfo: any[] = []; let ROLaborParts: any = this.GetFormatForROPrint(For, ROPrintData, argROType); PrintInfo.push(ROLaborParts); return PrintInfo; } static GetFormatForROPrint(For: any, ROPrintData: any, argROType: any) { let ROLaborParts: any = {}; ROLaborParts.Ops = ROPrintData.Ops; ROLaborParts.Parts = ROPrintData.Parts; ROLaborParts.Colli = ROPrintData.Colli; let NewOpFound: any = ROLaborParts.Ops.findIndex((Labor: any) => { return Labor.Sts === LaborStatusEnum.New || Labor.Sts === LaborStatusEnum.WtngForAppr; }); let NewPartFound: any = ROLaborParts.Parts.findIndex((Part: any) => { return !TrUtils.IsNull(Part.CollId) && (Part.Sts === LaborStatusEnum.New || Part.Sts === LaborStatusEnum.WtngForAppr); }); let NewIndPartFound: any = ROLaborParts.Colli.findIndex((IndPart: any) => { return IndPart.Sts === LaborStatusEnum.New || IndPart.Sts === LaborStatusEnum.WtngForAppr; }); if (NewOpFound !== -1 || NewIndPartFound !== -1 || NewPartFound !== -1) { ROLaborParts.Sts = ROStatusEnum.New; } else { ROLaborParts.Sts = ROStatusEnum.Approved; } ROLaborParts.Type = argROType; return ROLaborParts; } static CheckAndCombineDescMPNForAR(PrintPartNo: boolean, ROPrintData: any) { if (PrintPartNo) { ROPrintData.PrintInfo.forEach((PrintData: any) => { PrintData.Colli.forEach((Collision: any) => { if (!TrUtils.IsNull(Collision.PartData) && !TrUtils.IsEmpty(Collision.PartData.MPN)) { Collision.Desc = Collision.Desc + '/' + Collision.PartData.MPN; } }); }); } return ROPrintData; } }