161 lines
5.2 KiB
TypeScript
161 lines
5.2 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { Transaction } from './transaction';
|
|
import { UserService } from '../../services/user.service';
|
|
import { Contribution } from './contribution';
|
|
import { TransactionService } from '../../services/transaction.service';
|
|
import { MatDialog } from '@angular/material';
|
|
import { AddTransactionPopupComponent, ValDisplay } from './add-transaction-popup/add-transaction-popup.component';
|
|
import { TransactionType } from './transaction-type';
|
|
import { Fund } from './fund';
|
|
import { OkPopupComponent } from '../popups/ok-popup/ok-popup.component';
|
|
|
|
@Component({
|
|
selector: 'app-add-transaction-page',
|
|
templateUrl: './add-transaction-page.component.html',
|
|
styleUrls: ['./add-transaction-page.component.css']
|
|
})
|
|
export class AddTransactionPageComponent implements OnInit {
|
|
|
|
errorMessages = [];
|
|
|
|
contributions: Contribution[] = [];
|
|
|
|
submitButtonDisabled: boolean = false;
|
|
submitButtonText: string = "Submit Transactions";
|
|
transactionType = TransactionType;
|
|
fund = Fund;
|
|
transactionTypes: ValDisplay[] = [];
|
|
funds: ValDisplay[] = [];
|
|
contributors: {value:number,display:string}[] = [];
|
|
|
|
|
|
constructor(private dialogService: MatDialog, private userService: UserService, private transactionService: TransactionService) {
|
|
for(let m in TransactionType) {
|
|
if (typeof TransactionType[m] === 'number') {
|
|
this.transactionTypes.push({value:<any>TransactionType[m], display: m});
|
|
}
|
|
}
|
|
for(let m in Fund) {
|
|
if (typeof Fund[m] === 'number') {
|
|
this.funds.push({value:<any>Fund[m], display: m});
|
|
}
|
|
}
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.userService.getAll().subscribe(res => this.contributors = (<any>res).users);
|
|
}
|
|
|
|
|
|
|
|
|
|
addTransaction(contributorId: number) {
|
|
const contrib = this.contributors.find(c => c.value === contributorId);
|
|
const contributor = this.contributions.find(c => c.contributorId === contributorId);
|
|
const lastTransaction = contributor && contributor.transactions.length > 0 ? contributor.transactions[contributor.transactions.length-1] : null;
|
|
|
|
const ref = this.dialogService.open(AddTransactionPopupComponent, {
|
|
data: {
|
|
contributors: this.contributors,
|
|
funds: this.funds,
|
|
types: this.transactionTypes,
|
|
contributor: contrib ? contrib : undefined,
|
|
typeId: lastTransaction ? lastTransaction.typeId : TransactionType.Cash,
|
|
fundId: lastTransaction ? (lastTransaction.fundId === Fund.General ? Fund.Missions : Fund.General) : Fund.General,
|
|
checkNumber: lastTransaction ? lastTransaction.checkNumber : ''
|
|
}
|
|
});
|
|
|
|
ref.afterClosed().subscribe((res: {transaction:Transaction,saveAndAdd:boolean}) => {
|
|
if (res) {
|
|
let contrib = this.contributions.find(c => c.contributorId === res.transaction.contributorId);
|
|
if (!contrib) {
|
|
contrib = new Contribution();
|
|
contrib.contributorId = res.transaction.contributorId;
|
|
contrib.date = res.transaction.date;
|
|
contrib.transactions = [];
|
|
this.contributions.push(contrib);
|
|
}
|
|
contrib.transactions.push(res.transaction);
|
|
if (res.saveAndAdd === true) {
|
|
this.addTransaction(contrib.contributorId);
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
deleteContributor(contributorId: number) {
|
|
const contrib = this.contributions.findIndex(c => c.contributorId === contributorId);
|
|
if (contrib >= 0) {
|
|
this.contributions.splice(contrib, 1);
|
|
}
|
|
}
|
|
|
|
deleteTransaction(contributorId: number, transactionIndex: number) {
|
|
const contrib = this.contributions.find(c => c.contributorId === contributorId);
|
|
if (contrib) {
|
|
contrib.transactions.splice(transactionIndex, 1);
|
|
}
|
|
if (contrib.transactions.length === 0) {
|
|
this.deleteContributor(contributorId);
|
|
}
|
|
}
|
|
|
|
contributorName(id: number) {
|
|
const contributor = this.contributors.find(c => c.value === id);
|
|
return contributor ? contributor.display : undefined;
|
|
}
|
|
|
|
contributorTotal(contributorId: number, fundId: number) {
|
|
const contrib = this.contributions.find(c => c.contributorId === contributorId);
|
|
if (!contrib) return 0;
|
|
|
|
var sum = 0;
|
|
contrib.transactions.forEach(e => {
|
|
if (e.fundId === fundId || fundId === 0) {
|
|
sum += e.amount;
|
|
}
|
|
});
|
|
return sum;
|
|
}
|
|
|
|
combinedTotal(fundId: number) {
|
|
var sum = 0;
|
|
this.contributions.forEach(c => {
|
|
c.transactions.forEach(t => {
|
|
if (t.fundId === fundId || fundId === 0) {
|
|
sum += +t.amount;
|
|
}
|
|
});
|
|
});
|
|
return sum;
|
|
}
|
|
|
|
submit() {
|
|
this.submitButtonText = 'Submitting...';
|
|
this.submitButtonDisabled = true;
|
|
|
|
const transactions: Transaction[] = [];
|
|
for(let c = 0; c < this.contributions.length; c++) {
|
|
for(let t = 0; t < this.contributions[c].transactions.length; t++) {
|
|
transactions.push(this.contributions[c].transactions[t]);
|
|
}
|
|
}
|
|
|
|
this.transactionService.createFromArray(transactions).subscribe(x =>{
|
|
this.submitButtonText = 'Submit Transactions';
|
|
this.submitButtonDisabled = false;
|
|
this.contributions = [];
|
|
}, e => {
|
|
this.dialogService.open(OkPopupComponent,{
|
|
data: {
|
|
title: 'Error Saving Transactions',
|
|
message: e.toString()
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
}
|