-
-
-
-
-|;
+ $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
-map { print "$column_header{$_}\n" } @column_index;
+ $report->set_options('top_info_text' => join("\n", @options),
+ 'output_format' => 'HTML',
+ 'title' => $form->{title},
+ 'attachment_basename' => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
+ );
+ $report->set_options_from_form();
+ $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
-print "
-
-
-
-
-";
-
# add sort to callback
- $form->{callback} = "$callback&sort=$form->{sort}";
- $callback = $form->escape($form->{callback});
-
- # initial item for subtotals
- if (@{ $form->{GL} }) {
- $sameitem = $form->{GL}->[0]->{$form->{sort}};
- }
-
- if (($form->{accno} || $form->{gifi_accno}) && $form->{balance}) {
+ $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
- map { $column_data{$_} = " | " } @column_index;
- $column_data{balance} = "".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)." | ";
-
- $i++; $i %= 2;
- print qq|
-
-|;
- map { print "$column_data{$_}\n" } @column_index;
-
- print qq|
-
-|;
- }
-
- foreach $ref (@{ $form->{GL} }) {
- # if item ne sort print subtotal
- if ($form->{l_subtotal} eq 'Y') {
- if ($sameitem ne $ref->{$form->{sort}}) {
- &gl_subtotal;
+ my @totals_columns = qw(debit credit debit_tax credit_tax);
+ my %subtotals = map { $_ => 0 } @totals_columns;
+ my %totals = map { $_ => 0 } @totals_columns;
+ my $idx = 0;
+
+ foreach my $ref (@{ $form->{GL} }) {
+
+ my %rows;
+
+ foreach my $key (qw(debit credit debit_tax credit_tax)) {
+ $rows{$key} = [];
+ foreach my $idx (sort keys(%{ $ref->{$key} })) {
+ my $value = $ref->{$key}->{$idx};
+ $subtotals{$key} += $value;
+ $totals{$key} += $value;
+ if ($key =~ /debit.*/) {
+ $ml = -1;
+ } else {
+ $ml = 1;
+ }
+ $form->{balance} = $form->{balance} + $value * $ml;
+ push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
}
}
- foreach $key (sort keys (%{$ref->{amount}})) {
- $form->{balance} += $ref->{amount}{$key};
+
+ foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
+ my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
+ $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
}
-
- $debit = "";
- foreach $key (sort keys (%{$ref->{debit}})) {
- $subtotaldebit += $ref->{debit}{$key};
- $totaldebit += $ref->{debit}{$key};
- if ($key == 0) {
- $debit = $form->format_amount(\%myconfig, $ref->{debit}{$key} , 2, 0);
- } else {
- $debit .= " ".$form->format_amount(\%myconfig, $ref->{debit}{$key} , 2, 0);
- }
+
+ my $row = { };
+ map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
+
+ my $sh = "";
+ if ($form->{balance} < 0) {
+ $sh = " S";
+ $ml = -1;
+ } elsif ($form->{balance} > 0) {
+ $sh = " H";
+ $ml = 1;
}
-
- $credit = "";
- foreach $key (sort keys (%{$ref->{credit}})) {
- $subtotalcredit += $ref->{credit}{$key};
- $totalcredit += $ref->{credit}{$key};
- if ($key == 0) {
- $credit = $form->format_amount(\%myconfig, $ref->{credit}{$key} , 2, 0);
- } else {
- $credit .= " ".$form->format_amount(\%myconfig, $ref->{credit}{$key} , 2, 0);
- }
+ my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
+ $data .= $sh;
+
+ $row->{balance}->{data} = $data;
+ $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
+
+ map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes);
+
+ map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
+
+ foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
+ $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
}
-
- $debittax = "";
- foreach $key (sort keys (%{$ref->{debit_tax}})) {
- $subtotaldebittax += $ref->{debit_tax}{$key};
- $totaldebittax += $ref->{debit_tax}{$key};
- if ($key == 0) {
- $debittax = $form->format_amount(\%myconfig, $ref->{debit_tax}{$key} , 2, 0);
- } else {
- $debittax .= " ".$form->format_amount(\%myconfig, $ref->{debit_tax}{$key} , 2, 0);
- }
+
+ map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
+
+ $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
+
+ my $row_set = [ $row ];
+
+ if (($form->{l_subtotal} eq 'Y')
+ && (($idx == (scalar @{ $form->{GL} } - 1))
+ || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
+ push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
}
-
- $credittax = "";
- foreach $key (sort keys (%{$ref->{credit_tax}})) {
- $subtotalcredittax += $ref->{credit_tax}{$key};
- $totalcredittax += $ref->{credit_tax}{$key};
- if ($key == 0) {
- $credittax = $form->format_amount(\%myconfig, $ref->{credit_tax}{$key} , 2, 0);
- } else {
- $credittax .= " ".$form->format_amount(\%myconfig, $ref->{credit_tax}{$key} , 2, 0);
+
+ $report->add_data($row_set);
+
+ $idx++;
+ }
+
+ $report->add_separator();
+
+ # = 0 for balanced ledger
+ my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
+
+ my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
+
+ my $sh = "";
+ if ($form->{balance} < 0) {
+ $sh = " S";
+ $ml = -1;
+ } elsif ($form->{balance} > 0) {
+ $sh = " H";
+ $ml = 1;
+ }
+ my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
+ $data .= $sh;
+
+ $row->{balance}->{data} = $data;
+
+ $report->add_data($row);
+
+ my $raw_bottom_info_text;
+
+ if (!$form->{accno} && (abs($balanced_ledger) > 0.001)) {
+ $raw_bottom_info_text .=
+ ''
+ . $locale->text('Unbalanced Ledger')
+ . ': '
+ . $form->format_amount(\%myconfig, $balanced_ledger, 3)
+ . ' ';
+ }
+
+ $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
+
+ $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
+
+ $report->generate_with_headers();
+
+ $main::lxdebug->leave_sub();
+}
+
+sub update {
+ $main::lxdebug->enter_sub();
+
+ $main::auth->assert('general_ledger');
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+
+ $form->{oldtransdate} = $form->{transdate};
+
+ my @a = ();
+ my $count = 0;
+ my $debittax = 0;
+ my $credittax = 0;
+ my $debitcount = 0;
+ my $creditcount = 0;
+ my ($debitcredit, $amount);
+
+ my @flds =
+ qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
+
+ for my $i (1 .. $form->{rowcount}) {
+
+ unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
+ for (qw(debit credit tax)) {
+ $form->{"${_}_$i"} =
+ $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
}
- }
-
- $debitaccno = "";
- $debittaxkey = "";
- $taxaccno = "";
- foreach $key (sort keys (%{$ref->{debit_accno}})) {
- if ($key == 0) {
- $debitaccno = "{accno}&callback=$callback>$ref->{debit_accno}{$key}";
+
+ push @a, {};
+ $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
+ if ($debitcredit) {
+ $debitcount++;
} else {
- $debitaccno .= " {accno}&callback=$callback>$ref->{debit_accno}{$key}";
+ $creditcount++;
}
- if ($ref->{debit_taxkey}{$key} eq $debittaxkey) {
- $ref->{debit_tax_accno}{$key} = $taxaccno;
- }
- $taxaccno = $ref->{debit_tax_accno}{$key};
- $debittaxkey = $ref->{debit_taxkey}{$key};
- }
-
- $creditaccno = "";
- $credittaxkey = "";
- $taxaccno = "";
- foreach $key (sort keys (%{$ref->{credit_accno}})) {
- if ($key == 0) {
- $creditaccno = "{accno}&callback=$callback>$ref->{credit_accno}{$key}";
- } else {
- $creditaccno .= " {accno}&callback=$callback>$ref->{credit_accno}{$key}";
+
+ if (($debitcount >= 2) && ($creditcount == 2)) {
+ $form->{"credit_$i"} = 0;
+ $form->{"tax_$i"} = 0;
+ $creditcount--;
+ $form->{creditlock} = 1;
}
- if ($ref->{credit_taxkey}{$key} eq $credittaxkey) {
- $ref->{credit_tax_accno}{$key} = $taxaccno;
+ if (($creditcount >= 2) && ($debitcount == 2)) {
+ $form->{"debit_$i"} = 0;
+ $form->{"tax_$i"} = 0;
+ $debitcount--;
+ $form->{debitlock} = 1;
}
- $taxaccno = $ref->{credit_tax_accno}{$key};
- $credittaxkey = $ref->{credit_taxkey}{$key};
- }
-
- $debittaxaccno = "";
- foreach $key (sort keys (%{$ref->{debit_tax_accno}})) {
- if ($key == 0) {
- $debittaxaccno = "{accno}&callback=$callback>$ref->{debit_tax_accno}{$key}";
- } else {
- $debittaxaccno .= " {accno}&callback=$callback>$ref->{debit_tax_accno}{$key}";
+ if (($creditcount == 1) && ($debitcount == 2)) {
+ $form->{creditlock} = 1;
}
- }
-
- $credittaxaccno = "";
- foreach $key (sort keys (%{$ref->{credit_tax_accno}})) {
- if ($key == 0) {
- $credittaxaccno = "{accno}&callback=$callback>$ref->{credit_tax_accno}{$key}";
+ 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";
+ }
+ $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;
+ } else {
+ $form->{"tax_$i"} = $amount * $rate;
+ }
} else {
- $credittaxaccno .= " {accno}&callback=$callback>$ref->{credit_tax_accno}{$key}";
+ $form->{"tax_$i"} = 0;
}
- }
-# $ref->{debit} = $form->format_amount(\%myconfig, $ref->{debit}, 2, " ");
-# $ref->{credit} = $form->format_amount(\%myconfig, $ref->{credit}, 2, " ");
-
- $column_data{id} = " $ref->{id} | ";
- $column_data{transdate} = " $ref->{transdate} | ";
- $column_data{reference} = "{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{reference} | ";
- $column_data{description} = "$ref->{description} | ";
- $column_data{source} = "$ref->{source} | ";
- $column_data{notes} = "$ref->{notes} | ";
- $column_data{debit} = "$debit | ";
- $column_data{debit_accno} = "$debitaccno | ";
- $column_data{credit} = "$credit | ";
- $column_data{credit_accno} = "$creditaccno | ";
- $column_data{debit_tax} = ($ref->{debit_tax_accno} ne "") ? "$debittax | " : " | ";
- $column_data{debit_tax_accno} = "$debittaxaccno | ";
- $column_data{gifi_accno} = "{gifi_accno}&callback=$callback>$ref->{gifi_accno} | ";
- $column_data{credit_tax} = ($ref->{credit_tax_accno} ne "") ? "$credittax | " : " | ";
- $column_data{credit_tax_accno} = "$credittaxaccno | ";
- $column_data{gifi_accno} = "{gifi_accno}&callback=$callback>$ref->{gifi_accno} | ";
- $column_data{balance} = "".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)." | ";
-
- $i++; $i %= 2;
- print "
- ";
- map { print "$column_data{$_}\n" } @column_index;
- print " ";
-
+
+ for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
+ $count++;
+ }
}
+ for my $i (1 .. $count) {
+ my $j = $i - 1;
+ for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
+ }
- &gl_subtotal if ($form->{l_subtotal} eq 'Y');
+ for my $i ($count + 1 .. $form->{rowcount}) {
+ for (@flds) { delete $form->{"${_}_$i"} }
+ }
+ $form->{rowcount} = $count + 1;
- map { $column_data{$_} = " | " } @column_index;
-
- $column_data{debit} = "".$form->format_amount(\%myconfig, $totaldebit, 2, " ")." | ";
- $column_data{credit} = "".$form->format_amount(\%myconfig, $totalcredit, 2, " ")." | ";
- $column_data{debit_tax} = "".$form->format_amount(\%myconfig, $totaldebittax, 2, " ")." | ";
- $column_data{credit_tax} = "".$form->format_amount(\%myconfig, $totalcredittax, 2, " ")." | ";
- $column_data{balance} = "".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)." | ";
-
- print qq|
-
-|;
+ &display_form;
+ $main::lxdebug->leave_sub();
- map { print "$column_data{$_}\n" } @column_index;
+}
- print qq|
-
-
-
- |
-
-