Lieferantennummer/Kundennummer in Kunden/Lieferantenberichten ersetzt durch schlicht...
[kivitendo-erp.git] / bin / mozilla / gl.pl
index 58266b5..7516cf8 100644 (file)
@@ -41,6 +41,7 @@ use SL::PE;
 use SL::ReportGenerator;
 
 require "bin/mozilla/common.pl";
+require "bin/mozilla/drafts.pl";
 require "bin/mozilla/reportgenerator.pl";
 
 1;
@@ -80,6 +81,8 @@ sub add {
 
   $auth->assert('general_ledger');
 
+  return $lxdebug->leave_sub() if (load_draft_maybe());
+
   $form->{title} = "Add";
 
   $form->{callback} = "gl.pl?action=add" unless $form->{callback};
@@ -507,8 +510,9 @@ sub generate_report {
   map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
 
   my %column_alignment;
-  map { $column_alignment{$_}     = 'right'  } qw(balance id debit credit debit_tax credit_tax);
-  map { $column_alignment{$_}     = 'center' } qw(transdate reference description source notes debit_accno credit_accno debit_tax_accno credit_tax_accno);
+  map { $column_alignment{$_}     = 'right'  } qw(balance id debit credit debit_tax credit_tax balance);
+  map { $column_alignment{$_}     = 'center' } qw(reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
+  map { $column_alignment{$_}     = 'left' } qw(description source notes);
   map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
 
   my $report = SL::ReportGenerator->new(\%myconfig, $form);
@@ -530,18 +534,6 @@ sub generate_report {
   # add sort to callback
   $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
-  $form->{balance} *= $ml;
-
-  if ($form->{accno} && $form->{balance}) {
-    my $row = {
-      'balance' => {
-        'data'  => $form->format_amount(\%myconfig, $form->{balance}, 2),
-        'align' => 'right',
-      },
-    };
-
-    $report->add_data($row);
-  }
 
   my @totals_columns = qw(debit credit debit_tax credit_tax);
   my %subtotals      = map { $_ => 0 } @totals_columns;
@@ -549,7 +541,6 @@ sub generate_report {
   my $idx            = 0;
 
   foreach $ref (@{ $form->{GL} }) {
-    $form->{balance} *= $ml;
 
     my %rows;
 
@@ -559,7 +550,12 @@ sub generate_report {
         my $value         = $ref->{$key}->{$idx};
         $subtotals{$key} += $value;
         $totals{$key}    += $value;
-        $form->{balance}  = abs($form->{balance}) - abs($value);
+        if ($key =~ /debit.*/) {
+          $ml = -1;
+        } else {
+          $ml = 1;
+        }
+        $form->{balance}  = $form->{balance} + $value * $ml;
         push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
       }
     }
@@ -572,7 +568,18 @@ sub generate_report {
     my $row = { };
     map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
 
-    $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2);
+    my $sh = "";
+    if ($form->{balance} < 0) {
+      $sh = " S";
+      $ml = -1;
+    } elsif ($form->{balance} > 0) {
+      $sh = " H";
+      $ml = 1;
+    }
+    my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
+    $data .= $sh;
+
+    $row->{balance}->{data}        = $data;
     $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
 
     map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes);
@@ -606,11 +613,20 @@ sub generate_report {
   my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
 
   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
-  $row->{balance} = {
-    'data'  => $form->format_amount(\%myconfig, $form->{balance} * $ml, 2),
-    'align' => 'right',
-    'class' => 'listtotal',
-  };
+
+  my $sh = "";
+  if ($form->{balance} < 0) {
+    $sh = " S";
+    $ml = -1;
+  } elsif ($form->{balance} > 0) {
+    $sh = " H";
+    $ml = 1;
+  }
+  my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
+  $data .= $sh;
+
+  $row->{balance}->{data}        = $data;
+
   $report->add_data($row);
 
   my $raw_bottom_info_text;
@@ -768,17 +784,6 @@ sub display_rows {
   $form->{totaldebit}  = 0;
   $form->{totalcredit} = 0;
 
-  my @old_project_ids = ();
-  map({ push(@old_project_ids, $form->{"project_id_$_"})
-          if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
-
-  $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
-                                   "all" => 0,
-                                   "old_id" => \@old_project_ids },
-                   "charts" => { "key" => "ALL_CHARTS",
-                                 "transdate" => $form->{transdate} },
-                   "taxcharts" => "ALL_TAXCHARTS");
-
   my %project_labels = ();
   my @project_values = ("");
   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
@@ -792,7 +797,7 @@ sub display_rows {
   my $taxchart_init;
   foreach my $item (@{ $form->{ALL_CHARTS} }) {
     my $key = $item->{accno} . "--" . $item->{tax_id};
-    $taxchart_init = $item->{taxkey_id} unless (@chart_values);
+    $taxchart_init = $item->{tax_id} unless (@chart_values);
     push(@chart_values, $key);
     $chart_labels{$key} = $item->{accno} . "--" . $item->{description};
     $charts{$item->{accno}} = $item;
@@ -803,14 +808,13 @@ sub display_rows {
   my %taxcharts = ();
   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
     my $key = $item->{id} . "--" . $item->{rate};
-    $taxchart_init = $key if ($taxchart_init eq $item->{taxkey});
+    $taxchart_init = $key if ($taxchart_init == $item->{id});
     push(@taxchart_values, $key);
     $taxchart_labels{$key} = $item->{taxdescription} . " " . $item->{rate} * 100 . ' %';
     $taxcharts{$item->{id}} = $item;
   }
 
   for $i (1 .. $form->{rowcount}) {
-
     if ($form->{show_details}) {
       $source = qq|
       <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
@@ -839,13 +843,13 @@ sub display_rows {
       $selected_taxchart = "$item->{id}--$item->{rate}";
     }
 
-    $selected_accno = '' if ($init);
-    $selected_taxchart = $taxchart_init unless ($selected_taxchart ne "");
+    $selected_accno      = '' if ($init);
+    $selected_taxchart ||= $taxchart_init;
 
     $accno = qq|<td>| .
       NTI($cgi->popup_menu('-name' => "accno_$i",
                            '-id' => "accno_$i",
-                           '-onChange' => "setTaxkey(this, $i)",
+                           '-onChange' => "setTaxkey($i)",
                            '-style' => 'width:200px',
                            '-values' => \@chart_values,
                            '-labels' => \%chart_labels,
@@ -927,6 +931,7 @@ sub display_rows {
 
     print qq|<tr valign=top>
     $accno
+    <td id="chart_balance_$i" align="right">&nbsp;</td>
     $fx_transaction
     <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
     <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
@@ -959,6 +964,19 @@ sub form_header {
 
   $auth->assert('general_ledger');
 
+  my @old_project_ids = ();
+  map({ push(@old_project_ids, $form->{"project_id_$_"})
+          if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
+
+  $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
+                                    "all"       => 0,
+                                    "old_id"    => \@old_project_ids },
+                   "charts"    => { "key"       => "ALL_CHARTS",
+                                    "transdate" => $form->{transdate} },
+                   "taxcharts" => "ALL_TAXCHARTS");
+
+  GL->get_chart_balances('charts' => $form->{ALL_CHARTS});
+
   $title         = $form->{title};
   $form->{title} = $locale->text("$title General Ledger Transaction");
   $readonly      = ($form->{id}) ? "readonly" : "";
@@ -976,7 +994,8 @@ sub form_header {
 
   $form->{javascript} = qq|<script type="text/javascript">
   <!--
-  function setTaxkey(accno, row) {
+  function setTaxkey(row) {
+    var accno  = document.getElementById('accno_' + row);
     var taxkey = accno.options[accno.selectedIndex].value;
     var reg = /--([0-9]*)/;
     var found = reg.exec(taxkey);
@@ -996,10 +1015,11 @@ sub form_header {
     var txt = document.getElementsByName('debit_1')[0].value;
     document.getElementsByName('credit_2')[0].value = txt;
   };
-
   //-->
-  </script>|;
-  $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
+  </script>
+  <script type="text/javascript" src="js/show_form_details.js"></script>
+  <script type="text/javascript" src="js/jquery.js"></script>
+|;
 
   $form->{selectdepartment} =~ s/ selected//;
   $form->{selectdepartment} =~
@@ -1057,6 +1077,8 @@ sub form_header {
   $form->{previous_id}     ||= "--";
   $form->{previous_gldate} ||= "--";
 
+  $jsscript .= $form->parse_html_template('gl/form_header_chart_balances_js');
+
   $form->header;
 
   print qq|
@@ -1182,6 +1204,7 @@ sub form_header {
           <tr class=listheading>
          <th class=listheading style="width:15%">|
     . $locale->text('Account') . qq|</th>
+         <th class=listheading style="width:10%">| . $locale->text('Chart balance') . qq|</th>
          <th class=listheading style="width:10%">|
     . $locale->text('Debit') . qq|</th>
          <th class=listheading style="width:10%">|
@@ -1234,8 +1257,7 @@ sub form_footer {
 
   print qq|
     <tr class=listtotal>
-    <td></td>
-    <th align=right class=listtotal> $form->{totaldebit}</th>
+    <th colspan="3" align=right class=listtotal> $form->{totaldebit}</th>
     <th align=right class=listtotal> $form->{totalcredit}</th>
     <td colspan=6></td>
     </tr>
@@ -1274,9 +1296,20 @@ $follow_ups_block
       . qq|"> |;
 
   } else {
-      print qq|
+    if ($form->{draft_id}) {
+      my $remove_draft_checked = 'checked' if ($form->{remove_draft});
+      print qq|<p>\n|
+        . qq|  <input name="remove_draft" id="remove_draft" type="checkbox" class="checkbox" ${remove_draft_checked}>|
+        . qq|  <label for="remove_draft">| . $locale->text('Remove Draft') . qq|</label>\n|
+        . qq|</p>\n|;
+    }
+
+    print qq|
         <input class=submit type=submit name=action id=update_button value="| . $locale->text('Update') . qq|">
-        <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">|;
+        <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|"> |
+        . NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'))
+        . $cgi->hidden('-name' => 'draft_id',          '-default' => [$form->{draft_id}])
+        . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}]);
   }
 
   print "
@@ -1501,6 +1534,8 @@ sub post {
 
   post_transaction();
 
+  remove_draft() if $form->{remove_draft};
+
   $form->{callback} = build_std_url("action=add", "show_details");
   $form->redirect($form->{callback});