show_history.js in header eingebunden
[kivitendo-erp.git] / bin / mozilla / gl.pl
index 87e20d1..5154a3f 100644 (file)
@@ -35,6 +35,7 @@ use SL::GL;
 use SL::PE;
 
 require "$form->{path}/arap.pl";
+require "bin/mozilla/common.pl";
 
 1;
 
@@ -80,24 +81,14 @@ sub add {
   # we use this only to set a default date
   GL->transaction(\%myconfig, \%$form);
 
-  map {
-    $chart .=
-      "<option value=\"$_->{accno}--$_->{tax_id}\">$_->{accno}--$_->{description}</option>"
-  } @{ $form->{chart} };
   map {
     $tax .=
       qq|<option value="$_->{id}--$_->{rate}">$_->{taxdescription}  |
       . ($_->{rate} * 100) . qq| %|
   } @{ $form->{TAX} };
 
-  $form->{chart}     = $chart;
-  $form->{chartinit} = $chart;
   $form->{rowcount}  = 2;
 
-  $form->{debitchart}  = $chart;
-  $form->{creditchart} = $chart;
-  $form->{taxchart}    = $tax;
-
   $form->{debit}  = 0;
   $form->{credit} = 0;
   $form->{tax}    = 0;
@@ -122,10 +113,6 @@ sub edit {
   $lxdebug->enter_sub();
 
   GL->transaction(\%myconfig, \%$form);
-  map {
-    $chart .=
-      "<option value=\"$_->{accno}--$_->{tax_id}\">$_->{accno}--$_->{description}</option>"
-  } @{ $form->{chart} };
 
   map {
     $tax .=
@@ -133,10 +120,6 @@ sub edit {
       . ($_->{rate} * 100) . qq| %|
   } @{ $form->{TAX} };
 
-  $form->{chart} = $chart;
-
-  $form->{taxchart} = $tax;
-
   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
 
   # departments
@@ -154,8 +137,6 @@ sub edit {
   my $tax      = 0;
   my $taxaccno = "";
   foreach $ref (@{ $form->{GL} }) {
-    $form->{"projectnumber_$i"} = "$ref->{projectnumber}--$ref->{project_id}";
-
     $j = $i - 1;
     if ($tax && ($ref->{accno} eq $taxaccno)) {
       $form->{"tax_$j"}      = abs($ref->{amount});
@@ -167,6 +148,8 @@ sub edit {
           $form->{"credit_$j"} += $form->{"tax_$j"};
         }
       }
+      $form->{"project_id_$j"} = $ref->{project_id};
+
     } else {
       $form->{"accno_$i"} = "$ref->{accno}--$ref->{tax_id}";
       for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
@@ -178,6 +161,7 @@ sub edit {
         $form->{"credit_$i"} = $ref->{amount};
       }
       $form->{"taxchart_$i"} = "0--0.00";
+      $form->{"project_id_$i"} = $ref->{project_id};
       $i++;
     }
     if ($ref->{taxaccno} && !$tax) {
@@ -187,7 +171,6 @@ sub edit {
       $taxaccno = "";
       $tax      = 0;
     }
-
   }
 
   $form->{rowcount} = $i;
@@ -228,6 +211,21 @@ sub search {
        </tr>
 | if $form->{selectdepartment};
 
+  $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
+                                   "all" => 1 });
+
+  my %project_labels = ();
+  my @project_values = ("");
+  foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
+    push(@project_values, $item->{"id"});
+    $project_labels{$item->{"id"}} = $item->{"projectnumber"};
+  }
+
+  my $projectnumber =
+    NTI($cgi->popup_menu('-name' => "project_id",
+                         '-values' => \@project_values,
+                         '-labels' => \%project_labels));
+
   # use JavaScript Calendar or not
   $form->{jsscript} = $jscalendar;
   $jsscript = "";
@@ -290,6 +288,10 @@ sub search {
          <th align=right>| . $locale->text('Notes') . qq|</th>
          <td colspan=3><input name=notes size=40></td>
        </tr>
+       <tr>
+         <th align=right>| . $locale->text('Project Number') . qq|</th>
+         <td colspan=3>$projectnumber</td>
+       </tr>
        <tr>
          <th align=right>| . $locale->text('From') . qq|</th>
           $button1
@@ -343,6 +345,8 @@ sub search {
                  <tr>
                    <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
                    <td>| . $locale->text('Subtotal') . qq|</td>
+                   <td align=right><input name="l_projectnumbers" class=checkbox type=checkbox value=Y></td>
+                   <td>| . $locale->text('Project Number') . qq|</td>
                  </tr>
                </table>
              </tr>
@@ -448,6 +452,10 @@ sub generate_report {
     $option   .= "\n<br>" if $option;
     $option   .= $locale->text('Notes') . " : $form->{notes}";
   }
+ if ($form->{project_id}) {
+    $href     .= "&project_id=" . $form->escape($form->{project_id});
+    $callback .= "&project_id=" . $form->escape($form->{project_id});
+  }
 
   if ($form->{datefrom}) {
     $href     .= "&datefrom=$form->{datefrom}";
@@ -470,8 +478,13 @@ sub generate_report {
       . $locale->date(\%myconfig, $form->{dateto}, 1);
   }
 
-  @columns = $form->sort_columns(
-    qw(transdate id reference description notes source debit debit_accno credit credit_accno debit_tax debit_tax_accno credit_tax credit_tax_accno accno gifi_accno)
+  @columns = $form->sort_columns( qw(
+       transdate       id                reference         description  
+       notes           source            debit             debit_accno  
+       credit          credit_accno      debit_tax         debit_tax_accno
+       credit_tax      credit_tax_accno  accno             gifi_accno
+       projectnumbers  
+       )
   );
 
   if ($form->{accno} || $form->{gifi_accno}) {
@@ -561,6 +574,8 @@ sub generate_report {
     . $locale->text('GIFI')
     . "</a></th>";
   $column_header{balance} = "<th>" . $locale->text('Balance') . "</th>";
+  $column_header{projectnumbers} =
+      "<th class=listheading>"  . $locale->text('Project Numbers') . "</th>";
 
   $form->{landscape} = 1;
 
@@ -790,6 +805,8 @@ sub generate_report {
     $column_data{balance} =
       "<td align=right>"
       . $form->format_amount(\%myconfig, $form->{balance}, 2, 0) . "</td>";
+    $column_data{projectnumbers} =
+      "<td>" . join(", ", sort({ lc($a) cmp lc($b) } keys(%{ $ref->{projectnumbers} }))) . "</td>";
 
     $i++;
     $i %= 2;
@@ -804,6 +821,12 @@ sub generate_report {
 
   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
 
+  my $balanced_ledger = $totaldebit 
+                      + $totaldebittax 
+                      - $totalcredit 
+                      - $totalcredittax;
+                    # = 0 for balanced ledger
+                    
   $column_data{debit} =
     "<th align=right class=listtotal>"
     . $form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;") . "</th>";
@@ -828,6 +851,27 @@ sub generate_report {
 
   print qq|
         </tr>
+        <tr>|;
+
+
+  if ( abs($balanced_ledger) >  0.001 ) {
+
+    print qq|<td colspan="4" style="background-color:#FFA0A0" >|
+        . $locale->text('Unbalanced Ledger') 
+        . ": " 
+        . $form->format_amount(\%myconfig, $balanced_ledger, 3, "&nbsp;")
+
+  } elsif ( abs($balanced_ledger) <= 0.001 ) {
+
+    print qq|<td colspan="3">|
+          . $locale->text('Balanced Ledger') 
+
+  }
+
+  
+  print qq|
+         </td>
+        </tr>
         </tbody>
       </table>
     </td>
@@ -895,21 +939,7 @@ sub gl_subtotal {
 sub update {
   $lxdebug->enter_sub();
 
-  if ($form->{transdate} ne $form->{oldtransdate}) {
-    if ($form->{selectprojectnumber}) {
-      $form->all_projects(\%myconfig, undef, $form->{transdate});
-      if (@{ $form->{all_project} }) {
-        $form->{selectprojectnumber} = "<option>\n";
-        for (@{ $form->{all_project} }) {
-          $form->{selectprojectnumber} .=
-            qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
-        }
-        $form->{selectprojectnumber} =
-          $form->escape($form->{selectprojectnumber}, 1);
-      }
-    }
-    $form->{oldtransdate} = $form->{transdate};
-  }
+  $form->{oldtransdate} = $form->{transdate};
 
   my @a           = ();
   my $count       = 0;
@@ -1033,15 +1063,47 @@ sub display_rows {
   my ($init) = @_;
   $lxdebug->enter_sub();
 
-  $form->{selectprojectnumber} = $form->unescape($form->{selectprojectnumber})
-    if $form->{selectprojectnumber};
-
   $form->{totaldebit}  = 0;
   $form->{totalcredit} = 0;
-  my $chart = $form->{chart};
-  $chart            = $form->unquote($chart);
-  $form->{taxchart} = $form->unquote($form->{taxchart});
-  $taxchart         = $form->{taxchart};
+
+  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"} }) {
+    push(@project_values, $item->{"id"});
+    $project_labels{$item->{"id"}} = $item->{"projectnumber"};
+  }
+
+  my %chart_labels = ();
+  my @chart_values = ();
+  my $taxchart_init;
+  foreach my $item (@{ $form->{ALL_CHARTS} }) {
+    my $key = Q($item->{accno}) . "--" . Q($item->{tax_id});
+    $taxchart_init = $item->{taxkey_id} unless (@chart_values);
+    push(@chart_values, $key);
+    $chart_labels{$key} = H($item->{accno}) . "--" . H($item->{description});
+  }
+
+  my %taxchart_labels = ();
+  my @taxchart_values = ();
+  foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
+    my $key = Q($item->{id}) . "--" . Q($item->{rate});
+    $taxchart_init = $key if ($taxchart_init eq $item->{taxkey});
+    push(@taxchart_values, $key);
+    $taxchart_labels{$key} = H($item->{taxdescription}) . " " .
+      H($item->{rate} * 100) . ' %';
+  }
+
   for $i (1 .. $form->{rowcount}) {
 
     $source = qq|
@@ -1051,19 +1113,25 @@ sub display_rows {
     <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16" tabindex=|
       . ($i + 12 + (($i - 1) * 8)) . qq|></td>|;
 
+    $accno = qq|<td>| .
+      $cgi->popup_menu('-name' => "accno_$i",
+                       '-onChange' => "setTaxkey(this, $i)",
+                       '-style' => 'width:200px',
+                       '-tabindex' => ($i + 5 + (($i - 1) * 8)),
+                       '-values' => \@chart_values,
+                       '-labels' => \%chart_labels,
+                       '-default' => $init ? '' : $form->{"accno_$i"})
+      . qq|</td>|;
+    $tax = qq|<td>| .
+      $cgi->popup_menu('-name' => "taxchart_$i",
+                       '-style' => 'width:200px',
+                       '-tabindex' => ($i + 10 + (($i - 1) * 8)),
+                       '-values' => \@taxchart_values,
+                       '-labels' => \%taxchart_labels,
+                       '-default' => $init ? $taxchart_init : $form->{"taxchart_$i"})
+      . qq|</td>|;
+
     if ($init) {
-      $accno = qq|
-      <td><select name="accno_$i" onChange="setTaxkey(this, $i)" style="width:200px" tabindex=|
-        . ($i + 5 + (($i - 1) * 8)) . qq|>$form->{chartinit}</select></td>|;
-      $tax =
-        qq|<td><select id="taxchart_$i" name="taxchart_$i" style="width:200px" tabindex=|
-        . ($i + 10 + (($i - 1) * 8))
-        . qq|>$form->{taxchart}</select></td>|;
-
-      #       if ($form->{selectprojectnumber}) {
-      #         $project = qq|
-      #     <td><select name="projectnumber_$i">$form->{selectprojectnumber}</select></td>|;
-      #       }
       $korrektur =
         qq|<td><input type="checkbox" name="korrektur_$i" value="1" tabindex=|
         . ($i + 9 + (($i - 1) * 8))
@@ -1095,32 +1163,6 @@ sub display_rows {
       }
 
       if ($i < $form->{rowcount}) {
-
-        $accno          = $chart;
-        $chart_selected = $form->{"accno_$i"};
-        $accno =~
-          s/value=\"$chart_selected\"/value=\"$chart_selected\" selected/;
-        $accno =
-          qq|<td><select name="accno_$i" onChange="setTaxkey(this, $i)" style="width:200px" tabindex=|
-          . ($i + 5 + (($i - 1) * 8))
-          . qq|>$accno</select></td>|;
-        $tax          = $taxchart;
-        $tax_selected = $form->{"taxchart_$i"};
-        $tax =~ s/value=\"$tax_selected\"/value=\"$tax_selected\" selected/;
-        $tax =
-            qq|<td><select id="taxchart_$i" name="taxchart_$i" tabindex=|
-          . ($i + 10 + (($i - 1) * 8))
-          . qq|>$tax</select></td>|;
-
-        #         if ($form->{selectprojectnumber}) {
-        #           $form->{"projectnumber_$i"} = ""
-        #             if $form->{selectprojectnumber} !~ /$form->{"projectnumber_$i"}/;
-        #
-        #           $project = $form->{"projectnumber_$i"};
-        #           $project =~ s/--.*//;
-        #           $project = qq|<td>$project</td>|;
-        #         }
-
         if ($form->{transfer}) {
           $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
           $x = ($checked) ? "x" : "";
@@ -1136,18 +1178,6 @@ sub display_rows {
         $form->hide_form("accno_$i");
 
       } else {
-
-        $accno = qq|
-      <td><select name="accno_$i" onChange="setTaxkey(this, $i)" style="width:300px" tabindex=|
-          . ($i + 5 + (($i - 1) * 8)) . qq|>$chart</select></td>|;
-        $tax = qq|
-      <td><select id="taxchart_$i" name="taxchart_$i" tabindex=|
-          . ($i + 10 + (($i - 1) * 8)) . qq|>$taxchart</select></td>|;
-
-        #         if ($form->{selectprojectnumber}) {
-        #           $project = qq|
-        #       <td><select name="projectnumber_$i">$form->{selectprojectnumber}</select></td>|;
-        #         }
         $korrektur =
           qq|<td><input type="checkbox" name="korrektur_$i" value="1" tabindex=|
           . ($i + 9 + (($i - 1) * 8))
@@ -1169,6 +1199,12 @@ sub display_rows {
       }
     }
 
+    my $projectnumber =
+      NTI($cgi->popup_menu('-name' => "project_id_$i",
+                           '-values' => \@project_values,
+                           '-labels' => \%project_labels,
+                           '-default' => $form->{"project_id_$i"} ));
+
     print qq|<tr valign=top>
     $accno
     $fx_transaction
@@ -1182,6 +1218,7 @@ sub display_rows {
     $tax
     $source
     $memo
+    <td>$projectnumber</td>
   </tr>
 
   |;
@@ -1189,9 +1226,6 @@ sub display_rows {
 
   $form->hide_form(qw(rowcount selectaccno));
 
-  #   print qq|
-  # <input type=hidden name=selectprojectnumber value="|
-  #     . $form->escape($form->{selectprojectnumber}, 1) . qq|">|;
   $lxdebug->leave_sub();
 
 }
@@ -1293,8 +1327,6 @@ sub form_header {
 <input type=hidden name=closedto value=$form->{closedto}>
 <input type=hidden name=locked value=$form->{locked}>
 <input type=hidden name=title value="$title">
-<input type=hidden name=taxchart value="$form->{taxchart}">
-<input type=hidden name=chart value="$form->{chart}">
 
 
 <table width=100%>
@@ -1391,7 +1423,8 @@ sub form_header {
          <th class=listheading style="width:20%">|
     . $locale->text('Source') . qq|</th>
          <th class=listheading style="width:20%">| . $locale->text('Memo') . qq|</th>
-         $project
+         <th class=listheading style="width:20%">|
+    . $locale->text('Project Number') . qq|</th>
        </tr>
 
 $jsscript
@@ -1417,7 +1450,7 @@ sub form_footer {
     <td></td>
     <th align=right class=listtotal> $form->{totaldebit}</th>
     <th align=right class=listtotal> $form->{totalcredit}</th> 
-    <td colspan=5></td>
+    <td colspan=6></td>
     </tr>
   </table>
   </td>
@@ -1441,7 +1474,7 @@ sub form_footer {
     print qq|<input class=submit type=submit name=action value="|
       . $locale->text('Storno') . qq|">|;
 
-    # Löschen und ändern von Buchungen nicht mehr möglich (GoB) nur am selben Tag möglich
+    # Löschen und Ändern von Buchungen nicht mehr möglich (GoB) nur am selben Tag möglich
 
     if (!$form->{locked} && $radieren) {
       print qq|
@@ -1485,7 +1518,7 @@ sub delete {
 <form method=post action=$form->{script}>
 |;
 
-  map { $form->{$_} =~ s/\"/&quot;/g } qw(reference description chart taxchart);
+  map { $form->{$_} =~ s/\"/&quot;/g } qw(reference description);
 
   delete $form->{header};
 
@@ -1510,9 +1543,15 @@ sub delete {
 
 sub yes {
   $lxdebug->enter_sub();
-
-  $form->redirect($locale->text('Transaction deleted!'))
-    if (GL->delete_transaction(\%myconfig, \%$form));
+  if (GL->delete_transaction(\%myconfig, \%$form)){
+    # saving the history
+      if(!exists $form->{addition} && $form->{id} ne "") {
+           $form->{addition} = "DELETED";
+           $form->save_history($form->dbconnect(\%myconfig));
+      }
+    # /saving the history 
+    $form->redirect($locale->text('Transaction deleted!'))
+  }
   $form->error($locale->text('Cannot delete transaction!'));
   $lxdebug->leave_sub();
 
@@ -1529,9 +1568,6 @@ sub post {
   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
-  # check project
-  &check_project;
-
   my @a           = ();
   my $count       = 0;
   my $debittax    = 0;
@@ -1701,6 +1737,13 @@ sub post {
     $form->error($err[$errno]);
   }
   undef($form->{callback});
+  # saving the history
+  if(!exists $form->{addition} && $form->{id} ne "") {
+       $form->{addition} = "SAVED";
+       $form->{what_done} = $locale->text("Buchungsnummer") . " = " . $form->{id}; 
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history 
   $form->redirect("Buchung gespeichert. Buchungsnummer = " . $form->{id});
   $lxdebug->leave_sub();
 
@@ -1720,6 +1763,12 @@ sub storno {
 
   $form->{id}     = 0;
   $form->{storno} = 1;
+  # saving the history
+  if(!exists $form->{addition} && $form->{id} ne "") {
+       $form->{addition} = "STORNO";
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history 
   &post;
   $lxdebug->leave_sub();