From fa2c6a9a9f4ff32f8e95b62ef196d39619f8f0e7 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Mon, 8 Aug 2022 13:41:40 +0000 Subject: [PATCH] Bug 21173: Add Hours as an age unit to auto item modifications by age items.damaged_on, items.itemlost_on, and items.withdrawn_on are datetime fields, so it could be useful to configure rules based on the number of hours since these values. All other fields in the automatic item modifications by age tool will use the days unit. Test plan: 1. Go to Tools->Automatic item modifications by age 2. Create a new rule 3. Apply patches and restart services 4. Confirm the rule you created has an 'Age unit' displayed of 'Days' 5. Edit the existing rule. Notice if your 'Age field' is not set to items.damaged_on, items.itemlost_on or items.withdrawn_on then the 'Age unit' dropdown is disabled 6. Confirm if you try to save a rule with age unit = 'Hours' and a field other than items.damaged_on, items.itemlost_on, or items.withdrawn_on then the 'List of rules' table shows that rule with the unit of 'Days' 7. Click 'Add rule'. Observe the 'Age unit' dropdown is initially disabled and set to 'Days' until you change the 'Age field' dropdown to items.damaged_on, items.itemlost_on, or items.withdrawn_on 8. Save the rule: Age: 1 Age unit: Hours Age field: items.itemlost_on Conditions: items.barcode = TEST Substitutions: items.barcode = TEST1 9. Find an item. Set it's barcode to TEST. In the database change it's items.itemlost_on to more than 1 hour ago 10. Restart services 11. Run the automatic_item_modification_by_age.pl script: sudo koha-shell kohadev ./misc/cronjobs/automatic_item_modification_by_age.pl -c 12. Confirm the item's barcode has changed to TEST1 Sponsored-by: Toi Ohomai Institute of Technology, New Zealand Signed-off-by: David Nind --- C4/Items.pm | 17 ++++++++++++++++- tools/automatic_item_modification_by_age.pl | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/C4/Items.pm b/C4/Items.pm index c640b63baa..09b6da1419 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -1571,6 +1571,9 @@ sub ToggleNewStatus { my $report; for my $rule ( @rules ) { my $age = $rule->{age}; + # Default to using Days if there's an old item modification rule + # missing an ageunit value + my $unit = $rule->{ageunit} ? $rule->{ageunit} : 'Days'; # Default to using items.dateaccessioned if there's an old item modification rule # missing an agefield value my $agefield = $rule->{agefield} ? $rule->{agefield} : 'items.dateaccessioned'; @@ -1606,7 +1609,19 @@ sub ToggleNewStatus { } } if ( defined $age ) { - $query .= qq| AND TO_DAYS(NOW()) - TO_DAYS($agefield) >= ? |; + if ( defined $rule + && $unit eq "Hours" + && ( + $agefield eq "items.damaged_on" + || $agefield eq "items.itemlost_on" + || $agefield eq "items.withdrawn_on" ) + ) { + # Calculate item age based on hours since damaged_on, itemlost_on, withdrawn_on + $query .= qq| AND HOUR(TIMEDIFF(NOW(), $agefield)) >= ? |; + } else { + # Calculate item age based on days + $query .= qq| AND TO_DAYS(NOW()) - TO_DAYS($agefield) >= ? |; + } push @params, $age; } my $sth = $dbh->prepare($query); diff --git a/tools/automatic_item_modification_by_age.pl b/tools/automatic_item_modification_by_age.pl index a3138091d3..e149d503ba 100755 --- a/tools/automatic_item_modification_by_age.pl +++ b/tools/automatic_item_modification_by_age.pl @@ -67,6 +67,7 @@ if ( $op eq 'update' ) { my @condition_fields = $cgi->multi_param("condition_field_$unique_id"); my @condition_values = $cgi->multi_param("condition_value_$unique_id"); my @age_fields = $cgi->multi_param("agefield_$unique_id"); + my @age_units = $cgi->multi_param("ageunit_$unique_id"); my $rule = { substitutions => [], conditions => [], @@ -90,6 +91,17 @@ if ( $op eq 'update' ) { for my $age_field ( @age_fields ) { $rule->{agefield} = $age_field ? $age_field : "items.dateaccessioned"; } + + for my $age_unit ( @age_units ) { + if ($rule->{agefield} ne 'items.damaged_on' + && $rule->{agefield} ne 'items.itemlost_on' + && $rule->{agefield} ne 'items.withdrawn_on') { + $rule->{ageunit} = "Days"; + } else { + $rule->{ageunit} = $age_unit ? $age_unit : "Days"; + } + } + push @rules, $rule; } my $syspref_content = to_json( \@rules ); @@ -116,9 +128,11 @@ if ( $@ ) { my @item_fields = map { "items.$_" } Koha::Items->columns; my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns; my @age_fields = ('items.dateaccessioned', 'items.replacementpricedate', 'items.datelastborrowed', 'items.datelastseen', 'items.damaged_on', 'items.itemlost_on', 'items.withdrawn_on'); +my @age_units = ('Hours', 'Days'); $template->param( op => $op, messages => \@messages, + ageunits => [ @age_units ], agefields => [ @age_fields ], condition_fields => [ @item_fields, @biblioitem_fields ], substitution_fields => \@item_fields, -- 2.30.2