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

(-)a/C4/Items.pm (-25 / +13 lines)
Lines 37-42 use Data::Dumper; # used as part of logging item record changes, not just for Link Here
37
use Koha::DateUtils qw/dt_from_string/;
37
use Koha::DateUtils qw/dt_from_string/;
38
38
39
use Koha::Database;
39
use Koha::Database;
40
use Koha::Items;
40
41
41
use vars qw($VERSION @ISA @EXPORT);
42
use vars qw($VERSION @ISA @EXPORT);
42
43
Lines 149-186 names to values. If C<$serial> is true, include serial publication data. Link Here
149
sub GetItem {
150
sub GetItem {
150
    my ($itemnumber,$barcode, $serial) = @_;
151
    my ($itemnumber,$barcode, $serial) = @_;
151
    my $dbh = C4::Context->dbh;
152
    my $dbh = C4::Context->dbh;
152
	my $data;
153
153
154
    my $item;
154
    if ($itemnumber) {
155
    if ($itemnumber) {
155
        my $sth = $dbh->prepare("
156
        $item = Koha::Items->find( $itemnumber );
156
            SELECT * FROM items 
157
            WHERE itemnumber = ?");
158
        $sth->execute($itemnumber);
159
        $data = $sth->fetchrow_hashref;
160
    } else {
157
    } else {
161
        my $sth = $dbh->prepare("
158
        $item = Koha::Items->find( { barcode => $barcode } );
162
            SELECT * FROM items 
163
            WHERE barcode = ?"
164
            );
165
        $sth->execute($barcode);		
166
        $data = $sth->fetchrow_hashref;
167
    }
159
    }
168
160
169
    return unless ( $data );
161
    return unless ( $item );
162
163
    my $data = $item->unblessed();
164
    $data->{itype} = $item->effective_itemtype(); # set the correct itype
170
165
171
    if ( $serial) {      
166
    if ($serial) {
172
    my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=?");
167
        my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=?");
173
        $ssth->execute($data->{'itemnumber'}) ;
168
        $ssth->execute( $data->{'itemnumber'} );
174
        ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
169
        ( $data->{'serialseq'}, $data->{'publisheddate'} ) = $ssth->fetchrow_array();
175
    }
170
    }
176
	#if we don't have an items.itype, use biblioitems.itemtype.
171
177
    # FIXME this should respect the itypes systempreference
178
    # if (C4::Context->preference('item-level_itypes')) {
179
	if( ! $data->{'itype'} ) {
180
		my $sth = $dbh->prepare("SELECT itemtype FROM biblioitems  WHERE biblionumber = ?");
181
		$sth->execute($data->{'biblionumber'});
182
		($data->{'itype'}) = $sth->fetchrow_array;
183
	}
184
    return $data;
172
    return $data;
185
}    # sub GetItem
173
}    # sub GetItem
186
174
(-)a/t/db_dependent/Items.t (-21 / +64 lines)
Lines 40-46 my $location = 'My Location'; Link Here
40
40
41
subtest 'General Add, Get and Del tests' => sub {
41
subtest 'General Add, Get and Del tests' => sub {
42
42
43
    plan tests => 14;
43
    plan tests => 16;
44
44
45
    $schema->storage->txn_begin;
45
    $schema->storage->txn_begin;
46
46
Lines 48-60 subtest 'General Add, Get and Del tests' => sub { Link Here
48
    my $library = $builder->build({
48
    my $library = $builder->build({
49
        source => 'Branch',
49
        source => 'Branch',
50
    });
50
    });
51
    my $itemtype = $builder->build({
52
        source => 'Itemtype',
53
    });
51
54
52
    # Create a biblio instance for testing
55
    # Create a biblio instance for testing
53
    C4::Context->set_preference('marcflavour', 'MARC21');
56
    C4::Context->set_preference('marcflavour', 'MARC21');
54
    my ($bibnum, $bibitemnum) = get_biblio();
57
    my ($bibnum, $bibitemnum) = get_biblio();
55
58
56
    # Add an item.
59
    # Add an item.
57
    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location } , $bibnum);
60
    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
58
    cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
61
    cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
59
    cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
62
    cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
60
63
Lines 75-81 subtest 'General Add, Get and Del tests' => sub { Link Here
75
    my $getdeleted = GetItem($itemnumber);
78
    my $getdeleted = GetItem($itemnumber);
76
    is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
79
    is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
77
80
78
    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location' } , $bibnum);
81
    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $bibnum);
79
    $getitem = GetItem($itemnumber);
82
    $getitem = GetItem($itemnumber);
80
    is( $getitem->{location}, $location, "The location should not have been modified" );
83
    is( $getitem->{location}, $location, "The location should not have been modified" );
81
    is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" );
84
    is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" );
Lines 90-95 subtest 'General Add, Get and Del tests' => sub { Link Here
90
    is( $getitem->{location}, 'CART', "The location should have been set to CART" );
93
    is( $getitem->{location}, 'CART', "The location should have been set to CART" );
91
    is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" );
94
    is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" );
92
95
96
    C4::Context->set_preference('item-level_itypes', '1');
97
    $getitem = GetItem($itemnumber);
98
    is( $getitem->{itype}, $itemtype->{itemtype}, "Itemtype set correctly when using item_level-itypes" );
99
    C4::Context->set_preference('item-level_itypes', '0');
100
    $getitem = GetItem($itemnumber);
101
    is( $getitem->{itype}, undef, "Itemtype set correctly when not using item_level-itypes" );
102
93
    $schema->storage->txn_rollback;
103
    $schema->storage->txn_rollback;
94
};
104
};
95
105
Lines 109-131 subtest 'GetHiddenItemnumbers tests' => sub { Link Here
109
    my $library2 = $builder->build({
119
    my $library2 = $builder->build({
110
        source => 'Branch',
120
        source => 'Branch',
111
    });
121
    });
122
    my $itemtype = $builder->build({
123
        source => 'Itemtype',
124
    });
112
125
113
    # Create a new biblio
126
    # Create a new biblio
114
    C4::Context->set_preference('marcflavour', 'MARC21');
127
    C4::Context->set_preference('marcflavour', 'MARC21');
115
    my ($biblionumber, $biblioitemnumber) = get_biblio();
128
    my ($biblionumber, $biblioitemnumber) = get_biblio();
116
129
117
    # Add two items
130
    # Add two items
118
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
131
    my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem(
119
            { homebranch => $library1->{branchcode},
132
        {
120
              holdingbranch => $library1->{branchcode},
133
            homebranch    => $library1->{branchcode},
121
              withdrawn => 1 },
134
            holdingbranch => $library1->{branchcode},
122
            $biblionumber
135
            withdrawn     => 1,
136
            itype         => $itemtype->{itemtype},
137
        },
138
        $biblionumber
123
    );
139
    );
124
    my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
140
    my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem(
125
            { homebranch => $library2->{branchcode},
141
        {
126
              holdingbranch => $library2->{branchcode},
142
            homebranch    => $library2->{branchcode},
127
              withdrawn => 0 },
143
            holdingbranch => $library2->{branchcode},
128
            $biblionumber
144
            withdrawn     => 0,
145
            itype         => $itemtype->{itemtype},
146
        },
147
        $biblionumber
129
    );
148
    );
