View | Details | Raw Unified | Return to bug 11897
Collapse All | Expand All

(-)a/Koha/Item.pm (-1 / +48 lines)
Lines 30-35 use Koha::IssuingRules; Link Here
30
use Koha::Item::Transfer;
30
use Koha::Item::Transfer;
31
use Koha::Patrons;
31
use Koha::Patrons;
32
use Koha::Libraries;
32
use Koha::Libraries;
33
use Koha::Stockrotationitem;
34
use Koha::Stockrotationrotas;
33
35
34
use base qw(Koha::Object);
36
use base qw(Koha::Object);
35
37
Lines 235-241 sub current_holds { Link Here
235
    return Koha::Holds->_new_from_dbic($hold_rs);
237
    return Koha::Holds->_new_from_dbic($hold_rs);
236
}
238
}
237
239
238
=head3 type
240
=head3 stockrotationitem
241
242
  my $sritem = Koha::Item->stockrotationitem;
243
244
Returns the stock rotation item associated with the current item.
245
246
=cut
247
248
sub stockrotationitem {
249
    my ( $self ) = @_;
250
    my $rs = $self->_result->stockrotationitem;
251
    return 0 if !$rs;
252
    return Koha::Stockrotationitem->_new_from_dbic( $rs );
253
}
254
255
=head3 add_to_rota
256
257
  my $item = $item->add_to_rota($rota_id);
258
259
Add this item to the rota identified by $ROTA_ID, which means associating it
260
with the first stage of that rota.  Should this item already be associated
261
with a rota, then we will move it to the new rota.
262
263
=cut
264
265
sub add_to_rota {
266
    my ( $self, $rota_id ) = @_;
267
    Koha::Stockrotationrotas->find($rota_id)->add_item($self->itemnumber);
268
    return $self;
269
}
270
271
=head3 biblio
272
273
  my $biblio = $item->biblio;
274
275
Returns the biblio associated with the current item.
276
277
=cut
278
279
sub biblio {
280
    my ( $self ) = @_;
281
    my $rs = $self->_result->biblio;
282
    return Koha::Biblio->_new_from_dbic( $rs );
283
}
284
285
=head3 _type
239
286
240
=cut
287
=cut
241
288
(-)a/Koha/Library.pm (+15 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Stockrotationstages;
25
26
26
use base qw(Koha::Object);
27
use base qw(Koha::Object);
27
28
Lines 54-59 sub add_to_categories { Link Here
54
    }
55
    }
55
}
56
}
56
57
58
=head3 stockrotationstages
59
60
  my $stages = Koha::Library->stockrotationstages;
61
62
Returns the stockrotation stages associated with this Library.
63
64
=cut
65
66
sub stockrotationstages {
67
    my ( $self ) = @_;
68
    my $rs = $self->_result->stockrotationstages;
69
    return Koha::Stockrotationstages->_new_from_dbic( $rs );
70
}
71
57
=head3 type
72
=head3 type
58
73
59
=cut
74
=cut
(-)a/Koha/Stockrotationitem.pm (+305 lines)
Line 0 Link Here
1
package Koha::Stockrotationitem;
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 DateTime;
23
use DateTime::Duration;
24
use Koha::Database;
25
use Koha::DateUtils qw/dt_from_string/;
26
use Koha::Item::Transfer;
27
use Koha::Item;
28
use Koha::Stockrotationstage;
29
30
use base qw(Koha::Object);
31
32
=head1 NAME
33
34
Stockrotationitem - Koha Stockrotationitem Object class
35
36
=head1 SYNOPSIS
37
38
Stockrotationitem class used primarily by stockrotation .pls and the stock
39
rotation cron script.
40
41
=head1 DESCRIPTION
42
43
Standard Koha::Objects definitions, and additional methods.
44
45
=head1 API
46
47
=head2 Class Methods
48
49
=cut
50
51
=head3 _type
52
53
=cut
54
55
sub _type {
56
    return 'Stockrotationitem';
57
}
58
59
=head3 itemnumber
60
61
  my $item = Koha::Stockrotationitem->itemnumber;
62
63
Returns the item associated with the current stock rotation item.
64
65
=cut
66
67
sub itemnumber {
68
    my ( $self ) = @_;
69
    my $rs = $self->_result->itemnumber;
70
    return Koha::Item->_new_from_dbic( $rs );
71
}
72
73
=head3 stage
74
75
  my $stage = Koha::Stockrotationitem->stage;
76
77
Returns the stage associated with the current stock rotation item.
78
79
=cut
80
81
sub stage {
82
    my ( $self ) = @_;
83
    my $rs = $self->_result->stage;
84
    return Koha::Stockrotationstage->_new_from_dbic( $rs );
85
}
86
87
sub needs_initiating {
88
    my ( $self ) = @_;
89
    my $completed = $self->itemnumber->_result->branchtransfers->search;
90
    if ( !$completed->count ) {
91
        return 1;
92
    } else {
93
        my $stage_branch = $self->stage->branchcode_id;
94
        my $home_branch = $self->itemnumber->homebranch;
95
        return !( $stage_branch eq $home_branch );
96
    }
97
}
98
99
=head3 needs_repatriating
100
101
  my $status = $item->needs_repatriating;
102
103
Return 1 if this item is currently not at the library it should be at
104
according to our stockrotation plan.
105
106
=cut
107
108
# Check following things:
109
#
110
# Homebranch == stockrotationstage.branch
111
#   If not, check if Homebranch == stockrotationstage.stageafter.branch
112
#     If so, presumably in transit :=> 0
113
#     Else, :=> 1
114
#
115
# Holdingbranch == Homebranch
116
#   If not, :=> 1
117
118
sub needs_repatriating {
119
    my ( $self ) = @_;
120
    my ( $item, $stage ) = ( $self->itemnumber, $self->stage );
121
    if ( $item->homebranch ne $stage->branchcode_id ) {
122
        # We're either in transit to new branch or we got corrupted.
123
        my $next_stage = $stage->next_sibling;
124
        ( $next_stage && $item->homebranch eq $next_stage->branchcode_id )
125
            ? return 0          # We seem to be in transit.
126
            : return 1;         # We got corrupted, gotta be repatriated!
127
    }
128
    # Check if we are where we should be…
129
    if ( $item->holdingbranch eq $item->homebranch
130
             || $self->itemnumber->get_transfer ) {
131
        return 0;               # Yup, all good
132
    } else {
133
        return 1;               # Nope, gotta be repatriated
134
    }
135
}
136
137
=head3 needs_advancing
138
139
  my $status = $item->needs_advancing;
