View | Details | Raw Unified | Return to bug 20125
Collapse All | Expand All

(-)a/misc/cronjobs/ordersClaim.pl (-15 / +63 lines)
Lines 36-41 use Modern::Perl; Link Here
36
36
37
use Getopt::Long;
37
use Getopt::Long;
38
use Pod::Usage;
38
use Pod::Usage;
39
use Try::Tiny;
39
40
40
use C4::Letters qw( GetPreparedLetter EnqueueLetter );
41
use C4::Letters qw( GetPreparedLetter EnqueueLetter );
41
use Koha::Acquisition::Orders;
42
use Koha::Acquisition::Orders;
Lines 92-128 GetOptions( Link Here
92
93
93
pod2usage(1) if $help;
94
pod2usage(1) if $help;
94
95
95
my @lateorders = Koha::Acquisition::Orders->filter_by_lates( { delay => $delay } )->as_list;
96
sub get_late_orders {
97
    my ($delay) = @_;
96
98
97
# Sort orders by basket
99
    try {
98
my $baskets_orders;
100
        return Koha::Acquisition::Orders->filter_by_lates( { delay => $delay } )->as_list;
99
push @{ $baskets_orders->{ $_->basketno } }, $_->ordernumber for @lateorders;
101
    } catch {
102
        Koha::Exception->throw("Error getting late orders: $_");
103
        return [];
104
    };
105
}
100
106
101
while ( my ( $basketno, $orderids ) = each %$baskets_orders ) {
107
sub get_basket_orders {
102
    my $schema = Koha::Database->new->schema;
108
    my ($late_orders) = @_;
109
110
    my $baskets_orders = {};
111
    foreach my $order (@$late_orders) {
112
        push @{ $baskets_orders->{ $order->basketno } }, $order->ordernumber;
113
    }
114
    return $baskets_orders;
115
}
116
117
sub process_basket {
118
    my ( $basketno, $orderids, $max_claims, $claimed_for, $letter_code ) = @_;
103
119
120
    my $schema = Koha::Database->new->schema;
104
    my $basket = Koha::Acquisition::Baskets->find($basketno);
121
    my $basket = Koha::Acquisition::Baskets->find($basketno);
122
    unless ($basket) {
123
        Koha::Exception->throw("Basket not found for basketno: $basketno");
124
    }
105
125
106
    my $booksellerid = $basket->booksellerid;
126
    my $booksellerid = $basket->booksellerid;
107
    my $bookseller   = Koha::Acquisition::Booksellers->find($booksellerid);
127
    my $bookseller   = Koha::Acquisition::Booksellers->find($booksellerid);
128
    unless ($bookseller) {
129
        Koha::Exception->throw("Bookseller not found for booksellerid: $booksellerid");
130
    }
108
131
109
    my $contact = $schema->resultset('Aqcontact')->find(
132
    my $contact = $schema->resultset('Aqcontact')->find(
110
        { booksellerid => $booksellerid, claimacquisition => 1 },
133
        { booksellerid => $booksellerid, claimacquisition => 1 },
111
        { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }
134
        { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }
112
    );
135
    );
113
136
114
    unless ( defined($delay) ) {
137
    unless ($contact) {
115
        $delay = $bookseller->deliverytime || 0;
138
        Koha::Exception->throw("No contact found for booksellerid: $booksellerid");
116
    }
139
    }
117
140
141
    my $date_threshold = output_pref(
142
        {
143
            dt         => dt_from_string->subtract( days => $claimed_for ),
144
            dateformat => 'iso',
145
            dateonly   => 1
146
        }
147
    );
148
118
    my $orders        = [];
149
    my $orders        = [];
119
    my $order_objects = Koha::Acquisition::Orders->search( { ordernumber => { -in => $orderids } } );
150
    my $order_objects = Koha::Acquisition::Orders->search( { ordernumber => { -in => $orderids } } );
151
120
    while ( my $order = $order_objects->next ) {
152
    while ( my $order = $order_objects->next ) {
121
        my $claims_count = $order->claims_count;
153
        my $claims_count = $order->claims_count;
122
        my $last_claim   = dt_from_string( $order->claimed_date );
154
        my $last_claim   = $order->claimed_date;
123
155
124
        next if $claims_count > $max_claims;
156
        next if $claims_count > $max_claims;
125
        next if $last_claim && $last_claim > dt_from_string->subtract( days => $claimed_for );
157
        next if defined $last_claim && $last_claim > $date_threshold;
126
158
127
        my $result = {
159
        my $result = {
128
            %{ $order->unblessed },
160
            %{ $order->unblessed },
Lines 135-141 while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { Link Here
135
        push @$orders, $result;
167
        push @$orders, $result;
136
    }
168
    }
137
169
138
    next unless @$orders;
170
    unless (@$orders) {
171
        Koha::Exception->throw("No orders found for basketno: $basketno");
172
    }
139
173
140
    my $letter = GetPreparedLetter(
174
    my $letter = GetPreparedLetter(
141
        module                 => 'claimacquisition',
175
        module                 => 'claimacquisition',
Lines 147-153 while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { Link Here
147
            'aqbasket'      => $basket->unblessed
181
            'aqbasket'      => $basket->unblessed
148
        },
182
        },
149
        repeat => $orders,
183
        repeat => $orders,
150
    ) or next;
184
    );
185
186
    unless ($letter) {
187
        Koha::Exception->throw("Failed to prepare letter for basketno: $basketno");
188
    }
151
189
152
    my $from_address = C4::Context->preference('KohaAdminEmailAddress');
190
    my $from_address = C4::Context->preference('KohaAdminEmailAddress');
153
    if ( my $library = Koha::Libraries->find( $basket->branch ) ) {
191
    if ( my $library = Koha::Libraries->find( $basket->branch ) ) {
Lines 163-169 while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { Link Here
163
        }
201
        }
164
    );
202
    );
165
203
166
    continue unless $id;
204
    unless ($id) {
205
        Koha::Exception->throw("Failed to enqueue letter for basketno: $basketno");
206
    }
207
208
    foreach my $order (@$orders) {
209
        Koha::Acquisition::Orders->find( $order->{ordernumber} )->claim();
210
    }
211
}
212
213
my $late_orders    = get_late_orders($delay);
214
my $baskets_orders = get_basket_orders($late_orders);
167
215
168
    Koha::Acquisition::Orders->find( $_->{ordernumber} )->claim() for @$orders;
216
while ( my ( $basketno, $orderids ) = each %$baskets_orders ) {
217
    process_basket( $basketno, $orderids, $max_claims, $claimed_for, $letter_code );
169
}
218
}
170
- 

Return to bug 20125