Added hasPermission function to the auth service to check for permissions.

Added: Bill Type selection
Fix: Add product would add to old kots if product was added earlier
This commit is contained in:
Amritanshu 2019-08-08 16:35:03 +05:30
parent c81b92c336
commit 6503982897
8 changed files with 141 additions and 21 deletions

View File

@ -70,4 +70,8 @@ export class AuthService {
); );
} }
hasPermission(permission: string) : boolean {
return this.user !== undefined && this.user.isAuthenticated && this.user.perms.indexOf(permission) !== -1;
}
} }

View File

@ -0,0 +1,11 @@
.square-button {
min-width: 150px;
max-width: 150px;
min-height: 150px;
cursor: pointer;
margin: 20px;
}
.selected {
background-color: green;
}

View File

@ -0,0 +1,18 @@
<div fxLayout="row wrap" fxLayoutGap="grid 20px">
<mat-card fxLayout="column" class="square-button" matRipple (click)="select('REGULAR_BILL')"
[class.selected]="selected == 'REGULAR_BILL'">
<h3 class="item-name">Regular</h3>
</mat-card>
<mat-card fxLayout="column" class="square-button" matRipple (click)="select('STAFF')"
[class.selected]="selected == 'STAFF'">
<h3 class="item-name">Staff</h3>
</mat-card>
<mat-card fxLayout="column" class="square-button" matRipple (click)="select('NO_CHARGE')"
[class.selected]="selected == 'NO_CHARGE'">
<h3 class="item-name">No Charge</h3>
</mat-card>
</div>
<mat-dialog-actions align="end">
<button mat-button [mat-dialog-close]="false">Cancel</button>
<button mat-button [mat-dialog-close]="selected">Done</button>
</mat-dialog-actions>

View File

