import * as XLSX from 'xlsx'; import { TrUtils } from '../../../../utils/tr-utils'; export class IssueSparesXlsxFileService { static wb: any = {}; static Row: any = 2; static MergeArray: any[] = []; static range = { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }; static ws: any = {}; static GetInvoiceWiseExcelData(MainData: any, WOTypes: any, EntitySettings: any) { this.wb={}; this.ws = {}; this.Row=0; this.range = { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }; this.MergeArray=[]; let ws_name = 'Spares Issue'; this.wb.Sheets = {}; this.wb.Props = {}; this.wb.SSF = {}; this.wb.SheetNames = []; MainData = this.GetTotals(MainData, WOTypes); this.setHeadingInCell(); this.setInvoiceDetailsInCell(MainData, EntitySettings.DecimalsNumber); this.ws['!ref'] = XLSX.utils.encode_range(this.range); this.ws['!merges'] = this.MergeArray; this.ws["!cols"] = [{wch: 15},{wch: 15},{wch: 20},{wch: 20},{wch: 10},{wch: 15},{wch: 20},{wch: 20},{wch: 10},{wch: 50},{wch: 40}]; this.wb.SheetNames.push(ws_name); this.wb.Sheets[ws_name] = this.ws; return this.wb; } static GetTotals(MainData: any, WOTypes: any) { MainData.forEach((InvoiceInfo: any) => { if (InvoiceInfo.VIN == null) { InvoiceInfo.VIN = ''; } if (InvoiceInfo.Name == null) { InvoiceInfo.Name = ''; } if (InvoiceInfo.TeNa == null) { InvoiceInfo.TeNa = ''; } // if (InvoiceInfo.Type == null) { // InvoiceInfo.TypeName = ''; // } else { // InvoiceInfo.TypeName = this.GetWOTypeName(InvoiceInfo.Type, WOTypes); // } // InvoiceInfo.GSTAmount = // InvoiceInfo.ItemsCGST + InvoiceInfo.ItemsSGST + InvoiceInfo.ItemsIGST; // InvoiceInfo.TotalAmount = InvoiceInfo.GSTAmount + InvoiceInfo.ItemsNetAmt; // InvoiceInfo.OpsGSTAmount = // InvoiceInfo.OpsCGST + InvoiceInfo.OpsSGST + InvoiceInfo.OpsIGST; // InvoiceInfo.TotalOpsAmount = // InvoiceInfo.OpsGSTAmount + InvoiceInfo.OpsNetAmt; }); return MainData; } static GetWOTypeName(Type: any, WOTypes: any) { let Index: any = WOTypes.findIndex((wotype: any) => { return Type === wotype._id; }); if (Index !== -1) { return WOTypes[Index].Name; } else { return ''; } } static setHeadingInCell() { // let MainHeadings: any[] = [ // { // text: 'GSTIN/UIN', // ColRange: 1, // bold: true, // ChildHeadings: [] // }, // { // text: 'Name', // ColRange: 1, // bold: true, // ChildHeadings: [] // }, // { // text: 'Invoice Details', // ColRange: 4, // bold: true, // ChildHeadings: [ // { text: 'No', ColRange: 1 }, // { text: 'Date', ColRange: 1 }, // { text: 'Invoice Total', ColRange: 1 } // ] // }, // { // text: 'Rate', // ColRange: 1, // bold: true, // ChildHeadings: [] // }, // { // text: 'Taxable Value', // ColRange: 1, // bold: true, // ChildHeadings: [] // }, // { // text: 'Amount', // ColRange: 3, // bold: true, // ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, // ] // } // ] let MainHeadings: any[] = [ // { // text: 'Date', // ColRange: 1, // bold: true, // ChildHeadings: [], // }, // { // text: 'Invoice No', // ColRange: 1, // bold: true // }, { text: 'Date', ColRange: 1, bold: true, ChildHeadings: [], }, { text: 'RO No', ColRange: 1, bold: true, ChildHeadings: [], }, // { // text: 'Customer', // ColRange: 1, // bold: true, // ChildHeadings: [ // // { text: 'Central Tax', ColRange: 1 }, // // { text: 'State Tax', ColRange: 1 }, // // { text: 'IGST Tax', ColRange: 1 }, // ], // }, { text: 'Chassis No', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Vehicle', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, // { // text: 'Color', // ColRange: 1, // bold: true, // ChildHeadings: [ // // { text: 'Central Tax', ColRange: 1 }, // // { text: 'State Tax', ColRange: 1 }, // // { text: 'IGST Tax', ColRange: 1 }, // ] // }, { text: 'Year Of Model', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Reg. No', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, // { // text: 'Supervisor', // ColRange: 1, // bold: true, // ChildHeadings: [ // // { text: 'Central Tax', ColRange: 1 }, // // { text: 'State Tax', ColRange: 1 }, // // { text: 'IGST Tax', ColRange: 1 }, // ] // }, { text: 'Service Advisor', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Service Type', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Meter Reading', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Product', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Description', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Qty', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'UoM', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Unit Price', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Gross Amount', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Technician', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, ]; let MainColStart: any = 0; MainHeadings.forEach((MainHeader: any) => { this.SetDataInCell(MainHeader.text, MainColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: MainColStart }, e: { r: this.Row, c: MainColStart + MainHeader.ColRange - 1 }, }); MainColStart += MainHeader.ColRange; }); this.Row += 2; } static setInvoiceDetailsInCell(MainData: any, DecimalsNumber: number) { MainData.forEach((InvoiceList: any) => { this.SetInvoiceDataInExcel(InvoiceList, DecimalsNumber); }); } static SetInvoiceDataInExcel(InvoiceInfo: any, DecimalsNumber: number) { InvoiceInfo.CrDate = this.ConvertDateToReadableFormat(InvoiceInfo.CrDate); InvoiceInfo.WCrDate = this.ConvertDateToReadableFormat(InvoiceInfo.WCrDate); if(TrUtils.IsNull(InvoiceInfo.Prod)){ InvoiceInfo.Prod={}; } if(TrUtils.IsNull(InvoiceInfo.WOType)){ InvoiceInfo.WOType={}; } let Product:string=''; let Price:any=0; let UnAmt:any=0; if(!TrUtils.IsNull(InvoiceInfo.Parts)){ if(!TrUtils.IsNull(InvoiceInfo.Parts.MPN)){ Product= InvoiceInfo.Parts.MPN + ' - '+InvoiceInfo.Parts.Desc; }else{ Product= InvoiceInfo.Parts.Desc; } if(!TrUtils.IsZero(InvoiceInfo.Parts.AssPr)){ Price=TrUtils.FixPriceValue(InvoiceInfo.Parts.AssPr, DecimalsNumber); }else { Price=TrUtils.FixPriceValue(InvoiceInfo.Parts.UnPr, DecimalsNumber); } if(!TrUtils.IsZero(InvoiceInfo.Parts.AssAmt)){ UnAmt= TrUtils.FixPriceValue(InvoiceInfo.Parts.AssAmt, DecimalsNumber); } else { UnAmt=TrUtils.FixPriceValue(InvoiceInfo.Parts.UnAmt, DecimalsNumber); }; } if(TrUtils.IsNull(InvoiceInfo.Parts)){ InvoiceInfo.Parts={}; } let InvoiceData: any = [ // { text: InvoiceInfo.CrDate, ColRange: 1, IsString: true }, // { text: InvoiceInfo.Code, ColRange: 1, IsString: true }, { text: InvoiceInfo.CrDate, ColRange: 1, IsString: true }, { text: InvoiceInfo.Code, ColRange: 1, IsString: true }, // { text: InvoiceInfo.CustName, ColRange: 1, IsString: true }, { text: InvoiceInfo.Prod.VIN, ColRange: 1, IsString: false }, { text: InvoiceInfo.Prod.Model, ColRange: 1, IsString: false }, { text: InvoiceInfo.Prod.Year, ColRange: 1, IsString: false }, { text: InvoiceInfo.Prod.RegNo, ColRange: 1, IsString: false }, { text: InvoiceInfo.Name, ColRange: 1, IsString: false }, { text: InvoiceInfo.WOType.Name, ColRange: 1, IsString: false }, { text: InvoiceInfo.MIn, ColRange: 1, IsString: false }, { text: Product, ColRange: 1, IsString: false }, { text: InvoiceInfo.Parts.Desc, ColRange: 1, IsString: false }, { text: InvoiceInfo.Parts.Qty, ColRange: 1, IsString: false }, { text: InvoiceInfo.Parts.UoM, ColRange: 1, IsString: false }, { text: Price, ColRange: 1, IsString: false }, { text: UnAmt, ColRange: 1, IsString: false }, { text: InvoiceInfo.TeNa, ColRange: 1, IsString: false }, ]; let ColStart: any = 0; InvoiceData.forEach((InvData: any) => { InvData.text = this.ConvertToString(InvData.text, InvData.IsString, DecimalsNumber); this.SetDataInCell(InvData.text, ColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: ColStart }, e: { r: this.Row, c: ColStart + InvData.ColRange - 1 }, }); ColStart += InvData.ColRange; }); this.Row += 1; } static ConvertToString(Text: any, IsString: any, DecimalsNumber: number) { if (IsString) { if (Text == null) { Text = ''; } } else { if (Text == null) { Text = 0; } } if (typeof Text === 'number') { return Number(TrUtils.FixedTo(Text, DecimalsNumber)); } else { return Text; } } static SetDataInCell(Data: any, ColRange: number, RowNum: any) { var cell = { v: Data }; var cell_ref = XLSX.utils.encode_cell({ c: ColRange, r: RowNum }); if (this.range.e.c < ColRange) { this.range.e.c = ColRange; } if (this.range.e.r < RowNum) { this.range.e.r = RowNum; } cell = this.getcelltype(cell); this.ws[cell_ref] = cell; } static getcelltype(cell: any) { if (typeof cell.v === 'number') cell.t = 'n'; else if (typeof cell.v === 'boolean') cell.t = 'b'; else cell.t = 's'; return cell; } static ConvertDateToReadableFormat(DateObject: any) { if (!TrUtils.IsEmpty(DateObject)) { let date: any = new Date(DateObject); let Month: any; let Day: any; let MonthName = new Array(); MonthName[0] = 'Jan'; MonthName[1] = 'Feb'; MonthName[2] = 'Mar'; MonthName[3] = 'Apr'; MonthName[4] = 'May'; MonthName[5] = 'Jun'; MonthName[6] = 'Jul'; MonthName[7] = 'Aug'; MonthName[8] = 'Sept'; MonthName[9] = 'Oct'; MonthName[10] = 'Nov'; MonthName[11] = 'Dec'; Month = MonthName[date.getMonth()]; if (date.getDate() < 10) { Day = '0' + date.getDate(); } else { Day = date.getDate(); } date = Day + '-' + Month + '-' + date.getFullYear(); return date; } else { return null; } } }