Auftrags-Controller: Wechselkurs pro Beleg …
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Fri, 24 Jul 2020 09:25:42 +0000 (11:25 +0200)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Mon, 27 Jul 2020 13:31:52 +0000 (15:31 +0200)
- Wechselkurs wird pro Beleg gespeichert
- Wechselkurs ist immer änderbar
- vorausgefüllt aus "alter" Tages-Wechselkurstabelle

Bezieht sich auch auf #135
Refs #135

SL/Controller/Order.pm
SL/DB/Helper/FlattenToForm.pm
SL/DB/Order.pm
bin/mozilla/io.pl
js/kivi.Order.js
templates/webpages/order/tabs/basic_data.html

index caa3f91..5d30492 100644 (file)
@@ -934,7 +934,7 @@ sub action_update_exchangerate {
   my $data = {
     is_standard   => $self->order->currency_id == $::instance_conf->get_currency_id,
     currency_name => $self->order->currency->name,
-    exchangerate  => $self->order->exchangerate_as_null_number,
+    exchangerate  => $self->order->daily_exchangerate_as_null_number,
   };
 
   $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
@@ -1375,7 +1375,6 @@ sub make_order {
 
   my $form_orderitems                  = delete $::form->{order}->{orderitems};
   my $form_periodic_invoices_config    = delete $::form->{order}->{periodic_invoices_config};
-  my $form_exchangerate_as_null_number = delete $::form->{order}->{exchangerate_as_null_number};
 
   $order->assign_attributes(%{$::form->{order}});
 
@@ -1384,9 +1383,6 @@ sub make_order {
     $periodic_invoices_config->assign_attributes(%$periodic_invoices_config_attrs);
   }
 
-  # set exchangerate after transdate and currency_id
-  $order->assign_attributes(exchangerate_as_null_number => $form_exchangerate_as_null_number) if $order->currency_id;
-
   # remove deleted items
   $self->item_ids_to_delete([]);
   foreach my $idx (reverse 0..$#{$order->orderitems}) {
index a719894..9523c35 100644 (file)
@@ -42,7 +42,7 @@ sub flatten_to_form {
                             ($vc eq 'customer')? 'c_vendor_id': 'v_customer_id');
   my @vc_prefixed_fields = qw(email fax notes number phone);
 
-  _copy($self,                          $form, '',              '', 1, qw(amount netamount marge_total marge_percent container_remaining_weight container_remaining_volume paid));
+  _copy($self,                          $form, '',              '', 1, qw(amount netamount marge_total marge_percent container_remaining_weight container_remaining_volume paid exchangerate));
   _copy($self->$vc,                     $form, '',              '', 0, @vc_fields);
   _copy($self->$vc,                     $form, $vc,             '', 0, @vc_prefixed_fields);
   _copy($self->contact,                 $form, '',              '', 0, grep { /^cp_/    } map { $_->name } SL::DB::Contact->meta->columns) if _has($self, 'cp_id');
index 1aa92b0..8d7088f 100644 (file)
@@ -277,7 +277,7 @@ sub new_from {
   }
 
   my %args = ( map({ ( $_ => $source->$_ ) } qw(amount cp_id currency_id cusordnumber customer_id delivery_customer_id delivery_term_id delivery_vendor_id
-                                                department_id employee_id globalproject_id intnotes marge_percent marge_total language_id netamount notes
+                                                department_id employee_id exchangerate globalproject_id intnotes marge_percent marge_total language_id netamount notes
                                                 ordnumber payment_id quonumber reqdate salesman_id shippingpoint shipvia taxincluded taxzone_id
                                                 transaction_description vendor_id
                                              )),
@@ -392,7 +392,7 @@ sub new_from_multi {
   }
   foreach my $attr (qw(cp_id currency_id employee_id salesman_id department_id
                        delivery_customer_id delivery_vendor_id shipto_id
-                       globalproject_id)) {
+                       globalproject_id exchangerate)) {
     $attributes{$attr} = undef if any { ($sources->[0]->$attr||0) != ($_->$attr||0) }   @$sources;
   }
 
index a89a311..62f16d7 100644 (file)
@@ -56,6 +56,7 @@ use SL::PriceSource;
 use SL::Presenter::Part;
 
 use SL::DB::Contact;
+use SL::DB::Currency;
 use SL::DB::Customer;
 use SL::DB::Default;
 use SL::DB::Language;
@@ -910,9 +911,18 @@ sub order {
   _order();
 
   if ($::instance_conf->get_feature_experimental_order) {
+
+    # At this point, the record is saved and the exchangerate contains
+    # an unformatted value. _make_record uses RDBO attributes (i.e. _as_number)
+    # to assign values and thus expects an formatted value.
+    $::form->{exchangerate} = $::form->format_amount(\%::myconfig, $::form->{exchangerate});
+
     my $order = _make_record();
-    $order->globalproject_id(undef) if !$order->globalproject_id;
-    $order->payment_id(undef)       if !$order->payment_id;
+
+    $order->currency(SL::DB::Currency->new(name => $::form->{currency})->load) if $::form->{currency};
+    $order->globalproject_id(undef)                                            if !$order->globalproject_id;
+    $order->payment_id(undef)                                                  if !$order->payment_id;
+
     my $row = 1;
     foreach my $item (@{$order->items_sorted}) {
       $item->custom_variables([]);
index 907e15b..5929096 100644 (file)
@@ -174,6 +174,13 @@ namespace('kivi.Order', function(ns) {
     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
   };
 
+  ns.reformat_number_as_null_number = function(event) {
+    if ($(event.target).val() === '') {
+      return;
+    }
+    ns.reformat_number(event);
+  };
+
   ns.update_exchangerate = function(event) {
     if (!ns.check_cv()) {
       $('#order_currency_id').val($('#old_currency_id').val());
@@ -181,7 +188,15 @@ namespace('kivi.Order', function(ns) {
     }
 
     var rate_input = $('#order_exchangerate_as_null_number');
-    rate_input.prop('disabled', true);
+    // unset exchangerate if currency changed
+    if ($('#order_currency_id').val() !== $('#old_currency_id').val()) {
+      rate_input.val('');
+    }
+
+    // only set exchangerate if unset
+    if (rate_input.val() !== '') {
+      return;
+    }
 
     var data = $('#order_form').serializeArray();
     data.push({ name: 'action', value: 'Order/update_exchangerate' });
@@ -194,17 +209,14 @@ namespace('kivi.Order', function(ns) {
       success: function(data){
         if (!data.is_standard) {
           $('#currency_name').text(data.currency_name);
-          var rate_text = $('#exchangerate_text');
           if (data.exchangerate) {
-            rate_text.text(data.exchangerate);
-            rate_input.hide();
+            rate_input.val(data.exchangerate);
           } else {
-            rate_text.text('');
-            rate_input.prop('disabled', false);
-            rate_input.show().val('');
+            rate_input.val('');
           }
           $('#exchangerate_settings').show();
         } else {
+          rate_input.val('');
           $('#exchangerate_settings').hide();
         }
         if ($('#order_currency_id').val() != $('#old_currency_id').val() ||
@@ -892,4 +904,6 @@ $(function() {
     return false;
   });
 
+  $('.reformat_number_as_null_number').change(kivi.Order.reformat_number_as_null_number);
+
 });
index 94728b7..f3806ff 100644 (file)
           </tr>
 
           [% SET currency_id = SELF.order.currency_id || INSTANCE_CONF.get_currency_id  # use default currency for new order %]
-          [% IF SELF.order.exchangerate %]
-            [% SET exchangerate = SELF.order.exchangerate_as_null_number %]
-          [% END %]
           <tr id="currency_settings">
             <th align="right">[% 'Currency' | $T8 %]</th>
             <td>[% L.select_tag('order.currency_id', SELF.all_currencies, default=currency_id, value_key='id', title_key='name') %]</td>
           </tr>
-          <tr id="exchangerate_settings" [%- IF currency_id==INSTANCE_CONF.get_currency_id %]style='display:none'[%- END %]>
+          <tr id="exchangerate_settings" [%- IF SELF.order.currency_id==INSTANCE_CONF.get_currency_id %]style='display:none'[%- END %]>
             <th align="right">[% 'Exchangerate' | $T8 %]</th>
             <td> 1 <span id="currency_name">[% SELF.order.currency.name %]</span> =
-              <span id="exchangerate_text">
-              [% IF exchangerate %]
-                [% HTML.escape(exchangerate) %]
-              [% END %]
-              </span>
-              [% IF currency_id==INSTANCE_CONF.get_currency_id || SELF.order.exchangerate %]
-                [% L.input_tag('order.exchangerate_as_null_number', '', size="15", class="reformat_number numeric", style='display:none', disabled=1) %]
-              [% ELSE %]
-                [% L.input_tag('order.exchangerate_as_null_number', '', size="15", class="reformat_number numeric") %]
-              [% END %]
+              [% L.input_tag('order.exchangerate_as_null_number', SELF.order.exchangerate_as_null_number, size="15", class="reformat_number_as_null_number numeric") %]
               [% INSTANCE_CONF.default_currency %]
               [% L.hidden_tag('old_currency_id', currency_id) %]
-              [% L.hidden_tag('old_exchangerate', exchangerate) %]
+              [% L.hidden_tag('old_exchangerate', SELF.order.exchangerate_as_null_number) %]
             </td>
           </tr>