Skip to content

Commit

Permalink
Change stat 'cost to year end' to 'Average cost per year'
Browse files Browse the repository at this point in the history
  • Loading branch information
margual56 committed Sep 25, 2023
1 parent d741728 commit 6ad83be
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nix-bucks",
"private": true,
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "nix-bucks"
version = "0.1.0"
version = "0.1.1"
description = "A simple budgeting app"
authors = ["Marcos Gutiérrez <[email protected]"]
authors = ["Marcos Gutiérrez Alonso <[email protected]>"]
license = "GPL-3.0"
repository = "https://github.com/margual56/nix-bucks"
edition = "2021"
Expand Down
7 changes: 6 additions & 1 deletion src-tauri/src/getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ pub fn monthly_cost(app: tauri::State<Wrapper>) -> String {
}

#[tauri::command]
pub fn eoy_cost(app: tauri::State<Wrapper>) -> String {
pub fn yearly_cost(app: tauri::State<Wrapper>) -> String {
format_money(-app.0.lock().unwrap().yearly_costs())
}

#[tauri::command]
pub fn eoy_cost(app: tauri::State<Wrapper>) -> String {
format_money(-app.0.lock().unwrap().eoy_costs())
}

#[tauri::command]
pub fn eoy_income(app: tauri::State<Wrapper>) -> String {
format_money(app.0.lock().unwrap().yearly_income())
Expand Down
24 changes: 22 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,26 @@ impl App {

/// Returns the total cost of all subscriptions in a whole year.
pub fn yearly_costs(&self) -> f32 {
let mut cost = 0.0;

for subscription in self.subscriptions.clone().into_values() {
cost += subscription.cost()
/ subscription
.recurrence()
.times_in_a_year()
.unwrap_or(1.0 / 12.0);
}

for expense in self.fixed_expenses.clone().into_values() {
if expense.date().year_ce().1 == Utc::now().year_ce().1 {
cost += expense.cost();
}
}

cost
}

pub fn eoy_costs(&self) -> f32 {
cost_to_year_end(
self.subscriptions.clone().into_values().collect(),
self.fixed_expenses.clone().into_values().collect(),
Expand All @@ -260,7 +280,7 @@ impl App {
}

for subscription in self.subscriptions.values() {
amount -= subscription.cost();
amount -= subscription.cost_per_month();
}

amount
Expand All @@ -274,6 +294,6 @@ impl App {
}

pub fn yearly_balance(&self) -> f32 {
self.yearly_income() - self.yearly_costs()
self.yearly_income() - self.eoy_costs()
}
}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn main() {
get_punctual_incomes,
get_punctual_expenses,
monthly_cost,
yearly_cost,
eoy_cost,
eoy_income,
eoy_balance,
Expand Down
8 changes: 8 additions & 0 deletions src-tauri/src/utils/recurrence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ impl Recurrence {
SimpleRecurrence::Year => Self::Year(days, months, years),
}
}

pub fn times_in_a_year(self) -> Option<f32> {
match self {
Self::Day(days) => Some(365.0 / days as f32),
Self::Month(_, months) => Some(12.0 / months as f32),
Self::Year(_, _, years) => Some(1.0 / years as f32),
}
}
}

impl Display for Recurrence {
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "nix-bucks",
"version": "0.1.0"
"version": "0.1.1"
},
"tauri": {
"allowlist": {
Expand Down Expand Up @@ -39,7 +39,7 @@
"resizable": true,
"title": "Nix Bucks",
"width": 1300,
"height": 700
"height": 750
}
]
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/Stats.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script lang="ts">
import { invoke } from "@tauri-apps/api/tauri";
import { onMount } from 'svelte';
import { monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "./store.ts";
import { monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "./store.ts";
onMount(async () => {
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost = await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand All @@ -21,9 +21,9 @@
<p class="amount" class:negative={$monthly_cost.includes("-")}>{$monthly_cost}</p>
</div>
<div class="card">
<img src="/icon-arrowup.svg" alt="" width="32" height="32" />
<p class="title">Total cost by the end of year</p>
<p class="amount" class:negative={$eoy_cost.includes("-")}>{$eoy_cost}</p>
<img src="/src/assets/icon-arrowup.svg" alt="" width="32" height="32" />
<p class="title">Total average cost per year</p>
<p class="amount" class:negative={$yearly_cost.includes("-")}>{$yearly_cost}</p>
</div>
<div class="card">
<img src="/icon-arrowup.svg" alt="" width="32" height="32" />
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dialogs/NewFixedExpenseDialog.svelte
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script lang="ts">
import { invoke } from "@tauri-apps/api";
import { p_expenses, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { p_expenses, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { formatDate } from "../../App.svelte";
let concept = "";
Expand All @@ -31,7 +31,7 @@
$p_expenses = [...$p_expenses, new_p_expense];
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost= await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dialogs/NewIncomeDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script lang="ts">
import { invoke } from "@tauri-apps/api";
import { incomes, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { incomes, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import type { Subscription } from "../../App.svelte";
import type { Writable } from "svelte/store";
Expand Down Expand Up @@ -38,7 +38,7 @@
$incomes = [...$incomes, (new_income as Subscription)];
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost= await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dialogs/NewPunctualIncomeDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script lang="ts">
import { invoke } from "@tauri-apps/api";
import { p_incomes, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { p_incomes, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { formatDate } from "../../App.svelte";
let concept = "";
Expand All @@ -30,7 +30,7 @@
$p_incomes = [...$p_incomes, new_p_income];
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost= await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dialogs/NewSubscriptionDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<script lang="ts">
import { invoke } from "@tauri-apps/api";
import type { Subscription } from "../../App.svelte";
import { subscriptions, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { subscriptions, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
let selected = 'Month';
Expand Down Expand Up @@ -37,7 +37,7 @@
$subscriptions = [...$subscriptions, (new_subscription as Subscription)];
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost= await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down
1 change: 1 addition & 0 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const p_incomes: Writable<Punctual[]> = writable([]);
export const p_expenses: Writable<Punctual[]> = writable([]);

export const monthly_cost: Writable<string> = writable("Loading...");
export const yearly_cost: Writable<string> = writable("Loading...");
export const eoy_cost: Writable<string> = writable("Loading...");
export const eoy_income: Writable<string> = writable("Loading...");
export const eoy_balance: Writable<string> = writable("Loading...");
Expand Down
6 changes: 3 additions & 3 deletions src/lib/tables/FixedExpenseTable.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { openModal } from "../dialogs/NewFixedExpenseDialog.svelte";
import { p_expenses, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { p_expenses, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { onMount } from "svelte";
import { invoke } from "@tauri-apps/api/tauri";
import type { Punctual } from "../../App.svelte";
Expand All @@ -14,8 +14,8 @@
invoke("monthly_cost").then((value) => {
$monthly_cost = (value as string);
});
invoke("eoy_cost").then((value) => {
$eoy_cost = (value as string);
invoke("yearly_cost").then((value) => {
$yearly_cost = value
});
invoke("eoy_income").then((value) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tables/IncomeTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { invoke } from "@tauri-apps/api/tauri";
import type { Subscription } from "../../App.svelte";
import { incomes, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { incomes, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
onMount(async () => {
$incomes = (await invoke("get_incomes") as Subscription[]);
Expand All @@ -13,7 +13,7 @@
function delete_income(uuid: string) {
invoke("delete_uuid", {uuid: uuid}).then(async () => {
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost= await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tables/PunctualIncomeTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { openModal } from "../dialogs/NewPunctualIncomeDialog.svelte";
import { invoke } from "@tauri-apps/api/tauri";
import type { Punctual } from "../../App.svelte";
import { p_incomes, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { p_incomes, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { onMount } from "svelte";
onMount(async () => {
Expand All @@ -12,7 +12,7 @@
async function delete_p_income(uuid: string) {
invoke("delete_uuid", {uuid: uuid}).then(async () => {
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost= await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tables/SubscriptionsTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { openModal } from "../dialogs/NewSubscriptionDialog.svelte";
import { onMount } from "svelte";
import { invoke } from "@tauri-apps/api/tauri";
import { type Subscription, subscriptions, monthly_cost, eoy_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
import { type Subscription, subscriptions, monthly_cost, yearly_cost, eoy_income, eoy_balance, eom_balance } from "../store.ts";
onMount(async () => {
$subscriptions = (await invoke("get_subscriptions") as Subscription[]);
Expand All @@ -11,7 +11,7 @@
async function delete_subscription(uuid: string) {
await invoke("delete_uuid", {uuid: uuid}).then(async () => {
$monthly_cost = await invoke("monthly_cost");
$eoy_cost = await invoke("eoy_cost");
$yearly_cost = await invoke("yearly_cost");
$eoy_income = await invoke("eoy_income");
$eoy_balance = await invoke("eoy_balance");
$eom_balance = await invoke("eom_balance");
Expand Down

0 comments on commit 6ad83be

Please sign in to comment.