import * as XLSX from 'xlsx'; import { TrUtils } from '../../../../utils/tr-utils'; export class CustBalanceXlsxFileService { 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, isPhar: boolean, 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 = 'OUT STANDING REPORT'; this.wb.Sheets = {}; this.wb.Props = {}; this.wb.SSF = {}; this.wb.SheetNames = []; if (!TrUtils.IsNull(WOTypes) && WOTypes?.length !== 0) { MainData = this.GetTotals(MainData, WOTypes); } this.setHeadingInCell(isPhar); this.setInvoiceDetailsInCell(MainData, isPhar, EntitySettings.DecimalsNumber); this.ws['!ref'] = XLSX.utils.encode_range(this.range); this.ws['!merges'] = this.MergeArray; this.ws["!cols"] = [{ wch: 10 }, { wch: 15 }, { wch: 15 }, { wch: 35 }, { wch: 15 }, { wch: 15 }, { wch: 20 }, { wch: 15 }, { wch: 15 }, { wch: 20 }]; 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(isPhar: boolean) { let MainHeadings: any[] = [ { text: 'SNo', ColRange: 1, bold: true, ChildHeadings: [], }, { text: 'Date', ColRange: 1, bold: true, ChildHeadings: [], }, { text: isPhar ? 'Counter Sale No.' : 'Invoice No.', ColRange: 1, bold: true, ChildHeadings: [ ], }, { text: 'Customer Name', ColRange: 1, bold: true, ChildHeadings: [ ], }, { text: 'Amount', ColRange: 1, bold: true, ChildHeadings: [], }, // { // text: 'Due', // ColRange: 1, // bold: true, // ChildHeadings: [ // ], // }, ]; if (!isPhar) { MainHeadings.splice(4, 0, { text: 'Vehicle Reg No', ColRange: 1, bold: true, ChildHeadings: [ ], }, { text: 'Model', ColRange: 1, bold: true, ChildHeadings: [ ], }, { text: 'Advisor Name', ColRange: 1, bold: true, ChildHeadings: [ ], }, { text: 'RO NO.', ColRange: 1, bold: true, ChildHeadings: [ ], }, { text: 'RO Type', ColRange: 1, bold: true, ChildHeadings: [ ], }); } 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 += 1; } static setInvoiceDetailsInCell(MainData: any, isPhar: boolean, DecimalsNumber: number) { MainData.forEach((Record: any, index: any) => { // this.setHeading1InCell(Record.Name); // this.setHeadingInCell(); this.SetInvoiceDataInExcel(Record, index, isPhar, DecimalsNumber); // Record.Invoices.forEach((Invoice:any, index:any)=>{ // this.SetInvoiceDataInExcel(Invoice, index); // if (Record.Invoices.length === (index + 1)) { // this.setInvoiceFinalRowInCell(Record.Balance); // } // }); // this.Row += 1; }); } static SetInvoiceDataInExcel(InvoiceInfo: any, index: any, isPhar: boolean, DecimalsNumber: number) { // InvoiceInfo.CrDate = this.ConvertDateToReadableFormat(InvoiceInfo.CrDate); InvoiceInfo.CrDate = this.ConvertDateToReadableFormat(InvoiceInfo.CrDate); let InvoiceData: any = [ { text: index + 1, ColRange: 1, IsString: true }, { text: InvoiceInfo.CrDate, ColRange: 1, IsString: true }, { text: InvoiceInfo.Code, ColRange: 1, IsString: true }, { text: InvoiceInfo.BillTo?.Name, ColRange: 1, IsString: true }, { text: TrUtils.FixPriceValue(InvoiceInfo.Total, DecimalsNumber), ColRange: 1, IsString: false }, // { text: InvoiceInfo.Due, ColRange: 1, IsString: false }, ]; if (!isPhar) { InvoiceData.splice(4, 0, { text: InvoiceInfo.Prod?.RegNo, ColRange: 1, IsString: true }, { text: InvoiceInfo.Prod?.Model, ColRange: 1, IsString: true }, { text: InvoiceInfo.Name, ColRange: 1, IsString: true }, { text: InvoiceInfo.WCode, ColRange: 1, IsString: true }, { text: InvoiceInfo.TypeName, ColRange: 1, IsString: true }); } 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; } } }