140
141
Return 1 if this item is ready to be moved on to the next stage in its rota.
142
143
=cut
144
145
sub needs_advancing {
146
    my ( $self ) = @_;
147
    my $intransfers = $self->itemnumber->get_transfer;
148
    # Check whether we are in transfer
149
    if ( !$intransfers ) {
150
        my $completed = $self->itemnumber->_result->branchtransfers->search(
151
            {},
152
            {
153
                order_by => { -desc => 'datearrived' }
154
            }
155
        );
156
        # Do maths on whether we need to be moved on.
157
        if ( $completed->count ) {
158
            my $arrival = dt_from_string(
159
                $completed->next->datearrived, 'iso'
160
            );
161
            my $duration = DateTime::Duration
162
                ->new( days => $self->stage->duration );
163
            if ( $arrival + $duration le DateTime->now ) {
164
                return 1;
165
            } else {
166
                return 0;
167
            }
168
        } else {
169
            die "We have no historical branch transfer; this should not have happened!";
170
        }
171
    }
172
    # Currently in transfer, do not advance.
173
    return 0;
174
}
175
176
sub initiate {
177
    my ( $self ) = @_;
178
    my $stage_branch = $self->stage->branchcode_id;
179
    my $home_branch = $self->itemnumber->homebranch;
180
    my $holding_branch = $self->itemnumber->holdingbranch;
181
    if ( !( ( $stage_branch eq $home_branch )
182
             && ( $holding_branch eq $home_branch ) ) ) {
183
        return $self->repatriate("StockrotationInit");
184
    } else {
185
        $self->itemnumber->homebranch($self->stage->branchcode_id)->store;
186
        return Koha::Item::Transfer->new({
187
            'itemnumber'  => $self->itemnumber_id,
188
            'frombranch'  => $self->stage->branchcode_id,
189
            'tobranch'    => $self->stage->branchcode_id,
190
            'datesent'    => DateTime->now,
191
            'datearrived' => DateTime->now,
192
            'comments'    => "StockrotationInit"
193
        })->store;
194
    }
195
}
196
197
=head3 repatriate
198
199
  $sritem = $sritem->repatriate
200
201
Put this item into branch transfer with 'StockrotationCorrection' comment, so
202
that it may return to it's stage.branch to continue its rota as normal.
203
204
=cut
205
206
sub repatriate {
207
    my ( $self, $msg ) = @_;
208
    # Create the transfer.
209
    my $transfer_stored = Koha::Item::Transfer->new({
210
        'itemnumber' => $self->itemnumber_id,
211
        'frombranch' => $self->itemnumber->holdingbranch,
212
        'tobranch'   => $self->stage->branchcode_id,
213
        'datesent'   => DateTime->now,
214
        'comments'   => $msg || "StockrotationRepatriation",
215
    })->store;
216
    $self->itemnumber->homebranch($self->stage->branchcode_id)->store;
217
    return $transfer_stored;
218
}
219
220
=head3 advance
221
222
  $sritem = $sritem->advance
223
224
Put this item into branch transfer with 'StockrotationAdvance' comment, to
225
transfer it to the next stage in its rota.
226
227
If this is the last stage in the rota and this rota is cyclical, we return to
228
the first stage.  If it is not cyclical, then we delete this
229
Stockrotationitem.
230
231
If this item is 'indemand', and advance is invoked, we disable 'indemand' and
232
advance the item as per usual.
233
234
=cut
235
236
sub advance {
237
    my ( $self ) = @_;
238
239
    my $transfer = Koha::Item::Transfer->new({
240
        'itemnumber' => $self->itemnumber_id,
241
        'frombranch' => $self->itemnumber->holdingbranch,
242
        'datesent'   => DateTime->now,
243
        'comments'   => "StockrotationAdvance"
244
    });
245
246
    if ( $self->indemand ) {
247
        $self->indemand(0)->store;  # De-activate indemand
248
        $transfer->tobranch($self->stage->branchcode_id);
249
        $transfer->datearrived(DateTime->now);
250
    } else {
251
        # Find and update our stage.
252
        my $current_stage = $self->stage;
253
        my $new_stage;
254
        # Last stage in rota?
255
        if ( !$current_stage->last_sibling ) {
256
            # Special rules: Cyclical rota?
257
            if ( $current_stage->rota->cyclical ) {
258
                # Revert to first stage.
259
                my $xstg = $current_stage->first_sibling;
260
                # Is this the first (as well as last) stage?
261
                if ( $xstg ) {
262
                    # ... No, so return to first stage.
263
                    $new_stage = $xstg;
264
                } else {
265
                    # ... Yes, so we create a dummy transfer (for statistics)
266
                    my $transfer_stored = Koha::Item::Transfer->new({
267
                        'itemnumber'  => $self->itemnumber_id,
268
                        'frombranch'  => $self->itemnumber->holdingbranch,
269
                        'tobranch'    => $self->itemnumber->holdingbranch,
270
                        'datesent'    => DateTime->now,
271
                        'datearrived' => DateTime->now,
272
                        'comments'    => "StockrotationAdvance"
273
                    })->store;
274
                    # and return.
275
                    return 1;
276
                }
277
                $new_stage = ( $xstg ) ? $xstg : $current_stage;
278
            } else {
279
                # Stockrotationitem no longer needs to exist.
280
                $self->delete;
281
                return 1;
282
            }
283
        } else {
284
            # Just advance.
285
            $new_stage = $self->stage->next_sibling;
286
        }
287
288
        # Update our stage.
289
        $self->stage_id($new_stage->stage_id)->store;
290
        # Update our homebranch.
291
        $self->itemnumber->homebranch($self->stage->branchcode_id)->store;
292
        #Update the transfer
293
        $transfer->tobranch($new_stage->branchcode_id);
294
    }
295
296
    return $transfer->store;
297
}
298
299
1;
300
301
=head1 AUTHOR
302
303
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
304
305
=cut
(-)a/Koha/Stockrotationitems.pm (+65 lines)
Line 0 Link Here
1
package Koha::Stockrotationitems;
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::Stockrotationitem;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Stockrotationitems - Koha Stockrotationitems Object class
30
31
=head1 SYNOPSIS
32
33
Stockrotationitems class used primarily by stockrotation .pls and the stock
34
rotation cron script.
35
36
=head1 DESCRIPTION
37
38
Standard Koha::Objects definitions, and additional methods.
39
40
=head1 API
41
42
=head2 Class Methods
43
44
=cut
45
46
=head3 _type
47
48
=cut
49
50
sub _type {
51
    return 'Stockrotationitem';
52
}
53
54
sub object_class {
55
    return 'Koha::Stockrotationitem';
56
}
57
58
59
1;
60
61
=head1 AUTHOR
62
63
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
64
65
=cut
(-)a/Koha/Stockrotationrota.pm (+135 lines)
Line 0 Link Here
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
    my $stages = $self->stockrotationstages;