@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BillTypeComponent } from './bill-type.component';
describe('BillTypeComponent', () => {
let component: BillTypeComponent;
let fixture: ComponentFixture<BillTypeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BillTypeComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BillTypeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,28 @@
import { Component } from '@angular/core';
import { MatDialogRef } from '@angular/material';
import { PrintType } from "../bills/bill";
@Component({
selector: 'app-bill-type',
templateUrl: './bill-type.component.html',
styleUrls: ['./bill-type.component.css']
})
export class BillTypeComponent {
selected: string;
constructor(
public dialogRef: MatDialogRef<BillTypeComponent>,
) {
this.selected = null;
}
select(s: string) {
this.selected = s;
}
accept(): void {
this.dialogRef.close(PrintType[this.selected]);
}
}

View File

@ -57,7 +57,9 @@ export class BillService {
} }
addProduct(product: Product): void { addProduct(product: Product): void {
const old = this.data.find(x => !x.isKot && x.productId === product.id && x.isHappyHour === product.hasHappyHour); const old = this.data.find(
x => !x.isKot && !x.id && x.productId === product.id && x.isHappyHour === product.hasHappyHour
);
if (old !== undefined) { if (old !== undefined) {
old.quantity += 1; old.quantity += 1;
} else { } else {
@ -170,16 +172,15 @@ export class BillService {
); );
} }
printBill(guest_book_id: string): Observable<Bill> { printBill(guest_book_id: string, printType: PrintType): Observable<Bill> {
let item = JSON.parse(JSON.stringify(this.bill)); let item = JSON.parse(JSON.stringify(this.bill));
item.kots.forEach(k => { item.kots.forEach(k => {
k.inventories.forEach(i => { k.inventories.forEach(i => {
i.discount = this.data.find(x => !x.isKot && x.id === i.id).discount; i.discount = this.data.find(x => !x.isKot && x.id === i.id).discount;
}) })
}); });
console.log(item);
item.kots.push(this.getKot()); item.kots.push(this.getKot());
return this.ser.saveOrUpdate(item, PrintType.Bill, guest_book_id, true).pipe( return this.ser.saveOrUpdate(item, printType, guest_book_id, true).pipe(
tap(x => console.log(x)) tap(x => console.log(x))
); );
} }

View File

@ -1,12 +1,15 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router"; import { ActivatedRoute, Router } from "@angular/router";
import { MatDialog } from "@angular/material"; import { MatDialog } from "@angular/material";
import { mergeMap, tap } from "rxjs/operators"; import { concatMap, tap } from "rxjs/operators";
import { Observable, of as observableOf } from "rxjs"; import { iif, Observable, of as observableOf, throwError} from "rxjs";
import { BillService } from '../bill.service'; import { BillService } from '../bill.service';
import { ToasterService } from "../../core/toaster.service"; import { ToasterService } from "../../core/toaster.service";
import { DiscountComponent } from "../discount/discount.component"; import { DiscountComponent } from "../discount/discount.component";
import { SaleCategoryService } from "../../sale-category/sale-category.service"; import { SaleCategoryService } from "../../sale-category/sale-category.service";
import { BillTypeComponent } from "../bill-type/bill-type.component";
import { PrintType } from "../bills/bill";
import { AuthService } from "../../auth/auth.service";
@Component({ @Component({
selector: 'app-sales-home', selector: 'app-sales-home',
@ -19,6 +22,7 @@ export class SalesHomeComponent implements OnInit {
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private dialog: MatDialog, private dialog: MatDialog,
private auth: AuthService,
private toaster: ToasterService, private toaster: ToasterService,
private mcSer: SaleCategoryService, private mcSer: SaleCategoryService,
private bs: BillService) { private bs: BillService) {
@ -39,10 +43,12 @@ export class SalesHomeComponent implements OnInit {
} }
discount(): void { discount(): void {
this.showDiscount().subscribe(); if (this.auth.hasPermission("Discount")) {
this.showDiscount().subscribe();
}
} }
showDiscount(): Observable<boolean | { id: string, name: string, discount: number }[]> { showDiscount(): Observable<boolean | { id: string, name: string, discount: number }[]> {
const dialogRef = this.dialog.open(DiscountComponent, { const dialogRef = this.dialog.open(DiscountComponent, {
// width: '750px', // width: '750px',
data: this.mcSer.listForDiscount() data: this.mcSer.listForDiscount()
@ -56,24 +62,48 @@ export class SalesHomeComponent implements OnInit {
) )
} }
discountDialog (canGiveDiscount: boolean) : Observable<any> {
let discObs = null;
if (canGiveDiscount) {
return discObs = this.showDiscount();
} else {
return discObs = observableOf("");
}
}
billTypeDialog() {
return this.dialog.open(BillTypeComponent).afterClosed().pipe(
tap(x => {
if (!x) {
throwError ("No Bill Type Chosen")
}
})
)
}
printBill() { printBill() {
const canGiveDiscount = true; const canGiveDiscount = this.auth.hasPermission("Discount");
let guestBookId = null; let guestBookId = null;
if (this.route.snapshot.queryParamMap.has("guest")) { if (this.route.snapshot.queryParamMap.has("guest")) {
guestBookId = this.route.snapshot.queryParamMap.get("guest"); guestBookId = this.route.snapshot.queryParamMap.get("guest");
} }
let discObs = null this.discountDialog(canGiveDiscount).pipe(
if (!canGiveDiscount) { concatMap(() => this.billTypeDialog())
discObs = this.showDiscount(); ).pipe(
} else { concatMap(
discObs = observableOf(""); (x: boolean | PrintType) => iif(
} () => !!x,
discObs.pipe( this.bs.printBill(guestBookId, x as PrintType),
mergeMap(x => this.bs.printBill(guestBookId)) throwError(x)
).subscribe(x => { )
this.toaster.show('Success', ''); ),
this.router.navigate(['/sales']); ).subscribe(() => {
}); this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
x => {
this.toaster.show('Error', "No Bill Type Chosen")
});
} }
} }

View File

@ -26,6 +26,7 @@ import { SalesHomeComponent } from './home/sales-home.component';
import { BillService } from './bill.service'; import { BillService } from './bill.service';
import { QuantityComponent } from './quantity/quantity.component'; import { QuantityComponent } from './quantity/quantity.component';
import { DiscountComponent } from "./discount/discount.component"; import { DiscountComponent } from "./discount/discount.component";
import {BillTypeComponent} from "./bill-type/bill-type.component";
@NgModule({ @NgModule({
providers: [ providers: [
@ -33,6 +34,7 @@ import { DiscountComponent } from "./discount/discount.component";
], ],
declarations: [ declarations: [
BillsComponent, BillsComponent,
BillTypeComponent,
DiscountComponent, DiscountComponent,
MenuCategoriesComponent, MenuCategoriesComponent,
ModifiersComponent, ModifiersComponent,
@ -61,6 +63,7 @@ import { DiscountComponent } from "./discount/discount.component";
SalesRoutingModule SalesRoutingModule
], ],
entryComponents: [ entryComponents: [
BillTypeComponent,
DiscountComponent, DiscountComponent,
ModifiersComponent, ModifiersComponent,
QuantityComponent QuantityComponent