Bei Eingangslieferscheinen wurden Nachkommastellen beim Lagereingang abgeschnitten...
[kivitendo-erp.git] / SL / OE.pm
index 4f84b50..72c11ed 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -72,10 +72,11 @@ sub transactions {
     qq|  o.marge_total, o.marge_percent, | .
     qq|  ex.$rate AS exchangerate, | .
     qq|  pr.projectnumber AS globalprojectnumber, | .
-    qq|  e.name AS employee | .
+    qq|  e.name AS employee, s.name AS salesman | .
     qq|FROM oe o | .
     qq|JOIN $vc ct ON (o.${vc}_id = ct.id) | .
     qq|LEFT JOIN employee e ON (o.employee_id = e.id) | .
+    qq|LEFT JOIN employee s ON (o.salesman_id = s.id) | .
     qq|LEFT JOIN exchangerate ex ON (ex.curr = o.curr | .
     qq|  AND ex.transdate = o.transdate) | .
     qq|LEFT JOIN project pr ON (o.globalproject_id = pr.id) | .
@@ -109,6 +110,11 @@ sub transactions {
     $query .= " AND o.employee_id = ?";
     push @values, conv_i($form->{employee_id});
   }
+  
+  if ($form->{salesman_id}) {
+    $query .= " AND o.salesman_id = ?";
+    push @values, conv_i($form->{salesman_id});
+  }
 
   if (!$form->{open} && !$form->{closed}) {
     $query .= " AND o.id = 0";
@@ -143,16 +149,18 @@ sub transactions {
   }
 
   my $sortorder = join(', ', ("o.id", $form->sort_columns("transdate", $ordnumber, "name")));
-  my %allowed_sort_columns =
-    ("transdate" => "o.transdate",
-     "reqdate" => "o.reqdate",
-     "id" => "o.id",
-     "ordnumber" => "o.ordnumber",
-     "quonumber" => "o.quonumber",
-     "name" => "ct.name",
-     "employee" => "e.name",
-     "shipvia" => "o.shipvia",
-     "transaction_description" => "o.transaction_description");
+  my %allowed_sort_columns = (
+    "transdate"               => "o.transdate",
+    "reqdate"                 => "o.reqdate",
+    "id"                      => "o.id",
+    "ordnumber"               => "o.ordnumber",
+    "quonumber"               => "o.quonumber",
+    "name"                    => "ct.name",
+    "employee"                => "e.name",
+    "salesman"                => "e.name",
+    "shipvia"                 => "o.shipvia",
+    "transaction_description" => "o.transaction_description"
+  );
   if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
     $sortorder = $allowed_sort_columns{$form->{sort}};
   }
@@ -176,6 +184,41 @@ sub transactions {
   $main::lxdebug->leave_sub();
 }
 
+sub transactions_for_todo_list {
+  $main::lxdebug->enter_sub();
+
+  my $self     = shift;
+  my %params   = @_;
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+  my $query    = qq|SELECT id FROM employee WHERE login = ?|;
+  my ($e_id)   = selectrow_query($form, $dbh, $query, $form->{login});
+
+  $query       =
+    qq|SELECT oe.id, oe.transdate, oe.reqdate, oe.quonumber, oe.transaction_description, oe.amount,
+         c.name AS customer,
+         e.name AS employee
+       FROM oe
+       LEFT JOIN customer c ON (oe.customer_id = c.id)
+       LEFT JOIN employee e ON (oe.employee_id = e.id)
+       WHERE (COALESCE(quotation, FALSE) = TRUE)
+         AND (COALESCE(closed,    FALSE) = FALSE)
+         AND ((oe.employee_id = ?) OR (oe.salesman_id = ?))
+         AND NOT (oe.reqdate ISNULL)
+         AND (oe.reqdate < current_date)
+       ORDER BY transdate|;
+
+  my $quotations = selectall_hashref_query($form, $dbh, $query, $e_id, $e_id);
+
+  $main::lxdebug->leave_sub();
+
+  return $quotations;
+}
+
 sub save {
   $main::lxdebug->enter_sub();
 
@@ -199,8 +242,6 @@ sub save {
 
   if ($form->{id}) {
 
-    &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
-
     $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
     do_query($form, $dbh, $query, $form->{id});
 
@@ -236,8 +277,7 @@ sub save {
 
   for my $i (1 .. $form->{rowcount}) {
 
-    map({ $form->{"${_}_$i"} =
-            $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(qty ship));
+    map({ $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(qty ship));
 
     if ($form->{"id_$i"}) {
 
@@ -248,33 +288,26 @@ sub save {
       my $basefactor = 1;
       if (defined($all_units->{$item_unit}->{factor}) &&
           (($all_units->{$item_unit}->{factor} * 1) != 0)) {
-        $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} /
-          $all_units->{$item_unit}->{factor};
+        $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
       }
       my $baseqty = $form->{"qty_$i"} * $basefactor;
 
       $form->{"marge_percent_$i"} = $form->parse_amount($myconfig, $form->{"marge_percent_$i"}) * 1;
-      $form->{"marge_absolut_$i"} = $form->parse_amount($myconfig, $form->{"marge_absolut_$i"}) * 1;
+      $form->{"marge_total_$i"} = $form->parse_amount($myconfig, $form->{"marge_total_$i"}) * 1;
       $form->{"lastcost_$i"} = $form->{"lastcost_$i"} * 1;
 
       # set values to 0 if nothing entered
-      $form->{"discount_$i"} =
-        $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
+      $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
 
-      $form->{"sellprice_$i"} =
-        $form->parse_amount($myconfig, $form->{"sellprice_$i"});
+      $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
       $fxsellprice = $form->{"sellprice_$i"};
 
       my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
       $dec = length($dec);
       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
-      $discount =
-        $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
-                            $decimalplaces);
-      $form->{"sellprice_$i"} =
-        $form->round_amount($form->{"sellprice_$i"} - $discount,
-                            $decimalplaces);
+      $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}, $decimalplaces);
+      $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
 
       $form->{"inventory_accno_$i"} *= 1;
       $form->{"expense_accno_$i"}   *= 1;
@@ -303,15 +336,10 @@ sub save {
       if ($form->round_amount($taxrate, 7) == 0) {
         if ($form->{taxincluded}) {
           foreach $item (@taxaccounts) {
-            $taxamount =
-              $form->round_amount($linetotal * $form->{"${item}_rate"} /
-                                    (1 + abs($form->{"${item}_rate"})),
-                                  2);
-
+            $taxamount = $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
             $taxaccounts{$item} += $taxamount;
             $taxdiff            += $taxamount;
-
-            $taxbase{$item} += $taxbase;
+            $taxbase{$item}     += $taxbase;
           }
           $taxaccounts{ $taxaccounts[0] } += $taxdiff;
         } else {
@@ -322,16 +350,14 @@ sub save {
         }
       } else {
         foreach $item (@taxaccounts) {
-          $taxaccounts{$item} +=
-            $taxamount * $form->{"${item}_rate"} / $taxrate;
+          $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
           $taxbase{$item} += $taxbase;
         }
       }
 
       $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor;
 
-      $reqdate =
-        ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
+      $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
 
       # get pricegroup_id and save ist
       ($null, my $pricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
@@ -354,7 +380,7 @@ sub save {
       }
       $query .= qq|?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
                    (SELECT factor FROM price_factors WHERE id = ?), ?)|;
-                 push(@values,
+      push(@values,
            conv_i($form->{id}), conv_i($form->{"id_$i"}),
            $form->{"description_$i"}, $form->{"longdescription_$i"},
            $form->{"qty_$i"}, $baseqty,
@@ -363,7 +389,7 @@ sub save {
            $form->{"serialnumber_$i"}, $form->{"ship_$i"}, conv_i($pricegroup_id),
            $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
            $form->{"cusordnumber_$i"}, $form->{"subtotal_$i"} ? 't' : 'f',
-           $form->{"marge_percent_$i"}, $form->{"marge_absolut_$i"},
+           $form->{"marge_percent_$i"}, $form->{"marge_total_$i"},
            $form->{"lastcost_$i"},
            conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
            conv_i($form->{"marge_price_factor_$i"}));
@@ -386,21 +412,14 @@ sub save {
   if ($form->{currency} eq $form->{defaultcurrency}) {
     $form->{exchangerate} = 1;
   } else {
-    $exchangerate =
-      $form->check_exchangerate($myconfig,
-                                $form->{currency},
-                                $form->{transdate},
-                                ($form->{vc} eq 'customer') ? 'buy' : 'sell');
+    $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, ($form->{vc} eq 'customer') ? 'buy' : 'sell');
   }
 
-  $form->{exchangerate} =
-    ($exchangerate)
-    ? $exchangerate
-    : $form->parse_amount($myconfig, $form->{exchangerate});
+  $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
 
   my $quotation = $form->{type} =~ /_order$/ ? 'f' : 't';
 
-  ($null, $form->{department_id}) = split(/--/, $form->{department});
+  ($null, $form->{department_id}) = split(/--/, $form->{department}) if $form->{department};
 
   # save OE record
   $query =
@@ -437,7 +456,7 @@ sub save {
 
   # add shipto
   $form->{name} = $form->{ $form->{vc} };
-  $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
+  $form->{name} =~ s/--\Q$form->{"$form->{vc}_id"}\E//;
 
   if (!$form->{shipto_id}) {
     $form->add_shipto($dbh, $form->{id}, "OE");
@@ -448,29 +467,21 @@ sub save {
 
   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
     if ($form->{vc} eq 'customer') {
-      $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
-                                 $form->{exchangerate}, 0);
+      $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
     }
     if ($form->{vc} eq 'vendor') {
-      $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
-                                 0, $form->{exchangerate});
+      $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
     }
   }
 
-  if ($form->{type} =~ /_order$/) {
-
-    # adjust onhand
-    &adj_onhand($dbh, $form, $ml * -1);
-  }
-
-  my $rc = $dbh->commit;
-  $dbh->disconnect;
-
   $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
                                        "quonumber" : "ordnumber"};
 
   Common::webdav_folder($form) if ($main::webdav);
 
+  my $rc = $dbh->commit;
+  $dbh->disconnect;
+
   $main::lxdebug->leave_sub();
 
   return $rc;
@@ -504,7 +515,7 @@ sub close_order {
 
   my ($self, $myconfig, $form) = @_;
 
-  $main::lxdebug->leave_sub() unless ($form->{"id"});
+  return $main::lxdebug->leave_sub() unless ($form->{"id"});
 
   my $dbh = $form->dbconnect($myconfig);
   do_query($form, $dbh, qq|UPDATE oe SET closed = TRUE where id = ?|,
@@ -537,25 +548,9 @@ sub delete {
   }
   $sth->finish;
 
-  $query = qq|SELECT o.parts_id, o.ship FROM orderitems o | .
-           qq|WHERE o.trans_id = ?|;
-  @values = (conv_i($form->{id}));
-  $sth = $dbh->prepare($query);
-  $sth->execute(@values) || $self->dberror($query);
-
-  while (my ($id, $ship) = $sth->fetchrow_array) {
-    $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
-  }
-  $sth->finish;
-
   # delete-values
   @values = (conv_i($form->{id}));
 
-  # delete inventory
-  $query = qq|DELETE FROM inventory | .
-           qq|WHERE oe_id = ?|;
-  do_query($form, $dbh, $query, @values);
-
   # delete status entries
   $query = qq|DELETE FROM status | .
            qq|WHERE trans_id = ?|;
@@ -625,7 +620,7 @@ sub retrieve {
   my $ref = selectfirst_hashref_query($form, $dbh, $query);
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
-  ($form->{currency}) = split(/:/, $form->{currencies});
+  ($form->{currency}) = split(/:/, $form->{currencies}) unless ($form->{currency});
 
   # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
   # we come from invoices, feel free.
@@ -817,7 +812,7 @@ sub retrieve {
           $ptr->{accno} = $i;
         }
         $ref->{taxaccounts} .= "$ptr->{accno} ";
-        if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
+        if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) {
           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
           $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
@@ -910,8 +905,7 @@ sub order_details {
     $sth->finish();
   }
 
-  $form->{"globalprojectnumber"} =
-    $projectnumbers{$form->{"globalproject_id"}};
+  $form->{"globalprojectnumber"} = $projectnumbers{$form->{"globalproject_id"}};
 
   my @arrays =
     qw(runningnumber number description longdescription qty ship unit bin
@@ -1033,9 +1027,8 @@ sub order_details {
 
       if ($taxamount != 0) {
         foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
-          $taxaccounts{$item} +=
-            $taxamount * $form->{"${item}_rate"} / $taxrate;
-          $taxbase{$item} += $taxbase;
+          $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
+          $taxbase{$item}     += $taxbase;
         }
       }
 
@@ -1048,8 +1041,7 @@ sub order_details {
         # get parts and push them onto the stack
         my $sortorder = "";
         if ($form->{groupitems}) {
-          $sortorder =
-            qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
+          $sortorder = qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
         } else {
           $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
         }
@@ -1067,19 +1059,13 @@ sub order_details {
 
         while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
-            map({ push(@{ $form->{$_} }, "") }
-                grep({ $_ ne "description" } @arrays));
+            map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
             push(@{ $form->{description} }, $sameitem);
           }
 
-          push(@{ $form->{description} },
-               $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
-                 )
-                 . qq|, $ref->{partnumber}, $ref->{description}|);
-
-          map({ push(@{ $form->{$_} }, "") }
-              grep({ $_ ne "description" } @arrays));
+          push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|); 
+          map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
         }
         $sth->finish;
       }
@@ -1143,67 +1129,4 @@ sub project_description {
   return $value;
 }
 
-sub adj_onhand {
-  $main::lxdebug->enter_sub();
-
-  my ($dbh, $form, $ml) = @_;
-
-  my $all_units = $form->{all_units};
-
-  my $query =
-    qq|SELECT oi.parts_id, oi.ship, oi.unit, p.inventory_accno_id, p.assembly | .
-    qq|   FROM orderitems oi | .
-    qq|   JOIN parts p ON (p.id = oi.parts_id) | .
-    qq|   WHERE oi.trans_id = ?|;
-  my @values = ($form->{id});
-  my $sth = $dbh->prepare($query);
-  $sth->execute(@values) || $form->dberror($query);
-
-  $query =
-    qq|SELECT sum(p.inventory_accno_id) | .
-    qq|FROM parts p | .
-    qq|JOIN assembly a ON (a.parts_id = p.id) | .
-    qq|WHERE a.id = ?|;
-  my $ath = $dbh->prepare($query) || $form->dberror($query);
-
-  my $ispa;
-
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-    if ($ref->{inventory_accno_id} || $ref->{assembly}) {
-
-      # do not update if assembly consists of all services
-      if ($ref->{assembly}) {
-        $ath->execute($ref->{parts_id}) || $form->dberror($query);
-
-        ($ispa) = $sth->fetchrow_array;
-        $ath->finish;
-
-        next unless $ispa;
-
-      }
-
-      # get item baseunit
-      $query = qq|SELECT unit FROM parts WHERE id = ?|;
-      my ($item_unit) = selectrow_query($form, $dbh, $query, $ref->{parts_id});
-
-      my $basefactor = 1;
-      if (defined($all_units->{$item_unit}->{factor}) &&
-          (($all_units->{$item_unit}->{factor} * 1) != 0)) {
-        $basefactor = $all_units->{$ref->{unit}}->{factor} /
-          $all_units->{$item_unit}->{factor};
-      }
-      my $baseqty = $ref->{ship} * $basefactor;
-
-      # adjust onhand in parts table
-      $form->update_balance($dbh, "parts", "onhand",
-                            qq|id = $ref->{parts_id}|,
-                            $baseqty * $ml);
-    }
-  }
-
-  $sth->finish;
-
-  $main::lxdebug->leave_sub();
-}
-
 1;