Bill change should be working

Reduce quantity should be working.
This commit is contained in:
2020-10-11 20:53:43 +05:30
parent 73f83f1aa7
commit b7f382cac8
52 changed files with 283 additions and 641 deletions

View File

@ -199,5 +199,5 @@
</mat-card>
</div>
<footer class="footer">
<p>Backend: v{{ auth.user.ver }} / Frontend: v{{ version }}</p>
<p>Backend: v{{ auth.user?.ver }} / Frontend: v{{ version }}</p>
</footer>

View File

@ -79,13 +79,27 @@ export class BillService {
this.updateAmounts();
}
addProduct(product: Product): void {
minimum(productId: string, happyHour: boolean): number {
return this.data.reduce(
(a, c) => (c.productId === productId && c.isHappyHour === happyHour ? a + c.quantity : a),
0,
);
}
addProduct(product: Product, quantity: number, discount: number): void {
const old = this.data.find(
(x) =>
!x.isKot && !x.id && x.productId === product.id && x.isHappyHour === product.hasHappyHour,
);
if (quantity < 0) {
const minimum = this.minimum(product.id, product.hasHappyHour) + quantity;
if (minimum + quantity < 0) {
this.toaster.show('Error', 'Total quantity cannot be negative!');
return;
}
}
if (old !== undefined) {
old.quantity += 1;
old.quantity += quantity;
} else {
const item = {
isKot: false,
@ -94,8 +108,8 @@ export class BillService {
isHappyHour: product.hasHappyHour,
info: `${product.name} @ ${product.price} - ${0}%`,
price: product.price,
quantity: 1,
discount: 0,
quantity,
discount,
taxRate: product.tax.rate,
tax: product.tax,
modifiers: [],
@ -146,8 +160,8 @@ export class BillService {
this.updateAmounts();
}
subtractOne(item: any): void {
if (item.quantity > 1) {
subtractOne(item: any, canEdit: boolean): void {
if (item.quantity > 1 || (canEdit && this.minimum(item.productId, item.isHappyHour) >= 1)) {
item.quantity -= 1;
this.dataObs.next(this.data);
this.updateAmounts();

View File

@ -94,7 +94,7 @@
<button
mat-icon-button
(click)="quantity(row)"
[disabled]="row.isPrinted"
[disabled]="rowQuantityDisabled(row)"
*ngIf="!row.isKot"
>
{{ row.quantity }}

View File

@ -106,12 +106,24 @@ export class BillsComponent implements OnInit {
if (!result) {
return;
}
this.bs.quantity(item, result as number);
if (!item.isPrinted) {
this.bs.quantity(item, result as number);
} else {
const quantity = result as number;
const product = {
...item.product,
hasHappyHour: item.isHappyHour,
tax: item.tax,
price: item.price,
};
this.bs.addProduct(product, quantity, item.discount);
}
});
}
subtractOne(item: any): void {
this.bs.subtractOne(item);
const canEdit = this.auth.user.perms.indexOf('edit-printed-product') !== -1;
this.bs.subtractOne(item, canEdit);
}
removeItem(item: any): void {
@ -171,4 +183,17 @@ export class BillsComponent implements OnInit {
},
);
}
rowQuantityDisabled(row: any) {
if (!row.isPrinted) {
return false;
}
if (this.bs.bill.isVoid) {
return true;
}
if (this.auth.user.perms.indexOf('edit-printed-product') === -1) {
return true;
}
return false;
}
}

View File

@ -99,9 +99,7 @@ export class SalesHomeComponent {
if (this.bs.bill.voucherType !== PrintType.Kot) {
return observableOf(this.bs.bill.voucherType);
}
return this.dialog
.open(BillTypeComponent)
.afterClosed();
return this.dialog.open(BillTypeComponent).afterClosed();
}
confirmTableDialog(table: Table): Observable<{ table: Table; confirmed: boolean }> {
@ -159,13 +157,13 @@ export class SalesHomeComponent {
}
}),
switchMap(() => this.billTypeDialog()),
switchMap((x)=> {
switchMap((x) => {
if (x) {
return observableOf(x);
}
return throwError('No Bill Type Chosen')
return throwError('No Bill Type Chosen');
}),
switchMap((x: PrintType) => this.bs.printBill(guestBookId, x as PrintType)),
switchMap((x: PrintType) => this.bs.printBill(guestBookId, x as PrintType)),
)
.subscribe(
() => {

View File

@ -21,6 +21,6 @@ export class ProductsComponent implements OnInit {
}
addProduct(product: Product): void {
this.bs.addProduct(product);
this.bs.addProduct(product, 1, 0);
}
}

View File

@ -2,6 +2,8 @@ import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { MathService } from '../../shared/math.service';
@Component({
selector: 'app-quantity',
templateUrl: './quantity.component.html',
@ -14,6 +16,7 @@ export class QuantityComponent implements OnInit {
public dialogRef: MatDialogRef<QuantityComponent>,
@Inject(MAT_DIALOG_DATA) public data: number,
private fb: FormBuilder,
private math: MathService,
) {
this.createForm();
}
@ -31,7 +34,7 @@ export class QuantityComponent implements OnInit {
}
accept(): void {
const { quantity } = this.form.value;
const quantity = this.math.parseAmount(this.form.value.quantity);
this.dialogRef.close(quantity);
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MathService } from './math.service';
describe('MathService', () => {
let service: MathService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MathService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { evaluate, round } from 'mathjs';
@Injectable({
providedIn: 'root',
})
export class MathService {
// eslint-disable-next-line class-methods-use-this
journalAmount(amount: string = '', debit: number): any {
const val = this.parseAmount(amount, 2);
let newDebit = debit;
if (val < 0) {
newDebit *= -1;
}
return { debit: newDebit, amount: Math.abs(val) };
}
// eslint-disable-next-line class-methods-use-this
parseAmount(amount: string = '', rounding: number = 2): number {
const cleaned = `${amount}`.replace(new RegExp('(₹[s]*)|(,)|(s)', 'g'), '');
if (cleaned === '') {
return 0;
}
return round(evaluate(cleaned), rounding);
}
}