import {ChangeDetectorRef, Component, Inject, OnInit, ViewChild} from "@angular/core"; import { API, EAction, EText, STORAGE } from "../../../../@config/app"; import { AppService } from "../../../../app.service"; import { lastValueFrom } from "rxjs"; import { BaseFormComponent } from "../../../../@common/base/base-form.component"; import { ActivatedRoute, Router } from "@angular/router"; import { IProduct } from "../../../../app.interface"; import { addMonths } from "date-fns"; import { BasePopupComponent } from "../../../../@common/base/base-popup.component"; import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from "@angular/material/dialog"; import { IDialogConfigData } from "../../../../@common/interface/Dialog"; import deepCopy from "../../../../@common/utils/DeepCopy"; import {AttachmentsViewComponent} from "../../../@popup/attachments-view/attachments-view.component"; @Component({ selector: "app-finance-payment-do", templateUrl: "./finance-payment-create.component.html", styleUrls: [] }) export class FinancePaymentCreateComponent extends BasePopupComponent implements OnInit { title = 'รับชำระเงิน/ออกใบเสร็จ'; apiUrl: string = API.quotationPayment; paymentTypes = [ {value : 'deposit', name : 'ค่ามัดจำ'}, {value : 'installment', name : 'ผ่อนสินค้า'}, ]; paymentMethods = [ {value : 'transfer', name : 'โอนเงิน'}, {value : 'cash', name : 'เงินสด'}, ] quotations : any = []; dataFilter : any = {}; storage: any = STORAGE; @ViewChild('uploadFile') uploadFile: any; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public dialog : IDialogConfigData, public changeDetectorRef: ChangeDetectorRef, public appService: AppService, private attachmentsView: MatDialog, ) { super(); } async ngOnInit() { this.quotations = await lastValueFrom(this.appService.get(`${API.quotation}?showAll=true&statusContract=approved`)); this.dataForm.paymentDate = new Date(); this.dataForm.typeCode = this.quotations.typeCode; } async onChangeFilter(ev : any) { this.dataForm = {}; if (!ev) return; const data = this.quotations.find((f : any) => f.productNo === ev); this.dataForm = data ? data : {}; this.dataForm.paymentDate = new Date(); } async onSubmit(form : any) { if (!form.valid) return false; return await this.onCreate(); } async onCreate() { try { this.dataForm.quotationId = this.dataForm.id; this.dataForm.paymentType = 'receive'; this.dataForm.paymentAmountAll = Number(this.dataForm.paymentAmount); this.dataForm.type = 'receive'; delete this.dataForm.id; delete this.dataForm.seller; delete this.dataForm.customer; this.dataForm.pageAction = 'PopupCreate'; await lastValueFrom(this.appService.post(this.apiUrl, this.dataForm)); await this.appService.message(EAction.SUCCESS, EText.CREATE); await this.dialogRef.close(EAction.GET); } catch (err) { this.appService.message(EAction.ERROR, EText.ERROR); this.dialogRef.close(EAction.GET); } } async onAttachments($event: any) { const file = $event.target.files[0]; if (!file) return; const formData = new FormData(); formData.append("ref", 'images'); formData.append("file", file); try { const res = await lastValueFrom(this.appService.post(`${API.attachments}/images`, formData)); this.dataForm.paymentImages = res.fileName; this.uploadFile.nativeElement.value = null; console.log(this.uploadFile) this.changeDetectorRef.detectChanges(); } catch (e) { this.appService.message(EText.ERROR); } } async onRemoveAttachments() { const sweetalert = await lastValueFrom(this.appService.confirm(EAction.DELETE)); if (!sweetalert.isConfirmed) return; // await lastValueFrom(this.appService.delete(`${this.api.attachments}/deleteByName`, fileName)); this.dataForm.paymentImages = null; this.changeDetectorRef.detectChanges(); } async onAttachmentsView() { const dialogConfig = deepCopy(this.dialogConfig); dialogConfig.data.action = EAction.POPUP; dialogConfig.data.title = 'ไฟล์แนบ'; dialogConfig.data.type = 'images'; dialogConfig.data.images = this.dataForm.paymentImages; const dialogRef = this.attachmentsView.open(AttachmentsViewComponent, dialogConfig); const afterClosed = await lastValueFrom(dialogRef.afterClosed()); } }