Moved to Angular 6.0

----

Pending
* Table width for the points column in incentive
* Linting
* keyboard navigation where it was used earlier
* can remove the unused totals calculated serverside in productledger
* spinner and loading bars
* Activate Guard for Employee Function tabs
* Progress for Fingerprint uploads
* deleted reconcile and receipe features as they were not being used
* focus the right control on component load
This commit is contained in:
tanshu
2018-05-25 19:19:00 +05:30
parent b3cb01da02
commit 6be1dd5a3a
1380 changed files with 23914 additions and 18722 deletions

View File

@ -0,0 +1,18 @@
import {DataSource} from '@angular/cdk/collections';
import {Observable} from 'rxjs';
import {Inventory, Journal} from '../journal/voucher';
export class PurchaseDataSource extends DataSource<Inventory> {
constructor(private data: Observable<Inventory[]>) {
super();
}
connect(): Observable<Inventory[]> {
return this.data;
}
disconnect() {
}
}

View File

@ -0,0 +1,37 @@
<h1 mat-dialog-title>Edit Purchase Entry</h1>
<div mat-dialog-content>
<form [formGroup]="form">
<div fxLayout="row" fxLayoutAlign="space-around start" fxLayout.lt-md="column" fxLayoutGap="20px"
fxLayoutGap.lt-md="0px">
<mat-form-field fxFlex="52">
<input type="text" matInput placeholder="Product" #productElement [matAutocomplete]="autoP"
formControlName="product" autocomplete="off">
<mat-autocomplete #autoP="matAutocomplete" autoActiveFirstOption [displayWith]="displayProductName"
(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="12">
<mat-label>Quantity</mat-label>
<input type="text" matInput placeholder="Quantity" formControlName="quantity" autocomplete="off">
</mat-form-field>
<mat-form-field fxFlex="12">
<mat-label>Price</mat-label>
<input type="text" matInput placeholder="Price" formControlName="price" autocomplete="off">
</mat-form-field>
<mat-form-field fxFlex="12">
<mat-label>Tax</mat-label>
<input type="text" matInput placeholder="Tax" formControlName="tax" autocomplete="off">
</mat-form-field>
<mat-form-field fxFlex="12">
<mat-label>Discount</mat-label>
<input type="text" matInput placeholder="Discount" formControlName="discount" autocomplete="off">
</mat-form-field>
</div>
</form>
</div>
<div mat-dialog-actions>
<button mat-button [mat-dialog-close]="false" cdkFocusInitial>Cancel</button>
<button mat-button (click)="accept()" color="primary">Ok</button>
</div>

View File

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

View File

@ -0,0 +1,84 @@
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatAutocompleteSelectedEvent, MatDialogRef} from '@angular/material';
import {debounceTime, distinctUntilChanged, map, startWith, switchMap} from 'rxjs/operators';
import {FormBuilder, FormGroup} from '@angular/forms';
import {Observable, of as observableOf} from 'rxjs';
import {Product} from '../product/product';
import {ProductService} from '../product/product.service';
@Component({
selector: 'app-purchase-dialog',
templateUrl: './purchase-dialog.component.html',
styleUrls: ['./purchase-dialog.component.css']
})
export class PurchaseDialogComponent implements OnInit {
products: Observable<Product[]>;
form: FormGroup;
product: Product;
constructor(
public dialogRef: MatDialogRef<PurchaseDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private fb: FormBuilder,
private productSer: ProductService) {
this.createForm();
this.listenToProductAutocompleteChange();
}
ngOnInit() {
this.form.setValue({
product: this.data.inventory.product,
quantity: this.data.inventory.quantity,
price: this.data.inventory.rate,
tax: this.data.inventory.tax,
discount: this.data.inventory.discount
});
this.product = this.data.inventory.product;
}
createForm() {
this.form = this.fb.group({
product: '',
quantity: '',
price: '',
tax: '',
discount: ''
});
}
listenToProductAutocompleteChange(): void {
const control = this.form.get('product');
this.products = control.valueChanges
.pipe(
startWith(null),
map(x => (x !== null && x.length >= 1) ? x : null),
debounceTime(150),
distinctUntilChanged(),
switchMap(x => (x === null) ? observableOf([]) : this.productSer.autocomplete(x))
);
}
displayProductName(product?: Product): string | undefined {
return product ? product.name : undefined;
}
productSelected(event: MatAutocompleteSelectedEvent): void {
this.product = event.option.value;
this.form.get('price').setValue(this.product.price);
}
accept(): void {
const formValue = this.form.value;
const quantity = +(formValue.quantity);
const price = +(formValue.price);
const tax = +(formValue.tax);
const discount = +(formValue.discount);
this.data.inventory.product = this.product;
this.data.inventory.quantity = quantity;
this.data.inventory.rate = price;
this.data.inventory.tax = tax;
this.data.inventory.discount = discount;
this.data.inventory.amount = quantity * price * (1 + tax) * (1 - discount);
this.dialogRef.close(this.data.inventory);
}
}