130
149
131
    my $opachiddenitems;
150
    my $opachiddenitems;
Lines 192-206 subtest 'GetItemsInfo tests' => sub { Link Here
192
    my $library2 = $builder->build({
211
    my $library2 = $builder->build({
193
        source => 'Branch',
212
        source => 'Branch',
194
    });
213
    });
214
    my $itemtype = $builder->build({
215
        source => 'Itemtype',
216
    });
195
217
196
    # Add a biblio
218
    # Add a biblio
197
    my ($biblionumber, $biblioitemnumber) = get_biblio();
219
    my ($biblionumber, $biblioitemnumber) = get_biblio();
198
    # Add an item
220
    # Add an item
199
    my ($item_bibnum, $item_bibitemnum, $itemnumber)
221
    my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
200
        = AddItem({
222
        {
201
                homebranch    => $library1->{branchcode},
223
            homebranch    => $library1->{branchcode},
202
                holdingbranch => $library2->{branchcode},
224
            holdingbranch => $library2->{branchcode},
203
            }, $biblionumber );
225
            itype         => $itemtype->{itemtype},
226
        },
227
        $biblionumber
228
    );
204
229
205
    my $branch = GetBranchDetail( $library1->{branchcode} );
230
    my $branch = GetBranchDetail( $library1->{branchcode} );
