import * as XLSX from 'xlsx'; import { MyDate } from '../../../../utils/my-date'; import { TrUtils } from '../../../../utils/tr-utils'; import { Add } from '../../../../shared/math-operations'; export class ServiceAdvisorWisePartXlsxFileService { 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 GetServiceAdvisorPartsExcelData(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 = 'SA Wise Part Sales'; this.wb.Sheets = {}; this.wb.Props = {}; this.wb.SSF = {}; this.wb.SheetNames = []; // MainData = this.GetTotals(MainData); this.setHeadingInCell(); this.setWorkOrderDetailsInCell(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: 30},{wch: 40},{wch: 15},{wch: 15},{wch: 40},{wch: 15},{wch: 15},{wch: 15},{wch: 15}]; this.wb.SheetNames.push(ws_name); this.wb.Sheets[ws_name] = this.ws; return this.wb; } static setHeadingInCell() { let MainHeadings: any[] = [ // { // text: 'SNo', // ColRange: 1, // bold: true, // }, { text: 'Invoice No', ColRange: 1, bold: true, }, { text: 'Date', ColRange: 1, bold: true, }, { text: 'Service Advisor', 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: 'Description', ColRange: 1, bold: true, }, { text: 'Amount', ColRange: 1, bold: true, }, { text: 'Discount', ColRange: 1, bold: true, }, { text: 'Tax', ColRange: 1, bold: true, }, { text: 'Amount With Tax', ColRange: 1, bold: true, } ]; this.SetDataInCell('Service Advisor wise Part Sales', 0, this.Row); this.MergeArray.push({ s: { r: this.Row, c: 0 }, e: { r: this.Row, c: 10}, }); this.Row += 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 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); WorkOrder.Discount=TrUtils.FixPriceValue(Add(TrUtils.SetValueToZeroIfNull(WorkOrder.Items?.Disc), TrUtils.SetValueToZeroIfNull(WorkOrder.Items?.RecDisc)), DecimalsNumber); WorkOrder.Tax= TrUtils.FixPriceValue(Add(WorkOrder.Items?.CGST, WorkOrder.Items?.SGST, WorkOrder.Items?.IGST), DecimalsNumber); WorkOrder.Total= TrUtils.FixPriceValue(Add(WorkOrder.Items?.NetAmt, Number(WorkOrder.Tax)), DecimalsNumber); // if(!TrUtils.IsNull(WorkOrder.Prod) && !TrUtils.IsNull(WorkOrder.Prod.DDate)){ // WorkOrder.Prod.DDate= TrUtils.ConvertDateToReadableFormat(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.Name, ColRange: 1, IsString: true }, { text: WorkOrder.BillTo?.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.Items?.Desc, ColRange: 1, IsString: false }, { text: WorkOrder.Items?.UnAmt, ColRange: 1, IsString: false }, { text: WorkOrder.Discount, ColRange: 1, IsString: false }, { text: WorkOrder.Tax, 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; } }