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

(-)a/C4/Items.pm (-28 / +69 lines)
Lines 80-85 BEGIN { Link Here
80
        GetItemnumberFromBarcode
80
        GetItemnumberFromBarcode
81
        GetBarcodeFromItemnumber
81
        GetBarcodeFromItemnumber
82
        GetHiddenItemnumbers
82
        GetHiddenItemnumbers
83
        ItemSafeToDelete
83
        DelItemCheck
84
        DelItemCheck
84
    MoveItemFromBiblio
85
    MoveItemFromBiblio
85
    GetLatestAcquisitions
86
    GetLatestAcquisitions
Lines 2217-2284 sub MoveItemFromBiblio { Link Here
2217
    return;
2218
    return;
2218
}
2219
}
2219
2220
2220
=head2 DelItemCheck
2221
=head2 ItemSafeToDelete
2221
2222
2222
   DelItemCheck($dbh, $biblionumber, $itemnumber);
2223
   ItemSafeToDelete($dbh, $biblionumber, $itemnumber);
2223
2224
2224
Exported function (core API) for deleting an item record in Koha if there no current issue.
2225
Exported function (core API) for checking whether an item record is safe to delete.
2226
2227
returns 1 if the item is safe to delete,
2228
2229
"book_on_loan" if the item is checked out,
2230
2231
"not_same_branch" if the item is blocked by independent branches,
2232
2233
"book_reserved" if the there are holds aganst the item, or
2234
2235
"linked_analytics" if the item has linked analytic records.
2225
2236
2226
=cut
2237
=cut
2227
2238
2228
sub DelItemCheck {
2239
sub ItemSafeToDelete {
2229
    my ( $dbh, $biblionumber, $itemnumber ) = @_;
2240
    my ( $dbh, $biblionumber, $itemnumber ) = @_;
2230
2241
    my $status;
2231
    $dbh ||= C4::Context->dbh;
2242
    $dbh ||= C4::Context->dbh;
2232
2243
2233
    my $error;
2244
    my $error;
2234
2245
2235
        my $countanalytics=GetAnalyticsCount($itemnumber);
2246
    my $countanalytics = GetAnalyticsCount($itemnumber);
2236
2237
2247
2238
    # check that there is no issue on this item before deletion.
2248
    # check that there is no issue on this item before deletion.
2239
    my $sth = $dbh->prepare(q{
2249
    my $sth = $dbh->prepare(
2250
        q{
2240
        SELECT COUNT(*) FROM issues
2251
        SELECT COUNT(*) FROM issues
2241
        WHERE itemnumber = ?
2252
        WHERE itemnumber = ?
2242
    });
2253
    }
2254
    );
2243
    $sth->execute($itemnumber);
2255
    $sth->execute($itemnumber);
2244
    my ($onloan) = $sth->fetchrow;
2256
    my ($onloan) = $sth->fetchrow;
2245
2257
2246
    my $item = GetItem($itemnumber);
2258
    my $item = GetItem($itemnumber);
2247
2259
2248
    if ($onloan){
2260
    if ($onloan) {
2249
        $error = "book_on_loan" 
2261
        $status = "book_on_loan";
2250
    }
2262
    }
2251
    elsif ( defined C4::Context->userenv
2263
    elsif ( defined C4::Context->userenv
2252
        and !C4::Context->IsSuperLibrarian()
2264
        and !C4::Context->IsSuperLibrarian()
2253
        and C4::Context->preference("IndependentBranches")
2265
        and C4::Context->preference("IndependentBranches")
2254
        and ( C4::Context->userenv->{branch} ne $item->{'homebranch'} ) )
2266
        and ( C4::Context->userenv->{branch} ne $item->{'homebranch'} ) )
2255
    {
2267
    {
2256
        $error = "not_same_branch";
2268
        $status = "not_same_branch";
2257
    }
2269
    }
2258
	else{
2270
    else {
2259
        # check it doesn't have a waiting reserve
2271
        # check it doesn't have a waiting reserve
2260
        $sth = $dbh->prepare(q{
2272
        $sth = $dbh->prepare(
2273
            q{
2261
            SELECT COUNT(*) FROM reserves
2274
            SELECT COUNT(*) FROM reserves
2262
            WHERE (found = 'W' OR found = 'T')
2275
            WHERE (found = 'W' OR found = 'T')
2263
            AND itemnumber = ?
2276
            AND itemnumber = ?
2264
        });
2277
        }
2278
        );
2265
        $sth->execute($itemnumber);
2279
        $sth->execute($itemnumber);
2266
        my ($reserve) = $sth->fetchrow;
2280
        my ($reserve) = $sth->fetchrow;
2267
        if ($reserve){
2281
        if ($reserve) {
2268
            $error = "book_reserved";
2282
            $status = "book_reserved";
2269
        } elsif ($countanalytics > 0){
2270
		$error = "linked_analytics";
2271
	} else {
2272
            DelItem(
2273
                {
2274
                    biblionumber => $biblionumber,
2275
                    itemnumber   => $itemnumber
2276
                }
2277
            );
2278
            return 1;
2279
        }
2283
        }
2284
        elsif ( $countanalytics > 0 ) {
2285
            $status = "linked_analytics";
2286
        }
2287
        else {
2288
            $status = 1;
2289
        }
2290
    }
2291
    return $status;
2292
}
2293
2294
=head2 DelItemCheck
2295
2296
   DelItemCheck($dbh, $biblionumber, $itemnumber);
2297
2298
Exported function (core API) for deleting an item record in Koha if there no current issue.
2299
2300
DelItemCheck wraps ItemSafeToDelete around DelItem.
2301
2302
It takes a database handle, biblionumber and itemnumber as arguments, and can optionally take a hashref with a 'do_not_commit' flag:
2303
2304
    DelItemCheck(  $dbh, $biblionumber, $itemnumber, { do_not_commit => 1 } );
2305
2306
This is done so that command line scripts calling DelItemCheck have the option of doing a 'dry-run'.
2307
=cut
2308
2309
sub DelItemCheck {
2310
    my ( $dbh, $biblionumber, $itemnumber, $options ) = @_;
2311
    my $commit = ( defined $options && $options->{do_not_commit} eq 1 ) ? 0 : 1;
2312
    my $status = ItemSafeToDelete( $dbh, $biblionumber, $itemnumber );
2313
2314
    if ( $status == 1 && $commit ) {
2315
        DelItem(
2316
            {
2317
                biblionumber => $biblionumber,
2318
                itemnumber   => $itemnumber
2319
            }
2320
        );
2280
    }
2321
    }
2281
    return $error;
2322
    return $status;
2282
}
2323
}
2283
2324
2284
=head2 _koha_modify_item
2325
=head2 _koha_modify_item
(-)a/t/db_dependent/Items_DelItemCheck.t (-1 / +145 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
3
use MARC::Record;
4
use C4::Biblio;
5
use C4::Circulation;
6
use C4::Members;
7
use t::lib::Mocks;
8
9
10
use Test::More tests => 10;
11
12
*C4::Context::userenv = \&Mock_userenv;
13
14
BEGIN {
15
    use_ok('C4::Items');
16
}
17
18
my $dbh = C4::Context->dbh;
19
$dbh->{AutoCommit} = 0;
20
$dbh->{RaiseError} = 1;
21
22
my ( $biblionumber, $bibitemnum ) = get_biblio();
23
24
# book_on_loan
25
26
my ( $borrowernumber, $borrower ) = get_borrower();
27
my ( $itemnumber, $item )         = get_item( $biblionumber );
28
AddIssue( $borrower, 'i_dont_exist' );
29
30
is(
31
    ItemSafeToDelete($dbh, $biblionumber, $itemnumber),
32
    'book_on_loan',
33
    'ItemSafeToDelete reports item on loan',
34
);
35
36
is(
37
    DelItemCheck($dbh, $biblionumber, $itemnumber),
38
    'book_on_loan',
39
    'item that is on loan cannot be deleted',
40
);
41
42
AddReturn('i_dont_exist', 'CPL');
43
44
# book_reserved is tested in t/db_dependent/Reserves.t
45
46
# not_same_branch
47
t::lib::Mocks::mock_preference('IndependentBranches', 1);
48
ModItem( { homebranch => 'FPL', holdingbranch => 'FPL' }, $biblionumber, $itemnumber );
49
50
is(
51
    ItemSafeToDelete($dbh, $biblionumber, $itemnumber),
52
    'not_same_branch',
53
    'ItemSafeToDelete reports IndependentBranches restriction',
54
);
55
56
is(
57
    DelItemCheck($dbh, $biblionumber, $itemnumber),
58
    'not_same_branch',
59
    'IndependentBranches prevents deletion at another branch',
60
);
61
62
ModItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber, $itemnumber );
63
64
# linked_analytics
65
66
{ # codeblock to limit scope of $module->mock
67
68
    my $module = Test::MockModule->new('C4::Items');
69
    $module->mock( GetAnalyticsCount => sub { return 1 } );
70
71
    is(
72
        ItemSafeToDelete($dbh, $biblionumber, $itemnumber),
73
        'linked_analytics',
74
        'ItemSafeToDelete reports linked analytics',
75
    );
76
77
    is(
78
        DelItemCheck($dbh, $biblionumber, $itemnumber),
79
        'linked_analytics',
80
        'Linked analytics prevents deletion of item',
81
    );
82
83
}
84
85
is(
86
    ItemSafeToDelete($dbh, $biblionumber, $itemnumber),
87
    1,
88
    'ItemSafeToDelete shows item safe to delete'
89
);
90
91
DelItemCheck($dbh, $biblionumber, $itemnumber, { do_not_commit => 1 } );
92
93
my $testitem = GetItem( $itemnumber );
94
95
is( $testitem->{itemnumber} ,  $itemnumber,
96
    "DelItemCheck should not delete item if 'do_not_commit' is set"
97
);
98
99
DelItemCheck( $dbh, $biblionumber, $itemnumber );
100
101
$testitem = GetItem( $itemnumber );
102
103
is( $testitem->{itemnumber}, undef,
104
    "DelItemCheck should delete item if 'do_not_commit' not set"
105
);
106
107
# End of testing
108
109
# Helper methods to set up Biblio, Item, and Borrower.
110
sub get_biblio {
111
    my $bib = MARC::Record->new();
112
    $bib->append_fields(
113
        MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
114
        MARC::Field->new( '245', ' ', ' ', a => 'Silence in the library' ),
115
    );
116
    my ( $bibnum, $bibitemnum ) = AddBiblio( $bib, '' );
117
    return ( $bibnum, $bibitemnum );
118
}
119
120
sub get_item {
121
    my $biblionumber = shift;
122
    my ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
123
      AddItem( { homebranch => 'CPL', holdingbranch => 'CPL', barcode => 'i_dont_exist' }, $biblionumber );
124
    my $item = GetItem( $itemnumber );
125
    return ( $itemnumber, $item );
126
}
127
128
sub get_borrower {
129
    my $borrowernumber = AddMember(
130
        firstname =>  'my firstname',
131
        surname => 'my surname',
132
        categorycode => 'S',
133
        branchcode => 'CPL',
134
    );
135
136
    my $borrower = GetMember( borrowernumber => $borrowernumber );
137
    return ( $borrowernumber, $borrower );
138
}
139
140
# C4::Context->userenv
141
sub Mock_userenv {
142
        return { flags => 0, branch => 'CPL' };
143
}
144
145
$dbh->rollback;

Return to bug 14504