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 use SL::Locale::String qw(t8);
49 require "bin/mozilla/common.pl";
50 require "bin/mozilla/reportgenerator.pl";
52 # this is for our long dates
53 # $locale->text('January')
54 # $locale->text('February')
55 # $locale->text('March')
56 # $locale->text('April')
57 # $locale->text('May ')
58 # $locale->text('June')
59 # $locale->text('July')
60 # $locale->text('August')
61 # $locale->text('September')
62 # $locale->text('October')
63 # $locale->text('November')
64 # $locale->text('December')
66 # this is for our short month
67 # $locale->text('Jan')
68 # $locale->text('Feb')
69 # $locale->text('Mar')
70 # $locale->text('Apr')
71 # $locale->text('May')
72 # $locale->text('Jun')
73 # $locale->text('Jul')
74 # $locale->text('Aug')
75 # $locale->text('Sep')
76 # $locale->text('Oct')
77 # $locale->text('Nov')
78 # $locale->text('Dec')
81 $main::lxdebug->enter_sub();
83 $main::auth->assert('gl_transactions');
85 my $form = $main::form;
86 my %myconfig = %main::myconfig;
88 $form->{title} = "Add";
90 $form->{callback} = "gl.pl?action=add" unless $form->{callback};
92 # we use this only to set a default date
93 # yep. aber er holt hier auch schon ALL_CHARTS. Aufwand / Nutzen? jb
94 GL->transaction(\%myconfig, \%$form);
96 $form->{rowcount} = 2;
102 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
104 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
107 $main::lxdebug->leave_sub();
111 sub prepare_transaction {
112 $main::lxdebug->enter_sub();
114 $main::auth->assert('gl_transactions');
116 my $form = $main::form;
117 my %myconfig = %main::myconfig;
119 GL->transaction(\%myconfig, \%$form);
121 $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
123 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
128 foreach my $ref (@{ $form->{GL} }) {
130 if ($tax && ($ref->{accno} eq $taxaccno)) {
131 $form->{"tax_$j"} = abs($ref->{amount});
132 $form->{"taxchart_$j"} = $ref->{id} . "--" . $ref->{taxrate};
133 if ($form->{taxincluded}) {
134 if ($ref->{amount} < 0) {
135 $form->{"debit_$j"} += $form->{"tax_$j"};
137 $form->{"credit_$j"} += $form->{"tax_$j"};
140 $form->{"project_id_$j"} = $ref->{project_id};
143 $form->{"accno_id_$i"} = $ref->{chart_id};
144 for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
145 if ($ref->{amount} < 0) {
146 $form->{totaldebit} -= $ref->{amount};
147 $form->{"debit_$i"} = $ref->{amount} * -1;
149 $form->{totalcredit} += $ref->{amount};
150 $form->{"credit_$i"} = $ref->{amount};
152 $form->{"taxchart_$i"} = $ref->{id}."--0.00000";
153 $form->{"project_id_$i"} = $ref->{project_id};
156 if ($ref->{taxaccno} && !$tax) {
157 $taxaccno = $ref->{taxaccno};
165 $form->{rowcount} = $i;
167 ($form->datetonum($form->{transdate}, \%myconfig) <=
168 $form->datetonum($form->{closedto}, \%myconfig));
170 $main::lxdebug->leave_sub();
174 $main::lxdebug->enter_sub();
176 $main::auth->assert('gl_transactions');
178 my $form = $main::form;
179 my %myconfig = %main::myconfig;
181 prepare_transaction();
183 $form->{title} = "Edit";
185 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
187 if ($form->{reference} && $::instance_conf->get_webdav) {
188 my $webdav = SL::Webdav->new(
189 type => 'general_ledger',
190 number => $form->{reference},
192 my $webdav_path = $webdav->webdav_path;
193 my @all_objects = $webdav->get_all_objects;
194 @{ $form->{WEBDAV} } = map { { name => $_->filename,
196 link => File::Spec->catfile($_->full_filedescriptor),
203 $main::lxdebug->leave_sub();
208 $::lxdebug->enter_sub;
209 $::auth->assert('general_ledger | gl_transactions');
212 projects => { key => "ALL_PROJECTS", all => 1 },
214 $::form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
215 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
218 print $::form->parse_html_template('gl/search', {
219 employee_label => sub { "$_[0]{id}--$_[0]{name}" },
222 $::lxdebug->leave_sub;
225 sub create_subtotal_row {
226 $main::lxdebug->enter_sub();
228 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
230 my $form = $main::form;
231 my %myconfig = %main::myconfig;
233 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
235 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
237 map { $totals->{$_} = 0 } @{ $subtotal_columns };
239 $main::lxdebug->leave_sub();
244 sub generate_report {
245 $main::lxdebug->enter_sub();
247 $main::auth->assert('general_ledger | gl_transactions');
249 my $form = $main::form;
250 my %myconfig = %main::myconfig;
251 my $locale = $main::locale;
253 # 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
255 # <form method=post action=gl.pl>
256 # <input type=hidden name=sort value=datesort> # form->{sort} setzen
257 # <input type=hidden name=nextsub value=generate_report>
259 # anhand von neuer Variable datesort wird jetzt $form->{sort} auf transdate oder gldate gesetzt
260 # damit ist die Hidden Variable "sort" wahrscheinlich sogar überflüssig
262 # ändert man die Sortierreihenfolge per Klick auf eine der Überschriften wird die Variable "sort" per GET übergeben, z.B. id,transdate, gldate, ...
263 # 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
265 if ( $form->{sort} eq 'datesort' ) { # sollte bei einem Post (Aufruf aus Suchmaske) immer wahr sein
266 # je nachdem ob in Suchmaske "transdate" oder "gldate" ausgesucht wurde erstes Suchergebnis entsprechend sortieren
267 $form->{sort} = $form->{datesort};
271 report_generator_set_default_sort("$form->{datesort}", 1);
272 # report_generator_set_default_sort('transdate', 1);
274 GL->all_transactions(\%myconfig, \%$form);
276 my %acctype = ('A' => $locale->text('Asset'),
277 'C' => $locale->text('Contra'),
278 'L' => $locale->text('Liability'),
279 'Q' => $locale->text('Equity'),
280 'I' => $locale->text('Revenue'),
281 'E' => $locale->text('Expense'),);
283 $form->{title} = $locale->text('Journal');
284 if ($form->{category} ne 'X') {
285 $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
288 $form->{landscape} = 1;
290 my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
293 gldate transdate id reference description
294 notes source debit debit_accno
295 credit credit_accno debit_tax debit_tax_accno
296 credit_tax credit_tax_accno projectnumbers balance employee
299 # add employee here, so that variable is still known and passed in url when choosing a different sort order in resulting table
300 my @hidden_variables = qw(accno source reference description notes project_id datefrom dateto employee_id datesort category l_subtotal);
301 push @hidden_variables, map { "l_${_}" } @columns;
303 my $employee = $form->{employee_id} ? SL::DB::Employee->new(id => $form->{employee_id})->load->name : '';
305 my (@options, @date_options);
306 push @options, $locale->text('Account') . " : $form->{accno} $form->{account_description}" if ($form->{accno});
307 push @options, $locale->text('Source') . " : $form->{source}" if ($form->{source});
308 push @options, $locale->text('Reference') . " : $form->{reference}" if ($form->{reference});
309 push @options, $locale->text('Description') . " : $form->{description}" if ($form->{description});
310 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
311 push @options, $locale->text('Employee') . " : $employee" if $employee;
312 my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Invoice Date') : $locale->text('Booking Date');
313 push @date_options, "$datesorttext" if ($form->{datesort} and ($form->{datefrom} or $form->{dateto}));
314 push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1) if ($form->{datefrom});
315 push @date_options, $locale->text('Bis'), $locale->date(\%myconfig, $form->{dateto}, 1) if ($form->{dateto});
316 push @options, join(' ', @date_options) if (scalar @date_options);
318 if ($form->{department_id}) {
319 my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
320 push @options, $locale->text('Department') . " : " . $department->description;
323 my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
325 $form->{l_credit_accno} = 'Y';
326 $form->{l_debit_accno} = 'Y';
327 $form->{l_credit_tax} = 'Y';
328 $form->{l_debit_tax} = 'Y';
329 # $form->{l_gldate} = 'Y'; # Spalte mit gldate immer anzeigen
330 $form->{l_credit_tax_accno} = 'Y';
331 $form->{l_datesort} = 'Y';
332 $form->{l_debit_tax_accno} = 'Y';
333 $form->{l_balance} = $form->{accno} ? 'Y' : '';
336 'id' => { 'text' => $locale->text('ID'), },
337 'transdate' => { 'text' => $locale->text('Invoice Date'), },
338 'gldate' => { 'text' => $locale->text('Booking Date'), },
339 'reference' => { 'text' => $locale->text('Reference'), },
340 'source' => { 'text' => $locale->text('Source'), },
341 'description' => { 'text' => $locale->text('Description'), },
342 'notes' => { 'text' => $locale->text('Notes'), },
343 'debit' => { 'text' => $locale->text('Debit'), },
344 'debit_accno' => { 'text' => $locale->text('Debit Account'), },
345 'credit' => { 'text' => $locale->text('Credit'), },
346 'credit_accno' => { 'text' => $locale->text('Credit Account'), },
347 'debit_tax' => { 'text' => $locale->text('Debit Tax'), },
348 'debit_tax_accno' => { 'text' => $locale->text('Debit Tax Account'), },
349 'credit_tax' => { 'text' => $locale->text('Credit Tax'), },
350 'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
351 'balance' => { 'text' => $locale->text('Balance'), },
352 'projectnumbers' => { 'text' => $locale->text('Project Numbers'), },
353 'employee' => { 'text' => $locale->text('Employee'), },
356 foreach my $name (qw(id transdate gldate reference description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
357 my $sortname = $name =~ m/accno/ ? 'accno' : $name;
358 my $sortdir = $sortname eq $form->{sort} ? 1 - $form->{sortdir} : $form->{sortdir};
359 $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
362 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
363 map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
365 my %column_alignment;
366 map { $column_alignment{$_} = 'right' } qw(balance id debit credit debit_tax credit_tax balance);
367 map { $column_alignment{$_} = 'center' } qw(transdate gldate reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
368 map { $column_alignment{$_} = 'left' } qw(description source notes);
369 map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
371 my $report = SL::ReportGenerator->new(\%myconfig, $form);
373 $report->set_columns(%column_defs);
374 $report->set_column_order(@columns);
376 $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir));
378 $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
380 $report->set_options('top_info_text' => join("\n", @options),
381 'output_format' => 'HTML',
382 'title' => $form->{title},
383 'attachment_basename' => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
385 $report->set_options_from_form();
386 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
388 # add sort to callback
389 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
392 my @totals_columns = qw(debit credit debit_tax credit_tax);
393 my %subtotals = map { $_ => 0 } @totals_columns;
394 my %totals = map { $_ => 0 } @totals_columns;
397 foreach my $ref (@{ $form->{GL} }) {
401 foreach my $key (qw(debit credit debit_tax credit_tax)) {
403 foreach my $idx (sort keys(%{ $ref->{$key} })) {
404 my $value = $ref->{$key}->{$idx};
405 $subtotals{$key} += $value;
406 $totals{$key} += $value;
407 if ($key =~ /debit.*/) {
412 $form->{balance} = $form->{balance} + $value * $ml;
413 push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
417 foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
418 my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
419 $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
423 map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
426 if ($form->{balance} < 0) {
429 } elsif ($form->{balance} > 0) {
433 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
436 $row->{balance}->{data} = $data;
437 $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
439 map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes gldate employee);
441 map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
443 foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
444 $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
447 map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
449 $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
451 my $row_set = [ $row ];
453 if ( ($form->{l_subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
454 && (($idx == (scalar @{ $form->{GL} } - 1))
455 || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
456 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
459 $report->add_data($row_set);
464 # = 0 for balanced ledger
465 my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
467 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
470 if ($form->{balance} < 0) {
473 } elsif ($form->{balance} > 0) {
477 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
480 $row->{balance}->{data} = $data;
482 if ( !$form->{report_generator_csv_options_for_import} ) {
483 $report->add_separator();
484 $report->add_data($row);
487 my $raw_bottom_info_text;
489 if (!$form->{accno} && (abs($balanced_ledger) > 0.001)) {
490 $raw_bottom_info_text .=
491 '<p><span class="unbalanced_ledger">'
492 . $locale->text('Unbalanced Ledger')
494 . $form->format_amount(\%myconfig, $balanced_ledger, 3)
498 $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
500 $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
502 $report->generate_with_headers();
504 $main::lxdebug->leave_sub();
508 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
509 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
514 $main::lxdebug->enter_sub();
516 $main::auth->assert('gl_transactions');
518 my $form = $main::form;
519 my %myconfig = %main::myconfig;
521 $form->{oldtransdate} = $form->{transdate};
529 my ($debitcredit, $amount);
531 my $dbh = SL::DB->client->dbh;
532 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
533 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
536 qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
538 for my $i (1 .. $form->{rowcount}) {
540 unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
541 for (qw(debit credit tax)) {
543 $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
547 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
554 if (($debitcount >= 2) && ($creditcount == 2)) {
555 $form->{"credit_$i"} = 0;
556 $form->{"tax_$i"} = 0;
558 $form->{creditlock} = 1;
560 if (($creditcount >= 2) && ($debitcount == 2)) {
561 $form->{"debit_$i"} = 0;
562 $form->{"tax_$i"} = 0;
564 $form->{debitlock} = 1;
566 if (($creditcount == 1) && ($debitcount == 2)) {
567 $form->{creditlock} = 1;
569 if (($creditcount == 2) && ($debitcount == 1)) {
570 $form->{debitlock} = 1;
572 if ($debitcredit && $credittax) {
573 $form->{"taxchart_$i"} = "$notax_id--0.00";
575 if (!$debitcredit && $debittax) {
576 $form->{"taxchart_$i"} = "$notax_id--0.00";
579 ($form->{"debit_$i"} == 0)
580 ? $form->{"credit_$i"}
581 : $form->{"debit_$i"};
583 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
584 $form->{"taxchart_$i"} = "$notax_id--0.00";
585 $form->{"tax_$i"} = 0;
587 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
588 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
589 if (!$iswithouttax) {
596 my ($tmpnetamount,$tmpdiff);
597 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
599 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
604 for my $i (1 .. $count) {
606 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
609 for my $i ($count + 1 .. $form->{rowcount}) {
610 for (@flds) { delete $form->{"${_}_$i"} }
613 $form->{rowcount} = $count + 1;
616 $main::lxdebug->leave_sub();
622 $main::lxdebug->enter_sub();
624 $main::auth->assert('gl_transactions');
626 my $form = $main::form;
627 my %myconfig = %main::myconfig;
631 # for $i (1 .. $form->{rowcount}) {
632 # $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
633 # $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
637 &display_rows($init);
639 $main::lxdebug->leave_sub();
645 $main::lxdebug->enter_sub();
647 $main::auth->assert('gl_transactions');
649 my $form = $main::form;
650 my %myconfig = %main::myconfig;
651 my $cgi = $::request->{cgi};
653 my %balances = GL->get_chart_balances(map { $_->{id} } @{ $form->{ALL_CHARTS} });
655 $form->{debit_1} = 0 if !$form->{"debit_1"};
656 $form->{totaldebit} = 0;
657 $form->{totalcredit} = 0;
659 my %project_labels = ();
660 my @project_values = ("");
661 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
662 push(@project_values, $item->{"id"});
663 $project_labels{$item->{"id"}} = $item->{"projectnumber"};
666 my %charts_by_id = map { ($_->{id} => $_) } @{ $::form->{ALL_CHARTS} };
667 my $default_chart = $::form->{ALL_CHARTS}[0];
668 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
670 my ($source, $memo, $source_hidden, $memo_hidden);
671 for my $i (1 .. $form->{rowcount}) {
672 if ($form->{show_details}) {
674 <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
676 <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
679 <input type="hidden" name="source_$i" value="$form->{"source_$i"}" size="16">|;
681 <input type="hidden" name="memo_$i" value="$form->{"memo_$i"}" size="16">|;
684 my %taxchart_labels = ();
685 my @taxchart_values = ();
687 my $accno_id = $::form->{"accno_id_$i"};
688 my $chart = $charts_by_id{$accno_id} // $default_chart;
689 $accno_id = $chart->{id};
690 my $chart_has_changed = $::form->{"previous_accno_id_$i"} && ($accno_id != $::form->{"previous_accno_id_$i"});
691 my ($first_taxchart, $default_taxchart, $taxchart_to_use);
693 foreach my $item ( GL->get_active_taxes_for_chart($accno_id, $transdate) ) {
694 my $key = $item->id . "--" . $item->rate;
695 $first_taxchart //= $item;
696 $default_taxchart = $item if $item->{is_default};
697 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
699 push(@taxchart_values, $key);
700 $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
703 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
704 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
706 my $accno = qq|<td>| .
707 $::request->presenter->chart_picker("accno_id_$i", $accno_id, style => "width: 300px") .
708 $::request->presenter->hidden_tag("previous_accno_id_$i", $accno_id)
710 my $tax_ddbox = qq|<td>| .
711 NTI($cgi->popup_menu('-name' => "taxchart_$i",
712 '-id' => "taxchart_$i",
713 '-style' => 'width:200px',
714 '-values' => \@taxchart_values,
715 '-labels' => \%taxchart_labels,
716 '-default' => $selected_taxchart))
719 my ($fx_transaction, $checked);
721 if ($form->{transfer}) {
722 $fx_transaction = qq|
723 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
728 if ($form->{"debit_$i"} != 0) {
729 $form->{totaldebit} += $form->{"debit_$i"};
730 if (!$form->{taxincluded}) {
731 $form->{totaldebit} += $form->{"tax_$i"};
734 $form->{totalcredit} += $form->{"credit_$i"};
735 if (!$form->{taxincluded}) {
736 $form->{totalcredit} += $form->{"tax_$i"};
740 for (qw(debit credit tax)) {
743 ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
747 if ($i < $form->{rowcount}) {
748 if ($form->{transfer}) {
749 $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
750 my $x = ($checked) ? "x" : "";
751 $fx_transaction = qq|
752 <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
755 $form->hide_form("accno_$i");
758 if ($form->{transfer}) {
759 $fx_transaction = qq|
760 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
765 my $debitreadonly = "";
766 my $creditreadonly = "";
767 if ($i == $form->{rowcount}) {
768 if ($form->{debitlock}) {
769 $debitreadonly = "readonly";
770 } elsif ($form->{creditlock}) {
771 $creditreadonly = "readonly";
776 NTI($cgi->popup_menu('-name' => "project_id_$i",
777 '-values' => \@project_values,
778 '-labels' => \%project_labels,
779 '-default' => $form->{"project_id_$i"} ));
780 my $projectnumber_hidden = qq|
781 <input type="hidden" name="project_id_$i" value="$form->{"project_id_$i"}">|;
783 my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : '';
784 my $balance = $form->format_amount(\%::myconfig, $balances{$accno_id} // 0, 2, 'DRCR');
786 print qq|<tr valign=top>
788 <td id="chart_balance_$i" align="right">${balance}</td>
790 <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
791 <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
792 <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
795 if ($form->{show_details}) {
799 <td>$projectnumber</td>
805 $projectnumber_hidden
813 $form->hide_form(qw(rowcount selectaccno));
815 $main::lxdebug->leave_sub();
820 return ($::instance_conf->get_gl_changeable == 2) ? ($::form->current_date(\%::myconfig) eq $::form->{gldate}) : ($::instance_conf->get_gl_changeable == 1);
824 $::lxdebug->enter_sub;
825 $::auth->assert('gl_transactions');
829 $::request->layout->add_javascripts("autocomplete_chart.js", "kivi.GL.js");
831 my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount};
833 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS",
835 "old_id" => \@old_project_ids },
837 "charts" => { "key" => "ALL_CHARTS",
838 "transdate" => $::form->{transdate} });
840 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
842 my $title = $::form->{title};
843 $::form->{title} = $::locale->text("$title General Ledger Transaction");
844 # $locale->text('Add General Ledger Transaction')
845 # $locale->text('Edit General Ledger Transaction')
847 map { $::form->{$_} =~ s/\"/"/g }
851 $::request->{layout}->focus("#reference");
852 $::form->{taxincluded} = "1";
854 $::request->{layout}->focus("#accno_id_$::form->{rowcount}_name");
857 $::form->{previous_id} ||= "--";
858 $::form->{previous_gldate} ||= "--";
861 print $::form->parse_html_template('gl/form_header', {
862 hide_title => $title,
863 readonly => $::form->{id} && ($::form->{locked} || !_get_radieren()),
866 $::lxdebug->leave_sub;
871 $::lxdebug->enter_sub;
872 $::auth->assert('gl_transactions');
874 my ($follow_ups, $follow_ups_due);
877 $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
878 $follow_ups_due = sum map { $_->{due} * 1 } @{ $follow_ups || [] };
881 print $::form->parse_html_template('gl/form_footer', {
882 radieren => _get_radieren(),
883 follow_ups => $follow_ups,
884 follow_ups_due => $follow_ups_due,
887 $::lxdebug->leave_sub;
891 $main::lxdebug->enter_sub();
893 my $form = $main::form;
894 my $locale = $main::locale;
899 <form method=post action=gl.pl>
902 map { $form->{$_} =~ s/\"/"/g } qw(reference description);
904 delete $form->{header};
906 foreach my $key (keys %$form) {
907 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
908 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
912 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
915 . $locale->text('Are you sure you want to delete Transaction')
916 . qq| $form->{reference}</h4>
918 <input name=action class=submit type=submit value="|
919 . $locale->text('Yes') . qq|">
922 $main::lxdebug->leave_sub();
927 $main::lxdebug->enter_sub();
929 my $form = $main::form;
930 my %myconfig = %main::myconfig;
931 my $locale = $main::locale;
933 if (GL->delete_transaction(\%myconfig, \%$form)){
935 if(!exists $form->{addition} && $form->{id} ne "") {
936 $form->{snumbers} = qq|gltransaction_| . $form->{id};
937 $form->{addition} = "DELETED";
938 $form->{what_done} = "gl_transaction";
941 # /saving the history
942 $form->redirect($locale->text('Transaction deleted!'))
944 $form->error($locale->text('Cannot delete transaction!'));
945 $main::lxdebug->leave_sub();
949 sub post_transaction {
950 $main::lxdebug->enter_sub();
952 my $form = $main::form;
953 my %myconfig = %main::myconfig;
954 my $locale = $main::locale;
956 # check if there is something in reference and date
957 $form->isblank("reference", $locale->text('Reference missing!'));
958 $form->isblank("transdate", $locale->text('Transaction Date missing!'));
959 $form->isblank("description", $locale->text('Description missing!'));
961 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
962 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
971 my %split_safety = ();
973 my $dbh = SL::DB->client->dbh;
974 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
975 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
977 my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
979 for my $i (1 .. $form->{rowcount}) {
980 next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
982 for (qw(debit credit tax)) {
983 $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
987 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
989 $split_safety{ $form->{"debit_$i"} <=> 0 }++;
990 $split_safety{ - $form->{"credit_$i"} <=> 0 }++;
998 if (($debitcount >= 2) && ($creditcount == 2)) {
999 $form->{"credit_$i"} = 0;
1000 $form->{"tax_$i"} = 0;
1002 $form->{creditlock} = 1;
1004 if (($creditcount >= 2) && ($debitcount == 2)) {
1005 $form->{"debit_$i"} = 0;
1006 $form->{"tax_$i"} = 0;
1008 $form->{debitlock} = 1;
1010 if (($creditcount == 1) && ($debitcount == 2)) {
1011 $form->{creditlock} = 1;
1013 if (($creditcount == 2) && ($debitcount == 1)) {
1014 $form->{debitlock} = 1;
1016 if ($debitcredit && $credittax) {
1017 $form->{"taxchart_$i"} = "$notax_id--0.00";
1019 if (!$debitcredit && $debittax) {
1020 $form->{"taxchart_$i"} = "$notax_id--0.00";
1022 my $amount = ($form->{"debit_$i"} == 0)
1023 ? $form->{"credit_$i"}
1024 : $form->{"debit_$i"};
1026 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1027 $form->{"taxchart_$i"} = "$notax_id--0.00";
1028 $form->{"tax_$i"} = 0;
1030 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1031 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
1032 if (!$iswithouttax) {
1039 my ($tmpnetamount,$tmpdiff);
1040 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
1042 $form->{"debit_$i"} = $tmpnetamount;
1044 $form->{"credit_$i"} = $tmpnetamount;
1048 $form->{"tax_$i"} = 0;
1051 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1055 if ($split_safety{-1} > 1 && $split_safety{1} > 1) {
1056 $::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. " .
1057 "Due to known problems involving accounting software kivitendo does not allow these."));
1060 for my $i (1 .. $count) {
1062 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1065 for my $i ($count + 1 .. $form->{rowcount}) {
1066 for (@flds) { delete $form->{"${_}_$i"} }
1069 my ($debit, $credit, $taxtotal);
1070 for my $i (1 .. $form->{rowcount}) {
1071 my $dr = $form->{"debit_$i"};
1072 my $cr = $form->{"credit_$i"};
1073 my $tax = $form->{"tax_$i"};
1075 $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
1077 $debit += $dr + $tax if $dr;
1078 $credit += $cr + $tax if $cr;
1079 $taxtotal += $tax if $form->{taxincluded}
1082 $form->{taxincluded} = 0 if !$taxtotal;
1084 # this is just for the wise guys
1086 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
1087 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
1088 $form->error($locale->text('Cannot post transaction for a closed period!'))
1089 if ($form->date_closed($form->{"transdate"}, \%myconfig));
1090 if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1091 $form->error($locale->text('Out of balance transaction!'));
1094 if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1095 $form->error($locale->text('Empty transaction!'));
1098 if ((my $errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1101 $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1102 $err[2] = $locale->text('Debit and credit out of balance!');
1103 $err[3] = $locale->text('Cannot post a transaction without a value!');
1105 $form->error($err[$errno]);
1107 undef($form->{callback});
1108 # saving the history
1109 if(!exists $form->{addition} && $form->{id} ne "") {
1110 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1111 $form->{addition} = "POSTED";
1112 $form->{what_done} = "gl transaction";
1113 $form->save_history;
1115 # /saving the history
1117 $main::lxdebug->leave_sub();
1121 $main::lxdebug->enter_sub();
1123 $main::auth->assert('gl_transactions');
1125 my $form = $main::form;
1126 my $locale = $main::locale;
1128 if ($::myconfig{mandatory_departments} && !$form->{department_id}) {
1129 $form->{saved_message} = $::locale->text('You have to specify a department.');
1134 $form->{title} = $locale->text("$form->{title} General Ledger Transaction");
1135 $form->{storno} = 0;
1138 if ($::instance_conf->get_webdav) {
1139 SL::Webdav->new(type => 'general_ledger',
1140 number => $form->{reference},
1144 $form->{callback} = build_std_url("action=add", "show_details");
1145 $form->redirect($form->{callback});
1147 $main::lxdebug->leave_sub();
1151 $main::lxdebug->enter_sub();
1153 $main::auth->assert('gl_transactions');
1155 my $form = $main::form;
1159 $main::lxdebug->leave_sub();
1164 $main::lxdebug->enter_sub();
1166 $main::auth->assert('gl_transactions');
1168 my $form = $main::form;
1169 my %myconfig = %main::myconfig;
1170 my $locale = $main::locale;
1172 # don't cancel cancelled transactions
1173 if (IS->has_storno(\%myconfig, $form, 'gl')) {
1174 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1175 $form->error($locale->text("Transaction has already been cancelled!"));
1178 GL->storno($form, \%myconfig, $form->{id});
1180 # saving the history
1181 if(!exists $form->{addition} && $form->{id} ne "") {
1182 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1183 $form->{addition} = "STORNO";
1184 $form->{what_done} = "gl_transaction";
1185 $form->save_history;
1187 # /saving the history
1189 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1191 $main::lxdebug->leave_sub();
1195 call_sub($main::form->{nextsub});
1198 sub get_tax_dropdown {
1199 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
1200 my @tax_accounts = GL->get_active_taxes_for_chart($::form->{accno_id}, $transdate);
1201 my $html = $::form->parse_html_template("gl/update_tax_accounts", { TAX_ACCOUNTS => \@tax_accounts });
1203 print $::form->ajax_response_header, $html;
1206 sub get_chart_balance {
1207 my %balances = GL->get_chart_balances($::form->{accno_id});
1208 my $balance = $::form->format_amount(\%::myconfig, $balances{ $::form->{accno_id} }, 2, 'DRCR');
1210 print $::form->ajax_response_header, $balance;