Bug 25095: Remove warn left in FeePayment.pm
[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     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;
67     }
68
69     my $priority = C4::Reserves::CalculatePriority($item->biblionumber);
70     AddReserve(
71         {
72             priority       => $priority,
73             branch         => $branch,
74             borrowernumber => $patron->borrowernumber,
75             biblionumber   => $item->biblionumber
76         }
77     );
78
79     # unfortunately no meaningful return value
80     $self->ok(1);
81     return $self;
82 }
83
84 sub drop_hold {
85         my $self = shift;
86         unless ($self->{patron}) {
87                 $self->screen_msg('drop_hold called with undefined patron');
88                 $self->ok(0);
89                 return $self;
90         }
91     my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
92     unless ($patron) {
93                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
94                 $self->ok(0);
95                 return $self;
96         }
97
98     my $item = Koha::Items->find({ barcode => $self->{item}->id });
99     my $holds = $item->holds->search({ borrowernumber => $patron->borrowernumber });
100
101     return $self unless $holds->count;
102
103     $holds->next->cancel;
104
105         $self->ok(1);
106         return $self;
107 }
108
109 sub change_hold {
110         my $self = shift;
111         unless ($self->{patron}) {
112                 $self->screen_msg('change_hold called with undefined patron');
113                 $self->ok(0);
114                 return $self;
115         }
116     my $patron = Koha::Patrons->find( { cardnumber => $self->{patron}->id } );
117     unless ($patron) {
118                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
119                 $self->ok(0);
120                 return $self;
121         }
122     my $item = Koha::Items->find({ barcode => $self->{item}->id });
123     unless ($item) {
124                 $self->screen_msg('No biblio record matches barcode "' . $self->{item}->id . '".');
125                 $self->ok(0);
126                 return $self;
127         }
128         my $branch = ($self->pickup_location || $self->{patron}->branchcode);
129         unless ($branch) {
130                 $self->screen_msg('No branch specified (or found w/ patron).');
131                 $self->ok(0);
132                 return $self;
133         }
134     ModReserve({ biblionumber => $item->biblionumber, borrowernumber => $patron->borrowernumber, branchcode => $branch });
135
136         $self->ok(1);
137         return $self;
138 }
139
140 1;
141 __END__