Merge pull request #11 from freiphone/patch-3
[kivitendo-erp.git] / bin / mozilla / gl.pl
index 0776b75..6dfa457 100644 (file)
@@ -49,7 +49,6 @@ use SL::DBUtils qw(selectrow_query selectall_hashref_query);
 use SL::Webdav;
 use SL::Locale::String qw(t8);
 use SL::Helper::GlAttachments qw(count_gl_attachments);
-
 require "bin/mozilla/common.pl";
 require "bin/mozilla/reportgenerator.pl";
 
@@ -94,6 +93,7 @@ sub load_record_template {
   die "invalid template type" unless $template->template_type eq 'gl_transaction';
 
   $template->substitute_variables;
+  my $payment_suggestion =  $::form->{form_defaults}->{amount_1};
 
   # Clean the current $::form before rebuilding it from the template.
   my $form_defaults = delete $::form->{form_defaults};
@@ -133,8 +133,8 @@ sub load_record_template {
 
     $::form->{"accno_id_${row}"}          = $item->chart_id;
     $::form->{"previous_accno_id_${row}"} = $item->chart_id;
-    $::form->{"debit_${row}"}             = $::form->format_amount(\%::myconfig, $item->amount1, 2) if $item->amount1 * 1;
-    $::form->{"credit_${row}"}            = $::form->format_amount(\%::myconfig, $item->amount2, 2) if $item->amount2 * 1;
+    $::form->{"debit_${row}"}             = $::form->format_amount(\%::myconfig, ($payment_suggestion ? $payment_suggestion : $item->amount1), 2) if $item->amount1 * 1;
+    $::form->{"credit_${row}"}            = $::form->format_amount(\%::myconfig, ($payment_suggestion ? $payment_suggestion : $item->amount2), 2) if $item->amount2 * 1;
     $::form->{"taxchart_${row}"}          = $item->tax_id . '--' . $tax->rate;
     $::form->{"${_}_${row}"}              = $item->$_ for qw(source memo project_id);
   }
@@ -313,7 +313,6 @@ sub edit {
       type     => 'general_ledger',
       number   => $form->{id},
     );
-    my $webdav_path = $webdav->webdav_path;
     my @all_objects = $webdav->get_all_objects;
     @{ $form->{WEBDAV} } = map { { name => $_->filename,
                                    type => t8('File'),
@@ -416,7 +415,7 @@ sub generate_report {
   my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
 
   my @columns = qw(
-    gldate         transdate        id             reference      description
+    transdate      gldate   id      reference      description
     notes          source   doccnt  debit          debit_accno
     credit         credit_accno     debit_tax      debit_tax_accno
     credit_tax     credit_tax_accno projectnumbers balance employee
@@ -435,7 +434,7 @@ sub generate_report {
   push @options,      $locale->text('Description') . " : $form->{description}"                        if ($form->{description});
   push @options,      $locale->text('Notes')       . " : $form->{notes}"                              if ($form->{notes});
   push @options,      $locale->text('Employee')    . " : $employee"                                   if $employee;
-  my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Invoice Date') :  $locale->text('Booking Date');
+  my $datesorttext = $form->{datesort} eq 'transdate' ? $locale->text('Transdate') :  $locale->text('Gldate');
   push @date_options,      "$datesorttext"                              if ($form->{datesort} and ($form->{datefrom} or $form->{dateto}));
   push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1)          if ($form->{datefrom});
   push @date_options, $locale->text('Bis'),  $locale->date(\%myconfig, $form->{dateto},   1)          if ($form->{dateto});
@@ -461,8 +460,8 @@ sub generate_report {
 
   my %column_defs = (
     'id'               => { 'text' => $locale->text('ID'), },
-    'transdate'        => { 'text' => $locale->text('Invoice Date'), },
-    'gldate'           => { 'text' => $locale->text('Booking Date'), },
+    'transdate'        => { 'text' => $locale->text('Transdate'), },
+    'gldate'           => { 'text' => $locale->text('Gldate'), },
     'reference'        => { 'text' => $locale->text('Reference'), },
     'source'           => { 'text' => $locale->text('Source'), },
     'doccnt'           => { 'text' => $locale->text('Document Count'), },
@@ -817,10 +816,9 @@ sub display_rows {
     my %taxchart_labels = ();
     my @taxchart_values = ();
 
-    my $accno_id          = $::form->{"accno_id_$i"};
-    my $chart             = $charts_by_id{$accno_id} // $default_chart;
-    $accno_id             = $chart->{id};
-    my $chart_has_changed = $::form->{"previous_accno_id_$i"} && ($accno_id != $::form->{"previous_accno_id_$i"});
+    my $accno_id = $::form->{"accno_id_$i"};
+    my $chart    = $charts_by_id{$accno_id} // $default_chart;
+    $accno_id    = $chart->{id};
     my ($first_taxchart, $default_taxchart, $taxchart_to_use);
 
     foreach my $item ( GL->get_active_taxes_for_chart($accno_id, $transdate) ) {
@@ -833,7 +831,7 @@ sub display_rows {
       $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
     }
 
-    $taxchart_to_use      = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
+    $taxchart_to_use    //= $default_taxchart // $first_taxchart;
     my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
 
     my $accno = qq|<td>| .
@@ -1084,6 +1082,8 @@ sub form_header {
                    "charts"    => { "key"       => "ALL_CHARTS",
                                     "transdate" => $::form->{transdate} });
 
+  # we cannot book on charttype header
+  @{ $::form->{ALL_CHARTS} } = grep { $_->{charttype} ne 'H' }  @{ $::form->{ALL_CHARTS} };
   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
 
   my $title      = $::form->{title};
@@ -1317,7 +1317,6 @@ sub post_transaction {
 
     $form->error($err[$errno]);
   }
-  undef($form->{callback});
   # saving the history
   if(!exists $form->{addition} && $form->{id} ne "") {
     $form->{snumbers} = qq|gltransaction_| . $form->{id};
@@ -1327,6 +1326,12 @@ sub post_transaction {
   }
   # /saving the history
 
+  if ($form->{callback} =~ /BankTransaction/) {
+    print $form->redirect_header($form->{callback});
+    $form->redirect($locale->text('GL transaction posted.') . ' ' . $locale->text('ID') . ': ' . $form->{id});
+  }
+  # remove or clarify
+  undef($form->{callback});
   $main::lxdebug->leave_sub();
 }
 
@@ -1339,9 +1344,7 @@ sub post {
   my $locale   = $main::locale;
 
   if ($::myconfig{mandatory_departments} && !$form->{department_id}) {
-    $form->{saved_message} = $::locale->text('You have to specify a department.');
-    update();
-    exit;
+    $form->error($locale->text('You have to specify a department.'));
   }
 
   $form->{title}  = $locale->text("$form->{title} General Ledger Transaction");