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>.
25 use Koha::StockRotationItems;
28 our ( @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
31 @ISA = qw( Exporter );
42 %EXPORT_TAGS = ( ALL => [ @EXPORT_OK, @EXPORT ] );
47 Koha::Util::StockRotation - utility class with routines for Stock Rotation
53 returns all branches ordered by branchname as an array, each element
54 contains a hashref containing branch details
60 return Koha::Libraries->search(
62 { order_by => ['branchname'] }
69 returns an arrayref of StockRotationStage objects representing
70 all stages for a passed rota
80 if ($rota->stockrotationstages->count > 0) {
82 push @out, $rota->first_stage->unblessed;
84 push @out, @{$rota->first_stage->siblings->unblessed};
91 =head2 toggle_indemand
93 given an item's ID & stage ID toggle that item's in_demand
102 # Get the item object
103 my $sr_item = Koha::StockRotationItems->find(
105 itemnumber_id => $item_id,
109 $sr_item->toggle_indemand;
112 =head2 move_to_next_stage
114 given an item's ID and stage ID, move it
115 to the next stage on the rota
119 sub move_to_next_stage {
121 my ($item_id, $stage_id) = shift;
123 # Get the item object
124 my $item = Koha::StockRotationItems->find(
126 itemnumber_id => $item_id,
127 stage_id => $stage_id
135 =head2 remove_from_stage
137 given an item's ID & stage ID, remove that item from that stage
141 sub remove_from_stage {
143 my ($item_id, $stage_id) = @_;
145 # Get the item object and delete it
146 Koha::StockRotationItems->find(
148 itemnumber_id => $item_id,
149 stage_id => $stage_id
155 =head2 get_barcodes_status
157 take an arrayref of barcodes and a status hashref and populate it
161 sub get_barcodes_status {
163 my ($rota_id, $barcodes, $status) = @_;
165 # Get the items associated with these barcodes
166 my $items = Koha::Items->search(
168 barcode => { '-in' => $barcodes }
171 prefetch => 'stockrotationitem'
174 # Get an array of barcodes that were found
175 # Assign each barcode's status
177 while (my $item = $items->next) {
179 push @found, $item->barcode if $item->barcode;
181 # Check if it's on a rota
182 my $on_rota = $item->stockrotationitem;
187 # Check if it's on this rota
188 if ($on_rota->stage->rota->rota_id == $rota_id) {
191 push @{$status->{on_this}}, $item;
195 # It's on another rota
196 push @{$status->{on_other}}, $item;
202 # Item is not on a rota
203 push @{$status->{ok}}, $item;
209 # Create an array of barcodes supplied in the file that
210 # were not found in the catalogue
211 my %found_in_cat = map{ $_ => 1 } @found;
212 push @{$status->{not_found}}, grep(
213 !defined $found_in_cat{$_}, @{$barcodes}
218 =head2 add_items_to_rota
220 take an arrayref of Koha::Item objects and add them to the passed rota
224 sub add_items_to_rota {
226 my ($rota_id, $items) = @_;
228 foreach my $item(@{$items}) {
230 $item->add_to_rota($rota_id);
240 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>