d834516d102a0c0b2eb433dbf4046bb2b6f6fb9d
[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;       # AddReserve
11 use Koha::Holds;
12 use Koha::Patrons;
13 use parent qw(C4::SIP::ILS::Transaction);
14
15 use Koha::Items;
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     unless ( $self->{patron} ) {
41         $self->screen_msg('do_hold called with undefined patron');
42         $self->ok(0);
43         return $self;
44     }
45     my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
46     unless ($patron) {
47         $self->screen_msg( 'No borrower matches cardnumber "' . $self->{patron}->id . '".' );
48         $self->ok(0);
49         return $self;
50     }
51     my $item = Koha::Items->find({ barcode => $self->{item}->id });
52     unless ($item) {
53         $self->screen_msg( 'No biblio record matches barcode "' . $self->{item}->id . '".' );
54         $self->ok(0);
55         return $self;
56     }
57     my $branch = ( $self->pickup_location || $self->{patron}->branchcode );
58     unless ($branch) {
59         $self->screen_msg('No branch specified (or found w/ patron).');
60         $self->ok(0);
61         return $self;
62     }
63     AddReserve( $branch, $patron->borrowernumber, $item->biblionumber );
64
65     # unfortunately no meaningful return value
66     $self->ok(1);
67     return $self;
68 }
69
70 sub drop_hold {
71         my $self = shift;
72         unless ($self->{patron}) {
73                 $self->screen_msg('drop_hold called with undefined patron');
74                 $self->ok(0);
75                 return $self;
76         }
77     my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
78     unless ($patron) {
79                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
80                 $self->ok(0);
81                 return $self;
82         }
83     my $item = Koha::Items->find({ barcode => $self->{item}->id });
84
85     my $holds = Koha::Holds->search(
86         {
87             biblionumber   => $item->biblionumber,
88             itemnumber     => $self->{item}->id,
89             borrowernumber => $patron->borrowernumber
90         }
91     );
92     return $self unless $holds->count;
93
94     $holds->next->cancel;
95
96         $self->ok(1);
97         return $self;
98 }
99
100 sub change_hold {
101         my $self = shift;
102         unless ($self->{patron}) {
103                 $self->screen_msg('change_hold called with undefined patron');
104                 $self->ok(0);
105                 return $self;
106         }
107     my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
108     unless ($patron) {
109                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
110                 $self->ok(0);
111                 return $self;
112         }
113     my $item = Koha::Items->find({ barcode => $self->{item}->id });
114     unless ($item) {
115                 $self->screen_msg('No biblio record matches barcode "' . $self->{item}->id . '".');
116                 $self->ok(0);
117                 return $self;
118         }
119         my $branch = ($self->pickup_location || $self->{patron}->branchcode);
120         unless ($branch) {
121                 $self->screen_msg('No branch specified (or found w/ patron).');
122                 $self->ok(0);
123                 return $self;
124         }
125     ModReserve({ biblionumber => $item->biblionumber, borrowernumber => $patron->borrowernumber, branchcode => $branch });
126
127         $self->ok(1);
128         return $self;
129 }
130
131 1;
132 __END__