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);
 
  53 require "bin/mozilla/common.pl";
 
  54 require "bin/mozilla/reportgenerator.pl";
 
  56 # this is for our long dates
 
  57 # $locale->text('January')
 
  58 # $locale->text('February')
 
  59 # $locale->text('March')
 
  60 # $locale->text('April')
 
  61 # $locale->text('May ')
 
  62 # $locale->text('June')
 
  63 # $locale->text('July')
 
  64 # $locale->text('August')
 
  65 # $locale->text('September')
 
  66 # $locale->text('October')
 
  67 # $locale->text('November')
 
  68 # $locale->text('December')
 
  70 # this is for our short month
 
  71 # $locale->text('Jan')
 
  72 # $locale->text('Feb')
 
  73 # $locale->text('Mar')
 
  74 # $locale->text('Apr')
 
  75 # $locale->text('May')
 
  76 # $locale->text('Jun')
 
  77 # $locale->text('Jul')
 
  78 # $locale->text('Aug')
 
  79 # $locale->text('Sep')
 
  80 # $locale->text('Oct')
 
  81 # $locale->text('Nov')
 
  82 # $locale->text('Dec')
 
  84 sub load_record_template {
 
  85   $::auth->assert('gl_transactions');
 
  87   # Load existing template and verify that its one for this module.
 
  88   my $template = SL::DB::RecordTemplate
 
  89     ->new(id => $::form->{id})
 
  91       with_object => [ qw(customer payment currency record_items record_items.chart) ],
 
  94   die "invalid template type" unless $template->template_type eq 'gl_transaction';
 
  96   $template->substitute_variables;
 
  98   # Clean the current $::form before rebuilding it from the template.
 
  99   my $form_defaults = delete $::form->{form_defaults};
 
 100   delete @{ $::form }{ grep { !m{^(?:script|login)$}i } keys %{ $::form } };
 
 103   GL->transaction(\%::myconfig, $dummy_form);
 
 105   # Fill $::form from the template.
 
 106   my $today                   = DateTime->today_local;
 
 107   $::form->{title}            = "Add";
 
 108   $::form->{transdate}        = $today->to_kivitendo;
 
 109   $::form->{duedate}          = $today->to_kivitendo;
 
 110   $::form->{rowcount}         = @{ $template->items };
 
 111   $::form->{paidaccounts}     = 1;
 
 112   $::form->{$_}               = $template->$_     for qw(department_id taxincluded ob_transaction cb_transaction reference description);
 
 113   $::form->{$_}               = $dummy_form->{$_} for qw(closedto revtrans previous_id previous_gldate);
 
 116   foreach my $item (@{ $template->items }) {
 
 119     my $active_taxkey = $item->chart->get_active_taxkey;
 
 120     my $taxes         = SL::DB::Manager::Tax->get_all(
 
 121       where   => [ chart_categories => { like => '%' . $item->chart->category . '%' }],
 
 122       sort_by => 'taxkey, rate',
 
 125     my $tax   = first { $item->tax_id          == $_->id } @{ $taxes };
 
 126     $tax    //= first { $active_taxkey->tax_id == $_->id } @{ $taxes };
 
 127     $tax    //= $taxes->[0];
 
 134     $::form->{"accno_id_${row}"}          = $item->chart_id;
 
 135     $::form->{"previous_accno_id_${row}"} = $item->chart_id;
 
 136     $::form->{"debit_${row}"}             = $::form->format_amount(\%::myconfig, $item->amount1, 2) if $item->amount1 * 1;
 
 137     $::form->{"credit_${row}"}            = $::form->format_amount(\%::myconfig, $item->amount2, 2) if $item->amount2 * 1;
 
 138     $::form->{"taxchart_${row}"}          = $item->tax_id . '--' . $tax->rate;
 
 139     $::form->{"${_}_${row}"}              = $item->$_ for qw(source memo project_id);
 
 142   $::form->{$_} = $form_defaults->{$_} for keys %{ $form_defaults // {} };
 
 144   flash('info', $::locale->text("The record template '#1' has been loaded.", $template->template_name));
 
 147     keep_rows_without_amount => 1,
 
 148     dont_add_new_row         => 1,
 
 152 sub save_record_template {
 
 153   $::auth->assert('gl_transactions');
 
 155   my $template = $::form->{record_template_id} ? SL::DB::RecordTemplate->new(id => $::form->{record_template_id})->load : SL::DB::RecordTemplate->new;
 
 156   my $js       = SL::ClientJS->new(controller => SL::Controller::Base->new);
 
 157   my $new_name = $template->template_name_to_use($::form->{record_template_new_template_name});
 
 159   $js->dialog->close('#record_template_dialog');
 
 162     $_->{chart_id} && (($_->{tax_id} // '') ne '')
 
 164     +{ chart_id   => $::form->{"accno_id_${_}"},
 
 165        amount1    => $::form->parse_amount(\%::myconfig, $::form->{"debit_${_}"}),
 
 166        amount2    => $::form->parse_amount(\%::myconfig, $::form->{"credit_${_}"}),
 
 167        tax_id     => (split m{--}, $::form->{"taxchart_${_}"})[0],
 
 168        project_id => $::form->{"project_id_${_}"} || undef,
 
 169        source     => $::form->{"source_${_}"},
 
 170        memo       => $::form->{"memo_${_}"},
 
 172   } (1..($::form->{rowcount} || 1));
 
 174   $template->assign_attributes(
 
 175     template_type  => 'gl_transaction',
 
 176     template_name  => $new_name,
 
 178     currency_id    => $::instance_conf->get_currency_id,
 
 179     department_id  => $::form->{department_id}    || undef,
 
 180     project_id     => $::form->{globalproject_id} || undef,
 
 181     taxincluded    => $::form->{taxincluded}     ? 1 : 0,
 
 182     ob_transaction => $::form->{ob_transaction}  ? 1 : 0,
 
 183     cb_transaction => $::form->{cb_transaction}  ? 1 : 0,
 
 184     reference      => $::form->{reference},
 
 185     description    => $::form->{description},
 
 195       ->flash('error', $::locale->text("Saving the record template '#1' failed.", $new_name))
 
 200     ->flash('info', $::locale->text("The record template '#1' has been saved.", $new_name))
 
 205   $main::lxdebug->enter_sub();
 
 207   $main::auth->assert('gl_transactions');
 
 209   my $form     = $main::form;
 
 210   my %myconfig = %main::myconfig;
 
 212   $form->{title} = "Add";
 
 214   $form->{callback} = "gl.pl?action=add" unless $form->{callback};
 
 216   # we use this only to set a default date
 
 217   # yep. aber er holt hier auch schon ALL_CHARTS. Aufwand / Nutzen? jb
 
 218   GL->transaction(\%myconfig, \%$form);
 
 220   $form->{rowcount}  = 2;
 
 226   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
 
 228   $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
 
 231   $main::lxdebug->leave_sub();
 
 235 sub prepare_transaction {
 
 236   $main::lxdebug->enter_sub();
 
 238   $main::auth->assert('gl_transactions');
 
 240   my $form     = $main::form;
 
 241   my %myconfig = %main::myconfig;
 
 243   GL->transaction(\%myconfig, \%$form);
 
 245   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
 
 247   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
 
 252   foreach my $ref (@{ $form->{GL} }) {
 
 254     if ($tax && ($ref->{accno} eq $taxaccno)) {
 
 255       $form->{"tax_$j"}      = abs($ref->{amount});
 
 256       $form->{"taxchart_$j"} = $ref->{id} . "--" . $ref->{taxrate};
 
 257       if ($form->{taxincluded}) {
 
 258         if ($ref->{amount} < 0) {
 
 259           $form->{"debit_$j"} += $form->{"tax_$j"};
 
 261           $form->{"credit_$j"} += $form->{"tax_$j"};
 
 264       $form->{"project_id_$j"} = $ref->{project_id};
 
 267       $form->{"accno_id_$i"} = $ref->{chart_id};
 
 268       for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
 
 269       if ($ref->{amount} < 0) {
 
 270         $form->{totaldebit} -= $ref->{amount};
 
 271         $form->{"debit_$i"} = $ref->{amount} * -1;
 
 273         $form->{totalcredit} += $ref->{amount};
 
 274         $form->{"credit_$i"} = $ref->{amount};
 
 276       $form->{"taxchart_$i"} = $ref->{id}."--0.00000";
 
 277       $form->{"project_id_$i"} = $ref->{project_id};
 
 280     if ($ref->{taxaccno} && !$tax) {
 
 281       $taxaccno = $ref->{taxaccno};
 
 289   $form->{rowcount} = $i;
 
 291     ($form->datetonum($form->{transdate}, \%myconfig) <=
 
 292      $form->datetonum($form->{closedto}, \%myconfig));
 
 294   $main::lxdebug->leave_sub();
 
 298   $main::lxdebug->enter_sub();
 
 300   $main::auth->assert('gl_transactions');
 
 302   my $form     = $main::form;
 
 303   my %myconfig = %main::myconfig;
 
 305   prepare_transaction();
 
 307   $form->{title} = "Edit";
 
 309   $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
 
 311   if ($form->{id} && $::instance_conf->get_webdav) {
 
 312     my $webdav = SL::Webdav->new(
 
 313       type     => 'general_ledger',
 
 314       number   => $form->{id},
 
 316     my $webdav_path = $webdav->webdav_path;
 
 317     my @all_objects = $webdav->get_all_objects;
 
 318     @{ $form->{WEBDAV} } = map { { name => $_->filename,
 
 320                                    link => File::Spec->catfile($_->full_filedescriptor),
 
 327   $main::lxdebug->leave_sub();
 
 332   $::lxdebug->enter_sub;
 
 333   $::auth->assert('general_ledger | gl_transactions');
 
 336     projects  => { key => "ALL_PROJECTS", all => 1 },
 
 338   $::form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
 
 339   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
 
 342   print $::form->parse_html_template('gl/search', {
 
 343     employee_label => sub { "$_[0]{id}--$_[0]{name}" },
 
 346   $::lxdebug->leave_sub;
 
 349 sub create_subtotal_row {
 
 350   $main::lxdebug->enter_sub();
 
 352   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 354   my $form     = $main::form;
 
 355   my %myconfig = %main::myconfig;
 
 357   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 359   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 361   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 363   $main::lxdebug->leave_sub();
 
 368 sub generate_report {
 
 369   $main::lxdebug->enter_sub();
 
 371   $main::auth->assert('general_ledger | gl_transactions');
 
 373   my $form     = $main::form;
 
 374   my %myconfig = %main::myconfig;
 
 375   my $locale   = $main::locale;
 
 377   # 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
 
 379   # <form method=post action=gl.pl>
 
 380   # <input type=hidden name=sort value=datesort>    # form->{sort} setzen
 
 381   # <input type=hidden name=nextsub value=generate_report>
 
 383   # anhand von neuer Variable datesort wird jetzt $form->{sort} auf transdate oder gldate gesetzt
 
 384   # damit ist die Hidden Variable "sort" wahrscheinlich sogar Ã¼berflüssig
 
 386   # Ã¤ndert man die Sortierreihenfolge per Klick auf eine der Ãœberschriften wird die Variable "sort" per GET Ã¼bergeben, z.B. id,transdate, gldate, ...
 
 387   # 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
 
 389   if ( $form->{sort} eq 'datesort' ) {   # sollte bei einem Post (Aufruf aus Suchmaske) immer wahr sein
 
 390       # je nachdem ob in Suchmaske "transdate" oder "gldate" ausgesucht wurde erstes Suchergebnis entsprechend sortieren
 
 391       $form->{sort} = $form->{datesort};
 
 395   report_generator_set_default_sort("$form->{datesort}", 1);
 
 396 #  report_generator_set_default_sort('transdate', 1);
 
 398   GL->all_transactions(\%myconfig, \%$form);
 
 400   my %acctype = ('A' => $locale->text('Asset'),
 
 401                  'C' => $locale->text('Contra'),
 
 402                  'L' => $locale->text('Liability'),
 
 403                  'Q' => $locale->text('Equity'),
 
 404                  'I' => $locale->text('Revenue'),
 
 405                  'E' => $locale->text('Expense'),);
 
 407   $form->{title} = $locale->text('Journal');
 
 408   if ($form->{category} ne 'X') {
 
 409     $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
 
 412   $form->{landscape} = 1;
 
 414   my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
 
 417     gldate         transdate        id             reference      description
 
 418     notes          source   doccnt  debit          debit_accno
 
 419     credit         credit_accno     debit_tax      debit_tax_accno
 
 420     credit_tax     credit_tax_accno projectnumbers balance employee
 
 423   # add employee here, so that variable is still known and passed in url when choosing a different sort order in resulting table
 
 424   my @hidden_variables = qw(accno source reference description notes project_id datefrom dateto employee_id datesort category l_subtotal);
 
 425   push @hidden_variables, map { "l_${_}" } @columns;
 
 427   my $employee = $form->{employee_id} ? SL::DB::Employee->new(id => $form->{employee_id})->load->name : '';
 
 429   my (@options, @date_options);
 
 430   push @options,      $locale->text('Account')     . " : $form->{accno} $form->{account_description}" if ($form->{accno});
 
 431   push @options,      $locale->text('Source')      . " : $form->{source}"                             if ($form->{source});
 
 432   push @options,      $locale->text('Reference')   . " : $form->{reference}"                          if ($form->{reference});
 
 433   push @options,      $locale->text('Description') . " : $form->{description}"                        if ($form->{description});
 
 434   push @options,      $locale->text('Notes')       . " : $form->{notes}"                              if ($form->{notes});
 
 435   push @options,      $locale->text('Employee')    . " : $employee"                                   if $employee;
 
 436   my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Invoice Date') :  $locale->text('Booking Date');
 
 437   push @date_options,      "$datesorttext"                              if ($form->{datesort} and ($form->{datefrom} or $form->{dateto}));
 
 438   push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1)          if ($form->{datefrom});
 
 439   push @date_options, $locale->text('Bis'),  $locale->date(\%myconfig, $form->{dateto},   1)          if ($form->{dateto});
 
 440   push @options,      join(' ', @date_options)                                                        if (scalar @date_options);
 
 442   if ($form->{department_id}) {
 
 443     my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
 
 444     push @options, $locale->text('Department') . " : " . $department->description;
 
 447   my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
 
 449   $form->{l_credit_accno}     = 'Y';
 
 450   $form->{l_debit_accno}      = 'Y';
 
 451   $form->{l_credit_tax}       = 'Y';
 
 452   $form->{l_debit_tax}        = 'Y';
 
 453 #  $form->{l_gldate}           = 'Y';  # Spalte mit gldate immer anzeigen
 
 454   $form->{l_credit_tax_accno} = 'Y';
 
 455   $form->{l_datesort} = 'Y';
 
 456   $form->{l_debit_tax_accno}  = 'Y';
 
 457   $form->{l_balance}          = $form->{accno} ? 'Y' : '';
 
 458   $form->{l_doccnt}           = $form->{l_source} ? 'Y' : '';
 
 461     'id'               => { 'text' => $locale->text('ID'), },
 
 462     'transdate'        => { 'text' => $locale->text('Invoice Date'), },
 
 463     'gldate'           => { 'text' => $locale->text('Booking Date'), },
 
 464     'reference'        => { 'text' => $locale->text('Reference'), },
 
 465     'source'           => { 'text' => $locale->text('Source'), },
 
 466     'doccnt'           => { 'text' => $locale->text('Document Count'), },
 
 467     'description'      => { 'text' => $locale->text('Description'), },
 
 468     'notes'            => { 'text' => $locale->text('Notes'), },
 
 469     'debit'            => { 'text' => $locale->text('Debit'), },
 
 470     'debit_accno'      => { 'text' => $locale->text('Debit Account'), },
 
 471     'credit'           => { 'text' => $locale->text('Credit'), },
 
 472     'credit_accno'     => { 'text' => $locale->text('Credit Account'), },
 
 473     'debit_tax'        => { 'text' => $locale->text('Debit Tax'), },
 
 474     'debit_tax_accno'  => { 'text' => $locale->text('Debit Tax Account'), },
 
 475     'credit_tax'       => { 'text' => $locale->text('Credit Tax'), },
 
 476     'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
 
 477     'balance'          => { 'text' => $locale->text('Balance'), },
 
 478     'projectnumbers'   => { 'text' => $locale->text('Project Numbers'), },
 
 479     'employee'         => { 'text' => $locale->text('Employee'), },
 
 482   foreach my $name (qw(id transdate gldate reference description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
 
 483     my $sortname                = $name =~ m/accno/ ? 'accno' : $name;
 
 484     my $sortdir                 = $sortname eq $form->{sort} ? 1 - $form->{sortdir} : $form->{sortdir};
 
 485     $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
 
 488   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 489   map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
 
 491   my %column_alignment;
 
 492   map { $column_alignment{$_}     = 'right'  } qw(balance id debit credit debit_tax credit_tax balance);
 
 493   map { $column_alignment{$_}     = 'center' } qw(transdate gldate reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
 
 494   map { $column_alignment{$_}     = 'left' } qw(description source notes);
 
 495   map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
 
 497   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 499   $report->set_columns(%column_defs);
 
 500   $report->set_column_order(@columns);
 
 502   $form->{l_attachments} = 'Y';
 
 503   $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir l_attachments));
 
 505   $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
 
 507   $report->set_options('top_info_text'        => join("\n", @options),
 
 508                        'output_format'        => 'HTML',
 
 509                        'title'                => $form->{title},
 
 510                        'attachment_basename'  => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
 
 512   $report->set_options_from_form();
 
 513   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 515   # add sort to callback
 
 516   $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
 519   my @totals_columns = qw(debit credit debit_tax credit_tax);
 
 520   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 521   my %totals         = map { $_ => 0 } @totals_columns;
 
 524   foreach my $ref (@{ $form->{GL} }) {
 
 528     foreach my $key (qw(debit credit debit_tax credit_tax)) {
 
 530       foreach my $idx (sort keys(%{ $ref->{$key} })) {
 
 531         my $value         = $ref->{$key}->{$idx};
 
 532         $subtotals{$key} += $value;
 
 533         $totals{$key}    += $value;
 
 534         if ($key =~ /debit.*/) {
 
 539         $form->{balance}  = $form->{balance} + $value * $ml;
 
 540         push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
 
 544     foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
 
 545       my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
 
 546       $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
 
 550     map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
 
 552     if ( $form->{l_doccnt} ) {
 
 553       $row->{doccnt}->{data} = SL::Helper::GlAttachments->count_gl_pdf_attachments($ref->{id},$ref->{type});
 
 557     if ($form->{balance} < 0) {
 
 560     } elsif ($form->{balance} > 0) {
 
 564     my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
 
 567     $row->{balance}->{data}        = $data;
 
 568     $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
 
 570     map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes gldate employee);
 
 572     map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
 
 574     foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
 
 575       $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
 
 578     map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
 
 580     $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
 
 582     my $row_set = [ $row ];
 
 584     if ( ($form->{l_subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
 
 585         && (($idx == (scalar @{ $form->{GL} } - 1))
 
 586             || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
 
 587       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
 
 590     $report->add_data($row_set);
 
 595   # = 0 for balanced ledger
 
 596   my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
 
 598   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
 
 601   if ($form->{balance} < 0) {
 
 604   } elsif ($form->{balance} > 0) {
 
 608   my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
 
 611   $row->{balance}->{data}        = $data;
 
 613   if ( !$form->{report_generator_csv_options_for_import} ) {
 
 614     $report->add_separator();
 
 615     $report->add_data($row);
 
 618   my $raw_bottom_info_text;
 
 620   if (!$form->{accno} && (abs($balanced_ledger) >  0.001)) {
 
 621     $raw_bottom_info_text .=
 
 622         '<p><span class="unbalanced_ledger">'
 
 623       . $locale->text('Unbalanced Ledger')
 
 625       . $form->format_amount(\%myconfig, $balanced_ledger, 3)
 
 629   $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
 
 631   $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
 
 633   $report->generate_with_headers();
 
 635   $main::lxdebug->leave_sub();
 
 639   $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
 
 640   $::form->{gldate}    = $::form->{transdate} if !$::form->{gldate};
 
 647   $main::lxdebug->enter_sub();
 
 649   $main::auth->assert('gl_transactions');
 
 651   my $form     = $main::form;
 
 652   my %myconfig = %main::myconfig;
 
 654   $form->{oldtransdate} = $form->{transdate};
 
 662   my ($debitcredit, $amount);
 
 664   my $dbh = SL::DB->client->dbh;
 
 665   my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
 
 666   my $zerotaxes  = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
 
 669     qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
 
 671   for my $i (1 .. $form->{rowcount}) {
 
 672     $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) for qw(debit credit tax);
 
 674     next if !$form->{"debit_$i"} && !$form->{"credit_$i"} && !$params{keep_rows_without_amount};
 
 677     $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
 
 684     if (($debitcount >= 2) && ($creditcount == 2)) {
 
 685       $form->{"credit_$i"} = 0;
 
 686       $form->{"tax_$i"}    = 0;
 
 688       $form->{creditlock} = 1;
 
 690     if (($creditcount >= 2) && ($debitcount == 2)) {
 
 691       $form->{"debit_$i"} = 0;
 
 692       $form->{"tax_$i"}   = 0;
 
 694       $form->{debitlock} = 1;
 
 696     if (($creditcount == 1) && ($debitcount == 2)) {
 
 697       $form->{creditlock} = 1;
 
 699     if (($creditcount == 2) && ($debitcount == 1)) {
 
 700       $form->{debitlock} = 1;
 
 702     if ($debitcredit && $credittax) {
 
 703       $form->{"taxchart_$i"} = "$notax_id--0.00";
 
 705     if (!$debitcredit && $debittax) {
 
 706       $form->{"taxchart_$i"} = "$notax_id--0.00";
 
 709       ($form->{"debit_$i"} == 0)
 
 710       ? $form->{"credit_$i"}
 
 711       : $form->{"debit_$i"};
 
 713     if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
 
 714       $form->{"taxchart_$i"} = "$notax_id--0.00";
 
 715       $form->{"tax_$i"}      = 0;
 
 717     my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
 718     my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
 
 719     if (!$iswithouttax) {
 
 726     my ($tmpnetamount,$tmpdiff);
 
 727     ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
 
 729     for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
 
 733   for my $i (1 .. $count) {
 
 735     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
 
 738   for my $i ($count + 1 .. $form->{rowcount}) {
 
 739     for (@flds) { delete $form->{"${_}_$i"} }
 
 742   $form->{rowcount} = $count + ($params{dont_add_new_row} ? 0 : 1);
 
 745   $main::lxdebug->leave_sub();
 
 751   $main::lxdebug->enter_sub();
 
 753   $main::auth->assert('gl_transactions');
 
 755   my $form     = $main::form;
 
 756   my %myconfig = %main::myconfig;
 
 760   #   for $i (1 .. $form->{rowcount}) {
 
 761   #     $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
 
 762   #     $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
 
 766   &display_rows($init);
 
 768   $main::lxdebug->leave_sub();
 
 774   $main::lxdebug->enter_sub();
 
 776   $main::auth->assert('gl_transactions');
 
 778   my $form     = $main::form;
 
 779   my %myconfig = %main::myconfig;
 
 780   my $cgi      = $::request->{cgi};
 
 782   my %balances = GL->get_chart_balances(map { $_->{id} } @{ $form->{ALL_CHARTS} });
 
 784   $form->{debit_1}     = 0 if !$form->{"debit_1"};
 
 785   $form->{totaldebit}  = 0;
 
 786   $form->{totalcredit} = 0;
 
 788   my %project_labels = ();
 
 789   my @project_values = ("");
 
 790   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 791     push(@project_values, $item->{"id"});
 
 792     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
 
 795   my %charts_by_id  = map { ($_->{id} => $_) } @{ $::form->{ALL_CHARTS} };
 
 796   my $default_chart = $::form->{ALL_CHARTS}[0];
 
 797   my $transdate     = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
 
 799   my ($source, $memo, $source_hidden, $memo_hidden);
 
 800   for my $i (1 .. $form->{rowcount}) {
 
 801     if ($form->{show_details}) {
 
 803       <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
 
 805       <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
 
 808       <input type="hidden" name="source_$i" value="$form->{"source_$i"}" size="16">|;
 
 810       <input type="hidden" name="memo_$i" value="$form->{"memo_$i"}" size="16">|;
 
 813     my %taxchart_labels = ();
 
 814     my @taxchart_values = ();
 
 816     my $accno_id          = $::form->{"accno_id_$i"};
 
 817     my $chart             = $charts_by_id{$accno_id} // $default_chart;
 
 818     $accno_id             = $chart->{id};
 
 819     my $chart_has_changed = $::form->{"previous_accno_id_$i"} && ($accno_id != $::form->{"previous_accno_id_$i"});
 
 820     my ($first_taxchart, $default_taxchart, $taxchart_to_use);
 
 822     foreach my $item ( GL->get_active_taxes_for_chart($accno_id, $transdate) ) {
 
 823       my $key             = $item->id . "--" . $item->rate;
 
 824       $first_taxchart   //= $item;
 
 825       $default_taxchart   = $item if $item->{is_default};
 
 826       $taxchart_to_use    = $item if $key eq $form->{"taxchart_$i"};
 
 828       push(@taxchart_values, $key);
 
 829       $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
 
 832     $taxchart_to_use      = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
 
 833     my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
 
 835     my $accno = qq|<td>| .
 
 836       $::request->presenter->chart_picker("accno_id_$i", $accno_id, style => "width: 300px") .
 
 837       $::request->presenter->hidden_tag("previous_accno_id_$i", $accno_id)
 
 839     my $tax_ddbox = qq|<td>| .
 
 840       NTI($cgi->popup_menu('-name' => "taxchart_$i",
 
 841             '-id' => "taxchart_$i",
 
 842             '-style' => 'width:200px',
 
 843             '-values' => \@taxchart_values,
 
 844             '-labels' => \%taxchart_labels,
 
 845             '-default' => $selected_taxchart))
 
 848     my ($fx_transaction, $checked);
 
 850       if ($form->{transfer}) {
 
 851         $fx_transaction = qq|
 
 852         <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
 
 857       if ($form->{"debit_$i"} != 0) {
 
 858         $form->{totaldebit} += $form->{"debit_$i"};
 
 859         if (!$form->{taxincluded}) {
 
 860           $form->{totaldebit} += $form->{"tax_$i"};
 
 863         $form->{totalcredit} += $form->{"credit_$i"};
 
 864         if (!$form->{taxincluded}) {
 
 865           $form->{totalcredit} += $form->{"tax_$i"};
 
 869       for (qw(debit credit tax)) {
 
 872           ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
 
 876       if ($i < $form->{rowcount}) {
 
 877         if ($form->{transfer}) {
 
 878           $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
 
 879           my $x = ($checked) ? "x" : "";
 
 880           $fx_transaction = qq|
 
 881       <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
 
 884         $form->hide_form("accno_$i");
 
 887         if ($form->{transfer}) {
 
 888           $fx_transaction = qq|
 
 889       <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
 
 894     my $debitreadonly  = "";
 
 895     my $creditreadonly = "";
 
 896     if ($i == $form->{rowcount}) {
 
 897       if ($form->{debitlock}) {
 
 898         $debitreadonly = "readonly";
 
 899       } elsif ($form->{creditlock}) {
 
 900         $creditreadonly = "readonly";
 
 905       NTI($cgi->popup_menu('-name' => "project_id_$i",
 
 906                            '-values' => \@project_values,
 
 907                            '-labels' => \%project_labels,
 
 908                            '-default' => $form->{"project_id_$i"} ));
 
 909     my $projectnumber_hidden = qq|
 
 910     <input type="hidden" name="project_id_$i" value="$form->{"project_id_$i"}">|;
 
 912     my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : '';
 
 913     my $balance     = $form->format_amount(\%::myconfig, $balances{$accno_id} // 0, 2, 'DRCR');
 
 915     print qq|<tr valign=top>
 
 917     <td id="chart_balance_$i" align="right">${balance}</td>
 
 919     <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
 
 920     <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
 
 921     <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
 
 924     if ($form->{show_details}) {
 
 928     <td>$projectnumber</td>
 
 934     $projectnumber_hidden
 
 942   $form->hide_form(qw(rowcount selectaccno));
 
 944   $main::lxdebug->leave_sub();
 
 949   return ($::instance_conf->get_gl_changeable == 2) ? ($::form->current_date(\%::myconfig) eq $::form->{gldate}) : ($::instance_conf->get_gl_changeable == 1);
 
 953   $::lxdebug->enter_sub;
 
 954   $::auth->assert('gl_transactions');
 
 958   $::request->layout->add_javascripts("autocomplete_chart.js", "kivi.GL.js", "kivi.RecordTemplate.js");
 
 960   my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount};
 
 962   $::form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
 
 964                                     "old_id"    => \@old_project_ids },
 
 966                    "charts"    => { "key"       => "ALL_CHARTS",
 
 967                                     "transdate" => $::form->{transdate} });
 
 969   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
 
 971   my $title      = $::form->{title};
 
 972   $::form->{title} = $::locale->text("$title General Ledger Transaction");
 
 973   # $locale->text('Add General Ledger Transaction')
 
 974   # $locale->text('Edit General Ledger Transaction')
 
 976   map { $::form->{$_} =~ s/\"/"/g }
 
 980     $::request->{layout}->focus("#reference");
 
 981     $::form->{taxincluded} = "1";
 
 983     $::request->{layout}->focus("#accno_id_$::form->{rowcount}_name");
 
 986   $::form->{previous_id}     ||= "--";
 
 987   $::form->{previous_gldate} ||= "--";
 
 990   print $::form->parse_html_template('gl/form_header', {
 
 991     hide_title => $title,
 
 992     readonly   => $::form->{id} && ($::form->{locked} || !_get_radieren()),
 
 995   $::lxdebug->leave_sub;
 
1000   $::lxdebug->enter_sub;
 
1001   $::auth->assert('gl_transactions');
 
1003   my ($follow_ups, $follow_ups_due);
 
1005   if ($::form->{id}) {
 
1006     $follow_ups     = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
 
1007     $follow_ups_due = sum map { $_->{due} * 1 } @{ $follow_ups || [] };
 
1010   print $::form->parse_html_template('gl/form_footer', {
 
1011     radieren       => _get_radieren(),
 
1012     follow_ups     => $follow_ups,
 
1013     follow_ups_due => $follow_ups_due,
 
1016   $::lxdebug->leave_sub;
 
1020   $main::lxdebug->enter_sub();
 
1022   my $form     = $main::form;
 
1023   my $locale   = $main::locale;
 
1028 <form method=post action=gl.pl>
 
1031   map { $form->{$_} =~ s/\"/"/g } qw(reference description);
 
1033   delete $form->{header};
 
1035   foreach my $key (keys %$form) {
 
1036     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1037     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
 
1041 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
 
1044     . $locale->text('Are you sure you want to delete Transaction')
 
1045     . qq| $form->{reference}</h4>
 
1047 <input name=action class=submit type=submit value="|
 
1048     . $locale->text('Yes') . qq|">
 
1051   $main::lxdebug->leave_sub();
 
1056   $main::lxdebug->enter_sub();
 
1058   my $form     = $main::form;
 
1059   my %myconfig = %main::myconfig;
 
1060   my $locale   = $main::locale;
 
1062   if (GL->delete_transaction(\%myconfig, \%$form)){
 
1063     # saving the history
 
1064       if(!exists $form->{addition} && $form->{id} ne "") {
 
1065         $form->{snumbers} = qq|gltransaction_| . $form->{id};
 
1066         $form->{addition} = "DELETED";
 
1067         $form->{what_done} = "gl_transaction";
 
1068         $form->save_history;
 
1070     # /saving the history
 
1071     $form->redirect($locale->text('Transaction deleted!'))
 
1073   $form->error($locale->text('Cannot delete transaction!'));
 
1074   $main::lxdebug->leave_sub();
 
1078 sub post_transaction {
 
1079   $main::lxdebug->enter_sub();
 
1081   my $form     = $main::form;
 
1082   my %myconfig = %main::myconfig;
 
1083   my $locale   = $main::locale;
 
1085   # check if there is something in reference and date
 
1086   $form->isblank("reference",   $locale->text('Reference missing!'));
 
1087   $form->isblank("transdate",   $locale->text('Transaction Date missing!'));
 
1088   $form->isblank("description", $locale->text('Description missing!'));
 
1090   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
1091   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
1098   my $creditcount = 0;
 
1100   my %split_safety = ();
 
1102   my $dbh = SL::DB->client->dbh;
 
1103   my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", );
 
1104   my $zerotaxes  = selectall_hashref_query($form, $dbh, "SELECT id FROM tax WHERE rate = 0", );
 
1106   my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
 
1108   for my $i (1 .. $form->{rowcount}) {
 
1109     next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
 
1111     for (qw(debit credit tax)) {
 
1112       $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
 
1116     $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
 
1118     $split_safety{   $form->{"debit_$i"}  <=> 0 }++;
 
1119     $split_safety{ - $form->{"credit_$i"} <=> 0 }++;
 
1127     if (($debitcount >= 2) && ($creditcount == 2)) {
 
1128       $form->{"credit_$i"} = 0;
 
1129       $form->{"tax_$i"}    = 0;
 
1131       $form->{creditlock} = 1;
 
1133     if (($creditcount >= 2) && ($debitcount == 2)) {
 
1134       $form->{"debit_$i"} = 0;
 
1135       $form->{"tax_$i"}   = 0;
 
1137       $form->{debitlock} = 1;
 
1139     if (($creditcount == 1) && ($debitcount == 2)) {
 
1140       $form->{creditlock} = 1;
 
1142     if (($creditcount == 2) && ($debitcount == 1)) {
 
1143       $form->{debitlock} = 1;
 
1145     if ($debitcredit && $credittax) {
 
1146       $form->{"taxchart_$i"} = "$notax_id--0.00";
 
1148     if (!$debitcredit && $debittax) {
 
1149       $form->{"taxchart_$i"} = "$notax_id--0.00";
 
1151     my $amount = ($form->{"debit_$i"} == 0)
 
1152             ? $form->{"credit_$i"}
 
1153             : $form->{"debit_$i"};
 
1155     if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
 
1156       $form->{"taxchart_$i"} = "$notax_id--0.00";
 
1157       $form->{"tax_$i"}      = 0;
 
1159     my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
1160     my $iswithouttax = grep { $_->{id} == $taxkey } @{ $zerotaxes };
 
1161     if (!$iswithouttax) {
 
1168       my ($tmpnetamount,$tmpdiff);
 
1169       ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2);
 
1171         $form->{"debit_$i"} = $tmpnetamount;
 
1173         $form->{"credit_$i"} = $tmpnetamount;
 
1177       $form->{"tax_$i"} = 0;
 
1180     for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
 
1184   if ($split_safety{-1} > 1 && $split_safety{1} > 1) {
 
1185     $::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. " .
 
1186                                    "Due to known problems involving accounting software kivitendo does not allow these."));
 
1189   for my $i (1 .. $count) {
 
1191     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
 
1194   for my $i ($count + 1 .. $form->{rowcount}) {
 
1195     for (@flds) { delete $form->{"${_}_$i"} }
 
1198   my ($debit, $credit, $taxtotal);
 
1199   for my $i (1 .. $form->{rowcount}) {
 
1200     my $dr  = $form->{"debit_$i"};
 
1201     my $cr  = $form->{"credit_$i"};
 
1202     my $tax = $form->{"tax_$i"};
 
1204       $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
 
1206     $debit    += $dr + $tax if $dr;
 
1207     $credit   += $cr + $tax if $cr;
 
1208     $taxtotal += $tax if $form->{taxincluded}
 
1211   $form->{taxincluded} = 0 if !$taxtotal;
 
1213   # this is just for the wise guys
 
1215   $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
 
1216     if ($form->date_max_future($form->{"transdate"}, \%myconfig));
 
1217   $form->error($locale->text('Cannot post transaction for a closed period!'))
 
1218     if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
1219   if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
 
1220     $form->error($locale->text('Out of balance transaction!'));
 
1223   if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
 
1224     $form->error($locale->text('Empty transaction!'));
 
1227   if ((my $errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
 
1230     $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
 
1231     $err[2] = $locale->text('Debit and credit out of balance!');
 
1232     $err[3] = $locale->text('Cannot post a transaction without a value!');
 
1234     $form->error($err[$errno]);
 
1236   undef($form->{callback});
 
1237   # saving the history
 
1238   if(!exists $form->{addition} && $form->{id} ne "") {
 
1239     $form->{snumbers} = qq|gltransaction_| . $form->{id};
 
1240     $form->{addition} = "POSTED";
 
1241     $form->{what_done} = "gl transaction";
 
1242     $form->save_history;
 
1244   # /saving the history
 
1246   $main::lxdebug->leave_sub();
 
1250   $main::lxdebug->enter_sub();
 
1252   $main::auth->assert('gl_transactions');
 
1254   my $form     = $main::form;
 
1255   my $locale   = $main::locale;
 
1257   if ($::myconfig{mandatory_departments} && !$form->{department_id}) {
 
1258     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
1263   $form->{title}  = $locale->text("$form->{title} General Ledger Transaction");
 
1264   $form->{storno} = 0;
 
1267   if ($::instance_conf->get_webdav) {
 
1268     SL::Webdav->new(type     => 'general_ledger',
 
1269                     number   => $form->{id},
 
1273   $form->{callback} = build_std_url("action=add", "show_details");
 
1274   $form->redirect($::locale->text("General ledger transaction '#1' posted", $form->{reference}));
 
1276   $main::lxdebug->leave_sub();
 
1280   $main::lxdebug->enter_sub();
 
1282   $main::auth->assert('gl_transactions');
 
1284   my $form     = $main::form;
 
1288   $main::lxdebug->leave_sub();
 
1293   $main::lxdebug->enter_sub();
 
1295   $main::auth->assert('gl_transactions');
 
1297   my $form     = $main::form;
 
1298   my %myconfig = %main::myconfig;
 
1299   my $locale   = $main::locale;
 
1301   # don't cancel cancelled transactions
 
1302   if (IS->has_storno(\%myconfig, $form, 'gl')) {
 
1303     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
 
1304     $form->error($locale->text("Transaction has already been cancelled!"));
 
1307   GL->storno($form, \%myconfig, $form->{id});
 
1309   # saving the history
 
1310   if(!exists $form->{addition} && $form->{id} ne "") {
 
1311     $form->{snumbers} = qq|gltransaction_| . $form->{id};
 
1312     $form->{addition} = "STORNO";
 
1313     $form->{what_done} = "gl_transaction";
 
1314     $form->save_history;
 
1316   # /saving the history
 
1318   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
 
1320   $main::lxdebug->leave_sub();
 
1324   call_sub($main::form->{nextsub});
 
1327 sub get_tax_dropdown {
 
1328   my $transdate    = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
 
1329   my @tax_accounts = GL->get_active_taxes_for_chart($::form->{accno_id}, $transdate);
 
1330   my $html         = $::form->parse_html_template("gl/update_tax_accounts", { TAX_ACCOUNTS => \@tax_accounts });
 
1332   print $::form->ajax_response_header, $html;
 
1335 sub get_chart_balance {
 
1336   my %balances = GL->get_chart_balances($::form->{accno_id});
 
1337   my $balance  = $::form->format_amount(\%::myconfig, $balances{ $::form->{accno_id} }, 2, 'DRCR');
 
1339   print $::form->ajax_response_header, $balance;