return 0;
}
- if ( ($myconfig->{numberformat} eq '1.000,00')
- || ($myconfig->{numberformat} eq '1000,00')) {
+ if ($myconfig->{numberformat} eq '1,000.00') {
+ $amount =~ s/,//g;
+ } elsif ($myconfig->{numberformat} eq '1.000,00') {
$amount =~ s/\.//g;
- $amount =~ s/,/\./g;
- }
-
- if ($myconfig->{numberformat} eq "1'000.00") {
+ } elsif ($myconfig->{numberformat} eq "1'000.00") {
$amount =~ s/\'//g;
}
- $amount =~ s/,//g;
+ $amount =~ s/,/\./g;
$main::lxdebug->leave_sub(2);
}
sub round_amount {
- my ($self, $amount, $places) = @_;
+ my ($self, $amount, $places, $adjust) = @_;
+
+ if ($adjust) {
+ my $precision = 0.01;
+ # Round amounts to eight places before rounding to the requested
+ # number of places. This gets rid of errors due to internal floating
+ # point representation.
+ $amount = int($amount * 10**8 + .5 * ($amount <=> 0)) / 10**8 if $places < 8;
+ $amount = int($amount / ($precision = _get_precision()) + ($amount <=> 0) * .5) * $precision;
+ $amount = int($amount * 10**$places + .5 * ($amount <=> 0)) / 10**$places;
+ return $amount;
+ }
return 0 if !defined $amount;
}
sub mtime_ischanged {
- my ($self, $relation, $option) = @_;
+ my ($self, $table, $option) = @_;
- return unless $self->{id}; # maybe better croak, but i have no api doc to refer to ...
+ return unless $self->{id};
+ croak ("wrong call, no valid table defined") unless $table =~ /^(oe|ar|ap|delivery_orders|parts)$/;
- my $query = "SELECT mtime, itime FROM " . $relation . " WHERE id = ?";
- my $ref = selectfirst_hashref_query($self, $self->get_standard_dbh, $query, $self->{id});
- $ref->{mtime} = $ref->{itime} if !$ref->{mtime};
+ my $query = "SELECT mtime, itime FROM " . $table . " WHERE id = ?";
+ my $ref = selectfirst_hashref_query($self, $self->get_standard_dbh, $query, $self->{id});
+ $ref->{mtime} ||= $ref->{itime};
if ($self->{lastmtime} && $self->{lastmtime} ne $ref->{mtime} ) {
- my $etxt = $main::locale->text("The document has been changed from other user. Please reopen it in another window and copy the changes to the new window");
-
- $etxt = $main::locale->text("The document has been changed from other user. No mail was sent. Please reopen it in another window and copy the changes to the new window")
- if ($option eq 'mail');
- # ^^ I prefer:
- # my $etxt = ($option eq 'mail') ? locale1 : locale2;
+ my $etxt = ($option eq 'mail') ? "The document has been changed by another user. Please reopen it in another window and copy the changes to the new window" :
+ "The document has been changed by another user. No mail was sent. Please reopen it in another window and copy the changes to the new window";
$self->error($main::locale->text($etxt));
::end_of_request();
}
foreach my $key (keys %$ref) {
$self->{$key} = $ref->{$key};
}
- $self->{mtime} ||= $self->{itime};
+ $self->{mtime} ||= $self->{itime};
$self->{lastmtime} = $self->{mtime};
my $transdate = "current_date";
if ($self->{transdate}) {
return ($amount,$tax);
};
+sub _get_precision {
+ my ( $self ) = @_;
+ my $precision = 0.01;
+ eval {
+ my $client = $::auth->{client};
+ my $dbconnect = 'dbi:Pg:dbname=' . $client->{dbname} . ';host=' . $client->{dbhost} . ';port=' . $client->{dbport};
+ my $dbh = DBI->connect($dbconnect, $client->{dbuser}, $client->{dbpasswd});
+ my $query = q{ SELECT precision FROM defaults };
+ ($precision) = selectrow_query($::form, $dbh, $query);
+ $dbh->disconnect;
+ };
+ return $precision;
+}
+
1;
__END__
A html page title will be generated from this
+=item mtime_ischanged
+
+Tries to avoid concurrent write operations to records by checking the database mtime with a fetched one.
+
+Can be used / called with any table, that has itime and mtime attributes.
+Valid C<table> names are: oe, ar, ap, delivery_orders, parts.
+Can be called wit C<option> mail to generate a different error message.
+
+Returns undef if no save operation has been done yet ($self->{id} not present).
+Returns undef if no concurrent write process is detected otherwise a error message.
+
=back
=cut