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
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
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
140 =head2 remove_from_stage
142 given an item's ID & stage ID, remove that item from that stage
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
160 =head2 get_barcodes_status
162 take an arrayref of barcodes and a status hashref and populate it
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
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;
192 # Check if it's on this rota
193 if ($on_rota->stage->rota->rota_id == $rota_id) {
196 push @
{$status->{on_this
}}, $item;
200 # It's on another rota
201 push @
{$status->{on_other
}}, $item;
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
229 sub add_items_to_rota
{
231 my ($rota_id, $items) = @_;
233 foreach my $item(@
{$items}) {
235 $item->add_to_rota($rota_id);
245 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>