Receive payment working.
TODO: Nested subscribe in Receive Payment in sales-home-component.ts should be refactored into something more readable.
This commit is contained in:
@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
|
||||
import { MatDialog } from '@angular/material';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import * as math from 'mathjs';
|
||||
import { Product } from '../core/product';
|
||||
import { ModifiersComponent } from './modifiers/modifiers.component';
|
||||
import { ModifierCategoryService } from '../modifier-categories/modifier-category.service';
|
||||
@ -41,7 +42,7 @@ export class BillService {
|
||||
productId: i.product.id,
|
||||
isHappyHour: i.isHappyHour,
|
||||
isPrinted: true,
|
||||
info: `${i.product.name} (${i.product.units}) @ ${i.price} - ${i.discount * 100}%`,
|
||||
info: `${i.product.name} (${i.product.units}) @ ${i.price} - ${math.round(i.discount * 100, 2)}%`,
|
||||
price: i.price,
|
||||
quantity: i.quantity,
|
||||
discount: i.discount,
|
||||
@ -139,7 +140,7 @@ export class BillService {
|
||||
this.data.forEach(x=> {
|
||||
if (!x.isKot) {
|
||||
x.discount = discounts.find(d => d.id === x.product.saleCategory.id).discount / 100;
|
||||
x.info = `${x.product.name} (${x.product.units}) @ ${x.price} - ${x.discount * 100}%`;
|
||||
x.info = `${x.product.name} (${x.product.units}) @ ${x.price} - ${math.round(x.discount * 100, 2)}%`;
|
||||
}
|
||||
});
|
||||
this.dataObs.next(this.data);
|
||||
@ -185,4 +186,21 @@ export class BillService {
|
||||
);
|
||||
}
|
||||
|
||||
amount() {
|
||||
return math.round(this.bill.kots.reduce(
|
||||
(ka: number, k: Kot) => ka + k.inventories.reduce(
|
||||
(ca: number, c: Inventory) => ca + ((c.isHappyHour ? 0 : c.price) * c.quantity * (1 - c.discount) * (1 + c.taxRate))
|
||||
, 0)
|
||||
, 0))
|
||||
}
|
||||
|
||||
type() {
|
||||
return this.bill.voucherType;
|
||||
}
|
||||
|
||||
receivePayment(amounts: { id: string; name: string; amount: number }[]) {
|
||||
return this.ser.receivePayment(this.bill.id, amounts, true).pipe(
|
||||
tap(x => console.log(x))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,4 +75,12 @@ export class VoucherService {
|
||||
return this.update(voucher, printType, guest_book_id, updateTable);
|
||||
}
|
||||
}
|
||||
|
||||
receivePayment(id: string, amounts: { id: string; name: string; amount: number }[], updateTable: boolean): Observable<Bill> {
|
||||
const options = {params: new HttpParams().set('r', "").set('u', updateTable.toString())};
|
||||
return <Observable<Bill>>this.http.post<Bill>(`${url}/${id}`, amounts, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'receivePayment'))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,7 +15,19 @@
|
||||
<mat-card fxLayout="column" class="square-button warn" matRipple [routerLink]="['../../tables']">
|
||||
<h3 class="item-name">Back to Tables</h3>
|
||||
</mat-card>
|
||||
<mat-card fxLayout="column" class="square-button" matRipple [routerLink]="['../../tables']">
|
||||
<mat-card fxLayout="column" class="square-button" matRipple (click)="receivePayment()">
|
||||
<h3 class="item-name">Receive Payment</h3>
|
||||
</mat-card>
|
||||
<mat-card fxLayout="column" class="square-button" matRipple [routerLink]="['../../tables']">
|
||||
<h3 class="item-name">Move Table</h3>
|
||||
</mat-card>
|
||||
<mat-card fxLayout="column" class="square-button" matRipple [routerLink]="['../../tables']">
|
||||
<h3 class="item-name">Void Bill</h3>
|
||||
</mat-card>
|
||||
<mat-card fxLayout="column" class="square-button" matRipple [routerLink]="['../../tables']">
|
||||
<h3 class="item-name">Move KOT</h3>
|
||||
</mat-card>
|
||||
<mat-card fxLayout="column" class="square-button" matRipple [routerLink]="['../../tables']">
|
||||
<h3 class="item-name">Split Bill</h3>
|
||||
</mat-card>
|
||||
</div>
|
||||
|
||||
@ -10,6 +10,7 @@ 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";
|
||||
import { ReceivePaymentComponent } from "../receive-payment/receive-payment.component";
|
||||
|
||||
@Component({
|
||||
selector: 'app-sales-home',
|
||||
@ -106,4 +107,24 @@ export class SalesHomeComponent implements OnInit {
|
||||
this.toaster.show('Error', "No Bill Type Chosen")
|
||||
});
|
||||
}
|
||||
|
||||
receivePayment() {
|
||||
let amount = this.bs.amount();
|
||||
let type = this.bs.type();
|
||||
const dialogRef = this.dialog.open(ReceivePaymentComponent, {
|
||||
// width: '750px',
|
||||
data: {
|
||||
type: type,
|
||||
amount: amount
|
||||
}
|
||||
});
|
||||
dialogRef.afterClosed().subscribe((result: boolean | { id: string, name: string, amount: number }[]) => {
|
||||
if (!!result) {
|
||||
this.bs.receivePayment(result as { id: string, name: string, amount: number }[]).subscribe(() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigate(['/sales']);
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
import { DataSource } from '@angular/cdk/collections';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
|
||||
export class ReceivePaymentDatasource extends DataSource<{name: string, discount: number}> {
|
||||
|
||||
constructor(private data: {name: string, discount: number}[]) {
|
||||
super();
|
||||
}
|
||||
|
||||
connect(): Observable<{name: string, discount: number}[]> {
|
||||
return observableOf(this.data);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
.right {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
.bold {
|
||||
font-weight: bold;
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
<h2 mat-dialog-title>Receive Payment</h2>
|
||||
<mat-dialog-content>
|
||||
<form [formGroup]="form" fxLayout="column">
|
||||
<mat-table #table [dataSource]="dataSource" formArrayName="amounts">
|
||||
|
||||
<!-- Name Column -->
|
||||
<ng-container matColumnDef="name">
|
||||
<mat-header-cell *matHeaderCellDef class="bold">Amount</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
|
||||
<mat-footer-cell *matFooterCellDef class="bold">Balance</mat-footer-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Discount Column -->
|
||||
<ng-container matColumnDef="amount">
|
||||
<mat-header-cell *matHeaderCellDef class="center bold" fxFlex>{{ amount | currency:'INR'}}</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row; let i = index" class="center" [formGroupName]="i" fxFlex>
|
||||
<mat-form-field>
|
||||
<input matInput type="number" formControlName="amount" autocomplete="off">
|
||||
</mat-form-field>
|
||||
</mat-cell>
|
||||
<mat-footer-cell *matFooterCellDef class="bold">{{ balance | currency:'INR' }}</mat-footer-cell>
|
||||
</ng-container>
|
||||
|
||||
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
|
||||
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
|
||||
<mat-footer-row *matFooterRowDef="displayedColumns"></mat-footer-row>
|
||||
</mat-table>
|
||||
</form>
|
||||
</mat-dialog-content>
|
||||
<mat-dialog-actions align="end">
|
||||
<button mat-button [mat-dialog-close]="false">Cancel</button>
|
||||
<button mat-button (click)="accept()" color="primary" [disabled]="!!balance">Ok</button>
|
||||
</mat-dialog-actions>
|
||||
@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ReceivePaymentComponent } from './receive-payment.component';
|
||||
|
||||
describe('ReceivePaymentComponent', () => {
|
||||
let component: ReceivePaymentComponent;
|
||||
let fixture: ComponentFixture<ReceivePaymentComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ ReceivePaymentComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(ReceivePaymentComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,104 @@
|
||||
import { Component, Inject } from '@angular/core';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
|
||||
import { FormArray, FormBuilder, FormGroup } from "@angular/forms";
|
||||
import { distinctUntilChanged } from "rxjs/operators";
|
||||
import { ReceivePaymentDatasource } from "./receive-payment-datasource";
|
||||
import { PrintType } from "../bills/bill";
|
||||
|
||||
@Component({
|
||||
selector: 'app-receive-payment',
|
||||
templateUrl: './receive-payment.component.html',
|
||||
styleUrls: ['./receive-payment.component.css']
|
||||
})
|
||||
export class ReceivePaymentComponent {
|
||||
choices = {
|
||||
REGULAR_BILL: [
|
||||
{
|
||||
id: 2,
|
||||
name: "Cash",
|
||||
amount: 0
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Credit Card",
|
||||
amount: 0
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
name: "Bill To Company",
|
||||
amount: 0
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
name: "Tip",
|
||||
amount: 0
|
||||
}
|
||||
]
|
||||
,
|
||||
NO_CHARGE: [
|
||||
{
|
||||
id: 4,
|
||||
name: "No Charge",
|
||||
amount: 0
|
||||
}
|
||||
],
|
||||
STAFF: [
|
||||
{
|
||||
id: 10,
|
||||
name: "Staff Account",
|
||||
amount: 0
|
||||
}
|
||||
]
|
||||
};
|
||||
type: PrintType;
|
||||
amount: number;
|
||||
balance: number;
|
||||
form: FormGroup;
|
||||
dataSource: ReceivePaymentDatasource;
|
||||
|
||||
displayedColumns = ['name', 'amount'];
|
||||
|
||||
constructor(
|
||||
public dialogRef: MatDialogRef<ReceivePaymentComponent>,
|
||||
private fb: FormBuilder,
|
||||
@Inject(MAT_DIALOG_DATA) public data: {type: PrintType, amount: number}
|
||||
) {
|
||||
this.createForm();
|
||||
this.type = data.type;
|
||||
this.amount = data.amount;
|
||||
this.balance = data.amount;
|
||||
this.form.setControl('amounts', this.fb.array(
|
||||
this.choices[this.type].map(
|
||||
x => this.fb.group({
|
||||
name: [x.name],
|
||||
amount: [""]
|
||||
})
|
||||
)
|
||||
));
|
||||
this.dataSource = new ReceivePaymentDatasource(this.choices[this.type]);
|
||||
this.listenToAmountChange();
|
||||
}
|
||||
|
||||
createForm() {
|
||||
this.form = this.fb.group({
|
||||
amounts: ''
|
||||
});
|
||||
}
|
||||
|
||||
listenToAmountChange() {
|
||||
const array = this.form.get('amounts') as FormArray;
|
||||
this.choices[this.type].forEach(
|
||||
(z, i) => array.controls[i].valueChanges.pipe(
|
||||
distinctUntilChanged()
|
||||
).subscribe(x => {
|
||||
this.choices[this.type].find(s => s.name == x.name).amount = (x.amount === "" ? 0 : parseInt(x.amount, 10));
|
||||
this.balance = this.amount - this.choices[this.type].reduce((a, c) => a + c.amount, 0);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
accept(): void {
|
||||
this.dialogRef.close(this.choices[this.type]);
|
||||
}
|
||||
}
|
||||
@ -26,7 +26,8 @@ import { SalesHomeComponent } from './home/sales-home.component';
|
||||
import { BillService } from './bill.service';
|
||||
import { QuantityComponent } from './quantity/quantity.component';
|
||||
import { DiscountComponent } from "./discount/discount.component";
|
||||
import {BillTypeComponent} from "./bill-type/bill-type.component";
|
||||
import { BillTypeComponent } from "./bill-type/bill-type.component";
|
||||
import { ReceivePaymentComponent } from "./receive-payment/receive-payment.component";
|
||||
|
||||
@NgModule({
|
||||
providers: [
|
||||
@ -40,6 +41,7 @@ import {BillTypeComponent} from "./bill-type/bill-type.component";
|
||||
ModifiersComponent,
|
||||
ProductsComponent,
|
||||
QuantityComponent,
|
||||
ReceivePaymentComponent,
|
||||
RunningTablesComponent,
|
||||
SalesHomeComponent
|
||||
],
|
||||
@ -66,7 +68,8 @@ import {BillTypeComponent} from "./bill-type/bill-type.component";
|
||||
BillTypeComponent,
|
||||
DiscountComponent,
|
||||
ModifiersComponent,
|
||||
QuantityComponent
|
||||
QuantityComponent,
|
||||
ReceivePaymentComponent
|
||||
]
|
||||
})
|
||||
export class SalesModule { }
|
||||
|
||||
Reference in New Issue
Block a user