76
    die "Rota is missing stages." unless ( $stages->count > 0 );
77
    if ($sritem) {
78
        $sritem->stage_id($stages->next->stage_id)->store;
79
    } else {
80
        $sritem = Koha::Stockrotationitem->new({
81
            itemnumber_id => $itemnumber,
82
            stage_id      => $stages->next->stage_id,
83
            indemand      => 0,
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 items
106
107
  my $items = $rota->items;
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 _type
122
123
=cut
124
125
sub _type {
126
    return 'Stockrotationrota';
127
}
128
129
1;
130
131
=head1 AUTHOR
132
133
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
134
135
=cut
(-)a/Koha/Stockrotationrotas.pm (+64 lines)
Line 0 Link Here
1
package Koha::Stockrotationrotas;
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::Stockrotationrota;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Stockrotationrotas - Koha Stockrotationrotas Object class
30
31
=head1 SYNOPSIS
32
33
Stockrotationrotas class used primarily by stockrotation .pls and the stock
34
rotation cron script.
35
36
=head1 DESCRIPTION
37
38
Standard Koha::Objects definitions, and additional methods.
39
40
=head1 API
41
42
=head2 Class Methods
43
44
=cut
45
46
=head3 _type
47
48
=cut
49
50
sub _type {
51
    return 'Stockrotationrota';
52
}
53
54
sub object_class {
55
    return 'Koha::Stockrotationrota';
56
}
57
58
1;
59
60
=head1 AUTHOR
61
62
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
63
64
=cut
(-)a/Koha/Stockrotationstage.pm (+295 lines)
Line 0 Link Here
1
package Koha::Stockrotationstage;
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::Library;
24
use Koha::Stockrotationitems;
25
use Koha::Stockrotationrota;
26
27
use base qw(Koha::Object);
28
29
=head1 NAME
30
31
Stockrotationstage - Koha Stockrotationstage Object class
32
33
=head1 SYNOPSIS
34
35
Stockrotationstage 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 _type
49
50
=cut
51
52
sub _type {
53
    return 'Stockrotationstage';
54
}
55
56
sub _relation {
57
    my ( $self, $method, $type ) = @_;
58
    return sub {
59
        my $rs = $self->_result->$method;
60
        return 0 if !$rs;
61
        my $namespace = 'Koha::' . $type;
62
        return $namespace->_new_from_dbic( $rs );
63
    }
64
}
65
66
=head3 stockrotationitems
67
68
  my $stages = Koha::Stockrotationstage->stockrotationitems;
69
70
Returns the items associated with the current stage.
71
72
=cut
73
74
sub stockrotationitems {
75
    my ( $self ) = @_;
76
    return &{$self->_relation(qw/ stockrotationitems Stockrotationitems /)};
77
}
78
79
=head3 branchcode
80
81
  my $branch = Koha::Stockrotationstage->branchcode;
82
83
Returns the branch associated with the current stage.
84
85
=cut
86
87
sub branchcode {
88
    my ( $self ) = @_;
89
    return &{$self->_relation(qw/ branchcode Library /)};
90
}
91
92
=head3 rota
93
94
  my $rota = Koha::Stockrotationstage->rota;
95
96
Returns the rota associated with the current stage.
97
98
=cut
99
100
sub rota {
101
    my ( $self ) = @_;
102
    return &{$self->_relation(qw/ rota Stockrotationrota /)};
103
}
104
105
=head3 siblings
106
107
  my $siblings = $stage->siblings;
108
109
Koha::Object wrapper around DBIx::Class::Ordered.
110
111
=cut
112
113
sub siblings {
114
    my ( $self ) = @_;
115
    return &{$self->_relation(qw/ siblings Stockrotationstages /)};
116
}
117
118
=head3 next_siblings
119
120
  my $next_siblings = $stage->next_siblings;
121
122
Koha::Object wrapper around DBIx::Class::Ordered.
123
124
=cut
125
126
sub next_siblings {
127
    my ( $self ) = @_;
128
    return &{$self->_relation(qw/ next_siblings Stockrotationstages /)};
129
}
130
131
=head3 previous_siblings
132
133
  my $previous_siblings = $stage->previous_siblings;
134
135
Koha::Object wrapper around DBIx::Class::Ordered.
136
137
=cut
138
139
sub previous_siblings {
140
    my ( $self ) = @_;
141
    return &{$self->_relation(qw/ previous_siblings Stockrotationstages /)};
142
}
143
144
=head3 next_sibling
145
146
  my $next = $stage->next_sibling;
147
148
Koha::Object wrapper around DBIx::Class::Ordered.
149
150
=cut
151
152
sub next_sibling {
153
    my ( $self ) = @_;
154
    return &{$self->_relation(qw/ next_sibling Stockrotationstage /)};
155
}
156
157
=head3 previous_sibling
158
159
  my $previous = $stage->previous_sibling;
160
161
Koha::Object Wrapper around DBIx::Class::Ordered.
162
163
=cut
164
165
sub previous_sibling {
166
    my ( $self ) = @_;
167
    return &{$self->_relation(qw/ previous_sibling Stockrotationstage /)};
168
}
169
170
=head3 first_sibling
171
172
  my $first = $stage->first_sibling;
173
174
Koha::Object Wrapper around DBIx::Class::Ordered.
175
176
=cut
177
178
sub first_sibling {
179
    my ( $self ) = @_;
180
    return &{$self->_relation(qw/ first_sibling Stockrotationstage /)};
181
}
182
183
=head3 last_sibling
184
185
  my $last = $stage->last_sibling;
186
187
Koha::Object Wrapper around DBIx::Class::Ordered.
188
189
=cut
190
191
sub last_sibling {
192
    my ( $self ) = @_;
193
    return &{$self->_relation(qw/ last_sibling Stockrotationstage /)};
194
}
195
196
=head3 move_previous
197
198
  1|0 = $stage->move_previous;
199
200
Koha::Object Wrapper around DBIx::Class::Ordered.
201
202
=cut
203
204
sub move_previous {
205
    my ( $self ) = @_;
206
    return $self->_result->move_previous;
207
}
208
209
=head3 move_next
210
211
  1|0 = $stage->move_next;
212
213
Koha::Object Wrapper around DBIx::Class::Ordered.
214
215
=cut
216
217
sub move_next {
218
    my ( $self ) = @_;
219
    return $self->_result->move_next;
220
}
221
222
=head3 move_first
223
224
  1|0 = $stage->move_first;
225
226
Koha::Object Wrapper around DBIx::Class::Ordered.
227
228
=cut
229
230
sub move_first {
231
    my ( $self ) = @_;
232
    return $self->_result->move_first;
233
}
234
235
=head3 move_last
236
237
  1|0 = $stage->move_last;
238
239
Koha::Object Wrapper around DBIx::Class::Ordered.
240
241
=cut
242
243
sub move_last {
244
    my ( $self ) = @_;
245
    return $self->_result->move_last;
246
}
247
248
=head3 move_to
249
250
  1|0 = $stage->move_to($position);
251
252
Koha::Object Wrapper around DBIx::Class::Ordered.
253
254
=cut
255
256
sub move_to {
257
    my ( $self, $position ) = @_;
258
    return $self->_result->move_to($position)
259
        if ( $position le $self->rota->stockrotationstages->count );
260
    return 0;
261
}
262
263
=head3 move_to_group
264
265
  1|0 = $stage->move_to_group($rota_id, [$position]);
266
267
Koha::Object Wrapper around DBIx::Class::Ordered.
268
269
=cut
270
271
sub move_to_group {
272
    my ( $self, $rota_id, $position ) = @_;
273
    return $self->_result->move_to_group($rota_id, $position);
274
}
275
276
=head3 delete
277
278
  1|0 = $stage->delete;
279
280
Koha::Object Wrapper around DBIx::Class::Ordered.
281
282
=cut
283
284
sub delete {
285
    my ( $self ) = @_;
286
    return $self->_result->delete;
287
}
288
289
1;
290
291
=head1 AUTHOR
292
293
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
294
295
=cut
(-)a/Koha/Stockrotationstages.pm (+64 lines)
Line 0 Link Here
1
package Koha::Stockrotationstages;
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::Stockrotationstage;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Stockrotationstages - Koha Stockrotationstages Object class
30
31
=head1 SYNOPSIS
32
33
Stockrotationstages class used primarily by stockrotation .pls and the stock
34
rotation cron script.
35
36
=head1 DESCRIPTION
37
38
Standard Koha::Objects definitions, and additional methods.
39
40
=head1 API
41
42
=head2 Class Methods
43
44
=cut
45
46
=head3 _type
47
48
=cut
49
50
sub _type {
51
    return 'Stockrotationstage';
52
}
53
54
sub object_class {
55
    return 'Koha::Stockrotationstage';
56
}
57
58
1;
59
60
=head1 AUTHOR
61
62
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
63
64
=cut
(-)a/t/db_dependent/Items.t (-1 / +64 lines)
Lines 26-32 use Koha::Library; Link Here
26
use t::lib::Mocks;
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
28
28
29
use Test::More tests => 11;
29
use Test::More tests => 13;
30
30
31
use Test::Warn;
31
use Test::Warn;
32
32
Lines 782-787 subtest '_mod_item_dates' => sub { Link Here
782
        'yetanotherdatetime is ok' );
782
        'yetanotherdatetime is ok' );
783
};
783
};
784
784
785
subtest 'Check stockrotationitem relationship' => sub {
786
    plan tests => 1;
787
788
    $schema->storage->txn_begin();
789
790
    my $builder = t::lib::TestBuilder->new;
791
    my $item = $builder->build({ source => 'Item' });
792
793
    $builder->build({
794
        source => 'Stockrotationitem',
795
        value  => { itemnumber_id => $item->{itemnumber} }
796
    });
797
798
    my $sritem = Koha::Items->find($item->{itemnumber})->stockrotationitem;
799
    isa_ok( $sritem, 'Koha::Stockrotationitem', "Relationship works and correctly creates Koha::Object." );
800
801
    $schema->storage->txn_rollback;
802
};
803
804
subtest 'Check add_to_rota method' => sub {
805
    plan tests => 2;
806
807
    $schema->storage->txn_begin();
808
809
    my $builder = t::lib::TestBuilder->new;
810
    my $item = $builder->build({ source => 'Item' });
811
    my $rota = $builder->build({ source => 'Stockrotationrota' });
812
    my $srrota = Koha::Stockrotationrotas->find($rota->{rota_id});
813
814
    $builder->build({
815
        source => 'Stockrotationstage',
816
        value  => { rota_id => $rota->{rota_id} },
817
    });
818
819
    my $sritem = Koha::Items->find($item->{itemnumber});
820
    $sritem->add_to_rota($rota->{rota_id});
821
822
    is(
823
        Koha::Stockrotationitems->find($item->{itemnumber})->stage_id,
824
        $srrota->stockrotationstages->next->stage_id,
825
        "Adding to a rota a new sritem item being assigned to its first stage."
826
    );
827
828
    my $newrota = $builder->build({ source => 'Stockrotationrota' });
829
830
    my $srnewrota = Koha::Stockrotationrotas->find($newrota->{rota_id});
831
832
    $builder->build({
833
        source => 'Stockrotationstage',
834
        value  => { rota_id => $newrota->{rota_id} },
835
    });
836
837
    $sritem->add_to_rota($newrota->{rota_id});
838
839
    is(
840
        Koha::Stockrotationitems->find($item->{itemnumber})->stage_id,
841
        $srnewrota->stockrotationstages->next->stage_id,
842
        "Moving an item results in that sritem being assigned to the new first stage."
843
    );
844
845
    $schema->storage->txn_rollback;
846
};
847
785
# Helper method to set up a Biblio.
848
# Helper method to set up a Biblio.
786
sub get_biblio {
849
sub get_biblio {
787
    my ( $frameworkcode ) = @_;
850
    my ( $frameworkcode ) = @_;
(-)a/t/db_dependent/Koha/Libraries.t (-1 / +24 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 9;
22
use Test::More tests => 11;
23
23
24
use Koha::Library;
24
use Koha::Library;
25
use Koha::Libraries;
25
use Koha::Libraries;
Lines 86-90 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have de Link Here
86
$retrieved_category_2->delete;
86
$retrieved_category_2->delete;
87
is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
87
is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
88
88
89
# Stockrotation relationship testing
90
91
my $new_library_sr = $builder->build({ source => 'Branch' });
92
93
$builder->build({
94
    source => 'Stockrotationstage',
95
    value  => { branchcode_id => $new_library_sr->{branchcode} },
96
});
97
$builder->build({
98
    source => 'Stockrotationstage',
99
    value  => { branchcode_id => $new_library_sr->{branchcode} },
100
});
101
$builder->build({
102
    source => 'Stockrotationstage',
103
    value  => { branchcode_id => $new_library_sr->{branchcode} },
104
});
105
106
my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
107
    ->stockrotationstages;
108
is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
109
110
isa_ok( $srstages->next, 'Koha::Stockrotationstage', "Relationship correctly creates Koha::Objects." );
111
89
$schema->storage->txn_rollback;
112
$schema->storage->txn_rollback;
90
1;
113
1;
(-)a/t/db_dependent/Stockrotationitems.t (+253 lines)
Line 0 Link Here
1
#!/usr/bin/perl
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 DateTime;
23
use Koha::Database;
24
use Koha::Item::Transfer;
25
use t::lib::TestBuilder;
26
27
use Test::More tests => 7;
28
29
my $schema = Koha::Database->new->schema;
30
31
use_ok('Koha::Stockrotationitems');
32
use_ok('Koha::Stockrotationitem');
33
34
my $builder = t::lib::TestBuilder->new;
35
36
subtest 'Basic object tests' => sub {
37
38
    plan tests => 5;
39
40
    $schema->storage->txn_begin;
41
42
    my $itm = $builder->build({ source => 'Item' });
43
    my $stage = $builder->build({ source => 'Stockrotationstage' });
44
45
    my $item = $builder->build({
46
        source => 'Stockrotationitem',
47
        value  => {
48
            itemnumber_id => $itm->{itemnumber},
49
            stage_id      => $stage->{stage_id},
50
        },
51
    });
52
53
    my $sritem = Koha::Stockrotationitems->find($item->{itemnumber_id});
54
    isa_ok(
55
        $sritem,
56
        'Koha::Stockrotationitem',
57
        "Correctly create and load a stock rotation item."
58
    );
59
60
    # Relationship to rota
61
    isa_ok( $sritem->itemnumber, 'Koha::Item', "Fetched related item." );
62
    is( $sritem->itemnumber->itemnumber, $itm->{itemnumber}, "Related rota OK." );
63
64
    # Relationship to stage
65
    isa_ok( $sritem->stage, 'Koha::Stockrotationstage', "Fetched related stage." );
66
    is( $sritem->stage->stage_id, $stage->{stage_id}, "Related stage OK." );
67
68
69
    $schema->storage->txn_rollback;
70
};
71
72
subtest 'Tests for needs_repatriating' => sub {
73
74
    plan tests => 4;
75
76
    $schema->storage->txn_begin;
77
78
    # Setup a pristine stockrotation context.
79
    my $sritem = $builder->build({ source => 'Stockrotationitem' });
80
    my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
81
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id);
82
    $dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id);
83
    $dbitem->stage->position(1);
84
85
    my $dbrota = $dbitem->stage->rota;
86
    my $newstage = $builder->build({
87
        source => 'Stockrotationstage',
88
        values => {
89
            rota_id => $dbrota->rota_id,
90
            position => 2,
91
        }
92
    });
93
94
    # - homebranch == holdingbranch [0]
95
    is(
96
        $dbitem->needs_repatriating, 0,
97
        "Homebranch == Holdingbranch."
98
    );
99
100
    my $branch = $builder->build({ source => 'Branch' });
101
    $dbitem->itemnumber->holdingbranch($branch->{branchcode});
102
103
    # - homebranch != holdingbranch [1]
104
    is(
105
        $dbitem->needs_repatriating, 1,
106
        "Homebranch != holdingbranch."
107
    );
108
109
    # Set to incorrect homebranch.
110
    $dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id);
111
    $dbitem->itemnumber->homebranch($branch->{branchcode});
112
    # - homebranch != stockrotationstage.branch & not in transit [1]
113
    is(
114
        $dbitem->needs_repatriating, 1,
115
        "Homebranch != Stockrotationstage.Branchcode_id & not in transit."
116
    );
117
118
    # Set to in transit (by implication).
119
    $dbitem->stage($newstage->{stage_id});
120
    # - homebranch != stockrotaitonstage.branch & in transit [0]
121
    is(
122
        $dbitem->needs_repatriating, 1,
123
        "homebranch != stockrotaitonstage.branch & in transit."
124
    );
125
126
    $schema->storage->txn_rollback;
127
};
128
129
subtest "Tests for repatriate." => sub {
130
    plan tests => 3;
131
    $schema->storage->txn_begin;
132
    my $sritem = $builder->build({ source => 'Stockrotationitem' });
133
    my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
134
    $dbitem->stage->position(1);
135
    $dbitem->stage->duration(50);
136
    my $branch = $builder->build({ source => 'Branch' });
137
    $dbitem->itemnumber->holdingbranch($branch->{branchcode});
138
139
    # Test a straight up repatriate
140
    ok($dbitem->repatriate, "Repatriation done.");
141
    my $intransfer = $dbitem->itemnumber->get_transfer;
142
    is($intransfer->frombranch, $branch->{branchcode}, "Origin correct.");
143
    is($intransfer->tobranch, $dbitem->stage->branchcode_id, "Target Correct.");
144
145
    $schema->storage->txn_rollback;
146
};
147
148
subtest "Tests for needs_advancing." => sub {
149
    plan tests => 3;
150
    $schema->storage->txn_begin;
151
152
    # Setup a pristine stockrotation context.
153
    my $sritem = $builder->build({ source => 'Stockrotationitem' });
154
    my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
155
    $dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id);
