You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sfh-mohemm/Mohem/src/app/reports/transactions/transactions.component.ts

129 lines
4.7 KiB
TypeScript

import { Component, ElementRef, OnInit } from '@angular/core';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { File } from '@ionic-native/file/ngx';
import { Platform } from '@ionic/angular';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { ReportServiceService } from '../report-service.service';
@Component({
selector: 'app-transactions',
templateUrl: './transactions.component.html',
styleUrls: ['./transactions.component.scss'],
})
export class TransactionsComponent implements OnInit {
transactionList: any = [];
selectedMenu: any;
template: any;
direction: any;
programList: any = [];
constructor(public cs: CommonService, private elementRef: ElementRef, public reports: ReportServiceService, public ts: TranslatorService, private platform: Platform, private file: File, private opener: FileOpener,
) { }
ngOnInit() {
this.selectedMenu = this.cs.sharedService.getSharedData(MenuResponse.SELECTED_MENU, false);
// this.transactionList = this.cs.sharedService.getSharedData(ReportServiceService.TRANSACTION_LIST, false);
//this.template = this.cs.sharedService.getSharedData(ReportServiceService.TEMPLATE, false);
this.direction = TranslatorService.getCurrentDirection();
this.getConcurrentProgram();
}
getDate(date) {
return this.cs.evaluteDate(date);
}
getCppOutput(transaction) {
var request: any = {
P_REQUEST_ID: transaction['REQUEST_ID']
};
this.cs.startLoading();
this.reports.getCppOutput(request, {}, this.ts.trPK('general', 'retry')).subscribe((result: any) => {
this.saveAndOpenPdf(result.GetCcpOutputList['P_OUTPUT_FILE'], 'report.pdf');
console.log(result);
this.cs.stopLoading();
});
}
// saveAndOpenPdf(pdf: string, filename: string) {
// const linkSource = `data:application/pdf;base64,${pdf}`;
// const downloadLink = document.createElement("a");
// const fileName = "report.pdf";
// downloadLink.href = linkSource;
// downloadLink.download = fileName;
// downloadLink.click();
// }
saveAndOpenPdf(pdf: string, filename: string) {
const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory;
this.file.writeFile(writeDirectory, filename, this.convertBase64ToBlob(pdf, 'data:application/pdf;base64'), { replace: true })
.then(() => {
this.opener.open(writeDirectory + filename, 'application/pdf')
.catch(() => {
console.log('Error opening pdf file');
});
})
.catch(() => {
console.error('Error writing pdf file');
});
}
convertBase64ToBlob(b64Data, contentType): Blob {
contentType = contentType || '';
const sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
const byteCharacters = window.atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, { type: contentType });
}
refresh() {
// var request: any = {
// P_REQUEST_ID: transaction['REQUEST_ID']
// };
this.cs.startLoading();
var request: any = {};
request.P_MENU_TYPE = this.selectedMenu.MENU_TYPE;
request.P_SELECTED_RESP_ID = -999;
request.P_FUNCTION_NAME = this.selectedMenu.FUNCTION_NAME;
request.P_DESC_FLEX_NAME = this.template.CONCURRENT_PROGRAM_NAME;
this.reports.getTransaction(request, {}, this.ts.trPK('general', 'retry')).subscribe((result: any) => {
this.cs.stopLoading();
this.transactionList = result['GetCcpTransactionsList_New'];
});
}
getConcurrentProgram() {
this.cs.startLoading();
this.reports.getConcurrentProgram({
P_REQUEST_GROUP_ID: this.selectedMenu.REQUEST_GROUP_ID
}, () => { }, this.ts.trPK('general', 'retry')).subscribe((response) => {
this.cs.stopLoading();
this.programList = response.GetConcurrentProgramsList;
})
}
selectTemplates() {
// this.elementRef
// .nativeElement.querySelectorAll('#containerDiv')[0].innerHTML = '';
if (this.template) {
this.refresh();
}
}
createTransaction() {
this.cs.openConcurrentReport();
}
}