1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29 #======================================================================
33 #======================================================================
38 use POSIX qw(strftime);
39 use List::Util qw(sum);
44 use SL::ReportGenerator;
45 use SL::DBUtils qw(selectrow_query selectall_hashref_query);
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
50 # this is for our long dates
51 # $locale->text('January')
52 # $locale->text('February')
53 # $locale->text('March')
54 # $locale->text('April')
55 # $locale->text('May ')
56 # $locale->text('June')
57 # $locale->text('July')
58 # $locale->text('August')
59 # $locale->text('September')
60 # $locale->text('October')
61 # $locale->text('November')
62 # $locale->text('December')
64 # this is for our short month
65 # $locale->text('Jan')
66 # $locale->text('Feb')
67 # $locale->text('Mar')
68 # $locale->text('Apr')
69 # $locale->text('May')
70 # $locale->text('Jun')
71 # $locale->text('Jul')
72 # $locale->text('Aug')
73 # $locale->text('Sep')
74 # $locale->text('Oct')
75 # $locale->text('Nov')
76 # $locale->text('Dec')
79 $main::lxdebug->enter_sub();
81 $main::auth->assert('gl_transactions');
83 my $form = $main::form;
84 my %myconfig = %main::myconfig;
86 $form->{title} = "Add";
88 $form->{callback} = "gl.pl?action=add" unless $form->{callback};
90 # we use this only to set a default date
91 # yep. aber er holt hier auch schon ALL_CHARTS. Aufwand / Nutzen? jb
92 GL->transaction(\%myconfig, \%$form);
94 $form->{rowcount} = 2;
101 $form->all_departments(\%myconfig);
102 if (@{ $form->{all_departments} || [] }) {
103 $form->{selectdepartment} = "<option>\n";
106 $form->{selectdepartment} .=
107 "<option>$_->{description}--$_->{id}\n"
108 } (@{ $form->{all_departments} || [] });
111 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
114 $main::lxdebug->leave_sub();
118 sub prepare_transaction {
119 $main::lxdebug->enter_sub();
121 $main::auth->assert('gl_transactions');
123 my $form = $main::form;
124 my %myconfig = %main::myconfig;
126 GL->transaction(\%myconfig, \%$form);
128 $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
131 $form->all_departments(\%myconfig);
132 if (@{ $form->{all_departments} || [] }) {
133 $form->{selectdepartment} = "<option>\n";
136 $form->{selectdepartment} .=
137 "<option>$_->{description}--$_->{id}\n"
138 } (@{ $form->{all_departments} || [] });
144 foreach my $ref (@{ $form->{GL} }) {
146 if ($tax && ($ref->{accno} eq $taxaccno)) {
147 $form->{"tax_$j"} = abs($ref->{amount});
148 $form->{"taxchart_$j"} = $ref->{id} . "--" . $ref->{taxrate};
149 if ($form->{taxincluded}) {
150 if ($ref->{amount} < 0) {
151 $form->{"debit_$j"} += $form->{"tax_$j"};
153 $form->{"credit_$j"} += $form->{"tax_$j"};
156 $form->{"project_id_$j"} = $ref->{project_id};
159 $form->{"accno_$i"} = "$ref->{accno}--$ref->{tax_id}";
160 for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
161 if ($ref->{amount} < 0) {
162 $form->{totaldebit} -= $ref->{amount};
163 $form->{"debit_$i"} = $ref->{amount} * -1;
165 $form->{totalcredit} += $ref->{amount};
166 $form->{"credit_$i"} = $ref->{amount};
168 $form->{"taxchart_$i"} = $ref->{id}."--0.00000";
169 $form->{"project_id_$i"} = $ref->{project_id};
172 if ($ref->{taxaccno} && !$tax) {
173 $taxaccno = $ref->{taxaccno};
181 $form->{rowcount} = $i;
183 ($form->datetonum($form->{transdate}, \%myconfig) <=
184 $form->datetonum($form->{closedto}, \%myconfig));
186 $main::lxdebug->leave_sub();
190 $main::lxdebug->enter_sub();
192 $main::auth->assert('gl_transactions');
194 my $form = $main::form;
195 my %myconfig = %main::myconfig;
197 prepare_transaction();
199 $form->{title} = "Edit";
201 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
207 $main::lxdebug->leave_sub();
212 $::lxdebug->enter_sub;
213 $::auth->assert('general_ledger | gl_transactions');
215 $::form->all_departments(\%::myconfig);
217 projects => { key => "ALL_PROJECTS", all => 1 },
219 $::form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
222 print $::form->parse_html_template('gl/search', {
223 department_label => sub { ("$_[0]{description}--$_[0]{id}")x2 },
224 employee_label => sub { "$_[0]{id}--$_[0]{name}" },
227 $::lxdebug->leave_sub;
230 sub create_subtotal_row {
231 $main::lxdebug->enter_sub();
233 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
235 my $form = $main::form;
236 my %myconfig = %main::myconfig;
238 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
240 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
242 map { $totals->{$_} = 0 } @{ $subtotal_columns };
244 $main::lxdebug->leave_sub();
249 sub generate_report {
250 $main::lxdebug->enter_sub();
252 $main::auth->assert('general_ledger | gl_transactions');
254 my $form = $main::form;
255 my %myconfig = %main::myconfig;
256 my $locale = $main::locale;
258 # generate_report wird beim ersten Aufruf per Weiter-Knopf und POST mit der hidden Variablen sort mit Wert "datesort" (früher "transdate" als Defaultsortiervariable) übertragen
260 # <form method=post action=gl.pl>
261 # <input type=hidden name=sort value=datesort> # form->{sort} setzen
262 # <input type=hidden name=nextsub value=generate_report>
264 # anhand von neuer Variable datesort wird jetzt $form->{sort} auf transdate oder gldate gesetzt
265 # damit ist die Hidden Variable "sort" wahrscheinlich sogar überflüssig
267 # ändert man die Sortierreihenfolge per Klick auf eine der Überschriften wird die Variable "sort" per GET übergeben, z.B. id,transdate, gldate, ...
268 # gl.pl?action=generate_report&employee=18383--Jan%20B%c3%bcren&datesort=transdate&category=X&l_transdate=Y&l_gldate=Y&l_id=Y&l_reference=Y&l_description=Y&l_source=Y&l_debit=Y&l_credit=Y&sort=gldate&sortdir=0
270 if ( $form->{sort} eq 'datesort' ) { # sollte bei einem Post (Aufruf aus Suchmaske) immer wahr sein
271 # je nachdem ob in Suchmaske "transdate" oder "gldate" ausgesucht wurde erstes Suchergebnis entsprechend sortieren
272 $form->{sort} = $form->{datesort};
276 report_generator_set_default_sort("$form->{datesort}", 1);
277 # report_generator_set_default_sort('transdate', 1);
279 GL->all_transactions(\%myconfig, \%$form);
281 my %acctype = ('A' => $locale->text('Asset'),
282 'C' => $locale->text('Contra'),
283 'L' => $locale->text('Liability'),
284 'Q' => $locale->text('Equity'),
285 'I' => $locale->text('Revenue'),
286 'E' => $locale->text('Expense'),);
288 $form->{title} = $locale->text('Journal');
289 if ($form->{category} ne 'X') {
290 $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
293 $form->{landscape} = 1;
295 my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
298 gldate transdate id reference description
299 notes source debit debit_accno
300 credit credit_accno debit_tax debit_tax_accno
301 credit_tax credit_tax_accno projectnumbers balance employee
304 # add employee here, so that variable is still known and passed in url when choosing a different sort order in resulting table
305 my @hidden_variables = qw(accno source reference department description notes project_id datefrom dateto employee_id datesort category l_subtotal);
306 push @hidden_variables, map { "l_${_}" } @columns;
308 my $employee = $form->{employee_id} ? SL::DB::Employee->new(id => $form->{employee_id})->load->name : '';
310 my (@options, @date_options);
311 push @options, $locale->text('Account') . " : $form->{accno} $form->{account_description}" if ($form->{accno});
312 push @options, $locale->text('Source') . " : $form->{source}" if ($form->{source});
313 push @options, $locale->text('Reference') . " : $form->{reference}" if ($form->{reference});
314 push @options, $locale->text('Description') . " : $form->{description}" if ($form->{description});
315 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
316 push @options, $locale->text('Employee') . " : $employee" if $employee;
317 my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Invoice Date') : $locale->text('Booking Date');
318 push @date_options, "$datesorttext" if ($form->{datesort} and ($form->{datefrom} or $form->{dateto}));
319 push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1) if ($form->{datefrom});
320 push @date_options, $locale->text('Bis'), $locale->date(\%myconfig, $form->{dateto}, 1) if ($form->{dateto});
321 push @options, join(' ', @date_options) if (scalar @date_options);
323 if ($form->{department}) {
324 my ($department) = split /--/, $form->{department};
325 push @options, $locale->text('Department') . " : $department";
329 my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
331 $form->{l_credit_accno} = 'Y';
332 $form->{l_debit_accno} = 'Y';
333 $form->{l_credit_tax} = 'Y';
334 $form->{l_debit_tax} = 'Y';
335 # $form->{l_gldate} = 'Y'; # Spalte mit gldate immer anzeigen
336 $form->{l_credit_tax_accno} = 'Y';
337 $form->{l_datesort} = 'Y';
338 $form->{l_debit_tax_accno} = 'Y';
339 $form->{l_balance} = $form->{accno} ? 'Y' : '';
342 'id' => { 'text' => $locale->text('ID'), },
343 'transdate' => { 'text' => $locale->text('Invoice Date'), },
344 'gldate' => { 'text' => $locale->text('Booking Date'), },
345 'reference' => { 'text' => $locale->text('Reference'), },
346 'source' => { 'text' => $locale->text('Source'), },
347 'description' => { 'text' => $locale->text('Description'), },
348 'notes' => { 'text' => $locale->text('Notes'), },
349 'debit' => { 'text' => $locale->text('Debit'), },
350 'debit_accno' => { 'text' => $locale->text('Debit Account'), },
351 'credit' => { 'text' => $locale->text('Credit'), },
352 'credit_accno' => { 'text' => $locale->text('Credit Account'), },
353 'debit_tax' => { 'text' => $locale->text('Debit Tax'), },
354 'debit_tax_accno' => { 'text' => $locale->text('Debit Tax Account'), },
355 'credit_tax' => { 'text' => $locale->text('Credit Tax'), },
356 'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
357 'balance' => { 'text' => $locale->text('Balance'), },
358 'projectnumbers' => { 'text' => $locale->text('Project Numbers'), },
359 'employee' => { 'text' => $locale->text('Employee'), },
362 foreach my $name (qw(id transdate gldate reference description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
363 my $sortname = $name =~ m/accno/ ? 'accno' : $name;
364 my $sortdir = $sortname eq $form->{sort} ? 1 - $form->{sortdir} : $form->{sortdir};
365 $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
368 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
369 map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
371 my %column_alignment;
372 map { $column_alignment{$_} = 'right' } qw(balance id debit credit debit_tax credit_tax balance);
373 map { $column_alignment{$_} = 'center' } qw(transdate gldate reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
374 map { $column_alignment{$_} = 'left' } qw(description source notes);
375 map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
377 my $report = SL::ReportGenerator->new(\%myconfig, $form);
379 $report->set_columns(%column_defs);
380 $report->set_column_order(@columns);
382 $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir));
384 $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
386 $report->set_options('top_info_text' => join("\n", @options),
387 'output_format' => 'HTML',
388 'title' => $form->{title},
389 'attachment_basename' => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
391 $report->set_options_from_form();
392 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
394 # add sort to callback
395 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
398 my @totals_columns = qw(debit credit debit_tax credit_tax);
399 my %subtotals = map { $_ => 0 } @totals_columns;
400 my %totals = map { $_ => 0 } @totals_columns;
403 foreach my $ref (@{ $form->{GL} }) {
407 foreach my $key (qw(debit credit debit_tax credit_tax)) {
409 foreach my $idx (sort keys(%{ $ref->{$key} })) {
410 my $value = $ref->{$key}->{$idx};
411 $subtotals{$key} += $value;
412 $totals{$key} += $value;
413 if ($key =~ /debit.*/) {
418 $form->{balance} = $form->{balance} + $value * $ml;
419 push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
423 foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
424 my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
425 $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
429 map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
432 if ($form->{balance} < 0) {
435 } elsif ($form->{balance} > 0) {
439 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
442 $row->{balance}->{data} = $data;
443 $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
445 map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes gldate employee);
447 map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
449 foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
450 $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
453 map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
455 $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
457 my $row_set = [ $row ];
459 if ( ($form->{l_subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
460 && (($idx == (scalar @{ $form->{GL} } - 1))
461 || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
462 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
465 $report->add_data($row_set);
470 # = 0 for balanced ledger
471 my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
473 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
476 if ($form->{balance} < 0) {
479 } elsif ($form->{balance} > 0) {
483 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
486 $row->{balance}->{data} = $data;
488 if ( !$form->{report_generator_csv_options_for_import} ) {
489 $report->add_separator();
490 $report->add_data($row);
493 my $raw_bottom_info_text;
495 if (!$form->{accno} && (abs($balanced_ledger) > 0.001)) {
496 $raw_bottom_info_text .=
497 '<p><span class="unbalanced_ledger">'
498 . $locale->text('Unbalanced Ledger')
500 . $form->format_amount(\%myconfig, $balanced_ledger, 3)
504 $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
506 $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
508 $report->generate_with_headers();
510 $main::lxdebug->leave_sub();
514 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
515 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
520 $main::lxdebug->enter_sub();
522 $main::auth->assert('gl_transactions');
524 my $form = $main::form;
525 my %myconfig = %main::myconfig;
527 $form->{oldtransdate} = $form->{transdate};
535 my ($debitcredit, $amount);
537 my $dbh = SL::DB->client->dbh;
538 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
539 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
542 qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
544 for my $i (1 .. $form->{rowcount}) {
546 unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
547 for (qw(debit credit tax)) {
549 $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
553 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
560 if (($debitcount >= 2) && ($creditcount == 2)) {
561 $form->{"credit_$i"} = 0;
562 $form->{"tax_$i"} = 0;
564 $form->{creditlock} = 1;
566 if (($creditcount >= 2) && ($debitcount == 2)) {
567 $form->{"debit_$i"} = 0;
568 $form->{"tax_$i"} = 0;
570 $form->{debitlock} = 1;
572 if (($creditcount == 1) && ($debitcount == 2)) {
573 $form->{creditlock} = 1;
575 if (($creditcount == 2) && ($debitcount == 1)) {
576 $form->{debitlock} = 1;
578 if ($debitcredit && $credittax) {
579 $form->{"taxchart_$i"} = "$notax_id--0.00";
581 if (!$debitcredit && $debittax) {
582 $form->{"taxchart_$i"} = "$notax_id--0.00";
585 ($form->{"debit_$i"} == 0)
586 ? $form->{"credit_$i"}
587 : $form->{"debit_$i"};
589 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
590 $form->{"taxchart_$i"} = "$notax_id--0.00";
591 $form->{"tax_$i"} = 0;
593 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
594 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
595 if (!$iswithouttax) {
602 my ($tmpnetamount,$tmpdiff);
603 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
605 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
610 for my $i (1 .. $count) {
612 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
615 for my $i ($count + 1 .. $form->{rowcount}) {
616 for (@flds) { delete $form->{"${_}_$i"} }
619 $form->{rowcount} = $count + 1;
622 $main::lxdebug->leave_sub();
628 $main::lxdebug->enter_sub();
630 $main::auth->assert('gl_transactions');
632 my $form = $main::form;
633 my %myconfig = %main::myconfig;
637 # for $i (1 .. $form->{rowcount}) {
638 # $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
639 # $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
643 &display_rows($init);
645 $main::lxdebug->leave_sub();
651 $main::lxdebug->enter_sub();
653 $main::auth->assert('gl_transactions');
655 my $form = $main::form;
656 my %myconfig = %main::myconfig;
657 my $cgi = $::request->{cgi};
659 $form->{debit_1} = 0 if !$form->{"debit_1"};
660 $form->{totaldebit} = 0;
661 $form->{totalcredit} = 0;
663 my %project_labels = ();
664 my @project_values = ("");
665 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
666 push(@project_values, $item->{"id"});
667 $project_labels{$item->{"id"}} = $item->{"projectnumber"};
670 my %chart_labels = ();
671 my @chart_values = ();
674 foreach my $item (@{ $form->{ALL_CHARTS} }) {
675 if ($item->{charttype} eq 'H'){ #falls ÃŒberschrift
676 next; #ÃŒberspringen (Bug 1150)
678 my $key = $item->{accno} . "--" . $item->{tax_id};
679 $taxchart_init = $item->{tax_id} unless (@chart_values);
680 push(@chart_values, $key);
681 $chart_labels{$key} = $item->{accno} . "--" . $item->{description};
682 $charts{$item->{accno}} = $item;
685 my ($source, $memo, $source_hidden, $memo_hidden);
686 for my $i (1 .. $form->{rowcount}) {
687 if ($form->{show_details}) {
689 <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
691 <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
694 <input type="hidden" name="source_$i" value="$form->{"source_$i"}" size="16">|;
696 <input type="hidden" name="memo_$i" value="$form->{"memo_$i"}" size="16">|;
699 my $selected_accno_full;
700 my ($accno_row) = split(/--/, $form->{"accno_$i"});
701 my $item = $charts{$accno_row};
702 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
704 my $selected_taxchart = $form->{"taxchart_$i"};
705 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
706 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_accno_$i"});
708 my %taxchart_labels = ();
709 my @taxchart_values = ();
712 $filter_accno = $::form->{ALL_CHARTS}[0]->{accno};
713 $filter_accno = $selected_accno if (!$init and $i < $form->{rowcount});
714 foreach my $item ( GL->get_tax_dropdown($filter_accno) ) {
715 my $key = $item->{id} . "--" . $item->{rate};
716 $taxchart_init = $key if ($taxchart_init == $item->{id});
717 push(@taxchart_values, $key);
718 $taxchart_labels{$key} = $item->{taxdescription} . " " . $item->{rate} * 100 . ' %';
719 $taxcharts{$item->{id}} = $item;
722 if ($previous_accno &&
723 ($previous_accno eq $selected_accno) &&
724 ($previous_tax_id ne $selected_tax_id)) {
725 my $item = $taxcharts{$selected_tax_id};
726 $selected_taxchart = "$item->{id}--$item->{rate}";
729 $selected_accno = '' if ($init);
730 $selected_taxchart ||= $taxchart_init;
732 my $accno = qq|<td>| .
733 NTI($cgi->popup_menu('-name' => "accno_$i",
735 '-onChange' => "updateTaxes($i);",
736 '-style' => 'width:200px',
737 '-values' => \@chart_values,
738 '-labels' => \%chart_labels,
739 '-default' => $selected_accno_full))
740 . $cgi->hidden('-name' => "previous_accno_$i",
741 '-default' => $selected_accno_full)
743 my $tax_ddbox = qq|<td>| .
744 NTI($cgi->popup_menu('-name' => "taxchart_$i",
745 '-id' => "taxchart_$i",
746 '-style' => 'width:200px',
747 '-values' => \@taxchart_values,
748 '-labels' => \%taxchart_labels,
749 '-default' => $selected_taxchart))
752 my ($fx_transaction, $checked);
754 if ($form->{transfer}) {
755 $fx_transaction = qq|
756 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
761 if ($form->{"debit_$i"} != 0) {
762 $form->{totaldebit} += $form->{"debit_$i"};
763 if (!$form->{taxincluded}) {
764 $form->{totaldebit} += $form->{"tax_$i"};
767 $form->{totalcredit} += $form->{"credit_$i"};
768 if (!$form->{taxincluded}) {
769 $form->{totalcredit} += $form->{"tax_$i"};
773 for (qw(debit credit tax)) {
776 ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
780 if ($i < $form->{rowcount}) {
781 if ($form->{transfer}) {
782 $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
783 my $x = ($checked) ? "x" : "";
784 $fx_transaction = qq|
785 <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
788 $form->hide_form("accno_$i");
791 if ($form->{transfer}) {
792 $fx_transaction = qq|
793 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
798 my $debitreadonly = "";
799 my $creditreadonly = "";
800 if ($i == $form->{rowcount}) {
801 if ($form->{debitlock}) {
802 $debitreadonly = "readonly";
803 } elsif ($form->{creditlock}) {
804 $creditreadonly = "readonly";
809 NTI($cgi->popup_menu('-name' => "project_id_$i",
810 '-values' => \@project_values,
811 '-labels' => \%project_labels,
812 '-default' => $form->{"project_id_$i"} ));
813 my $projectnumber_hidden = qq|
814 <input type="hidden" name="project_id_$i" value="$form->{"project_id_$i"}">|;
816 my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : '';
818 print qq|<tr valign=top>
820 <td id="chart_balance_$i" align="right"> </td>
822 <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
823 <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
824 <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
827 if ($form->{show_details}) {
831 <td>$projectnumber</td>
837 $projectnumber_hidden
845 $form->hide_form(qw(rowcount selectaccno));
847 $main::lxdebug->leave_sub();
852 return ($::instance_conf->get_gl_changeable == 2) ? ($::form->current_date(\%::myconfig) eq $::form->{gldate}) : ($::instance_conf->get_gl_changeable == 1);
856 $::lxdebug->enter_sub;
857 $::auth->assert('gl_transactions');
861 my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount};
863 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS",
865 "old_id" => \@old_project_ids },
866 "charts" => { "key" => "ALL_CHARTS",
867 "transdate" => $::form->{transdate} });
869 GL->get_chart_balances('charts' => $::form->{ALL_CHARTS});
871 my $title = $::form->{title};
872 $::form->{title} = $::locale->text("$title General Ledger Transaction");
873 # $locale->text('Add General Ledger Transaction')
874 # $locale->text('Edit General Ledger Transaction')
876 map { $::form->{$_} =~ s/\"/"/g }
879 $::form->{selectdepartment} =~ s/ selected//;
880 $::form->{selectdepartment} =~
881 s/option>\Q$::form->{department}\E/option selected>$::form->{department}/;
884 $::request->{layout}->focus("#reference");
885 $::form->{taxincluded} = "1";
887 $::request->{layout}->focus("#accno_$::form->{rowcount}");
890 $::form->{previous_id} ||= "--";
891 $::form->{previous_gldate} ||= "--";
894 print $::form->parse_html_template('gl/form_header', {
895 hide_title => $title,
896 readonly => $::form->{id} && ($::form->{locked} || !_get_radieren()),
899 $::lxdebug->leave_sub;
904 $::lxdebug->enter_sub;
905 $::auth->assert('gl_transactions');
907 my ($follow_ups, $follow_ups_due);
910 $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
911 $follow_ups_due = sum map { $_->{due} * 1 } @{ $follow_ups || [] };
914 print $::form->parse_html_template('gl/form_footer', {
915 radieren => _get_radieren(),
916 follow_ups => $follow_ups,
917 follow_ups_due => $follow_ups_due,
920 $::lxdebug->leave_sub;
924 $main::lxdebug->enter_sub();
926 my $form = $main::form;
927 my $locale = $main::locale;
932 <form method=post action=gl.pl>
935 map { $form->{$_} =~ s/\"/"/g } qw(reference description);
937 delete $form->{header};
939 foreach my $key (keys %$form) {
940 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
941 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
945 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
948 . $locale->text('Are you sure you want to delete Transaction')
949 . qq| $form->{reference}</h4>
951 <input name=action class=submit type=submit value="|
952 . $locale->text('Yes') . qq|">
955 $main::lxdebug->leave_sub();
960 $main::lxdebug->enter_sub();
962 my $form = $main::form;
963 my %myconfig = %main::myconfig;
964 my $locale = $main::locale;
966 if (GL->delete_transaction(\%myconfig, \%$form)){
968 if(!exists $form->{addition} && $form->{id} ne "") {
969 $form->{snumbers} = qq|gltransaction_| . $form->{id};
970 $form->{addition} = "DELETED";
971 $form->{what_done} = "gl_transaction";
974 # /saving the history
975 $form->redirect($locale->text('Transaction deleted!'))
977 $form->error($locale->text('Cannot delete transaction!'));
978 $main::lxdebug->leave_sub();
982 sub post_transaction {
983 $main::lxdebug->enter_sub();
985 my $form = $main::form;
986 my %myconfig = %main::myconfig;
987 my $locale = $main::locale;
989 # check if there is something in reference and date
990 $form->isblank("reference", $locale->text('Reference missing!'));
991 $form->isblank("transdate", $locale->text('Transaction Date missing!'));
992 $form->isblank("description", $locale->text('Description missing!'));
994 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
995 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1002 my $creditcount = 0;
1004 my %split_safety = ();
1006 my $dbh = SL::DB->client->dbh;
1007 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
1008 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
1010 my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
1012 for my $i (1 .. $form->{rowcount}) {
1013 next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
1015 for (qw(debit credit tax)) {
1016 $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
1020 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
1022 $split_safety{ $form->{"debit_$i"} <=> 0 }++;
1023 $split_safety{ - $form->{"credit_$i"} <=> 0 }++;
1031 if (($debitcount >= 2) && ($creditcount == 2)) {
1032 $form->{"credit_$i"} = 0;
1033 $form->{"tax_$i"} = 0;
1035 $form->{creditlock} = 1;
1037 if (($creditcount >= 2) && ($debitcount == 2)) {
1038 $form->{"debit_$i"} = 0;
1039 $form->{"tax_$i"} = 0;
1041 $form->{debitlock} = 1;
1043 if (($creditcount == 1) && ($debitcount == 2)) {
1044 $form->{creditlock} = 1;
1046 if (($creditcount == 2) && ($debitcount == 1)) {
1047 $form->{debitlock} = 1;
1049 if ($debitcredit && $credittax) {
1050 $form->{"taxchart_$i"} = "$notax_id--0.00";
1052 if (!$debitcredit && $debittax) {
1053 $form->{"taxchart_$i"} = "$notax_id--0.00";
1055 my $amount = ($form->{"debit_$i"} == 0)
1056 ? $form->{"credit_$i"}
1057 : $form->{"debit_$i"};
1059 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1060 $form->{"taxchart_$i"} = "$notax_id--0.00";
1061 $form->{"tax_$i"} = 0;
1063 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1064 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
1065 if (!$iswithouttax) {
1072 my ($tmpnetamount,$tmpdiff);
1073 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
1075 $form->{"debit_$i"} = $tmpnetamount;
1077 $form->{"credit_$i"} = $tmpnetamount;
1081 $form->{"tax_$i"} = 0;
1084 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1088 if ($split_safety{-1} > 1 && $split_safety{1} > 1) {
1089 $::form->error($::locale->text("Split entry detected. The values you have entered will result in an entry with more than one position on both debit and credit. " .
1090 "Due to known problems involving accounting software kivitendo does not allow these."));
1093 for my $i (1 .. $count) {
1095 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1098 for my $i ($count + 1 .. $form->{rowcount}) {
1099 for (@flds) { delete $form->{"${_}_$i"} }
1102 my ($debit, $credit, $taxtotal);
1103 for my $i (1 .. $form->{rowcount}) {
1104 my $dr = $form->{"debit_$i"};
1105 my $cr = $form->{"credit_$i"};
1106 my $tax = $form->{"tax_$i"};
1108 $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
1110 $debit += $dr + $tax if $dr;
1111 $credit += $cr + $tax if $cr;
1112 $taxtotal += $tax if $form->{taxincluded}
1115 $form->{taxincluded} = 0 if !$taxtotal;
1117 # this is just for the wise guys
1119 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
1120 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
1121 $form->error($locale->text('Cannot post transaction for a closed period!'))
1122 if ($form->date_closed($form->{"transdate"}, \%myconfig));
1123 if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1124 $form->error($locale->text('Out of balance transaction!'));
1127 if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1128 $form->error($locale->text('Empty transaction!'));
1131 if ((my $errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1134 $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1135 $err[2] = $locale->text('Debit and credit out of balance!');
1136 $err[3] = $locale->text('Cannot post a transaction without a value!');
1138 $form->error($err[$errno]);
1140 undef($form->{callback});
1141 # saving the history
1142 if(!exists $form->{addition} && $form->{id} ne "") {
1143 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1144 $form->{addition} = "POSTED";
1145 $form->{what_done} = "gl transaction";
1146 $form->save_history;
1148 # /saving the history
1150 $main::lxdebug->leave_sub();
1154 $main::lxdebug->enter_sub();
1156 $main::auth->assert('gl_transactions');
1158 my $form = $main::form;
1159 my $locale = $main::locale;
1161 if ($::myconfig{mandatory_departments} && !$form->{department}) {
1162 $form->{saved_message} = $::locale->text('You have to specify a department.');
1167 $form->{title} = $locale->text("$form->{title} General Ledger Transaction");
1168 $form->{storno} = 0;
1172 $form->{callback} = build_std_url("action=add", "show_details");
1173 $form->redirect($form->{callback});
1175 $main::lxdebug->leave_sub();
1179 $main::lxdebug->enter_sub();
1181 $main::auth->assert('gl_transactions');
1183 my $form = $main::form;
1187 $main::lxdebug->leave_sub();
1192 $main::lxdebug->enter_sub();
1194 $main::auth->assert('gl_transactions');
1196 my $form = $main::form;
1197 my %myconfig = %main::myconfig;
1198 my $locale = $main::locale;
1200 # don't cancel cancelled transactions
1201 if (IS->has_storno(\%myconfig, $form, 'gl')) {
1202 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1203 $form->error($locale->text("Transaction has already been cancelled!"));
1206 GL->storno($form, \%myconfig, $form->{id});
1208 # saving the history
1209 if(!exists $form->{addition} && $form->{id} ne "") {
1210 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1211 $form->{addition} = "STORNO";
1212 $form->{what_done} = "gl_transaction";
1213 $form->save_history;
1215 # /saving the history
1217 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1219 $main::lxdebug->leave_sub();
1223 call_sub($main::form->{nextsub});
1226 sub get_tax_dropdown {
1227 $main::lxdebug->enter_sub();
1229 my $form = $main::form;
1230 my @tax_accounts = GL->get_tax_dropdown($form->{accno});
1232 foreach my $item (@tax_accounts) {
1233 $item->{taxdescription} = $::locale->{iconv_utf8}->convert($item->{taxdescription});
1234 $item->{taxdescription} .= ' ' . $form->round_amount($item->{rate} * 100);
1237 $form->{TAX_ACCOUNTS} = [ @tax_accounts ];
1239 print $form->ajax_response_header, $form->parse_html_template("gl/update_tax_accounts");
1241 $main::lxdebug->leave_sub();