Bug 11897: Stockrotation
[koha.git] / Koha / StockRotationItem.pm
1 package Koha::StockRotationItem;
2
3 # Copyright PTFS Europe 2016
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use DateTime;
23 use DateTime::Duration;
24 use Koha::Database;
25 use Koha::DateUtils qw/dt_from_string/;
26 use Koha::Item::Transfer;
27 use Koha::Item;
28 use Koha::StockRotationStage;
29
30 use base qw(Koha::Object);
31
32 =head1 NAME
33
34 StockRotationItem - Koha StockRotationItem Object class
35
36 =head1 SYNOPSIS
37
38 StockRotationItem class used primarily by stockrotation .pls and the stock
39 rotation cron script.
40
41 =head1 DESCRIPTION
42
43 Standard Koha::Objects definitions, and additional methods.
44
45 =head1 API
46
47 =head2 Class Methods
48
49 =cut
50
51 =head3 _type
52
53 =cut
54
55 sub _type {
56     return 'Stockrotationitem';
57 }
58
59 =head3 itemnumber
60
61   my $item = Koha::StockRotationItem->itemnumber;
62
63 Returns the item associated with the current stock rotation item.
64
65 =cut
66
67 sub itemnumber {
68     my ( $self ) = @_;
69     my $rs = $self->_result->itemnumber;
70     return Koha::Item->_new_from_dbic( $rs );
71 }
72
73 =head3 stage
74
75   my $stage = Koha::StockRotationItem->stage;
76
77 Returns the stage associated with the current stock rotation item.
78
79 =cut
80
81 sub stage {
82     my ( $self ) = @_;
83     my $rs = $self->_result->stage;
84     return Koha::StockRotationStage->_new_from_dbic( $rs );
85 }
86
87 =head3 needs_repatriating
88
89   1|0 = $item->needs_repatriating;
90
91 Return 1 if this item is currently not at the library it should be at
92 according to our stockrotation plan.
93
94 =cut
95
96 sub needs_repatriating {
97     my ( $self ) = @_;
98     my ( $item, $stage ) = ( $self->itemnumber, $self->stage );
99     if ( $self->itemnumber->get_transfer ) {
100         return 0;               # We're in transit.
101     } elsif ( $item->holdingbranch ne $stage->branchcode_id
102                   || $item->homebranch ne $stage->branchcode_id ) {
103         return 1;               # We're not where we should be.
104     } else {
105         return 0;               # We're at home.
106     }
107 }
108
109 =head3 needs_advancing
110
111   1|0 = $item->needs_advancing;
112
113 Return 1 if this item is ready to be moved on to the next stage in its rota.
114
115 =cut
116
117 sub needs_advancing {
118     my ( $self ) = @_;
119     return 0 if $self->itemnumber->get_transfer; # intransfer: don't advance.
120     return 1 if $self->fresh;                    # Just on rota: advance.
121     my $completed = $self->itemnumber->_result->branchtransfers->search(
122         { 'comments'    => "StockrotationAdvance" },
123         { order_by => { -desc => 'datearrived' } }
124     );
125     # Do maths on whether we need to be moved on.
126     if ( $completed->count ) {
127         my $arrival = dt_from_string(
128             $completed->next->datearrived, 'iso'
129         );
130         my $duration = DateTime::Duration
131             ->new( days => $self->stage->duration );
132         if ( $arrival + $duration le DateTime->now ) {
133             return 1;
134         } else {
135             return 0;
136         }
137     } else {
138         die "We have no historical branch transfer; this should not have happened!";
139     }
140 }
141
142 =head3 repatriate
143
144   1|0 = $sritem->repatriate
145
146 Put this item into branch transfer with 'StockrotationCorrection' comment, so
147 that it may return to it's stage.branch to continue its rota as normal.
148
149 =cut
150
151 sub repatriate {
152     my ( $self, $msg ) = @_;
153     # Create the transfer.
154     my $transfer_stored = Koha::Item::Transfer->new({
155         'itemnumber' => $self->itemnumber_id,
156         'frombranch' => $self->itemnumber->holdingbranch,
157         'tobranch'   => $self->stage->branchcode_id,
158         'datesent'   => DateTime->now,
159         'comments'   => $msg || "StockrotationRepatriation",
160     })->store;
161     $self->itemnumber->homebranch($self->stage->branchcode_id)->store;
162     return $transfer_stored;
163 }
164
165 =head3 advance
166
167   1|0 = $sritem->advance;
168
169 Put this item into branch transfer with 'StockrotationAdvance' comment, to
170 transfer it to the next stage in its rota.
171
172 If this is the last stage in the rota and this rota is cyclical, we return to
173 the first stage.  If it is not cyclical, then we delete this
174 StockRotationItem.
175
176 If this item is 'indemand', and advance is invoked, we disable 'indemand' and
177 advance the item as per usual.
178
179 =cut
180
181 sub advance {
182     my ( $self ) = @_;
183     my $item = $self->itemnumber;
184     my $transfer = Koha::Item::Transfer->new({
185         'itemnumber' => $self->itemnumber_id,
186         'frombranch' => $item->holdingbranch,
187         'datesent'   => DateTime->now,
188         'comments'   => "StockrotationAdvance"
189     });
190
191     if ( $self->indemand && !$self->fresh ) {
192         $self->indemand(0)->store;  # De-activate indemand
193         $transfer->tobranch($self->stage->branchcode_id);
194         $transfer->datearrived(DateTime->now);
195     } else {
196         # Find and update our stage.
197         my $stage = $self->stage;
198         my $new_stage;
199         if ( $self->fresh ) {   # Just added to rota
200             $new_stage = $self->stage->first_sibling || $self->stage;
201             $transfer->tobranch($new_stage->branchcode_id);
202             $transfer->datearrived(DateTime->now) # Already at first branch
203                 if $item->holdingbranch eq $new_stage->branchcode_id;
204             $self->fresh(0)->store;         # Reset fresh
205         } elsif ( !$stage->last_sibling ) { # Last stage
206             if ( $stage->rota->cyclical ) { # Cyclical rota?
207                 # Revert to first stage.
208                 $new_stage = $stage->first_sibling || $stage;
209                 $transfer->tobranch($new_stage->branchcode_id);
210                 $transfer->datearrived(DateTime->now);
211             } else {
212                 $self->delete;  # StockRotationItem is done.
213                 return 1;
214             }
215         } else {
216             # Just advance.
217             $new_stage = $self->stage->next_sibling;
218         }
219         $self->stage_id($new_stage->stage_id)->store;        # Set new stage
220         $item->homebranch($new_stage->branchcode_id)->store; # Update homebranch
221         $transfer->tobranch($new_stage->branchcode_id);      # Send to new branch
222     }
223
224     return $transfer->store;
225 }
226
227 =head3 investigate
228
229   my $report = $item->investigate;
230
231 Return the base set of information, namely this individual item's report, for
232 generating stockrotation reports about this stockrotationitem.
233
234 =cut
235
236 sub investigate {
237     my ( $self ) = @_;
238     my $item_report = {
239         title      => $self->itemnumber->_result->biblioitem
240             ->biblionumber->title,
241         author     => $self->itemnumber->_result->biblioitem
242             ->biblionumber->author,
243         callnumber => $self->itemnumber->itemcallnumber,
244         location   => $self->itemnumber->location,
245         onloan     => $self->itemnumber->onloan,
246         barcode    => $self->itemnumber->barcode,
247         itemnumber => $self->itemnumber_id,
248         branch => $self->itemnumber->_result->holdingbranch,
249         object => $self,
250     };
251     my $reason;
252     if ( $self->fresh ) {
253         $reason = 'initiation';
254     } elsif ( $self->needs_repatriating ) {
255         $reason = 'repatriation';
256     } elsif ( $self->needs_advancing ) {
257         $reason = 'advancement';
258         $reason = 'in-demand' if $self->indemand;
259     } else {
260         $reason = 'not-ready';
261     }
262     $item_report->{reason} = $reason;
263
264     return $item_report;
265 }
266
267 1;
268
269 =head1 AUTHOR
270
271 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
272
273 =cut