156
    $dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id);
157
    $dbitem->stage->position(1);
158
    $dbitem->stage->duration(50);
159
160
    my $dbtransfer = Koha::Item::Transfer->new({
161
        'itemnumber'  => $dbitem->itemnumber_id,
162
        'frombranch'  => $dbitem->stage->branchcode_id,
163
        'tobranch'    => $dbitem->stage->branchcode_id,
164
        'datesent'    => DateTime->now,
165
        'datearrived' => undef,
166
        'comments'    => "Test item",
167
    })->store;
168
169
    # Test item will not be advanced if in transit.
170
    is($dbitem->needs_advancing, 0, "Not ready to advance: in transfer.");
171
172
    # Test item will not be advanced if it has not spent enough time.
173
    $dbtransfer->datearrived(DateTime->now)->store;
174
    is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time.");
175
176
    # Test item will be advanced if it has spent enough time.
177
    $dbtransfer->datesent(      # Item was sent 100 days ago...
178
        DateTime->now - DateTime::Duration->new( days => 100 )
179
    )->store;
180
    $dbtransfer->datearrived(   # And arrived 75 days ago.
181
        DateTime->now - DateTime::Duration->new( days => 75 )
182
    )->store;
183
    is($dbitem->needs_advancing, 1, "Ready to be advanced.");
