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:
@@ -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';
|
||||
|
||||
/**
|
||||
* Frequent Postings component.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'mifosx-frequent-postings',
|
||||
templateUrl: './frequent-postings.component.html',
|
||||
@@ -11,35 +16,64 @@ import { AccountingService } from '../accounting.service';
|
||||
})
|
||||
export class FrequentPostingsComponent implements OnInit {
|
||||
|
||||
// TODO: Validations
|
||||
// https://github.com/openMF/incubator-fineract/blob/master/fineract-provider/src/main/java/org/apache/fineract/accounting/journalentry/exception/JournalEntryInvalidException.java
|
||||
|
||||
/** Minimum transaction date allowed. */
|
||||
minDate = new Date(2000, 0, 1);
|
||||
/** Maximum transaction date allowed. */
|
||||
maxDate = new Date();
|
||||
/** Frequent postings form. */
|
||||
frequentPostingsForm: FormGroup;
|
||||
/** Office data. */
|
||||
officeData: any;
|
||||
/** Accounting rule data. */
|
||||
accountingRuleData: any;
|
||||
/** Currency data. */
|
||||
currencyData: any;
|
||||
/** Payment type data. */
|
||||
paymentTypeData: any;
|
||||
/** Debit account data. */
|
||||
debitAccountData: any;
|
||||
/** Credit account data. */
|
||||
creditAccountData: any;
|
||||
|
||||
/** True if multiple credit entries are allowed. */
|
||||
allowMultipleCreditEntries: boolean;
|
||||
/** True if multiple debit entries are allowed. */
|
||||
allowMultipleDebitEntries: boolean;
|
||||
|
||||
/**
|
||||
* Retrieves the offices, accounting rules, currencies and payment types 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) { }
|
||||
private route: ActivatedRoute,
|
||||
private router: Router) {
|
||||
this.route.data.subscribe((data: {
|
||||
offices: any,
|
||||
accountingRules: any,
|
||||
currencies: any,
|
||||
paymentTypes: any
|
||||
}) => {
|
||||
this.officeData = data.offices;
|
||||
this.accountingRuleData = data.accountingRules;
|
||||
this.currencyData = data.currencies.selectedCurrencyOptions;
|
||||
this.paymentTypeData = data.paymentTypes;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the frequent postings form and sets the affected gl entry form array.
|
||||
*/
|
||||
ngOnInit() {
|
||||
this.createFrequentPostingsForm();
|
||||
this.setAffectedGLEntryForm();
|
||||
this.getOffices();
|
||||
this.getAccountingRules();
|
||||
this.getCurrencies();
|
||||
this.getPaymentTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the frequent postings form.
|
||||
*/
|
||||
createFrequentPostingsForm() {
|
||||
this.frequentPostingsForm = this.formBuilder.group({
|
||||
'officeId': ['', Validators.required],
|
||||
@@ -59,25 +93,30 @@ export class FrequentPostingsComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the affected gl entry form array.
|
||||
*/
|
||||
setAffectedGLEntryForm() {
|
||||
this.frequentPostingsForm.get('accountingRule').valueChanges.subscribe(accountingRule => {
|
||||
const debits = this.frequentPostingsForm.get('debits') as FormArray;
|
||||
const credits = this.frequentPostingsForm.get('credits') as FormArray;
|
||||
while (debits.length) {
|
||||
debits.removeAt(0);
|
||||
while (this.debits.length) {
|
||||
this.debits.removeAt(0);
|
||||
}
|
||||
while (credits.length) {
|
||||
credits.removeAt(0);
|
||||
while (this.credits.length) {
|
||||
this.credits.removeAt(0);
|
||||
}
|
||||
this.allowMultipleDebitEntries = accountingRule.allowMultipleDebitEntries;
|
||||
this.allowMultipleCreditEntries = accountingRule.allowMultipleCreditEntries;
|
||||
this.debitAccountData = accountingRule.debitAccounts;
|
||||
this.creditAccountData = accountingRule.creditAccounts;
|
||||
this.addAffectedGLEntry(debits);
|
||||
this.addAffectedGLEntry(credits);
|
||||
this.addAffectedGLEntry(this.debits);
|
||||
this.addAffectedGLEntry(this.credits);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the affected gl entry form.
|
||||
* @returns {FormGroup} Affected gl entry form.
|
||||
*/
|
||||
createAffectedGLEntryForm(): FormGroup {
|
||||
return this.formBuilder.group({
|
||||
'glAccountId': ['', Validators.required],
|
||||
@@ -85,51 +124,63 @@ export class FrequentPostingsComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
getFormArrayData(type: string): FormArray {
|
||||
return <FormArray>this.frequentPostingsForm.get(type);
|
||||
/**
|
||||
* Gets the affected gl entry (debits) form array.
|
||||
* @returns {FormArray} Debits form array.
|
||||
*/
|
||||
get debits(): FormArray {
|
||||
return this.frequentPostingsForm.get('debits') as FormArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the affected gl entry (credits) form array.
|
||||
* @returns {FormArray} Credits form array.
|
||||
*/
|
||||
get credits(): FormArray {
|
||||
return this.frequentPostingsForm.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;
|
||||
});
|
||||
}
|
||||
|
||||
getAccountingRules() {
|
||||
this.accountingService.getAccountingRules(true).subscribe(accountingRuleData => {
|
||||
this.accountingRuleData = accountingRuleData;
|
||||
});
|
||||
}
|
||||
|
||||
getCurrencies() {
|
||||
this.accountingService.getCurrencies().subscribe(currencyData => {
|
||||
this.currencyData = currencyData.selectedCurrencyOptions;
|
||||
});
|
||||
}
|
||||
|
||||
getPaymentTypes() {
|
||||
this.accountingService.getPaymentTypes().subscribe(paymentTypeData => {
|
||||
this.paymentTypeData = paymentTypeData;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the frequent postings form and creates journal entry,
|
||||
* if successful redirects to view created transaction.
|
||||
*/
|
||||
submit() {
|
||||
const journalEntry = this.frequentPostingsForm.value;
|
||||
journalEntry.accountingRule = journalEntry.accountingRule.id;
|
||||
// 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 });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user