Got drop down list appearing when more than one borrower matches
[koha.git] / koha-curses / circ.pl
1 #!/usr/bin/perl -w
2
3 # $Id$
4
5 # Copyright 2005 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use lib '/usr/local/koha/intranet/modules';
24 use Curses::UI;
25 use C4::Circulation::Circ2;
26 use C4::Search;
27 use C4::Print;
28
29 my $cui = new Curses::UI( -color_support => 1 );
30
31 my @menu = (
32     {
33         -label   => 'File',
34         -submenu => [
35             { -label => 'Issues   ^I', -value => \&issues },
36             { -label => 'Returns  ^R', -value => \&returns },
37             { -label => 'Exit     ^Q', -value => \&exit_dialog }
38         ]
39     },
40 );
41
42 my $menu = $cui->add(
43     'menu', 'Menubar',
44     -menu => \@menu,
45     -fg   => "blue",
46 );
47
48 my $win1 = $cui->add(
49     'win1', 'Window',
50     -border => 1,
51     -y      => 1,
52     -bfg    => 'red',
53     -width  => 40,
54 );
55
56 my $win2 = $cui->add(
57     'win2', 'Window',
58     -border => 1,
59     -y      => 1,
60     -x      => 40,
61     -height => 10,
62     -bfg    => 'red',
63 );
64
65 my $win3 = $cui->add(
66     'win3', 'Window',
67     -border => 1,
68     -y      => 11,
69     -x      => 40,
70     -height => 10,
71     -bfg    => 'red',
72 );
73
74 my $texteditor = $win1->add( "text", "TextEditor",
75     -text =>
76       "This is the first cut of a \ncirculations system using Curses::UI\n"
77       . "Use the menus (or the keyboard\nshortcuts) to choose issues or \nreturns"
78 );
79
80 $cui->set_binding( sub { $menu->focus() }, "\cX" );
81 $cui->set_binding( \&exit_dialog, "\cQ" );
82 $cui->set_binding( \&issues,      "\cI" );
83 $cui->set_binding( \&returns,     "\cR" );
84
85 $texteditor->focus();
86 $cui->mainloop();
87
88 sub exit_dialog() {
89     my $return = $cui->dialog(
90         -message => "Do you really want to quit?",
91         -title   => "Are you sure???",
92         -buttons => [ 'yes', 'no' ],
93
94     );
95
96     exit(0) if $return;
97 }
98
99 sub returns {
100     my $barcode = $cui->question(
101         -title    => 'Returns',
102         -question => 'Barcode'
103     );
104     my $branch = 'MAIN';
105     my %env;
106     if ($barcode) {
107         my ( $returned, $messages, $iteminformation, $borrower ) =
108           returnbook( $barcode, $branch );
109         if ( $borrower && $borrower->{'borrowernumber'} ) {
110             $borrower =
111               getpatroninformation( \%env, $borrower->{'borrowernumber'}, 0 );
112             $win1->delete('borrowerdata');
113             my $borrowerdata = $win1->add(
114                 'borrowerdata', 'TextViewer',
115                 -text => "Cardnumber: $borrower->{'cardnumber'}\n"
116                   . "Name: $borrower->{'title'} $borrower->{'firstname'} $borrower->{'surname'}\n"
117
118             );
119
120             $borrowerdata->focus();
121         }
122         else {
123             $cui->error( -message => 'That item isnt on loan' );
124         }
125     }
126 }
127
128 sub issues {
129
130     # this routine does the actual issuing
131
132     my %env;
133     my $borrowernumber;
134     my $borrowerlist;
135
136    # the librarian can overide system issue date, need to fetch values from them
137     my $year;
138     my $month;
139     my $day;
140
141     $win1->delete('text');
142
143     # get a cardnumber or a name
144     my $cardnumber = $cui->question(
145         -title    => 'Issues',
146         -question => 'Cardnumber'
147     );
148
149     # search for that borrower
150     my ( $count, $borrowers ) =
151       BornameSearch( \%env, $cardnumber, 'cardnumber', 'web' );
152     my @borrowers = @$borrowers;
153     if ( $#borrowers == -1 ) {
154         $cui->error( -message =>
155               'No borrowers match that name or cardnumber please try again.' );
156     }
157     elsif ( $#borrowers == 0 ) {
158         $borrowernumber = $borrowers[0]->{'borrowernumber'};
159     }
160     else {
161         $borrowerlist = \@borrowers;
162     }
163
164     if ($borrowernumber) {
165
166         # if we have one single borrower, we can start issuing
167         doissues( $borrowernumber, \%env, $year, $month, $day );
168     }
169     elsif ($borrowerlist) {
170
171         # choose from a list then start issuing
172         my @borrowernumbers;
173         my %borrowernames;
174         foreach my $bor (@$borrowerlist) {
175             push @borrowernumbers, $bor->{'borrowernumber'};
176             $borrowernames{ $bor->{'borrowernumber'} } =
177               "$bor->{'cardnumber'} $bor->{'firstname'} $bor->{surname}";
178         }
179         $win1->delete('mypopupbox');
180         my $popupbox = $win1->add(
181             'mypopupbox', 'Popupmenu',
182             -values   => [@borrowernumbers],
183             -labels   => \%borrowernames,
184             -onchange => \&dolistissues,
185         );
186
187         $popupbox->focus();
188         $borrowernumber = $popupbox->get();
189         if ($borrowernumber) {
190             doissues( $borrowernumber, \%env, $year, $month, $day );
191         }
192     }
193     else {
194     }
195 }
196
197 sub dolistissues {
198     my $list           = shift;
199     my $borrowernumber = $list->get();
200     doissues($borrowernumber);
201 }
202
203 sub doissues {
204     my ( $borrowernumber, $env, $year, $month, $day ) = @_;
205     my $datedue;
206
207     my $borrower = getpatroninformation( $env, $borrowernumber, 0 );
208     $win1->delete('borrowerdata');
209     my $borrowerdata = $win1->add( 'borrowerdata', 'TextViewer',
210         -text => "Cardnumber: $borrower->{'cardnumber'}\n"
211           . "Name: $borrower->{'title'} $borrower->{'firstname'} $borrower->{'surname'}"
212     );
213
214     $borrowerdata->focus();
215
216     $win3->delete('pastissues');
217     my $issueslist = getissues($borrower);
218     my $oldissues;
219     foreach my $it ( keys %$issueslist ) {
220         $oldissues .=
221           $issueslist->{$it}->{'barcode'}
222           . " $issueslist->{$it}->{'title'} $issueslist->{$it}->{'date_due'}\n";
223
224     }
225
226     my $pastissues =
227       $win3->add( 'pastissues', 'TextViewer', -text => $oldissues, );
228     $pastissues->focus();
229
230     $win2->delete('currentissues');
231     my $currentissues =
232       $win2->add( 'currentissues', 'TextViewer',
233         -text => "Todays issues go here", );
234     $currentissues->focus();
235
236     # go into a loop issuing until a blank barcode is given
237     while ( my $barcode = $cui->question( -question => 'Barcode' ) ) {
238         my $issues;
239         my $issueconfirmed;
240         my ( $error, $question ) =
241           canbookbeissued( $env, $borrower, $barcode, $year, $month, $day );
242         my $noerror    = 1;
243         my $noquestion = 1;
244         foreach my $impossible ( keys %$error ) {
245             $cui->error( -message => $impossible );
246             $noerror = 0;
247         }
248         if ($noerror) {
249
250             # no point asking confirmation questions if we cant issue
251             foreach my $needsconfirmation ( keys %$question ) {
252                 $noquestion     = 0;
253                 $issueconfirmed = $cui->dialog(
254                     -message =>
255 "$needsconfirmation $question->{$needsconfirmation} Issue anyway?",
256                     -title   => "Confirmation",
257                     -buttons => [ 'yes', 'no' ],
258
259                 );
260
261             }
262         }
263         if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
264             issuebook( $env, $borrower, $barcode, $datedue );
265             $issues .= "$barcode";
266             $win2->delete('currentissues');
267             $currentissues =
268               $win2->add( 'currentissues', 'TextViewer', -text => $issues, );
269
270         }
271
272     }
273
274     # finished issuing
275     my $printconfirm = $cui->dialog(
276         -message => "Print a slip for this borrower?",
277         -title   => "Print Slip",
278         -buttons => [ 'yes', 'no' ],
279
280     );
281     if ($printconfirm) {
282         printslip( $env, $borrowernumber );
283     }
284 }