Auftrags-Controller: Workflow von Angebot nach Auftrag (Ein- und Verkauf).
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Wed, 7 Feb 2018 12:30:25 +0000 (13:30 +0100)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Wed, 7 Feb 2018 13:52:45 +0000 (14:52 +0100)
SL/Controller/Order.pm
templates/webpages/order/form.html
templates/webpages/order/tabs/_row.html

index 54674c6..f3063f9 100644 (file)
@@ -17,6 +17,7 @@ use SL::DB::Unit;
 use SL::DB::Part;
 use SL::DB::Printer;
 use SL::DB::Language;
+use SL::DB::RecordLink;
 
 use SL::Helper::CreatePDF qw(:all);
 use SL::Helper::PrintOptions;
@@ -60,11 +61,7 @@ sub action_add {
   $self->_pre_render();
   $self->render(
     'order/form',
-    title => $self->type eq _sales_order_type()       ? $::locale->text('Add Sales Order')
-           : $self->type eq _purchase_order_type()    ? $::locale->text('Add Purchase Order')
-           : $self->type eq _sales_quotation_type()   ? $::locale->text('Add Quotation')
-           : $self->type eq _request_quotation_type() ? $::locale->text('Add Request for Quotation')
-           : '',
+    title => $self->_get_title_for('add'),
     %{$self->{template_args}}
   );
 }
@@ -78,11 +75,7 @@ sub action_edit {
   $self->_pre_render();
   $self->render(
     'order/form',
-    title => $self->type eq _sales_order_type()       ? $::locale->text('Edit Sales Order')
-           : $self->type eq _purchase_order_type()    ? $::locale->text('Edit Purchase Order')
-           : $self->type eq _sales_quotation_type()   ? $::locale->text('Edit Quotation')
-           : $self->type eq _request_quotation_type() ? $::locale->text('Edit Request for Quotation')
-           : '',
+    title => $self->_get_title_for('edit'),
     %{$self->{template_args}}
   );
 }
@@ -519,6 +512,16 @@ sub action_save_and_invoice {
   $self->redirect_to(@redirect_params);
 }
 
+# workflow from sales quotation to sales order
+sub action_sales_order {
+  $_[0]->_workflow_sales_or_purchase_order();
+}
+
+# workflow from rfq to purchase order
+sub action_purchase_order {
+  $_[0]->_workflow_sales_or_purchase_order();
+}
+
 # set form elements in respect to a changed customer or vendor
 #
 # This action is called on an change of the customer/vendor picker.
@@ -1087,9 +1090,9 @@ sub _make_item {
   $item ||= SL::DB::OrderItem->new(custom_variables => []);
 
   $item->assign_attributes(%$attr);
-  $item->longdescription($item->part->notes)   if $is_new && !defined $attr->{longdescription};
-  $item->project_id($record->globalproject_id) if $is_new && !defined $attr->{project_id};
-  $item->lastcost($item->part->lastcost)       if $is_new && !defined $attr->{lastcost_as_number};
+  $item->longdescription($item->part->notes)                     if $is_new && !defined $attr->{longdescription};
+  $item->project_id($record->globalproject_id)                   if $is_new && !defined $attr->{project_id};
+  $item->lastcost($record->is_sales ? $item->part->lastcost : 0) if $is_new && !defined $attr->{lastcost_as_number};
 
   return $item;
 }
@@ -1145,7 +1148,7 @@ sub _new_item {
   $new_attr{active_discount_source} = $discount_src;
   $new_attr{longdescription}        = $part->notes           if ! defined $attr->{longdescription};
   $new_attr{project_id}             = $record->globalproject_id;
-  $new_attr{lastcost}               = $part->lastcost;
+  $new_attr{lastcost}               = $record->is_sales ? $part->lastcost : 0;
 
   # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
   # they cannot be retrieved via custom_variables until the order/orderitem is
@@ -1230,11 +1233,63 @@ sub _save {
   $db->with_transaction(sub {
     SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete};
     $self->order->save(cascade => 1);
+
+    # link records
+    if ($::form->{converted_from_oe_id}) {
+      SL::DB::Order->new(id => $::form->{converted_from_oe_id})->load->link_to_record($self->order);
+
+      if (scalar @{ $::form->{converted_from_orderitems_ids} || [] }) {
+        my $idx = 0;
+        foreach (@{ $self->order->items_sorted }) {
+          my $from_id = $::form->{converted_from_orderitems_ids}->[$idx];
+          next if !$from_id;
+          SL::DB::RecordLink->new(from_table => 'orderitems',
+                                  from_id    => $from_id,
+                                  to_table   => 'orderitems',
+                                  to_id      => $_->id
+          )->save;
+          $idx++;
+        }
+      }
+    }
+    1;
   }) || push(@{$errors}, $db->error);
 
   return $errors;
 }
 
