Skip to content

Commit

Permalink
Initial support for editable fields.
Browse files Browse the repository at this point in the history
* Add percentage support for aeat 720 records.
* Enable editable rows in table when editable Mutable is true.
  • Loading branch information
vaijira committed Oct 11, 2024
1 parent 314b6dc commit 522e0c3
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl App {
fn render_download_button(this: &Arc<Self>) -> Dom {
html!("section", {
.child_signal(
App::is_needed_to_generate_report(this).map(clone!(this => move |x| {
Self::is_needed_to_generate_report(this).map(clone!(this => move |x| {
let default_button = Some(
html!("button", {
.attr("type", "button")
Expand Down
1 change: 1 addition & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub struct Aeat720Record {
pub value_in_euro: Decimal,
pub first_tx_date: usize,
pub broker: Arc<BrokerInformation>,
pub percentage: Decimal,
}

#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
Expand Down
19 changes: 15 additions & 4 deletions src/reports/aeat_720.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Write;

/*
aeat 720 model specification.
https://www.agenciatributaria.es/static_files/AEAT/Contenidos_Comunes/La_Agencia_Tributaria/Ayuda/Disenyos_de_registro/Ayudas/DR_Resto_Modelos/Ficheros/modelo_720.pdf
https://www.boe.es/buscar/act.php?id=BOE-A-2013-954
Summary Register:
Expand Down Expand Up @@ -68,7 +68,7 @@ use std::io::Write;
stockQuantityFrac: NumericField, // Pos 473-474
realStateRepresentation: StringField, // pos 475
OwnPercentageInt: NumericField, // Pos 476-478
OwnPercentageFrac: NumericField, // Pos 479-489
OwnPercentageFrac: NumericField, // Pos 479-480
blank: StringField, // Pos 481-500
*/
const AEAT_720_REGISTER_SIZE_BYTES: usize = 500;
Expand Down Expand Up @@ -524,8 +524,19 @@ impl DetailRegister {
remainder.trunc().to_usize().unwrap_or(0),
)?;

Aeat720Field::write_numeric_field(&mut fields, Self::OWNED_PERCENTAGE_INT_FIELD, 100)?;
Aeat720Field::write_numeric_field(&mut fields, Self::OWNED_PERCENTAGE_FRACTION_FIELD, 0)?;
Aeat720Field::write_numeric_field(
&mut fields,
Self::OWNED_PERCENTAGE_INT_FIELD,
record.percentage.abs().to_usize().unwrap_or(0),
)?;
let mut remainder = record.percentage.fract();
remainder.set_scale(2)?;

Aeat720Field::write_numeric_field(
&mut fields,
Self::OWNED_PERCENTAGE_FRACTION_FIELD,
remainder.trunc().to_usize().unwrap_or(0),
)?;

Ok(Self { fields })
}
Expand Down
112 changes: 105 additions & 7 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{sync::Arc, usize};
use std::sync::Arc;

use dominator::{clone, events, html, with_node, Dom};
use futures_signals::{
signal::{Mutable, SignalExt},
map_ref,
signal::{Mutable, Signal, SignalExt},
signal_vec::{MutableVec, SignalVecExt},
};
use web_sys::HtmlElement;
Expand All @@ -16,6 +17,7 @@ use crate::{
pub struct Table {
headers: Vec<&'static str>,
data: MutableVec<Aeat720Record>,
editable: Mutable<bool>,
}

impl Table {
Expand All @@ -31,6 +33,7 @@ impl Table {
"Porcentaje",
],
data: aeat720_records,
editable: Mutable::new(false),
})
}

Expand All @@ -53,6 +56,15 @@ impl Table {
html!("thead", {
.child(
html!("tr", {
.child(
html!("th", {
.attr("scope", "col")
.style("vertical-align", "bottom")
.style("font-weight", "bold")
.style("background-color", "#ddd")
.text("")
})
)
.children(Self::render_header_cells(this))
.child(
html!("th", {
Expand All @@ -75,6 +87,9 @@ impl Table {
html!("tr", {
.class(&*TABLE_ROW)
.children(&mut [
html!("td", {
.text(&format!("{}", index + 1))
}),
html!("td", {
.text(&record.company.name)
}),
Expand All @@ -94,13 +109,79 @@ impl Table {
.text(&record.quantity.to_string())
}),
html!("td", {
.text("100%")
.text(&record.percentage.to_string())
.text("%")
}),
html!("td" => HtmlElement, {
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this, record => move |_: events::Click| {
this.data.lock_mut().retain(|x| x != &record);
.event(clone!(this => move |_: events::Click| {
this.data.lock_mut().remove(index);
}))
})
}),
])
})
}

fn render_editable_row(this: &Arc<Self>, index: usize, record: &Aeat720Record) -> Dom {
let date = usize_to_date(record.first_tx_date)
.map_or("".to_string(), |d| d.format("%d/%m/%Y").to_string());

html!("tr", {
.class(&*TABLE_ROW)
.children(&mut [
html!("td", {
.text(&format!("{}", index + 1))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &record.company.name)
}))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &record.company.isin)
}))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &record.broker.country_code)
}))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &date)
}))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &record.value_in_euro.to_string())
}))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &record.quantity.to_string())
}))
}),
html!("td", {
.child(html!("input", {
.attr("type", "text")
.attr("value", &record.percentage.to_string())
}))
.text("%")
}),
html!("td" => HtmlElement, {
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
this.data.lock_mut().remove(index);
}))
})
}),
Expand All @@ -112,12 +193,25 @@ impl Table {
html!("tbody", {
.children_signal_vec(this.data.signal_vec_cloned()
.enumerate().map(clone!(this => move |(index, record)| {
Table::render_row(&this, index.get().unwrap_or(usize::MAX), &record)
if this.editable.get() {

Table::render_editable_row(&this, index.get().unwrap_or(usize::MAX), &record)
} else {
Table::render_row(&this, index.get().unwrap_or(usize::MAX), &record)
}
}))
)
})
}

fn is_needed_to_rerender_rows(this: &Arc<Self>) -> impl Signal<Item = bool> {
map_ref! {
let _editable_changed = this.editable.signal(),
let _records_len = this.data.signal_vec_cloned().to_signal_map(|x| x.len()) =>
true
}
}

pub fn render(this: &Arc<Self>) -> Dom {
html!("table", {
.style("overflow", "auto")
Expand All @@ -133,7 +227,11 @@ impl Table {

)
.child(Self::render_header(this))
.child(Self::render_body(this))
.child_signal(Self::is_needed_to_rerender_rows(this).map(
clone!(this => move |_x| {
Some(Self::render_body(&this))
}))
)
})
}
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::{Arc, LazyLock};

use anyhow::{bail, Result};
use chrono::NaiveDate;
use rust_decimal::Decimal;
use zip::read_zip;

use crate::{
Expand Down Expand Up @@ -111,6 +112,7 @@ fn transform_to_aeat720_records(notes: (BalanceNotes, AccountNotes)) -> Result<A
value_in_euro: note.value_in_euro,
first_tx_date,
broker: note.broker.clone(),
percentage: Decimal::new(100, 0),
})
}

Expand Down

1 comment on commit 522e0c3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.