From f0ddfbfe848f0d94a7a28b8b5593786217c5dd39 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 22 Oct 2021 12:30:57 +0200 Subject: [PATCH] =?utf8?q?Pflichtenhefte:=20bei=20Pflichtenheft=20?= =?utf8?q?=E2=86=92=20Angebot=20=E2=86=92=20Auftrag=20auch=20PH=20mit=20Au?= =?utf8?q?ftrag=20verkn=C3=BCpfen?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Controller/Order.pm | 19 ++++++++ SL/OE.pm | 43 +++++++++++++++++++ doc/changelog | 3 ++ ...otations_created_from_requirement_spec.sql | 29 +++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 sql/Pg-upgrade2/link_requirement_spec_to_orders_created_from_quotations_created_from_requirement_spec.sql diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index fdfa155c8..eac3ad9db 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -23,6 +23,7 @@ use SL::DB::PartsGroup; use SL::DB::Printer; use SL::DB::Language; use SL::DB::RecordLink; +use SL::DB::RequirementSpec; use SL::DB::Shipto; use SL::DB::Translation; @@ -1678,6 +1679,7 @@ sub save { # link records if ($::form->{converted_from_oe_id}) { my @converted_from_oe_ids = split ' ', $::form->{converted_from_oe_id}; + foreach my $converted_from_oe_id (@converted_from_oe_ids) { my $src = SL::DB::Order->new(id => $converted_from_oe_id)->load; $src->update_attributes(closed => 1) if $src->type =~ /_quotation$/; @@ -1696,6 +1698,8 @@ sub save { $idx++; } } + + $self->link_requirement_specs_linking_to_created_from_objects(@converted_from_oe_ids); } $self->save_history('SAVED'); @@ -2309,6 +2313,21 @@ sub store_pdf_to_webdav_and_filemanagement { return @errors; } +sub link_requirement_specs_linking_to_created_from_objects { + my ($self, @converted_from_oe_ids) = @_; + + return unless @converted_from_oe_ids; + + my $rs_orders = SL::DB::Manager::RequirementSpecOrder->get_all(where => [ order_id => \@converted_from_oe_ids ]); + foreach my $rs_order (@{ $rs_orders }) { + SL::DB::RequirementSpecOrder->new( + order_id => $self->order->id, + requirement_spec_id => $rs_order->requirement_spec_id, + version_id => $rs_order->version_id, + )->save; + } +} + 1; __END__ diff --git a/SL/OE.pm b/SL/OE.pm index 6b4831267..266e55793 100644 --- a/SL/OE.pm +++ b/SL/OE.pm @@ -44,6 +44,7 @@ use SL::DB::Order; use SL::DB::PeriodicInvoicesConfig; use SL::DB::Project; use SL::DB::ProjectType; +use SL::DB::RequirementSpecOrder; use SL::DB::Status; use SL::DB::Tax; use SL::DBUtils; @@ -479,6 +480,7 @@ sub _save { my $number_field = $form->{type} =~ m{order} ? 'ordnumber' : 'quonumber'; my $trans_number = SL::TransNumber->new(type => $form->{type}, dbh => $dbh, number => $form->{$number_field}, id => $form->{id}); $form->{$number_field} ||= $trans_number->create_unique; # set $form->{ordnumber} or $form->{quonumber} + my $is_new = !$form->{id}; if ($form->{id}) { $query = qq|DELETE FROM custom_variables @@ -832,11 +834,52 @@ SQL config_yaml => $form->{periodic_invoices_config}) if ($form->{type} eq 'sales_order'); + $self->_link_created_sales_order_to_requirement_specs_for_sales_quotations( + type => $form->{type}, + converted_from_ids => \@convert_from_oe_ids, + sales_order_id => $form->{id}, + is_new => $is_new, + ); + $main::lxdebug->leave_sub(); return 1; } +sub _link_created_sales_order_to_requirement_specs_for_sales_quotations { + my ($self, %params) = @_; + + # If this is a sales order created from a sales quotation and if + # that sales quotation was created from a requirement spec document + # then link the newly created sales order to the requirement spec + # document, too. + + return if !$params{is_new}; + return if $params{type} ne 'sales_order'; + return if !@{ $params{converted_from_ids} }; + + my $oe_objects = SL::DB::Manager::Order->get_all(where => [ id => $params{converted_from_ids} ]); + my @sales_quotations = grep { $_->is_type('sales_quotation') } @{ $oe_objects }; + + return if !@sales_quotations; + + my $rs_orders = SL::DB::Manager::RequirementSpecOrder->get_all(where => [ order_id => [ map { $_->id } @sales_quotations ] ]); + + return if !@{ $rs_orders }; + + $rs_orders->[0]->db->with_transaction(sub { + foreach my $rs_order (@{ $rs_orders }) { + SL::DB::RequirementSpecOrder->new( + order_id => $params{sales_order_id}, + requirement_spec_id => $rs_order->requirement_spec_id, + version_id => $rs_order->version_id, + )->save; + } + + 1; + }); +} + sub save_periodic_invoices_config { my ($self, %params) = @_; diff --git a/doc/changelog b/doc/changelog index 369dc8c9a..c32820016 100644 --- a/doc/changelog +++ b/doc/changelog @@ -29,6 +29,9 @@ Kleinere neue Features und Detailverbesserungen: vom Auftrag her aufgebaut werden - SEPA-Überweisungen & -Bankeinzüge nutzen jetzt aktuelle Standard-Versionen, die momentan von der Kreditindustrie unterstützt werden. +- Pflichtenhefte: wenn man im Workflow vom Pflichtenheft ein neues + Angebot anlegt und später von diesem Angebot aus einen Auftrag, so + wird auch der Auftrag direkt mit dem Pflichtenheft verknüpft. Bugfixes (Tracker: https://www.kivitendo.de/redmine): diff --git a/sql/Pg-upgrade2/link_requirement_spec_to_orders_created_from_quotations_created_from_requirement_spec.sql b/sql/Pg-upgrade2/link_requirement_spec_to_orders_created_from_quotations_created_from_requirement_spec.sql new file mode 100644 index 000000000..cf549acbe --- /dev/null +++ b/sql/Pg-upgrade2/link_requirement_spec_to_orders_created_from_quotations_created_from_requirement_spec.sql @@ -0,0 +1,29 @@ +-- @tag: link_requirement_spec_to_orders_created_from_quotations_created_from_requirement_spec +-- @description: Pflichtenhefte mit Aufträgen verknüpfen, die aus Angeboten erstellt wurden, die wiederum aus einem Pflichtenheft erstellt wurden +-- @depends: release_3_2_0 +CREATE TEMPORARY TABLE temp_link_requirement_spec_to_orders AS +SELECT rs_orders.requirement_spec_id, orders.id AS order_id, rs_orders.version_id +FROM record_links rl, + requirement_spec_orders rs_orders, + oe quotations, + oe orders +WHERE (rl.from_table = 'oe') + AND (rl.from_id = quotations.id) + AND (rl.to_table = 'oe') + AND (rl.to_id = orders.id) + AND (rs_orders.order_id = quotations.id) + AND COALESCE(quotations.quotation, FALSE) + AND NOT COALESCE(orders.quotation, FALSE) + AND (quotations.customer_id IS NOT NULL) + AND (orders.customer_id IS NOT NULL); + +INSERT INTO requirement_spec_orders (requirement_spec_id, order_id, version_id) +SELECT requirement_spec_id, order_id, version_id +FROM temp_link_requirement_spec_to_orders new_orders +WHERE NOT EXISTS ( + SELECT existing_orders.id + FROM requirement_spec_orders existing_orders + WHERE (existing_orders.requirement_spec_id = new_orders.requirement_spec_id) + AND (existing_orders.order_id = new_orders.order_id) + LIMIT 1 +); -- 2.20.1