Bug 12159: (QA follow-up) Rename relation in Patron::Attributes
[koha.git] / Koha / StockRotationRota.pm
1 package Koha::StockRotationRota;
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 Koha::Database;
23 use Koha::StockRotationStages;
24 use Koha::StockRotationItem;
25 use Koha::StockRotationItems;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 StockRotationRota - Koha StockRotationRota Object class
32
33 =head1 SYNOPSIS
34
35 StockRotationRota class used primarily by stockrotation .pls and the stock
36 rotation cron script.
37
38 =head1 DESCRIPTION
39
40 Standard Koha::Objects definitions, and additional methods.
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =cut
47
48 =head3 stockrotationstages
49
50   my $stages = Koha::StockRotationRota->stockrotationstages;
51
52 Returns the stages associated with the current rota.
53
54 =cut
55
56 sub stockrotationstages {
57     my ( $self ) = @_;
58     my $rs = $self->_result->stockrotationstages;
59     return Koha::StockRotationStages->_new_from_dbic( $rs );
60 }
61
62 =head3 add_item
63
64   my $rota = $rota->add_item($itemnumber);
65
66 Add item identified by $ITEMNUMBER to this rota, which means we associate it
67 with the first stage of this rota.  Should the item already be associated with
68 a rota, move it from that rota to this rota.
69
70 =cut
71
72 sub add_item {
73     my ( $self, $itemnumber ) = @_;
74     my $sritem = Koha::StockRotationItems->find($itemnumber);
75     if ($sritem) {
76         $sritem->stage_id($self->first_stage->stage_id)
77             ->indemand(0)->fresh(1)->store;
78     } else {
79         $sritem = Koha::StockRotationItem->new({
80             itemnumber_id => $itemnumber,
81             stage_id      => $self->first_stage->stage_id,
82             indemand      => 0,
83             fresh         => 1,
84         })->store;
85     }
86     return $self;
87 }
88
89 =head3 first_stage
90
91   my $stage = $rota->first_stage;
92
93 Return the first stage attached to this rota (the one that has an undefined
94 `stagebefore`).
95
96 =cut
97
98 sub first_stage {
99     my ( $self ) = @_;
100     my $guess = $self->stockrotationstages->next;
101     my $stage = $guess->first_sibling;
102     return ( $stage ) ? $stage : $guess;
103 }
104
105 =head3 stockrotationitems
106
107   my $items = $rota->stockrotationitems;
108
109 Return all items associated with this rota via its stages.
110
111 =cut
112
113 sub stockrotationitems {
114     my ( $self ) = @_;
115     my $rs = Koha::StockRotationItems->search(
116         { 'stage.rota_id' => $self->rota_id }, { join =>  [ qw/stage/ ] }
117     );
118     return $rs;
119 }
120
121 =head3 investigate
122
123   my $report = $rota->investigate($report_so_far);
124
125 Aim here is to return $report augmented with content for this rota.  We
126 delegate to $stage->investigate.
127
128 The report will include some basic information and 2 primary reports:
129
130 - per rota report in 'rotas'. This report is mainly used by admins to do check
131   & compare results.
132
133 - branched report in 'branched'.  This is the workhorse: emails to libraries
134   are compiled from these reports, and they will have the actionable work.
135
136 Both reports are generated in stage based investigations; the rota report is
137 then glued into place at this stage.
138
139 =cut
140
141 sub investigate {
142     my ( $self, $report ) = @_;
143     my $count = $self->stockrotationitems->count;
144     $report->{sum_items} += $count;
145
146     if ( $self->active ) {
147         $report->{rotas_active}++;
148         # stockrotationstages->investigate augments $report with the stage's
149         # content.  This is how 'branched' slowly accumulates all items.
150         $report = $self->stockrotationstages->investigate($report);
151         # Add our rota report to the full report.
152         push @{$report->{rotas}}, {
153             name  => $self->title,
154             id    => $self->rota_id,
155             items => $report->{tmp_items} || [],
156             log   => $report->{tmp_log} || [],
157         };
158         delete $report->{tmp_items};
159         delete $report->{tmp_log};
160     } else {                    # Rota is not active.
161         $report->{rotas_inactive}++;
162         $report->{items_inactive} += $count;
163     }
164
165     return $report;
166 }
167
168 =head3 _type
169
170 =cut
171
172 sub _type {
173     return 'Stockrotationrota';
174 }
175
176 1;
177
178 =head1 AUTHOR
179
180 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
181
182 =cut