import * as XLSX from 'xlsx'; import { TrUtils } from '../../../../utils/tr-utils'; import { MyDate } from '../../../../utils/my-date'; export class AnalysisXlsxFileService { 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 GetAnalysisExcelData(MainData: any, isSale: boolean, searchValue: any, HeaderName: any, HeaderText:any, EntityData: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 = HeaderName; this.wb.Sheets = {}; this.wb.Props = {}; this.wb.SSF = {}; this.wb.SheetNames = []; // MainData = this.GetTotals(MainData); let showSearch: any = []; for (const key in searchValue) { if (!TrUtils.IsNull(searchValue[key]) && key !== 'Date' && key !== 'CustCntrlId') { showSearch.push([{ text: (key === 'StDate') ? 'Start Date' : (key === 'EnDate') ? 'End Date' : (key === '_id') ? isSale ? 'Report' : 'Report' : key, ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: (key === 'StDate') ? MyDate.ConvertUTCDateToReadable(searchValue[key]) : (key === 'EnDate') ? MyDate.ConvertUTCDateToReadable(searchValue[key]) : searchValue[key], ColRange: 7, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }]); } // console.log(`${key}: ${searchValue[key]}`); } this.setHeadingInCell(HeaderName, showSearch, HeaderText); this.setInvoiceDetailsInCell(MainData, isSale, HeaderText, EntityData.User.TZ, EntityData.Entity?.Settings?.DecimalsNumber); this.ws['!ref'] = XLSX.utils.encode_range(this.range); this.ws['!merges'] = this.MergeArray; this.ws["!cols"] = [{ wch: 50 }, { wch: 10 }, { wch: 15 }, { wch: 15 },{ wch: 15 }, { wch: 15 }, { 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(HeaderName: any, showSearch: any, HeadText: any) { let MainHeadings: any[] = [ // { // text: 'SNo', // ColRange: 1, // bold: true, // ChildHeadings: [] // }, { text: HeadText, ColRange: 1, bold: true, ChildHeadings: [], }, { text: 'Count', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'No', ColRange: 1 }, // { text: 'Date', ColRange: 1 }, // { text: 'Invoice Total', ColRange: 1 } ], }, { text: 'Qty', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'No', ColRange: 1 }, // { text: 'Date', ColRange: 1 }, // { text: 'Invoice Total', ColRange: 1 } ], }, { text: 'Offer Qty', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'No', ColRange: 1 }, // { text: 'Date', ColRange: 1 }, // { text: 'Invoice Total', ColRange: 1 } ], }, { text: 'SubTotal', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Discount', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Tax', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Adjustment', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Round off', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], }, { text: 'Total', ColRange: 1, bold: true, ChildHeadings: [ // { text: 'Central Tax', ColRange: 1 }, // { text: 'State Tax', ColRange: 1 }, // { text: 'IGST Tax', ColRange: 1 }, ], } ]; this.SetDataInCell(HeaderName, 0, this.Row); this.MergeArray.push({ s: { r: this.Row, c: 0 }, e: { r: this.Row, c: 0 + 10 - 1 }, }); this.Row += 2; // console.log('showSearch', showSearch); showSearch.forEach((search: any) => { let ColStart: any = 0; // console.log('search', search, ColStart); search.forEach((item: any) => { // console.log('text', oyo.text, ColStart); this.SetDataInCell(item.text, ColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: ColStart }, e: { r: this.Row, c: ColStart + item.ColRange - 1 }, }); ColStart += item.ColRange; }); this.Row += 1; }); 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 setInvoiceDetailsInCell(MainData: any, isSale: boolean, HeaderText:any, TZ:any, DecimalsNumber: number) { MainData.forEach((InvoiceList: any) => { this.SetInvoiceDataInExcel(InvoiceList, isSale, HeaderText, TZ, DecimalsNumber); }); } static SetInvoiceDataInExcel(InvoiceInfo: any, isSale: boolean, HeaderText:any, TZ:any, DecimalsNumber: number) { if(HeaderText === 'Month'){ InvoiceInfo._id = MyDate.GetMonthName(InvoiceInfo._id, TZ); }else{ InvoiceInfo._id=MyDate.ConvertUTCDateToReadable(InvoiceInfo._id) } let InvoiceData: any = [ { text: InvoiceInfo._id, ColRange: 1, IsString: true }, { text: InvoiceInfo.Qty, ColRange: 1, IsString: true }, { text: InvoiceInfo.OfQty, ColRange: 1, IsString: true }, { text: InvoiceInfo.Count, ColRange: 1, IsString: true }, { text: InvoiceInfo.SubTotal, ColRange: 1, IsString: true }, { text: InvoiceInfo.Disc, ColRange: 1, IsString: true }, { text: InvoiceInfo.Tax, ColRange: 1, IsString: true }, { text: InvoiceInfo.Adjust, ColRange: 1, IsString: true }, { text: InvoiceInfo.Round, ColRange: 1, IsString: true }, { text: InvoiceInfo.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) { // console.log(ColRange, RowNum); var cell = { v: Data, bold: true }; 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; } }