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

(-)a/Koha/Biblio.pm (+90 lines)
Lines 32-37 use Koha::Biblioitems; Link Here
32
use Koha::ArticleRequests;
32
use Koha::ArticleRequests;
33
use Koha::ArticleRequest::Status;
33
use Koha::ArticleRequest::Status;
34
use Koha::IssuingRules;
34
use Koha::IssuingRules;
35
use Koha::Item::Transfer::Limits;
36
use Koha::Libraries;
37
38
use Koha::Exceptions::Library;
35
39
36
=head1 NAME
40
=head1 NAME
37
41
Lines 79-84 sub can_article_request { Link Here
79
    return q{};
83
    return q{};
80
}
84
}
81
85
86
=head3 can_be_transferred
87
88
$biblio->can_be_transferred({ to => $to_library, from => $from_library })
89
90
Checks if at least one item of a biblio can be transferred to given library.
91
92
This feature is controlled by two system preferences:
93
UseBranchTransferLimits to enable / disable the feature
94
BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
95
                         for setting the limitations
96
97
Performance-wise, it is recommended to use this method for a biblio instead of
98
iterating each item of a biblio with Koha::Item->can_be_transferred().
99
100
Takes HASHref that can have the following parameters:
101
    MANDATORY PARAMETERS:
102
    $to   : Koha::Library or branchcode string
103
    OPTIONAL PARAMETERS:
104
    $from : Koha::Library or branchcode string  # if given, only items from that
105
                                                # holdingbranch are considered
106
107
Returns 1 if at least one of the item of a biblio can be transferred
108
to $to_library, otherwise 0.
109
110
=cut
111
112
sub can_be_transferred {
113
    my ($self, $params) = @_;
114
115
    my $to = $params->{'to'};
116
    my $from = $params->{'from'};
117
    if (ref($to) ne 'Koha::Library') {
118
        my $tobranchcode = defined $to ? $to : '';
119
        $to = Koha::Libraries->find($tobranchcode);
120
        unless ($to) {
121
            Koha::Exceptions::Library::NotFound->throw(
122
                error => "Library '$tobranchcode' not found.",
123
            );
124
        }
125
    }
126
    if ($from && ref($from) ne 'Koha::Library') {
127
        my $frombranchcode = defined $from ? $from : '';
128
        $from = Koha::Libraries->find($frombranchcode);
129
        unless ($from) {
130
            Koha::Exceptions::Library::NotFound->throw(
131
                error => "Library '$frombranchcode' not found.",
132
            );
133
        }
134
    }
135
136
    return 1 unless C4::Context->preference('UseBranchTransferLimits');
137
    my $limittype = C4::Context->preference('BranchTransferLimitsType');
138
139
    my $items;
140
    foreach my $item_of_bib ($self->items) {
141
        next unless $item_of_bib->holdingbranch;
142
        next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
143
        return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
144
        my $code = $limittype eq 'itemtype'
145
                    ? $item_of_bib->effective_itemtype
146
                    : $item_of_bib->ccode;
147
        return 1 unless $code;
148
        $items->{$code}->{$item_of_bib->holdingbranch} = 1;
149
    }
150
151
    # At this point we will have a HASHref containing each itemtype/ccode that
152
    # this biblio has, inside which are all of the holdingbranches where those
153
    # items are located at. Then, we will query Koha::Item::Transfer::Limits to
154
    # find out whether a transfer limits for such $limittype from any of the
155
    # listed holdingbranches to the given $to library exist. If at least one
156
    # holdingbranch for that $limittype does not have a transfer limit to given
157
    # $to library, then we know that the transfer is possible.
158
    foreach my $code (keys %{$items}) {
159
        my @holdingbranches = keys %{$items->{$code}};
160
        return 1 if Koha::Item::Transfer::Limits->search({
161
            toBranch => $to->branchcode,
162
            fromBranch => { 'in' => \@holdingbranches },
163
            $limittype => $code
164
        }, {
165
            group_by => [qw/fromBranch/]
166
        })->count == scalar(@holdingbranches) ? 0 : 1;
167
    }
168
169
    return 0;
170
}
171
82
=head3 article_request_type
172
=head3 article_request_type
83
173
84
my $type = $biblio->article_request_type( $borrower );
174
my $type = $biblio->article_request_type( $borrower );
(-)a/Koha/Item.pm (+4 lines)
Lines 180-185 Takes HASHref that can have the following parameters: Link Here
180
180
181
Returns 1 if item can be transferred to $to_library, otherwise 0.
181
Returns 1 if item can be transferred to $to_library, otherwise 0.
182
182
183
To find out whether at least one item of a Koha::Biblio can be transferred, please
184
see Koha::Biblio->can_be_transferred() instead of using this method for
185
multiple items of the same biblio.
186
183
=cut
187
=cut
184
188
185
sub can_be_transferred {
189
sub can_be_transferred {
(-)a/t/db_dependent/Koha/Biblios.t (-2 / +80 lines)
Lines 19-26 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
23
24
use C4::Biblio;
25
use C4::Items;
24
use C4::Reserves;
26
use C4::Reserves;
25
27
26
use Koha::Biblios;
28
use Koha::Biblios;
Lines 52-57 subtest 'holds' => sub { Link Here
52
    is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
54
    is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
53
};
55
};
54
56
57
subtest 'can_be_transferred' => sub {
58
    plan tests => 11;
59
60
    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
61
    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
62
63
    my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
64
    my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
65
    my $library3 = $builder->build( { source => 'Branch' } )->{branchcode};
66
    my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
67
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
68
    = AddItem({ homebranch => $library1, holdingbranch => $library1 }, $bibnum);
69
    my $item  = Koha::Items->find($itemnumber);
70
    my $biblio = Koha::Biblios->find($bibnum);
71
72
    is(Koha::Item::Transfer::Limits->search({
73
        fromBranch => $library1,
74
        toBranch => $library2,
75
    })->count, 0, 'There are no transfer limits between libraries.');
76
    ok($biblio->can_be_transferred({ to => $library2 }),
77
       'Some items of this biblio can be transferred between libraries.');
78
79
    my $limit = Koha::Item::Transfer::Limit->new({
80
        fromBranch => $library1,
81
        toBranch => $library2,
82
        itemtype => $item->effective_itemtype,
83
    })->store;
84
    is(Koha::Item::Transfer::Limits->search({
85
        fromBranch => $library1,
86
        toBranch => $library2,
87
    })->count, 1, 'Given we have added a transfer limit that applies for all '
88
       .'of this biblio\s items,');
89
    is($biblio->can_be_transferred({ to => $library2 }), 0,
90
       'None of the items of biblio can no longer be transferred between '
91
       .'libraries.');
92
    is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
93
       'We get the same result also if we pass the from-library parameter.');
94
    $item->holdingbranch($library2)->store;
95
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
96
       .'items is already located at to-library, then the transfer is possible.');
