Nach dem Auslagern der Druckoptionen in eigene HTML-Vorlagen wurden nach dem Drucken...
[kivitendo-erp.git] / bin / mozilla / oe.pl
index 0d0d801..9820c3c 100644 (file)
@@ -110,6 +110,9 @@ sub add {
 
 sub edit {
   $lxdebug->enter_sub();
+  # show history button
+  $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
+  #/show hhistory button
 
   $form->{simple_save} = 0;
 
@@ -117,19 +120,19 @@ sub edit {
 
   # editing without stuff to edit? try adding it first
   if ($form->{rowcount}) {
-    map { $id++ if $form->{"id_$_"} } (1 .. $form->{rowcount});
+    map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
     if (!$id) {
 
       # reset rowcount
       undef $form->{rowcount};
       &add;
+      $lxdebug->leave_sub();
       return;
     }
-  } else {
-    if (!$form->{id}) {
-      &add;
-      return;
-    }
+  } elsif (!$form->{id}) {
+    &add;
+    $lxdebug->leave_sub();
+    return;
   }
 
   if ($form->{print_and_save}) {
@@ -164,6 +167,8 @@ sub order_links {
   # set jscalendar
   $form->{jscalendar} = $jscalendar;
 
+  my $editing = $form->{id};
+
   OE->retrieve(\%myconfig, \%$form);
 
   if ($form->{payment_id}) {
@@ -176,6 +181,8 @@ sub order_links {
     $taxzone_id = $form->{taxzone_id};
   }
 
+  $salesman_id = $form->{salesman_id} if ($editing);
+
 
   # if multiple rowcounts (== collective order) then check if the
   # there were more than one customer (in that case OE::retrieve removes
@@ -233,7 +240,7 @@ sub order_links {
   if ($taxzone_id) {
     $form->{taxzone_id} = $taxzone_id;
   }
-  $form->{intnotes} = $intnotes;
+  $form->{intnotes} = $intnotes if $intnotes;
   ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
   $form->{"old$form->{vc}"} =
     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
@@ -242,7 +249,8 @@ sub order_links {
   if (@{ $form->{"all_$form->{vc}"} }) {
     $form->{ $form->{vc} } =
       qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
-    map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" }
+    map { $form->{"select$form->{vc}"} .=
+"<option>$_->{name}--$_->{id}</option>\n" }
       (@{ $form->{"all_$form->{vc}"} });
   }
 
@@ -252,7 +260,7 @@ sub order_links {
   $form->{defaultcurrency} = $curr[0];
   $form->{currency}        = $form->{defaultcurrency} unless $form->{currency};
 
-  map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
+  map { $form->{selectcurrency} .= "<option>$_</option>\n" } @curr;
 
   $form->{taxincluded} = $taxincluded if ($form->{id});
 
@@ -263,7 +271,7 @@ sub order_links {
 
     map {
       $form->{selectdepartment} .=
-        "<option>$_->{description}--$_->{id}\n"
+        "<option>$_->{description}--$_->{id}</option>\n"
     } (@{ $form->{all_departments} });
   }
 
@@ -272,13 +280,15 @@ sub order_links {
   # sales staff
   if (@{ $form->{all_employees} }) {
     $form->{selectemployee} = "";
-    map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
+    map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}</option>\n" }
       (@{ $form->{all_employees} });
   }
 
   # forex
   $form->{forex} = $form->{exchangerate};
 
+  $form->{salesman_id} = $salesman_id if ($editing);
+
   $lxdebug->leave_sub();
 }
 
@@ -407,18 +417,30 @@ sub form_header {
   #quote select[customer|vendor] Bug 133
   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
 
-  $form->get_lists("contacts" => "ALL_CONTACTS",
-                   "shipto" => "ALL_SHIPTO");
+  #substitute \n and \r to \s (bug 543)
+  $form->{"select$form->{vc}"} =~ s/[\n\r]/&nbsp;/g;
+  
+  my @old_project_ids = ($form->{"globalproject_id"});
+  map({ push(@old_project_ids, $form->{"project_id_$_"})
+          if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
-  my (%labels, @values);
+  $form->get_lists("contacts" => "ALL_CONTACTS",
+                   "shipto" => "ALL_SHIPTO",
+                   "projects" => { "key" => "ALL_PROJECTS",
+                                   "all" => 0,
+                                   "old_id" => \@old_project_ids },
+                   "employees" => "ALL_SALESMEN");
+
+  my %labels;
+  my @values = (undef);
   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
     push(@values, $item->{"cp_id"});
     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
   }
   my $contact =
-    $cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
-                     '-labels' => \%labels, '-default' => $form->{"cp_id"});
+    NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
+                         '-labels' => \%labels, '-default' => $form->{"cp_id"}));
 
   %labels = ();
   @values = ("");
@@ -431,10 +453,40 @@ sub form_header {
   my $shipto = qq|
                <th align=right>| . $locale->text('Shipping Address') . qq|</th>
                <td>| .
-    $cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values,
-                     '-labels' => \%labels, '-default' => $form->{"shipto_id"})
+    NTI($cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values,
+                         '-labels' => \%labels, '-default' => $form->{"shipto_id"}))
     . qq|</td>|;
 
+  %labels = ();
+  @values = ("");
+  foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
+    push(@values, $item->{"id"});
+    $labels{$item->{"id"}} = $item->{"projectnumber"};
+  }
+  my $globalprojectnumber =
+    NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
+                         '-labels' => \%labels,
+                         '-default' => $form->{"globalproject_id"}));
+
+  $salesman = "";
+  if ($form->{type} =~ /^sales_/) {
+    %labels = ();
+    @values = ("");
+    foreach my $item (@{ $form->{ALL_SALESMEN} }) {
+      push(@values, $item->{id});
+      $labels{$item->{id}} = $item->{name} ne "" ? $item->{name} : $item->{login};
+    }
+
+    $salesman =
+      qq|<tr>
+          <th align="right">| . $locale->text('Salesman') . qq|</th>
+          <td>| .
+      NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{salesman_id},
+                           '-values' => \@values, '-labels' => \%labels))
+      . qq|</td>
+         </tr>|;
+  }
+
   $form->{exchangerate} =
     $form->format_amount(\%myconfig, $form->{exchangerate});
 
@@ -623,7 +675,10 @@ sub form_header {
 
   $vc =
     ($form->{"select$form->{vc}"})
-    ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="$form->{"select$form->{vc}"}">|
+    ? qq|<select name="$form->{vc}"
+onchange="document.getElementById('update_button').click();">| .
+    qq|$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="| .
+    Q($form->{"select$form->{vc}"}) . qq|">|
     : qq|<input name=$form->{vc} value="$form->{$form->{vc}}" size=35>|;
 
   $department = qq|
@@ -644,7 +699,7 @@ sub form_header {
       $employee = qq|
     <input type=hidden name=customer_klass value=$form->{customer_klass}>
              <tr>
-               <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
+               <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
                <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
                <input type=hidden name=selectemployee value="$form->{selectemployee}">
                 <td></td>
@@ -677,7 +732,9 @@ sub form_header {
   }
 
   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
-
+  # show history button js
+  $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
+  #/show history button js
   $form->header;
 
   print qq|
@@ -780,7 +837,12 @@ print qq|      </table>
            <table>
              $openclosed
              $employee
+        $salesman
              $ordnumber
+             <tr>
+          <th width="70%" align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
+          <td>$globalprojectnumber</td>
+             </tr>
            </table>
          </td>
        </tr>
@@ -866,7 +928,8 @@ sub form_footer {
 
         $tax .= qq|
              <tr>
-               <th align=right>$form->{"${item}_description"}</th>
+               <th align=right>$form->{"${item}_description"}&nbsp;|
+                                   . $form->{"${item}_rate"} * 100 .qq|%</th>
                <td align=right>$form->{"${item}_total"}</td>
              </tr>
 |;
@@ -905,7 +968,8 @@ sub form_footer {
 
         $tax .= qq|
              <tr>
-               <th align=right>Enthaltene $form->{"${item}_description"}</th>
+               <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
+                                   . $form->{"${item}_rate"} * 100 .qq|%</th>
                <td align=right>$form->{"${item}_total"}</td>
              </tr>
              <tr>
@@ -938,7 +1002,7 @@ sub form_footer {
                <td>$intnotes</td>
              </tr>
          <th align=right>| . $locale->text('Payment Terms') . qq|</th>
-         <td><select name=payment_id tabindex=24>$payment
+         <td><select name=payment_id>$payment
                           </select></td>
            </table>
          </td>
@@ -999,7 +1063,7 @@ sub form_footer {
   <tr>
     <td>
 |;
-  &print_options;
+  print_options();
 
   print qq|
     </td>
@@ -1023,6 +1087,12 @@ sub form_footer {
 
   if (($form->{id})) {
     print qq|
+       <input type="button" class="submit" onclick="set_history_window(|
+       . Q($form->{id})
+       . qq|);" name="history" id="history" value="|
+       . $locale->text('history')
+       . qq|">
+
 <br>| . $locale->text("Workflow $form->{type}") . qq|<br>
 <input class=submit type=submit name=action value="|
       . $locale->text('Save as new') . qq|">
@@ -1080,6 +1150,8 @@ sub form_footer {
 |;
   }
 
+  $form->hide_form("saved_xyznumber");
+
   print qq|
 
 <input type=hidden name=rowcount value=$form->{rowcount}>
@@ -1109,8 +1181,6 @@ sub update {
 
   &check_name($form->{vc});
 
-  &check_project;
-
   $buysell              = 'buy';
   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
   $form->{exchangerate} = $exchangerate
@@ -1262,7 +1332,7 @@ sub search {
     $form->{vc}    = 'customer';
     $ordlabel      = $locale->text('Order Number');
     $ordnumber     = 'ordnumber';
-    $employee      = $locale->text('Salesperson');
+    $employee      = $locale->text('Employee');
   }
 
   if ($form->{type} eq 'sales_quotation') {
@@ -1277,7 +1347,7 @@ sub search {
   $form->all_vc(\%myconfig, $form->{vc},
                 ($form->{vc} eq 'customer') ? "AR" : "AP");
 
-  map { $vc .= "<option>$_->{name}--$_->{id}\n" }
+  map { $vc .= "<option>$_->{name}--$_->{id}</option>\n" }
     @{ $form->{"all_$form->{vc}"} };
 
   $vclabel = ucfirst $form->{vc};
@@ -1288,7 +1358,7 @@ sub search {
 
   $vc =
     ($vc)
-    ? qq|<select name=$form->{vc}><option>\n$vc</select>|
+    ? qq|<select name=$form->{vc}><option>\n$vc</option></select>|
     : qq|<input name=$form->{vc} size=35>|;
 
   # departments
@@ -1297,7 +1367,7 @@ sub search {
 
     map {
       $form->{selectdepartment} .=
-        "<option>$_->{description}--$_->{id}\n"
+        "<option>$_->{description}--$_->{id}</option>\n"
     } (@{ $form->{all_departments} });
   }
 
@@ -1308,10 +1378,6 @@ sub search {
        </tr>
 | if $form->{selectdepartment};
 
-  $openclosed = qq|
-               <input type=hidden name="open" value=1>
-|;
-
   my $delivered;
   if (($form->{"type"} eq "sales_order") ||
       ($form->{"type"} eq "purchase_order")) {
@@ -1355,6 +1421,19 @@ sub search {
                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
   }
 
+  $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
+                                   "all" => 1 });
+
+  my %labels = ();
+  my @values = ("");
+  foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
+    push(@values, $item->{"id"});
+    $labels{$item->{"id"}} = $item->{"projectnumber"};
+  }
+  my $projectnumber =
+    NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
+                         '-labels' => \%labels));
+
   $form->header;
 
   print qq|
@@ -1372,13 +1451,17 @@ sub search {
       <table>
         <tr>
           <th align=right>$vclabel</th>
-          <td colspan=3>$vc</td>
+          <td colspan="3">$vc</td>
         </tr>
        $department
         <tr>
           <th align=right>$ordlabel</th>
           <td colspan=3><input name="$ordnumber" size=20></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
@@ -1390,7 +1473,12 @@ sub search {
           <th align=right>| . $locale->text('Include in Report') . qq|</th>
           <td colspan=5>
            <table>
-             $openclosed
+        <tr>
+          <td><input type="checkbox" name="open" value="1" id="open" checked>
+            <label for="open">| . $locale->text("Open") . qq|</td>
+          <td><input type="checkbox" name="closed" value="1" id="closed">
+            <label for="closed">| . $locale->text("Closed") . qq|</td>
+        </tr>
         $delivered
              <tr>
                <td><input name="l_id" class=checkbox type=checkbox value=Y>
@@ -1406,6 +1494,7 @@ sub search {
                <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
                <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
     . $locale->text('Ship via') . qq|</td>
+               <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
              </tr>
              <tr>
                <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
@@ -1414,6 +1503,8 @@ sub search {
     . $locale->text('Tax') . qq|</td>
                <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
     . $locale->text('Total') . qq|</td>
+          <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> |
+          . $locale->text('Project Number') . qq|</td>
              </tr>
              <tr>
                <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
@@ -1479,8 +1570,8 @@ sub orders {
   @columns =
     $form->sort_columns("transdate", "reqdate",   "id",      "$ordnumber",
                         "name",      "netamount", "tax",     "amount",
-                        "curr",      "employee",  "shipvia", "open",
-                        "closed",    "delivered");
+                        "curr",      "employee",  "shipvia", "globalprojectnumber",
+                        "open",      "closed",    "delivered");
 
   $form->{l_open} = $form->{l_closed} = "Y"
     if ($form->{open} && $form->{closed});
@@ -1520,11 +1611,10 @@ sub orders {
   if ($form->{vc} eq 'customer') {
     if ($form->{type} eq 'sales_order') {
       $form->{title} = $locale->text('Sales Orders');
-      $employee = $locale->text('Salesperson');
     } else {
       $form->{title} = $locale->text('Quotations');
-      $employee = $locale->text('Employee');
     }
+    $employee = $locale->text('Employee');
     $name = $locale->text('Customer');
   }
 
@@ -1564,6 +1654,8 @@ sub orders {
       qq|<th><a class=listheading href=$href&sort=shipvia>|
     . $locale->text('Ship via')
     . qq|</a></th>|;
+  $column_header{globalprojectnumber} =
+    qq|<th class="listheading">| . $locale->text('Project Number') . qq|</th>|;
   $column_header{open} =
     qq|<th class=listheading>| . $locale->text('O') . qq|</th>|;
   $column_header{closed} =
@@ -1669,7 +1761,7 @@ sub orders {
     $subtotalamount    += $oe->{amount};
 
     $column_data{ids} =
-      qq|<td><input name="id_$j" class=checkbox type=checkbox><input type="hidden" name="trans_id_$j" value="$oe->{id}"></td>|;
+      qq|<td><input name="multi_id_$j" class=checkbox type=checkbox><input type="hidden" name="trans_id_$j" value="$oe->{id}"></td>|;
     $column_data{id}        = "<td>$oe->{id}</td>";
     $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
     $column_data{reqdate}   = "<td>$oe->{reqdate}&nbsp;</td>";
@@ -1680,6 +1772,7 @@ sub orders {
 
     $column_data{employee} = "<td>$oe->{employee}&nbsp;</td>";
     $column_data{shipvia}  = "<td>$oe->{shipvia}&nbsp;</td>";
+    $column_data{globalprojectnumber}  = "<td>" . H($oe->{globalprojectnumber}) . "</td>";
 
     if ($oe->{closed}) {
       $column_data{closed} = "<td align=center>X</td>";
@@ -1879,10 +1972,17 @@ sub save_and_close {
 
   relink_accounts();
 
-  $form->redirect(
-            $form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
-    if (OE->save(\%myconfig, \%$form));
-  $form->error($err);
+  $form->error($err) if (!OE->save(\%myconfig, \%$form));
+
+  # saving the history
+  if(!exists $form->{addition}) {
+       $form->{addition} = "SAVED";
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history
+
+  $form->redirect($form->{label} . " $form->{$ordnumber} " .
+                  $locale->text('saved!'));
 
   $lxdebug->leave_sub();
 }
@@ -1954,6 +2054,14 @@ sub save {
   relink_accounts();
 
   OE->save(\%myconfig, \%$form);
+
+  # saving the history
+  if(!exists $form->{addition}) {
+       $form->{addition} = "SAVED";
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history 
+
   $form->{simple_save} = 1;
   if(!$form->{print_and_save}) {
     set_headings("edit");
@@ -2016,8 +2124,15 @@ sub yes {
     $msg = $locale->text('Quotation deleted!');
     $err = $locale->text('Cannot delete quotation!');
   }
-
-  $form->redirect($msg) if (OE->delete(\%myconfig, \%$form, $spool));
+  if (OE->delete(\%myconfig, \%$form, $spool)){
+    $form->redirect($msg);
+    # saving the history
+    if(!exists $form->{addition}) {
+         $form->{addition} = "DELETED";
+         $form->save_history($form->dbconnect(\%myconfig));
+    }
+    # /saving the history 
+  }
   $form->error($err);
 
   $lxdebug->leave_sub();
@@ -2031,11 +2146,9 @@ sub invoice {
     # these checks only apply if the items don't bring their own ordnumbers/transdates.
     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
-      if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
-          ->{''});
+      if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
     $form->isblank("transdate", $locale->text('Order Date missing!'))
-      if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
-          ->{''});
+      if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
 
     # also copy deliverydate from the order
     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
@@ -2340,7 +2453,15 @@ sub save_as_new {
 
   $form->{saveasnew} = 1;
   $form->{closed}    = 0;
-  map { delete $form->{$_} } qw(printed emailed queued ordnumber quonumber);
+  map { delete $form->{$_} } qw(printed emailed queued);
+
+  # Let Lx-Office assign a new order number if the user hasn't changed the
+  # previous one. If it has been changed manually then use it as-is.
+  my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
+  if ($form->{saved_xyznumber} &&
+      ($form->{saved_xyznumber} eq $form->{$idx})) {
+    delete($form->{$idx});
+  }
 
   &save;
 
@@ -2355,6 +2476,10 @@ sub purchase_order {
     OE->close_order(\%myconfig, \%$form);
   }
 
+  if ($form->{type} =~ /^sales_/) {
+    delete($form->{ordnumber});
+  }
+
   $form->{cp_id} *= 1;
 
   $form->{title} = $locale->text('Add Purchase Order');
@@ -2374,6 +2499,10 @@ sub sales_order {
     OE->close_order(\%myconfig, $form);
   }
 
+  if ($form->{type} eq "purchase_order") {
+    delete($form->{ordnumber});
+  }
+
   $form->{cp_id} *= 1;
 
   $form->{title} = $locale->text('Add Sales Order');
@@ -2422,3 +2551,26 @@ sub poso {
   $lxdebug->leave_sub();
 }
 
+sub e_mail {
+  $lxdebug->enter_sub();
+
+  $form->{print_and_save} = 1;
+
+  if (!$form->{id}) {
+    $print_post = 1;
+
+    my $saved_form = save_form();
+
+    save();
+
+    my %saved_vars;
+    map({ $saved_vars{$_} = $form->{$_}; } qw(id ordnumber quonumber));
+    restore_form($saved_form);
+    map({ $form->{$_} = $saved_vars{$_}; } qw(id ordnumber quonumber));
+  }
+
+  edit_e_mail();
+
+  $lxdebug->leave_sub();
+}
+