Comments (3)
- A new file shall be created with name js/get_print_data.js to get the data from the forms and printing the tables. This file shall have functions which return an object whose content is the content of the forms
- There shall be three getting functions for each form, get_form_part1(), get_form_part2() and get_form_part3()
- The function calcula_custos_auto() shall be renamed to calculate_costs() and shall be the "black box". This new function shall be changed drastically. This function shall have as input 3 objects with the forms parts data, and shall output an object, with 3 (4 for PT) other objects for each table results. In the first table/first output object we shall have an object whose fields are the monthly costs. For the other tables the same principle applies, the object fields shall have the numbers which appear in the table.
- Finally, we shall have print functions, print_costs_table(), print_extern_table(), print_publict_table() and print_feffort_table() which get data from the objects and return a string to be printed
==CODE==
The file js/get_print_data.js shall be something like
function get_form_part1() {
form1_data = new Object();
//insurance
form1_d.insurance_type = getCheckedValue(document.custo.tipo_seguro);
form1_d.insurance_value = document.custo.seguro_val.value;
..
..
return form1_data;
}
We'll need to include a line in index.php, around line 70, after reference to autocostsCore.js
<script type="text/javascript" src=js/get_print_data.js"></script>
Before we call calculate_costs(), we shall call our check data and object data getting functions; and then with its returning object, we print the tables
if (!is_userdata_formpart1_ok()) return;
if (!is_userdata_formpart2_ok()) return;
if (!is_userdata_formpart3_ok()) return;
var f1 = get_form_part1();
var f2 = get_form_part2();
var f3 = get_form_part3();
var data=calculate_costs(f1, f2, f3); //the "business_logic", the core function, the "black box"
result_object = document.getElementById('result_div');
var tables_HTML = "";
tables_HTML+= print_costs_table(f1, f2, f3, data.monthly_costs);
.
result_object.innerHTML = tables_HTML;
.
Then in businessLogic_js.php the function calculate_costs() shall be the "black box", having the forms objects as input and outputting the table objects in one global object named output
function calculate_costs(f1, f2, f3){
output = new Object ();
monthly_costs = new Object();
external_costs = new Object();
public_transports = new Object();
//************* INSURANCE ********************************
var tipo_seguro_auto=f1.tipo_seguro_auto;
var val_seguro_por_mes;
val_seguro_por_mes = calculateInsuranceMonthlyValue(tipo_seguro_auto, f1.seguro_val.value);
monthly_costs.insurance = val_seguro_por_mes;
..
..
output.monthly_costs = monthly_costs;
..
return output;
}
The print tables functions are in the file js/get_print_data.js
function print_costs_table(f1, f2, f3, costs) {
var insurance_text;
switch(f1.tipo_seguro_auto)
{
case "semestral":
insurance_text=f1.seguro_val + " <?echo $CURR_NAME_PLURAL?> <?echo $WORD_PER?> <?echo $SEMESTER?>";
break;
case "anual":
insurance_text=f1.seguro_val + " <?echo $CURR_NAME_PLURAL?> <?echo $WORD_PER?> <?echo $YEAR?>";
break;
case "mensal":
insurance_text=costs.insurance + " <?echo $CURR_NAME_PLURAL?> <?echo $WORD_PER?> <?echo $MONTH?>";
break;
case "trimestral":
insurance_text=f1.seguro_val + " <?echo $CURR_NAME_PLURAL?> <?echo $WORD_PER?> <?echo $TRIMESTER?>";
break;
}
.
.
var varResult="";
//main table
varResult+="<center><table class=\"result_table\" border=\"1\" cellpadding=\"4\">";
//header
varResult+="<tr><td align=\"center\"><b><span class=\"p3\"><?echo $PRIVATE_COSTS?><\/span><\/b><br><\/td><td width=\"20%\" align=\"center\"><b><span class=\"p3\"><?echo $MONTHLY_AMOUNT?><\/span><\/b><\/td><\/tr>";
//cost items
varResult+="<tr><td align=\"left\">"+desva_text+" <\/td>" + "<td> <span class=\"p2\"><?echo $CURR_SYMBOL?> "+costs.depreciation.toFixed(1)+"<\/span><\/td><\/tr>";
varResult+="<tr><td align=\"left\"><b><span class=\"p3\"><?echo $INSURANCE?><\/span><\/b><br><span class=\"p2\">"+insurance_text+"<\/span><\/td>"+"<td> <span class=\"p2\"><?echo $CURR_SYMBOL?> "+costs.insurance.toFixed(1)+"<\/span><\/td><\/tr>";
.
.
return varResult;
}
from autocosts.
We'd like to make all files pure JS as possible removing PHP parameters
So (changing what was said previously) we shall have two JS files instead of "get_print_data.js"
We shall split it in two: get_data.js (this shall be pure JS with the get_form_part#() functions) and print_data.php (this must be PHP because it needs to get data from translation files)
The object variable "monthly_costs" (in businessLogic) shall only have 12 main fields/parameters which are the monthly cost for each item
var monthly_costs = {
depreciation,
insurance,
credit,
inspection,
car_tax,
fuel,
maintenance,
repairs_improv,
parking,
tolls,
fines,
washing
}
Then the main object "output", the one is to be returned, shall then still have:
output = {
total_running_costs_month,
total_standing_costs_month,
total_costs_month,
total_costs_year,
age_months, //variable "meses"
distance_unit:, //"km" or "mile" or "mil" (10km) , check "NO.php" in country_files folder
distance_per_month:
running_costs_p_unit_distance:, //ex:€/km
total_costs_p_unit_distance:,
}
In the other tables, the same principle applies, though more simple. We shall only put to output what we'll need to print in the tables or useful data, the rest shall be temporary variables from the function calculate_costs
The function calculate_costs shall have a fourth parameter calculate_costs(f1, f2, f3, country)
Variable country shall be an object with
country = {
country_code:, //"PT", "GB", etc. ISO 3166-1 alpha-2
currency:, //3 letters "USD", "EUR", etc. ISO 4217
//you'll need to add an extra PHP variable ($CURR_CODE, around line 75) in each country translation file
fuel_efficiency_std:,
}
The last field refers to the PHP variable $fuel_efficiency_std;
//1 - l/100km - litres per 100 kilometres
//2 - km/l - kilometres per litre
//3 - mpg(imp) - miles per imperial gallon
//4 - mpg(US) - miles per US gallon
//5 - l/mil - litres per 10 kilometers
We shall move drawChartResult to newly created file "print_data.php".
formsInit() can be moved to "js_functions.php"
The function calculate_costs shall then be alone in one pure JS file businesLogic.js
These variables names (and everything else where these words appear) shall be changed:
"tipo_seguro_auto" to "insurance_type"
"seguro_val" to "insurance_value"
"cred_auto_montante" to "credit_amount"
"cred_auto_period" to "credit_period"
"cred_auto_val_mes" to "credit_value_p_month"
"cred_auto_valresidual" to "credit_residual_vaule"
"IUC" to "car_tax"
"tipo_calc_combustiveis" to "type_calc_fuel"
"leva_auto_job" to "take_car_to_job"
"combustivel_period_km" to "fuel_period_distance"
"km_por_mes" to "distance"
"consumo_auto" to "car_consumption"
"km_entre_casa_trabalho" to "distance_home2job"
"km_fds" to "distance_weekend"
"dias_por_semana" to "days_p_week"
"combustiveis_periodo_euro" to "fuel_period_money"
"combustiveis_euro" to "fuel_money"
"revisoes" to "maintenance"
"reparacoes" to "repairs"
"parqueamento" to "parking"
"tipo_calc_portagens" to "type_calc_tolls"
"portagens_select" to "tolls_select"
"portagens" to "tolls"
"preco_portagens_por_dia" to "price_tolls_p_day"
"dias_portagens_por_mes" to "tolls_days_p_month"
"multas" to "fines"
"multas_select" to "fines_select"
"lavagens" to "washing"
"lavagens_select" to "washing_select"
from autocosts.
Suggestion from @PerGon
from autocosts.
Related Issues (20)
- Reorder world statistics chart
- Introduce externalities table
- Your Sitemap or Sitemap index file does not properly declare the namespace. HOT 1
- Res.status is not a function
- localhost on db credentials HOT 2
- Rebooting issues on the server HOT 2
- Add further icons for table JPG image and for world stats on the right corner HOT 7
- Reactivate CSP HOT 1
- Correct Spanish words HOT 2
- Change domain policy and url rules HOT 1
- Change term to "car" for UK version HOT 1
- Reassure uber information is working
- Automate browser testing
- Check compatibility with some browsers version
- Swap title with short description on Google Entry page HOT 1
- restore html-validator from w3C and use async.retry HOT 1
- remove libxmljs HOT 2
- imagemagick for compressing images is failing test on windows and macos
- Add donation popup after results are shown
- TypeError: useragent.version.split is not a function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autocosts.