import { Component, EventEmitter, Input, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { BsModalRef } from 'ngx-bootstrap'; import { ToastrService } from 'ngx-toastr'; import { InvoicesService } from '../../../services/invoices.service'; @Component({ selector: 'app-refund-sales', templateUrl: './refund-sales.component.html', styleUrls: ['./refund-sales.component.css'] }) export class RefundSalesComponent implements OnInit { @Input() invoiceId: any; @Input() amountPaid: any; refundSalesForm: FormGroup; amountDisabled: boolean = false; public event: EventEmitter = new EventEmitter(); constructor(public bsModalRef: BsModalRef,private fb:FormBuilder,private toastr: ToastrService, private invoiceService: InvoicesService) { } ngOnInit() { this.refundSalesForm = this.fb.group({ refund_amount: ['', { validators: [Validators.required, Validators.pattern("^[0-9]*$")] }], total_refund: [false] }); } setRefundAmount(event){ if(event.target.checked){ this.refundSalesForm.get('refund_amount').setValue(this.amountPaid); this.amountDisabled = true; }else{ this.refundSalesForm.get('refund_amount').setValue(''); this.amountDisabled = false; } } async onSubmit(){ if (this.refundSalesForm.get('refund_amount').value === ''){ this.refundSalesForm.controls['refund_amount'].markAllAsTouched(); }else{ if(this.refundSalesForm.get('refund_amount').value > this.amountPaid){ this.refundSalesForm.get('refund_amount').setValue(''); this.toastr.error('Refund amount must be less than the amount paid'); } } if (this.refundSalesForm.valid){ this.refundSalesForm.value.invoice_id = this.invoiceId; this.invoiceService.updateInvoiceStateToRefund(this.refundSalesForm.value).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.bsModalRef.hide(); this.toastr.success('Refund completed successfully'); this.event.emit(true); }else{ this.bsModalRef.hide(); this.toastr.error('Order Refund Failed'); this.event.emit(false); } },(error) => { this.bsModalRef.hide(); this.toastr.error('Order Refund Failed'); this.event.emit(false); }, ); } } get refund_amount() { return this.refundSalesForm.get('refund_amount') } }