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

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

Return to bug 18072