|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2018 Biblibre |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
=head1 NAME |
| 21 |
|
| 22 |
ordersClaim.pl - cron script that put late orders in message queue grouped by basket. |
| 23 |
|
| 24 |
=head1 SYNOPSIS |
| 25 |
|
| 26 |
./ordersClaim.pl --delay 40 --claimed-for 10 --max-claims 3 |
| 27 |
|
| 28 |
=head1 DESCRIPTION |
| 29 |
|
| 30 |
This script get all late orders (depending on --delay parameter, |
| 31 |
group them by basket and put an entry in message_queue table. |
| 32 |
|
| 33 |
=cut |
| 34 |
|
| 35 |
use Modern::Perl; |
| 36 |
|
| 37 |
use Getopt::Long; |
| 38 |
use Pod::Usage; |
| 39 |
|
| 40 |
use C4::Letters; |
| 41 |
use C4::Acquisition; |
| 42 |
|
| 43 |
=head1 NAME |
| 44 |
|
| 45 |
ordersClaim.pl - cron script that put late orders in message queue grouped by basket. |
| 46 |
|
| 47 |
=head1 SYNOPSIS |
| 48 |
|
| 49 |
perl ordersClaim.pl |
| 50 |
[ --delay|d <number of days> ][ --claimed-for|c <number of days> ][ --max-claims <number of claims> ] |
| 51 |
|
| 52 |
=head1 OPTIONS |
| 53 |
|
| 54 |
=over |
| 55 |
|
| 56 |
=item B<--help> |
| 57 |
|
| 58 |
Print a brief help message and exits. |
| 59 |
|
| 60 |
=item B<--delay> |
| 61 |
|
| 62 |
Number of days from which an orders is considered as late. |
| 63 |
Default is the booseller's delivery time or 0. |
| 64 |
|
| 65 |
=item B<--claimed-for> |
| 66 |
|
| 67 |
Minimum number of days since the last claim. Default is 0. |
| 68 |
|
| 69 |
=item B<--max-claim> |
| 70 |
|
| 71 |
Number of claims beyond which we stop sending others. |
| 72 |
Default is 1; |
| 73 |
|
| 74 |
=cut |
| 75 |
|
| 76 |
my $help; |
| 77 |
my $delay; |
| 78 |
my $claimed_for = 0; |
| 79 |
my $max_claims = 1; |
| 80 |
|
| 81 |
GetOptions( |
| 82 |
'help|?' => \$help, |
| 83 |
'delay|d:i' => \$delay, |
| 84 |
'claimed-for|c:i' => \$claimed_for, |
| 85 |
'max-claims|m:i' => \$max_claims, |
| 86 |
) or pod2usage(1); |
| 87 |
|
| 88 |
pod2usage(1) if $help; |
| 89 |
|
| 90 |
my @lateorders = GetLateOrders( ($delay) ); |
| 91 |
|
| 92 |
# Sort orders by basket |
| 93 |
my $baskets_orders; |
| 94 |
push @{ $baskets_orders->{ $_->{basketno} } }, $_->{ordernumber} for @lateorders; |
| 95 |
|
| 96 |
while (my ($basketno, $orderids) = each %$baskets_orders) { |
| 97 |
my $schema = Koha::Database->new->schema; |
| 98 |
|
| 99 |
my $basket = $schema->resultset('Aqbookseller')->find( |
| 100 |
{ id => $basketno }, |
| 101 |
{ result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); |
| 102 |
|
| 103 |
my $booksellerid = $basket->{booksellerid}; |
| 104 |
my $bookseller = $schema->resultset('Aqbookseller')->find( |
| 105 |
{ id => $booksellerid }, |
| 106 |
{ result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); |
| 107 |
|
| 108 |
my $contact = $schema->resultset('Aqcontact')->find( |
| 109 |
{ booksellerid => $booksellerid, claimacquisition => 1 }, |
| 110 |
{ result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); |
| 111 |
|
| 112 |
unless ( defined( $delay ) ) { |
| 113 |
$delay = $bookseller->{deliverytime} || 0; |
| 114 |
} |
| 115 |
|
| 116 |
my $dbh = C4::Context->dbh; |
| 117 |
|
| 118 |
my $orderids_str = join(',', ('?') x @$orderids); |
| 119 |
my $orders = $dbh->selectall_arrayref(qq{ |
| 120 |
SELECT aqorders.*, aqbasket.*, biblio.* |
| 121 |
FROM aqorders |
| 122 |
LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber |
| 123 |
LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno |
| 124 |
WHERE aqorders.ordernumber IN ($orderids_str) |
| 125 |
AND aqorders.claims_count < $max_claims |
| 126 |
AND (aqorders.claimed_date IS NULL or aqorders.claimed_date <= DATE_SUB(CAST(now() AS date),INTERVAL $claimed_for DAY)) |
| 127 |
}, {Slice => {}}, @$orderids); |
| 128 |
|
| 129 |
next unless @$orders; |
| 130 |
|
| 131 |
my $letter = GetPreparedLetter( |
| 132 |
module => 'claimacquisition', |
| 133 |
letter_code => 'ACQCLAIM', |
| 134 |
message_transport_type => 'email', |
| 135 |
tables => { |
| 136 |
'aqbooksellers' => $bookseller, |
| 137 |
'aqcontacts' => $contact, |
| 138 |
'aqbasket' => $basket |
| 139 |
}, |
| 140 |
repeat => $orders, |
| 141 |
) or next; |
| 142 |
|
| 143 |
my $admin_address = C4::Context->preference('KohaAdminEmailAddress'); |
| 144 |
my $id = C4::Letters::EnqueueLetter({ |
| 145 |
letter => $letter, |
| 146 |
from_address => $admin_address, |
| 147 |
to_address => $contact->{email}, |
| 148 |
message_transport_type => $letter->{message_transport_type} |
| 149 |
}); |
| 150 |
|
| 151 |
continue unless $id |
| 152 |
|
| 153 |
my $today = Koha::DateUtils::dt_from_string; |
| 154 |
foreach my $o ( @$orders ) { |
| 155 |
my $order = $schema->resultset('Aqorder')->find($o->{ordernumber}); |
| 156 |
my $count = $order->get_column('claims_count') || 0; |
| 157 |
$order->set_columns({ |
| 158 |
claimed_date => $today->ymd, |
| 159 |
claims_count => $count+1 |
| 160 |
})->update_or_insert; |
| 161 |
} |
| 162 |
} |