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(first sum);
41 use SL::DB::RecordTemplate;
45 use SL::Helper::Flash qw(flash);
47 use SL::ReportGenerator;
48 use SL::DBUtils qw(selectrow_query selectall_hashref_query);
50 use SL::Locale::String qw(t8);
51 use SL::Helper::GlAttachments qw(count_gl_attachments);
52 use SL::Presenter::Tag;
53 use SL::Presenter::Chart;
54 require "bin/mozilla/common.pl";
55 require "bin/mozilla/reportgenerator.pl";
57 # this is for our long dates
58 # $locale->text('January')
59 # $locale->text('February')
60 # $locale->text('March')
61 # $locale->text('April')
62 # $locale->text('May ')
63 # $locale->text('June')
64 # $locale->text('July')
65 # $locale->text('August')
66 # $locale->text('September')
67 # $locale->text('October')
68 # $locale->text('November')
69 # $locale->text('December')
71 # this is for our short month
72 # $locale->text('Jan')
73 # $locale->text('Feb')
74 # $locale->text('Mar')
75 # $locale->text('Apr')
76 # $locale->text('May')
77 # $locale->text('Jun')
78 # $locale->text('Jul')
79 # $locale->text('Aug')
80 # $locale->text('Sep')
81 # $locale->text('Oct')
82 # $locale->text('Nov')
83 # $locale->text('Dec')
85 sub load_record_template {
86 $::auth->assert('gl_transactions');
88 # Load existing template and verify that its one for this module.
89 my $template = SL::DB::RecordTemplate
90 ->new(id => $::form->{id})
92 with_object => [ qw(customer payment currency record_items record_items.chart) ],
95 die "invalid template type" unless $template->template_type eq 'gl_transaction';
97 $template->substitute_variables;
98 my $payment_suggestion = $::form->{form_defaults}->{amount_1};
100 # Clean the current $::form before rebuilding it from the template.
101 my $form_defaults = delete $::form->{form_defaults};
102 delete @{ $::form }{ grep { !m{^(?:script|login)$}i } keys %{ $::form } };
105 GL->transaction(\%::myconfig, $dummy_form);
107 # Fill $::form from the template.
108 my $today = DateTime->today_local;
109 $::form->{title} = "Add";
110 $::form->{transdate} = $today->to_kivitendo;
111 $::form->{duedate} = $today->to_kivitendo;
112 $::form->{rowcount} = @{ $template->items };
113 $::form->{paidaccounts} = 1;
114 $::form->{$_} = $template->$_ for qw(department_id taxincluded ob_transaction cb_transaction reference description show_details);
115 $::form->{$_} = $dummy_form->{$_} for qw(closedto revtrans previous_id previous_gldate);
118 foreach my $item (@{ $template->items }) {
121 my $active_taxkey = $item->chart->get_active_taxkey;
122 my $taxes = SL::DB::Manager::Tax->get_all(
123 where => [ chart_categories => { like => '%' . $item->chart->category . '%' }],
124 sort_by => 'taxkey, rate',
127 my $tax = first { $item->tax_id == $_->id } @{ $taxes };
128 $tax //= first { $active_taxkey->tax_id == $_->id } @{ $taxes };
129 $tax //= $taxes->[0];
136 $::form->{"accno_id_${row}"} = $item->chart_id;
137 $::form->{"previous_accno_id_${row}"} = $item->chart_id;
138 $::form->{"debit_${row}"} = $::form->format_amount(\%::myconfig, ($payment_suggestion ? $payment_suggestion : $item->amount1), 2) if $item->amount1 * 1;
139 $::form->{"credit_${row}"} = $::form->format_amount(\%::myconfig, ($payment_suggestion ? $payment_suggestion : $item->amount2), 2) if $item->amount2 * 1;
140 $::form->{"taxchart_${row}"} = $item->tax_id . '--' . $tax->rate;
141 $::form->{"${_}_${row}"} = $item->$_ for qw(source memo project_id);
144 $::form->{$_} = $form_defaults->{$_} for keys %{ $form_defaults // {} };
146 flash('info', $::locale->text("The record template '#1' has been loaded.", $template->template_name));
149 keep_rows_without_amount => 1,
150 dont_add_new_row => 1,
154 sub save_record_template {
155 $::auth->assert('gl_transactions');
157 my $template = $::form->{record_template_id} ? SL::DB::RecordTemplate->new(id => $::form->{record_template_id})->load : SL::DB::RecordTemplate->new;
158 my $js = SL::ClientJS->new(controller => SL::Controller::Base->new);
159 my $new_name = $template->template_name_to_use($::form->{record_template_new_template_name});
161 $js->dialog->close('#record_template_dialog');
164 $_->{chart_id} && (($_->{tax_id} // '') ne '')
166 +{ chart_id => $::form->{"accno_id_${_}"},
167 amount1 => $::form->parse_amount(\%::myconfig, $::form->{"debit_${_}"}),
168 amount2 => $::form->parse_amount(\%::myconfig, $::form->{"credit_${_}"}),
169 tax_id => (split m{--}, $::form->{"taxchart_${_}"})[0],
170 project_id => $::form->{"project_id_${_}"} || undef,
171 source => $::form->{"source_${_}"},
172 memo => $::form->{"memo_${_}"},
174 } (1..($::form->{rowcount} || 1));
176 $template->assign_attributes(
177 template_type => 'gl_transaction',
178 template_name => $new_name,
180 currency_id => $::instance_conf->get_currency_id,
181 department_id => $::form->{department_id} || undef,
182 project_id => $::form->{globalproject_id} || undef,
183 taxincluded => $::form->{taxincluded} ? 1 : 0,
184 ob_transaction => $::form->{ob_transaction} ? 1 : 0,
185 cb_transaction => $::form->{cb_transaction} ? 1 : 0,
186 reference => $::form->{reference},
187 description => $::form->{description},
188 show_details => $::form->{show_details},
198 ->flash('error', $::locale->text("Saving the record template '#1' failed.", $new_name))
203 ->flash('info', $::locale->text("The record template '#1' has been saved.", $new_name))
208 $main::lxdebug->enter_sub();
210 $main::auth->assert('gl_transactions');
212 my $form = $main::form;
213 my %myconfig = %main::myconfig;
215 $form->{title} = "Add";
217 $form->{callback} = "gl.pl?action=add" unless $form->{callback};
219 # we use this only to set a default date
220 # yep. aber er holt hier auch schon ALL_CHARTS. Aufwand / Nutzen? jb
221 GL->transaction(\%myconfig, \%$form);
223 $form->{rowcount} = 2;
229 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
231 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
234 $main::lxdebug->leave_sub();
238 sub prepare_transaction {
239 $main::lxdebug->enter_sub();
241 $main::auth->assert('gl_transactions');
243 my $form = $main::form;
244 my %myconfig = %main::myconfig;
246 GL->transaction(\%myconfig, \%$form);
248 $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
250 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
255 foreach my $ref (@{ $form->{GL} }) {
257 if ($tax && ($ref->{accno} eq $taxaccno)) {
258 $form->{"tax_$j"} = abs($ref->{amount});
259 $form->{"taxchart_$j"} = $ref->{id} . "--" . $ref->{taxrate};
260 if ($form->{taxincluded}) {
261 if ($ref->{amount} < 0) {
262 $form->{"debit_$j"} += $form->{"tax_$j"};
264 $form->{"credit_$j"} += $form->{"tax_$j"};
267 $form->{"project_id_$j"} = $ref->{project_id};
270 $form->{"accno_id_$i"} = $ref->{chart_id};
271 for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
272 if ($ref->{amount} < 0) {
273 $form->{totaldebit} -= $ref->{amount};
274 $form->{"debit_$i"} = $ref->{amount} * -1;
276 $form->{totalcredit} += $ref->{amount};
277 $form->{"credit_$i"} = $ref->{amount};
279 $form->{"taxchart_$i"} = $ref->{id}."--0.00000";
280 $form->{"project_id_$i"} = $ref->{project_id};
283 if ($ref->{taxaccno} && !$tax) {
284 $taxaccno = $ref->{taxaccno};
292 $form->{rowcount} = $i;
294 ($form->datetonum($form->{transdate}, \%myconfig) <=
295 $form->datetonum($form->{closedto}, \%myconfig));
297 $main::lxdebug->leave_sub();
301 $main::lxdebug->enter_sub();
303 $main::auth->assert('gl_transactions');
305 my $form = $main::form;
306 my %myconfig = %main::myconfig;
308 prepare_transaction();
310 $form->{title} = "Edit";
312 $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
314 if ($form->{id} && $::instance_conf->get_webdav) {
315 my $webdav = SL::Webdav->new(
316 type => 'general_ledger',
317 number => $form->{id},
319 my @all_objects = $webdav->get_all_objects;
320 @{ $form->{WEBDAV} } = map { { name => $_->filename,
322 link => File::Spec->catfile($_->full_filedescriptor),
329 $main::lxdebug->leave_sub();
334 $::lxdebug->enter_sub;
335 $::auth->assert('general_ledger | gl_transactions');
338 projects => { key => "ALL_PROJECTS", all => 1 },
340 $::form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
341 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
343 setup_gl_search_action_bar();
346 print $::form->parse_html_template('gl/search', {
347 employee_label => sub { "$_[0]{id}--$_[0]{name}" },
350 $::lxdebug->leave_sub;
353 sub create_subtotal_row {
354 $main::lxdebug->enter_sub();
356 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
358 my $form = $main::form;
359 my %myconfig = %main::myconfig;
361 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
363 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
365 map { $totals->{$_} = 0 } @{ $subtotal_columns };
367 $main::lxdebug->leave_sub();
372 sub generate_report {
373 $main::lxdebug->enter_sub();
375 $main::auth->assert('general_ledger | gl_transactions');
377 my $form = $main::form;
378 my %myconfig = %main::myconfig;
379 my $locale = $main::locale;
381 # 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
383 # <form method=post action=gl.pl>
384 # <input type=hidden name=sort value=datesort> # form->{sort} setzen
385 # <input type=hidden name=nextsub value=generate_report>
387 # anhand von neuer Variable datesort wird jetzt $form->{sort} auf transdate oder gldate gesetzt
388 # damit ist die Hidden Variable "sort" wahrscheinlich sogar überflüssig
390 # ändert man die Sortierreihenfolge per Klick auf eine der Überschriften wird die Variable "sort" per GET übergeben, z.B. id,transdate, gldate, ...
391 # 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
393 if ( $form->{sort} eq 'datesort' ) { # sollte bei einem Post (Aufruf aus Suchmaske) immer wahr sein
394 # je nachdem ob in Suchmaske "transdate" oder "gldate" ausgesucht wurde erstes Suchergebnis entsprechend sortieren
395 $form->{sort} = $form->{datesort};
399 report_generator_set_default_sort("$form->{datesort}", 1);
400 # report_generator_set_default_sort('transdate', 1);
402 GL->all_transactions(\%myconfig, \%$form);
404 my %acctype = ('A' => $locale->text('Asset'),
405 'C' => $locale->text('Contra'),
406 'L' => $locale->text('Liability'),
407 'Q' => $locale->text('Equity'),
408 'I' => $locale->text('Revenue'),
409 'E' => $locale->text('Expense'),);
411 $form->{title} = $locale->text('Journal');
412 if ($form->{category} ne 'X') {
413 $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
416 $form->{landscape} = 1;
418 my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
421 transdate gldate id reference description
422 notes source doccnt debit debit_accno
423 credit credit_accno debit_tax debit_tax_accno
424 credit_tax credit_tax_accno projectnumbers balance employee
427 # add employee here, so that variable is still known and passed in url when choosing a different sort order in resulting table
428 my @hidden_variables = qw(accno source reference description notes project_id datefrom dateto employee_id datesort category l_subtotal department_id);
429 push @hidden_variables, map { "l_${_}" } @columns;
431 my $employee = $form->{employee_id} ? SL::DB::Employee->new(id => $form->{employee_id})->load->name : '';
433 my (@options, @date_options);
434 push @options, $locale->text('Account') . " : $form->{accno} $form->{account_description}" if ($form->{accno});
435 push @options, $locale->text('Source') . " : $form->{source}" if ($form->{source});
436 push @options, $locale->text('Reference') . " : $form->{reference}" if ($form->{reference});
437 push @options, $locale->text('Description') . " : $form->{description}" if ($form->{description});
438 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
439 push @options, $locale->text('Employee') . " : $employee" if $employee;
440 my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Transdate') : $locale->text('Gldate');
441 push @date_options, "$datesorttext" if ($form->{datesort} and ($form->{datefrom} or $form->{dateto}));
442 push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1) if ($form->{datefrom});
443 push @date_options, $locale->text('Bis'), $locale->date(\%myconfig, $form->{dateto}, 1) if ($form->{dateto});
444 push @options, join(' ', @date_options) if (scalar @date_options);
446 if ($form->{department_id}) {
447 my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
448 push @options, $locale->text('Department') . " : " . $department->description;
451 my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
453 $form->{l_credit_accno} = 'Y';
454 $form->{l_debit_accno} = 'Y';
455 $form->{l_credit_tax} = 'Y';
456 $form->{l_debit_tax} = 'Y';
457 # $form->{l_gldate} = 'Y'; # Spalte mit gldate immer anzeigen
458 $form->{l_credit_tax_accno} = 'Y';
459 $form->{l_datesort} = 'Y';
460 $form->{l_debit_tax_accno} = 'Y';
461 $form->{l_balance} = $form->{accno} ? 'Y' : '';
462 $form->{l_doccnt} = $form->{l_source} ? 'Y' : '';
465 'id' => { 'text' => $locale->text('ID'), },
466 'transdate' => { 'text' => $locale->text('Transdate'), },
467 'gldate' => { 'text' => $locale->text('Gldate'), },
468 'reference' => { 'text' => $locale->text('Reference'), },
469 'source' => { 'text' => $locale->text('Source'), },
470 'doccnt' => { 'text' => $locale->text('Document Count'), },
471 'description' => { 'text' => $locale->text('Description'), },
472 'notes' => { 'text' => $locale->text('Notes'), },
473 'debit' => { 'text' => $locale->text('Debit'), },
474 'debit_accno' => { 'text' => $locale->text('Debit Account'), },
475 'credit' => { 'text' => $locale->text('Credit'), },
476 'credit_accno' => { 'text' => $locale->text('Credit Account'), },
477 'debit_tax' => { 'text' => $locale->text('Debit Tax'), },
478 'debit_tax_accno' => { 'text' => $locale->text('Debit Tax Account'), },
479 'credit_tax' => { 'text' => $locale->text('Credit Tax'), },
480 'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
481 'balance' => { 'text' => $locale->text('Balance'), },
482 'projectnumbers' => { 'text' => $locale->text('Project Numbers'), },
483 'employee' => { 'text' => $locale->text('Employee'), },
486 foreach my $name (qw(id transdate gldate reference description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
487 my $sortname = $name =~ m/accno/ ? 'accno' : $name;
488 my $sortdir = $sortname eq $form->{sort} ? 1 - $form->{sortdir} : $form->{sortdir};
489 $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
492 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
493 map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
495 my %column_alignment;
496 map { $column_alignment{$_} = 'right' } qw(balance id debit credit debit_tax credit_tax balance);
497 map { $column_alignment{$_} = 'center' } qw(transdate gldate reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
498 map { $column_alignment{$_} = 'left' } qw(description source notes);
499 map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
501 my $report = SL::ReportGenerator->new(\%myconfig, $form);
503 $report->set_columns(%column_defs);
504 $report->set_column_order(@columns);
506 $form->{l_attachments} = 'Y';
507 $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir l_attachments));
509 $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
511 $report->set_options('top_info_text' => join("\n", @options),
512 'output_format' => 'HTML',
513 'title' => $form->{title},
514 'attachment_basename' => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
516 $report->set_options_from_form();
517 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
519 # add sort to callback
520 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
523 my @totals_columns = qw(debit credit debit_tax credit_tax);
524 my %subtotals = map { $_ => 0 } @totals_columns;
525 my %totals = map { $_ => 0 } @totals_columns;
528 foreach my $ref (@{ $form->{GL} }) {
532 foreach my $key (qw(debit credit debit_tax credit_tax)) {
534 foreach my $idx (sort keys(%{ $ref->{$key} })) {
535 my $value = $ref->{$key}->{$idx};
536 $subtotals{$key} += $value;
537 $totals{$key} += $value;
538 if ($key =~ /debit.*/) {
543 $form->{balance} = $form->{balance} + $value * $ml;
544 push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
548 foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
549 my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
550 $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
554 map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
556 if ( $form->{l_doccnt} ) {
557 $row->{doccnt}->{data} = SL::Helper::GlAttachments->count_gl_pdf_attachments($ref->{id},$ref->{type});
561 if ($form->{balance} < 0) {
564 } elsif ($form->{balance} > 0) {
568 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
571 $row->{balance}->{data} = $data;
572 $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
574 map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes gldate employee);
576 map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
578 foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
579 $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
582 map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
584 $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
586 my $row_set = [ $row ];
588 if ( ($form->{l_subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
589 && (($idx == (scalar @{ $form->{GL} } - 1))
590 || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
591 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
594 $report->add_data($row_set);
599 # = 0 for balanced ledger
600 my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
602 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
605 if ($form->{balance} < 0) {
608 } elsif ($form->{balance} > 0) {
612 my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
615 $row->{balance}->{data} = $data;
617 if ( !$form->{report_generator_csv_options_for_import} ) {
618 $report->add_separator();
619 $report->add_data($row);
622 my $raw_bottom_info_text;
624 if (!$form->{accno} && (abs($balanced_ledger) > 0.001)) {
625 $raw_bottom_info_text .=
626 '<p><span class="unbalanced_ledger">'
627 . $locale->text('Unbalanced Ledger')
629 . $form->format_amount(\%myconfig, $balanced_ledger, 3)
633 $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
635 $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
637 setup_gl_transactions_action_bar(num_rows => scalar(@{$form->{GL}}));
639 $report->generate_with_headers();
641 $main::lxdebug->leave_sub();
645 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
646 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
653 $main::lxdebug->enter_sub();
655 $main::auth->assert('gl_transactions');
657 my $form = $main::form;
658 my %myconfig = %main::myconfig;
660 $form->{oldtransdate} = $form->{transdate};
668 my ($debitcredit, $amount);
670 my $dbh = SL::DB->client->dbh;
671 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
672 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
675 qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
677 for my $i (1 .. $form->{rowcount}) {
678 $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) for qw(debit credit tax);
680 next if !$form->{"debit_$i"} && !$form->{"credit_$i"} && !$params{keep_rows_without_amount};
683 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
690 if (($debitcount >= 2) && ($creditcount == 2)) {
691 $form->{"credit_$i"} = 0;
692 $form->{"tax_$i"} = 0;
694 $form->{creditlock} = 1;
696 if (($creditcount >= 2) && ($debitcount == 2)) {
697 $form->{"debit_$i"} = 0;
698 $form->{"tax_$i"} = 0;
700 $form->{debitlock} = 1;
702 if (($creditcount == 1) && ($debitcount == 2)) {
703 $form->{creditlock} = 1;
705 if (($creditcount == 2) && ($debitcount == 1)) {
706 $form->{debitlock} = 1;
708 if ($debitcredit && $credittax) {
709 $form->{"taxchart_$i"} = "$notax_id--0.00";
711 if (!$debitcredit && $debittax) {
712 $form->{"taxchart_$i"} = "$notax_id--0.00";
715 ($form->{"debit_$i"} == 0)
716 ? $form->{"credit_$i"}
717 : $form->{"debit_$i"};
719 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
720 $form->{"taxchart_$i"} = "$notax_id--0.00";
721 $form->{"tax_$i"} = 0;
723 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
724 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
725 if (!$iswithouttax) {
732 my ($tmpnetamount,$tmpdiff);
733 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
735 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
739 for my $i (1 .. $count) {
741 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
744 for my $i ($count + 1 .. $form->{rowcount}) {
745 for (@flds) { delete $form->{"${_}_$i"} }
748 $form->{rowcount} = $count + ($params{dont_add_new_row} ? 0 : 1);
751 $main::lxdebug->leave_sub();
757 $main::lxdebug->enter_sub();
759 $main::auth->assert('gl_transactions');
761 my $form = $main::form;
762 my %myconfig = %main::myconfig;
766 # for $i (1 .. $form->{rowcount}) {
767 # $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
768 # $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
772 &display_rows($init);
774 $main::lxdebug->leave_sub();
780 $main::lxdebug->enter_sub();
782 $main::auth->assert('gl_transactions');
784 my $form = $main::form;
785 my %myconfig = %main::myconfig;
786 my $cgi = $::request->{cgi};
788 my %balances = GL->get_chart_balances(map { $_->{id} } @{ $form->{ALL_CHARTS} });
790 $form->{debit_1} = 0 if !$form->{"debit_1"};
791 $form->{totaldebit} = 0;
792 $form->{totalcredit} = 0;
794 my %project_labels = ();
795 my @project_values = ("");
796 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
797 push(@project_values, $item->{"id"});
798 $project_labels{$item->{"id"}} = $item->{"projectnumber"};
801 my %charts_by_id = map { ($_->{id} => $_) } @{ $::form->{ALL_CHARTS} };
802 my $default_chart = $::form->{ALL_CHARTS}[0];
803 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
805 my ($source, $memo, $source_hidden, $memo_hidden);
806 for my $i (1 .. $form->{rowcount}) {
807 if ($form->{show_details}) {
809 <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
811 <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
814 <input type="hidden" name="source_$i" value="$form->{"source_$i"}" size="16">|;
816 <input type="hidden" name="memo_$i" value="$form->{"memo_$i"}" size="16">|;
819 my %taxchart_labels = ();
820 my @taxchart_values = ();
822 my $accno_id = $::form->{"accno_id_$i"};
823 my $chart = $charts_by_id{$accno_id} // $default_chart;
824 $accno_id = $chart->{id};
825 my ($first_taxchart, $default_taxchart, $taxchart_to_use);
827 foreach my $item ( GL->get_active_taxes_for_chart($accno_id, $transdate) ) {
828 my $key = $item->id . "--" . $item->rate;
829 $first_taxchart //= $item;
830 $default_taxchart = $item if $item->{is_default};
831 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
833 push(@taxchart_values, $key);
834 $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
837 $taxchart_to_use //= $default_taxchart // $first_taxchart;
838 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
840 my $accno = qq|<td>| .
841 SL::Presenter::Chart::picker("accno_id_$i", $accno_id, style => "width: 300px") .
842 SL::Presenter::Tag::hidden_tag("previous_accno_id_$i", $accno_id)
844 my $tax_ddbox = qq|<td>| .
845 NTI($cgi->popup_menu('-name' => "taxchart_$i",
846 '-id' => "taxchart_$i",
847 '-style' => 'width:200px',
848 '-values' => \@taxchart_values,
849 '-labels' => \%taxchart_labels,
850 '-default' => $selected_taxchart))
853 my ($fx_transaction, $checked);
855 if ($form->{transfer}) {
856 $fx_transaction = qq|
857 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
862 if ($form->{"debit_$i"} != 0) {
863 $form->{totaldebit} += $form->{"debit_$i"};
864 if (!$form->{taxincluded}) {
865 $form->{totaldebit} += $form->{"tax_$i"};
868 $form->{totalcredit} += $form->{"credit_$i"};
869 if (!$form->{taxincluded}) {
870 $form->{totalcredit} += $form->{"tax_$i"};
874 for (qw(debit credit tax)) {
877 ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
881 if ($i < $form->{rowcount}) {
882 if ($form->{transfer}) {
883 $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
884 my $x = ($checked) ? "x" : "";
885 $fx_transaction = qq|
886 <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
889 $form->hide_form("accno_$i");
892 if ($form->{transfer}) {
893 $fx_transaction = qq|
894 <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
899 my $debitreadonly = "";
900 my $creditreadonly = "";
901 if ($i == $form->{rowcount}) {
902 if ($form->{debitlock}) {
903 $debitreadonly = "readonly";
904 } elsif ($form->{creditlock}) {
905 $creditreadonly = "readonly";
910 NTI($cgi->popup_menu('-name' => "project_id_$i",
911 '-values' => \@project_values,
912 '-labels' => \%project_labels,
913 '-default' => $form->{"project_id_$i"} ));
914 my $projectnumber_hidden = qq|
915 <input type="hidden" name="project_id_$i" value="$form->{"project_id_$i"}">|;
917 my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : '';
918 my $balance = $form->format_amount(\%::myconfig, $balances{$accno_id} // 0, 2, 'DRCR');
920 # if we have a bt_chart_id we disallow changing the amount of the bank account
921 if ($form->{bt_chart_id}) {
922 $debitreadonly = $creditreadonly = "readonly" if ($form->{"accno_id_$i"} eq $form->{bt_chart_id});
923 $copy2credit = '' if $i == 1; # and disallow copy2credit
926 print qq|<tr valign=top>
928 <td id="chart_balance_$i" align="right">${balance}</td>
930 <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
931 <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
932 <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
935 if ($form->{show_details}) {
939 <td>$projectnumber</td>
945 $projectnumber_hidden
953 $form->hide_form(qw(rowcount selectaccno));
955 $main::lxdebug->leave_sub();
960 return ($::instance_conf->get_gl_changeable == 2) ? ($::form->current_date(\%::myconfig) eq $::form->{gldate}) : ($::instance_conf->get_gl_changeable == 1);
963 sub setup_gl_action_bar {
966 my $change_never = $::instance_conf->get_gl_changeable == 0;
967 my $change_on_same_day_only = $::instance_conf->get_gl_changeable == 2 && ($form->current_date(\%::myconfig) ne $form->{gldate});
969 for my $bar ($::request->layout->get('actionbar')) {
973 submit => [ '#form', { action => 'update' } ],
974 id => 'update_button',
975 accesskey => 'enter',
979 submit => [ '#form', { action => 'post' } ],
980 disabled => $form->{locked} ? t8('The billing period has already been locked.')
981 : $form->{storno} ? t8('A canceled general ledger transaction cannot be posted.')
982 : ($form->{id} && $change_never) ? t8('Changing general ledger transaction has been disabled in the configuration.')
983 : ($form->{id} && $change_on_same_day_only) ? t8('General ledger transactions can only be changed on the day they are posted.')
987 action => [ t8('Storno'),
988 submit => [ '#form', { action => 'storno' } ],
989 confirm => t8('Do you really want to cancel this general ledger transaction?'),
990 disabled => !$form->{id} ? t8('This general ledger transaction has not been posted yet.') : undef,
992 action => [ t8('Delete'),
993 submit => [ '#form', { action => 'delete' } ],
994 confirm => t8('Do you really want to delete this object?'),
995 disabled => !$form->{id} ? t8('This invoice has not been posted yet.')
996 : $form->{locked} ? t8('The billing period has already been locked.')
997 : $change_never ? t8('Changing invoices has been disabled in the configuration.')
998 : $change_on_same_day_only ? t8('Invoices can only be changed on the day they are posted.')
1001 ], # end of combobox "Storno"
1004 action => [ t8('more') ],
1007 call => [ 'set_history_window', $form->{id} * 1, 'id' ],
1008 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
1012 call => [ 'follow_up_window' ],
1013 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
1016 t8('Record templates'),
1017 call => [ 'kivi.RecordTemplate.popup', 'gl_transaction' ],
1021 call => [ 'kivi.Draft.popup', 'gl', 'unknown', $form->{draft_id}, $form->{draft_description} ],
1022 disabled => $form->{id} ? t8('This invoice has already been posted.')
1023 : $form->{locked} ? t8('The billing period has already been locked.')
1026 ], # end of combobox "more"
1031 sub setup_gl_search_action_bar {
1034 for my $bar ($::request->layout->get('actionbar')) {
1038 submit => [ '#form', { action => 'continue', nextsub => 'generate_report' } ],
1039 accesskey => 'enter',
1045 sub setup_gl_transactions_action_bar {
1048 for my $bar ($::request->layout->get('actionbar')) {
1051 action => [ $::locale->text('Create new') ],
1053 $::locale->text('GL Transaction'),
1054 submit => [ '#create_new_form', { action => 'gl_transaction' } ],
1057 $::locale->text('AR Transaction'),
1058 submit => [ '#create_new_form', { action => 'ar_transaction' } ],
1061 $::locale->text('AP Transaction'),
1062 submit => [ '#create_new_form', { action => 'ap_transaction' } ],
1065 $::locale->text('Sales Invoice'),
1066 submit => [ '#create_new_form', { action => 'sales_invoice' } ],
1069 $::locale->text('Vendor Invoice'),
1070 submit => [ '#create_new_form', { action => 'vendor_invoice' } ],
1072 ], # end of combobox "Create new"
1078 $::lxdebug->enter_sub;
1079 $::auth->assert('gl_transactions');
1083 $::request->layout->add_javascripts("autocomplete_chart.js", "kivi.File.js", "kivi.GL.js", "kivi.RecordTemplate.js");
1085 my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount};
1087 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1089 "old_id" => \@old_project_ids },
1091 "charts" => { "key" => "ALL_CHARTS",
1092 "transdate" => $::form->{transdate} });
1094 # we cannot book on charttype header
1095 @{ $::form->{ALL_CHARTS} } = grep { $_->{charttype} ne 'H' } @{ $::form->{ALL_CHARTS} };
1096 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
1098 my $title = $::form->{title};
1099 $::form->{title} = $::locale->text("$title General Ledger Transaction");
1100 # $locale->text('Add General Ledger Transaction')
1101 # $locale->text('Edit General Ledger Transaction')
1103 map { $::form->{$_} =~ s/\"/"/g }
1107 $::request->{layout}->focus("#reference");
1108 $::form->{taxincluded} = "1";
1110 $::request->{layout}->focus("#accno_id_$::form->{rowcount}_name");
1113 $::form->{previous_id} ||= "--";
1114 $::form->{previous_gldate} ||= "--";
1116 setup_gl_action_bar();
1119 print $::form->parse_html_template('gl/form_header', {
1120 hide_title => $title,
1121 readonly => $::form->{id} && ($::form->{locked} || !_get_radieren()),
1124 $::lxdebug->leave_sub;
1129 $::lxdebug->enter_sub;
1130 $::auth->assert('gl_transactions');
1132 my ($follow_ups, $follow_ups_due);
1134 if ($::form->{id}) {
1135 $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
1136 $follow_ups_due = sum map { $_->{due} * 1 } @{ $follow_ups || [] };
1139 print $::form->parse_html_template('gl/form_footer', {
1140 radieren => _get_radieren(),
1141 follow_ups => $follow_ups,
1142 follow_ups_due => $follow_ups_due,
1145 $::lxdebug->leave_sub;
1149 $main::lxdebug->enter_sub();
1151 my $form = $main::form;
1152 my %myconfig = %main::myconfig;
1153 my $locale = $main::locale;
1155 if (GL->delete_transaction(\%myconfig, \%$form)){
1156 # saving the history
1157 if(!exists $form->{addition} && $form->{id} ne "") {
1158 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1159 $form->{addition} = "DELETED";
1160 $form->{what_done} = "gl_transaction";
1161 $form->save_history;
1163 # /saving the history
1164 $form->redirect($locale->text('Transaction deleted!'))
1166 $form->error($locale->text('Cannot delete transaction!'));
1167 $main::lxdebug->leave_sub();
1171 sub post_transaction {
1172 $main::lxdebug->enter_sub();
1174 my $form = $main::form;
1175 my %myconfig = %main::myconfig;
1176 my $locale = $main::locale;
1178 # check if there is something in reference and date
1179 $form->isblank("reference", $locale->text('Reference missing!'));
1180 $form->isblank("transdate", $locale->text('Transaction Date missing!'));
1181 $form->isblank("description", $locale->text('Description missing!'));
1183 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1184 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1191 my $creditcount = 0;
1193 my %split_safety = ();
1195 my $dbh = SL::DB->client->dbh;
1196 my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
1197 my $zerotaxes = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
1199 my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
1201 for my $i (1 .. $form->{rowcount}) {
1202 next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
1204 for (qw(debit credit tax)) {
1205 $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
1209 $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
1211 $split_safety{ $form->{"debit_$i"} <=> 0 }++;
1212 $split_safety{ - $form->{"credit_$i"} <=> 0 }++;
1220 if (($debitcount >= 2) && ($creditcount == 2)) {
1221 $form->{"credit_$i"} = 0;
1222 $form->{"tax_$i"} = 0;
1224 $form->{creditlock} = 1;
1226 if (($creditcount >= 2) && ($debitcount == 2)) {
1227 $form->{"debit_$i"} = 0;
1228 $form->{"tax_$i"} = 0;
1230 $form->{debitlock} = 1;
1232 if (($creditcount == 1) && ($debitcount == 2)) {
1233 $form->{creditlock} = 1;
1235 if (($creditcount == 2) && ($debitcount == 1)) {
1236 $form->{debitlock} = 1;
1238 if ($debitcredit && $credittax) {
1239 $form->{"taxchart_$i"} = "$notax_id--0.00";
1241 if (!$debitcredit && $debittax) {
1242 $form->{"taxchart_$i"} = "$notax_id--0.00";
1244 my $amount = ($form->{"debit_$i"} == 0)
1245 ? $form->{"credit_$i"}
1246 : $form->{"debit_$i"};
1248 if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1249 $form->{"taxchart_$i"} = "$notax_id--0.00";
1250 $form->{"tax_$i"} = 0;
1252 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1253 my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
1254 if (!$iswithouttax) {
1261 my ($tmpnetamount,$tmpdiff);
1262 ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
1264 $form->{"debit_$i"} = $tmpnetamount;
1266 $form->{"credit_$i"} = $tmpnetamount;
1270 $form->{"tax_$i"} = 0;
1273 for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1277 if ($split_safety{-1} > 1 && $split_safety{1} > 1) {
1278 $::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. " .
1279 "Due to known problems involving accounting software kivitendo does not allow these."));
1282 for my $i (1 .. $count) {
1284 for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1287 for my $i ($count + 1 .. $form->{rowcount}) {
1288 for (@flds) { delete $form->{"${_}_$i"} }
1291 my ($debit, $credit, $taxtotal);
1292 for my $i (1 .. $form->{rowcount}) {
1293 my $dr = $form->{"debit_$i"};
1294 my $cr = $form->{"credit_$i"};
1295 my $tax = $form->{"tax_$i"};
1297 $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
1299 $debit += $dr + $tax if $dr;
1300 $credit += $cr + $tax if $cr;
1301 $taxtotal += $tax if $form->{taxincluded}
1304 $form->{taxincluded} = 0 if !$taxtotal;
1306 # this is just for the wise guys
1308 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
1309 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
1310 $form->error($locale->text('Cannot post transaction for a closed period!'))
1311 if ($form->date_closed($form->{"transdate"}, \%myconfig));
1312 if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1313 $form->error($locale->text('Out of balance transaction!'));
1316 if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1317 $form->error($locale->text('Empty transaction!'));
1321 # start transaction (post + history + (optional) banktrans)
1322 SL::DB->client->with_transaction(sub {
1324 if ((my $errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1327 $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1328 $err[2] = $locale->text('Debit and credit out of balance!');
1329 $err[3] = $locale->text('Cannot post a transaction without a value!');
1333 # saving the history
1334 if(!exists $form->{addition} && $form->{id} ne "") {
1335 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1336 $form->{addition} = "POSTED";
1337 $form->{what_done} = "gl transaction";
1338 $form->save_history;
1341 # Case BankTransaction: update RecordLink and BankTransaction
1342 if ($form->{callback} =~ /BankTransaction/ && $form->{bt_id}) {
1343 # set invoice_amount - we only rely on bt_id in form, do all other stuff ui independent
1344 # die if we have a unlogic or NYI case and abort the whole transaction
1345 my ($bt, $chart_id, $payment);
1346 require SL::DB::Manager::BankTransaction;
1348 $bt = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
1349 die "No bank transaction found" unless $bt;
1351 $chart_id = SL::DB::Manager::BankAccount->find_by(id => $bt->local_bank_account_id)->chart_id;
1352 die "no chart id:" unless $chart_id;
1354 $payment = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $::form->{id},
1355 chart_link => { like => '%_paid%' },
1356 chart_id => $chart_id ]);
1357 die "guru meditation error: Can only assign amount to one bank account booking" if scalar @{ $payment } > 1;
1359 # credit/debit * -1 matches the sign for bt.amount and bt.invoice_amount
1360 die "Can only assign the full bank amount to a single general ledger booking" unless $bt->amount == $payment->[0]->amount * -1;
1361 $bt->update_attributes(invoice_amount => $bt->invoice_amount + ($payment->[0]->amount * -1));
1363 # create record_link
1365 from_table => 'bank_transactions',
1366 from_id => $::form->{bt_id},
1368 to_id => $::form->{id},
1370 SL::DB::RecordLink->new(@props)->save;
1374 }) or do { die SL::DB->client->error };
1376 if ($form->{callback} =~ /BankTransaction/ && $form->{bt_id}) {
1377 print $form->redirect_header($form->{callback});
1378 $form->redirect($locale->text('GL transaction posted.') . ' ' . $locale->text('ID') . ': ' . $form->{id});
1382 undef($form->{callback});
1383 $main::lxdebug->leave_sub();
1387 $main::lxdebug->enter_sub();
1389 $main::auth->assert('gl_transactions');
1391 my $form = $main::form;
1392 my $locale = $main::locale;
1394 if ($::myconfig{mandatory_departments} && !$form->{department_id}) {
1395 $form->error($locale->text('You have to specify a department.'));
1398 $form->{title} = $locale->text("$form->{title} General Ledger Transaction");
1399 $form->{storno} = 0;
1402 if ($::instance_conf->get_webdav) {
1403 SL::Webdav->new(type => 'general_ledger',
1404 number => $form->{id},
1408 $form->{callback} = build_std_url("action=add", "show_details");
1409 $form->redirect($::locale->text("General ledger transaction '#1' posted", $form->{reference}));
1411 $main::lxdebug->leave_sub();
1415 $main::lxdebug->enter_sub();
1417 $main::auth->assert('gl_transactions');
1419 my $form = $main::form;
1423 $main::lxdebug->leave_sub();
1428 $main::lxdebug->enter_sub();
1430 $main::auth->assert('gl_transactions');
1432 my $form = $main::form;
1433 my %myconfig = %main::myconfig;
1434 my $locale = $main::locale;
1436 # don't cancel cancelled transactions
1437 if (IS->has_storno(\%myconfig, $form, 'gl')) {
1438 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1439 $form->error($locale->text("Transaction has already been cancelled!"));
1442 GL->storno($form, \%myconfig, $form->{id});
1444 # saving the history
1445 if(!exists $form->{addition} && $form->{id} ne "") {
1446 $form->{snumbers} = qq|gltransaction_| . $form->{id};
1447 $form->{addition} = "STORNO";
1448 $form->{what_done} = "gl_transaction";
1449 $form->save_history;
1451 # /saving the history
1453 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1455 $main::lxdebug->leave_sub();
1459 call_sub($main::form->{nextsub});
1462 sub get_tax_dropdown {
1463 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
1464 my @tax_accounts = GL->get_active_taxes_for_chart($::form->{accno_id}, $transdate);
1465 my $html = $::form->parse_html_template("gl/update_tax_accounts", { TAX_ACCOUNTS => \@tax_accounts });
1467 print $::form->ajax_response_header, $html;
1470 sub get_chart_balance {
1471 my %balances = GL->get_chart_balances($::form->{accno_id});
1472 my $balance = $::form->format_amount(\%::myconfig, $balances{ $::form->{accno_id} }, 2, 'DRCR');
1474 print $::form->ajax_response_header, $balance;