import { ChangeDetectorRef, Component, OnInit } from "@angular/core"; import {API, CONDITIONS, EAction, EText, GENDER, PREFIX, SOURCES, 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 {sortByProperty} from "../../../../@common/utils/OrderBy"; import {C} from "@angular/cdk/keycodes"; @Component({ selector: "app-appraisal-3rd-time-do", templateUrl: "./contract-approved-do.component.html", styleUrls: [] }) export class ContractApprovedDoComponent extends BaseFormComponent implements OnInit { override dataForm: any = {}; dataView: IProduct = {}; auth: any = {}; title = ""; api: any = API; storage: any = STORAGE; attachments: any = []; equipment: any = []; equipmentOther: any = []; settings: any = []; masterProductUnit: any = []; deviation: any = 0; isTabs: any = 1; prefixData = PREFIX; genderData = GENDER; conditions = CONDITIONS; constructor( public activatedRoute: ActivatedRoute, public router: Router, public changeDetectorRef: ChangeDetectorRef, public appService: AppService ) { super(); } async ngOnInit() { this.activatedRoute.params.subscribe(async params => { this.ids = params["id"]; this.action = params["action"]; this.auth = this.appService.auth(); this.dataForm.customer = {}; this.dataForm.sellsr = {}; if (this.ids) await this.getData(); }); } async onAction(action: string) { const sweetalert = await lastValueFrom(this.appService.confirm(EAction.BACK)); if (!sweetalert.isConfirmed) return; if (action === "back") return this.router.navigate(["/pages/contract/approved/list", this.action]); return; } async getData() { if (!this.ids) this.appService.message(EAction.INFO, EText.NO_DATA); try { this.dataForm = await lastValueFrom(this.appService.get(`${this.api.quotation}/getById/${this.ids}`)); this.attachments = this.dataForm.images ? this.dataForm.images?.split(",") : []; this.equipment = this.dataForm.equipment ? this.dataForm.equipment?.split(",") : []; this.equipmentOther = this.dataForm.equipmentOther ? this.dataForm.equipmentOther?.split(",") : []; this.dataForm.deposit = Number(this.dataForm.deposit) + Number(this.dataForm.sellerDeposit2ndTime) + Number(this.dataForm.sellerDeposit3rdTime); if (!this.dataForm.customerId) { this.dataForm.customer = {}; this.dataForm.customer.prefix = this.dataForm.customerPrefix; this.dataForm.customer.firstName = this.dataForm.customerFirstName; this.dataForm.customer.lastName = this.dataForm.customerLastName; this.dataForm.customer.phone = this.dataForm.customerPhone; } sortByProperty(this.dataForm.quotationDetail, 'installment', 'ASC'); this.dataForm.principleSum = 0 this.dataForm.interestTotalSum = 0 this.dataForm.feeSum = 0 this.dataForm.feeSum = 0 this.dataForm.totalPaymentSum = 0 this.dataForm.quotationDetail.map((item : any) => { this.dataForm.principleSum += Number(item.principle) this.dataForm.interestTotalSum += Number(item.interestTotal) this.dataForm.feeSum += Number(item.fee) this.dataForm.totalPaymentSum += Number(item.totalPayment) }) this.dataForm.principleSum = Math.round(this.dataForm.principleSum); this.dataForm.contractBankName = 'ธนาคารทหารไทยธนชาต จำกัด (มหาชน)'; this.dataForm.contractAccountName = 'บริษัท ซีเอ็ม เอฟเอส จำกัด'; this.dataForm.contractAccountNumber = '263-2-17778-4'; this.dataForm.contractDetail = 'ชำระเงินงวดอย่างน้อย ทุกเดือนตามวันและยอดขั้นต่ำตามตาราง โดยที่ไม่เสียค่าปรับ ทั้งนี้หากเกินกำหนด ผู้กู้ต้องเสียค่าดอกเบี้ยผิดนัดเพิ่มเติมวันละ 1,000 บาท (ไม่รวมค่าทวงถาม) หากขาดส่งเกินกว่า 60 วัน นับแต่วันผ่อนล่าสุดจะถือว่าผิดสัญญา โดยหากผู้กู้ติดสัญญาไม่ว่ากรณีใดๆ ผู้กู้ยินดีที่จะนำสังหาริมทรัพย์ที่ผู้กู้นำเงินที่กู้ไปซื้อเป็นค่าตอบแทนในการชำระหนี้สินส่วนที่เหลือโดยทันที'; this.changeDetectorRef.detectChanges(); } catch (err) { this.appService.message(EAction.ERROR, EText.ERROR); } } async onSubmitCancel(form: any) { this.dataForm.isStatusContract = 'cancel'; await this.onSubmit(form) } async onSubmit(form: any) { try { // console.log(form); // if (!form.valid) return false; const sweetalert = await lastValueFrom(this.appService.confirm(EAction.CREATE)); if (!sweetalert.isConfirmed) return; if (this.dataForm.isStatusContract) { this.dataForm.statusContract = 'cancel'; this.dataForm.contractCancelDate = new Date(); this.dataForm.contractCancelBy = this.auth.id; } if (!this.dataForm.isStatusContract) { this.dataForm.statusContract = 'approved'; this.dataForm.contractApprovedDate = new Date(); this.dataForm.contractApprovedBy = this.auth.id; this.dataForm.statusWarehouse = 'warehouse'; } this.dataForm.images = this.attachments?.[0] ? this.attachments.join(",") : null; return await this.onUpdate(); } catch (e) { console.log(e); } } async onUpdate() { try { await lastValueFrom(this.appService.post(`${this.api.quotation}/update/${this.ids}`, this.dataForm)); await this.appService.message(EAction.SUCCESS, EText.UPDATE); await this.router.navigate(["/pages/contract/approved/list", this.action]); } catch (err) { this.appService.message(EAction.ERROR, EText.ERROR); } } async onAttachments($event: any, type: any) { const file = $event.target.files[0]; if (!file) return; const formData = new FormData(); formData.append("ref", type); formData.append("file", file); try { const res = await lastValueFrom(this.appService.post(`${this.api.attachments}/products`, formData)); if (!this.attachments[0]) { this.dataForm.coverImage = res.fileName; } this.attachments.push(res.fileName); console.log(this.attachments, res); this.changeDetectorRef.detectChanges(); } catch (e) { this.appService.message(EText.ERROR); } } async onRemoveAttachments(i: number, fileName: string) { const sweetalert = await lastValueFrom(this.appService.confirm(EAction.DELETE)); if (!sweetalert.isConfirmed) return; // await lastValueFrom(this.appService.delete(`${this.api.attachments}/deleteByName`, fileName)); this.attachments?.splice(i, 1); if (!this.attachments[0]) { this.dataForm.coverImage = null; } this.changeDetectorRef.detectChanges(); } protected readonly SOURCES = SOURCES; }