184
185
    $schema->storage->txn_rollback;
186
};
187
188
subtest "Tests for advance." => sub {
189
    plan tests => 12;
190
    $schema->storage->txn_begin;
191
192
    my $sritem = $builder->build({ source => 'Stockrotationitem' });
193
    my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
194
    $dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id);
195
    my $dbstage = $dbitem->stage;
196
    $dbstage->position(1)->duration(50)->store; # Configure stage.
197
    # Configure item
198
    $dbitem->itemnumber->holdingbranch($dbstage->branchcode_id)->store;
199
    $dbitem->itemnumber->homebranch($dbstage->branchcode_id)->store;
200
    # Sanity check
201
    is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage sanity check.");
202
203
    # Test cases of single stage
204
    $dbstage->rota->cyclical(1)->store;         # Set Rota to cyclical.
205
    ok($dbitem->advance, "Single stage cyclical advance done.");
206
    ## Refetch dbitem
207
    $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
208
    is($dbitem->stage->stage_id, $dbstage->stage_id, "Single stage cyclical stage OK.");
209
210
    # Test with indemand advance
211
    $dbitem->indemand(1)->store;
212
    ok($dbitem->advance, "Indemand item advance done.");
213
    ## Refetch dbitem
214
    $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
215
    is($dbitem->indemand, 0, "Indemand OK.");
