|
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 to automatically generate late order claims in the 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 qw( GetPreparedLetter EnqueueLetter ); |
| 41 |
use Koha::Acquisition::Orders; |
| 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 |
=back |
| 75 |
|
| 76 |
=cut |
| 77 |
|
| 78 |
my $help; |
| 79 |
my $delay; |
| 80 |
my $claimed_for = 0; |
| 81 |
my $max_claims = 1; |
| 82 |
my $letter_code = 'ACQCLAIM'; |
| 83 |
|
| 84 |
GetOptions( |
| 85 |
'help|?' => \$help, |
| 86 |
'delay|d:i' => \$delay, |
| 87 |
'claimed-for|c:i' => \$claimed_for, |
| 88 |
'max-claims|m:i' => \$max_claims, |
| 89 |
'letter-code|l:s' => \$letter_code, |
| 90 |
) or pod2usage(1); |
| 91 |
|
| 92 |
pod2usage(1) if $help; |
| 93 |
|
| 94 |
my @lateorders = Koha::Acquisition::Orders->filter_by_lates( { delay => $delay } )->as_list; |
| 95 |
|
| 96 |
# Sort orders by basket |
| 97 |
my $baskets_orders; |
| 98 |
push @{ $baskets_orders->{ $_->basketno } }, $_->ordernumber for @lateorders; |
| 99 |
|
| 100 |
while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { |
| 101 |
my $schema = Koha::Database->new->schema; |
| 102 |
|
| 103 |
my $basket = Koha::Acquisition::Baskets->find($basketno); |
| 104 |
|
| 105 |
my $booksellerid = $basket->booksellerid; |
| 106 |
my $bookseller = Koha::Acquisition::Booksellers->find($booksellerid); |
| 107 |
|
| 108 |
my $contact = $schema->resultset('Aqcontact')->find( |
| 109 |
{ booksellerid => $booksellerid, claimacquisition => 1 }, |
| 110 |
{ result_class => 'DBIx::Class::ResultClass::HashRefInflator' } |
| 111 |
); |
| 112 |
|
| 113 |
unless ( defined($delay) ) { |
| 114 |
$delay = $bookseller->deliverytime || 0; |
| 115 |
} |
| 116 |
|
| 117 |
my $dbh = C4::Context->dbh; |
| 118 |
|
| 119 |
my $orderids_str = join( ',', ('?') x @$orderids ); |
| 120 |
my $orders = $dbh->selectall_arrayref( |
| 121 |
qq{ |
| 122 |
SELECT aqorders.*, aqbasket.*, biblio.*, |
| 123 |
COUNT(aqorders_claims.claimed_on) AS claims_count, |
| 124 |
MAX(aqorders_claims.claimed_on) AS last_claim |
| 125 |
FROM aqorders |
| 126 |
LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber |
| 127 |
LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno |
| 128 |
LEFT JOIN aqorders_claims ON aqorders.ordernumber = aqorders_claims.ordernumber |
| 129 |
WHERE aqorders.ordernumber IN ($orderids_str) |
| 130 |
HAVING claims_count <= $max_claims |
| 131 |
AND (last_claim IS NULL or last_claim <= DATE_SUB(CAST(now() AS date),INTERVAL $claimed_for DAY)) |
| 132 |
}, { Slice => {} }, @$orderids |
| 133 |
); |
| 134 |
|
| 135 |
next unless @$orders; |
| 136 |
|
| 137 |
my $letter = GetPreparedLetter( |
| 138 |
module => 'claimacquisition', |
| 139 |
letter_code => $letter_code, |
| 140 |
message_transport_type => 'email', |
| 141 |
tables => { |
| 142 |
'aqbooksellers' => $bookseller->unblessed, |
| 143 |
'aqcontacts' => $contact, |
| 144 |
'aqbasket' => $basket->unblessed |
| 145 |
}, |
| 146 |
repeat => $orders, |
| 147 |
) or next; |
| 148 |
|
| 149 |
my $from_address = C4::Context->preference('KohaAdminEmailAddress'); |
| 150 |
if ( my $library = Koha::Libraries->find( $basket->branch ) ) { |
| 151 |
$from_address = $library->branchemail || $from_address; |
| 152 |
} |
| 153 |
|
| 154 |
my $id = C4::Letters::EnqueueLetter( |
| 155 |
{ |
| 156 |
letter => $letter, |
| 157 |
from_address => $from_address, |
| 158 |
to_address => $contact->{email}, |
| 159 |
message_transport_type => $letter->{message_transport_type} |
| 160 |
} |
| 161 |
); |
| 162 |
|
| 163 |
continue unless $id; |
| 164 |
|
| 165 |
Koha::Acquisition::Orders->find( $_->{ordernumber} )->claim() for @$orders; |
| 166 |
} |