Fix amount handling in categories and transactions
This commit is contained in:
parent
529a420c14
commit
3957651211
3 changed files with 14 additions and 6 deletions
|
@ -2,6 +2,7 @@ import { Component, OnInit, Input, Inject } from '@angular/core';
|
||||||
import { Category } from '../category';
|
import { Category } from '../category';
|
||||||
import { AppComponent } from 'src/app/app.component';
|
import { AppComponent } from 'src/app/app.component';
|
||||||
import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service';
|
import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service';
|
||||||
|
import { decimalToInteger } from 'src/app/shared/utils';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-category-form',
|
selector: 'app-category-form',
|
||||||
|
@ -27,6 +28,7 @@ export class CategoryFormComponent implements OnInit {
|
||||||
|
|
||||||
save(): void {
|
save(): void {
|
||||||
let promise;
|
let promise;
|
||||||
|
this.currentCategory.amount = decimalToInteger(String(this.currentCategory.amount))
|
||||||
if (this.create) {
|
if (this.create) {
|
||||||
// This is a new category, save it
|
// This is a new category, save it
|
||||||
promise = this.twigsService.createCategory(
|
promise = this.twigsService.createCategory(
|
||||||
|
@ -34,14 +36,13 @@ export class CategoryFormComponent implements OnInit {
|
||||||
this.budgetId,
|
this.budgetId,
|
||||||
this.currentCategory.title,
|
this.currentCategory.title,
|
||||||
this.currentCategory.description,
|
this.currentCategory.description,
|
||||||
this.currentCategory.amount * 100,
|
this.currentCategory.amount,
|
||||||
this.currentCategory.expense
|
this.currentCategory.expense
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// This is an existing category, update it
|
// This is an existing category, update it
|
||||||
const updatedCategory: Category = {
|
const updatedCategory: Category = {
|
||||||
...this.currentCategory,
|
...this.currentCategory,
|
||||||
amount: this.currentCategory.amount * 100
|
|
||||||
}
|
}
|
||||||
promise = this.twigsService.updateCategory(
|
promise = this.twigsService.updateCategory(
|
||||||
this.currentCategory.id,
|
this.currentCategory.id,
|
||||||
|
|
|
@ -5,3 +5,11 @@ export function randomId(): string {
|
||||||
window.crypto.getRandomValues(bytes)
|
window.crypto.getRandomValues(bytes)
|
||||||
return Array.from(bytes, (byte) => CHARACTERS[byte % CHARACTERS.length]).join('')
|
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")
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import { Category } from 'src/app/categories/category';
|
||||||
import { AppComponent } from 'src/app/app.component';
|
import { AppComponent } from 'src/app/app.component';
|
||||||
import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service';
|
import { TWIGS_SERVICE, TwigsService } from 'src/app/shared/twigs.service';
|
||||||
import { MatRadioChange } from '@angular/material/radio';
|
import { MatRadioChange } from '@angular/material/radio';
|
||||||
|
import { decimalToInteger } from 'src/app/shared/utils';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-add-edit-transaction',
|
selector: 'app-add-edit-transaction',
|
||||||
|
@ -60,9 +61,8 @@ export class AddEditTransactionComponent implements OnInit, OnChanges {
|
||||||
}
|
}
|
||||||
|
|
||||||
save(): void {
|
save(): void {
|
||||||
// The amount will be input as a decimal value so we need to convert it
|
|
||||||
// to an integer
|
|
||||||
let promise;
|
let promise;
|
||||||
|
this.currentTransaction.amount = decimalToInteger(String(this.currentTransaction.amount))
|
||||||
this.currentTransaction.date = new Date();
|
this.currentTransaction.date = new Date();
|
||||||
const dateParts = this.transactionDate.split('-');
|
const dateParts = this.transactionDate.split('-');
|
||||||
this.currentTransaction.date.setFullYear(parseInt(dateParts[0], 10));
|
this.currentTransaction.date.setFullYear(parseInt(dateParts[0], 10));
|
||||||
|
@ -78,7 +78,7 @@ export class AddEditTransactionComponent implements OnInit, OnChanges {
|
||||||
this.budgetId,
|
this.budgetId,
|
||||||
this.currentTransaction.title,
|
this.currentTransaction.title,
|
||||||
this.currentTransaction.description,
|
this.currentTransaction.description,
|
||||||
Math.round(this.currentTransaction.amount * 100),
|
this.currentTransaction.amount,
|
||||||
this.currentTransaction.date,
|
this.currentTransaction.date,
|
||||||
this.currentTransaction.expense,
|
this.currentTransaction.expense,
|
||||||
this.currentTransaction.categoryId,
|
this.currentTransaction.categoryId,
|
||||||
|
@ -87,7 +87,6 @@ export class AddEditTransactionComponent implements OnInit, OnChanges {
|
||||||
// This is an existing transaction, update it
|
// This is an existing transaction, update it
|
||||||
const updatedTransaction: Transaction = {
|
const updatedTransaction: Transaction = {
|
||||||
...this.currentTransaction,
|
...this.currentTransaction,
|
||||||
amount: Math.round(this.currentTransaction.amount * 100)
|
|
||||||
}
|
}
|
||||||
promise = this.twigsService.updateTransaction(
|
promise = this.twigsService.updateTransaction(
|
||||||
this.currentTransaction.id,
|
this.currentTransaction.id,
|
||||||
|
|
Loading…
Reference in a new issue