216
    is($dbitem->stage->stage_id, $dbstage->stage_id, "Indemand item advance stage OK.");
217
218
    # Multi stages
219
    my $srstage = $builder->build({
220
        source => 'Stockrotationstage',
221
        values => { duration => 50 }
222
    });
223
    my $dbstage2 = Koha::Stockrotationstages->find($srstage->{stage_id});
224
    $dbstage2->move_to_group($dbitem->stage->rota_id);
225
    $dbstage2->move_last;
226
227
    # Test a straight up advance
228
    ok($dbitem->advance, "Advancement done.");
229
    ## Refetch dbitem
230
    $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
231
    ## Test results
232
    is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated.");
233
    my $intransfer = $dbitem->itemnumber->get_transfer;
234
    is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct.");
235
    is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct.");
236
237
    $dbstage->rota->cyclical(0)->store;         # Set Rota to non-cyclical.
238
239
    # Arrive at new branch
240
    $intransfer->datearrived(DateTime->now)->store;
241
    $dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store;
242
    $dbitem->itemnumber->homebranch($srstage->{branchcode_id})->store;
243
244
    # Advance again, Remove from rota.
245
    ok($dbitem->advance, "Non-cyclical advance.");
246
    ## Refetch dbitem
247
    $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id});
248
    is($dbitem, undef, "Stockrotationitem has been removed.");
249
250
    $schema->storage->txn_rollback;
