Bug 29094: Adding hold via SIP should check if patron can hold item first
[koha.git] / C4 / SIP / ILS / Transaction / Hold.pm
1 #
2 # status of a Hold transaction
3
4 package C4::SIP::ILS::Transaction::Hold;
5
6 use Modern::Perl;
7
8 use C4::SIP::ILS::Transaction;
9
10 use C4::Reserves qw( CalculatePriority AddReserve ModReserve CanItemBeReserved );
11 use Koha::Holds;
12 use Koha::Patrons;
13 use Koha::Items;
14
15 use parent qw(C4::SIP::ILS::Transaction);
16
17 my %fields = (
18         expiration_date => 0,
19         pickup_location => undef,
20         constraint_type => undef,
21 );
22
23 sub new {
24         my $class = shift;
25         my $self = $class->SUPER::new();
26     foreach my $element (keys %fields) {
27                 $self->{_permitted}->{$element} = $fields{$element};
28         }
29         @{$self}{keys %fields} = values %fields;
30         return bless $self, $class;
31 }
32
33 sub queue_position {
34     my $self = shift;
35     return $self->item->hold_queue_position($self->patron->id);
36 }
37
38 sub do_hold {
39     my $self = shift;
40     my $patron = Koha::Patrons->find( $self->{patron}->borrowernumber );
41     unless ( $patron ) {
42         $self->screen_msg('do_hold called with undefined patron');
43         $self->ok(0);
44         return $self;
45     }
46     my $item = Koha::Items->find({ barcode => $self->{item}->id });
47     unless ($item) {
48         $self->screen_msg( 'No biblio record matches barcode "' . $self->{item}->id . '".' );
49         $self->ok(0);
50         return $self;
51     }
52     my $branch = ( $self->pickup_location || $self->{patron}->{branchcode} );
53     unless ($branch) {
54         $self->screen_msg('No branch specified (or found w/ patron).');
55         $self->ok(0);
56         return $self;
57     }
58     unless ( $item->can_be_transferred( { to => Koha::Libraries->find( $branch ) } ) ) {
59         $self->screen_msg('Item cannot be transferred.');
60         $self->ok(0);
61         return $self;
62     }
63
64     my $canReserve = CanItemBeReserved($patron, $item, $branch);
65     if ($canReserve->{status} eq 'OK') {
66         my $priority = C4::Reserves::CalculatePriority($item->biblionumber);
67         AddReserve(
68             {
69                 priority       => $priority,
70                 branchcode     => $branch,
71                 borrowernumber => $patron->borrowernumber,
72                 biblionumber   => $item->biblionumber
73             }
74         );
75
76         $self->ok(1);
77     } else {
78         $self->ok(0);
79     }
80
81     return $self;
82 }
83
84 sub drop_hold {
85         my $self = shift;
86     my $patron = Koha::Patrons->find( $self->{patron}->borrowernumber );
87     unless ($patron) {
88         $self->screen_msg('drop_hold called with undefined patron');
89         $self->ok(0);
90         return $self;
91     }
92
93     my $item = Koha::Items->find({ barcode => $self->{item}->id });
94     my $holds = $item->holds->search({ borrowernumber => $patron->borrowernumber });
95
96     return $self unless $holds->count;
97
98     $holds->next->cancel;
99
100         $self->ok(1);
101         return $self;
102 }
103
104 sub change_hold {
105         my $self = shift;
106     my $patron = Koha::Patrons->find( $self->{patron}->borrowernumber );
107     unless ($patron) {
108         $self->screen_msg('change_hold called with undefined patron');
109         $self->ok(0);
110         return $self;
111     }
112     my $item = Koha::Items->find({ barcode => $self->{item}->id });
113     unless ($item) {
114                 $self->screen_msg('No biblio record matches barcode "' . $self->{item}->id . '".');
115                 $self->ok(0);
116                 return $self;
117         }
118         my $branch = ($self->pickup_location || $self->{patron}->branchcode);
119         unless ($branch) {
120                 $self->screen_msg('No branch specified (or found w/ patron).');
121                 $self->ok(0);
122                 return $self;
123         }
124     ModReserve({ biblionumber => $item->biblionumber, borrowernumber => $patron->borrowernumber, branchcode => $branch });
125
126         $self->ok(1);
127         return $self;
128 }
129
130 1;
131 __END__