View File

@ -0,0 +1,15 @@
import {inject, TestBed} from '@angular/core/testing';
import {PurchaseResolver} from './purchase-resolver.service';
describe('PurchaseResolver', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [PurchaseResolver]
});
});
it('should be created', inject([PurchaseResolver], (service: PurchaseResolver) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,23 @@
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/internal/Observable';
import {Voucher} from '../journal/voucher';
import {VoucherService} from '../journal/voucher.service';
@Injectable({
providedIn: 'root'
})
export class PurchaseResolver implements Resolve<Voucher> {
constructor(private ser: VoucherService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Voucher> {
const id = route.paramMap.get('id');
if (id === null) {
return this.ser.getOfType('Purchase');
} else {
return this.ser.get(id);
}
}
}

View File

@ -0,0 +1,13 @@
import {PurchaseRoutingModule} from './purchase-routing.module';
describe('PurchaseRoutingModule', () => {
let purchaseRoutingModule: PurchaseRoutingModule;
beforeEach(() => {
purchaseRoutingModule = new PurchaseRoutingModule();
});
it('should create an instance', () => {
expect(purchaseRoutingModule).toBeTruthy();
});
});

View File

@ -0,0 +1,49 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule, Routes} from '@angular/router';
import {PurchaseResolver} from './purchase-resolver.service';
import {AuthGuard} from '../auth/auth-guard.service';
import {PurchaseComponent} from './purchase.component';
const purchaseRoutes: Routes = [
{
path: 'Purchase',
component: PurchaseComponent,
canActivate: [AuthGuard],
data: {
permission: 'Purchase'
},
resolve: {
voucher: PurchaseResolver
},
runGuardsAndResolvers: 'always'
},
{
path: 'Purchase/:id',
component: PurchaseComponent,
canActivate: [AuthGuard],
data: {
permission: 'Purchase'
},
resolve: {
voucher: PurchaseResolver
},
runGuardsAndResolvers: 'always'
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(purchaseRoutes)
],
exports: [
RouterModule
],
providers: [
PurchaseResolver
]
})
export class PurchaseRoutingModule {
}

View File

@ -0,0 +1,32 @@
.right {
display: flex;
justify-content: flex-end;
}
.selected {
background: #fff3cd
}
.unposted {
background: #f8d7da
}
.center {
display: flex;
justify-content: center;
}
.img-container {
position: relative;
}
.img-container .overlay {
display: none;
}
.img-container:hover > .overlay {
display: inline-block;
position: absolute;
left: 60px;
top: 0;
}

View File

@ -0,0 +1,149 @@
<mat-card>
<mat-card-title-group>
<mat-card-title>Purchase</mat-card-title>
</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="40">
<input matInput [matDatepicker]="date" (focus)="date.open()" placeholder="Date" formControlName="date"
autocomplete="off">
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date></mat-datepicker>
</mat-form-field>
<mat-form-field fxFlex="40">
<input type="text" matInput placeholder="Account" #accountElement [matAutocomplete]="autoA"
formControlName="account" autocomplete="off">
<mat-hint *ngIf="accBal">
Balance as on Date: <strong>{{accBal.date | currency:'INR' | accounting}}</strong> /
Final balance: <strong>{{accBal.total | currency:'INR' | accounting}}</strong>
</mat-hint>
<mat-autocomplete #autoA="matAutocomplete" autoActiveFirstOption [displayWith]="displayAccount"
(optionSelected)="accountSelected($event)">
<mat-option *ngFor="let account of accounts | async" [value]="account">{{account.name}}</mat-option>
</mat-autocomplete>
</mat-form-field>
<mat-form-field fxFlex="20">
<mat-label>Amount</mat-label>
<span matPrefix></span>
<input type="text" matInput formControlName="amount">
</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="Product" #productElement [matAutocomplete]="autoP"
formControlName="product" autocomplete="off">
<mat-autocomplete #autoP="matAutocomplete" autoActiveFirstOption [displayWith]="displayProductName"
(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>Quantity</mat-label>
<input type="text" matInput placeholder="Quantity" formControlName="quantity" autocomplete="off">
</mat-form-field>
<mat-form-field fxFlex="10">
<mat-label>Price</mat-label>
<input type="text" matInput placeholder="Price" formControlName="price" autocomplete="off">
</mat-form-field>
<mat-form-field fxFlex="10">
<mat-label>Tax</mat-label>
<input type="text" matInput placeholder="Tax" formControlName="tax" autocomplete="off">
</mat-form-field>
<mat-form-field fxFlex="10">
<mat-label>Discount</mat-label>
<input type="text" matInput placeholder="Discount" formControlName="discount" autocomplete="off">
</mat-form-field>
<button mat-raised-button color="primary" (click)="addRow()" fxFlex="10">Add</button>
</div>
<mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
<!-- Product 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'}}</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.rate | currency:'INR'}}</mat-cell>
</ng-container>
<!-- Tax Column -->
<ng-container matColumnDef="tax">
<mat-header-cell *matHeaderCellDef class="right">Tax</mat-header-cell>
<mat-cell *matCellDef="let row" class="right">{{row.tax | percent:'1.2-2'}}</mat-cell>
</ng-container>
<!-- Discount Column -->
<ng-container matColumnDef="discount">
<mat-header-cell *matHeaderCellDef class="right">Discount</mat-header-cell>
<mat-cell *matCellDef="let row" class="right">{{row.discount | percent:'1.2-2'}}</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.amount | 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 (click)="editRow(row)">
<mat-icon>edit</mat-icon>
</button>
<button mat-icon-button 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>
<mat-form-field>
<mat-label>Narration</mat-label>
<textarea matInput placeholder="Narration" formControlName="narration"></textarea>
</mat-form-field>
<div fxLayout="row" fxLayoutGap="0.5%" fxLayoutAlign="center">
<div class="img-container" fxFlex="20%" *ngFor="let item of voucher.files">
<img [src]="item.thumbnail" (click)="zoomImage(item)">
<button mat-icon-button class="overlay" (click)="deleteImage(item)">
<mat-icon>delete</mat-icon>
</button>
</div>
</div>
<input type="file" multiple accept="image/*" (change)="detectFiles($event)">
</form>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary" (click)="save()" [disabled]="!canSave()">
{{(voucher.id) ? 'Update' : 'Save'}}
</button>
<button mat-raised-button (click)="post()" *ngIf="voucher.id"
[disabled]="voucher.posted || auth.user.perms.indexOf('Post Vouchers') === -1">
{{(voucher.posted) ? 'Posted' : 'Post'}}
</button>
<button mat-raised-button color="warn" (click)="confirmDelete()" *ngIf="voucher.id" [disabled]="!canSave()">
Delete
</button>
</mat-card-actions>
<mat-card-subtitle *ngIf="voucher.id">
Created on <strong>{{voucher.creationDate | localTime}}</strong> and
Last Edited on <strong>{{voucher.lastEditDate | localTime}}</strong>
by <strong>{{voucher.user.name}}</strong>. {{(voucher.poster) ? 'Posted by ' + voucher.poster : ''}}
</mat-card-subtitle>
</mat-card>

View File

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

View File

@ -0,0 +1,353 @@
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MatAutocompleteSelectedEvent, MatDialog} from '@angular/material';
import {ActivatedRoute, Router} from '@angular/router';
import {BehaviorSubject, fromEvent, Observable, of, of as observableOf, zip} from 'rxjs';
import {PurchaseDataSource} from './purchase-datasource';
import {Account} from '../account/account';
import {VoucherService} from '../journal/voucher.service';
import {AccountService} from '../account/account.service';
import {DbFile, Inventory, Journal, Voucher} from '../journal/voucher';
import * as moment from 'moment';
import {AuthService} from '../auth/auth.service';
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
import {ToasterService} from '../core/toaster.service';
import {debounceTime, distinctUntilChanged, map, startWith, switchMap} from 'rxjs/operators';
import {PurchaseDialogComponent} from './purchase-dialog.component';
import {ImageDialogComponent} from '../shared/image-dialog/image-dialog.component';
import {ProductService} from '../product/product.service';
import {Product} from '../product/product';
@Component({
selector: 'app-purchase',
templateUrl: './purchase.component.html',
styleUrls: ['./purchase.component.css']
})
export class PurchaseComponent implements OnInit, AfterViewInit {
@ViewChild('accountElement') accountElement: ElementRef;
@ViewChild('productElement') productElement: ElementRef;
public inventoryObservable = new BehaviorSubject<Inventory[]>([]);
dataSource: PurchaseDataSource;
form: FormGroup;
purchaseJournal: Journal;
voucher: Voucher;
account: Account;
product: Product;
accBal: any;
displayedColumns = ['product', 'quantity', 'rate', 'tax', 'discount', 'amount', 'action'];
accounts: Observable<Account[]>;
products: Observable<Product[]>;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private dialog: MatDialog,
private toaster: ToasterService,
private auth: AuthService,
private ser: VoucherService,
private productSer: ProductService,
private accountSer: AccountService
) {
this.createForm();
this.listenToAccountAutocompleteChange();
this.listenToProductAutocompleteChange();
}
ngOnInit() {
this.route.data
.subscribe((data: { voucher: Voucher }) => {
this.loadVoucher(data.voucher);
});
}
ngAfterViewInit() {
this.focusAccount();
}
loadVoucher(voucher) {
this.voucher = voucher;
this.purchaseJournal = this.voucher.journals.filter(x => x.debit === -1)[0];
this.form.setValue({
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
account: this.purchaseJournal.account,
amount: this.purchaseJournal.amount,
addRow: {
product: '',
quantity: '',
price: '',
tax: '',
discount: ''
},
narration: this.voucher.narration
});
this.dataSource = new PurchaseDataSource(this.inventoryObservable);
this.updateView();
}
focusAccount() {
setTimeout(() => {
this.accountElement.nativeElement.focus();
}, 0);
}
addRow() {
const formValue = this.form.get('addRow').value;
const quantity = +(formValue.quantity);
const price = +(formValue.price);
const tax = +(formValue.tax);
const discount = +(formValue.discount);
if (this.product === null || quantity <= 0 || price <= 0) {
return;
}
const oldFiltered = this.voucher.inventories.filter((x) => x.product.id === this.product.id);
if (oldFiltered.length) {
this.toaster.show('Danger', 'Product already added');
return;
}
this.voucher.inventories.push({
id: null,
quantity: quantity,
rate: price,
tax: tax,
discount: discount,
amount: quantity * price * (1 + tax) * (1 - discount),
product: this.product,
batch: null
});
this.resetAddRow();
this.updateView();
}
resetAddRow() {
this.form.get('addRow').reset({
product: null,
quantity: '',
price: '',
tax: '',
discount: ''
});
this.product = null;
setTimeout(() => {
this.productElement.nativeElement.focus();
}, 0);
}
updateView() {
this.inventoryObservable.next(this.voucher.inventories);
this.purchaseJournal.amount = Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0));
this.form.get('amount').setValue(this.purchaseJournal.amount);
}
editRow(row: Inventory) {
const dialogRef = this.dialog.open(PurchaseDialogComponent, {
width: '750px',
data: {inventory: Object.assign({}, row)}
});
dialogRef.afterClosed().subscribe((result: boolean | Inventory) => {
if (!result) {
return;
}
const j = result as Inventory;
if (j.product.id !== row.product.id && this.voucher.inventories.filter((x) => x.product.id === j.product.id).length) {
return;
}
Object.assign(row, j);
this.updateView();
});
}
deleteRow(row: Inventory) {
this.voucher.inventories.splice(this.voucher.inventories.indexOf(row), 1);
this.updateView();
}
createForm() {
this.form = this.fb.group({
date: '',
account: '',
amount: {value: '', disabled: true},
addRow: this.fb.group({
product: '',
quantity: '',
price: '',
tax: '',
discount: ''
}),
narration: ''
});
this.accBal = null;
}
canSave() {
if (!this.voucher.id) {
return true;
} else if (this.voucher.posted && this.auth.user.perms.indexOf('Edit Posted Vouchers') !== -1) {
return true;
} else {
return this.voucher.user.id === this.auth.user.id || this.auth.user.perms.indexOf('Edit Other User\'s Vouchers') !== -1;
}
}
post() {
this.ser.post(this.voucher.id)
.subscribe(
(result) => {
this.loadVoucher(result);
this.toaster.show('Success', 'Voucher Posted');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
save() {
this.ser.save(this.getVoucher())
.subscribe(
(result) => {
this.loadVoucher(result);
this.toaster.show('Success', '');
this.router.navigate(['/Purchase', result.id]);
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
getVoucher(): Voucher {
const formModel = this.form.value;
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
this.purchaseJournal.account = formModel.account;
this.voucher.narration = formModel.narration;
return this.voucher;
}
delete() {
this.ser.delete(this.voucher.id)
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigate(['/Purchase'], {replaceUrl: true});
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.'}
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
listenToAccountAutocompleteChange(): void {
const control = this.form.get('account');
this.accounts = control.valueChanges
.pipe(
startWith(null),
map(x => (x !== null && x.length >= 1) ? x : null),
debounceTime(150),
distinctUntilChanged(),
switchMap(x => (x === null) ? observableOf([]) : this.accountSer.autocomplete(x))
);
}
listenToProductAutocompleteChange(): void {
const control = this.form.get('addRow').get('product');
this.products = control.valueChanges
.pipe(
startWith(null),
map(x => (x !== null && x.length >= 1) ? x : null),
debounceTime(150),
distinctUntilChanged(),
switchMap(x => (x === null) ? observableOf([]) : this.productSer.autocomplete(x))
);
}
displayAccount(account?: Account): string | undefined {
return account ? account.name : undefined;
}
accountSelected(event: MatAutocompleteSelectedEvent): void {
this.account = event.option.value;
}
displayProductName(product?: Product): string | undefined {
return product ? product.name : undefined;
}
productSelected(event: MatAutocompleteSelectedEvent): void {
this.product = event.option.value;
this.form.get('addRow').get('price').setValue(this.product.price);
}
zoomImage(file: DbFile) {
this.dialog.open(ImageDialogComponent, {
width: '750px',
data: file.resized
});
}
deleteImage(file: DbFile) {
const index = this.voucher.files.indexOf(file);
this.voucher.files.splice(index, 1);
}
detectFiles(event) {
const files = event.target.files;
if (files) {
for (const file of files) {
const reader = new FileReader();
reader.onload = (e: any) => {
zip(of(e.target.result),
this.resizeImage(e.target.result, 100, 150),
this.resizeImage(e.target.result, 825, 1170)
).subscribe(
val => this.voucher.files.push({id: null, thumbnail: val[1], resized: val[2]})
);
};
reader.readAsDataURL(file);
}
}
}
resizeImage(image, MAX_WIDTH, MAX_HEIGHT) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
const ex = fromEvent(img, 'load').pipe(
map((e) => {
let width = img.naturalWidth,
height = img.naturalHeight;
const ratio = Math.min(1, MAX_WIDTH / width, MAX_HEIGHT / height);
if (ratio === 1) {
return image;
}
width *= ratio;
height *= ratio;
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
return canvas.toDataURL('image/jpeg', 0.95);
})
);
img.src = image;
return ex;
}
}

View File

@ -0,0 +1,13 @@
import {PurchaseModule} from './purchase.module';
describe('PurchaseModule', () => {
let purchaseModule: PurchaseModule;
beforeEach(() => {
purchaseModule = new PurchaseModule();
});
it('should create an instance', () => {
expect(purchaseModule).toBeTruthy();
});
});

View File

@ -0,0 +1,84 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {
DateAdapter,
MAT_DATE_FORMATS,
MAT_DATE_LOCALE,
MatAutocompleteModule,
MatButtonModule,
MatCardModule,
MatCheckboxModule,
MatDatepickerModule,
MatDialogModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatNativeDateModule,
MatPaginatorModule,
MatProgressSpinnerModule,
MatSelectModule,
MatSortModule,
MatTableModule
} from '@angular/material';
import {SharedModule} from '../shared/shared.module';
import {ReactiveFormsModule} from '@angular/forms';
import {CdkTableModule} from '@angular/cdk/table';
import {PurchaseRoutingModule} from './purchase-routing.module';
import {PurchaseComponent} from './purchase.component';
import {MomentDateAdapter} from '@angular/material-moment-adapter';
import {A11yModule} from '@angular/cdk/a11y';
import {PurchaseDialogComponent} from './purchase-dialog.component';
import {FlexLayoutModule, FlexModule} from '@angular/flex-layout';
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: [
A11yModule,
CommonModule,
CdkTableModule,
FlexModule,
FlexLayoutModule,
MatAutocompleteModule,
MatButtonModule,
MatCardModule,
MatCheckboxModule,
MatDatepickerModule,
MatDialogModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatNativeDateModule,
MatPaginatorModule,
MatProgressSpinnerModule,
MatSelectModule,
MatSortModule,
MatTableModule,
ReactiveFormsModule,
SharedModule,
PurchaseRoutingModule
],
declarations: [
PurchaseComponent,
PurchaseDialogComponent
],
providers: [
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
entryComponents: [
PurchaseDialogComponent
]
})
export class PurchaseModule {
}

View File

@ -0,0 +1,15 @@
import {inject, TestBed} from '@angular/core/testing';
import {VoucherService} from './purchase.service';
describe('PurchaseService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [VoucherService]
});
});
it('should be created', inject([VoucherService], (service: VoucherService) => {
expect(service).toBeTruthy();
}));
});