import * as XLSX from 'xlsx'; import { MyDate } from '../../../utils/my-date'; import { TrUtils } from '../../../utils/tr-utils'; export class RepairOrdersXlsxFileService { 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 GetRepairOrdersExcelData(MainData: 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 = 'Repair Order List'; this.wb.Sheets = {}; this.wb.Props = {}; this.wb.SSF = {}; this.wb.SheetNames = []; MainData = this.GetTotals(MainData, EntitySettings.DecimalsNumber); this.setHeadingInCell(); this.setWorkOrderDetailsInCell(MainData, EntitySettings.DecimalsNumber); this.ws['!ref'] = XLSX.utils.encode_range(this.range); this.ws['!merges'] = this.MergeArray; this.wb.SheetNames.push(ws_name); this.wb.Sheets[ws_name] = this.ws; return this.wb; } static GetTotals(MainData: any, DecimalsNumber: number) { MainData.forEach((WorkOrder: any) => { if (TrUtils.IsNull(WorkOrder.Prod?.VIN)) { WorkOrder.Prod.VIN = ''; } if (TrUtils.IsNull(WorkOrder.Name)) { WorkOrder.Name = ''; } if (TrUtils.IsNull(WorkOrder.TeNa)) { WorkOrder.TeNa = ''; } if (TrUtils.IsNull(WorkOrder.Type)) { WorkOrder.TypeName = ''; } else { WorkOrder.TypeName = WorkOrder.WOType.Name; } if (!TrUtils.IsNull(WorkOrder.Total)) { WorkOrder.Total = TrUtils.FixPriceValue(WorkOrder.Total, DecimalsNumber); } else { WorkOrder.Total = '0.00'; } }); return MainData; } static setHeadingInCell() { let MainHeadings: any[] = [ { text: 'SNo', ColRange: 1, bold: true, }, { text: 'RO No', ColRange: 1, bold: true, }, { text: 'RO Date', ColRange: 1, bold: true, }, { text: 'RO Status', ColRange: 1, bold: true, }, { text: 'Customer', ColRange: 1, bold: true, }, { text: 'Phone', ColRange: 1, bold: true, }, { text: 'Reg. No', ColRange: 1, bold: true, }, { text: 'VIN', ColRange: 1, bold: true, }, { text: 'Model', ColRange: 1, bold: true, }, { text: 'Sale Data', ColRange: 1, bold: true, }, { text: 'Mileage In', ColRange: 1, bold: true, }, { text: 'Work Type', ColRange: 1, bold: true, }, { text: 'Service Advisor', ColRange: 1, bold: true, }, { text: 'Technician', ColRange: 1, bold: true, }, { text: 'Total Amount', ColRange: 1, bold: true, }, ]; 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 setWorkOrderDetailsInCell(MainData: any, DecimalsNumber: number) { MainData.forEach((InvoiceList: any, index: any) => { this.SetWorkOrderDataInExcel(InvoiceList, index, DecimalsNumber); }); } static SetWorkOrderDataInExcel(WorkOrder: any, index: any, DecimalsNumber: number) { WorkOrder.CrDate = MyDate.ConvertUTCDateToReadable(WorkOrder.CrDate); if(!TrUtils.IsNull(WorkOrder.Prod) && !TrUtils.IsNull(WorkOrder.Prod.DDate)){ WorkOrder.Prod.DDate= MyDate.ConvertUTCDateToReadable(WorkOrder.Prod.DDate); }else{ WorkOrder.Prod.DDate=''; } let InvoiceData: any = [ { text: index + 1, ColRange: 1, IsString: true }, { text: WorkOrder.Code, ColRange: 1, IsString: true }, { text: WorkOrder.CrDate, ColRange: 1, IsString: true }, { text: WorkOrder.Sts, ColRange: 1, IsString: true }, { text: WorkOrder.CBillTo?.Name, ColRange: 1, IsString: true }, { text: WorkOrder.CBillTo?.Ph, ColRange: 1, IsString: true }, { text: WorkOrder.Prod?.RegNo, ColRange: 1, IsString: false }, { text: WorkOrder.Prod?.VIN, ColRange: 1, IsString: false }, { text: WorkOrder.Prod?.Model, ColRange: 1, IsString: false }, { text: WorkOrder.Prod?.DDate, ColRange: 1, IsString: false }, { text: WorkOrder.Prod?.MIn, ColRange: 1, IsString: false }, { text: WorkOrder.TypeName, ColRange: 1, IsString: false }, { text: WorkOrder.Name, ColRange: 1, IsString: false }, { text: WorkOrder.TeNa, ColRange: 1, IsString: false }, { text: WorkOrder.Total, 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; } }