Merge remote-tracking branch 'origin/new/bug_5337'
[koha.git] / C4 / SIP / ILS / Transaction / Hold.pm
1 #
2 # status of a Hold transaction
3
4 package ILS::Transaction::Hold;
5
6 use warnings;
7 use strict;
8
9 use ILS;
10 use ILS::Transaction;
11
12 use C4::Reserves;       # AddReserve
13 use C4::Members;        # GetMember
14 use C4::Biblio;         # GetBiblioFromItemNumber GetBiblioItemByBiblioNumber
15
16 use vars qw($VERSION @ISA);
17
18 BEGIN {
19     $VERSION = 3.07.00.049;
20             @ISA = qw(ILS::Transaction);
21 }
22
23 my %fields = (
24         expiration_date => 0,
25         pickup_location => undef,
26         constraint_type => undef,
27 );
28
29 sub new {
30         my $class = shift;
31         my $self = $class->SUPER::new();
32         my $element;
33         foreach $element (keys %fields) {
34                 $self->{_permitted}->{$element} = $fields{$element};
35         }
36         @{$self}{keys %fields} = values %fields;
37         return bless $self, $class;
38 }
39
40 sub queue_position {
41     my $self = shift;
42     return $self->item->hold_queue_position($self->patron->id);
43 }
44
45 sub do_hold {
46         my $self = shift;
47         unless ($self->{patron}) {
48                 $self->screen_msg('do_hold called with undefined patron');
49                 $self->ok(0);
50                 return $self;
51         }
52         my $borrower = GetMember( 'cardnumber'=>$self->{patron}->id);
53         unless ($borrower) {
54                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
55                 $self->ok(0);
56                 return $self;
57         }
58         my $bib = GetBiblioFromItemNumber(undef, $self->{item}->id);
59         unless ($bib) {
60                 $self->screen_msg('No biblio record matches barcode "' . $self->{item}->id . '".');
61                 $self->ok(0);
62                 return $self;
63         }
64         my $branch = ($self->pickup_location || $self->{patron}->branchcode);
65         unless ($branch) {
66                 $self->screen_msg('No branch specified (or found w/ patron).');
67                 $self->ok(0);
68                 return $self;
69         }
70         my $bibno = $bib->{biblionumber};
71         AddReserve($branch, $borrower->{borrowernumber}, 
72                                 $bibno, 'a', GetBiblioItemByBiblioNumber($bibno)) ;
73                 # unfortunately no meaningful return value
74         $self->ok(1);
75         return $self;
76 }
77
78 sub drop_hold {
79         my $self = shift;
80         unless ($self->{patron}) {
81                 $self->screen_msg('drop_hold called with undefined patron');
82                 $self->ok(0);
83                 return $self;
84         }
85         my $borrower = GetMember( 'cardnumber'=>$self->{patron}->id);
86         unless ($borrower) {
87                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
88                 $self->ok(0);
89                 return $self;
90         }
91         my $bib = GetBiblioFromItemNumber(undef, $self->{item}->id);
92         # FIXME: figure out if it is a item or title hold.  Till then, cancel both.
93         CancelReserve($bib->{biblionumber}, undef, $borrower->{borrowernumber});
94         CancelReserve(undef, $self->{item}->id, $borrower->{borrowernumber});
95                 # unfortunately no meaningful return value here either
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 $borrower = GetMember( 'cardnumber'=>$self->{patron}->id);
108         unless ($borrower) {
109                 $self->screen_msg('No borrower matches cardnumber "' . $self->{patron}->id . '".');
110                 $self->ok(0);
111                 return $self;
112         }
113         my $bib = GetBiblioFromItemNumber(undef, $self->{item}->id);
114         unless ($bib) {
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         my $bibno = $bib->{biblionumber};
126         ModReserve($bibno, $borrower->{borrowernumber}, $branch, GetBiblioItemByBiblioNumber($bibno));
127                 # unfortunately no meaningful return value
128                 # ModReserve needs to be fixed to maintain priority by iteself (Feb 2008)
129         $self->ok(1);
130         return $self;
131 }
132 1;
133 __END__
134
135 # 11 friggin arguments
136 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$startdate,$notes,$title,$checkitem,$found)
137
138 ModReserve($rank, $biblio, $borrower, $branch , $itemnumber)
139