97
    $item->holdingbranch($library1)->store;
98
    my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
99
    = AddItem({ homebranch => $library1, holdingbranch => $library3 }, $bibnum);
100
    my $item2  = Koha::Items->find($itemnumber2);
101
    is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
102
       .'another item that should have no transfer limits applying on, then '
103
       .'the transfer is possible.');
104
    $item2->holdingbranch($library1)->store;
105
    is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
106
       .' of the biblio are from same, transfer limited library, then transfer'
107
       .' is not possible.');
108
    eval { $biblio->can_be_transferred({ to => undef }); };
109
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no'
110
       .' library given.');
111
    eval { $biblio->can_be_transferred({ to => 'heaven' }); };
112
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
113
       .' invalid library is given.');
114
    eval { $biblio->can_be_transferred({ to => $library2, from => 'hell' }); };
115
    is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
116
       .' invalid library is given.');
117
};
118
55
$schema->storage->txn_rollback;
119
$schema->storage->txn_rollback;
56
120
121
122
# Helper method to set up a Biblio.
123
sub create_helper_biblio {
124
    my $itemtype = shift;
125
    my ($bibnum, $title, $bibitemnum);
126
    my $bib = MARC::Record->new();
127
    $title = 'Silence in the library';
128
    $bib->append_fields(
129
        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
130
        MARC::Field->new('245', ' ', ' ', a => $title),
131
        MARC::Field->new('942', ' ', ' ', c => $itemtype),
132
    );
133
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
134
}
135
57
1;
136
1;
58
- 

Return to bug 18072