From 80d890384681ccbbfda88f169204820dfad15f40 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Wed, 31 Jan 2018 15:16:16 +0000 Subject: [PATCH] Bug 20125 - Add a script to claim late orders (ordersClaim.pl) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test plan: - Put some orders late from different baskets by changing the closedate column in aqbasket table, Testing delay parameters: - launch "perl misc/cronjobs/ordersClaim.pl" with the following parameters: - delay: Up to the number of days from the basket's closedate, - claimed-for: no matter for the first claim, - Check that there is one entry in message_queue *for each basket with late order(s)* - Check these late orders in database: - claims_count column should be set to 1, - claimed_date should be today Testing claimed-for parameter: - change the claimed_date column in database for all orders (i.e 2 days back), - launch "perl misc/cronjobs/ordersClaim.pl" again with the following parameters: - delay: Same than for the first test, - claimed-for: 3 (if set claimed_date 2 days back), - max-claims: at least 2 - check that there is no new entry in message_queue - launch "perl misc/cronjobs/ordersClaim.pl" again by moving only claimed-for to 1 - There should be new entry in message_queue - claims_count column should be set to 2 - claimed_date should be today Testing max-claims parameter: - change the claimed_date column in database for all orders (i.e 2 days back), - launch "perl misc/cronjobs/ordersClaim.pl" again with the following parameters: - delay: Same than for the first and second test, - claimed-for: 1 (if set claimed_date 2 days back), - max-claims: 1 and 2 (test the two options), - There should be new entry in message_queue - launch "perl misc/cronjobs/ordersClaim.pl" again by moving only max-claims to 3 - There should be new entry in message_queue - claims_count column should be set to 3 - claimed_date should be today Signed-off-by: Séverine QUEUNE --- misc/cronjobs/ordersClaim.pl | 162 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100755 misc/cronjobs/ordersClaim.pl diff --git a/misc/cronjobs/ordersClaim.pl b/misc/cronjobs/ordersClaim.pl new file mode 100755 index 0000000..3ce5643 --- /dev/null +++ b/misc/cronjobs/ordersClaim.pl @@ -0,0 +1,162 @@ +#!/usr/bin/perl + +# Copyright 2018 Biblibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +ordersClaim.pl - cron script that put late orders in message queue grouped by basket. + +=head1 SYNOPSIS + +./ordersClaim.pl --delay 40 --claimed-for 10 --max-claims 3 + +=head1 DESCRIPTION + +This script get all late orders (depending on --delay parameter, +group them by basket and put an entry in message_queue table. + +=cut + +use Modern::Perl; + +use Getopt::Long; +use Pod::Usage; + +use C4::Letters; +use C4::Acquisition; + +=head1 NAME + +ordersClaim.pl - cron script that put late orders in message queue grouped by basket. + +=head1 SYNOPSIS + +perl ordersClaim.pl + [ --delay|d ][ --claimed-for|c ][ --max-claims ] + +=head1 OPTIONS + +=over + +=item B<--help> + +Print a brief help message and exits. + +=item B<--delay> + +Number of days from which an orders is considered as late. +Default is the booseller's delivery time or 0. + +=item B<--claimed-for> + +Minimum number of days since the last claim. Default is 0. + +=item B<--max-claim> + +Number of claims beyond which we stop sending others. +Default is 1; + +=cut + +my $help; +my $delay; +my $claimed_for = 0; +my $max_claims = 1; + +GetOptions( + 'help|?' => \$help, + 'delay|d:i' => \$delay, + 'claimed-for|c:i' => \$claimed_for, + 'max-claims|m:i' => \$max_claims, +) or pod2usage(1); + +pod2usage(1) if $help; + +my @lateorders = GetLateOrders( ($delay) ); + +# Sort orders by basket +my $baskets_orders; +push @{ $baskets_orders->{ $_->{basketno} } }, $_->{ordernumber} for @lateorders; + +while (my ($basketno, $orderids) = each %$baskets_orders) { + my $schema = Koha::Database->new->schema; + + my $basket = $schema->resultset('Aqbookseller')->find( + { id => $basketno }, + { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); + + my $booksellerid = $basket->{booksellerid}; + my $bookseller = $schema->resultset('Aqbookseller')->find( + { id => $booksellerid }, + { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); + + my $contact = $schema->resultset('Aqcontact')->find( + { booksellerid => $booksellerid, claimacquisition => 1 }, + { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); + + unless ( defined( $delay ) ) { + $delay = $bookseller->{deliverytime} || 0; + } + + my $dbh = C4::Context->dbh; + + my $orderids_str = join(',', ('?') x @$orderids); + my $orders = $dbh->selectall_arrayref(qq{ + SELECT aqorders.*, aqbasket.*, biblio.* + FROM aqorders + LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber + LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno + WHERE aqorders.ordernumber IN ($orderids_str) + AND aqorders.claims_count < $max_claims + AND (aqorders.claimed_date IS NULL or aqorders.claimed_date <= DATE_SUB(CAST(now() AS date),INTERVAL $claimed_for DAY)) + }, {Slice => {}}, @$orderids); + + next unless @$orders; + + my $letter = GetPreparedLetter( + module => 'claimacquisition', + letter_code => 'ACQCLAIM', + message_transport_type => 'email', + tables => { + 'aqbooksellers' => $bookseller, + 'aqcontacts' => $contact, + 'aqbasket' => $basket + }, + repeat => $orders, + ) or next; + + my $admin_address = C4::Context->preference('KohaAdminEmailAddress'); + my $id = C4::Letters::EnqueueLetter({ + letter => $letter, + from_address => $admin_address, + to_address => $contact->{email}, + message_transport_type => $letter->{message_transport_type} + }); + + continue unless $id; + + my $today = Koha::DateUtils::dt_from_string; + foreach my $o ( @$orders ) { + my $order = $schema->resultset('Aqorder')->find($o->{ordernumber}); + my $count = $order->get_column('claims_count') || 0; + $order->set_columns({ + claimed_date => $today->ymd, + claims_count => $count+1 + })->update_or_insert; + } +} -- 2.1.4