import * as XLSX from 'xlsx'; import { TrUtils } from '../../utils/tr-utils'; export class HSNSummaryExcelService { static wb: any = {}; static Row: any = 0; static MergeArray: any[] = []; static range: any = { s: { c: 0, r: 0 }, e: { c: 0, r: 0 } }; static ws: any = {}; static GetHSNSummaryExcelData(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 = 'HSN Summary'; this.wb.Sheets = {}; this.wb.Props = {}; this.wb.SSF = {}; this.wb.SheetNames = []; // MainData = this.GetTotals(MainData); this.setHeadingInCell(); this.setInvoiceDetailsInCell(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 setHeadingInCell() { let MainHeadings: any[] = [ { text: '', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'HSN/SAC', ColRange: 1, font: { bold: true }, ChildHeadings: [] }, { text: 'Quantity', ColRange: 1, bold: true, ChildHeadings: [ ] }, { text: 'Unit of Measure', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'Taxable Value', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'CGST', ColRange: 1, bold: true, ChildHeadings: [ ] }, { text: 'SGST', ColRange: 1, bold: true, ChildHeadings: [ ] }, { text: 'IGST', 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, DecimalsNumber: number) { this.SetInvoiceDataInExcel(MainData, DecimalsNumber); } static SetInvoiceDataInExcel(InvoiceList: any, DecimalsNumber: number) { let ColStart: any = 0; if ((!TrUtils.IsNull(InvoiceList.B2B.Items) && InvoiceList.B2B.Items.length !== 0) || !TrUtils.IsNull(InvoiceList.B2C.Items) && InvoiceList.B2C.Items.length !== 0) { let PartsHeading: any = { text: 'Items', ColRange: 1 }; this.SetDataInCell(PartsHeading.text, ColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: ColStart }, e: { r: this.Row, c: ColStart + 8 - 1 } }); this.Row += 1; InvoiceList.B2B.Items.forEach((Part: any) => { let PartData: any = [ { text: 'GST', ColRange: 1, IsString: false }, { text: Part.HSN, ColRange: 1, IsString: false }, { text: Part.Quantity, ColRange: 1, IsString: false }, { text: Part.UoM, ColRange: 1, IsString: false }, { text: Part.TaxableValue, ColRange: 1, IsString: false }, { text: Part.CGST, ColRange: 1, IsString: false }, { text: Part.SGST, ColRange: 1, IsString: false }, { text: Part.IGST, ColRange: 1, IsString: false } ]; let PartColStart: any = 0; PartData.forEach((PartInfo: any) => { PartInfo.text = this.ConvertToString(PartInfo.text, PartInfo.IsString, DecimalsNumber); this.SetDataInCell(PartInfo.text, PartColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: PartColStart }, e: { r: this.Row, c: PartColStart + PartInfo.ColRange - 1 } }); PartColStart += PartInfo.ColRange; }); this.Row += 1; }); InvoiceList.B2C.Items.forEach((Part: any) => { let PartData: any = [ { text: 'No GST', ColRange: 1, IsString: false }, { text: Part.HSN, ColRange: 1, IsString: false }, { text: Part.Quantity, ColRange: 1, IsString: false }, { text: Part.UoM, ColRange: 1, IsString: false }, { text: Part.TaxableValue, ColRange: 1, IsString: false }, { text: Part.CGST, ColRange: 1, IsString: false }, { text: Part.SGST, ColRange: 1, IsString: false }, { text: Part.IGST, ColRange: 1, IsString: false } ]; let PartColStart: any = 0; PartData.forEach((PartInfo: any) => { PartInfo.text = this.ConvertToString(PartInfo.text, PartInfo.IsString, DecimalsNumber); this.SetDataInCell(PartInfo.text, PartColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: PartColStart }, e: { r: this.Row, c: PartColStart + PartInfo.ColRange - 1 } }); PartColStart += PartInfo.ColRange; }); this.Row += 1; }); this.Row += 1; } let Start: any = 0; if ((!TrUtils.IsNull(InvoiceList.B2B.Ops) && InvoiceList.B2B.Ops.length !== 0) || !TrUtils.IsNull(InvoiceList.B2C.Ops) && InvoiceList.B2C.Ops.length !== 0) { let PartsHeading: any = { text: 'Services', ColRange: 1 }; this.SetDataInCell(PartsHeading.text, Start, this.Row); this.MergeArray.push({ s: { r: this.Row, c: Start }, e: { r: this.Row, c: Start + 8 - 1 } }); this.Row += 1; InvoiceList.B2B.Ops.forEach((Labor: any) => { let OpsData: any = [ { text: 'GST', ColRange: 1, IsString: false }, { text: Labor.HSN, ColRange: 1, IsString: false }, { text: Labor.Quantity, ColRange: 1, IsString: false }, { text: Labor.UoM, ColRange: 1, IsString: false }, { text: Labor.TaxableValue, ColRange: 1, IsString: false }, { text: Labor.CGST, ColRange: 1, IsString: false }, { text: Labor.SGST, ColRange: 1, IsString: false }, { text: Labor.IGST, ColRange: 1, IsString: false } ]; let PartColStart: any = 0; OpsData.forEach((PartInfo: any) => { PartInfo.text = this.ConvertToString(PartInfo.text, PartInfo.IsString, DecimalsNumber); this.SetDataInCell(PartInfo.text, PartColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: PartColStart }, e: { r: this.Row, c: PartColStart + PartInfo.ColRange - 1 } }); PartColStart += PartInfo.ColRange; }); this.Row += 1; }); InvoiceList.B2C.Ops.forEach((Labor: any) => { let OpsData: any = [ { text: 'No GST', ColRange: 1, IsString: false }, { text: Labor.HSN, ColRange: 1, IsString: false }, { text: Labor.Quantity, ColRange: 1, IsString: false }, { text: Labor.UoM, ColRange: 1, IsString: false }, { text: Labor.TaxableValue, ColRange: 1, IsString: false }, { text: Labor.CGST, ColRange: 1, IsString: false }, { text: Labor.SGST, ColRange: 1, IsString: false }, { text: Labor.IGST, ColRange: 1, IsString: false } ]; let PartColStart: any = 0; OpsData.forEach((PartInfo: any) => { PartInfo.text = this.ConvertToString(PartInfo.text, PartInfo.IsString, DecimalsNumber); this.SetDataInCell(PartInfo.text, PartColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: PartColStart }, e: { r: this.Row, c: PartColStart + PartInfo.ColRange - 1 } }); PartColStart += PartInfo.ColRange; }); this.Row += 1; }); this.Row += 1; } } static ConvertToString(Text: any, IsString: any, DecimalsNumber: number) { if (IsString) { if (TrUtils.IsNull(Text)) { Text = ''; } } else { if (TrUtils.IsNull(Text)) { 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; } } }