Bug 22630: Update Koha::Schema::Result::CourseItem
[koha.git] / Koha / StockRotationStage.pm
blob59f38b5c62bc24092b84bc63a3a596de88a1a3e6
1 package Koha::StockRotationStage;
3 # Copyright PTFS Europe 2016
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use Koha::Database;
23 use Koha::Library;
24 use Koha::StockRotationRota;
26 use base qw(Koha::Object);
28 =head1 NAME
30 StockRotationStage - Koha StockRotationStage Object class
32 =head1 SYNOPSIS
34 StockRotationStage class used primarily by stockrotation .pls and the stock
35 rotation cron script.
37 =head1 DESCRIPTION
39 Standard Koha::Objects definitions, and additional methods.
41 =head1 API
43 =head2 Class Methods
45 =cut
47 =head3 _type
49 =cut
51 sub _type {
52 return 'Stockrotationstage';
55 sub _relation {
56 my ( $self, $method, $type ) = @_;
57 return sub {
58 my $rs = $self->_result->$method;
59 return 0 if !$rs;
60 my $namespace = 'Koha::' . $type;
61 return $namespace->_new_from_dbic( $rs );
65 =head3 stockrotationitems
67 my $stages = Koha::StockRotationStage->stockrotationitems;
69 Returns the items associated with the current stage.
71 =cut
73 sub stockrotationitems {
74 my ( $self ) = @_;
75 return &{$self->_relation(qw/ stockrotationitems StockRotationItems /)};
78 =head3 branchcode
80 my $branch = Koha::StockRotationStage->branchcode;
82 Returns the branch associated with the current stage.
84 =cut
86 sub branchcode {
87 my ( $self ) = @_;
88 return &{$self->_relation(qw/ branchcode Library /)};
91 =head3 rota
93 my $rota = Koha::StockRotationStage->rota;
95 Returns the rota associated with the current stage.
97 =cut
99 sub rota {
100 my ( $self ) = @_;
101 return &{$self->_relation(qw/ rota StockRotationRota /)};
104 =head3 siblings
106 my $siblings = $stage->siblings;
108 Koha::Object wrapper around DBIx::Class::Ordered.
110 =cut
112 sub siblings {
113 my ( $self ) = @_;
114 return &{$self->_relation(qw/ siblings StockRotationStages /)};
117 =head3 next_siblings
119 my $next_siblings = $stage->next_siblings;
121 Koha::Object wrapper around DBIx::Class::Ordered.
123 =cut
125 sub next_siblings {
126 my ( $self ) = @_;
127 return &{$self->_relation(qw/ next_siblings StockRotationStages /)};
130 =head3 previous_siblings
132 my $previous_siblings = $stage->previous_siblings;
134 Koha::Object wrapper around DBIx::Class::Ordered.
136 =cut
138 sub previous_siblings {
139 my ( $self ) = @_;
140 return &{$self->_relation(qw/ previous_siblings StockRotationStages /)};
143 =head3 next_sibling
145 my $next = $stage->next_sibling;
147 Koha::Object wrapper around DBIx::Class::Ordered.
149 =cut
151 sub next_sibling {
152 my ( $self ) = @_;
153 return &{$self->_relation(qw/ next_sibling StockRotationStage /)};
156 =head3 previous_sibling
158 my $previous = $stage->previous_sibling;
160 Koha::Object Wrapper around DBIx::Class::Ordered.
162 =cut
164 sub previous_sibling {
165 my ( $self ) = @_;
166 return &{$self->_relation(qw/ previous_sibling StockRotationStage /)};
169 =head3 first_sibling
171 my $first = $stage->first_sibling;
173 Koha::Object Wrapper around DBIx::Class::Ordered.
175 =cut
177 sub first_sibling {
178 my ( $self ) = @_;
179 return &{$self->_relation(qw/ first_sibling StockRotationStage /)};
182 =head3 last_sibling
184 my $last = $stage->last_sibling;
186 Koha::Object Wrapper around DBIx::Class::Ordered.
188 =cut
190 sub last_sibling {
191 my ( $self ) = @_;
192 return &{$self->_relation(qw/ last_sibling StockRotationStage /)};
195 =head3 move_previous
197 1|0 = $stage->move_previous;
199 Koha::Object Wrapper around DBIx::Class::Ordered.
201 =cut
203 sub move_previous {
204 my ( $self ) = @_;
205 return $self->_result->move_previous;
208 =head3 move_next
210 1|0 = $stage->move_next;
212 Koha::Object Wrapper around DBIx::Class::Ordered.
214 =cut
216 sub move_next {
217 my ( $self ) = @_;
218 return $self->_result->move_next;
221 =head3 move_first
223 1|0 = $stage->move_first;
225 Koha::Object Wrapper around DBIx::Class::Ordered.
227 =cut
229 sub move_first {
230 my ( $self ) = @_;
231 return $self->_result->move_first;
234 =head3 move_last
236 1|0 = $stage->move_last;
238 Koha::Object Wrapper around DBIx::Class::Ordered.
240 =cut
242 sub move_last {
243 my ( $self ) = @_;
244 return $self->_result->move_last;
247 =head3 move_to
249 1|0 = $stage->move_to($position);
251 Koha::Object Wrapper around DBIx::Class::Ordered.
253 =cut
255 sub move_to {
256 my ( $self, $position ) = @_;
257 return $self->_result->move_to($position)
258 if ( $position le $self->rota->stockrotationstages->count );
259 return 0;
262 =head3 move_to_group
264 1|0 = $stage->move_to_group($rota_id, [$position]);
266 Koha::Object Wrapper around DBIx::Class::Ordered.
268 =cut
270 sub move_to_group {
271 my ( $self, $rota_id, $position ) = @_;
272 return $self->_result->move_to_group($rota_id, $position);
275 =head3 delete
277 1|0 = $stage->delete;
279 Koha::Object Wrapper around DBIx::Class::Ordered.
281 =cut
283 sub delete {
284 my ( $self ) = @_;
285 return $self->_result->delete;
288 =head3 investigate
290 my $report = $stage->investigate($report_so_far);
292 Return a stage based report. This report will mutate and augment the report
293 that is passed to it. It slots item reports into the branched and temporary
294 rota sections of the report. It also increments a number of counters.
296 For details of intent and context of this procedure, please see
297 Koha::StockRotationRota->investigate.
299 =cut
301 sub investigate {
302 my ( $self, $report ) = @_;
303 my $new_stage = $self->next_sibling;
304 my $duration = $self->duration;
305 # Generate stage items report
306 my $items_report = $self->stockrotationitems->investigate;
308 # Merge into general report
310 ## Branched indexes
311 ### The branched indexes work as follows:
312 ### - They contain information about the relevant branch
313 ### - They contain an index of actionable items for that branch
314 ### - They contain an index of non-actionable items for that branch
316 ### Items are assigned to a particular branched index as follows:
317 ### - 'advanceable' : assigned to branch of the current stage
318 ### (this should also be the current holding branch)
319 ### - 'log' items are always assigned to branch of current stage.
320 ### - 'indemand' : assigned to branch of current stage
321 ### (this should also be the current holding branch)
322 ### - 'initiable' : assigned to the current holding branch of item
323 ### - 'repatriable' : assigned to the current holding branch of item
325 ### 'Advanceable', 'log', 'indemand':
327 # Set up our stage branch info.
328 my $stagebranch = $self->_result->branchcode;
329 my $stagebranchcode = $stagebranch->branchcode;
331 # Initiate our stage branch index if it does not yet exist.
332 if ( !$report->{branched}->{$stagebranchcode} ) {
333 $report->{branched}->{$stagebranchcode} = {
334 code => $stagebranchcode,
335 name => $stagebranch->branchname,
336 email => $stagebranch->branchreplyto
337 ? $stagebranch->branchreplyto
338 : $stagebranch->branchemail,
339 phone => $stagebranch->branchphone,
340 items => [],
341 log => [],
345 push @{$report->{branched}->{$stagebranchcode}->{items}},
346 @{$items_report->{advanceable_items}};
347 push @{$report->{branched}->{$stagebranchcode}->{log}},
348 @{$items_report->{log}};
349 push @{$report->{branched}->{$stagebranchcode}->{items}},
350 @{$items_report->{indemand_items}};
352 ### 'Initiable' & 'Repatriable'
353 foreach my $ireport (@{$items_report->{initiable_items}}) {
354 my $branch = $ireport->{branch};
355 my $branchcode = $branch->branchcode;
356 if ( !$report->{branched}->{$branchcode} ) {
357 $report->{branched}->{$branchcode} = {
358 code => $branchcode,
359 name => $branch->branchname,
360 email => $stagebranch->branchreplyto
361 ? $stagebranch->branchreplyto
362 : $stagebranch->branchemail,
363 phone => $branch->branchphone,
364 items => [],
365 log => [],
368 push @{$report->{branched}->{$branchcode}->{items}}, $ireport;
371 foreach my $ireport (@{$items_report->{repatriable_items}}) {
372 my $branch = $ireport->{branch};
373 my $branchcode = $branch->branchcode;
374 if ( !$report->{branched}->{$branchcode} ) {
375 $report->{branched}->{$branchcode} = {
376 code => $branchcode,
377 name => $branch->branchname,
378 email => $stagebranch->branchreplyto
379 ? $stagebranch->branchreplyto
380 : $stagebranch->branchemail,
381 phone => $branch->branchphone,
382 items => [],
383 log => [],
386 push @{$report->{branched}->{$branchcode}->{items}}, $ireport;
389 ## Per rota indexes
390 ### Per rota indexes are item reports pushed into the index for the
391 ### current rota. We don't know where that index is yet as we don't know
392 ### about the current rota. To resolve this we assign our items and log
393 ### to tmp indexes. They will be merged into the proper rota index at the
394 ### rota level.
395 push @{$report->{tmp_items}}, @{$items_report->{items}};
396 push @{$report->{tmp_log}}, @{$items_report->{log}};
398 ## Collection of items
399 ### Finally we just add our collection of items to the full item index.
400 push @{$report->{items}}, @{$items_report->{items}};
402 ## Assemble counters
403 $report->{actionable} += $items_report->{actionable};
404 $report->{indemand} += scalar @{$items_report->{indemand_items}};
405 $report->{advanceable} += scalar @{$items_report->{advanceable_items}};
406 $report->{initiable} += scalar @{$items_report->{initiable_items}};
407 $report->{repatriable} += scalar @{$items_report->{repatriable_items}};
408 $report->{stationary} += scalar @{$items_report->{log}};
410 return $report;
415 =head1 AUTHOR
417 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
419 =cut