From e3f23bb481234309e7bc11e6b339e947996e115b Mon Sep 17 00:00:00 2001
From: Jonathan Druart
Date: Wed, 10 May 2023 15:18:15 +0200
Subject: [PATCH] Bug 28844: Add link between suggestions and orders
---
Koha/Acquisition/Order.pm | 19 ++++++++
acqui/addorder.pl | 1 +
acqui/basket.pl | 6 ---
acqui/orderreceive.pl | 1 -
acqui/parcel.pl | 6 +--
api/v1/swagger/definitions/order.yaml | 9 ++++
api/v1/swagger/paths/acquisitions_orders.yaml | 6 ++-
.../prog/en/modules/acqui/basket.tt | 8 ++--
.../prog/en/modules/acqui/orderreceive.tt | 35 +++++++-------
.../prog/en/modules/acqui/parcel.tt | 48 +++++++++----------
t/db_dependent/Koha/Acquisition/Order.t | 31 +++++++++++-
11 files changed, 111 insertions(+), 59 deletions(-)
diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm
index e0fea073f1a..eee73ce0533 100644
--- a/Koha/Acquisition/Order.pm
+++ b/Koha/Acquisition/Order.pm
@@ -297,6 +297,24 @@ sub subscription {
return Koha::Subscription->_new_from_dbic( $subscription_rs );
}
+=head3 suggestion
+
+ my $suggestion = $order->suggestion
+
+Returns the I object for the suggestion associated
+to the order.
+
+It returns B if no linked suggestion is found.
+
+=cut
+
+sub suggestion {
+ my ( $self ) = @_;
+ my $rs = $self->_result->suggestion;
+ return unless $rs;
+ return Koha::Suggestion->_new_from_dbic( $rs );
+}
+
=head3 current_item_level_holds
my $holds = $order->current_item_level_holds;
@@ -657,6 +675,7 @@ sub to_api_mapping {
sort2 => 'statistics_2',
sort2_authcat => 'statistics_2_authcat',
subscriptionid => 'subscription_id',
+ suggestionid => 'suggestion_id',
suppliers_reference_number => undef, # EDIFACT related
suppliers_reference_qualifier => undef, # EDIFACT related
suppliers_report => undef, # EDIFACT related
diff --git a/acqui/addorder.pl b/acqui/addorder.pl
index c5b9847b019..de8d3648f89 100755
--- a/acqui/addorder.pl
+++ b/acqui/addorder.pl
@@ -264,6 +264,7 @@ my $orderinfo = {
sort2 => scalar $input->param('sort2'),
subscriptionid => scalar $input->param('subscriptionid'),
estimated_delivery_date => scalar $input->param('estimated_delivery_date'),
+ suggestionid => scalar $input->param('suggestionid'),
};
$orderinfo->{uncertainprice} ||= 0;
diff --git a/acqui/basket.pl b/acqui/basket.pl
index 33253aad47b..17589b66f8d 100755
--- a/acqui/basket.pl
+++ b/acqui/basket.pl
@@ -27,7 +27,6 @@ use CGI qw ( -utf8 );
use C4::Acquisition qw( GetBasket CanUserManageBasket GetBasketAsCSV NewBasket NewBasketgroup ModBasket ReopenBasket ModBasketUsers GetBasketgroup GetBasketgroups GetBasketUsers GetOrders GetOrder get_rounded_price );
use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
use C4::Contract qw( GetContract );
-use C4::Suggestions qw( GetSuggestion GetSuggestionInfoFromBiblionumber GetSuggestionInfo );
use Koha::Biblios;
use Koha::Acquisition::Baskets;
use Koha::Acquisition::Booksellers;
@@ -503,11 +502,6 @@ sub get_order_infos {
}
- my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
- $line{suggestionid} = $$suggestion{suggestionid};
- $line{surnamesuggestedby} = $$suggestion{surnamesuggestedby};
- $line{firstnamesuggestedby} = $$suggestion{firstnamesuggestedby};
-
$line{estimated_delivery_date} = $order->{estimated_delivery_date};
foreach my $key (qw(transferred_from transferred_to)) {
diff --git a/acqui/orderreceive.pl b/acqui/orderreceive.pl
index d8192292f2f..2ca33d16471 100755
--- a/acqui/orderreceive.pl
+++ b/acqui/orderreceive.pl
@@ -68,7 +68,6 @@ use C4::Output qw( output_html_with_http_headers );
use C4::Budgets qw( GetBudget GetBudgetPeriods GetBudgetPeriod GetBudgetHierarchy CanUserUseBudget );
use C4::Members;
use C4::Biblio qw( GetMarcStructure );
-use C4::Suggestions qw( GetSuggestion GetSuggestionInfoFromBiblionumber GetSuggestionInfo );
use Koha::Acquisition::Booksellers;
use Koha::Acquisition::Currencies qw( get_active );
diff --git a/acqui/parcel.pl b/acqui/parcel.pl
index 06172690f68..ed3ca1a8fee 100755
--- a/acqui/parcel.pl
+++ b/acqui/parcel.pl
@@ -61,7 +61,6 @@ use C4::Acquisition qw( CancelReceipt GetInvoice GetInvoiceDetails get_rounded_p
use C4::Budgets qw( GetBudget GetBudgetByOrderNumber GetBudgetName );
use CGI qw ( -utf8 );
use C4::Output qw( output_html_with_http_headers );
-use C4::Suggestions qw( GetSuggestion GetSuggestionInfoFromBiblionumber GetSuggestionInfo );
use Koha::Acquisition::Baskets;
use Koha::Acquisition::Bookseller;
@@ -149,10 +148,7 @@ for my $order ( @orders ) {
$total_tax_excluded += get_rounded_price($line{unitprice_tax_excluded}) * $line{quantity};
$total_tax_included += get_rounded_price($line{unitprice_tax_included}) * $line{quantity};
- my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
- $line{suggestionid} = $suggestion->{suggestionid};
- $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
- $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
+ $line{order_object} = $order_object;
if ( $line{parent_ordernumber} != $line{ordernumber} ) {
if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
diff --git a/api/v1/swagger/definitions/order.yaml b/api/v1/swagger/definitions/order.yaml
index 61aaae480d9..f2c0ecfaa30 100644
--- a/api/v1/swagger/definitions/order.yaml
+++ b/api/v1/swagger/definitions/order.yaml
@@ -204,6 +204,11 @@ properties:
- integer
- "null"
description: Subscription ID linking the order to a subscription
+ suggestion_id:
+ type:
+ - integer
+ - "null"
+ description: Suggestion ID linking the order to a suggestion
parent_order_id:
type:
- integer
@@ -264,4 +269,8 @@ properties:
- object
- "null"
description: Patron that created the order
+ suggestion:
+ type:
+ - object
+ - "null"
additionalProperties: false
diff --git a/api/v1/swagger/paths/acquisitions_orders.yaml b/api/v1/swagger/paths/acquisitions_orders.yaml
index 260597281da..5a0e7256ca1 100644
--- a/api/v1/swagger/paths/acquisitions_orders.yaml
+++ b/api/v1/swagger/paths/acquisitions_orders.yaml
@@ -57,7 +57,6 @@
- biblio.active_orders+count
- biblio.holds+count
- biblio.items+count
- - biblio.suggestions.suggester
- creator
- fund
- fund.budget
@@ -66,6 +65,8 @@
- items
- items+strings
- subscription
+ - suggestion
+ - suggestion.suggester
collectionFormat: csv
responses:
"200":
@@ -177,12 +178,13 @@
- biblio.active_orders+count
- biblio.holds+count
- biblio.items+count
- - biblio.suggestions.suggester
- fund
- current_item_level_holds+count
- invoice
- items
- subscription
+ - suggestion
+ - suggestion.suggester
collectionFormat: csv
produces:
- application/json
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
index 129567a290d..ccc1648b9d1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
@@ -582,10 +582,12 @@
[%- IF ( books_loo.publicationyear ) %], [% books_loo.publicationyear | html -%]
[%- ELSIF ( books_loo.copyrightdate ) %] [% books_loo.copyrightdate | html %][% END -%]
[%- IF ( books_loo.editionstatement ) %], [% books_loo.editionstatement | html %][% END -%]
- [%- IF ( books_loo.suggestionid ) %]
+ [% SET suggestion = books_loo.order_object.suggestion %]
+ [% IF suggestion %]
- Suggested by: [% books_loo.surnamesuggestedby | html %][% IF ( books_loo.firstnamesuggestedby ) %], [% books_loo.firstnamesuggestedby | html %] [% END %]
- (suggestion #[% books_loo.suggestionid | html %])
+ [% SET suggester = suggestion.suggester %]
+ Suggested by: [% suggester.surname | html %][% IF suggester.firstname %] %], [% suggester.firstname | html %] [% END %]
+ (suggestion #[% suggestion.suggestionid | html %])
[% END %]
[% IF ( books_loo.order_internalnote ) %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt
index c20f14bf994..94b2aaddcf0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt
@@ -561,10 +561,11 @@
},
"embed": [
"basket",
- "biblio.suggestions.suggester",
+ "biblio",
"fund.budget",
"items+strings",
- "creator"
+ "creator",
+ "suggestion.suggester",
],
'dom': 'C<"top pager"ilpfB><"#filter_c">tr<"bottom pager"ip>',
"columns": [
@@ -693,13 +694,13 @@
params['ordernumber'] = row.order_id;
params['booksellerid'] = row.basket.vendor_id;
- if(row.biblio.suggestions.length && row.biblio.suggestions[0].reason) {
- params["suggestionid"] = row.biblio.suggestions[0].suggestion_id;
- if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
- params['reason'] = row.biblio.suggestions[0].reason;
+ if(row.suggestion && row.suggestion.reason) {
+ params["suggestionid"] = row.suggestion.suggestion_id;
+ if($("#reason option[value='"+row.suggestion.reason+"']").length) {
+ params['reason'] = row.suggestion.reason;
} else {
params['reason'] = 'other';
- params['other_reason'] = row.biblio.suggestions[0].reason;
+ params['other_reason'] = row.suggestion.reason;
}
}
params['datereceived'] = row.date_received;
@@ -837,14 +838,14 @@
$("#other_reason").show();
$(this).hide();
} else {
- row.biblio.suggestions[0].reason = val;
+ row.suggestion.reason = val;
}
});
$("#order_edit").on("change", "#select-other_reason", function() {
var val = $(this).val();
var row = $("#order_edit").data('row');
- row.biblio.suggestions[0].reason = val;
+ row.suggestion.reason = val;
});
$("#order_edit").on("click", "#other_reason a", function() {
@@ -1132,30 +1133,30 @@
o.parent().hide();
}
});
- if(row.biblio.suggestions.length) {
+ if(row.suggestion) {
$("#suggestion_fieldset").show();
- if(row.biblio.suggestions[0].suggester) {
+ if(row.suggestion.suggester) {
$("#biblio_suggestion_suggester").parent().show();
$("#biblio_suggestion_suggester")
.html(
- [row.biblio.suggestions[0].suggester.surname, row.biblio.suggestions[0].suggester.firstname]
+ [row.suggestion.suggester.surname, row.suggestion.suggester.firstname]
.filter(function(name){
return name
})
- .join(', ')+' ('+SUGGESTION.format(row.biblio.suggestions[0].suggestion_id)+')'
+ .join(', ')+' ('+SUGGESTION.format(row.suggestion.suggestion_id)+')'
);
} else {
$("#biblio_suggestion_suggester").parent().hide();
}
- if(row.biblio.suggestions[0].reason) {
+ if(row.suggestion.reason) {
$("#suggestion_reason").show();
- if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
+ if($("#reason option[value='"+row.suggestion.reason+"']").length) {
$("#other_reason a").click();
- $("#reason").val(row.biblio.suggestions[0].reason);
+ $("#reason").val(row.suggestion.reason);
$("#select-other_reason").val(null);
} else {
$("#reason").val("other").change();
- $("#select-other_reason").val(row.biblio.suggestions[0].reason);
+ $("#select-other_reason").val(row.suggestion.reason);
}
} else {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt
index c015324c2b2..0d33db4c00d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt
@@ -244,10 +244,13 @@
[%- ELSIF ( order.copyrightdate > 0) -%] [% order.copyrightdate | html %]
[% END %]
[% END %]
- [% IF ( order.suggestionid ) %]
+
+ [% SET suggestion = order.order_object.suggestion %]
+ [% IF suggestion %]
+ [% SET suggester = suggestion.suggester %]
- Suggested by: [% order.surnamesuggestedby | html %][% IF ( order.firstnamesuggestedby ) %], [% order.firstnamesuggestedby | html %] [% END %]
- (suggestion #[% order.suggestionid | html %])
+ Suggested by: [% suggester.surname | html %][% IF suggester.firstname %] %], [% suggester.firstname | html %] [% END %]
+ (suggestion #[% suggestion.suggestionid | html %])
[% END %]
[% IF ( order.order_internalnote ) %]
@@ -394,10 +397,10 @@
"biblio.active_orders+count",
"biblio.holds+count",
"biblio.items+count",
- "biblio.suggestions.suggester",
"fund",
"current_item_level_holds+count",
- "items"
+ "items",
+ "suggestion.suggester"
],
"stateSave": true, // remember state on page reload
"drawCallback": function (settings) {
@@ -468,26 +471,23 @@
result += escape_str(row.biblio.copyright_date);
}
}
- var suggestions = row.biblio.suggestions;
- if ( suggestions != null && suggestions.length > 0 ) {
- var suggestion = suggestions[0];
- if ( suggestion.suggester != null ) {
- var suggester = suggestion.suggester;
- var suggested_by = [];
- if ( suggester.surname != null ) {
- suggested_by.push(escape_str(suggester.surname));
- }
- if ( suggester.firstname != null ) {
- suggested_by.push(escape_str(suggester.firstname));
- }
-
- result += "
" + _("Suggested by: ") +
- ''
- + suggested_by.join(", ")
- + " (#" + escape_str(suggestions[0].suggestionid) + ")"; // FIXME: could be changed if we allow matching multiple suggestions
+ let suggestion = row.suggestion;
+ if ( suggestion && suggestion.suggester != null ) {
+ var suggester = suggestion.suggester;
+ var suggested_by = [];
+ if ( suggester.surname != null ) {
+ suggested_by.push(escape_str(suggester.surname));
+ }
+ if ( suggester.firstname != null ) {
+ suggested_by.push(escape_str(suggester.firstname));
}
+
+ result += "
" + _("Suggested by: ") +
+ ''
+ + suggested_by.join(", ")
+ + " (#" + escape_str(suggestion.suggestionid) + ")";
}
result += '';
}
diff --git a/t/db_dependent/Koha/Acquisition/Order.t b/t/db_dependent/Koha/Acquisition/Order.t
index 10fdfa46851..a7de91b39ab 100755
--- a/t/db_dependent/Koha/Acquisition/Order.t
+++ b/t/db_dependent/Koha/Acquisition/Order.t
@@ -19,7 +19,7 @@
use Modern::Perl;
-use Test::More tests => 13;
+use Test::More tests => 14;
use Test::Exception;
use t::lib::TestBuilder;
@@ -210,6 +210,35 @@ subtest 'subscription' => sub {
$schema->storage->txn_rollback;
};
+subtest 'suggestion' => sub {
+ plan tests => 2;
+
+ $schema->storage->txn_begin;
+ my $o = $builder->build_object(
+ {
+ class => 'Koha::Acquisition::Orders',
+ value => { suggestionid => undef }, # not linked to a suggestion
+ }
+ );
+
+ my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
+ is( $order->suggestion, undef,
+ '->suggestion should return undef if not created from a suggestion');
+
+ $o = $builder->build_object(
+ {
+ class => 'Koha::Acquisition::Orders',
+ # Will be linked to a suggestion by TestBuilder
+ }
+ );
+
+ $order = Koha::Acquisition::Orders->find( $o->ordernumber );
+ is( ref( $order->suggestion ), 'Koha::Suggestion',
+ '->suggestion should return a Koha::Suggestion object if created from a suggestion');
+
+ $schema->storage->txn_rollback;
+};
+
subtest 'duplicate_to | add_item' => sub {
plan tests => 3;
--
2.34.1