App-InvestSim
view release on metacpan or search on metacpan
lib/App/InvestSim/Values.pm view on Meta::CPAN
sub validate_float {
my ($val, $event) = @_;
if ($val =~ m/^\d*((\.|,)\d*)?$/) {
return 1;
} else {
return 0;
}
}
# The file name used for the auto-save feature.
my $autosave_file = catfile(File::HomeDir->my_data(), '.investment_simulator');
# The default options used for the getSaveFile and getOpenFile dialog boxes.
my @open_save_options = (
-defaultextension => '.investment',
-filetypes => [["Simulation d'investissement", [".investment"]],
["Tout les fichiers", ["*"]]]);
my $current_file;
# This hash list all the variables that can be used to configure the simulation.
# Each variable name points to the following values (in an array-ref):
# - its default value (which also give its type if its a reference);
# - a validation function for values entered by the user or read from files;
# - for an array variable, its expected size.
our %values_config = (
# Values set through the left-bar of the program:
invested => [300000, \&validate_positive_integer],
tax_rate => [41.0, \&validate_float],
base_rent => [800, \&validate_positive_integer],
rent_charges => [24, \&validate_float],
rent_increase => [0.5, \&validate_float],
duration => [20, \&validate_positive_integer],
loan_insurance => [0.3, \&validate_float],
other_rate => [0.0, \&validate_float],
social_tax => [17.2, \&validate_float],
surface => [40, \&validate_float],
loan_delay => [0, \&validate_non_negative_integer],
rent_delay => [0, \&validate_non_negative_integer],
notary_fees => [2.5, \&validate_float],
application_fees => [1000, \&validate_non_negative_integer],
mortgage_fees => [1.2, \&validate_float],
# Values set through the top-bar and the core data table:
loan_durations => [[qw(10 12 14 16 18 20)], \&validate_positive_integer, NUM_LOAN_DURATION],
loan_rates => [[qw(0.9 0.9 1.0 1.2 1.3 1.4)], \&validate_float, NUM_LOAN_DURATION],
loan_amounts => [[qw(0 50000 100000 150000 200000 250000 300000)], \&validate_positive_integer, NUM_LOAN_AMOUNT],
# Values set through the menu:
pinel_duration => [0, \&validate_non_negative_integer],
pinel_zone => [0, \&validate_non_negative_integer],
automatic_duration => [0, \&validate_non_negative_integer],
);
Hash::Util::lock_hash(%values_config);
# This hash has the actual values used to configure the simulation. Its keys are
# restricted to those of %values_config.
our %values;
Hash::Util::lock_keys(%values, keys %values_config);
# Whether any of the values has changed. This must be updated manually by the
# caller when they touch a value.
our $has_changes = 0;
# Display a dialog box and returns true if the user wants to proceed.
sub lose_data_warning {
return 1 unless $has_changes;
my $res = Tkx::tk___messageBox(
-message => 'La simulation courante sera perdue, êtes-vous sûr de vouloir continuer ?',
-type => 'yesno', -icon => 'question', -title => 'Êtes-vous sûr?', -parent => '.');
return $res eq 'yes';
}
# Initializes %values with the default values from %values_config.
sub init_values {
return unless lose_data_warning();
undef $current_file;
set_window_title('');
while (my ($key, $conf) = each %values_config) {
my ($default, undef, $size) = @$conf;
if (ref $default eq '') {
$values{$key} = $default;
} elsif (ref $default eq 'ARRAY') {
if ($size != @$default) {
message_and_die(
"Erreur interne: taille par défaut invalide pour $key",
sprintf("attendu: %d\nréel: %d", $size, scalar(@$default)));
}
# We're using this loop (instead of just @$values{$key} = @$default ) to
# not invalidate the references to the values of the array.
for my $i (0..$#$default) {
$values{$key}[$i] = $default->[$i];
}
} else {
message_and_die(
"Erreur interne: type de reference inatendu dans init_value pour $key: ".(ref $default));
}
}
$has_changes = 0;
}
# Saves the content of %values to the given file.
sub save_to_file {
my ($file) = @_;
my $fh;
if (not open($fh, '>', $file)) {
message_and_warn("Impossible d'ouvrir le fichier $file", $!);
return 0;
}
{
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Sortkeys = 1;
print $fh Dumper(\%values);
}
close $fh;
$has_changes = 0;
return 1;
}
# Asks for a destination files. If the user select one, the save the content of
# %values to that file and remember the name of the selected file for future
( run in 1.147 second using v1.01-cache-2.11-cpan-39bf76dae61 )