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;
103 $form->all_departments(\%myconfig);
104 if (@{ $form->{all_departments} || [] }) {
105 $form->{selectdepartment} = "<option>\n";
108 $form->{selectdepartment} .=
109 "<option>$_->{description}--$_->{id}\n"
110 } (@{ $form->{all_departments} || [] });
113 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
116 $main::lxdebug->leave_sub();
120 sub prepare_transaction {
121 $main::lxdebug->enter_sub();
123 $main::auth->assert('gl_transactions');
125 my $form = $main::form;
126 my %myconfig = %main::myconfig;
128 GL->transaction(\%myconfig, \%$form);
130 $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
133 $form->all_departments(\%myconfig);
134 if (@{ $form->{all_departments} || [] }) {
135 $form->{selectdepartment} = "<option>\n";
138 $form->{selectdepartment} .=
139 "<option>$_->{description}--$_->{id}\n"
140 } (@{ $form->{all_departments} || [] });
146 foreach my $ref (@{ $form->{GL} }) {
148 if ($tax && ($ref->{accno} eq $taxaccno)) {
149 $form->{"tax_$j"} = abs($ref->{amount});
150 $form->{"taxchart_$j"} = $ref->{id} . "--" . $ref->{taxrate};
151 if ($form->{taxincluded}) {
152 if ($ref->{amount} < 0) {
153 $form->{"debit_$j"} += $form->{"tax_$j"};
155 $form->{"credit_$j"} += $form->{"tax_$j"};
158 $form->{"project_id_$j"} = $ref->{project_id};
161 $form->{"accno_$i"} = "$ref->{accno}--$ref->{tax_id}";
162 for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
163 if ($ref->{amount} < 0) {
164 $form->{totaldebit} -= $ref->{amount};
165 $form->{"debit_$i"} = $ref->{amount} * -1;
167 $form->{totalcredit} += $ref->{amount};
168 $form->{"credit_$i"} = $ref->{amount};
170 $form->{"taxchart_$i"} = $ref->{id}."--0.00000";
171 $form->{"project_id_$i"} = $ref->{project_id};
174 if ($ref->{taxaccno} && !$tax) {
175 $taxaccno = $ref->{taxaccno};
183 $form->{rowcount} = $i;
185 ($form->datetonum($form->{transdate}, \%myconfig) <=
186 $form->datetonum($form->{closedto}, \%myconfig));
188 $main::lxdebug->leave_sub();
192 $main::lxdebug->enter_sub();
194 $main::auth->assert('gl_transactions');
196 my $form = $main::form;
197 my %myconfig = %main::myconfig;
199 prepare_transaction();
201 $form->{title} = "Edit";
203 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
205 if ($form->{reference} && $::instance_conf->get_webdav) {
206 my $webdav = SL::Webdav->new(
207 type => 'general_ledger',
208 number => $form->{reference},
210 my $webdav_path = $webdav->webdav_path;
211 my @all_objects = $webdav->get_all_objects;
212 @{ $form->{WEBDAV} } = map { { name => $_->filename,
214 link => File::Spec->catdir($webdav_path, $_->filename),
221 $main::lxdebug->leave_sub();
226 $::lxdebug->enter_sub;
227 $::auth->assert('general_ledger | gl_transactions');
229 $::form->all_departments(\%::myconfig);
231 projects => { key => "ALL_PROJECTS", all => 1 },
233 $::form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
236 print $::form->parse_html_template('gl/search', {
237 department_label => sub { ("$_[0]{description}--$_[0]{id}")x2 },
238 employee_label => sub { "$_[0]{id}--$_[0]{name}" },
241 $::lxdebug->leave_sub;
244 sub create_subtotal_row {
245 $main::lxdebug->enter_sub();
247 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
249 my $form = $main::form;
250 my %myconfig = %main::myconfig;
252 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
254 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
256 map { $totals->{$_} = 0 } @{ $subtotal_columns };
258 $main::lxdebug->leave_sub();
263 sub generate_report {
264 $main::lxdebug->enter_sub();
266 $main::auth->assert('general_ledger | gl_transactions');
268 my $form = $main::form;
269 my %myconfig = %main::myconfig;
270 my $locale = $main::locale;
272 # 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
274 # <form method=post action=gl.pl>
275 # <input type=hidden name=sort value=datesort> # form->{sort} setzen
276 # <input type=hidden name=nextsub value=generate_report>
278 # anhand von neuer Variable datesort wird jetzt $form->{sort} auf transdate oder gldate gesetzt
279 # damit ist die Hidden Variable "sort" wahrscheinlich sogar überflüssig
281 # ändert man die Sortierreihenfolge per Klick auf eine der Überschriften wird die Variable "sort" per GET übergeben, z.B. id,transdate, gldate, ...
282 # 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
284 if ( $form->{sort} eq 'datesort' ) { # sollte bei einem Post (Aufruf aus Suchmaske) immer wahr sein
285 # je nachdem ob in Suchmaske "transdate" oder "gldate" ausgesucht wurde erstes Suchergebnis entsprechend sortieren
286 $form->{sort} = $form->{datesort};
290 report_generator_set_default_sort("$form->{datesort}", 1);
291 # report_generator_set_default_sort('transdate', 1);
293 GL->all_transactions(\%myconfig, \%$form);
295 my %acctype = ('A' => $locale->text('Asset'),
296 'C' => $locale->text('Contra'),
297 'L' => $locale->text('Liability'),
298 'Q' => $locale->text('Equity'),
299 'I' => $locale->text('Revenue'),
300 'E' => $locale->text('Expense'),);
302 $form->{title} = $locale->text('Journal');
303 if ($form->{category} ne 'X') {
304 $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
307 $form->{landscape} = 1;
309 my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
312 gldate transdate id reference description
313 notes source debit debit_accno
314 credit credit_accno debit_tax debit_tax_accno
315 credit_tax credit_tax_accno projectnumbers balance employee
318 # add employee here, so that variable is still known and passed in url when choosing a different sort order in resulting table
319 my @hidden_variables = qw(accno source reference department description notes project_id datefrom dateto employee_id datesort category l_subtotal);
320 push @hidden_variables, map { "l_${_}" } @columns;
322 my $employee = $form->{employee_id} ? SL::DB::Employee->new(id => $form->{employee_id})->load->name : '';
324 my (@options, @date_options);
325 push @options, $locale->text('Account') . " : $form->{accno} $form->{account_description}" if ($form->{accno});
326 push @options, $locale->text('Source') . " : $form->{source}" if ($form->{source});
327 push @options, $locale->text('Reference') . " : $form->{reference}" if ($form->{reference});
328 push @options, $locale->text('Description') . " : $form->{description}" if ($form->{description});
329 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
330 push @options, $locale->text('Employee') . " : $employee" if $employee;
331 my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Invoice Date') : $locale->text('Booking Date');
332 push @date_options, "$datesorttext" if ($form->{datesort} and ($form->{datefrom} or $form->{dateto}));
333 push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1) if ($form->{datefrom});
334 push @date_options, $locale->text('Bis'), $locale->date(\%myconfig, $form->{dateto}, 1) if ($form->{dateto});
335 push @options, join(' ', @date_options) if (scalar @date_options);
337 if ($form->{department}) {
338 my ($department) = split /--/, $form->{department};
339 push @options, $locale->text('Department') . " : $department";
343 my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
345 $form->{l_credit_accno} = 'Y';
346 $form->{l_debit_accno} = 'Y';
347 $form->{l_credit_tax} = 'Y';
348 $form->{l_debit_tax} = 'Y';
349 # $form->{l_gldate} = 'Y'; # Spalte mit gldate immer anzeigen
350 $form->{l_credit_tax_accno} = 'Y';
351 $form->{l_datesort} = 'Y';
352 $form->{l_debit_tax_accno} = 'Y';
353 $form->{l_balance} = $form->{accno} ? 'Y' : '';
356 'id' => { 'text' => $locale->text('ID'), },
357 'transdate' => { 'text' => $locale->text('Invoice Date'), },
358 'gldate' => { 'text' => $locale->text('Booking Date'), },
359 'reference' => { 'text' => $locale->text('Reference'), },
360 'source' => { 'text' => $locale->text('Source'), },
361 'description' => { 'text' => $locale->text('Description'), },
362 'notes' => { 'text' => $locale->text('Notes'), },
363 'debit' => { 'text' => $locale->text('Debit'), },
364 'debit_accno' => { 'text' => $locale->text('Debit Account'), },
365 'credit' => { 'text' => $locale->text('Credit'), },
366 'credit_accno' => { 'text' => $locale->text('Credit Account'), },
367 'debit_tax' => { 'text' => $locale->text('Debit Tax'), },
368 'debit_tax_accno' => { 'text' => $locale->text('Debit Tax Account'), },
369 'credit_tax' => { 'text' => $locale->text('Credit Tax'), },
370 'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
371 'balance' => { 'text' => $locale->text('Balance'), },
372 'projectnumbers' => { 'text' => $locale->text('Project Numbers'), },
373 'employee' => { 'text' => $locale->text('Employee'), },
376 foreach my $name (qw(id transdate gldate reference description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
377 my $sortname = $name =~ m/accno/ ? 'accno' : $name;
378 my $sortdir = $sortname eq $form->{sort} ? 1 - $form->{sortdir} : $form->{sortdir};
379 $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
382 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
383 map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
385 my %column_alignment;
386 map { $column_alignment{$_} = 'right' } qw(balance id debit credit debit_tax credit_tax balance);
387 map { $column_alignment{$_} = 'center' } qw(transdate gldate reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
388 map { $column_alignment{$_} = 'left' } qw(description source notes);
389 map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
391 my $report = SL::ReportGenerator->new(\%myconfig, $form);
393 $report->set_columns(%column_defs);
394 $report->set_column_order(@columns);
396 $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir));
398 $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
400 $report->set_options('top_info_text' => join("\n", @options),
401 'output_format' => 'HTML',
402 'title' => $form->{title},
403 'attachment_basename' => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
405 $report->set_options_from_form();
406 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
408 # add sort to callback
409 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
412 my @totals_columns = qw(debit credit debit_tax credit_tax);
413 my %subtotals = map { $_ => 0 } @totals_columns;
414 my %totals = map { $_ => 0 } @totals_columns;
417 foreach my $ref (@{ $form->{GL} }) {
421 foreach my $key (qw(debit credit debit_tax credit_tax)) {
423 foreach my $idx (sort keys(%{ $ref->{$key} })) {
424 my $value = $ref->{$key}->{$idx};
425 $subtotals{$key} += $value;
426 $totals{$key} += $value;
427 if ($key =~ /debit.*/) {
432 $form->{balance} = $form->{balance} + $value * $ml;
433 push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
437 foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
438 my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
439 $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
443 map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
446 if ($form->{balance} < 0) {
449 } elsif ($form->{balance} > 0) {
453 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
456 $row->{balance}->{data} = $data;
457 $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
459 map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes gldate employee);
461 map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
463 foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
464 $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
467 map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
469 $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
471 my $row_set = [ $row ];
473 if ( ($form->{l_subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
474 && (($idx == (scalar @{ $form->{GL} } - 1))
475 || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
476 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
479 $report->add_data($row_set);
484 # = 0 for balanced ledger
485 my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
487 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
490 if ($form->{balance} < 0) {
493 } elsif ($form->{balance} > 0) {
497 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
500 $row->{balance}->{data} = $data;
502 if ( !$form->{report_generator_csv_options_for_import} ) {
503 $report->add_separator();
504 $report->add_data($row);
507 my $raw_bottom_info_text;
509 if (!$form->{accno} && (abs($balanced_ledger) > 0.001)) {
510 $raw_bottom_info_text .=
511 '<p><span class="unbalanced_ledger">'
512 . $locale->text('Unbalanced Ledger')
514 . $form->format_amount(\%myconfig, $balanced_ledger, 3)
518 $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
520 $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
522 $report->generate_with_headers();
524 $main::lxdebug->leave_sub();
528 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
529 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
534 $main::lxdebug->enter_sub();
536 $main::auth->assert('gl_transactions');
538 my $form = $main::form;
539 my %myconfig = %main::myconfig;
541 $form->{oldtransdate} = $form->{transdate};
549 my ($debitcredit, $amount);
551 my $dbh = SL::DB->client->dbh;
552 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
553 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
556 qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
558 for my $i (1 .. $form->{rowcount}) {
560 unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
561 for (qw(debit credit tax)) {
563 $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
567 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
574 if (($debitcount >= 2) && ($creditcount == 2)) {
575 $form->{"credit_$i"} = 0;
576 $form->{"tax_$i"} = 0;
578 $form->{creditlock} = 1;
580 if (($creditcount >= 2) && ($debitcount == 2)) {
581 $form->{"debit_$i"} = 0;
582 $form->{"tax_$i"} = 0;
584 $form->{debitlock} = 1;
586 if (($creditcount == 1) && ($debitcount == 2)) {
587 $form->{creditlock} = 1;
589 if (($creditcount == 2) && ($debitcount == 1)) {
590 $form->{debitlock} = 1;
592 if ($debitcredit && $credittax) {
593 $form->{"taxchart_$i"} = "$notax_id--0.00";
595 if (!$debitcredit && $debittax) {
596 $form->{"taxchart_$i"} = "$notax_id--0.00";
599 ($form->{"debit_$i"} == 0)
600 ? $form->{"credit_$i"}
601 : $form->{"debit_$i"};
603 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
604 $form->{"taxchart_$i"} = "$notax_id--0.00";
605 $form->{"tax_$i"} = 0;
607 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
608 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
609 if (!$iswithouttax) {
616 my ($tmpnetamount,$tmpdiff);
617 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
619 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
624 for my $i (1 .. $count) {
626 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
629 for my $i ($count + 1 .. $form->{rowcount}) {
630 for (@flds) { delete $form->{"${_}_$i"} }
633 $form->{rowcount} = $count + 1;
636 $main::lxdebug->leave_sub();
642 $main::lxdebug->enter_sub();
644 $main::auth->assert('gl_transactions');
646 my $form = $main::form;
647 my %myconfig = %main::myconfig;
651 # for $i (1 .. $form->{rowcount}) {
652 # $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
653 # $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
657 &display_rows($init);
659 $main::lxdebug->leave_sub();
665 $main::lxdebug->enter_sub();
667 $main::auth->assert('gl_transactions');
669 my $form = $main::form;
670 my %myconfig = %main::myconfig;
671 my $cgi = $::request->{cgi};
673 $form->{debit_1} = 0 if !$form->{"debit_1"};
674 $form->{totaldebit} = 0;
675 $form->{totalcredit} = 0;
677 my %project_labels = ();
678 my @project_values = ("");
679 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
680 push(@project_values, $item->{"id"});
681 $project_labels{$item->{"id"}} = $item->{"projectnumber"};
684 my %chart_labels = ();
685 my @chart_values = ();
688 foreach my $item (@{ $form->{ALL_CHARTS} }) {
689 if ($item->{charttype} eq 'H'){ #falls ÃŒberschrift
690 next; #ÃŒberspringen (Bug 1150)
692 my $key = $item->{accno} . "--" . $item->{tax_id};
693 $taxchart_init = $item->{tax_id} unless (@chart_values);
694 push(@chart_values, $key);
695 $chart_labels{$key} = $item->{accno} . "--" . $item->{description};
696 $charts{$item->{accno}} = $item;
699 my ($source, $memo, $source_hidden, $memo_hidden);
700 for my $i (1 .. $form->{rowcount}) {
701 if ($form->{show_details}) {
703 <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
705 <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
708 <input type="hidden" name="source_$i" value="$form->{"source_$i"}" size="16">|;
710 <input type="hidden" name="memo_$i" value="$form->{"memo_$i"}" size="16">|;
713 my $selected_accno_full;
714 my ($accno_row) = split(/--/, $form->{"accno_$i"});
715 my $item = $charts{$accno_row};
716 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
718 my $selected_taxchart = $form->{"taxchart_$i"};
719 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
720 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_accno_$i"});
722 my %taxchart_labels = ();
723 my @taxchart_values = ();
726 $filter_accno = $::form->{ALL_CHARTS}[0]->{accno};
727 $filter_accno = $selected_accno if (!$init and $i < $form->{rowcount});
728 foreach my $item ( GL->get_tax_dropdown($filter_accno) ) {
729 my $key = $item->{id} . "--" . $item->{rate};
730 $taxchart_init = $key if ($taxchart_init == $item->{id});
731 push(@taxchart_values, $key);
732 $taxchart_labels{$key} = $item->{taxdescription} . " " . $item->{rate} * 100 . ' %';
733 $taxcharts{$item->{id}} = $item;
736 if ($previous_accno &&
737 ($previous_accno eq $selected_accno) &&
738 ($previous_tax_id ne $selected_tax_id)) {
739 my $item = $taxcharts{$selected_tax_id};
740 $selected_taxchart = "$item->{id}--$item->{rate}";
743 $selected_accno = '' if ($init);
744 $selected_taxchart ||= $taxchart_init;
746 my $accno = qq|<td>| .
747 NTI($cgi->popup_menu('-name' => "accno_$i",
749 '-onChange' => "updateTaxes($i);",
750 '-style' => 'width:200px',
751 '-values' => \@chart_values,
752 '-labels' => \%chart_labels,
753 '-default' => $selected_accno_full))
754 . $cgi->hidden('-name' => "previous_accno_$i",
755 '-default' => $selected_accno_full)
757 my $tax_ddbox = qq|<td>| .
758 NTI($cgi->popup_menu('-name' => "taxchart_$i",
759 '-id' => "taxchart_$i",
760 '-style' => 'width:200px',
761 '-values' => \@taxchart_values,
762 '-labels' => \%taxchart_labels,
763 '-default' => $selected_taxchart))
766 my ($fx_transaction, $checked);
768 if ($form->{transfer}) {
769 $fx_transaction = qq|
770 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
775 if ($form->{"debit_$i"} != 0) {
776 $form->{totaldebit} += $form->{"debit_$i"};
777 if (!$form->{taxincluded}) {
778 $form->{totaldebit} += $form->{"tax_$i"};
781 $form->{totalcredit} += $form->{"credit_$i"};
782 if (!$form->{taxincluded}) {
783 $form->{totalcredit} += $form->{"tax_$i"};
787 for (qw(debit credit tax)) {
790 ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
794 if ($i < $form->{rowcount}) {
795 if ($form->{transfer}) {
796 $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
797 my $x = ($checked) ? "x" : "";
798 $fx_transaction = qq|
799 <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
802 $form->hide_form("accno_$i");
805 if ($form->{transfer}) {
806 $fx_transaction = qq|
807 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
812 my $debitreadonly = "";
813 my $creditreadonly = "";
814 if ($i == $form->{rowcount}) {
815 if ($form->{debitlock}) {
816 $debitreadonly = "readonly";
817 } elsif ($form->{creditlock}) {
818 $creditreadonly = "readonly";
823 NTI($cgi->popup_menu('-name' => "project_id_$i",
824 '-values' => \@project_values,
825 '-labels' => \%project_labels,
826 '-default' => $form->{"project_id_$i"} ));
827 my $projectnumber_hidden = qq|
828 <input type="hidden" name="project_id_$i" value="$form->{"project_id_$i"}">|;
830 my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : '';
832 print qq|<tr valign=top>
834 <td id="chart_balance_$i" align="right"> </td>
836 <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
837 <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
838 <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
841 if ($form->{show_details}) {
845 <td>$projectnumber</td>
851 $projectnumber_hidden
859 $form->hide_form(qw(rowcount selectaccno));
861 $main::lxdebug->leave_sub();
866 return ($::instance_conf->get_gl_changeable == 2) ? ($::form->current_date(\%::myconfig) eq $::form->{gldate}) : ($::instance_conf->get_gl_changeable == 1);
870 $::lxdebug->enter_sub;
871 $::auth->assert('gl_transactions');
875 my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount};
877 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS",
879 "old_id" => \@old_project_ids },
880 "charts" => { "key" => "ALL_CHARTS",
881 "transdate" => $::form->{transdate} });
883 GL->get_chart_balances('charts' => $::form->{ALL_CHARTS});
885 my $title = $::form->{title};
886 $::form->{title} = $::locale->text("$title General Ledger Transaction");
887 # $locale->text('Add General Ledger Transaction')
888 # $locale->text('Edit General Ledger Transaction')
890 map { $::form->{$_} =~ s/\"/"/g }
893 $::form->{selectdepartment} =~ s/ selected//;
894 $::form->{selectdepartment} =~
895 s/option>\Q$::form->{department}\E/option selected>$::form->{department}/;
898 $::request->{layout}->focus("#reference");
899 $::form->{taxincluded} = "1";
901 $::request->{layout}->focus("#accno_$::form->{rowcount}");
904 $::form->{previous_id} ||= "--";
905 $::form->{previous_gldate} ||= "--";
908 print $::form->parse_html_template('gl/form_header', {
909 hide_title => $title,
910 readonly => $::form->{id} && ($::form->{locked} || !_get_radieren()),
913 $::lxdebug->leave_sub;
918 $::lxdebug->enter_sub;
919 $::auth->assert('gl_transactions');
921 my ($follow_ups, $follow_ups_due);
924 $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
925 $follow_ups_due = sum map { $_->{due} * 1 } @{ $follow_ups || [] };
928 print $::form->parse_html_template('gl/form_footer', {
929 radieren => _get_radieren(),
930 follow_ups => $follow_ups,
931 follow_ups_due => $follow_ups_due,
934 $::lxdebug->leave_sub;
938 $main::lxdebug->enter_sub();
940 my $form = $main::form;
941 my $locale = $main::locale;
946 <form method=post action=gl.pl>
949 map { $form->{$_} =~ s/\"/"/g } qw(reference description);
951 delete $form->{header};
953 foreach my $key (keys %$form) {
954 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
955 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
959 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
962 . $locale->text('Are you sure you want to delete Transaction')
963 . qq| $form->{reference}</h4>
965 <input name=action class=submit type=submit value="|
966 . $locale->text('Yes') . qq|">
969 $main::lxdebug->leave_sub();
974 $main::lxdebug->enter_sub();
976 my $form = $main::form;
977 my %myconfig = %main::myconfig;
978 my $locale = $main::locale;
980 if (GL->delete_transaction(\%myconfig, \%$form)){
982 if(!exists $form->{addition} && $form->{id} ne "") {
983 $form->{snumbers} = qq|gltransaction_| . $form->{id};
984 $form->{addition} = "DELETED";
985 $form->{what_done} = "gl_transaction";
988 # /saving the history
989 $form->redirect($locale->text('Transaction deleted!'))
991 $form->error($locale->text('Cannot delete transaction!'));
992 $main::lxdebug->leave_sub();
996 sub post_transaction {
997 $main::lxdebug->enter_sub();
999 my $form = $main::form;
1000 my %myconfig = %main::myconfig;
1001 my $locale = $main::locale;
1003 # check if there is something in reference and date
1004 $form->isblank("reference", $locale->text('Reference missing!'));
1005 $form->isblank("transdate", $locale->text('Transaction Date missing!'));
1006 $form->isblank("description", $locale->text('Description missing!'));
1008 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1009 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1016 my $creditcount = 0;
1018 my %split_safety = ();
1020 my $dbh = SL::DB->client->dbh;
1021 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
1022 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
1024 my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
1026 for my $i (1 .. $form->{rowcount}) {
1027 next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
1029 for (qw(debit credit tax)) {
1030 $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
1034 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
1036 $split_safety{ $form->{"debit_$i"} <=> 0 }++;
1037 $split_safety{ - $form->{"credit_$i"} <=> 0 }++;
1045 if (($debitcount >= 2) && ($creditcount == 2)) {
1046 $form->{"credit_$i"} = 0;
1047 $form->{"tax_$i"} = 0;
1049 $form->{creditlock} = 1;
1051 if (($creditcount >= 2) && ($debitcount == 2)) {
1052 $form->{"debit_$i"} = 0;
1053 $form->{"tax_$i"} = 0;
1055 $form->{debitlock} = 1;
1057 if (($creditcount == 1) && ($debitcount == 2)) {
1058 $form->{creditlock} = 1;
1060 if (($creditcount == 2) && ($debitcount == 1)) {
1061 $form->{debitlock} = 1;
1063 if ($debitcredit && $credittax) {
1064 $form->{"taxchart_$i"} = "$notax_id--0.00";
1066 if (!$debitcredit && $debittax) {
1067 $form->{"taxchart_$i"} = "$notax_id--0.00";
1069 my $amount = ($form->{"debit_$i"} == 0)
1070 ? $form->{"credit_$i"}
1071 : $form->{"debit_$i"};
1073 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1074 $form->{"taxchart_$i"} = "$notax_id--0.00";
1075 $form->{"tax_$i"} = 0;
1077 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1078 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
1079 if (!$iswithouttax) {
1086 my ($tmpnetamount,$tmpdiff);
1087 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
1089 $form->{"debit_$i"} = $tmpnetamount;
1091 $form->{"credit_$i"} = $tmpnetamount;
1095 $form->{"tax_$i"} = 0;
1098 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1102 if ($split_safety{-1} > 1 && $split_safety{1} > 1) {
1103 $::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. " .
1104 "Due to known problems involving accounting software kivitendo does not allow these."));
1107 for my $i (1 .. $count) {
1109 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1112 for my $i ($count + 1 .. $form->{rowcount}) {
1113 for (@flds) { delete $form->{"${_}_$i"} }
1116 my ($debit, $credit, $taxtotal);
1117 for my $i (1 .. $form->{rowcount}) {
1118 my $dr = $form->{"debit_$i"};
1119 my $cr = $form->{"credit_$i"};
1120 my $tax = $form->{"tax_$i"};
1122 $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
1124 $debit += $dr + $tax if $dr;
1125 $credit += $cr + $tax if $cr;
1126 $taxtotal += $tax if $form->{taxincluded}
1129 $form->{taxincluded} = 0 if !$taxtotal;
1131 # this is just for the wise guys
1133 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
1134 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
1135 $form->error($locale->text('Cannot post transaction for a closed period!'))
1136 if ($form->date_closed($form->{"transdate"}, \%myconfig));
1137 if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1138 $form->error($locale->text('Out of balance transaction!'));
1141 if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1142 $form->error($locale->text('Empty transaction!'));
1145 if ((my $errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1148 $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1149 $err[2] = $locale->text('Debit and credit out of balance!');
1150 $err[3] = $locale->text('Cannot post a transaction without a value!');
1152 $form->error($err[$errno]);
1154 undef($form->{callback});
1155 # saving the history
1156 if(!exists $form->{addition} && $form->{id} ne "") {
1157 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1158 $form->{addition} = "POSTED";
1159 $form->{what_done} = "gl transaction";
1160 $form->save_history;
1162 # /saving the history
1164 $main::lxdebug->leave_sub();
1168 $main::lxdebug->enter_sub();
1170 $main::auth->assert('gl_transactions');
1172 my $form = $main::form;
1173 my $locale = $main::locale;
1175 if ($::myconfig{mandatory_departments} && !$form->{department}) {
1176 $form->{saved_message} = $::locale->text('You have to specify a department.');
1181 $form->{title} = $locale->text("$form->{title} General Ledger Transaction");
1182 $form->{storno} = 0;
1185 if ($::instance_conf->get_webdav) {
1186 SL::Webdav->new(type => 'general_ledger',
1187 number => $form->{reference},
1191 $form->{callback} = build_std_url("action=add", "show_details");
1192 $form->redirect($form->{callback});
1194 $main::lxdebug->leave_sub();
1198 $main::lxdebug->enter_sub();
1200 $main::auth->assert('gl_transactions');
1202 my $form = $main::form;
1206 $main::lxdebug->leave_sub();
1211 $main::lxdebug->enter_sub();
1213 $main::auth->assert('gl_transactions');
1215 my $form = $main::form;
1216 my %myconfig = %main::myconfig;
1217 my $locale = $main::locale;
1219 # don't cancel cancelled transactions
1220 if (IS->has_storno(\%myconfig, $form, 'gl')) {
1221 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1222 $form->error($locale->text("Transaction has already been cancelled!"));
1225 GL->storno($form, \%myconfig, $form->{id});
1227 # saving the history
1228 if(!exists $form->{addition} && $form->{id} ne "") {
1229 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1230 $form->{addition} = "STORNO";
1231 $form->{what_done} = "gl_transaction";
1232 $form->save_history;
1234 # /saving the history
1236 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1238 $main::lxdebug->leave_sub();
1242 call_sub($main::form->{nextsub});
1245 sub get_tax_dropdown {
1246 $main::lxdebug->enter_sub();
1248 my $form = $main::form;
1249 my @tax_accounts = GL->get_tax_dropdown($form->{accno});
1251 foreach my $item (@tax_accounts) {
1252 $item->{taxdescription} = $::locale->{iconv_utf8}->convert($item->{taxdescription});
1253 $item->{taxdescription} .= ' ' . $form->round_amount($item->{rate} * 100);
1256 $form->{TAX_ACCOUNTS} = [ @tax_accounts ];
1258 print $form->ajax_response_header, $form->parse_html_template("gl/update_tax_accounts");
1260 $main::lxdebug->leave_sub();