Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 9b04d6e3

Von Jan Büren vor fast 2 Jahren hinzugefügt

  • ID 9b04d6e3d59ed21cd893c330d31d85492474a8bf
  • Vorgänger 8557458b
  • Nachfolger eaa30664

Form: update_exchangerate, check_exchangerate um Belegwechselkurs erw.

Ferner POD, strikte Parameter-Überprüfung und Verdacht auf
weiteren ungenutzen Code hinzugefügt.

Unterschiede anzeigen:

SL/Form.pm
1428 1428
sub update_exchangerate {
1429 1429
  $main::lxdebug->enter_sub();
1430 1430

  
1431
  my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1432
  my ($query);
1433
  # some sanity check for currency
1434
  if ($curr eq '') {
1435
    $main::lxdebug->leave_sub();
1436
    return;
1437
  }
1438
  $query = qq|SELECT name AS curr FROM currencies WHERE id=(SELECT currency_id FROM defaults)|;
1431
  validate_pos(@_,
1432
                 { isa  => 'Form'},
1433
                 { isa  => 'DBI::db'},
1434
                 { type => SCALAR, callbacks  => { is_fx_currency     => sub { shift ne $_[1]->[0]->{defaultcurrency} } } }, # should be ISO three letter codes for currency identification (ISO 4217)
1435
                 { type => SCALAR, callbacks  => { is_valid_kivi_date => sub { shift =~ m/\d+\d+\d+/ } } }, # we have three numers
1436
                 { type => SCALAR, callbacks  => { is_null_or_digit   => sub { shift =~ m/(^0$|\d+)/ } } }, # value buy fxrate
1437
                 { type => SCALAR, callbacks  => { is_null_or_digit   => sub { shift =~ m/(^0$|\d+)/ } } }, # value sell fxrate
1438
                 { type => SCALAR, callbacks  => { is_current_form_id => sub { $_[0] == $_[1]->[0]->{id} } },              optional => 1 },
1439
                 { type => SCALAR, callbacks  => { is_valid_fx_table  => sub { shift =~ m/(ar|ap|bank_transactions)/  } }, optional => 1 }
1440
              );
1439 1441

  
1440
  my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
1442
  my ($self, $dbh, $curr, $transdate, $buy, $sell, $id, $record_table) = @_;
1441 1443

  
1442
  if ($curr eq $defaultcurrency) {
1444
  # record has a exchange rate and should be updated
1445
  if ($record_table && $id) {
1446
    do_query($self, $dbh, qq|UPDATE $record_table SET exchangerate = ? WHERE id = ?|, $buy || $sell, $id);
1443 1447
    $main::lxdebug->leave_sub();
1444 1448
    return;
1445 1449
  }
1446 1450

  
1451
  my ($query);
1447 1452
  $query = qq|SELECT e.currency_id FROM exchangerate e
1448 1453
                 WHERE e.currency_id = (SELECT cu.id FROM currencies cu WHERE cu.name=?) AND e.transdate = ?
1449 1454
                 FOR UPDATE|;
......
1469 1474
  }
1470 1475

  
1471 1476
  if ($sth->fetchrow_array) {
1477
    die "this never happens never";
1472 1478
    $query = qq|UPDATE exchangerate
1473 1479
                SET $set
1474 1480
                WHERE currency_id = (SELECT id FROM currencies WHERE name = ?)
......
1487 1493
sub check_exchangerate {
1488 1494
  $main::lxdebug->enter_sub();
1489 1495

  
1490
  my $self = shift;
1491 1496
  validate_pos(@_,
1497
                 { isa  => 'Form'},
1492 1498
                 { type => HASHREF, callbacks => { has_yy_in_dateformat => sub { $_[0]->{dateformat} =~ m/yy/ } } },
1493
                 { type => SCALAR  }, # should be ISO three letter codes for currency identification (ISO 4217)
1499
                 { type => SCALAR, callbacks  => { is_fx_currency       => sub { shift ne $_[1]->[0]->{defaultcurrency} } } }, # should be ISO three letter codes for currency identification (ISO 4217)
1494 1500
                 { type => SCALAR, callbacks  => { is_valid_kivi_date   => sub { shift =~ m/\d+\d+\d+/ } } }, # we have three numers
1495 1501
                 { type => SCALAR, callbacks  => { is_buy_or_sell_rate  => sub { shift =~ m/^buy|sell$/ } } },
1502
                 { type => SCALAR, callbacks  => { is_current_form_id   => sub { $_[0] == $_[1]->[0]->{id} } },              optional => 1 },
1503
                 { type => SCALAR, callbacks  => { is_valid_fx_table    => sub { shift =~ m/(ar|ap|bank_transactions)/  } }, optional => 1 }
1496 1504
              );
1497
  my ($myconfig, $currency, $transdate, $fld) = @_;
1505
  my ($self, $myconfig, $currency, $transdate, $fld, $id, $record_table) = @_;
1498 1506

  
1499
  my ($defaultcurrency) = $self->get_default_currency($myconfig);
1500
  if ($currency eq $defaultcurrency) {
1501
    $main::lxdebug->leave_sub();
1502
    return 1;
1507
  my $dbh   = $self->get_standard_dbh($myconfig);
1508

  
1509
  # callers wants a check if record has a exchange rate and should be fetched instead
1510
  if ($record_table && $id) {
1511
    my ($record_exchange_rate) = selectrow_query($self, $dbh, qq|SELECT exchangerate FROM $record_table WHERE id = ?|, $id);
1512
    if ($record_exchange_rate) {
1513

  
1514
      $main::lxdebug->leave_sub();
1515

  
1516
      return $record_exchange_rate;
1517
    }
1503 1518
  }
1504 1519

  
1505
  my $dbh   = $self->get_standard_dbh($myconfig);
1520
  # fetch default from exchangerate table
1506 1521
  my $query = qq|SELECT e.$fld FROM exchangerate e
1507 1522
                 WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|;
1508 1523

  
......
3525 3540
Returns undef if no save operation has been done yet ($self->{id} not present).
3526 3541
Returns undef if no concurrent write process is detected otherwise a error message.
3527 3542

  
3543
=back
3544

  
3545
=over 4
3546

  
3547
=item C<check_exchangerate>  $myconfig, $currency, $transdate, $fld, $id, $record_table
3548

  
3549
Needs a local myconfig, a currency string, a date of the transaction, a field (fld) which
3550
has to be either the buy or sell exchangerate and checks if there is already a buy or
3551
sell exchangerate for this date.
3552
Returns 0 or (NULL) if no entry is found or the already stored exchangerate.
3553
If the optional parameter id and record_table is passed, the method tries to look up
3554
a custom exchangerate for a record with id. record_table can either be ar, ap or bank_transactions.
3555
If none is found the default (daily) entry will be checked.
3556
The method is very strict about the parameters and tries to fail if anything does
3557
not look like the expected type.
3558

  
3559
=item C<update_exchangerate> $dbh, $curr, $transdate, $buy, $sell, $id, $record_table
3560

  
3561
Needs a dbh connection, a currency string, a date of the transaction, buy (0|1), sell (0|1) which
3562
determines if either the buy or sell or both exchangerates should be updated and updates
3563
the exchangerate for this currency for this date.
3564
If the optional parameter id and record_table is passed, the method saves
3565
a custom exchangerate for a record with id. record_table can either be ar, ap or bank_transactions.
3566

  
3567
The method is very strict about the parameters and tries to fail if anything does not look
3568
like the expected type.
3569

  
3570

  
3571

  
3572

  
3528 3573
=back
3529 3574

  
3530 3575
=cut

Auch abrufbar als: Unified diff