import {ChangeDetectorRef, Component, OnInit} from "@angular/core"; import {lastValueFrom} from "rxjs"; import {AppService} from "../../../../app.service"; import {API} from "../../../../@config/app"; import {ActivatedRoute, Router} from "@angular/router"; import {DomSanitizer} from "@angular/platform-browser"; import {BaseFormComponent} from "../../../../@common/base/base-form.component"; import {IQuotation} from "../../../../@common/interface/Quotation"; @Component({ selector: "app-finance-payment-pdf", templateUrl: "./finance-payment-pdf-invoice.component.html", styleUrls: [] }) export class FinancePaymentPdfInvoiceComponent extends BaseFormComponent implements OnInit { pageTitle = "ใบเสนอราคา"; apiUrl: string = API.quotation; api: any = API; dataView: any; pdfView: any; paymentMethods : any = { transfer : "โอนเงิน", cash : "เงินสด", } constructor( public activatedRoute: ActivatedRoute, public router: Router, public changeDetectorRef: ChangeDetectorRef, public appService: AppService, private sanitizer: DomSanitizer ) { super(); } async ngOnInit() { this.activatedRoute.params.subscribe(async params => { this.ids = params["id"]; await this.getData(); }); } async getData() { try { const quotation: IQuotation = await lastValueFrom(this.appService.get(`${this.api.quotation}/getById/${this.ids}`)); // const startDate = quotation.startDate ? format(parseISO(quotation.startDate), "dd/MM/yyyy") : null; const customerPrefix = quotation.customerPrefix ? quotation.customerPrefix : ''; const customerName = quotation?.customerId ? `${quotation.customer?.prefix} ${quotation.customer?.firstName} ${quotation.customer?.lastName}` : `${customerPrefix} ${quotation.customerFirstName} ${quotation.customerLastName}`; const phone = quotation?.customerId ? quotation.customer?.phone : quotation.customerPhone; const address = quotation?.customerId ? quotation.customer?.address : quotation.customerAddress; let paymentAmountAll: any = 0; if (quotation.step === 2) paymentAmountAll = quotation.transferSummary; if (quotation.step === 3) paymentAmountAll = quotation.sellerDeposit2ndTime; if (quotation.step === 4) paymentAmountAll = quotation.sellerDeposit3rdTime; if (quotation.step === 5) paymentAmountAll = quotation.priceDisbursement; const data: any = { payment_no: quotation.quotationNo, due_date: quotation.startDate, due_dates: "", customer_name: customerName, phone: phone ? phone : '', address: address ? address : '', data: [ { item_code: quotation.productNo, item_name_th: quotation.productName, item_amount: Number(paymentAmountAll) } ], total_amount: Number(paymentAmountAll), payee: quotation?.userFullName } console.log(data) const pdf = await lastValueFrom(this.appService.post(`${this.api.paymentReport}/pdf`, data, {responseType: "arraybuffer"})); const url = URL.createObjectURL(new Blob([pdf], {type: "application/pdf"})); this.pdfView = this.sanitizer.bypassSecurityTrustResourceUrl(url); } catch (e) { console.log(e); } } }