Customer discount with prefill discount in sales.
This commit is contained in:
16
bookie/src/app/sales/discount/discount-item.ts
Normal file
16
bookie/src/app/sales/discount/discount-item.ts
Normal file
@ -0,0 +1,16 @@
|
||||
export class DiscountItem {
|
||||
id: string;
|
||||
name: string;
|
||||
discount: number;
|
||||
discountLimit: number;
|
||||
customerDiscount: number;
|
||||
|
||||
public constructor(init?: Partial<DiscountItem>) {
|
||||
this.id = '';
|
||||
this.name = '';
|
||||
this.discount = 0;
|
||||
this.discountLimit = 0;
|
||||
this.customerDiscount = 0;
|
||||
Object.assign(this, init);
|
||||
}
|
||||
}
|
||||
@ -12,9 +12,11 @@
|
||||
<ng-container matColumnDef="discount">
|
||||
<mat-header-cell *matHeaderCellDef class="center" fxFlex>Discount</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row; let i = index" class="center" [formGroupName]="i" fxFlex>
|
||||
<mat-form-field>
|
||||
<mat-form-field fxFlex="100%">
|
||||
<input matInput type="number" formControlName="discount" autocomplete="off" />
|
||||
<mat-hint>Maximum Discount {{ row.discountLimit | percent: '1.2-2' }}</mat-hint>
|
||||
<span matSuffix>%</span>
|
||||
<mat-hint>Cust: {{ row.customerDiscount | percent: '1.2-2' }}</mat-hint>
|
||||
<mat-hint align="end">Max: {{ row.discountLimit | percent: '1.2-2' }}</mat-hint>
|
||||
</mat-form-field>
|
||||
</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
@ -5,6 +5,7 @@ import { round } from 'mathjs';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { DiscountDataSource } from './discount-datasource';
|
||||
import { DiscountItem } from './discount-item';
|
||||
|
||||
@Component({
|
||||
selector: 'app-modifiers',
|
||||
@ -12,7 +13,7 @@ import { DiscountDataSource } from './discount-datasource';
|
||||
styleUrls: ['./discount.component.css'],
|
||||
})
|
||||
export class DiscountComponent {
|
||||
list: { name: string; discount: number; discountLimit: number }[] = [];
|
||||
list: DiscountItem[] = [];
|
||||
form: FormGroup;
|
||||
dataSource: DiscountDataSource = new DiscountDataSource([]);
|
||||
|
||||
@ -22,12 +23,12 @@ export class DiscountComponent {
|
||||
public dialogRef: MatDialogRef<DiscountComponent>,
|
||||
private fb: FormBuilder,
|
||||
@Inject(MAT_DIALOG_DATA)
|
||||
public data: Observable<{ name: string; discount: number; discountLimit: number }[]>,
|
||||
public data: Observable<DiscountItem[]>,
|
||||
) {
|
||||
this.form = this.fb.group({
|
||||
discounts: '',
|
||||
});
|
||||
this.data.subscribe((list: { name: string; discount: number; discountLimit: number }[]) => {
|
||||
this.data.subscribe((list: DiscountItem[]) => {
|
||||
this.list = list;
|
||||
this.form.setControl(
|
||||
'discounts',
|
||||
@ -35,12 +36,15 @@ export class DiscountComponent {
|
||||
this.list.map((x) =>
|
||||
this.fb.group({
|
||||
name: [x.name],
|
||||
discount: ['', [Validators.min(0), Validators.max(x.discountLimit * 100)]],
|
||||
discount: [
|
||||
'' + (x.discount !== 0 ? x.discount * 100 : ''),
|
||||
[Validators.min(0), Validators.max(x.discountLimit * 100)],
|
||||
],
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
this.dataSource = new DiscountDataSource(list);
|
||||
this.dataSource = new DiscountDataSource(this.list);
|
||||
});
|
||||
}
|
||||
|
||||
@ -49,13 +53,15 @@ export class DiscountComponent {
|
||||
for (let i = this.list.length - 1; i >= 0; i--) {
|
||||
const item = this.list[i];
|
||||
const control = (array.controls[i] as FormGroup).controls.discount as FormControl;
|
||||
if (control.pristine || control.value === null) {
|
||||
console.log(item.name, control.value, typeof control.value);
|
||||
if (
|
||||
control.value === null ||
|
||||
control.value === '' ||
|
||||
(control.pristine && control.value === '0')
|
||||
) {
|
||||
this.list.splice(i, 1);
|
||||
} else {
|
||||
item.discount = Math.max(
|
||||
Math.min(round(array.controls[i].value.discount / 100, 5), item.discountLimit),
|
||||
0,
|
||||
);
|
||||
item.discount = Math.max(Math.min(round(control.value / 100, 5), item.discountLimit), 0);
|
||||
}
|
||||
}
|
||||
this.dialogRef.close(this.list);
|
||||
|
||||
@ -8,6 +8,7 @@ import { AuthService } from '../../auth/auth.service';
|
||||
import { ReceivePaymentItem } from '../../core/receive-payment-item';
|
||||
import { Table } from '../../core/table';
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { CustomerDiscountsService } from '../../customers/customer-discounts.service';
|
||||
import { SaleCategoryService } from '../../sale-category/sale-category.service';
|
||||
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
||||
import { TableService } from '../../tables/table.service';
|
||||
@ -33,8 +34,9 @@ export class SalesHomeComponent {
|
||||
private dialog: MatDialog,
|
||||
private auth: AuthService,
|
||||
private toaster: ToasterService,
|
||||
private mcSer: SaleCategoryService,
|
||||
private tSer: TableService,
|
||||
private saleCategoryService: SaleCategoryService,
|
||||
private tableService: TableService,
|
||||
private customerDiscountsService: CustomerDiscountsService,
|
||||
private bs: BillService,
|
||||
) {}
|
||||
|
||||
@ -78,7 +80,7 @@ export class SalesHomeComponent {
|
||||
if (this.discountAllowed()) {
|
||||
const dialogRef = this.dialog.open(DiscountComponent, {
|
||||
// width: '750px',
|
||||
data: this.mcSer.listForDiscount(),
|
||||
data: this.customerDiscountsService.list(this.bs.bill.customer?.id),
|
||||
});
|
||||
return dialogRef.afterClosed();
|
||||
}
|
||||
@ -338,7 +340,7 @@ export class SalesHomeComponent {
|
||||
> {
|
||||
return this.dialog
|
||||
.open(SplitBillComponent, {
|
||||
data: this.mcSer
|
||||
data: this.saleCategoryService
|
||||
.list()
|
||||
.pipe(map((x) => x.map((y) => ({ id: y.id, name: y.name, selected: false })))),
|
||||
})
|
||||
@ -350,7 +352,7 @@ export class SalesHomeComponent {
|
||||
.open(TablesDialogComponent, {
|
||||
// width: '750px',
|
||||
data: {
|
||||
list: this.tSer.running(),
|
||||
list: this.tableService.running(),
|
||||
canChooseRunning,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user