Feature: Recipe module mostly working. What needs to be done is duplicating recipes and export for checking.
Feature: Non Contract Purchases Report
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { DataSource } from '@angular/cdk/collections';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { RecipeItem } from '../recipe-item';
|
||||
|
||||
export class RecipeDetailDatasource extends DataSource<RecipeItem> {
|
||||
constructor(private data: Observable<RecipeItem[]>) {
|
||||
super();
|
||||
}
|
||||
|
||||
connect(): Observable<RecipeItem[]> {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
disconnect() {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
.example-card {
|
||||
max-width: 400px;
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<mat-card>
|
||||
<mat-card-title-group>
|
||||
<mat-card-title>Recipe Detail</mat-card-title>
|
||||
</mat-card-title-group>
|
||||
<mat-card-content>
|
||||
<form [formGroup]="form" fxLayout="column">
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
fxLayoutAlign="space-around start"
|
||||
>
|
||||
<mat-form-field fxFlex="50">
|
||||
<input
|
||||
matInput
|
||||
[matDatepicker]="validFrom"
|
||||
placeholder="Valid From"
|
||||
formControlName="validFrom"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<mat-datepicker-toggle matSuffix [for]="validFrom"></mat-datepicker-toggle>
|
||||
<mat-datepicker #validFrom></mat-datepicker>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="50">
|
||||
<input
|
||||
matInput
|
||||
[matDatepicker]="validTill"
|
||||
placeholder="Valid Till"
|
||||
formControlName="validTill"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<mat-datepicker-toggle matSuffix [for]="validTill"></mat-datepicker-toggle>
|
||||
<mat-datepicker #validTill></mat-datepicker>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px">
|
||||
<mat-form-field fxFlex="60">
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Product"
|
||||
#productElement
|
||||
[matAutocomplete]="autoP"
|
||||
formControlName="product"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<mat-autocomplete
|
||||
#autoP="matAutocomplete"
|
||||
autoActiveFirstOption
|
||||
[displayWith]="displayProduct"
|
||||
(optionSelected)="productSelected($event)"
|
||||
>
|
||||
<mat-option *ngFor="let product of products | async" [value]="product">{{
|
||||
product.name
|
||||
}}</mat-option>
|
||||
</mat-autocomplete>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="10">
|
||||
<mat-label>Yield</mat-label>
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Yield"
|
||||
formControlName="recipeYield"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="10">
|
||||
<mat-label>Sale Price</mat-label>
|
||||
<span matPrefix>₹ </span>
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Sale Price"
|
||||
formControlName="salePrice"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="10">
|
||||
<mat-label>Cost Price</mat-label>
|
||||
<span matPrefix>₹ </span>
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Cost Price"
|
||||
formControlName="costPrice"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="10">
|
||||
<mat-label>Cost Percentage</mat-label>
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Cost Percentage"
|
||||
formControlName="costPercentage"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<span matSuffix>%</span>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
formGroupName="addRow"
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex="50">
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Ingredient"
|
||||
#ingredientElement
|
||||
[matAutocomplete]="autoI"
|
||||
formControlName="ingredient"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<mat-autocomplete
|
||||
#autoI="matAutocomplete"
|
||||
autoActiveFirstOption
|
||||
[displayWith]="displayIngredient"
|
||||
(optionSelected)="ingredientSelected($event)"
|
||||
>
|
||||
<mat-option *ngFor="let product of ingredients | async" [value]="product"
|
||||
>{{ product.name }} ({{ product.fractionUnits }})</mat-option
|
||||
>
|
||||
</mat-autocomplete>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="20">
|
||||
<mat-label>Quantity</mat-label>
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Quantity"
|
||||
formControlName="quantity"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="20">
|
||||
<mat-label>Rate</mat-label>
|
||||
<span matPrefix>₹ </span>
|
||||
<input
|
||||
type="text"
|
||||
matInput
|
||||
placeholder="Rate"
|
||||
formControlName="rate"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</mat-form-field>
|
||||
|
||||
<button mat-raised-button color="primary" (click)="addRow()" fxFlex="15">Add</button>
|
||||
</div>
|
||||
<mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
|
||||
<!-- Ingredient Column -->
|
||||
<ng-container matColumnDef="product">
|
||||
<mat-header-cell *matHeaderCellDef>Product</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{ row.product.name }}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Quantity Column -->
|
||||
<ng-container matColumnDef="quantity">
|
||||
<mat-header-cell *matHeaderCellDef class="right">Quantity</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row" class="right"
|
||||
>{{ row.quantity | number: '1.2-2' }} {{ row.product.fractionUnits }}</mat-cell
|
||||
>
|
||||
</ng-container>
|
||||
|
||||
<!-- Rate Column -->
|
||||
<ng-container matColumnDef="rate">
|
||||
<mat-header-cell *matHeaderCellDef class="right">Rate</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row" class="right">{{ row.price | currency: 'INR' }}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Amount Column -->
|
||||
<ng-container matColumnDef="amount">
|
||||
<mat-header-cell *matHeaderCellDef class="right">Amount</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row" class="right">{{
|
||||
row.quantity * row.price | currency: 'INR'
|
||||
}}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Action Column -->
|
||||
<ng-container matColumnDef="action">
|
||||
<mat-header-cell *matHeaderCellDef class="center">Action</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row" class="center">
|
||||
<button mat-icon-button tabindex="-1" color="warn" (click)="deleteRow(row)">
|
||||
<mat-icon>delete</mat-icon>
|
||||
</button>
|
||||
</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
|
||||
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
|
||||
</mat-table>
|
||||
</form>
|
||||
</mat-card-content>
|
||||
<mat-card-actions>
|
||||
<button mat-raised-button (click)="save()" color="primary">
|
||||
{{ item.id ? 'Update' : 'Save' }}
|
||||
</button>
|
||||
<button mat-raised-button color="warn" (click)="confirmDelete()" *ngIf="item.id">Delete</button>
|
||||
</mat-card-actions>
|
||||
</mat-card>
|
||||
@@ -0,0 +1,29 @@
|
||||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
|
||||
import { ReactiveFormsModule } from '@angular/forms';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
|
||||
import { RecipeDetailComponent } from './recipe-detail.component';
|
||||
|
||||
describe('ProductGroupDetailComponent', () => {
|
||||
let component: RecipeDetailComponent;
|
||||
let fixture: ComponentFixture<RecipeDetailComponent>;
|
||||
|
||||
beforeEach(
|
||||
waitForAsync(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [ReactiveFormsModule, RouterTestingModule],
|
||||
declarations: [RecipeDetailComponent],
|
||||
}).compileComponents();
|
||||
}),
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(RecipeDetailComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,258 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { round } from 'mathjs';
|
||||
import * as moment from 'moment';
|
||||
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
|
||||
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { Product } from '../../core/product';
|
||||
import { ProductSku } from '../../core/product-sku';
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { ProductService } from '../../product/product.service';
|
||||
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
||||
import { MathService } from '../../shared/math.service';
|
||||
import { Recipe } from '../recipe';
|
||||
import { RecipeItem } from '../recipe-item';
|
||||
import { RecipeService } from '../recipe.service';
|
||||
|
||||
import { RecipeDetailDatasource } from './recipe-detail-datasource';
|
||||
|
||||
@Component({
|
||||
selector: 'app-product-group-detail',
|
||||
templateUrl: './recipe-detail.component.html',
|
||||
styleUrls: ['./recipe-detail.component.css'],
|
||||
})
|
||||
export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('productElement', { static: true }) productElement?: ElementRef;
|
||||
@ViewChild('ingredientElement', { static: true }) ingredientElement?: ElementRef;
|
||||
public itemsObservable = new BehaviorSubject<RecipeItem[]>([]);
|
||||
dataSource: RecipeDetailDatasource = new RecipeDetailDatasource(this.itemsObservable);
|
||||
form: FormGroup;
|
||||
product: ProductSku | null;
|
||||
products: Observable<ProductSku[]>;
|
||||
ingredient: ProductSku | null;
|
||||
ingredients: Observable<ProductSku[]>;
|
||||
item: Recipe = new Recipe();
|
||||
|
||||
displayedColumns = ['product', 'quantity', 'rate', 'amount', 'action'];
|
||||
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
private router: Router,
|
||||
private fb: FormBuilder,
|
||||
private dialog: MatDialog,
|
||||
private toaster: ToasterService,
|
||||
private math: MathService,
|
||||
private ser: RecipeService,
|
||||
private productSer: ProductService,
|
||||
) {
|
||||
this.product = null;
|
||||
this.ingredient = null;
|
||||
this.form = this.fb.group({
|
||||
validFrom: '',
|
||||
validTill: '',
|
||||
recipeYield: '',
|
||||
salePrice: '',
|
||||
costPrice: '',
|
||||
costPercentage: '',
|
||||
product: new Product(),
|
||||
addRow: this.fb.group({
|
||||
ingredient: '',
|
||||
quantity: '',
|
||||
rate: '',
|
||||
}),
|
||||
});
|
||||
// Setup Product Autocomplete
|
||||
this.products = (this.form.get('product') as FormControl).valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocompleteSku(x, false))),
|
||||
);
|
||||
// Setup Product Autocomplete
|
||||
this.ingredients = (
|
||||
(this.form.get('addRow') as FormControl).get('ingredient') as FormControl
|
||||
).valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) =>
|
||||
x === null ? observableOf([]) : this.productSer.autocompleteProduct(x, null),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.route.data.subscribe((value) => {
|
||||
const data = value as { item: Recipe };
|
||||
this.showItem(data.item);
|
||||
this.updateView();
|
||||
});
|
||||
}
|
||||
|
||||
showItem(item: Recipe) {
|
||||
this.item = item;
|
||||
this.form.setValue({
|
||||
validFrom: moment(item.validFrom, 'DD-MMM-YYYY').toDate(),
|
||||
validTill: moment(item.validTill, 'DD-MMM-YYYY').toDate(),
|
||||
recipeYield: '' + item.recipeYield,
|
||||
salePrice: '' + item.salePrice,
|
||||
costPrice: '' + item.costPrice,
|
||||
costPercentage: '',
|
||||
product: item.sku,
|
||||
addRow: {
|
||||
ingredient: null,
|
||||
quantity: '',
|
||||
rate: '',
|
||||
},
|
||||
});
|
||||
this.dataSource = new RecipeDetailDatasource(this.itemsObservable);
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
setTimeout(() => {
|
||||
if (this.productElement) {
|
||||
this.productElement.nativeElement.focus();
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
displayIngredient(product?: Product): string {
|
||||
return product ? `${product.name} (${product.fractionUnits})` : '';
|
||||
}
|
||||
|
||||
displayProduct(product?: Product): string {
|
||||
return product?.name || '';
|
||||
}
|
||||
|
||||
productSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
const product: ProductSku = event.option.value;
|
||||
this.item.sku = product;
|
||||
(this.form.get('product') as FormControl).setValue(product);
|
||||
(this.form.get('salePrice') as FormControl).setValue('' + (product.salePrice ?? 0));
|
||||
}
|
||||
|
||||
ingredientSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
const ingredient: ProductSku = event.option.value;
|
||||
this.ingredient = ingredient;
|
||||
const item = this.getItem();
|
||||
this.ser
|
||||
.getIngredientDetails(ingredient.id, item.validFrom, item.validTill)
|
||||
.subscribe((x) =>
|
||||
((this.form.get('addRow') as FormControl).get('rate') as FormControl).setValue(
|
||||
'' + x.costPrice,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
addRow() {
|
||||
const formValue = (this.form.get('addRow') as FormControl).value;
|
||||
const quantity = this.math.parseAmount(formValue.quantity, 2);
|
||||
const rate = this.math.parseAmount(formValue.rate, 2);
|
||||
if (this.ingredient === null || quantity <= 0 || rate <= 0) {
|
||||
return;
|
||||
}
|
||||
const oldFiltered = this.item.items.filter(
|
||||
(x) => x.product.id === (this.ingredient as ProductSku).id,
|
||||
);
|
||||
if (oldFiltered.length) {
|
||||
this.toaster.show('Danger', 'Product already added');
|
||||
return;
|
||||
}
|
||||
this.item.items.push(
|
||||
new RecipeItem({
|
||||
product: this.ingredient,
|
||||
quantity,
|
||||
price: rate,
|
||||
}),
|
||||
);
|
||||
this.resetAddRow();
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
resetAddRow() {
|
||||
(this.form.get('addRow') as FormControl).reset({
|
||||
ingredient: null,
|
||||
quantity: '',
|
||||
rate: '',
|
||||
});
|
||||
this.ingredient = null;
|
||||
setTimeout(() => {
|
||||
if (this.ingredientElement) {
|
||||
this.ingredientElement.nativeElement.focus();
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
updateView() {
|
||||
this.itemsObservable.next(this.item.items);
|
||||
const costPrice = round(
|
||||
this.item.items.map((x) => x.quantity * x.price).reduce((p, c) => p + c, 0),
|
||||
2,
|
||||
);
|
||||
(this.form.get('costPrice') as FormControl).setValue(costPrice);
|
||||
const salePrice = this.math.parseAmount(this.form.value.salePrice, 2);
|
||||
if (salePrice < 0) {
|
||||
return;
|
||||
}
|
||||
const costPercentage = round((100 * costPrice) / salePrice, 2);
|
||||
(this.form.get('costPercentage') as FormControl).setValue(costPercentage);
|
||||
}
|
||||
|
||||
deleteRow(row: RecipeItem) {
|
||||
this.item.items.splice(this.item.items.indexOf(row), 1);
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
save() {
|
||||
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigateByUrl('/recipes');
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
delete() {
|
||||
this.ser.delete(this.item.id as string).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigate(['/recipes']);
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
confirmDelete(): void {
|
||||
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
||||
width: '250px',
|
||||
data: { title: 'Delete Recipe?', content: 'Are you sure? This cannot be undone.' },
|
||||
});
|
||||
|
||||
dialogRef.afterClosed().subscribe((result: boolean) => {
|
||||
if (result) {
|
||||
this.delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getItem(): Recipe {
|
||||
const formModel = this.form.value;
|
||||
this.item.validFrom = moment(formModel.validFrom).format('DD-MMM-YYYY');
|
||||
this.item.validTill = moment(formModel.validTill).format('DD-MMM-YYYY');
|
||||
this.item.recipeYield = this.math.parseAmount(formModel.recipeYield, 2);
|
||||
this.item.salePrice = this.math.parseAmount(formModel.salePrice, 2);
|
||||
this.item.costPrice = this.math.parseAmount(formModel.costPrice, 2);
|
||||
return this.item;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductSku } from '../core/product-sku';
|
||||
|
||||
export class RecipeItem {
|
||||
id: string | undefined;
|
||||
product: ProductSku;
|
||||
quantity: number;
|
||||
price: number;
|
||||
|
||||
public constructor(init?: Partial<RecipeItem>) {
|
||||
this.product = new ProductSku();
|
||||
this.quantity = 0;
|
||||
this.price = 0;
|
||||
Object.assign(this, init);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
import { inject, TestBed } from '@angular/core/testing';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
|
||||
import { RecipeListResolver } from './recipe-list-resolver.service';
|
||||
|
||||
describe('RecipeListResolver', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpClientModule, RouterTestingModule],
|
||||
providers: [RecipeListResolver],
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([RecipeListResolver], (service: RecipeListResolver) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Resolve } from '@angular/router';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
|
||||
import { Recipe } from './recipe';
|
||||
import { RecipeService } from './recipe.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RecipeListResolver implements Resolve<Recipe[]> {
|
||||
constructor(private ser: RecipeService) {}
|
||||
|
||||
resolve(): Observable<Recipe[]> {
|
||||
return this.ser.list();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
import { DataSource } from '@angular/cdk/collections';
|
||||
import { EventEmitter } from '@angular/core';
|
||||
import { MatPaginator, PageEvent } from '@angular/material/paginator';
|
||||
import { MatSort, Sort } from '@angular/material/sort';
|
||||
import * as moment from 'moment';
|
||||
import { merge, Observable } from 'rxjs';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
|
||||
import { Recipe } from '../recipe';
|
||||
|
||||
export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
public data: Recipe[];
|
||||
public filteredData: Recipe[];
|
||||
public validFrom: Date | null;
|
||||
public validTill: Date | null;
|
||||
public productGroup: string;
|
||||
|
||||
constructor(
|
||||
private readonly validFromFilter: Observable<Date | null>,
|
||||
private readonly validTillFilter: Observable<Date | null>,
|
||||
private readonly productGroupFilter: Observable<string>,
|
||||
private readonly dataObs: Observable<Recipe[]>,
|
||||
|
||||
private paginator?: MatPaginator,
|
||||
private sort?: MatSort,
|
||||
) {
|
||||
super();
|
||||
this.data = [];
|
||||
this.filteredData = [];
|
||||
this.validFrom = null;
|
||||
this.validTill = null;
|
||||
this.productGroup = '';
|
||||
}
|
||||
|
||||
connect(): Observable<Recipe[]> {
|
||||
const dataMutations: (
|
||||
| Observable<Recipe[]>
|
||||
| Observable<Date | null>
|
||||
| Observable<string>
|
||||
| EventEmitter<PageEvent>
|
||||
| EventEmitter<Sort>
|
||||
)[] = [
|
||||
this.dataObs.pipe(
|
||||
tap((x) => {
|
||||
this.data = x;
|
||||
}),
|
||||
),
|
||||
this.validFromFilter.pipe(
|
||||
tap((x) => {
|
||||
this.validFrom = x;
|
||||
}),
|
||||
),
|
||||
this.validTillFilter.pipe(
|
||||
tap((x) => {
|
||||
this.validTill = x;
|
||||
}),
|
||||
),
|
||||
this.productGroupFilter.pipe(
|
||||
tap((x) => {
|
||||
this.productGroup = x;
|
||||
}),
|
||||
),
|
||||
];
|
||||
if (this.paginator) {
|
||||
dataMutations.push((this.paginator as MatPaginator).page);
|
||||
}
|
||||
if (this.sort) {
|
||||
dataMutations.push((this.sort as MatSort).sortChange);
|
||||
}
|
||||
|
||||
return merge(...dataMutations).pipe(
|
||||
map(() => this.getFilteredData(this.data, this.productGroup, this.validFrom, this.validTill)),
|
||||
tap((x: Recipe[]) => {
|
||||
if (this.paginator) {
|
||||
this.paginator.length = x.length;
|
||||
}
|
||||
}),
|
||||
tap((x) => {
|
||||
this.filteredData = x;
|
||||
}),
|
||||
map(() => this.getPagedData(this.getSortedData([...this.filteredData]))),
|
||||
);
|
||||
}
|
||||
|
||||
disconnect() {}
|
||||
|
||||
private getFilteredData(
|
||||
data: Recipe[],
|
||||
productGroup: string,
|
||||
validFrom: Date | null,
|
||||
validTill: Date | null,
|
||||
): Recipe[] {
|
||||
return data
|
||||
.filter((x: Recipe) => productGroup === '' || x.notes === productGroup)
|
||||
.filter((x) => validFrom === null || validFrom <= moment(x.validFrom, 'DD-MMM-YYYY').toDate())
|
||||
.filter(
|
||||
(x) => validTill === null || validTill >= moment(x.validTill, 'DD-MMM-YYYY').toDate(),
|
||||
);
|
||||
}
|
||||
|
||||
private getPagedData(data: Recipe[]) {
|
||||
if (this.paginator === undefined) {
|
||||
return data;
|
||||
}
|
||||
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
|
||||
return data.splice(startIndex, this.paginator.pageSize);
|
||||
}
|
||||
|
||||
private getSortedData(data: Recipe[]) {
|
||||
if (this.sort === undefined) {
|
||||
return data;
|
||||
}
|
||||
if (!this.sort.active || this.sort.direction === '') {
|
||||
return data;
|
||||
}
|
||||
|
||||
const sort = this.sort as MatSort;
|
||||
return data.sort((a, b) => {
|
||||
const isAsc = sort.direction === 'asc';
|
||||
switch (sort.active) {
|
||||
case 'name':
|
||||
return compare(a.sku.name, b.sku.name, isAsc);
|
||||
case 'validity':
|
||||
if (isAsc) {
|
||||
return compareDate(a.validFrom, b.validFrom, isAsc);
|
||||
} else {
|
||||
return compareDate(a.validTill, b.validTill, isAsc);
|
||||
}
|
||||
case 'salePrice':
|
||||
return compare(a.salePrice, b.salePrice, isAsc);
|
||||
case 'costPrice':
|
||||
return compare(a.costPrice, b.costPrice, isAsc);
|
||||
case 'costPercentage':
|
||||
return compare(a.costPrice / a.salePrice, b.costPrice / b.salePrice, isAsc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
|
||||
const compare = (a: string | number | Date, b: string | number | Date, isAsc: boolean) =>
|
||||
(a < b ? -1 : 1) * (isAsc ? 1 : -1);
|
||||
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
|
||||
const compareDate = (a: string, b: string, isAsc: boolean) =>
|
||||
(moment(a, 'DD-MMM-YYYY').toDate() < moment(b, 'DD-MMM-YYYY').toDate() ? -1 : 1) *
|
||||
(isAsc ? 1 : -1);
|
||||
@@ -0,0 +1,105 @@
|
||||
<mat-card>
|
||||
<mat-card-title-group>
|
||||
<mat-card-title>Recipes</mat-card-title>
|
||||
<a mat-button [routerLink]="['/recipes', 'new']">
|
||||
<mat-icon>add_box</mat-icon>
|
||||
Add
|
||||
</a>
|
||||
</mat-card-title-group>
|
||||
<mat-card-content>
|
||||
<form [formGroup]="form" fxLayout="column">
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<input
|
||||
matInput
|
||||
[matDatepicker]="validFrom"
|
||||
placeholder="Valid From"
|
||||
formControlName="validFrom"
|
||||
autocomplete="off"
|
||||
(dateChange)="filterValidFrom($event.value)"
|
||||
/>
|
||||
<mat-datepicker-toggle matSuffix [for]="validFrom"></mat-datepicker-toggle>
|
||||
<mat-datepicker #validFrom></mat-datepicker>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex>
|
||||
<input
|
||||
matInput
|
||||
[matDatepicker]="validTill"
|
||||
placeholder="Valid Till"
|
||||
formControlName="validTill"
|
||||
autocomplete="off"
|
||||
(dateChange)="filterValidTill($event.value)"
|
||||
/>
|
||||
<mat-datepicker-toggle matSuffix [for]="validTill"></mat-datepicker-toggle>
|
||||
<mat-datepicker #validTill></mat-datepicker>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Product Type</mat-label>
|
||||
<mat-select
|
||||
placeholder="Product Category"
|
||||
formControlName="productGroup"
|
||||
(selectionChange)="filterProductGroup($event.value)"
|
||||
>
|
||||
<mat-option>-- All Products --</mat-option>
|
||||
<mat-option *ngFor="let mc of productGroups" [value]="mc.id">
|
||||
{{ mc.name }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
</form>
|
||||
<mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
|
||||
<!-- Name Column -->
|
||||
<ng-container matColumnDef="name">
|
||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row"
|
||||
><a [routerLink]="['/recipes', row.id]">{{ row.sku.name }}</a></mat-cell
|
||||
>
|
||||
</ng-container>
|
||||
|
||||
<!-- Validity Column -->
|
||||
<ng-container matColumnDef="validity">
|
||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Validity</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{ row.validFrom }} - {{ row.validTill }}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Sale Price Column -->
|
||||
<ng-container matColumnDef="salePrice">
|
||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Sale Price</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{ row.salePrice | currency: 'INR' }}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Cost Price Column -->
|
||||
<ng-container matColumnDef="costPrice">
|
||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Cost Price</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{ row.costPrice | currency: 'INR' }}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<!-- Cost Percentage Column -->
|
||||
<ng-container matColumnDef="costPercentage">
|
||||
<mat-header-cell *matHeaderCellDef mat-sort-header>Cost Price</mat-header-cell>
|
||||
<mat-cell *matCellDef="let row">{{
|
||||
row.costPrice / row.salePrice | percent: '1.2-2'
|
||||
}}</mat-cell>
|
||||
</ng-container>
|
||||
|
||||
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
|
||||
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
|
||||
</mat-table>
|
||||
|
||||
<mat-paginator
|
||||
#paginator
|
||||
[length]="0"
|
||||
[pageIndex]="0"
|
||||
[pageSize]="50"
|
||||
[pageSizeOptions]="[25, 50, 100, 250]"
|
||||
>
|
||||
</mat-paginator>
|
||||
</mat-card-content>
|
||||
</mat-card>
|
||||
@@ -0,0 +1,24 @@
|
||||
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
|
||||
import { RecipeListComponent } from './recipe-list.component';
|
||||
|
||||
describe('RoleListComponent', () => {
|
||||
let component: RecipeListComponent;
|
||||
let fixture: ComponentFixture<RecipeListComponent>;
|
||||
|
||||
beforeEach(fakeAsync(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [RouterTestingModule],
|
||||
declarations: [RecipeListComponent],
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(RecipeListComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
}));
|
||||
|
||||
it('should compile', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
import { Component, OnInit, ViewChild } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { MatPaginator } from '@angular/material/paginator';
|
||||
import { MatSort } from '@angular/material/sort';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import * as moment from 'moment';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
import { ProductGroup } from '../../core/product-group';
|
||||
import { Recipe } from '../recipe';
|
||||
|
||||
import { RecipeListDatasource } from './recipe-list-datasource';
|
||||
|
||||
@Component({
|
||||
selector: 'app-role-list',
|
||||
templateUrl: './recipe-list.component.html',
|
||||
styleUrls: ['./recipe-list.component.css'],
|
||||
})
|
||||
export class RecipeListComponent implements OnInit {
|
||||
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
|
||||
@ViewChild(MatSort, { static: true }) sort?: MatSort;
|
||||
form: FormGroup;
|
||||
productGroups: ProductGroup[] = [];
|
||||
validFromFilter: BehaviorSubject<Date | null> = new BehaviorSubject<Date | null>(null);
|
||||
validTillFilter: BehaviorSubject<Date | null> = new BehaviorSubject<Date | null>(null);
|
||||
productGroupFilter: BehaviorSubject<string> = new BehaviorSubject('');
|
||||
list: Recipe[] = [];
|
||||
data: BehaviorSubject<Recipe[]> = new BehaviorSubject<Recipe[]>([]);
|
||||
dataSource: RecipeListDatasource = new RecipeListDatasource(
|
||||
this.validFromFilter,
|
||||
this.validTillFilter,
|
||||
this.productGroupFilter,
|
||||
this.data,
|
||||
);
|
||||
|
||||
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
||||
displayedColumns = ['name', 'validity', 'salePrice', 'costPrice', 'costPercentage'];
|
||||
|
||||
constructor(private route: ActivatedRoute, private fb: FormBuilder) {
|
||||
this.form = this.fb.group({
|
||||
validFrom: '',
|
||||
validTill: '',
|
||||
productGroup: '',
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.dataSource = new RecipeListDatasource(
|
||||
this.validFromFilter,
|
||||
this.validTillFilter,
|
||||
this.productGroupFilter,
|
||||
this.data,
|
||||
this.paginator,
|
||||
this.sort,
|
||||
);
|
||||
// this.dataSource = new RecipeListDatasource(this.validFromFilter, this.validTillFilter, this.productGroupFilter, this.data);
|
||||
this.route.data.subscribe((value) => {
|
||||
const data = value as { list: Recipe[]; productGroups: ProductGroup[] };
|
||||
const vf = data.list
|
||||
.map((x) => x.validFrom)
|
||||
.reduce((p, c) => {
|
||||
const pdate = moment(p, 'DD-MMM-YYYY').toDate();
|
||||
const cdate = moment(c, 'DD-MMM-YYYY').toDate();
|
||||
return pdate < cdate ? p : c;
|
||||
});
|
||||
const vt = data.list
|
||||
.map((x) => x.validTill)
|
||||
.reduce((p, c) => {
|
||||
const pdate = moment(p, 'DD-MMM-YYYY').toDate();
|
||||
const cdate = moment(c, 'DD-MMM-YYYY').toDate();
|
||||
return pdate > cdate ? p : c;
|
||||
});
|
||||
this.productGroups = data.productGroups;
|
||||
this.form.setValue({
|
||||
validFrom: vf === null ? '' : moment(vf, 'DD-MMM-YYYY').toDate(),
|
||||
validTill: vt === null ? '' : moment(vt, 'DD-MMM-YYYY').toDate(),
|
||||
productGroup: '',
|
||||
});
|
||||
this.data.next(data.list);
|
||||
});
|
||||
}
|
||||
|
||||
filterProductGroup(val: string) {
|
||||
this.productGroupFilter.next(val || '');
|
||||
}
|
||||
|
||||
filterValidFrom(val: Date) {
|
||||
this.validFromFilter.next(val);
|
||||
}
|
||||
|
||||
filterValidTill(val: Date) {
|
||||
this.validTillFilter.next(val);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
import { inject, TestBed } from '@angular/core/testing';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
|
||||
import { RecipeResolver } from './recipe-resolver.service';
|
||||
|
||||
describe('RecipeResolver', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpClientModule, RouterTestingModule],
|
||||
providers: [RecipeResolver],
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([RecipeResolver], (service: RecipeResolver) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
|
||||
import { Recipe } from './recipe';
|
||||
import { RecipeService } from './recipe.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RecipeResolver implements Resolve<Recipe> {
|
||||
constructor(private ser: RecipeService) {}
|
||||
|
||||
resolve(route: ActivatedRouteSnapshot): Observable<Recipe> {
|
||||
const id = route.paramMap.get('id');
|
||||
return this.ser.get(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { RecipeRoutingModule } from './recipe-routing.module';
|
||||
|
||||
describe('RecipeRoutingModule', () => {
|
||||
let recipeRoutingModule: RecipeRoutingModule;
|
||||
|
||||
beforeEach(() => {
|
||||
recipeRoutingModule = new RecipeRoutingModule();
|
||||
});
|
||||
|
||||
it('should create an instance', () => {
|
||||
expect(recipeRoutingModule).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { RouterModule, Routes } from '@angular/router';
|
||||
|
||||
import { AuthGuard } from '../auth/auth-guard.service';
|
||||
import { ProductGroupListResolver } from '../product-group/product-group-list-resolver.service';
|
||||
|
||||
import { RecipeDetailComponent } from './recipe-detail/recipe-detail.component';
|
||||
import { RecipeListResolver } from './recipe-list-resolver.service';
|
||||
import { RecipeListComponent } from './recipe-list/recipe-list.component';
|
||||
import { RecipeResolver } from './recipe-resolver.service';
|
||||
|
||||
const recipeRoutes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
component: RecipeListComponent,
|
||||
canActivate: [AuthGuard],
|
||||
data: {
|
||||
permission: 'Recipes',
|
||||
},
|
||||
resolve: {
|
||||
list: RecipeListResolver,
|
||||
productGroups: ProductGroupListResolver,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'new',
|
||||
component: RecipeDetailComponent,
|
||||
canActivate: [AuthGuard],
|
||||
data: {
|
||||
permission: 'Recipes',
|
||||
},
|
||||
resolve: {
|
||||
item: RecipeResolver,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: ':id',
|
||||
component: RecipeDetailComponent,
|
||||
canActivate: [AuthGuard],
|
||||
data: {
|
||||
permission: 'Recipes',
|
||||
},
|
||||
resolve: {
|
||||
item: RecipeResolver,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule, RouterModule.forChild(recipeRoutes)],
|
||||
exports: [RouterModule],
|
||||
providers: [RecipeListResolver, RecipeResolver],
|
||||
})
|
||||
export class RecipeRoutingModule {}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { RecipeModule } from './recipe.module';
|
||||
|
||||
describe('RoleModule', () => {
|
||||
let roleModule: RecipeModule;
|
||||
|
||||
beforeEach(() => {
|
||||
roleModule = new RecipeModule();
|
||||
});
|
||||
|
||||
it('should create an instance', () => {
|
||||
expect(roleModule).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import { CdkTableModule } from '@angular/cdk/table';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { FlexLayoutModule } from '@angular/flex-layout';
|
||||
import { ReactiveFormsModule } from '@angular/forms';
|
||||
import { MomentDateAdapter } from '@angular/material-moment-adapter';
|
||||
import { MatAutocompleteModule } from '@angular/material/autocomplete';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatCardModule } from '@angular/material/card';
|
||||
import { MatCheckboxModule } from '@angular/material/checkbox';
|
||||
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';
|
||||
import { MatDatepickerModule } from '@angular/material/datepicker';
|
||||
import { MatDividerModule } from '@angular/material/divider';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatPaginatorModule } from '@angular/material/paginator';
|
||||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||||
import { MatSelectModule } from '@angular/material/select';
|
||||
import { MatSortModule } from '@angular/material/sort';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
|
||||
import { SharedModule } from '../shared/shared.module';
|
||||
|
||||
import { RecipeDetailComponent } from './recipe-detail/recipe-detail.component';
|
||||
import { RecipeListComponent } from './recipe-list/recipe-list.component';
|
||||
import { RecipeRoutingModule } from './recipe-routing.module';
|
||||
|
||||
export const MY_FORMATS = {
|
||||
parse: {
|
||||
dateInput: 'DD-MMM-YYYY',
|
||||
},
|
||||
display: {
|
||||
dateInput: 'DD-MMM-YYYY',
|
||||
monthYearLabel: 'MMM YYYY',
|
||||
dateA11yLabel: 'DD-MMM-YYYY',
|
||||
monthYearA11yLabel: 'MMM YYYY',
|
||||
},
|
||||
};
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
CdkTableModule,
|
||||
FlexLayoutModule,
|
||||
MatButtonModule,
|
||||
MatCardModule,
|
||||
MatCheckboxModule,
|
||||
MatDividerModule,
|
||||
MatIconModule,
|
||||
MatInputModule,
|
||||
MatPaginatorModule,
|
||||
MatProgressSpinnerModule,
|
||||
MatSortModule,
|
||||
MatTableModule,
|
||||
ReactiveFormsModule,
|
||||
SharedModule,
|
||||
RecipeRoutingModule,
|
||||
MatAutocompleteModule,
|
||||
MatDatepickerModule,
|
||||
MatSelectModule,
|
||||
],
|
||||
declarations: [RecipeListComponent, RecipeDetailComponent],
|
||||
providers: [
|
||||
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
|
||||
{ provide: MAT_DATE_FORMATS, useValue: MY_FORMATS },
|
||||
],
|
||||
})
|
||||
export class RecipeModule {}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
import { inject, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { RecipeService } from './recipe.service';
|
||||
|
||||
describe('ProductService', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpClientModule],
|
||||
providers: [RecipeService],
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([RecipeService], (service: RecipeService) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
|
||||
import { ErrorLoggerService } from '../core/error-logger.service';
|
||||
import { ProductSku } from '../core/product-sku';
|
||||
|
||||
import { Recipe } from './recipe';
|
||||
|
||||
const url = '/api/recipes';
|
||||
const serviceName = 'RecipeService';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class RecipeService {
|
||||
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
||||
|
||||
get(id: string | null): Observable<Recipe> {
|
||||
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
||||
return this.http
|
||||
.get<Recipe>(getUrl)
|
||||
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Recipe>;
|
||||
}
|
||||
|
||||
list(): Observable<Recipe[]> {
|
||||
return this.http
|
||||
.get<Recipe[]>(`${url}/list`)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'getList'))) as Observable<Recipe[]>;
|
||||
}
|
||||
|
||||
save(recipe: Recipe): Observable<Recipe> {
|
||||
return this.http
|
||||
.post<Recipe>(`${url}`, recipe)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Recipe>;
|
||||
}
|
||||
|
||||
update(recipe: Recipe): Observable<Recipe> {
|
||||
return this.http
|
||||
.put<Recipe>(`${url}/${recipe.id}`, recipe)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Recipe>;
|
||||
}
|
||||
|
||||
saveOrUpdate(recipe: Recipe): Observable<Recipe> {
|
||||
if (!recipe.id) {
|
||||
return this.save(recipe);
|
||||
}
|
||||
return this.update(recipe);
|
||||
}
|
||||
|
||||
delete(id: string): Observable<Recipe> {
|
||||
return this.http
|
||||
.delete<Recipe>(`${url}/${id}`)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Recipe>;
|
||||
}
|
||||
|
||||
getIngredientDetails(
|
||||
id: string,
|
||||
startDate: string | null,
|
||||
finishDate: string | null,
|
||||
): Observable<ProductSku> {
|
||||
const getUrl: string = `${url}/ingredient-details/${id}`;
|
||||
const options = {
|
||||
params: new HttpParams(),
|
||||
};
|
||||
if (startDate !== null) {
|
||||
options.params = options.params.set('s', startDate);
|
||||
}
|
||||
if (finishDate !== null) {
|
||||
options.params = options.params.set('f', finishDate);
|
||||
}
|
||||
return this.http
|
||||
.get<ProductSku>(getUrl, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, `get id=${id}`)),
|
||||
) as Observable<ProductSku>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ProductSku } from '../core/product-sku';
|
||||
|
||||
import { RecipeItem } from './recipe-item';
|
||||
|
||||
export class Recipe {
|
||||
id: string | undefined;
|
||||
sku: ProductSku;
|
||||
recipeYield: number;
|
||||
costPrice: number;
|
||||
salePrice: number;
|
||||
notes: string;
|
||||
items: RecipeItem[];
|
||||
validFrom: string;
|
||||
validTill: string;
|
||||
|
||||
public constructor(init?: Partial<Recipe>) {
|
||||
this.sku = new ProductSku();
|
||||
this.recipeYield = 0;
|
||||
this.salePrice = 0;
|
||||
this.costPrice = 0;
|
||||
this.notes = '';
|
||||
this.items = [];
|
||||
this.validFrom = '';
|
||||
this.validTill = '';
|
||||
Object.assign(this, init);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user