Bug 26145: (QA follow-up) Add missing filters
[koha.git] / Koha / Util / StockRotation.pm
blob858d2cb2d3a85a7fe4b2d73c814fbb70927e7e29
1 package Koha::Util::StockRotation;
3 # Module contains subroutines used with Stock Rotation
5 # Copyright 2016 PTFS-Europe Ltd
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Modern::Perl;
24 use Koha::Items;
25 use Koha::StockRotationItems;
26 use Koha::Database;
28 our ( @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
29 BEGIN {
30 require Exporter;
31 @ISA = qw( Exporter );
32 @EXPORT = qw( );
33 @EXPORT_OK = qw(
34 get_branches
35 get_stages
36 toggle_indemand
37 remove_from_stage
38 get_barcodes_status
39 add_items_to_rota
40 move_to_next_stage
42 %EXPORT_TAGS = ( ALL => [ @EXPORT_OK, @EXPORT ] );
45 =head1 NAME
47 Koha::Util::StockRotation - utility class with routines for Stock Rotation
49 =head1 FUNCTIONS
51 =head2 get_branches
53 returns all branches ordered by branchname as an array, each element
54 contains a hashref containing branch details
56 =cut
58 sub get_branches {
60 return Koha::Libraries->search(
61 {},
62 { order_by => ['branchname'] }
63 )->unblessed;
67 =head2 get_stages
69 returns an arrayref of StockRotationStage objects representing
70 all stages for a passed rota
72 =cut
74 sub get_stages {
76 my $rota = shift;
78 my @out = ();
80 if ($rota->stockrotationstages->count > 0) {
82 push @out, $rota->first_stage->unblessed;
84 push @out, @{$rota->first_stage->siblings->unblessed};
88 return \@out;
91 =head2 toggle_indemand
93 given an item's ID & stage ID toggle that item's in_demand
94 status on that stage
96 =cut
98 sub toggle_indemand {
100 my ($item_id, $stage_id) = @_;
102 # Get the item object
103 my $item = Koha::StockRotationItems->find(
105 itemnumber_id => $item_id,
106 stage_id => $stage_id
110 # Toggle the item's indemand flag
111 my $new_indemand = ($item->indemand == 1) ? 0 : 1;
113 $item->indemand($new_indemand)->store;
117 =head2 move_to_next_stage
119 given an item's ID and stage ID, move it
120 to the next stage on the rota
122 =cut
124 sub move_to_next_stage {
126 my ($item_id, $stage_id) = shift;
128 # Get the item object
129 my $item = Koha::StockRotationItems->find(
131 itemnumber_id => $item_id,
132 stage_id => $stage_id
136 $item->advance;
140 =head2 remove_from_stage
142 given an item's ID & stage ID, remove that item from that stage
144 =cut
146 sub remove_from_stage {
148 my ($item_id, $stage_id) = @_;
150 # Get the item object and delete it
151 Koha::StockRotationItems->find(
153 itemnumber_id => $item_id,
154 stage_id => $stage_id
156 )->delete;
160 =head2 get_barcodes_status
162 take an arrayref of barcodes and a status hashref and populate it
164 =cut
166 sub get_barcodes_status {
168 my ($rota_id, $barcodes, $status) = @_;
170 # Get the items associated with these barcodes
171 my $items = Koha::Items->search(
173 barcode => { '-in' => $barcodes }
176 prefetch => 'stockrotationitem'
179 # Get an array of barcodes that were found
180 # Assign each barcode's status
181 my @found = ();
182 while (my $item = $items->next) {
184 push @found, $item->barcode if $item->barcode;
186 # Check if it's on a rota
187 my $on_rota = $item->stockrotationitem;
189 # It is on a rota
190 if ($on_rota) {
192 # Check if it's on this rota
193 if ($on_rota->stage->rota->rota_id == $rota_id) {
195 # It's on this rota
196 push @{$status->{on_this}}, $item;
198 } else {
200 # It's on another rota
201 push @{$status->{on_other}}, $item;
205 } else {
207 # Item is not on a rota
208 push @{$status->{ok}}, $item;
214 # Create an array of barcodes supplied in the file that
215 # were not found in the catalogue
216 my %found_in_cat = map{ $_ => 1 } @found;
217 push @{$status->{not_found}}, grep(
218 !defined $found_in_cat{$_}, @{$barcodes}
223 =head2 add_items_to_rota
225 take an arrayref of Koha::Item objects and add them to the passed rota
227 =cut
229 sub add_items_to_rota {
231 my ($rota_id, $items) = @_;
233 foreach my $item(@{$items}) {
235 $item->add_to_rota($rota_id);
243 =head1 AUTHOR
245 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
247 =cut