Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / bin / mozilla / oe.pl
index b9abb0f..904a4b9 100644 (file)
@@ -375,7 +375,7 @@ sub form_header {
       @old_ids_cond,
     ]);
 
-  $TMPL_VAR{ALL_PROJECTS}          = SL::DB::Manager::Project->get_all(query => \@conditions);
+  $TMPL_VAR{ALL_PROJECTS}          = SL::DB::Manager::Project->get_all_sorted(query => \@conditions);
 
   # label subs
   my $employee_list_query_gen      = sub { $::form->{$_[0]} ? [ or => [ id => $::form->{$_[0]}, deleted => 0 ] ] : [ deleted => 0 ] };
@@ -465,7 +465,7 @@ sub form_header {
     }
   }
 
-  $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase show_form_details show_history show_vc_details ckeditor/ckeditor ckeditor/adapters/jquery));
+  $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase show_form_details show_history show_vc_details ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part));
 
   $form->header;
   if ($form->{CFDD_shipto} && $form->{CFDD_shipto_id} ) {
@@ -594,8 +594,6 @@ sub update {
 
   check_oe_access();
 
-  my $order = _make_record();
-
   set_headings($form->{"id"} ? "edit" : "add");
 
   $form->{update} = 1;
@@ -680,7 +678,20 @@ sub update {
         if ($sellprice) {
           $form->{"sellprice_$i"} = $sellprice;
         } else {
-          $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
+          my $record        = _make_record();
+          my $price_source  = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
+          my $best_price    = $price_source->best_price;
+          my $best_discount = $price_source->best_discount;
+
+          if ($best_price) {
+            $::form->{"sellprice_$i"}           = $best_price->price;
+            $::form->{"active_price_source_$i"} = $best_price->source;
+          }
+          if ($best_discount) {
+            $::form->{"discount_$i"}               = $best_discount->discount;
+            $::form->{"active_discount_source_$i"} = $best_discount->source;
+          }
+
           $form->{"sellprice_$i"} /= $exchangerate;   # if there is an exchange rate adjust sellprice
         }
 
@@ -1461,6 +1472,7 @@ sub invoice {
 
   # bo creates the id, reset it
   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
+  delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
   $form->{ $form->{vc} } =~ s/--.*//g;
   $form->{type} = "invoice";
 
@@ -1880,6 +1892,7 @@ sub delivery_order {
 
   # reset
   delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
+  delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
 
   for my $i (1 .. $form->{rowcount}) {
     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice lastcost basefactor discount);
@@ -2111,28 +2124,3 @@ sub dispatcher {
   $::form->error($::locale->text('No action defined.'));
 }
 
-sub _make_record {
-  my $obj = SL::DB::Order->new;
-
-  for my $method (keys %$::form) {
-    next unless $obj->can($method);
-    next unless $obj->meta->column($method);
-
-    if ($obj->meta->column($method)->isa('Rose::DB::Object::Metadata::Column::Date')) {
-      $obj->${\"$method\_as_date"}($::form->{$method});
-    } elsif ((ref $obj->meta->column($method)) =~ /^Rose::DB::Object::Metadata::Column::(?:Integer|Numeric|Float|DoublePrecsion)$/) {
-      $obj->$method($::form->{$method});
-    }
-  }
-
-  my @items;
-  for my $i (1 .. $::form->{rowcount}) {
-    next unless $::form->{"id_$i"};
-    push @items, _make_record_item($i)
-  }
-
-  $obj->orderitems(@items);
-
-  return $obj;
-}
-