+sub _workflow_sales_or_purchase_order {
+  my ($self) = @_;
+
+  my $destination_type = $::form->{type} eq _sales_quotation_type()   ? _sales_order_type()
+                       : $::form->{type} eq _request_quotation_type() ? _purchase_order_type()
+                       : '';
+
+  $self->order(SL::DB::Order->new_from($self->order, destination_type => $destination_type));
+  $self->{converted_from_oe_id} = delete $::form->{id};
+
+  # change form type
+  $::form->{type} = $destination_type;
+  $self->init_type;
+  $self->_check_auth;
+
+  $self->_recalc();
+  $self->_get_unalterable_data();
+  $self->_pre_render();
+
+  # trigger rendering values for second row/longdescription as hidden,
+  # because they are loaded only on demand. So we need to keep the values
+  # from the source.
+  $_->{render_second_row}      = 1 for @{ $self->order->items_sorted };
+  $_->{render_longdescription} = 1 for @{ $self->order->items_sorted };
+
+  $self->render(
+    'order/form',
+    title => $self->_get_title_for('edit'),
+    %{$self->{template_args}}
+  );
+}
+
 
 sub _pre_render {
   my ($self) = @_;
@@ -1319,9 +1374,24 @@ sub _setup_edit_action_bar {
           call      => [ 'kivi.Order.save_and_invoice', $::instance_conf->get_order_warn_duplicate_parts ],
           checks    => [ 'kivi.Order.check_save_active_periodic_invoices' ],
         ],
-
       ], # end of combobox "Save"
 
+      combobox => [
+        action => [
+          t8('Workflow'),
+        ],
+        action => [
+          t8('Sales Order'),
+          submit  => [ '#order_form', { action => "Order/sales_order" } ],
+          only_if => (any { $self->type eq $_ } (_sales_quotation_type())),
+        ],
+        action => [
+          t8('Purchase Order'),
+          submit  => [ '#order_form', { action => "Order/purchase_order" } ],
+          only_if   => (any { $self->type eq $_ } (_request_quotation_type())),
+        ],
+      ], # end of combobox "Workflow"
+
       combobox => [
         action => [
           t8('Export'),
@@ -1458,6 +1528,29 @@ sub _get_periodic_invoices_status {
   return $active ? t8('active') : t8('inactive');
 }
 
+sub _get_title_for {
+  my ($self, $action) = @_;
+
+  return '' if none { lc($action)} qw(add edit);
+
+  # for locales:
+  # $::locale->text("Add Sales Order");
+  # $::locale->text("Add Purchase Order");
+  # $::locale->text("Add Quotation");
+  # $::locale->text("Add Request for Quotation");
+  # $::locale->text("Edit Sales Order");
+  # $::locale->text("Edit Purchase Order");
+  # $::locale->text("Edit Quotation");
+  # $::locale->text("Edit Request for Quotation");
+
+  $action = ucfirst(lc($action));
+  return $self->type eq _sales_order_type()       ? $::locale->text("$action Sales Order")
+       : $self->type eq _purchase_order_type()    ? $::locale->text("$action Purchase Order")
+       : $self->type eq _sales_quotation_type()   ? $::locale->text("$action Quotation")
+       : $self->type eq _request_quotation_type() ? $::locale->text("$action Request for Quotation")
+       : '';
+}
+
 sub _sales_order_type {
   'sales_order';
 }
index f36dff2..0e28642 100644 (file)
 </div>
 
 <form method="post" action="controller.pl" id="order_form">
-  [% L.hidden_tag('callback', FORM.callback) %]
-  [% L.hidden_tag('type',     FORM.type) %]
-  [% L.hidden_tag('id',       SELF.order.id) %]
+  [% L.hidden_tag('callback',             FORM.callback) %]
+  [% L.hidden_tag('type',                 FORM.type) %]
+  [% L.hidden_tag('id',                   SELF.order.id) %]
+  [% L.hidden_tag('converted_from_oe_id', SELF.converted_from_oe_id) %]
 
   [%- INCLUDE 'common/flash.html' %]
 
index c057747..0de680f 100644 (file)
@@ -15,6 +15,7 @@
                      alt=LxERP.t8('Show details'), title=LxERP.t8('Show details'), class="expand") %]
       [%- END %]
       [% L.hidden_tag("orderitem_ids[+]", ID) %]
+      [% L.hidden_tag("converted_from_orderitems_ids[+]", ITEM.converted_from_orderitems_id) %]
       [% L.hidden_tag("order.orderitems[+].id", ITEM.id, id='item_' _ ID) %]
       [% L.hidden_tag("order.orderitems[].parts_id", ITEM.parts_id) %]
     </td>
@@ -42,6 +43,9 @@
                      ITEM.description,
                      size='40',
                      style='width: 300px') %]
+      [%- IF ITEM.render_longdescription -%]
+        [%- L.hidden_tag("order.orderitems[].longdescription", ITEM.longdescription) %]
+      [%- END -%]
       [%- L.button_tag("kivi.Order.show_longdescription_dialog(this)", LxERP.t8("L")) %]
     </td>
     <td>
 
   <tr [%- IF !MYCONFIG.show_form_details -%]style="display:none"[%- END -%]>
     <td colspan="100%">
-      [%- IF MYCONFIG.show_form_details %]
+      [%- IF MYCONFIG.show_form_details || ITEM.render_second_row %]
         <div name="second_row" data-loaded="1">
           [%- PROCESS order/tabs/_second_row.html ITEM=ITEM TYPE=TYPE %]
         </div>