Fix amount handling in categories and transactions

This commit is contained in:
William Brawner 2023-06-05 17:04:14 +00:00
parent 529a420c14
commit 3957651211
3 changed files with 14 additions and 6 deletions

View file

@ -2,6 +2,7 @@ import { Component, OnInit, Input, Inject } from '@angular/core';
import { Category } from '../category';
import { AppComponent } from 'src/app/app.component';
import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service';
import { decimalToInteger } from 'src/app/shared/utils';
@Component({
selector: 'app-category-form',
@ -27,6 +28,7 @@ export class CategoryFormComponent implements OnInit {
save(): void {
let promise;
this.currentCategory.amount = decimalToInteger(String(this.currentCategory.amount))
if (this.create) {
// This is a new category, save it
promise = this.twigsService.createCategory(
@ -34,14 +36,13 @@ export class CategoryFormComponent implements OnInit {
this.budgetId,
this.currentCategory.title,
this.currentCategory.description,
this.currentCategory.amount * 100,
this.currentCategory.amount,
this.currentCategory.expense
);
} else {
// This is an existing category, update it
const updatedCategory: Category = {
...this.currentCategory,
amount: this.currentCategory.amount * 100
}
promise = this.twigsService.updateCategory(
this.currentCategory.id,

View file

@ -5,3 +5,11 @@ export function randomId(): string {
window.crypto.getRandomValues(bytes)
return Array.from(bytes, (byte) => CHARACTERS[byte % CHARACTERS.length]).join('')
}
export function decimalToInteger(amount: string): number {
if (amount[amount.length - 3] === "." || amount[amount.length - 3] === ",") {
return Number(amount.replace(/[,.]/g, ""))
} else {
return Number(amount + "00")
}
}

View file

@ -5,6 +5,7 @@ import { Category } from 'src/app/categories/category';
import { AppComponent } from 'src/app/app.component';
import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service';
import { MatRadioChange } from '@angular/material/radio';
import { decimalToInteger } from 'src/app/shared/utils';
@Component({
selector: 'app-add-edit-transaction',
@ -60,9 +61,8 @@ export class AddEditTransactionComponent implements OnInit, OnChanges {
}
save(): void {
// The amount will be input as a decimal value so we need to convert it
// to an integer
let promise;
this.currentTransaction.amount = decimalToInteger(String(this.currentTransaction.amount))
this.currentTransaction.date = new Date();
const dateParts = this.transactionDate.split('-');
this.currentTransaction.date.setFullYear(parseInt(dateParts[0], 10));
@ -78,7 +78,7 @@ export class AddEditTransactionComponent implements OnInit, OnChanges {
this.budgetId,
this.currentTransaction.title,
this.currentTransaction.description,
Math.round(this.currentTransaction.amount * 100),
this.currentTransaction.amount,
this.currentTransaction.date,
this.currentTransaction.expense,
this.currentTransaction.categoryId,
@ -87,7 +87,6 @@ export class AddEditTransactionComponent implements OnInit, OnChanges {
// This is an existing transaction, update it
const updatedTransaction: Transaction = {
...this.currentTransaction,
amount: Math.round(this.currentTransaction.amount * 100)
}
promise = this.twigsService.updateTransaction(
this.currentTransaction.id,