feat(accounting-rules): edit and delete rule

Add edit accounting rule component and ability to delete accounting
rule.
This commit is contained in:
Abhay Chawla
2018-08-06 00:57:30 +05:30
parent 6b008b968d
commit ca80e5eff4
8 changed files with 312 additions and 5 deletions
@@ -29,6 +29,7 @@ import { AccountingRulesComponent } from './accounting-rules/accounting-rules.co
import { CreateRuleComponent } from './accounting-rules/create-rule/create-rule.component';
import { ViewRuleComponent } from './accounting-rules/view-rule/view-rule.component';
import { ViewAccountingRuleResolver } from './accounting-rules/view-rule/view-rule.resolver';
import { EditRuleComponent } from './accounting-rules/edit-rule/edit-rule.component';
const routes: Routes = [
Route.withShell([
@@ -181,11 +182,22 @@ const routes: Routes = [
},
{
path: 'view/:id',
component: ViewRuleComponent,
data: { title: extract('View Accounting Rule'), routeResolveBreadcrumb: ['accountingRule', 'name'] },
resolve: {
accountingRule: ViewAccountingRuleResolver
}
},
runGuardsAndResolvers: 'always',
children: [
{
path: '',
component: ViewRuleComponent
},
{
path: 'edit',
component: EditRuleComponent,
data: { title: extract('Edit Accounting Rules'), breadcrumb: 'Edit', routeResolveBreadcrumb: false }
}
]
}
]
}
@@ -0,0 +1,105 @@
<div class="container">
<mat-card>
<form [formGroup]="accountingRuleForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column">
<mat-form-field fxFlex="48%">
<input matInput placeholder="Accounting Rule Name" required formControlName="name">
</mat-form-field>
<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 fxFlex="48%" fxLayout="row" fxLayout.lt-md="column" class="rule-wrapper">
<label fxFlex="50%">Affected GL Entry (Debit) Rule Type *</label>
<mat-radio-group fxFlex="50%" fxLayout="row" fxLayoutGap="5%" formControlName="debitRuleType">
<mat-radio-button value="fixedAccount">Fixed Account</mat-radio-button>
<mat-radio-button value="listOfAccounts">List of Accounts</mat-radio-button>
</mat-radio-group>
</div>
<mat-form-field fxFlex="48%" *ngIf="accountingRuleForm.controls.debitRuleType.value === 'fixedAccount'">
<mat-select placeholder="Account to Debit" required formControlName="accountToDebit">
<mat-option *ngFor="let glAccount of glAccountData" [value]="glAccount.id">
{{ glAccount.name }}
</mat-option>
</mat-select>
</mat-form-field>
<div fxFlex="48%" fxLayout="row" fxLayoutGap="2%" fxLayout.lt-md="column" *ngIf="accountingRuleForm.controls.debitRuleType.value === 'listOfAccounts'">
<mat-form-field fxFlex="50%">
<mat-select placeholder="Debit Tags" required formControlName="debitTags" multiple>
<mat-option *ngFor="let debitTag of debitTagData" [value]="debitTag.id">
{{ debitTag.name }}
</mat-option>
</mat-select>
</mat-form-field>
<div fxFlex="50%" class="entries-wrapper">
<mat-checkbox labelPosition="before" required formControlName="allowMultipleDebitEntries" class="entries">
Multiple Debit Entries Allowed
</mat-checkbox>
</div>
</div>
<div fxFlex="48%" fxLayout="row" fxLayout.lt-md="column" class="rule-wrapper">
<label fxFlex="50%">Affected GL Entry (Credit) Rule Type *</label>
<mat-radio-group fxFlex="50%" fxLayout="row" fxLayoutGap="5%" formControlName="creditRuleType">
<mat-radio-button value="fixedAccount">Fixed Account</mat-radio-button>
<mat-radio-button value="listOfAccounts">List of Accounts</mat-radio-button>
</mat-radio-group>
</div>
<mat-form-field fxFlex="48%" *ngIf="accountingRuleForm.controls.creditRuleType.value === 'fixedAccount'">
<mat-select placeholder="Account to Credit" required formControlName="accountToCredit">
<mat-option *ngFor="let glAccount of glAccountData" [value]="glAccount.id">
{{ glAccount.name }}
</mat-option>
</mat-select>
</mat-form-field>
<div fxFlex="48%" fxLayout="row" fxLayoutGap="2%" fxLayout.lt-md="column" *ngIf="accountingRuleForm.controls.creditRuleType.value === 'listOfAccounts'">
<mat-form-field fxFlex="50%">
<mat-select placeholder="Credit Tags" required formControlName="creditTags" multiple>
<mat-option *ngFor="let creditTag of creditTagData" [value]="creditTag.id">
{{ creditTag.name }}
</mat-option>
</mat-select>
</mat-form-field>
<div fxFlex="50%" class="entries-wrapper">
<mat-checkbox labelPosition="before" required formControlName="allowMultipleCreditEntries" class="entries">
Multiple Credit Entries Allowed
</mat-checkbox>
</div>
</div>
<mat-form-field fxFlex="98%">
<textarea matInput placeholder="Description" formControlName="description"></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/accounting-rules/view', accountingRule?.id]">Cancel</button>
<button mat-raised-button color="primary">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,19 @@
.rule-wrapper {
margin: 17.5px 0 0 0;
}
.additional-selection {
opacity: 0.75;
font-size: 0.75em;
}
.entries-wrapper {
position: relative;
.entries {
padding: 0 0 17.5px 0;
position: absolute;
bottom: 0;
right: 0;
}
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { EditRuleComponent } from './edit-rule.component';
describe('EditRuleComponent', () => {
let component: EditRuleComponent;
let fixture: ComponentFixture<EditRuleComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EditRuleComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditRuleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,115 @@
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, ValidatorFn, ValidationErrors } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { AccountingService } from '../../accounting.service';
const oneOfTheFieldsIsRequiredValidator: ValidatorFn = (accountingRuleForm: FormGroup): ValidationErrors | null => {
const accountToDebit = accountingRuleForm.controls.accountToDebit.value;
const debitTags = accountingRuleForm.controls.debitTags.value;
const accountToCredit = accountingRuleForm.controls.accountToCredit.value;
const creditTags = accountingRuleForm.controls.creditTags.value;
return ((accountToDebit || debitTags) && (accountToCredit || creditTags)) ? null : { 'error': true };
};
@Component({
selector: 'mifosx-edit-rule',
templateUrl: './edit-rule.component.html',
styleUrls: ['./edit-rule.component.scss']
})
export class EditRuleComponent implements OnInit {
accountingRuleForm: FormGroup;
accountingRule: any;
officeData: any;
glAccountData: any;
debitTagData: any;
creditTagData: any;
constructor(private route: ActivatedRoute,
private formBuilder: FormBuilder,
private accountingService: AccountingService,
private router: Router) {
this.route.data.subscribe((data: { accountingRule: any }) => {
this.accountingRule = data.accountingRule;
});
}
ngOnInit() {
this.createAccountingRulesForm();
this.getAccountingRulesTemplate();
this.setAccountingRulesForm();
}
createAccountingRulesForm() {
this.accountingRuleForm = this.formBuilder.group({
'name': [this.accountingRule.name, Validators.required],
'officeId': [this.accountingRule.officeId, Validators.required],
'debitRuleType': [''],
'accountToDebit': [''],
'debitTags': [''],
'allowMultipleDebitEntries': [''],
'creditRuleType': [''],
'accountToCredit': [''],
'creditTags': [''],
'allowMultipleCreditEntries': [''],
'description': [this.accountingRule.description]
}, { validator: oneOfTheFieldsIsRequiredValidator });
}
setAccountingRulesForm() {
this.accountingRuleForm.get('debitRuleType').valueChanges.subscribe(debitRuleType => {
if (debitRuleType === 'fixedAccount') {
this.accountingRuleForm.get('debitTags').reset();
this.accountingRuleForm.get('allowMultipleDebitEntries').reset();
} else {
this.accountingRuleForm.get('allowMultipleDebitEntries').setValue(false);
}
});
this.accountingRuleForm.get('creditRuleType').valueChanges.subscribe(creditRuleType => {
if (creditRuleType === 'fixedAccount') {
this.accountingRuleForm.get('creditTags').reset();
this.accountingRuleForm.get('allowMultipleCreditEntries').reset();
} else {
this.accountingRuleForm.get('allowMultipleCreditEntries').setValue(false);
}
});
if (this.accountingRule.debitAccounts) {
this.accountingRuleForm.get('debitRuleType').setValue('fixedAccount');
this.accountingRuleForm.get('accountToDebit').setValue(this.accountingRule.debitAccounts[0].id);
} else {
this.accountingRuleForm.get('debitRuleType').setValue('listOfAccounts');
this.accountingRuleForm.get('debitTags').setValue(this.accountingRule.debitTags.map((debitTag: any) => debitTag.tag.id));
this.accountingRuleForm.get('allowMultipleDebitEntries').setValue(this.accountingRule.allowMultipleDebitEntries);
}
if (this.accountingRule.creditAccounts) {
this.accountingRuleForm.get('creditRuleType').setValue('fixedAccount');
this.accountingRuleForm.get('accountToCredit').setValue(this.accountingRule.creditAccounts[0].id);
} else {
this.accountingRuleForm.get('creditRuleType').setValue('listOfAccounts');
this.accountingRuleForm.get('creditTags').setValue(this.accountingRule.creditTags.map((creditTag: any) => creditTag.tag.id));
this.accountingRuleForm.get('allowMultipleCreditEntries').setValue(this.accountingRule.allowMultipleCreditEntries);
}
}
getAccountingRulesTemplate() {
this.accountingService.getAccountingRulesTemplate().subscribe((accountingRulesData: any) => {
this.officeData = accountingRulesData.allowedOffices;
this.glAccountData = accountingRulesData.allowedAccounts;
this.debitTagData = accountingRulesData.allowedDebitTagOptions;
this.creditTagData = accountingRulesData.allowedCreditTagOptions;
});
}
submit() {
const accountingRule = this.accountingRuleForm.value;
delete accountingRule.debitRuleType;
delete accountingRule.creditRuleType;
this.accountingService.updateAccountingRule(this.accountingRule.id, accountingRule).subscribe((response: any) => {
this.router.navigate(['/accounting/accounting-rules/view', response.resourceId]);
});
}
}
@@ -1,5 +1,9 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog } from '@angular/material';
import { AccountingService } from '../../accounting.service';
import { DeleteDialogComponent } from '../../../shared/delete-dialog/delete-dialog.component';
@Component({
selector: 'mifosx-view-rule',
@@ -10,7 +14,10 @@ export class ViewRuleComponent implements OnInit {
accountingRule: any;
constructor(private route: ActivatedRoute) {
constructor(private route: ActivatedRoute,
private router: Router,
private accountingService: AccountingService,
public dialog: MatDialog) {
this.route.data.subscribe((data: { accountingRule: any }) => {
this.accountingRule = data.accountingRule;
});
@@ -19,4 +26,18 @@ export class ViewRuleComponent implements OnInit {
ngOnInit() {
}
deleteAccountingRule() {
const deleteAccountingRuleDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `accounting rule ${this.accountingRule.id}` }
});
deleteAccountingRuleDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.accountingService.deleteAccountingRule(this.accountingRule.id)
.subscribe(() => {
this.router.navigate(['/accounting/accounting-rules']);
});
}
});
}
}
+3 -1
View File
@@ -27,6 +27,7 @@ import { EditClosureComponent } from './closing-entries/edit-closure/edit-closur
import { AccountingRulesComponent } from './accounting-rules/accounting-rules.component';
import { CreateRuleComponent } from './accounting-rules/create-rule/create-rule.component';
import { ViewRuleComponent } from './accounting-rules/view-rule/view-rule.component';
import { EditRuleComponent } from './accounting-rules/edit-rule/edit-rule.component';
@NgModule({
imports: [
@@ -58,7 +59,8 @@ import { ViewRuleComponent } from './accounting-rules/view-rule/view-rule.compon
EditClosureComponent,
AccountingRulesComponent,
CreateRuleComponent,
ViewRuleComponent
ViewRuleComponent,
EditRuleComponent
],
entryComponents: [
RevertTransactionComponent,
+8
View File
@@ -157,4 +157,12 @@ export class AccountingService {
return this.http.get(`/accountingrules/${accountingRuleId}`);
}
deleteAccountingRule(accountingRuleId: number) {
return this.http.delete(`/accountingrules/${accountingRuleId}`);
}
updateAccountingRule(accoutingRuleId: number, accountingRule: any) {
return this.http.put(`/accountingrules/${accoutingRuleId}`, accountingRule);
}
}