use js_sys::Array;
use rust_decimal::prelude::*;
use wasm_bindgen::prelude::*;
use amfnengine::core::*;
use amfnengine::engine::*;
use amfnengine::*;
pub const APP_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
pub const TABLE_EVENT: u32 = 0;
pub const TABLE_AM: u32 = 1;
#[wasm_bindgen]
pub struct WasmParameter {
name: String,
label: String,
description: String,
sym_type: String,
int_value: i32,
dec_value: String,
str_value: String,
}
#[wasm_bindgen]
impl WasmParameter {
#[wasm_bindgen(skip)]
pub fn new(
name_param: &str,
label_param: &str,
desc_param: &str,
sym_type_param: &str,
int_value_param: i32,
dec_value_param: &str,
str_value_param: &str,
) -> WasmParameter {
WasmParameter {
name: String::from(name_param),
label: String::from(label_param),
description: String::from(desc_param),
sym_type: String::from(sym_type_param),
int_value: int_value_param,
dec_value: String::from(dec_value_param),
str_value: String::from(str_value_param),
}
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[wasm_bindgen(getter)]
pub fn label(&self) -> String {
self.label.clone()
}
#[wasm_bindgen(setter)]
pub fn set_label(&mut self, label: String) {
self.label = label;
}
#[wasm_bindgen(getter)]
pub fn description(&self) -> String {
self.description.clone()
}
#[wasm_bindgen(setter)]
pub fn set_description(&mut self, description: String) {
self.description = description;
}
#[wasm_bindgen(getter)]
pub fn sym_type(&self) -> String {
self.sym_type.clone()
}
#[wasm_bindgen(setter)]
pub fn set_sym_type(&mut self, sym_type: String) {
self.sym_type = sym_type;
}
#[wasm_bindgen(getter)]
pub fn int_value(&self) -> i32 {
self.int_value
}
#[wasm_bindgen(setter)]
pub fn set_int_value(&mut self, int_value: i32) {
self.int_value = int_value;
}
#[wasm_bindgen(getter)]
pub fn dec_value(&self) -> String {
self.dec_value.clone()
}
#[wasm_bindgen(setter)]
pub fn set_dec_value(&mut self, dec_value: String) {
self.dec_value = dec_value;
}
#[wasm_bindgen(getter)]
pub fn str_value(&self) -> String {
self.str_value.clone()
}
#[wasm_bindgen(setter)]
pub fn set_str_value(&mut self, str_value: String) {
self.str_value = str_value;
}
}
#[wasm_bindgen]
pub struct WasmDescriptor {
group: String,
name: String,
desc_type: String,
code: String,
value: String,
value_expr: String,
propagate: bool,
list_event_index: u32,
}
#[wasm_bindgen]
impl WasmDescriptor {
#[allow(clippy::too_many_arguments)]
#[wasm_bindgen(skip)]
pub fn new(
group_param: &str,
name_param: &str,
desc_type_param: &str,
code_param: &str,
value_param: &str,
value_expr_param: &str,
propagate_param: bool,
list_event_index_param: u32,
) -> WasmDescriptor {
WasmDescriptor {
group: String::from(group_param),
name: String::from(name_param),
desc_type: String::from(desc_type_param),
code: String::from(code_param),
value: String::from(value_param),
value_expr: String::from(value_expr_param),
propagate: propagate_param,
list_event_index: list_event_index_param,
}
}
#[wasm_bindgen(getter)]
pub fn group(&self) -> String {
self.group.clone()
}
#[wasm_bindgen(setter)]
pub fn set_group(&mut self, group: String) {
self.group = group;
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[wasm_bindgen(getter)]
pub fn desc_type(&self) -> String {
self.desc_type.clone()
}
#[wasm_bindgen(setter)]
pub fn set_desc_type(&mut self, desc_type: String) {
self.desc_type = desc_type;
}
#[wasm_bindgen(getter)]
pub fn code(&self) -> String {
self.code.clone()
}
#[wasm_bindgen(setter)]
pub fn set_code(&mut self, code: String) {
self.code = code;
}
#[wasm_bindgen(getter)]
pub fn value(&self) -> String {
self.value.clone()
}
#[wasm_bindgen(setter)]
pub fn set_value(&mut self, value: String) {
self.value = value;
}
#[wasm_bindgen(getter)]
pub fn value_expr(&self) -> String {
self.value_expr.clone()
}
#[wasm_bindgen(setter)]
pub fn set_value_expr(&mut self, value_expr: String) {
self.value_expr = value_expr;
}
#[wasm_bindgen(getter)]
pub fn propagate(&self) -> bool {
self.propagate
}
#[wasm_bindgen(setter)]
pub fn set_propagate(&mut self, propagate: bool) {
self.propagate = propagate;
}
#[wasm_bindgen(getter)]
pub fn list_event_index(&self) -> u32 {
self.list_event_index
}
#[wasm_bindgen(setter)]
pub fn set_list_event_index(&mut self, list_event_index: u32) {
self.list_event_index = list_event_index;
}
}
#[wasm_bindgen]
pub struct WasmElemChart {
name: String,
value: String,
}
#[wasm_bindgen]
impl WasmElemChart {
#[wasm_bindgen(skip)]
pub fn new(name_param: &str, value_param: &str) -> WasmElemChart {
WasmElemChart {
name: String::from(name_param),
value: String::from(value_param),
}
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[wasm_bindgen(getter)]
pub fn value(&self) -> String {
self.value.clone()
}
#[wasm_bindgen(setter)]
pub fn set_value(&mut self, value: String) {
self.value = value;
}
}
#[wasm_bindgen]
pub struct WasmElemColumn {
col_name: String,
col_name_index: u32,
col_header: String,
col_description: String,
group: String,
name: String,
col_type: String,
code: String,
format: u32,
decimal_digits: u32,
col_width: u32,
col_editable: bool,
}
#[wasm_bindgen]
impl WasmElemColumn {
#[allow(clippy::too_many_arguments)]
#[wasm_bindgen(skip)]
pub fn new(
col_name_param: &str,
col_name_index_param: u32,
col_header_param: &str,
col_description_param: &str,
group_param: &str,
name_param: &str,
col_type_param: &str,
code_param: &str,
format_param: u32,
decimal_digits_param: u32,
col_width_param: u32,
col_editable_param: bool,
) -> WasmElemColumn {
WasmElemColumn {
col_name: String::from(col_name_param),
col_name_index: col_name_index_param,
col_header: String::from(col_header_param),
col_description: String::from(col_description_param),
group: String::from(group_param),
name: String::from(name_param),
col_type: String::from(col_type_param),
code: String::from(code_param),
format: format_param,
decimal_digits: decimal_digits_param,
col_width: col_width_param,
col_editable: col_editable_param,
}
}
#[wasm_bindgen(getter)]
pub fn col_name(&self) -> String {
self.col_name.clone()
}
#[wasm_bindgen(setter)]
pub fn set_col_name(&mut self, col_name: String) {
self.col_name = col_name;
}
#[wasm_bindgen(getter)]
pub fn col_name_index(&self) -> u32 {
self.col_name_index
}
#[wasm_bindgen(setter)]
pub fn set_col_name_index(&mut self, col_name_index: u32) {
self.col_name_index = col_name_index;
}
#[wasm_bindgen(getter)]
pub fn col_header(&self) -> String {
self.col_header.clone()
}
#[wasm_bindgen(setter)]
pub fn set_col_header(&mut self, col_header: String) {
self.col_header = col_header;
}
#[wasm_bindgen(getter)]
pub fn col_description(&self) -> String {
self.col_description.clone()
}
#[wasm_bindgen(setter)]
pub fn set_col_description(&mut self, col_description: String) {
self.col_description = col_description;
}
#[wasm_bindgen(getter)]
pub fn group(&self) -> String {
self.group.clone()
}
#[wasm_bindgen(setter)]
pub fn set_group(&mut self, group: String) {
self.group = group;
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[wasm_bindgen(getter)]
pub fn col_type(&self) -> String {
self.col_type.clone()
}
#[wasm_bindgen(setter)]
pub fn set_col_type(&mut self, col_type: String) {
self.col_type = col_type;
}
#[wasm_bindgen(getter)]
pub fn code(&self) -> String {
self.code.clone()
}
#[wasm_bindgen(setter)]
pub fn set_code(&mut self, code: String) {
self.code = code;
}
#[wasm_bindgen(getter)]
pub fn format(&self) -> u32 {
self.format
}
#[wasm_bindgen(setter)]
pub fn set_format(&mut self, format: u32) {
self.format = format;
}
#[wasm_bindgen(getter)]
pub fn decimal_digits(&self) -> u32 {
self.decimal_digits
}
#[wasm_bindgen(setter)]
pub fn set_decimal_digits(&mut self, decimal_digits: u32) {
self.decimal_digits = decimal_digits;
}
#[wasm_bindgen(getter)]
pub fn col_width(&self) -> u32 {
self.col_width
}
#[wasm_bindgen(setter)]
pub fn set_col_width(&mut self, col_width: u32) {
self.col_width = col_width;
}
#[wasm_bindgen(getter)]
pub fn col_editable(&self) -> bool {
self.col_editable
}
#[wasm_bindgen(setter)]
pub fn set_col_editable(&mut self, col_editable: bool) {
self.col_editable = col_editable;
}
}
#[wasm_bindgen]
pub struct WasmElemPreferences {
locale_str: String,
group: String,
cross_rate_code: String,
default_encoding: String,
fiscal_year_start: u32,
decimal_digits: u32,
target: String,
}
#[wasm_bindgen]
impl WasmElemPreferences {
#[wasm_bindgen(constructor)]
pub fn new(
locale_str_param: &str,
group_param: &str,
cross_rate_code_param: &str,
default_encoding_param: &str,
fiscal_year_start_param: u32,
decimal_digits_param: u32,
target_param: &str,
) -> WasmElemPreferences {
WasmElemPreferences {
locale_str: String::from(locale_str_param),
group: String::from(group_param),
cross_rate_code: String::from(cross_rate_code_param),
default_encoding: String::from(default_encoding_param),
fiscal_year_start: fiscal_year_start_param,
decimal_digits: decimal_digits_param,
target: String::from(target_param),
}
}
#[wasm_bindgen(getter)]
pub fn locale_str(&self) -> String {
self.locale_str.clone()
}
#[wasm_bindgen(setter)]
pub fn set_locale_str(&mut self, locale_str: String) {
self.locale_str = locale_str;
}
#[wasm_bindgen(getter)]
pub fn group(&self) -> String {
self.group.clone()
}
#[wasm_bindgen(setter)]
pub fn set_group(&mut self, group: String) {
self.group = group;
}
#[wasm_bindgen(getter)]
pub fn cross_rate_code(&self) -> String {
self.cross_rate_code.clone()
}
#[wasm_bindgen(setter)]
pub fn set_cross_rate_code(&mut self, cross_rate_code: String) {
self.cross_rate_code = cross_rate_code;
}
#[wasm_bindgen(getter)]
pub fn default_encoding(&self) -> String {
self.default_encoding.clone()
}
#[wasm_bindgen(setter)]
pub fn set_default_encoding(&mut self, default_encoding: String) {
self.default_encoding = default_encoding;
}
#[wasm_bindgen(getter)]
pub fn fiscal_year_start(&self) -> u32 {
self.fiscal_year_start
}
#[wasm_bindgen(setter)]
pub fn set_fiscal_year_start(&mut self, fiscal_year_start: u32) {
self.fiscal_year_start = fiscal_year_start;
}
#[wasm_bindgen(getter)]
pub fn decimal_digits(&self) -> u32 {
self.decimal_digits
}
#[wasm_bindgen(setter)]
pub fn set_decimal_digits(&mut self, decimal_digits: u32) {
self.decimal_digits = decimal_digits;
}
#[wasm_bindgen(getter)]
pub fn target(&self) -> String {
self.target.clone()
}
#[wasm_bindgen(setter)]
pub fn set_target(&mut self, target: String) {
self.target = target;
}
}
#[wasm_bindgen]
pub struct WasmElemSummary {
name: String,
label: String,
label_expr: String,
result: String,
result_expr: String,
}
#[wasm_bindgen]
impl WasmElemSummary {
#[wasm_bindgen(skip)]
pub fn new(
name_param: &str,
label_param: &str,
label_expr_param: &str,
result_param: &str,
result_expr_param: &str,
) -> WasmElemSummary {
WasmElemSummary {
name: String::from(name_param),
label: String::from(label_param),
label_expr: String::from(label_expr_param),
result: String::from(result_param),
result_expr: String::from(result_expr_param),
}
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
#[wasm_bindgen(getter)]
pub fn label(&self) -> String {
self.label.clone()
}
#[wasm_bindgen(setter)]
pub fn set_label(&mut self, label: String) {
self.label = label;
}
#[wasm_bindgen(getter)]
pub fn label_expr(&self) -> String {
self.label_expr.clone()
}
#[wasm_bindgen(setter)]
pub fn set_label_expr(&mut self, label_expr: String) {
self.label_expr = label_expr;
}
#[wasm_bindgen(getter)]
pub fn result(&self) -> String {
self.result.clone()
}
#[wasm_bindgen(setter)]
pub fn set_result(&mut self, result: String) {
self.result = result;
}
#[wasm_bindgen(getter)]
pub fn result_expr(&self) -> String {
self.result_expr.clone()
}
#[wasm_bindgen(setter)]
pub fn set_result_expr(&mut self, result_expr: String) {
self.result_expr = result_expr;
}
}
#[wasm_bindgen]
pub struct Engine {
engine: CalcEngine,
initialized: bool,
}
impl Default for Engine {
fn default() -> Self {
Engine::new()
}
}
#[wasm_bindgen]
impl Engine {
#[wasm_bindgen(constructor)]
pub fn new() -> Engine {
let eng = CalcEngine::new();
Engine {
engine: eng,
initialized: false,
}
}
pub fn init_engine(&mut self) -> String {
let mut encoding = String::from(amfnengine::DEFAULT_ENCODING);
let mut decimal_digits = amfnengine::DEFAULT_DECIMAL_DIGITS;
if self.initialized {
return String::from("");
}
let locale_str = String::from(self.engine.calc_mgr().preferences().locale_str());
if !self
.engine
.calc_mgr()
.preferences()
.default_encoding()
.is_empty()
{
encoding = String::from(self.engine.calc_mgr().preferences().default_encoding());
}
if !self.engine.calc_mgr().preferences().decimal_digits() > 0 {
decimal_digits = self.engine.calc_mgr().preferences().decimal_digits();
}
self.engine.init_engine(locale_str.as_str());
self.initialized = true;
format!("{}|{}|{}", locale_str, encoding, decimal_digits)
}
pub fn get_engine_version(&self) -> String {
match amfnengine::APP_VERSION {
None => String::from(""),
Some(o) => String::from(o),
}
}
pub fn get_wasm_version(&self) -> String {
match APP_VERSION {
None => String::from(""),
Some(o) => String::from(o),
}
}
pub fn calculate_value(&self, cf_index: i32, index: u32) -> String {
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return String::from("");
}
match calc_mgr.list_cashflow().list_event() {
None => {
return String::from("");
}
Some(o) => {
if !o.get_element(index as usize) {
return String::from("");
}
}
}
}
match self.engine.calculate_value() {
Err(_e) => String::from(""),
Ok(o) => o.result_decimal().to_string(),
}
}
pub fn calculate_periods(&self, cf_index: i32, index: u32) -> i32 {
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return 0;
}
match calc_mgr.list_cashflow().list_event() {
None => {
return 0;
}
Some(o) => {
if !o.get_element(index as usize) {
return 0;
}
}
}
}
match self.engine.calculate_periods() {
Err(_e) => 0,
Ok(o) => o.result_integer(),
}
}
pub fn create_template_events(
&self,
group_param: &str,
event_param: &str,
cf_index: i32,
) -> String {
match self
.engine
.create_template_events(group_param, event_param, cf_index as usize)
{
Err(_e) => String::from(""),
Ok(o) => {
let mut events = String::from("");
let orig_index = o.index();
let mut index: usize = 0;
loop {
if !o.get_element(index) {
break;
}
if !events.is_empty() {
events.push('|');
}
let new_date = o.event_date();
events.push_str(
format!(
"{:04}-{:02}-{:02}",
new_date / 10000,
new_date / 100 % 100,
new_date % 100
)
.as_str(),
);
events.push('~');
events.push_str(o.sort_order().to_string().as_str());
events.push('~');
let param_count: usize = match o.list_parameter() {
None => 0,
Some(o) => o.count(),
};
events.push_str(param_count.to_string().as_str());
index += 1;
}
o.get_element(orig_index);
events
}
}
}
pub fn create_cashflow_from_template_group(
&self,
group_param: &str,
new_name_param: &str,
) -> String {
match self.engine.create_cashflow_from_template_group(
group_param,
new_name_param,
group_param,
) {
Err(_e) => String::from(""),
Ok(_o) => {
let mut initial_name = String::from("*");
let calc_mgr = self.engine.calc_mgr();
let list_template_event = calc_mgr.list_template_group().list_template_event();
let orig_index = list_template_event.index();
let mut index: usize = 0;
loop {
if !list_template_event.get_element(index) {
break;
}
if list_template_event.initial_event() {
initial_name = String::from(list_template_event.name());
break;
}
index += 1;
}
list_template_event.get_element(orig_index);
initial_name
}
}
}
pub fn date_diff(
&self,
date1: &str,
date2: &str,
frequency: &str,
intervals: u32,
eom_param: bool,
) -> i32 {
let freq = CoreUtility::get_frequency(frequency);
CoreUtility::date_diff(
CoreUtility::parse_date(date1),
CoreUtility::parse_date(date2),
freq,
intervals as usize,
eom_param,
) as i32
}
pub fn date_new(
&self,
date_orig: &str,
date_param: &str,
frequency: &str,
intervals: i32,
eom_param: bool,
) -> String {
let freq = CoreUtility::get_frequency(frequency);
let new_date = CoreUtility::date_newi(
CoreUtility::parse_date(date_orig),
CoreUtility::parse_date(date_param),
freq,
intervals,
eom_param,
);
self.engine.format_date_out(new_date)
}
pub fn format_date_in(&self, display_val: &str) -> String {
self.engine.format_date_in(display_val)
}
pub fn format_integer_in(&self, display_val: &str) -> String {
self.engine.format_integer_in(display_val)
}
pub fn format_decimal_in(&self, display_val: &str) -> String {
self.engine.format_decimal_in(display_val)
}
pub fn format_currency_in(&self, display_val: &str) -> String {
self.engine.format_currency_in(display_val)
}
pub fn format_date_out(&self, val: &str) -> String {
self.engine.format_date_out(CoreUtility::parse_date(val))
}
pub fn format_integer_out(&self, val: u32) -> String {
self.engine.format_integer_out(val as usize)
}
pub fn format_decimal_out(&self, val: &str) -> String {
match val.parse::<Decimal>() {
Err(_e) => String::from("0.0"),
Ok(o) => self.engine.format_decimal_out(o),
}
}
pub fn format_currency_out(&self, val: &str) -> String {
match val.parse::<Decimal>() {
Err(_e) => String::from("0.0"),
Ok(o) => self.engine.format_currency_out(o),
}
}
pub fn clear_lists(&self) {
self.engine
.calc_mgr()
.list_locale()
.select_cashflow_locale("");
self.engine.calc_mgr_mut().list_cashflow_mut().clear();
self.engine.calc_mgr_mut().list_template_group_mut().clear();
}
pub fn deserialize(&self, json_input: &str) -> String {
let json = CalcJsonDeserialize::new(self.engine.calc_manager());
match json.deserialize(String::from(json_input)) {
Err(_e) => {
return String::from("Json error");
}
Ok(_o) => {}
}
String::from("")
}
pub fn get_cashflow_status(&self, cf_index: i32, status: &str) -> String {
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return String::from("");
}
let mut list_parameter: Option<&ListParameter> = None;
match calc_mgr.list_cashflow().preferences() {
None => {}
Some(o) => {
list_parameter = Option::from(o.list_parameter());
}
}
let result_symbol = self
.engine
.evaluate_expression(list_parameter, status, true);
match result_symbol.sym_type() {
amfnengine::TokenType::Integer => {
self.engine.format_integer_out(result_symbol.sym_integer())
}
amfnengine::TokenType::Decimal => {
self.engine.format_decimal_out(result_symbol.sym_decimal())
}
_ => String::from(result_symbol.sym_string()),
}
}
pub fn get_chart_definitions(&self, cf_index: i32) -> Array {
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return Array::new();
}
let mut ary_chart: Vec<WasmElemChart> = Vec::new();
let list_descriptor = calc_mgr.preferences().list_descriptor();
let mut index: usize = 0;
loop {
if !list_descriptor.get_element(index) {
break;
}
if list_descriptor.group() == "Chart" && list_descriptor.desc_type() == "custom" {
ary_chart.push(WasmElemChart::new(
list_descriptor.name(),
list_descriptor.value().as_str(),
));
}
index += 1;
}
match calc_mgr.list_cashflow().preferences() {
None => {}
Some(o) => {
let list_descriptor = o.list_descriptor();
let mut index: usize = 0;
loop {
if !list_descriptor.get_element(index) {
break;
}
if list_descriptor.group() == "Chart" && list_descriptor.desc_type() == "custom"
{
ary_chart.push(WasmElemChart::new(
list_descriptor.name(),
list_descriptor.value().as_str(),
));
}
index += 1;
}
}
}
ary_chart.into_iter().map(JsValue::from).collect()
}
pub fn get_event_by_date(&self, cf_index: i32, date_param: &str, sort_param: u32) -> u32 {
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return 0;
}
let date_in = CoreUtility::parse_date(self.engine.format_date_in(date_param).as_str());
match calc_mgr.list_cashflow().list_event() {
None => 0,
Some(o) => {
if !o.get_element_by_date(date_in, sort_param as usize) {
return 0;
}
o.index() as u32
}
}
}
pub fn get_preferences(&self, cf_index: i32) -> WasmElemPreferences {
let calc_mgr = self.engine.calc_mgr();
let prefs: &ElemPreferences;
if cf_index >= 0 {
calc_mgr.list_cashflow().get_element(cf_index as usize);
match calc_mgr.list_cashflow().preferences() {
None => {
prefs = calc_mgr.preferences();
}
Some(o) => {
prefs = o;
}
}
} else {
prefs = calc_mgr.preferences();
}
WasmElemPreferences::new(
prefs.locale_str(),
prefs.group(),
prefs.cross_rate_code(),
prefs.default_encoding(),
prefs.fiscal_year_start() as u32,
prefs.decimal_digits() as u32,
prefs.target().to_string().as_str(),
)
}
pub fn get_resource(&self, cf_index: i32, key: &str) -> String {
let calc_mgr = self.engine.calc_mgr();
if cf_index >= 0 {
calc_mgr.list_cashflow().get_element(cf_index as usize);
}
let locale = calc_mgr.list_locale();
String::from(locale.get_resource(key))
}
pub fn get_template_names(&self) -> Array {
let calc_mgr = self.engine.calc_mgr();
let list_template_group = calc_mgr.list_template_group();
let mut ary_template_groups: Vec<String> = Vec::new();
let orig_index = list_template_group.index();
let mut index: usize = 0;
loop {
if !list_template_group.get_element(index) {
break;
}
ary_template_groups.push(String::from(list_template_group.group()));
index += 1;
}
list_template_group.get_element(orig_index);
ary_template_groups.into_iter().map(JsValue::from).collect()
}
pub fn get_template_event_names(&self, group_param: &str) -> Array {
let calc_mgr = self.engine.calc_mgr();
let list_template_group = calc_mgr.list_template_group();
let mut ary_template_events: Vec<String> = Vec::new();
if !list_template_group.get_element_by_group(group_param, true) {
return ary_template_events.into_iter().map(JsValue::from).collect();
}
let list_template_event = list_template_group.list_template_event();
let orig_index = list_template_event.index();
let mut index: usize = 0;
loop {
if !list_template_event.get_element(index) {
break;
}
ary_template_events.push(String::from(list_template_event.name()));
index += 1;
}
list_template_event.get_element(orig_index);
ary_template_events.into_iter().map(JsValue::from).collect()
}
pub fn init_cashflow(&self, cf_index: i32) -> String {
if cf_index < 0 {
return String::from("");
}
if !self.engine.init_cashflow(cf_index as u32) {
return String::from("");
}
let calc_mgr = self.engine.calc_mgr();
let locale_str: &str;
let group: &str;
match calc_mgr.list_cashflow().preferences() {
None => {
locale_str = calc_mgr.preferences().locale_str();
group = "";
}
Some(o) => {
if o.locale_str().is_empty() {
locale_str = calc_mgr.preferences().locale_str();
} else {
locale_str = o.locale_str();
}
group = o.group();
}
}
format!(
"{}|{}|{}",
calc_mgr.list_cashflow().name(),
locale_str,
group
)
}
pub fn init_cashflow_status(&self, cf_index: i32) -> String {
if cf_index < 0 {
return String::from("");
}
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return String::from("");
}
let locale = calc_mgr.list_locale();
let cashflow_locale_str = locale.cashflow_locale().locale_str();
let mut status = self.engine.calc_mgr().descriptor_value(
amfnengine::GROUP_GENERAL,
amfnengine::NAME_STATUS,
amfnengine::TYPE_LOCALE,
cashflow_locale_str,
true,
false,
);
if status.is_empty() {
status = self.engine.calc_mgr().descriptor_value(
amfnengine::GROUP_GENERAL,
amfnengine::NAME_STATUS,
"",
"",
true,
false,
);
if status.is_empty() {
status = String::from(locale.get_resource(amfnengine::USER_STATUS));
}
}
status
}
pub fn parse_columns(&self, cf_index: i32, table_type_param: u32) -> Array {
if cf_index < 0 {
return Array::new();
}
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return Array::new();
}
let table_type: TableType = match table_type_param {
TABLE_AM => TableType::Amortization,
_ => TableType::Event,
};
let mut ary_column: Vec<WasmElemColumn> = Vec::new();
let list_column = self.engine.parse_columns(table_type);
let mut index: usize = 0;
loop {
if !list_column.get_element(index) {
break;
}
ary_column.push(WasmElemColumn::new(
list_column.col_name(),
list_column.col_name_index() as u32,
list_column.col_header(),
list_column.col_description(),
list_column.group(),
list_column.name(),
list_column.col_type(),
list_column.col_name(),
list_column.format() as u32,
list_column.decimal_digits() as u32,
list_column.column_width() as u32,
list_column.column_editable(),
));
index += 1;
}
ary_column.into_iter().map(JsValue::from).collect()
}
pub fn parse_descriptors(&self, cf_index: i32, index: u32, table_type_param: u32) -> Array {
if cf_index < 0 {
return Array::new();
}
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return Array::new();
}
match table_type_param {
TABLE_AM => match calc_mgr.list_cashflow().list_amortization() {
None => Array::new(),
Some(o) => {
if !o.get_element(index as usize) {
return Array::new();
}
match o.list_descriptor() {
None => Array::new(),
Some(o2) => {
let mut list: Vec<WasmDescriptor> = Vec::new();
let orig_index = o2.index();
let mut index: usize = 0;
loop {
if !o2.get_element(index) {
break;
}
list.push(WasmDescriptor::new(
o2.group(),
o2.name(),
o2.desc_type(),
o2.code(),
o2.value().as_str(),
o2.value_expr().as_str(),
o2.propagate(),
o2.list_event_index() as u32,
));
index += 1;
}
o2.get_element(orig_index);
list.into_iter().map(JsValue::from).collect()
}
}
}
},
_ => match calc_mgr.list_cashflow().list_event() {
None => Array::new(),
Some(o) => {
if !o.get_element(index as usize) {
return Array::new();
}
match o.list_descriptor() {
None => Array::new(),
Some(o2) => {
let mut list: Vec<WasmDescriptor> = Vec::new();
let orig_index = o2.index();
let mut index: usize = 0;
loop {
if !o2.get_element(index) {
break;
}
list.push(WasmDescriptor::new(
o2.group(),
o2.name(),
o2.desc_type(),
o2.code(),
o2.value().as_str(),
o2.value_expr().as_str(),
o2.propagate(),
o2.list_event_index() as u32,
));
index += 1;
}
o2.get_element(orig_index);
list.into_iter().map(JsValue::from).collect()
}
}
}
},
}
}
pub fn parse_parameters(&self, cf_index: i32, index: u32, table_type_param: u32) -> Array {
if cf_index < 0 {
return Array::new();
}
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return Array::new();
}
match table_type_param {
TABLE_AM => match calc_mgr.list_cashflow().list_amortization() {
None => Array::new(),
Some(o) => {
if !o.get_element(index as usize) {
return Array::new();
}
match o.list_parameter() {
None => Array::new(),
Some(o2) => {
let mut list: Vec<WasmParameter> = Vec::new();
let orig_index = o2.index();
let mut index: usize = 0;
loop {
if !o2.get_element(index) {
break;
}
list.push(WasmParameter::new(
o2.name(),
o2.label(),
o2.description(),
CoreUtility::get_param_type(o2.param_type()).as_str(),
o2.param_integeri(),
o2.param_decimal().to_string().as_str(),
o2.param_string(),
));
index += 1;
}
o2.get_element(orig_index);
list.into_iter().map(JsValue::from).collect()
}
}
}
},
_ => match calc_mgr.list_cashflow().list_event() {
None => Array::new(),
Some(o) => {
if !o.get_element(index as usize) {
return Array::new();
}
match o.list_parameter() {
None => Array::new(),
Some(o2) => {
let mut list: Vec<WasmParameter> = Vec::new();
let orig_index = o2.index();
let mut index: usize = 0;
loop {
if !o2.get_element(index) {
break;
}
list.push(WasmParameter::new(
o2.name(),
o2.label(),
o2.description(),
CoreUtility::get_param_type(o2.param_type()).as_str(),
o2.param_integeri(),
o2.param_decimal().to_string().as_str(),
o2.param_string(),
));
index += 1;
}
o2.get_element(orig_index);
list.into_iter().map(JsValue::from).collect()
}
}
}
},
}
}
pub fn parse_summary(&self, cf_index: i32) -> Array {
if cf_index < 0 {
return Array::new();
}
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return Array::new();
}
let mut ary_summary: Vec<WasmElemSummary> = Vec::new();
let list_summary = self.engine.parse_summary();
let mut index: usize = 0;
loop {
if !list_summary.get_element(index) {
break;
}
ary_summary.push(WasmElemSummary::new(
list_summary.name(),
list_summary.label(),
list_summary.label_expr(),
list_summary.result(),
list_summary.result_expr(),
));
index += 1;
}
ary_summary.into_iter().map(JsValue::from).collect()
}
pub fn remove_cashflow(&self, cf_index: i32) -> bool {
if cf_index < 0 {
return false;
}
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return false;
}
}
self.engine.calc_mgr_mut().list_cashflow_mut().remove()
}
pub fn remove_event(&self, cf_index: i32, index: u32) -> bool {
let mut result: bool = false;
if cf_index < 0 {
return result;
}
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return result;
}
match calc_mgr.list_cashflow().list_event() {
None => {
return result;
}
Some(o) => {
if !o.get_element(index as usize) {
return result;
}
}
}
}
match self
.engine
.calc_mgr_mut()
.list_cashflow_mut()
.list_event_mut()
{
None => return result,
Some(o) => {
result = o.remove();
}
}
match self.engine.balance_cashflow() {
Err(_e) => {}
Ok(_o) => {}
}
result
}
pub fn serialize(&self, cf_index: i32, options: u32) -> String {
if cf_index < 0 {
return String::from("");
}
if !self
.engine
.calc_mgr()
.list_cashflow()
.get_element(cf_index as usize)
{
return String::from("");
}
let json = CalcJsonSerialize::new(self.engine.calc_manager());
json.serialize(options as usize)
}
pub fn set_event_value(
&self,
col_name_index_param: u32,
type_param: &str,
code_param: &str,
cf_index_param: i32,
index_param: u32,
value_param: &str,
) -> String {
if cf_index_param < 0 {
return String::from("");
}
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr
.list_cashflow()
.get_element(cf_index_param as usize)
{
return String::from("");
}
match calc_mgr.list_cashflow().list_event() {
None => {
return String::from("");
}
Some(o) => {
o.get_element(index_param as usize);
}
}
}
let mut event_date = String::from("");
let mut sort_order: usize = 0;
{
let calc_mgr = self.engine.calc_mgr();
let list_locale = calc_mgr.list_locale();
match calc_mgr.list_cashflow().list_event() {
None => {}
Some(o) => {
event_date = list_locale.format_date_out(o.event_date());
sort_order = o.sort_order();
}
}
}
let result = self.engine.set_event_value(
col_name_index_param as usize,
type_param,
code_param,
index_param as usize,
value_param,
);
match self.engine.balance_cashflow() {
Err(_e) => String::from(""),
Ok(_o) => format!("{}|{}|{}", event_date, sort_order, result),
}
}
pub fn set_extension_values(
&self,
cf_index_param: i32,
index_param: u32,
ext_param: &str,
) -> String {
if cf_index_param < 0 {
return String::from("");
}
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr
.list_cashflow()
.get_element(cf_index_param as usize)
{
return String::from("");
}
}
let ext: ElemExtension;
{
let json = CalcJsonDeserialize::new(self.engine.calc_manager());
match json.deserialize_extension_from_str(ext_param) {
Err(_e) => {
return String::from("");
}
Ok(o) => {
ext = o;
}
}
}
if !self.engine.set_extension_values(index_param as usize, &ext) {
return String::from("");
}
self.engine.evaluate_cashflow_event_type_all();
let mut result = String::from("");
{
let calc_mgr = self.engine.calc_mgr();
let list_cashflow = calc_mgr.list_cashflow();
let list_event_opt = list_cashflow.list_event();
match list_event_opt {
None => {}
Some(o) => {
let orig_index = o.index();
if o.get_element(index_param as usize) {
result = String::from(o.event_type());
o.get_element(orig_index);
}
}
}
}
match self.engine.balance_cashflow() {
Err(_e) => String::from(""),
Ok(_o) => result,
}
}
pub fn set_parameter_values(&self, cf_index: i32, index_param: u32, parameters: &str) -> bool {
if cf_index < 0 {
return false;
}
{
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return false;
}
}
let mut values: Vec<String> = Vec::new();
for param in parameters.split('|') {
values.push(String::from(param));
}
if !self
.engine
.set_parameter_values(index_param as usize, values)
{
return false;
}
true
}
pub fn set_preferences(&self, cf_index: i32, prefs: &WasmElemPreferences) -> bool {
if cf_index >= 0 {
let calc_mgr = self.engine.calc_mgr();
calc_mgr.list_cashflow().get_element(cf_index as usize);
}
let mut calc_mgr = self.engine.calc_mgr_mut();
let elem_prefs: &mut ElemPreferences;
if cf_index >= 0 {
match calc_mgr.list_cashflow_mut().preferences_mut() {
None => {
elem_prefs = calc_mgr.preferences_mut();
}
Some(o) => {
elem_prefs = o;
}
}
} else {
elem_prefs = calc_mgr.preferences_mut();
}
elem_prefs.set_cross_rate_code(prefs.cross_rate_code().as_str());
elem_prefs.set_default_encoding(prefs.default_encoding().as_str());
elem_prefs.set_fiscal_year_start(prefs.fiscal_year_start() as usize);
elem_prefs.set_decimal_digits(prefs.decimal_digits() as usize);
elem_prefs.set_target(CoreUtility::parse_decimal(prefs.target().as_str()));
true
}
pub fn table_values(&self, cf_index: i32, table_type_param: u32) -> String {
if cf_index < 0 {
return String::from("");
}
let calc_mgr = self.engine.calc_mgr();
if !calc_mgr.list_cashflow().get_element(cf_index as usize) {
return String::from("");
}
let table_type: TableType = match table_type_param {
TABLE_AM => TableType::Amortization,
_ => TableType::Event,
};
let list_column = self.engine.parse_columns(table_type);
let json = CalcJsonSerialize::new(self.engine.calc_manager());
match table_type_param {
TABLE_AM => {
let mut cresult = String::from("");
let mut list_am: ListAmortization = match calc_mgr
.list_cashflow()
.create_cashflow_output(true, false, false, false, true)
{
Err(_e) => return cresult,
Ok(o) => o,
};
let mut row_index: usize = 0;
loop {
if !list_am.get_element(row_index as usize) {
break;
}
let mut row = json.serialize_extension(
list_am.elem_extension(),
list_am.value(),
list_am.frequency(),
false,
true,
);
let orig_index = list_column.index();
let mut index: usize = 0;
loop {
if !list_column.get_element(index) {
break;
}
let val = self.engine.am_value(list_column.column(), &list_am);
row = format!("{},\"{}\":\"{}\"", row, list_column.col_name(), val);
index += 1;
}
list_column.get_element(orig_index);
let delimiter = if row_index == 0 { "" } else { "," };
cresult = format!("{}{}{{{}}}", cresult, delimiter, row);
row_index += 1;
}
cresult = format!("\"compressed\": [{}]", cresult);
let mut eresult = String::from("");
match calc_mgr
.list_cashflow()
.create_cashflow_output(false, false, false, false, true)
{
Err(_e) => {
return eresult;
}
Ok(o) => {
list_am = o;
}
}
row_index = 0;
loop {
if !list_am.get_element(row_index as usize) {
break;
}
let mut row = json.serialize_extension(
list_am.elem_extension(),
list_am.value(),
list_am.frequency(),
false,
true,
);
let orig_index = list_column.index();
let mut index: usize = 0;
loop {
if !list_column.get_element(index) {
break;
}
let val = self.engine.am_value(list_column.column(), &list_am);
row = format!("{},\"{}\":\"{}\"", row, list_column.col_name(), val);
index += 1;
}
list_column.get_element(orig_index);
let delimiter = if row_index == 0 { "" } else { "," };
eresult = format!("{}{}{{{}}}", eresult, delimiter, row);
row_index += 1;
}
eresult = format!("\"expanded\": [{}]", eresult);
format!("{{{},{}}}", cresult, eresult)
}
_ => {
let mut result = String::from("");
let mut row_index: usize = 0;
loop {
let mut row = String::from("");
let mut next_name = String::from("");
match calc_mgr.list_cashflow().list_event() {
None => {}
Some(o) => {
if !o.get_element(row_index as usize) {
break;
}
row = json.serialize_extension(
o.elem_extension(),
dec!(0.0),
o.frequency(),
false,
true,
);
next_name = String::from(o.next_name());
}
}
let mut next_name_seen = false;
let orig_index = list_column.index();
let mut index: usize = 0;
loop {
if !list_column.get_element(index) {
break;
}
let val = self.engine.event_value(list_column.column());
row = format!("{},\"{}\":\"{}\"", row, list_column.col_name(), val);
if !next_name_seen {
next_name_seen = list_column.col_name() == "Next-name";
}
index += 1;
}
list_column.get_element(orig_index);
if !next_name_seen {
row = format!("{},\"{}\":\"{}\"", row, "Next-name", next_name);
}
let delimiter = if row_index == 0 { "" } else { "," };
result = format!("{}{}{{{}}}", result, delimiter, row);
row_index += 1;
}
format!("[{}]", result)
}
}
}
}