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