PriceSource: credit_notes auch im Popup unterstützen
[kivitendo-erp.git] / SL / OE.pm
index 52a5a61..506db6b 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -118,6 +118,7 @@ sub transactions {
     qq|  , o.order_probability, o.expected_billing_date, (o.netamount * o.order_probability / 100) AS expected_netamount | .
     qq|FROM oe o | .
     qq|JOIN $vc ct ON (o.${vc}_id = ct.id) | .
+    qq|LEFT JOIN contacts cp ON (o.cp_id = cp.cp_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.currency_id = o.currency_id | .
@@ -168,6 +169,11 @@ SQL
     push(@values, '%' . $form->{$vc} . '%');
   }
 
+  if ($form->{"cp_name"}) {
+    $query .= " AND (cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)";
+    push(@values, ('%' . $form->{"cp_name"} . '%')x2);
+  }
+
   if (!$main::auth->assert('sales_all_edit', 1)) {
     $query .= " AND o.employee_id = (select id from employee where login= ?)";
     push @values, $form->{login};
@@ -381,9 +387,6 @@ sub save {
                   AND (trans_id IN (SELECT id FROM orderitems WHERE trans_id = ?))|;
     do_query($form, $dbh, $query, $form->{id});
 
-    $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
-    do_query($form, $dbh, $query, $form->{id});
-
     $query = qq|DELETE FROM shipto | .
              qq|WHERE trans_id = ? AND module = 'OE'|;
     do_query($form, $dbh, $query, $form->{id});
@@ -411,6 +414,7 @@ sub save {
   my @taxaccounts;
   my %taxaccounts;
   my $netamount = 0;
+  my @processed_orderitems;
 
   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
@@ -512,19 +516,28 @@ sub save {
       $pricegroup_id  = undef if !$pricegroup_id;
 
       # save detail record in orderitems table
+      if (! $form->{"orderitems_id_$i"}) {
+        $query = qq|SELECT nextval('orderitemsid')|;
+        ($form->{"orderitems_id_$i"}) = selectrow_query($form, $dbh, $query);
+
+        $query = qq|INSERT INTO orderitems (id) VALUES (?)|;
+        do_query($form, $dbh, $query, $form->{"orderitems_id_$i"});
+      }
       my $orderitems_id = $form->{"orderitems_id_$i"};
-      ($orderitems_id)  = selectfirst_array_query($form, $dbh, qq|SELECT nextval('orderitemsid')|) if (!$orderitems_id);
-
-      @values = ();
-      $query = qq|INSERT INTO orderitems (
-                    id, trans_id, parts_id, description, longdescription, qty, base_qty,
-                    sellprice, discount, unit, reqdate, project_id, serialnumber, ship,
-                    pricegroup_id, ordnumber, transdate, cusordnumber, subtotal,
-                    marge_percent, marge_total, lastcost, price_factor_id, price_factor, marge_price_factor)
-                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
-                          (SELECT factor FROM price_factors WHERE id = ?), ?)|;
-      push(@values,
-           conv_i($orderitems_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
+      push @processed_orderitems, $orderitems_id;
+
+       $query = <<SQL;
+         UPDATE orderitems SET
+          trans_id = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
+          sellprice = ?, discount = ?, unit = ?, reqdate = ?, project_id = ?, serialnumber = ?, ship = ?,
+          pricegroup_id = ?, ordnumber = ?, transdate = ?, cusordnumber = ?, subtotal = ?,
+          marge_percent = ?, marge_total = ?, lastcost = ?, price_factor_id = ?,
+          active_price_source = ?,
+          price_factor = (SELECT factor FROM price_factors WHERE id = ?), marge_price_factor = ?
+        WHERE id = ?
+SQL
+      @values = (
+           conv_i($form->{id}), conv_i($form->{"id_$i"}),
            $form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}),
            $form->{"qty_$i"}, $baseqty,
            $fxsellprice, $form->{"discount_$i"},
@@ -534,8 +547,12 @@ sub save {
            $form->{"cusordnumber_$i"}, $form->{"subtotal_$i"} ? 't' : 'f',
            $form->{"marge_percent_$i"}, $form->{"marge_absolut_$i"},
            $form->{"lastcost_$i"},
+           $form->{"active_price_source_$i"},
            conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
-           conv_i($form->{"marge_price_factor_$i"}));
+           conv_i($form->{"marge_price_factor_$i"}),
+           conv_i($orderitems_id),
+      );
+
       do_query($form, $dbh, $query, @values);
 
       $form->{"sellprice_$i"} = $fxsellprice;
@@ -551,6 +568,16 @@ sub save {
                                   dbh          => $dbh);
     }
   }
+  # search for orphaned ids
+  $query  = sprintf 'SELECT id FROM orderitems WHERE trans_id = ? AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_orderitems;
+  @values = (conv_i($form->{id}), map { conv_i($_) } @processed_orderitems);
+  my @orphaned_ids = map { $_->{id} } selectall_hashref_query($form, $dbh, $query, @values);
+
+  if (scalar @orphaned_ids) {
+    # clean up orderitems
+    $query  = sprintf 'DELETE FROM orderitems WHERE id IN (%s)', join ', ', ("?") x scalar @orphaned_ids;
+    do_query($form, $dbh, $query, @orphaned_ids);
+  }
 
   $reqdate = ($form->{reqdate}) ? $form->{reqdate} : undef;
 
@@ -795,6 +822,11 @@ sub retrieve {
   if (!$form->{id}) {
     my $wday         = (localtime(time))[6];
     my $next_workday = $wday == 5 ? 3 : $wday == 6 ? 2 : 1;
+
+    # if we have a client configured interval for sales quotation, we add this
+    $next_workday   += $::instance_conf->get_reqdate_interval if ($::instance_conf->get_reqdate_interval &&
+                                                                    $form->{type} eq 'sales_quotation' );
+
     $query_add       = qq|, current_date AS transdate, date(current_date + interval '${next_workday} days') AS reqdate|;
   }
 
@@ -915,7 +947,7 @@ sub retrieve {
            o.sellprice, o.parts_id AS id, o.unit, o.discount, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id,
            o.reqdate, o.project_id, o.serialnumber, o.ship, o.lastcost,
            o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription,
-           o.price_factor_id, o.price_factor, o.marge_price_factor,
+           o.price_factor_id, o.price_factor, o.marge_price_factor, o.active_price_source,
            pr.projectnumber, p.formel,
            pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
          FROM orderitems o
@@ -990,7 +1022,7 @@ sub retrieve {
       }
 
       # delete orderitems_id in collective orders, so that they get cloned no matter what
-      delete $ref->{orderitems_id} if (@ids);
+      delete $ref->{orderitems_id} if $is_collective_order;
 
       # get tax rates and description
       my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};