+ my @result = selectall_hashref_query($form, $dbh, $query, @values);
+
+ $form->{AR} = [ @result ];
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_transdate {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = SL::DB->client->dbh;
+
+ my $query =
+ "SELECT COALESCE(" .
+ " (SELECT transdate FROM ar WHERE id = " .
+ " (SELECT MAX(id) FROM ar) LIMIT 1), " .
+ " current_date)";
+ ($form->{transdate}) = $dbh->selectrow_array($query);
+
+ $main::lxdebug->leave_sub();
+}
+
+sub setup_form {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $form, $for_post_payments) = @_;
+
+ my ($exchangerate, $akey, $j, $k, $index, $taxamount, $totaltax, $taxrate, $diff, $totalwithholding, $withholdingrate,
+ $totalamount, $tax);
+
+ # forex
+ $form->{forex} = $form->{exchangerate};
+ $exchangerate = $form->{exchangerate} ? $form->{exchangerate} : 1;
+
+ # expected keys: AR, AR_paid, AR_tax, AR_amount
+ foreach my $key (keys %{ $form->{AR_links} }) {
+ $j = 0;
+ $k = 0;
+
+ # if there is a value we have an old entry
+ next unless $form->{acc_trans}{$key};
+
+ # do not use old entries for payments. They come from the form
+ # even if they are not changeable (then they are in hiddens)
+ next if $for_post_payments && $key eq "AR_paid";
+
+ for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
+ if ($key eq "AR_paid") {
+ $j++;
+ $form->{"AR_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
+
+ $form->{"acc_trans_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
+ # reverse paid
+ $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
+ $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
+ $form->{"gldate_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
+ $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
+ $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
+ $form->{"forex_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
+ $form->{"exchangerate_$i"} = $form->{"forex_$j"};
+ $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
+ $form->{paidaccounts}++;
+
+ } else { # e.g. AR_amount, AR, AR_tax
+
+ $akey = $key;
+ $akey =~ s/AR_//; # e.g. tax, amount, AR, used to store form key tax_$i, amount_$i, ...
+
+ if ($key eq "AR_tax" || $key eq "AP_tax") { # AR_tax
+ $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
+ # determine the rounded tax amounts for each account, e.g. tax_1776
+ $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
+
+ # check e.g. $form->{1776_rate}, does this make sense for AR_tax charts? Is this ever valid? If it was, totaltax would be calculated twice
+ if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
+ $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
+ $taxrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
+
+ } else {
+ $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
+ $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
+ }
+
+ $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
+ $form->{"tax_$index"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2); # convert the tax_$i amounts
+ # currently totaltax is the sum of rounded tax amounts, is this correct?
+ $totaltax += $form->{"tax_$index"};
+
+ } else { # e.g. AR_amount, AR
+ $k++;
+ $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
+
+ if ($akey eq 'amount') {
+ $form->{rowcount}++;
+ $totalamount += $form->{"${akey}_$i"};
+
+ $form->{"oldprojectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber};
+ $form->{"projectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber};
+ $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
+ $form->{"project_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{project_id};
+
+ $form->{"${key}_chart_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{chart_id};
+ $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
+ }
+ }
+ }
+ }