Start of a module to handle circulation use Curses::UI
[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
28 my $cui = new Curses::UI( -color_support => 1 );
29
30 my @menu = (
31     {
32         -label   => 'File',
33         -submenu => [
34             { -label => 'Issues   ^I', -value => \&issues },
35             { -label => 'Returns  ^R', -value => \&returns },
36             { -label => 'Exit     ^Q', -value => \&exit_dialog }
37         ]
38     },
39 );
40
41 my $menu = $cui->add(
42     'menu', 'Menubar',
43     -menu => \@menu,
44     -fg   => "blue",
45 );
46
47 my $win1 = $cui->add(
48     'win1', 'Window',
49     -border => 1,
50     -y      => 1,
51     -bfg    => 'red',
52     -width  => 40,
53 );
54
55 my $win2 = $cui->add(
56     'win2', 'Window',
57     -border => 1,
58     -y      => 1,
59     -x      => 40,
60     -height => 10,
61     -bfg    => 'red',
62 );
63
64 my $win3 = $cui->add(
65     'win3', 'Window',
66     -border => 1,
67     -y      => 11,
68     -x      => 40,
69     -height => 10,
70     -bfg    => 'red',
71 );
72
73 my $texteditor =
74   $win1->add( "text", "TextEditor",
75     -text => "Here is some text\n" . "And some more" );
76
77 $cui->set_binding( sub { $menu->focus() }, "\cX" );
78 $cui->set_binding( \&exit_dialog, "\cQ" );
79 $cui->set_binding( \&issues,      "\cI" );
80 $cui->set_binding( \&returns,     "\cR" );
81
82 $texteditor->focus();
83 $cui->mainloop();
84
85 sub exit_dialog() {
86     my $return = $cui->dialog(
87         -message => "Do you really want to quit?",
88         -title   => "Are you sure???",
89         -buttons => [ 'yes', 'no' ],
90
91     );
92
93     exit(0) if $return;
94 }
95
96 sub returns {
97     my $barcode = $cui->question(
98         -title    => 'Returns',
99         -question => 'Barcode'
100     );
101     my $branch = 'MAIN';
102     my %env;
103     if ($barcode) {
104         my ( $returned, $messages, $iteminformation, $borrower ) =
105           returnbook( $barcode, $branch );
106         if ( $borrower && $borrower->{'borrowernumber'} ) {
107             $borrower =
108               getpatroninformation( \%env, $borrower->{'borrowernumber'}, 0 );
109             $win1->delete('borrowerdata');
110             my $borrowerdata = $win1->add( 'borrowerdata', 'TextViewer',
111                 -text => "Cardnumber: $borrower->{'cardnumber'}\n"
112                   . "Name: $borrower->{'title'} $borrower->{'firstname'} $borrower->{'surname'}"
113             );
114
115             $borrowerdata->focus();
116         }
117         else {
118             $cui->error( -message => 'That item isnt on loan' );
119         }
120     }
121 }
122
123 sub issues {
124
125     # this routine does the actual issuing
126
127     my %env;
128     my $borrowernumber;
129     my $borrowerlist;
130
131    # the librarian can overide system issue date, need to fetch values from them
132     my $year;
133     my $month;
134     my $day;
135     my $datedue;
136
137     $win1->delete('text');
138
139     # get a cardnumber or a name
140     my $cardnumber = $cui->question(
141         -title    => 'Issues',
142         -question => 'Cardnumber'
143     );
144
145     # search for that borrower
146     my ( $count, $borrowers ) =
147       BornameSearch( \%env, $cardnumber, 'cardnumber', 'web' );
148     my @borrowers = @$borrowers;
149     if ( $#borrowers == -1 ) {
150         $cui->error( -message =>
151               'No borrowers match that name or cardnumber please try again.' );
152     }
153     elsif ( $#borrowers == 0 ) {
154         $borrowernumber = $borrowers[0]->{'borrowernumber'};
155     }
156     else {
157         $borrowerlist = \@borrowers;
158     }
159
160     if ($borrowernumber) {
161
162         # if we have one single borrower, we can start issuing
163         my $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
164         $win1->delete('borrowerdata');
165         my $borrowerdata = $win1->add( 'borrowerdata', 'TextViewer',
166             -text => "Cardnumber: $borrower->{'cardnumber'}\n"
167               . "Name: $borrower->{'title'} $borrower->{'firstname'} $borrower->{'surname'}"
168         );
169
170         $borrowerdata->focus();
171
172         $win3->delete('pastissues');
173         my $issueslist = getissues($borrower);
174         my $oldissues;
175         foreach my $it ( keys %$issueslist ) {
176             $oldissues .=
177               $issueslist->{$it}->{'barcode'}
178               . " $issueslist->{$it}->{'title'} $issueslist->{$it}->{'date_due'}\n";
179
180         }
181
182         my $pastissues =
183           $win3->add( 'pastissues', 'TextViewer', -text => $oldissues, );
184         $pastissues->focus();
185
186         $win2->delete('currentissues');
187         my $currentissues =
188           $win2->add( 'currentissues', 'TextViewer',
189             -text => "Todays issues go here", );
190         $currentissues->focus();
191
192         # go into a loop issuing until a blank barcode is given
193         while ( my $barcode = $cui->question( -question => 'Barcode' ) ) {
194             my $issueconfirmed;
195             my ( $error, $question ) =
196               canbookbeissued( \%env, $borrower, $barcode, $year, $month,
197                 $day );
198             my $noerror    = 1;
199             my $noquestion = 1;
200             foreach my $impossible ( keys %$error ) {
201                 $cui->error( -message => $impossible );
202                 $noerror = 0;
203             }
204
205             foreach my $needsconfirmation ( keys %$question ) {
206                 $noquestion     = 0;
207                 $issueconfirmed = $cui->dialog(
208                     -message => $needsconfirmation,
209                     -title   => "Confirmation",
210                     -buttons => [ 'yes', 'no' ],
211
212                 );
213
214             }
215             if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
216                 issuebook( \%env, $borrower, $barcode, $datedue );
217             }
218
219         }
220
221     }
222     elsif ($borrowerlist) {
223         my $listbox = $win1->add(
224             'mylistbox',
225             'Listbox',
226             -values => [ 1, 2, 3 ],
227             -labels => {
228                 1 => 'One',
229                 2 => 'Two',
230                 3 => 'Three'
231             },
232             -radio => 1,
233         );
234
235         $listbox->focus();
236         my $selected = $listbox->get();
237     }
238     else {
239     }
240 }