Files
barker/bookie/src/app/customers/customer-detail/customer-detail.component.ts
tanshu db5f2731be Feature: Added a column called print in bill to the table customer.
This will prevent printing all customer's names and phone numbers in the bill in case of simple walkins.
This is a breaking change as there is schema changes in the database.
It also bolds the customers who are to be printed in the bill in the running tables list.
2021-06-28 08:41:32 +05:30

128 lines
3.5 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { AbstractControl, FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { round } from 'mathjs';
import { Customer } from '../../core/customer';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { CustomerService } from '../customer.service';
@Component({
selector: 'app-customer-detail',
templateUrl: './customer-detail.component.html',
styleUrls: ['./customer-detail.component.css'],
})
export class CustomerDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
form: FormGroup;
item: Customer = new Customer();
hide: boolean;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private toaster: ToasterService,
private dialog: MatDialog,
private ser: CustomerService,
) {
this.hide = true;
// Create form
this.form = this.fb.group({
name: '',
phone: '',
address: '',
printInBill: false,
discounts: this.fb.array([]),
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { item: Customer };
this.showItem(data.item);
});
}
showItem(item: Customer) {
this.item = item;
(this.form.get('name') as AbstractControl).setValue(item.name);
(this.form.get('phone') as AbstractControl).setValue(item.phone);
(this.form.get('address') as AbstractControl).setValue(item.address);
(this.form.get('printInBill') as AbstractControl).setValue(item.printInBill);
this.form.setControl(
'discounts',
this.fb.array(
item.discounts.map((x) =>
this.fb.group({
discount: '' + x.discount * 100,
}),
),
),
);
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement !== undefined) {
this.nameElement.nativeElement.focus();
}
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/customers');
},
(error) => {
this.toaster.show('Error', error);
},
);
}
delete() {
this.ser.delete(this.item.id as string).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/customers');
},
(error) => {
this.toaster.show('Error', error);
},
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Customer?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Customer {
const formModel = this.form.value;
this.item.name = formModel.name;
this.item.phone = formModel.phone;
this.item.address = formModel.address;
this.item.printInBill = formModel.printInBill;
const array = this.form.get('discounts') as FormArray;
this.item.discounts.forEach((item, index) => {
item.discount = Math.max(
Math.min(round(array.controls[index].value.discount / 100, 5), 100),
0,
);
});
return this.item;
}
}