-
- $transdate = $form->datetonum($form->{transdate}, \%myconfig);
- $closedto = $form->datetonum($form->{closedto}, \%myconfig);
-
- ($debitaccno, $debittaxkey) = split(/--/, $form->{debitchartselected});
- ($creditaccno, $credittaxkey) = split(/--/, $form->{creditchartselected});
-
- $credit_save = $form->{credit};
- $debit_save = $form->{debit};
-
- # check project
- &check_project;
- ($taxkey, $taxrate) = split(/--/, $form->{taxchartselected});
-
- if ($debittaxkey >0) {
- $form->{taxchart} = $form->unescape($form->{taxchart});
- $form->{taxchart} =~ s/\"$debittaxkey--([^\"]*)\"/\"$debittaxkey--$1\"/;
-
- $rate = ($form->{taxchart} =~ /selected/) ? $taxrate : $1;
- $form->{taxkey} = ($form->{taxchart} =~ /selected/) ? $taxkey : $debittaxkey;
-
- if ($form->{storno}) {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{debit});
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{credit});
- $form->{tax} = $form->parse_amount(\%myconfig, $form->{tax});
- } else {
- if ($form->{taxincluded}) {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1);
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1;
- $form->{tax} = $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1) * $rate;
- } else {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1;
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * ($rate + 1);
- $form->{tax} = $form->parse_amount(\%myconfig, $form->{amount}) * $rate;
- }
- }
- $form->{debittaxkey}=1;
-
- } else {
- $form->{taxchart} = $form->unescape($form->{taxchart});
- $form->{taxchart} =~ s/\"$credittaxkey--([^\"]*)\"/\"$credittaxkey--$1\"/;
-
-
- $rate = ($form->{taxchart} =~ /selected/) ? $taxrate : $1;
- $form->{taxkey} = ($form->{taxchart} =~ /selected/) ? $taxkey : $credittaxkey;
-
- if ($form->{storno}) {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{debit});
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{credit});
- $form->{tax} = $form->parse_amount(\%myconfig, $form->{tax});
- } else {
- if ($form->{taxincluded}) {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1;
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1);
- $form->{tax} = $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1) * $rate;
- } else {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * ($rate + 1);
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1;
- $form->{tax} = $form->parse_amount(\%myconfig, $form->{amount}) * $rate;
- }
- }
- $form->{debittaxkey}=0;
-
- }
- if ($form->{credit_splited}) {
- $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1;
- $form->{credit} = $credit_save;
- $form->{tax} = 0;
- } elsif ($form->{debit_splited}) {
- $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1;
- $form->{debit} = $debit_save;
- $form->{tax} = 0;
+
+ my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
+ my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
+
+ my @a = ();
+ my $count = 0;
+ my $debittax = 0;
+ my $credittax = 0;
+ my $debitcount = 0;
+ my $creditcount = 0;
+ my $debitcredit;
+
+ my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
+
+ for my $i (1 .. $form->{rowcount}) {
+ next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
+
+ for (qw(debit credit tax)) {
+ $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
+ }
+
+ push @a, {};
+ $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
+
+ if ($debitcredit) {
+ $debitcount++;
+ } else {
+ $creditcount++;
+ }
+
+ if (($debitcount >= 2) && ($creditcount == 2)) {
+ $form->{"credit_$i"} = 0;
+ $form->{"tax_$i"} = 0;
+ $creditcount--;
+ $form->{creditlock} = 1;
+ }
+ if (($creditcount >= 2) && ($debitcount == 2)) {
+ $form->{"debit_$i"} = 0;
+ $form->{"tax_$i"} = 0;
+ $debitcount--;
+ $form->{debitlock} = 1;
+ }
+ if (($creditcount == 1) && ($debitcount == 2)) {
+ $form->{creditlock} = 1;
+ }
+ if (($creditcount == 2) && ($debitcount == 1)) {
+ $form->{debitlock} = 1;
+ }
+ if ($debitcredit && $credittax) {
+ $form->{"taxchart_$i"} = "0--0.00";
+ }
+ if (!$debitcredit && $debittax) {
+ $form->{"taxchart_$i"} = "0--0.00";
+ }
+ my $amount = ($form->{"debit_$i"} == 0)
+ ? $form->{"credit_$i"}
+ : $form->{"debit_$i"};
+ my $j = $#a;
+ if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
+ $form->{"taxchart_$i"} = "0--0.00";
+ $form->{"tax_$i"} = 0;
+ }
+ my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
+ if ($taxkey > 1) {
+ if ($debitcredit) {
+ $debittax = 1;
+ } else {
+ $credittax = 1;
+ }
+ if ($form->{taxincluded}) {
+ $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
+ if ($debitcredit) {
+ $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
+ } else {
+ $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
+ }
+ } else {
+ $form->{"tax_$i"} = $amount * $rate;
+ }
+ } else {
+ $form->{"tax_$i"} = 0;
+ }
+
+ for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
+ $count++;
+ }
+
+ for my $i (1 .. $count) {
+ my $j = $i - 1;
+ for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
+ }
+
+ for my $i ($count + 1 .. $form->{rowcount}) {
+ for (@flds) { delete $form->{"${_}_$i"} }
+ }
+
+ my ($debit, $credit, $taxtotal);
+ for my $i (1 .. $form->{rowcount}) {
+ my $dr = $form->{"debit_$i"};
+ my $cr = $form->{"credit_$i"};
+ my $tax = $form->{"tax_$i"};
+ if ($dr && $cr) {
+ $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
+ }
+ $debit += $dr + $tax if $dr;
+ $credit += $cr + $tax if $cr;
+ $taxtotal += $tax if $form->{taxincluded}