import * as XLSX from 'xlsx'; import { MyDate } from '../../../../utils/my-date'; import { TrUtils } from '../../../../utils/tr-utils'; export class PaymentReceiveXlsxFileService { 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 = 'RECEIPTS'; 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.Amt)) { WorkOrder.Total = TrUtils.FixPriceValue(WorkOrder.Amt, DecimalsNumber) } else { WorkOrder.Total = '0.00'; } }); return MainData; } static setHeadingInCell() { let MainHeadings: any[] = [ { text: 'SNo', ColRange: 1, bold: true, }, { text: 'Payment No', ColRange: 1, bold: true, }, { text: 'Date', ColRange: 1, bold: true, }, { text: 'Customer Name', ColRange: 1, bold: true, }, { text: 'Payment Mode', ColRange: 1, bold: true, }, // { // text: 'Reg. No', // ColRange: 1, // bold: true, // }, { text: 'Invoice No\'s', ColRange: 1, bold: true, }, { text: '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.Date = MyDate.ConvertUTCDateToReadable(WorkOrder.Date); WorkOrder.InvCodes = this.GetInvoiceCodes(WorkOrder); let InvoiceData: any = [ { text: index + 1, ColRange: 1, IsString: true }, { text: WorkOrder.Code, ColRange: 1, IsString: true }, { text: WorkOrder.Date, ColRange: 1, IsString: true }, { text: WorkOrder.Name, ColRange: 1, IsString: true }, { text: WorkOrder.PType, ColRange: 1, IsString: true }, // { text: WorkOrder.RegNo, ColRange: 1, IsString: true }, { text: WorkOrder.InvCodes, 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?.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 GetInvoiceCodes(PaymentData: any) { let InvoiceCodes: string = ''; if (!TrUtils.IsNull(PaymentData.Invs)) { PaymentData.Invs.forEach((inv: any, index: any) => { InvoiceCodes = InvoiceCodes + inv.Code; if (PaymentData.Invs.length !== (index + 1)) { InvoiceCodes = InvoiceCodes + ', '; } }); } return InvoiceCodes; } 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; } }