Nicht-editierbare CVars bei "als neu speichern/verwenden" richtig setzen.
[kivitendo-erp.git] / bin / mozilla / oe.pl
index ad5406c..8a14425 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} ) {
@@ -678,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
         }
 
@@ -692,12 +705,6 @@ sub update {
         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
         $form->{"lastcost_$i"}  = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
         $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
-
-        # get pricegroups for parts
-        IS->get_pricegroups_for_parts(\%myconfig, \%$form);
-
-        # build up html code for prices_$i
-        &set_pricegroup($i);
       }
 
       display_form();
@@ -1416,6 +1423,7 @@ sub invoice {
     for (qw(ship qty sellprice listprice basefactor)) {
       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
     }
+    $form->{"converted_from_orderitems_id_$i"} = delete $form->{"orderitems_id_$i"};
   }
 
   my ($buysell, $orddate, $exchangerate);
@@ -1510,10 +1518,6 @@ sub invoice {
       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
   }
 
-  #  show pricegroup in newly loaded invoice when creating invoice from quotation/order
-  IS->get_pricegroups_for_parts(\%myconfig, \%$form);
-  set_pricegroup($_) for 1 .. $form->{rowcount};
-
   &display_form;
 
   $main::lxdebug->leave_sub();
@@ -1617,7 +1621,7 @@ sub save_as_new {
 
   $form->{saveasnew} = 1;
   map { delete $form->{$_} } qw(printed emailed queued delivered closed);
-  delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
+  $form->{"converted_from_orderitems_id_$_"} = delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
 
   # Let kivitendo 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.
@@ -1633,25 +1637,14 @@ sub save_as_new {
   if ( $form->{reqdate} && $form->{id} ) {
     my $saved_order = OE->retrieve_simple(id => $form->{id});
     if ( $saved_order && $saved_order->{reqdate} eq $form->{reqdate} && $saved_order->{transdate} eq $form->{transdate} ) {
+      my $extra_days   = $form->{type} eq 'sales_quotation' ? $::instance_conf->get_reqdate_interval : 1;
+      my $next_workday = DateTime->today_local->add(days => $extra_days);
+      my $day_of_week  = $next_workday->day_of_week;
 
-      my $dbh = $form->get_standard_dbh;
+      $next_workday->add(days => (8 - $day_of_week)) if $day_of_week >= 6;
 
-      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'       );
-
-      my $query = 'SELECT
-                     date(current_date + interval \''. $next_workday .' days\') AS reqdate,
-                     date(current_date) AS transdate';
-      my $ref = selectfirst_hashref_query($form, $dbh, $query);
-
-      map(
-        { $form->{$_} = $ref->{$_} }
-        keys %{$ref}
-      );
+      $form->{transdate} = DateTime->today_local->to_kivitendo;
+      $form->{reqdate}   = $next_workday->to_kivitendo;
     }
   }
 
@@ -1816,7 +1809,8 @@ sub poso {
 
   # reset
   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered ordnumber);
-  delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};  # always reset orderitems_id
+  # this converted variable is also used for sales_order to purchase order and vice versa
+  $form->{"converted_from_orderitems_id_$_"} = delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
 
   # if purchase_order was generated from sales_order, use  lastcost_$i as sellprice_$i
   # also reset discounts
@@ -1891,6 +1885,7 @@ sub delivery_order {
 
   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);
+    $form->{"converted_from_orderitems_id_$i"} = delete $form->{"orderitems_id_$i"};
   }
 
   my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor shipto_id);
@@ -2118,3 +2113,4 @@ sub dispatcher {
 
   $::form->error($::locale->text('No action defined.'));
 }
+