206
    $branch->{ opac_info } = "homebranch OPAC info";
231
    $branch->{ opac_info } = "homebranch OPAC info";
Lines 279-284 subtest 'SearchItems test' => sub { Link Here
279
    my $library2 = $builder->build({
304
    my $library2 = $builder->build({
280
        source => 'Branch',
305
        source => 'Branch',
281
    });
306
    });
307
    my $itemtype = $builder->build({
308
        source => 'Itemtype',
309
    });
282
310
283
    C4::Context->set_preference('marcflavour', 'MARC21');
311
    C4::Context->set_preference('marcflavour', 'MARC21');
284
    my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
312
    my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
Lines 291-300 subtest 'SearchItems test' => sub { Link Here
291
    my (undef, undef, $item1_itemnumber) = AddItem({
319
    my (undef, undef, $item1_itemnumber) = AddItem({
292
        homebranch => $library1->{branchcode},
320
        homebranch => $library1->{branchcode},
293
        holdingbranch => $library1->{branchcode},
321
        holdingbranch => $library1->{branchcode},
322
        itype => $itemtype->{itemtype},
294
    }, $biblionumber);
323
    }, $biblionumber);
295
    my (undef, undef, $item2_itemnumber) = AddItem({
324
    my (undef, undef, $item2_itemnumber) = AddItem({
296
        homebranch => $library2->{branchcode},
325
        homebranch => $library2->{branchcode},
297
        holdingbranch => $library2->{branchcode},
326
        holdingbranch => $library2->{branchcode},
327
        itype => $itemtype->{itemtype},
298
    }, $biblionumber);
328
    }, $biblionumber);
299
329
300
    my ($items, $total_results);
330
    my ($items, $total_results);
Lines 442-452 subtest 'Koha::Item(s) tests' => sub { Link Here
442
    my $library2 = $builder->build({
472
    my $library2 = $builder->build({
443
        source => 'Branch',
473
        source => 'Branch',
444
    });
474
    });
475
    my $itemtype = $builder->build({
476
        source => 'Itemtype',
477
    });
445
478
446
    # Create a biblio and item for testing
479
    # Create a biblio and item for testing
447
    C4::Context->set_preference('marcflavour', 'MARC21');
480
    C4::Context->set_preference('marcflavour', 'MARC21');
448
    my ($bibnum, $bibitemnum) = get_biblio();
481
    my ($bibnum, $bibitemnum) = get_biblio();
449
    my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} } , $bibnum);
482
    my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
483
        {
484
            homebranch    => $library1->{branchcode},
485
            holdingbranch => $library2->{branchcode},
486
            itype         => $itemtype->{itemtype},
487
        },
488
        $bibnum
489
    );
450
490
451
    # Get item.
491
    # Get item.
452
    my $item = Koha::Items->find( $itemnumber );
492
    my $item = Koha::Items->find( $itemnumber );
Lines 475-480 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { Link Here
475
    my $library2 = $builder->build({
515
    my $library2 = $builder->build({
476
        source => 'Branch',
516
        source => 'Branch',
477
    });
517
    });
518
    my $itemtype = $builder->build({
519
        source => 'Itemtype',
520
    });
478
521
479
    my ( $biblionumber, $biblioitemnumber ) = get_biblio();
522
    my ( $biblionumber, $biblioitemnumber ) = get_biblio();
480
    my $item_infos = [
523
    my $item_infos = [
Lines 496-502 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { Link Here
496
        my ( undef, undef, $itemnumber ) = AddItem(
539
        my ( undef, undef, $itemnumber ) = AddItem(
497
            {
540
            {
498
                homebranch    => $item_info->{homebranch},
541
                homebranch    => $item_info->{homebranch},
499
                holdingbranch => $item_info->{holdingbanch}
542
                holdingbranch => $item_info->{holdingbanch},
543
                itype         => $itemtype->{itemtype},
500
            },
544
            },
501
            $biblionumber
545
            $biblionumber
502
        );
546
        );
503
- 

Return to bug 14598