import * as XLSX from 'xlsx'; import { TrUtils } from '../../utils/tr-utils'; export class GSTR2ExcelService { 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 GetGSTR1ExcelData(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 = 'GSTR2'; 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 GetTotals(MainData: any) { MainData.forEach((InvoicesList: any) => { InvoicesList.Bills.forEach((InvoiceInfo: any) => { // if (InvoiceInfo.Inv.InsTotal != null) { // InvoiceInfo.Inv.Amount = InvoiceInfo.Inv.InsTotal; // } // if (InvoiceInfo.Inv.Total != null) { // InvoiceInfo.Inv.Amount = InvoiceInfo.Inv.Total; // } if (InvoiceInfo.Total != null) { InvoiceInfo.Amount = InvoiceInfo.Total; } }); }); return MainData; } static setHeadingInCell() { let MainHeadings: any[] = [ { text: 'GSTIN/UIN', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'Name', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'Bill Details', ColRange: 4, bold: true, ChildHeadings: [ { text: 'No', ColRange: 1 }, { text: 'Date', ColRange: 1 }, { text: 'Total', ColRange: 1 } ] }, { text: 'Rate', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'Taxable Value', ColRange: 1, bold: true, ChildHeadings: [] }, { text: 'Amount', ColRange: 3, bold: true, ChildHeadings: [ { text: 'Central Tax', ColRange: 1 }, { text: 'State Tax', ColRange: 1 }, { text: 'IGST Tax', ColRange: 1 }, ] } ] let MainColStart: any = 0; MainHeadings.forEach((MainHeader: any) => { let ChildColStart: any = MainColStart; 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; MainHeader.ChildHeadings.forEach((ChildHeaderInfo: any) => { this.SetDataInCell(ChildHeaderInfo.text, ChildColStart, this.Row + 1); this.MergeArray.push({ s: { r: this.Row + 1, c: ChildColStart }, e: { r: this.Row + 1, c: ChildColStart + ChildHeaderInfo.ColRange - 1 } }); ChildColStart += ChildHeaderInfo.ColRange; }); }); this.Row += 2; } static setInvoiceDetailsInCell(MainData: any, DecimalsNumber: number) { MainData.forEach((InvoiceList: any) => { this.SetInvoiceDataInExcel(InvoiceList, DecimalsNumber); }); } static SetInvoiceDataInExcel(InvoiceList: any, DecimalsNumber: number) { InvoiceList.Bills.forEach((InvoiceInfo: any, index: any) => { // let InvoiceInfo = InvoiceInfoData.Inv; InvoiceInfo.CrDate = this.ConvertDateToReadableFormat(InvoiceInfo.CrDate); let GSTIN: any; let Name: any; if (index === 0) { if (InvoiceList._id == 'Unspecified') { GSTIN = 'No GST'; Name = InvoiceInfo.Name; } else { GSTIN = InvoiceList._id; Name = InvoiceInfo.Name; } } else { if (InvoiceList._id == 'Unspecified') { GSTIN = 'No GST'; Name = InvoiceInfo.Name; } } let InvoiceData: any = [ { text: GSTIN, ColRange: 1, IsString: true }, { text: Name, ColRange: 1, IsString: true }, { text: InvoiceInfo.BNo, ColRange: 1, IsString: true }, { text: InvoiceInfo.CrDate, ColRange: 1, IsString: true }, { text: InvoiceInfo.Amount, 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; }); let TotalsColStart: any = ColStart; if (!TrUtils.IsNull(InvoiceInfo.ItemsArray) && InvoiceInfo.ItemsArray.length !== 0) { let PartsHeading: any = { text: 'Parts', ColRange: 1 }; this.SetDataInCell(PartsHeading.text, TotalsColStart, this.Row); this.MergeArray.push({ s: { r: this.Row, c: TotalsColStart }, e: { r: this.Row, c: TotalsColStart + PartsHeading.ColRange - 1 } }); InvoiceInfo.ItemsArray.forEach((Part: any) => { let PartData: any = [ { text: Part.Rate, ColRange: 1, IsString: false }, { text: Part.Taxable, 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 = TotalsColStart + 1; 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; } // if (!TrUtils.IsNull(InvoiceInfo.LaborArray) && InvoiceInfo.LaborArray.length !== 0) { // let LaborHeading: any = { text: 'Labor', ColRange: 1 }; // this.SetDataInCell(LaborHeading.text, TotalsColStart, this.Row); // this.MergeArray.push({ s: { r: this.Row, c: TotalsColStart }, e: { r: this.Row, c: TotalsColStart + LaborHeading.ColRange - 1 } }); // InvoiceInfo.LaborArray.forEach((Service: any) => { // let ServiceData: any = [ // { text: Service.Rate, ColRange: 1, IsString: false }, // { text: Service.Taxable, ColRange: 1, IsString: false }, // { text: Service.CGST, ColRange: 1, IsString: false }, // { text: Service.SGST, ColRange: 1, IsString: false }, // { text: Service.IGST, ColRange: 1, IsString: false } // ]; // let LaborColStart: any = TotalsColStart + 1; // ServiceData.forEach((ServiceInfo: any) => { // ServiceInfo.text = this.ConvertToString(ServiceInfo.text, ServiceInfo.IsString); // this.SetDataInCell(ServiceInfo.text, LaborColStart, this.Row); // this.MergeArray.push({ s: { r: this.Row, c: LaborColStart }, e: { r: this.Row, c: LaborColStart + ServiceInfo.ColRange - 1 } }); // LaborColStart += ServiceInfo.ColRange; // }); // this.Row += 1; // }); // 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; } } }