import { Component, OnInit, EventEmitter, Input } from '@angular/core'; import { BsModalRef } from 'ngx-bootstrap'; import { VendorCreditsService } from '../../../services/vendor-credits.service'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { newVendorCreditRefundDefaults } from '../../../schemas/vendor-credits/vendor-credit-refund'; import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'app-refund-modal', templateUrl: './refund-modal.component.html', styleUrls: ['./refund-modal.component.css'] }) export class RefundModalComponent implements OnInit { refundForm: FormGroup; modalRef: BsModalRef; message: string; confirm: boolean; @Input() title: string; @Input() deleteBtn: string; @Input() buttonText: string; @Input() balanceValue: string @Input() vendorName: string @Input() vendorCreditId: number @Input() creditUsed: number allPaymentModes: any[] allAccountNames: any[] public event: EventEmitter = new EventEmitter(); constructor(public bsModalRef: BsModalRef, private toastr: ToastrService,private vendorCreditService: VendorCreditsService,private fb: FormBuilder ) { this.vendorCreditService.getAllVendorCreditRefundData().subscribe((exisitingDataFromDb: newVendorCreditRefundDefaults) => { this.allPaymentModes = exisitingDataFromDb.data.allPaymentModes; this.allAccountNames = exisitingDataFromDb.data.allAccountNames; }); } ngOnInit(): void { this.refundForm = this.fb.group({ reference: ['',{validators:[Validators.maxLength(30)]}], paymentMode: ['',{validators:[Validators.required]}], startDate: [new Date(),Validators.required], amount: [,{validators:[Validators.required,Validators.min(0.01),Validators.max(parseFloat(this.balanceValue))]}], depositTo: [,Validators.required], description:[''] }); } /* @usage: called when a form is submitted */ onSubmit() { if (this.refundForm.valid) { this.refundForm.value.vendorName = this.vendorName; this.refundForm.value.vendorCreditId = this.vendorCreditId; this.refundForm.value.vendorCreditUsed = Math.floor(this.creditUsed) + parseFloat(this.refundForm.get('amount').value); this.refundForm.value.FullAmount = (parseFloat(this.refundForm.get('amount').value) === (parseFloat(this.balanceValue))) ? true : false; this.vendorCreditService.createVendorCreditRefund(this.refundForm.value).subscribe((result: {status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Refund Information of ' + this.vendorCreditId + ' has been saved'), this.confirm = true, this.reloadVendorCredit(this.confirm) } }, (error) => { this.toastr.error('Some error occured'); this.confirm = false; }, ); this.bsModalRef.hide(); } else { this.refundForm.controls['startDate'].markAllAsTouched(); this.refundForm.controls['paymentMode'].markAllAsTouched(); this.refundForm.controls['amount'].markAllAsTouched(); this.refundForm.controls['depositTo'].markAllAsTouched(); } } /* @usage: called on successfull refund form submitted @purpose: to send the success status to credits grid page. */ reloadVendorCredit(item: boolean) { this.event.emit(item); } get startDate() {return this.refundForm.get('startDate')} get paymentMode() {return this.refundForm.get('paymentMode')} get reference() {return this.refundForm.get('reference')} get amount() {return this.refundForm.get('amount')} get depositTo() {return this.refundForm.get('depositTo')} get description() {return this.refundForm.get('description')} }