Breaking: Discount is applicable on sale category and not on menu category

Fix the import, etc on this.
While entering discount in sale, it checks the max allowed.
This commit is contained in:
2020-12-16 11:49:22 +05:30
parent e229ecefa2
commit e4500f0d46
48 changed files with 353 additions and 277 deletions

View File

@ -1,7 +1,7 @@
import { SelectionModel } from '@angular/cdk/collections';
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import * as math from 'mathjs';
import { round } from 'mathjs';
import { BehaviorSubject } from 'rxjs';
import { Observable } from 'rxjs/internal/Observable';
@ -9,6 +9,7 @@ import { BillViewItem } from '../core/bill-view-item';
import { ModifierCategory } from '../core/modifier-category';
import { Product } from '../core/product';
import { ReceivePaymentItem } from '../core/receive-payment-item';
import { SaleCategory } from '../core/sale-category';
import { Table } from '../core/table';
import { ToasterService } from '../core/toaster.service';
import { ModifierCategoryService } from '../modifier-categories/modifier-category.service';
@ -63,7 +64,7 @@ export class BillService {
productId: i.product.id,
isHappyHour: i.isHappyHour,
isPrinted: true,
info: `${i.product.name} @ ${i.price} - ${math.round(i.discount * 100, 2)}%`,
info: `${i.product.name} @ ${i.price} - ${round(i.discount * 100, 2)}%`,
price: i.price,
quantity: i.quantity,
discount: i.discount,
@ -182,13 +183,11 @@ export class BillService {
discount(discounts: { id: string; name: string; discount: number }[]): void {
this.data.forEach((x) => {
if (!x.isKot) {
x.discount =
(discounts.find((d) => d.id === x.product.saleCategory.id) as {
id: string;
name: string;
discount: number;
}).discount / 100;
x.info = `${x.product.name} @ ${x.price} - ${math.round(x.discount * 100, 2)}%`;
const e = discounts.find((d) => d.id === (x.product.saleCategory as SaleCategory).id);
if (e !== undefined) {
x.discount = e.discount;
x.info = `${x.product.name} @ ${x.price} - ${round(x.discount * 100, 2)}%`;
}
}
});
this.dataObs.next(this.data);
@ -246,7 +245,7 @@ export class BillService {
updateAmounts() {
this.netAmount.next(
math.round(
round(
this.data
.filter((x) => !x.isKot)
.reduce(
@ -256,7 +255,7 @@ export class BillService {
),
);
this.discountAmount.next(
math.round(
round(
this.data
.filter((x) => !x.isKot)
.reduce(
@ -267,7 +266,7 @@ export class BillService {
),
);
this.taxAmount.next(
math.round(
round(
this.data
.filter((x) => !x.isKot)
.reduce(
@ -278,7 +277,7 @@ export class BillService {
),
);
this.amount.next(
math.round(
round(
this.data
.filter((x) => !x.isKot)
.reduce(
@ -291,7 +290,7 @@ export class BillService {
}
amountVal(): number {
return math.round(
return round(
this.bill.kots.reduce(
(ka: number, k: Kot) =>
ka +

View File

@ -14,6 +14,7 @@
<mat-cell *matCellDef="let row; let i = index" class="center" [formGroupName]="i" fxFlex>
<mat-form-field>
<input matInput type="number" formControlName="discount" autocomplete="off" />
<mat-hint>Maximum Discount {{ row.discountLimit | percent: '1.2-2' }}</mat-hint>
</mat-form-field>
</mat-cell>
</ng-container>

View File

@ -1,6 +1,7 @@
import { Component, Inject } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { round } from 'mathjs';
import { Observable } from 'rxjs';
import { DiscountDataSource } from './discount-datasource';
@ -11,7 +12,7 @@ import { DiscountDataSource } from './discount-datasource';
styleUrls: ['./discount.component.css'],
})
export class DiscountComponent {
list: { name: string; discount: number }[] = [];
list: { name: string; discount: number; discountLimit: number }[] = [];
form: FormGroup;
dataSource: DiscountDataSource = new DiscountDataSource([]);
@ -20,12 +21,13 @@ export class DiscountComponent {
constructor(
public dialogRef: MatDialogRef<DiscountComponent>,
private fb: FormBuilder,
@Inject(MAT_DIALOG_DATA) public data: Observable<{ name: string; discount: number }[]>,
@Inject(MAT_DIALOG_DATA)
public data: Observable<{ name: string; discount: number; discountLimit: number }[]>,
) {
this.form = this.fb.group({
discounts: '',
});
this.data.subscribe((list: { name: string; discount: number }[]) => {
this.data.subscribe((list: { name: string; discount: number; discountLimit: number }[]) => {
this.list = list;
this.form.setControl(
'discounts',
@ -33,7 +35,7 @@ export class DiscountComponent {
this.list.map((x) =>
this.fb.group({
name: [x.name],
discount: ['', [Validators.min(0), Validators.max(100)]],
discount: ['', [Validators.min(0), Validators.max(x.discountLimit * 100)]],
}),
),
),
@ -45,7 +47,10 @@ export class DiscountComponent {
accept(): void {
const array = this.form.get('discounts') as FormArray;
this.list.forEach((item, index) => {
item.discount = Math.max(Math.min(array.controls[index].value.discount, 100), 0);
item.discount = Math.max(
Math.min(round(array.controls[index].value.discount / 100, 5), item.discountLimit),
0,
);
});
this.dialogRef.close(this.list);
}

View File

@ -12,6 +12,6 @@ export class MenuCategoriesResolver implements Resolve<MenuCategory[]> {
constructor(private ser: MenuCategoryService) {}
resolve(): Observable<MenuCategory[]> {
return this.ser.list();
return this.ser.list(true);
}
}