251
};
252
253
1;
(-)a/t/db_dependent/Stockrotationrotas.t (+175 lines)
Line 0 Link Here
1
#!/usr/bin/perl
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 t::lib::TestBuilder;
24
25
use Test::More tests => 5;
26
27
my $schema = Koha::Database->new->schema;
28
29
use_ok('Koha::Stockrotationrotas');
30
use_ok('Koha::Stockrotationrota');
31
32
subtest 'Basic object tests' => sub {
33
34
    plan tests => 5;
35
36
    $schema->storage->txn_begin;
37
38
    my $builder = t::lib::TestBuilder->new;
39
40
    my $rota = $builder->build({ source => 'Stockrotationrota' });
41
42
    my $srrota = Koha::Stockrotationrotas->find($rota->{rota_id});
43
    isa_ok(
44
        $srrota,
45
        'Koha::Stockrotationrota',
46
        "Correctly create and load a stock rotation rota."
47
    );
48
49
    $builder->build({
50
        source => 'Stockrotationstage',
51
        value  => { rota_id => $rota->{rota_id} },
52
    });
53
    $builder->build({
54
        source => 'Stockrotationstage',
55
        value  => { rota_id => $rota->{rota_id} },
56
    });
57
    $builder->build({
58
        source => 'Stockrotationstage',
59
        value  => { rota_id => $rota->{rota_id} },
60
    });
61
62
    my $srstages = $srrota->stockrotationstages;
63
    is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this rota');
64
65
    isa_ok( $srstages->next, 'Koha::Stockrotationstage', "Relationship correctly creates Koha::Objects." );
66
67
    #### Test add_item
68
69
    my $item = $builder->build({ source => 'Item' });
70
71
    $srrota->add_item($item->{itemnumber});
72
73
    is(
74
        Koha::Stockrotationitems->find($item->{itemnumber})->stage_id,
75
        $srrota->stockrotationstages->next->stage_id,
76
        "Adding an item results in a new sritem item being assigned to the first stage."
77
    );
78
79
    my $newrota = $builder->build({ source => 'Stockrotationrota' });
80
81
    my $srnewrota = Koha::Stockrotationrotas->find($newrota->{rota_id});
82
83
    $builder->build({
84
        source => 'Stockrotationstage',
85
        value  => { rota_id => $newrota->{rota_id} },
86
    });
87
88
    $srnewrota->add_item($item->{itemnumber});
89
90
    is(
91
        Koha::Stockrotationitems->find($item->{itemnumber})->stage_id,
92
        $srnewrota->stockrotationstages->next->stage_id,
93
        "Moving an item results in that sritem being assigned to the new first stage."
94
    );
95
96
    $schema->storage->txn_rollback;
97
};
98
99
subtest '->first_stage test' => sub {
100
    plan tests => 2;
101
102
    $schema->storage->txn_begin;
103
104
    my $builder = t::lib::TestBuilder->new;
105
106
    my $rota = $builder->build({ source => 'Stockrotationrota' });
107
108
    my $stage1 = $builder->build({
109
        source => 'Stockrotationstage',
110
        value  => { rota_id => $rota->{rota_id} },
111
    });
112
    my $stage2 = $builder->build({
113
        source => 'Stockrotationstage',
114
        value  => { rota_id => $rota->{rota_id} },
115
    });
116
    my $stage3 = $builder->build({
117
        source => 'Stockrotationstage',
118
        value  => { rota_id => $rota->{rota_id} },
119
    });
120
121
    my $srrota = Koha::Stockrotationrotas->find($rota->{rota_id});
122
    my $srstage2 = Koha::Stockrotationstages->find($stage2->{stage_id});
123
    my $firststage = $srstage2->first_sibling || $srstage2;
124
125
    is( $srrota->first_stage->stage_id, $firststage->stage_id, "First stage works" );
126
127
    $srstage2->move_first;
128
129
    is( Koha::Stockrotationrotas->find($rota->{rota_id})->first_stage->stage_id, $stage2->{stage_id}, "Stage re-organized" );
130
131
    $schema->storage->txn_rollback;
132
};
133
134
subtest '->items test' => sub {
135
    plan tests => 1;
136
137
    $schema->storage->txn_begin;
138
139
    my $builder = t::lib::TestBuilder->new;
140
141
    my $rota = $builder->build({ source => 'Stockrotationrota' });
142
143
    my $stage1 = $builder->build({
144
        source => 'Stockrotationstage',
145
        value  => { rota_id => $rota->{rota_id} },
146
    });
147
    my $stage2 = $builder->build({
148
        source => 'Stockrotationstage',
149
        value  => { rota_id => $rota->{rota_id} },
150
    });
151
    my $stage3 = $builder->build({
152
        source => 'Stockrotationstage',
153
        value  => { rota_id => $rota->{rota_id} },
154
    });
155
156
    map { $builder->build({
157
        source => 'Stockrotationitem',
158
        value => { stage_id => $_ },
159
    }) } (
160
        $stage1->{stage_id}, $stage1->{stage_id},
161
        $stage2->{stage_id}, $stage2->{stage_id},
162
        $stage3->{stage_id}, $stage3->{stage_id},
163
    );
164
165
    my $srrota = Koha::Stockrotationrotas->find($rota->{rota_id});
166
167
    is(
168
        $srrota->stockrotationitems->count,
169
        6, "Correct number of items"
170
    );
171
172
    $schema->storage->txn_rollback;
173
};
174
175
1;
(-)a/t/db_dependent/Stockrotationstages.t (-1 / +196 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
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 t::lib::TestBuilder;
24
25
use Test::More tests => 5;
26
27
my $schema = Koha::Database->new->schema;
28
29
use_ok('Koha::Stockrotationstages');
30
use_ok('Koha::Stockrotationstage');
31
32
my $builder = t::lib::TestBuilder->new;
33
34
subtest 'Basic object tests' => sub {
35
    plan tests => 5;
36
37
    $schema->storage->txn_begin;
38
39
    my $library = $builder->build({ source => 'Branch' });
40
    my $rota = $builder->build({ source => 'Stockrotationrota' });
41
    my $stage = $builder->build({
42
        source => 'Stockrotationstage',
43
        value  => {
44
            branchcode_id => $library->{branchcode},
45
            rota_id       => $rota->{rota_id},
46
        },
47
    });
48
49
    my $srstage = Koha::Stockrotationstages->find($stage->{stage_id});
50
    isa_ok(
51
        $srstage,
52
        'Koha::Stockrotationstage',
53
        "Correctly create and load a stock rotation stage."
54
    );
55
56
    # Relationship to library
57
    isa_ok( $srstage->branchcode, 'Koha::Library', "Fetched related branch." );
58
    is( $srstage->branchcode->branchcode, $library->{branchcode}, "Related branch OK." );
59
60
    # Relationship to rota
61
    isa_ok( $srstage->rota, 'Koha::Stockrotationrota', "Fetched related rota." );
62
    is( $srstage->rota->rota_id, $rota->{rota_id}, "Related rota OK." );
63
64
    $schema->storage->txn_rollback;
65
};
66
67
subtest 'DBIx::Class::Ordered tests' => sub {
68
    plan tests => 33;
69
70
    $schema->storage->txn_begin;
71
72
    my $library = $builder->build({ source => 'Branch' });
73
    my $rota = $builder->build({ source => 'Stockrotationrota' });
74
    my $stagefirst = $builder->build({
75
        source   => 'Stockrotationstage',
76
        value    => { rota_id  => $rota->{rota_id}, position => 1 }
77
    });
78
    my $stageprevious = $builder->build({
79
        source   => 'Stockrotationstage',
80
        value    => { rota_id  => $rota->{rota_id}, position => 2 }
81
    });
82
    my $stage = $builder->build({
83
        source => 'Stockrotationstage',
84
        value  => { rota_id => $rota->{rota_id}, position => 3 },
85
    });
86
    my $stagenext = $builder->build({
87
        source   => 'Stockrotationstage',
88
        value    => { rota_id  => $rota->{rota_id}, position => 4 }
89
    });
90
    my $stagelast = $builder->build({
91
        source   => 'Stockrotationstage',
92
        value    => { rota_id  => $rota->{rota_id}, position => 5 }
93
    });
94
95
    my $srstage = Koha::Stockrotationstages->find($stage->{stage_id});
96
97
    is($srstage->siblings->count, 4, "Siblings works.");
98
    is($srstage->previous_siblings->count, 2, "Previous Siblings works.");
99
    is($srstage->next_siblings->count, 2, "Next Siblings works.");
100
101
    my $map = {
102
        first_sibling    => $stagefirst,
103
        previous_sibling => $stageprevious,
104
        next_sibling     => $stagenext,
105
        last_sibling     => $stagelast,
106
    };
107
    # Test plain relations:
108
    while ( my ( $srxsr, $check ) = each %{$map} ) {
109
        my $sr = $srstage->$srxsr;
110
        isa_ok($sr, 'Koha::Stockrotationstage', "Fetched using '$srxsr'.");
111
        is($sr->stage_id, $check->{stage_id}, "'$srxsr' data is correct.");
112
    };
113
114
    # Test mutators
115
    ## Move Previous
116
    ok($srstage->move_previous, "Previous.");
117
    is($srstage->previous_sibling->stage_id, $stagefirst->{stage_id}, "Previous, correct previous.");
118
    is($srstage->next_sibling->stage_id, $stageprevious->{stage_id}, "Previous, correct next.");
119
    ## Move Next
120
    ok($srstage->move_next, "Back to middle.");
121
    is($srstage->previous_sibling->stage_id, $stageprevious->{stage_id}, "Middle, correct previous.");
122
    is($srstage->next_sibling->stage_id, $stagenext->{stage_id}, "Middle, correct next.");
123
    ## Move First
124
    ok($srstage->move_first, "First.");
125
    is($srstage->previous_sibling, 0, "First, correct previous.");
126
    is($srstage->next_sibling->stage_id, $stagefirst->{stage_id}, "First, correct next.");
127
    ## Move Last
128
    ok($srstage->move_last, "Last.");
129
    is($srstage->previous_sibling->stage_id, $stagelast->{stage_id}, "Last, correct previous.");
130
    is($srstage->next_sibling, 0, "Last, correct next.");
131
    ## Move To
132
133
    ### Out of range moves.
134
    is(
135
        $srstage->move_to($srstage->siblings->count + 2),
136
        0, "Move above count of stages."
137
    );
138
    is($srstage->move_to(0), 0, "Move to 0th position.");
139
    is($srstage->move_to(-1), 0, "Move to negative position.");
140
141
    ### Move To
142
    ok($srstage->move_to(3), "Move.");
143
    is($srstage->previous_sibling->stage_id, $stageprevious->{stage_id}, "Move, correct previous.");
144
    is($srstage->next_sibling->stage_id, $stagenext->{stage_id}, "Move, correct next.");
145
146
    # Group manipulation
147
    my $newrota = $builder->build({ source => 'Stockrotationrota' });
148
    ok($srstage->move_to_group($newrota->{rota_id}), "Move to Group.");
149
    is(Koha::Stockrotationstages->find($srstage->stage_id)->rota_id, $newrota->{rota_id}, "Moved correctly.");
150
151
    # Delete in ordered context
152
    ok($srstage->delete, "Deleted OK.");
153
    is(
154
        Koha::Stockrotationstages->find($stageprevious)->next_sibling->stage_id,
155
        $stagenext->{stage_id},
156
        "Delete, correctly re-ordered."
157
    );
158
159
    $schema->storage->txn_rollback;
160
};
161
162
subtest 'Relationship to stockrotationitems' => sub {
163
    plan tests => 2;
164
165
    $schema->storage->txn_begin;
166
    my $stage = $builder->build({ source => 'Stockrotationstage' });
167
168
    $builder->build({
169
        source => 'Stockrotationitem',
170
        value  => { stage_id => $stage->{stage_id} },
171
    });
172
    $builder->build({
173
        source => 'Stockrotationitem',
174
        value  => { stage_id => $stage->{stage_id} },
175
    });
176
    $builder->build({
177
        source => 'Stockrotationitem',
178
        value  => { stage_id => $stage->{stage_id} },
179
    });
180
181
    my $srstage = Koha::Stockrotationstages->find($stage->{stage_id});
182
    my $sritems = $srstage->stockrotationitems;
183
    is(
184
        $sritems->count, 3,
185
        'Correctly fetched stockrotationitems associated with this stage'
186
    );
187
188
    isa_ok(
189
        $sritems->next, 'Koha::Stockrotationitem',
190
        "Relationship correctly creates Koha::Objects."
191
    );
192
193
    $schema->storage->txn_rollback;
194
};
195
196
1;

Return to bug 11897