refractor: accounting module (#300)

* Refractor components.
* Add resolvers.
* Add relative router links.
* Replace mat-icons by font-awesome icons.
* Add comments.

Fixes: #298
This commit is contained in:
Abhay Chawla
2018-08-19 22:49:48 +05:30
committed by GitHub
parent 598f239251
commit 6e552d72eb
114 changed files with 3760 additions and 1432 deletions
@@ -1,131 +1,178 @@
<mat-card class="container">
<div class="container">
<form [formGroup]="journalEntryForm" (ngSubmit)="submit()">
<mat-card>
<mat-card-content>
<form [formGroup]="journalEntryForm" (ngSubmit)="submit()">
<div fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column">
<mat-card-content>
<mat-form-field fxFlex="48%">
<mat-select placeholder="Office" required formControlName="officeId">
<mat-option *ngFor="let office of officeData" [value]="office.id">
{{ office.name }}
</mat-option>
</mat-select>
</mat-form-field>
<div fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column">
<mat-form-field fxFlex="48%">
<mat-select placeholder="Currency" required formControlName="currencyCode">
<mat-option *ngFor="let currency of currencyData" [value]="currency.code">
{{ currency.displayLabel }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Office</mat-label>
<mat-select required formControlName="officeId">
<mat-option *ngFor="let office of officeData" [value]="office.id">
{{ office.name }}
</mat-option>
</mat-select>
<mat-error *ngIf="journalEntryForm.controls.officeId.hasError('required')">
Office is <strong>required</strong>
</mat-error>
</mat-form-field>
<div fxFlexFill fxLayout="row wrap" fxLayout.lt-md="column" formArrayName="debits"
*ngFor="let debit of getFormArrayData('debits').controls; let i = index;">
<mat-form-field fxFlex="48%">
<mat-label>Currency</mat-label>
<mat-select required formControlName="currencyCode">
<mat-option *ngFor="let currency of currencyData" [value]="currency.code">
{{ currency.displayLabel }}
</mat-option>
</mat-select>
<mat-error *ngIf="journalEntryForm.controls.currencyCode.hasError('required')">
Currency is <strong>required</strong>
</mat-error>
</mat-form-field>
<div fxFlexFill fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column" [formGroupName]="i">
<div fxFlexFill fxLayout="row wrap" fxLayout.lt-md="column" formArrayName="debits"
*ngFor="let debit of debits.controls; let i = index;">
<mat-form-field fxFlex="48%">
<mat-select placeholder="Affected GL Entry (Debit)" required formControlName="glAccountId">
<mat-option *ngFor="let affectedGLEntryDebit of glAccountData" [value]="affectedGLEntryDebit.id">
{{ affectedGLEntryDebit.name + ' (' + affectedGLEntryDebit.glCode + ')' }}
</mat-option>
</mat-select>
</mat-form-field>
<div fxFlexFill fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column" [formGroupName]="i">
<mat-form-field fxFlex="43%">
<input type="number" matInput placeholder="Debit Amount" required formControlName="amount">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Affected GL Entry (Debit)</mat-label>
<mat-select required formControlName="glAccountId">
<mat-option *ngFor="let affectedGLEntryDebit of glAccountData" [value]="affectedGLEntryDebit.id">
{{ affectedGLEntryDebit.name + ' (' + affectedGLEntryDebit.glCode + ')' }}
</mat-option>
</mat-select>
<mat-error *ngIf="debits.at(i).controls.glAccountId.hasError('required')">
Affected GL Entry (Debit) is <strong>required</strong>
</mat-error>
</mat-form-field>
<span fxFlex>
<button *ngIf="i !== 0" type="button" mat-icon-button (click)="removeAffectedGLEntry(journalEntryForm.get('debits'), i)"><mat-icon>remove_circle</mat-icon></button>
<button *ngIf="i === 0" type="button" mat-icon-button color="primary" (click)="addAffectedGLEntry(journalEntryForm.get('debits'))"><mat-icon>add_circle</mat-icon></button>
</span>
<mat-form-field fxFlex="43%">
<mat-label>Debit Amount</mat-label>
<input type="number" matInput required formControlName="amount">
<mat-error *ngIf="debits.at(i).controls.amount.hasError('required')">
Debit Amount is <strong>required</strong>
</mat-error>
</mat-form-field>
<span fxFlex>
<button *ngIf="i !== 0" type="button" mat-icon-button (click)="removeAffectedGLEntry(debits, i)">
<fa-icon icon="minus-circle" size="lg"></fa-icon>
</button>
<button *ngIf="i === 0" type="button" mat-icon-button color="primary" (click)="addAffectedGLEntry(debits)">
<fa-icon icon="plus-circle" size="lg"></fa-icon>
</button>
</span>
</div>
</div>
</div>
<div fxFlexFill fxLayout="row wrap" fxLayout.lt-md="column" formArrayName="credits"
*ngFor="let credit of credits.controls; let i = index;">
<div fxFlexFill fxLayout="row wrap" fxLayout.lt-md="column" formArrayName="credits"
*ngFor="let credit of getFormArrayData('credits').controls; let i = index;">
<div fxFlexFill fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column" [formGroupName]="i">
<div fxFlexFill fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column" [formGroupName]="i">
<mat-form-field fxFlex="48%">
<mat-label>Affected GL Entry (Credit)</mat-label>
<mat-select required formControlName="glAccountId">
<mat-option *ngFor="let affectedGLEntryCredit of glAccountData" [value]="affectedGLEntryCredit.id">
{{ affectedGLEntryCredit.name + ' (' + affectedGLEntryCredit.glCode + ')' }}
</mat-option>
</mat-select>
<mat-error *ngIf="credits.at(i).controls.glAccountId.hasError('required')">
Affected GL Entry (Credit) is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-select placeholder="Affected GL Entry (Credit)" required formControlName="glAccountId">
<mat-option *ngFor="let affectedGLEntryCredit of glAccountData" [value]="affectedGLEntryCredit.id">
{{ affectedGLEntryCredit.name + ' (' + affectedGLEntryCredit.glCode + ')' }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="43%">
<mat-label>Credit Amount</mat-label>
<input type="number" matInput required formControlName="amount">
<mat-error *ngIf="credits.at(i).controls.amount.hasError('required')">
Credit Amount is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="43%">
<input type="number" matInput placeholder="Credit Amount" required formControlName="amount">
</mat-form-field>
<span fxFlex>
<button *ngIf="i !== 0" type="button" mat-icon-button (click)="removeAffectedGLEntry(credits, i)">
<fa-icon icon="minus-circle" size="lg"></fa-icon>
</button>
<button *ngIf="i === 0" type="button" mat-icon-button color="primary" (click)="addAffectedGLEntry(credits)">
<fa-icon icon="plus-circle" size="lg"></fa-icon>
</button>
</span>
<span fxFlex>
<button *ngIf="i !== 0" type="button" mat-icon-button (click)="removeAffectedGLEntry(journalEntryForm.get('credits'), i)"><mat-icon>remove_circle</mat-icon></button>
<button *ngIf="i === 0 glAccountData" type="button" mat-icon-button color="primary" (click)="addAffectedGLEntry(journalEntryForm.get('credits'))"><mat-icon>add_circle</mat-icon></button>
</span>
</div>
</div>
<mat-form-field fxFlex="48%">
<mat-label>Reference Number</mat-label>
<input matInput formControlName="referenceNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Transaction Date</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="transactionDatePicker" required formControlName="transactionDate">
<mat-datepicker-toggle matSuffix [for]="transactionDatePicker"></mat-datepicker-toggle>
<mat-datepicker #transactionDatePicker></mat-datepicker>
<mat-error *ngIf="journalEntryForm.controls.transactionDate.hasError('required')">
Transaction Date is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Payment Type</mat-label>
<mat-select formControlName="paymentTypeId">
<mat-option *ngFor="let paymentType of paymentTypeData" [value]="paymentType.id">
{{ paymentType.name }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Account Number</mat-label>
<input matInput formControlName="accountNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Cheque Number</mat-label>
<input matInput formControlName="checkNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Routing Code</mat-label>
<input matInput formControlName="routingCode">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Receipt Number</mat-label>
<input matInput formControlName="receiptNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Bank Number</mat-label>
<input matInput formControlName="bankNumber">
</mat-form-field>
<mat-form-field fxFlex="98%">
<mat-label>Comments</mat-label>
<textarea matInput formControlName="comments"></textarea>
</mat-form-field>
</div>
<mat-form-field fxFlex="48%">
<input matInput placeholder="Reference Number" formControlName="referenceNumber">
</mat-form-field>
</mat-card-content>
<mat-form-field fxFlex="48%">
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="transactionDatePicker" placeholder="Transaction Date" required formControlName="transactionDate">
<mat-datepicker-toggle matSuffix [for]="transactionDatePicker"></mat-datepicker-toggle>
<mat-datepicker #transactionDatePicker></mat-datepicker>
</mat-form-field>
<mat-card-actions fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center" fxLayoutGap="5px">
<button type="button" mat-raised-button [routerLink]="['../']">Cancel</button>
<button mat-raised-button color="primary" [disabled]="!journalEntryForm.valid">Submit</button>
</mat-card-actions>
<mat-form-field fxFlex="48%">
<mat-select placeholder="Payment Type" formControlName="paymentTypeId">
<mat-option *ngFor="let paymentType of paymentTypeData" [value]="paymentType.id">
{{ paymentType.name }}
</mat-option>
</mat-select>
</mat-form-field>
</form>
<mat-form-field fxFlex="48%">
<input matInput placeholder="Account Number" formControlName="accountNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<input matInput placeholder="Cheque Number" formControlName="checkNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<input matInput placeholder="Routing Code" formControlName="routingCode">
</mat-form-field>
<mat-form-field fxFlex="48%">
<input matInput placeholder="Receipt Number" formControlName="receiptNumber">
</mat-form-field>
<mat-form-field fxFlex="48%">
<input matInput placeholder="Bank Number" formControlName="bankNumber">
</mat-form-field>
<mat-form-field fxFlex="98%">
<textarea matInput placeholder="Comments" formControlName="comments"></textarea>
</mat-form-field>
</div>
</mat-card-content>
<mat-card-actions fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center" fxLayoutGap="5px">
<button type="button" mat-raised-button [routerLink]="['/accounting']">Cancel</button>
<button mat-raised-button color="primary">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</mat-card>
</div>
@@ -1,9 +1,14 @@
/** Angular Imports */
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormArray } from '@angular/forms';
import { Router } from '@angular/router';
import { Router, ActivatedRoute } from '@angular/router';
/** Custom Services */
import { AccountingService } from '../accounting.service';
/**
* Create Journal Entry component.
*/
@Component({
selector: 'mifosx-create-journal-entry',
templateUrl: './create-journal-entry.component.html',
@@ -11,28 +16,55 @@ import { AccountingService } from '../accounting.service';
})
export class CreateJournalEntryComponent implements OnInit {
// TODO: Validations
/** Minimum transaction date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum transaction date allowed. */
maxDate = new Date();
/** Journal entry form. */
journalEntryForm: FormGroup;
/** Office data. */
officeData: any;
/** Currency data. */
currencyData: any;
/** Payment type data. */
paymentTypeData: any;
/** Gl Account data. */
glAccountData: any;
/**
* Retrieves the offices, currencies, payment types and gl accounts data from `resolve`.
* @param {FormBuilder} formBuilder Form Builder.
* @param {AccountingService} accountingService Accounting Service.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
*/
constructor(private formBuilder: FormBuilder,
private accountingService: AccountingService,
private router: Router) { }
ngOnInit() {
this.createJournalEntryForm();
this.getOffices();
this.getCurrencies();
this.getPaymentTypes();
this.getGlAccounts();
private route: ActivatedRoute,
private router: Router) {
this.route.data.subscribe((data: {
offices: any,
currencies: any,
paymentTypes: any,
glAccounts: any
}) => {
this.officeData = data.offices;
this.currencyData = data.currencies.selectedCurrencyOptions;
this.paymentTypeData = data.paymentTypes;
this.glAccountData = data.glAccounts;
});
}
/**
* Creates the journal entry form.
*/
ngOnInit() {
this.createJournalEntryForm();
}
/**
* Creates the journal entry form.
*/
createJournalEntryForm() {
this.journalEntryForm = this.formBuilder.group({
'officeId': ['', Validators.required],
@@ -51,6 +83,10 @@ export class CreateJournalEntryComponent implements OnInit {
});
}
/**
* Creates the affected gl entry form.
* @returns {FormGroup} Affected gl entry form.
*/
createAffectedGLEntryForm(): FormGroup {
return this.formBuilder.group({
'glAccountId': ['', Validators.required],
@@ -58,50 +94,62 @@ export class CreateJournalEntryComponent implements OnInit {
});
}
getFormArrayData(type: string): FormArray {
return <FormArray>this.journalEntryForm.get(type);
/**
* Gets the affected gl entry (debits) form array.
* @returns {FormArray} Debits form array.
*/
get debits(): FormArray {
return this.journalEntryForm.get('debits') as FormArray;
}
/**
* Gets the affected gl entry (credits) form array.
* @returns {FormArray} Credits form array.
*/
get credits(): FormArray {
return this.journalEntryForm.get('credits') as FormArray;
}
/**
* Adds the affected gl entry form to given affected gl entry form array.
* @param {FormArray} affectedGLEntryFormArray Given affected gl entry form array (debit/credit).
*/
addAffectedGLEntry(affectedGLEntryFormArray: FormArray) {
affectedGLEntryFormArray.push(this.createAffectedGLEntryForm());
}
/**
* Removes the affected gl entry form from given affected gl entry form array at given index.
* @param {FormArray} affectedGLEntryFormArray Given affected gl entry form array (debit/credit).
* @param {number} index Array index from where affected gl entry form needs to be removed.
*/
removeAffectedGLEntry(affectedGLEntryFormArray: FormArray, index: number) {
affectedGLEntryFormArray.removeAt(index);
}
getOffices() {
this.accountingService.getOffices().subscribe(officeData => {
this.officeData = officeData;
});
}
getCurrencies() {
this.accountingService.getCurrencies().subscribe(currencyData => {
this.currencyData = currencyData.selectedCurrencyOptions;
});
}
getPaymentTypes() {
this.accountingService.getPaymentTypes().subscribe(paymentTypeData => {
this.paymentTypeData = paymentTypeData;
});
}
getGlAccounts() {
this.accountingService.getGlAccounts().subscribe(glAccountData => {
this.glAccountData = glAccountData;
});
}
/**
* Submits the journal entries form and creates journal entry,
* if successful redirects to view created transaction.
*/
submit() {
const journalEntry = this.journalEntryForm.value;
// TODO: Update once language and date settings are setup
journalEntry.locale = 'en';
journalEntry.dateFormat = 'dd MMMM yyyy';
journalEntry.transactionDate = '20 July 2018';
journalEntry.dateFormat = 'yyyy-MM-dd';
if (journalEntry.transactionDate instanceof Date) {
let day = journalEntry.transactionDate.getDate();
let month = journalEntry.transactionDate.getMonth() + 1;
const year = journalEntry.transactionDate.getFullYear();
if (day < 10) {
day = `0${day}`;
}
if (month < 10) {
month = `0${month}`;
}
journalEntry.transactionDate = `${year}-${month}-${day}`;
}
this.accountingService.createJournalEntry(journalEntry).subscribe(response => {
this.router.navigate(['/accounting/transactions/view', response.transactionId]);
this.router.navigate(['../transactions/view', response.transactionId], { relativeTo: this.route });
});
}