Bug 19809: Re-allow to call Koha::Objects::find in list context
[koha.git] / C4 / SIP / ILS / Transaction / Hold.pm
blobdaefee5d41de5a1d8e215666b799bedf944575c3
2 # status of a Hold transaction
4 package C4::SIP::ILS::Transaction::Hold;
6 use Modern::Perl;
8 use C4::SIP::ILS::Transaction;
10 use C4::Reserves; # AddReserve
11 use Koha::Holds;
12 use Koha::Patrons;
13 use parent qw(C4::SIP::ILS::Transaction);
15 use Koha::Items;
17 my %fields = (
18 expiration_date => 0,
19 pickup_location => undef,
20 constraint_type => undef,
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};
29 @{$self}{keys %fields} = values %fields;
30 return bless $self, $class;
33 sub queue_position {
34 my $self = shift;
35 return $self->item->hold_queue_position($self->patron->id);
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;
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;
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;
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;
63 unless ( $item->can_be_transferred( { to => Koha::Libraries->find( $branch ) } ) ) {
64 $self->screen_msg('Item cannot be transferred.');
65 $self->ok(0);
66 return $self;
69 AddReserve( $branch, $patron->borrowernumber, $item->biblionumber );
71 # unfortunately no meaningful return value
72 $self->ok(1);
73 return $self;
76 sub drop_hold {
77 my $self = shift;
78 unless ($self->{patron}) {
79 $self->screen_msg('drop_hold called with undefined patron');
80 $self->ok(0);
81 return $self;
83 my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
84 unless ($patron) {
85 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
86 $self->ok(0);
87 return $self;
90 my $item = Koha::Items->find({ barcode => $self->{item}->id });
91 my $holds = $item->holds->search({ borrowernumber => $patron->borrowernumber });
93 return $self unless $holds->count;
95 $holds->next->cancel;
97 $self->ok(1);
98 return $self;
101 sub change_hold {
102 my $self = shift;
103 unless ($self->{patron}) {
104 $self->screen_msg('change_hold called with undefined patron');
105 $self->ok(0);
106 return $self;
108 my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
109 unless ($patron) {
110 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
111 $self->ok(0);
112 return $self;
114 my $item = Koha::Items->find({ barcode => $self->{item}->id });
115 unless ($item) {
116 $self->screen_msg('No biblio record matches barcode "' . $self->{item}->id . '".');
117 $self->ok(0);
118 return $self;
120 my $branch = ($self->pickup_location || $self->{patron}->branchcode);
121 unless ($branch) {
122 $self->screen_msg('No branch specified (or found w/ patron).');
123 $self->ok(0);
124 return $self;
126 ModReserve({ biblionumber => $item->biblionumber, borrowernumber => $patron->borrowernumber, branchcode => $branch });
128 $self->ok(1);
129 return $self;
133 __END__