Skip to content

Commit

Permalink
Wait to have account to prefill form #10557
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed Oct 9, 2024
1 parent 10663fd commit 4f9016c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 53 deletions.
12 changes: 10 additions & 2 deletions client/app/admin/accounts/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class AccountService extends NaturalAbstractModelService<
.pipe(map(result => result.data.nextAccountCode));
}

public getAccountByCode(code: number): Observable<Accounts['accounts']> {
public getAccountByCode(code: number): Observable<Accounts['accounts']['items'][0]> {
const variables: AccountsVariables = {
filter: {
groups: [
Expand All @@ -116,7 +116,15 @@ export class AccountService extends NaturalAbstractModelService<
const qvm = new NaturalQueryVariablesManager<AccountsVariables>();
qvm.set('variables', variables);

return this.getAll(qvm);
return this.getAll(qvm).pipe(
map(res => {
if (res.length === 1) {
return res.items[0];
}

throw new Error(`Account not found for code ${code}`);
}),
);
}

public getReportExportLink(
Expand Down
97 changes: 53 additions & 44 deletions client/app/admin/transactions/services/transaction.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Injectable, inject} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {Validators} from '@angular/forms';
import {formatIsoDateTime, FormValidators, Literal, NaturalAbstractModelService} from '@ecodev/natural';
import {
Expand Down Expand Up @@ -26,6 +26,8 @@ import {
import {TransactionLineService} from './transactionLine.service';
import {localConfig} from '../../../shared/generated-config';
import {AccountService} from '../../accounts/services/account.service';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';

@Injectable({
providedIn: 'root',
Expand All @@ -44,8 +46,7 @@ export class TransactionService extends NaturalAbstractModelService<
> {
private readonly transactionLineService = inject(TransactionLineService);
private readonly accountService = inject(AccountService);

private bankAccount: Accounts['accounts']['items'][0] | null = null;
private readonly bankAccount: Observable<Accounts['accounts']['items'][0]>;

public constructor() {
super(
Expand All @@ -57,53 +58,61 @@ export class TransactionService extends NaturalAbstractModelService<
deleteTransactions,
);

this.accountService.getAccountByCode(localConfig.accounting.bankAccountCode).subscribe(res => {
if (res.length === 1) {
this.bankAccount = res.items[0];
}
});
this.bankAccount = this.accountService.getAccountByCode(localConfig.accounting.bankAccountCode);
}

public getRefundPreset(account: {id: string}, amount: string): TransactionLineInput[] {
const emptyLine = this.transactionLineService.getDefaultForServer();

const line: TransactionLineInput = {
name: 'Remboursement du membre',
debit: account,
credit: this.bankAccount,
balance: amount,
transactionDate: formatIsoDateTime(new Date()),
};

return [Object.assign(emptyLine, line)];
public getRefundPreset(account: {id: string}, amount: string): Observable<TransactionLineInput[]> {
return this.bankAccount.pipe(
map(bankAccount => {
const emptyLine = this.transactionLineService.getDefaultForServer();

const line: TransactionLineInput = {
name: 'Remboursement du membre',
debit: account,
credit: bankAccount,
balance: amount,
transactionDate: formatIsoDateTime(new Date()),
};

return [Object.assign(emptyLine, line)];
}),
);
}

public getExpenseClaimPreset(account: {id: string}, amount: string): TransactionLineInput[] {
const emptyLine = this.transactionLineService.getDefaultForServer();

const line: TransactionLineInput = {
name: 'Remboursement sur le solde',
debit: this.bankAccount,
credit: account,
balance: amount,
transactionDate: formatIsoDateTime(new Date()),
};

return [Object.assign(emptyLine, line)];
public getExpenseClaimPreset(account: {id: string}, amount: string): Observable<TransactionLineInput[]> {
return this.bankAccount.pipe(
map(bankAccount => {
const emptyLine = this.transactionLineService.getDefaultForServer();

const line: TransactionLineInput = {
name: 'Remboursement sur le solde',
debit: bankAccount,
credit: account,
balance: amount,
transactionDate: formatIsoDateTime(new Date()),
};

return [Object.assign(emptyLine, line)];
}),
);
}

public getInvoicePreset(name: string, amount: string): TransactionLineInput[] {
const emptyLine = this.transactionLineService.getDefaultForServer();

const line: TransactionLineInput = {
name: name,
debit: null,
credit: this.bankAccount,
balance: amount,
transactionDate: formatIsoDateTime(new Date()),
};

return [Object.assign(emptyLine, line)];
public getInvoicePreset(name: string, amount: string): Observable<TransactionLineInput[]> {
return this.bankAccount.pipe(
map(bankAccount => {
const emptyLine = this.transactionLineService.getDefaultForServer();

const line: TransactionLineInput = {
name: name,
debit: null,
credit: bankAccount,
balance: amount,
transactionDate: formatIsoDateTime(new Date()),
};

return [Object.assign(emptyLine, line)];
}),
);
}

protected override getFormExtraFieldDefaultValues(): Literal {
Expand Down
18 changes: 11 additions & 7 deletions client/app/admin/transactions/transaction/transaction.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, OnInit, ViewChild, inject} from '@angular/core';
import {Component, inject, OnInit, ViewChild} from '@angular/core';
import {NavigationEnd, RouterLink} from '@angular/router';
import {
cancellableTimeout,
Expand All @@ -18,6 +18,7 @@ import {
CurrentUserForProfile,
ExpenseClaim,
ExpenseClaimType,
TransactionLineInput,
UpdateTransaction,
} from '../../../shared/generated-types';
import {BookableService} from '../../bookables/services/bookable.service';
Expand Down Expand Up @@ -165,18 +166,21 @@ export class TransactionComponent
expenseClaimControl.setValue(expenseClaim);
}

let preset: Observable<TransactionLineInput[]> | null = null;
if (expenseClaim.owner?.account) {
if (expenseClaim.type === ExpenseClaimType.ExpenseClaim) {
const preset = this.service.getExpenseClaimPreset(expenseClaim.owner.account, expenseClaim.amount);
this.transactionLinesComponent.setItems(preset);
preset = this.service.getExpenseClaimPreset(expenseClaim.owner.account, expenseClaim.amount);
} else if (expenseClaim.type === ExpenseClaimType.Refund) {
const preset = this.service.getRefundPreset(expenseClaim.owner.account, expenseClaim.amount);
this.transactionLinesComponent.setItems(preset);
preset = this.service.getRefundPreset(expenseClaim.owner.account, expenseClaim.amount);
}
}

if (expenseClaim.type === ExpenseClaimType.Invoice) {
const preset = this.service.getInvoicePreset(transactionName, expenseClaim.amount);
this.transactionLinesComponent.setItems(preset);
preset = this.service.getInvoicePreset(transactionName, expenseClaim.amount);
}

if (preset) {
preset.subscribe(preset => this.transactionLinesComponent.setItems(preset));
}
}

Expand Down

0 comments on commit 4f9016c

Please sign in to comment.