Added a FIXME comment.
[koha.git] / C4 / InterfaceCDK.pm
1 package C4::InterfaceCDK; #assumes C4/InterfaceCDK
2
3 #uses Newt
4
5 # Copyright 2000-2002 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 # FIXME - This module doesn't compile!
23
24 use C4::Format;
25 use strict;
26 use Cdk;
27 use Date::Manip;
28 use C4::Accounts;
29 use C4::Circulation::Borrissues;
30 use C4::Circulation::Renewals;
31 #use C4::Circulation;
32
33 require Exporter;
34 use DBI;
35 use vars qw($VERSION @ISA @EXPORT);
36   
37 # set the version for version checking
38 $VERSION = 0.01;
39     
40 @ISA = qw(Exporter);
41 @EXPORT = qw(&dialog &startint &endint &output &clearscreen &pause &helptext
42 &textbox &menu &issuewindow &msg_yn &msg_ny &borrower_dialog &debug_msg &error_msg
43 &info_msg &selborrower &returnwindow &logondialog &borrowerwindow &titlepanel
44 &borrbind &borrfill &preeborr &borrowerbox &brmenu &prmenu);
45
46 sub suspend_cb {
47     # is this planned for something?
48 }
49       
50 sub startint {
51   my ($env,$msg)=@_;
52   Cdk::init();
53 }
54
55 sub menu {
56   my ($env,$type,$title,@items)=@_;
57   $env->{'sysarea'}="Menu";
58   my $titlebar=titlepanel($env,"Koha","Main Menu");
59   my $reason;
60   my $data;
61   my @mitems;
62   my $x = 0;
63   while ($items[$x] ne "") {
64     $mitems[$x]="<C>".$items[$x];
65     $x++;
66   }  
67   if ($type eq 'console'){
68     my $menucnt = @items;
69     my $menu = new Cdk::Scroll ('Title'=>"  ",
70       'List'=>\@mitems,
71       'Height'=> $menucnt+4,
72       'Width'=> 26);
73     # Activate the object.         
74     my ($menuItem) = $menu->activate();
75     # Check the results.
76     undef $menu;
77     if (!defined $menuItem) {      
78       $data = "Quit";
79     }
80     else { 
81       $data = $items[$menuItem];
82     }
83   }
84   return($reason,$data);
85   # end of menu
86 }
87
88 # FIXME - Is this a placeholder?
89 sub clearscreen { 
90 }
91
92 # FIXME - Is this a placeholder?
93 sub pause {
94  
95 }
96
97 sub output {
98   my($left,$top,$msg)=@_;
99   my @outm;
100   $outm[0]=$msg;
101   my $output = new Cdk::Label ('Message' =>\@outm,
102     'Ypos'=>$top, 'Xpos'=>$left, 'Box'=>0);
103   $output->draw();
104   return $output;
105 }
106
107 sub helptext {
108   my ($text)=@_;
109   my $helptext = output(1,24,$text);
110   return $helptext;
111 }
112
113
114 sub titlepanel{
115   my ($env,$title,$title2)=@_;
116   my @header;
117   @header[0] = fmtstr($env,$title,"L24");
118   @header[0] = @header[0].fmtstr($env,
119     $env->{'branchname'}."-".$env->{'queue'},"C28");
120   @header[0] = @header[0].fmtstr($env,$title2,"R24");
121   my $label = new Cdk::Label ('Message' =>\@header,'Ypos'=>0,'Xpos'=>0);
122   $label->draw();
123   return $label;
124   }
125
126 sub msg_yn {
127   my ($env,$text1,$text2)=@_;
128   # Create the dialog buttons.
129   my @buttons = ("Yes", "No");
130   my @mesg = ("<C>$text1", "<C>$text2");
131   # Create the dialog object.
132   my $dialog = new Cdk::Dialog ('Message' => \@mesg, 'Buttons' => \@buttons);
133   my $resp = $dialog->activate();
134   my $response = "Y";
135   if ($resp == 1) {
136     $response = "N";
137   }
138   undef $dialog;
139   return $response;
140 }
141 sub msg_ny {
142   my ($env,$text1,$text2)=@_;
143   # Cdk::init();
144   # Create the dialog buttons.
145   my @buttons = ("No", "Yes");
146   my @mesg = ("<C>$text1", "<C>$text2");
147   # Create the dialog object.
148   my $dialog = new Cdk::Dialog ('Message' => \@mesg, 'Buttons' => \@buttons);
149   my $resp = $dialog->activate();
150   my $response = "N";
151   if ($resp == 1) {
152     $response = "Y";
153   }
154   undef $dialog;
155   return $response;
156 }
157
158 sub debug_msg {
159   my ($env,$text)=@_;
160   if ($env->{'telnet'} eq "Y") {
161     popupLabel (["Debug </R>$text"]);
162 #  } else {
163 #    print "****DEBUG $text****";
164   }  
165   return();
166 }
167
168 sub error_msg {
169   my ($env,$text)=@_;
170   popupLabel (["<C>Error </R>$text"]);
171   return();
172 }
173
174 sub info_msg {
175   my ($env,$text)=@_;
176   popupLabel ([$text]);
177   return();
178 }
179
180 sub endint {
181   Cdk::end();
182 }
183
184
185 sub brmenu {
186   my ($env,$brrecs)=@_;
187   $env->{'sysarea'}="Menu";
188   my $titlebar=titlepanel($env,"Koha","Select branch");
189   my @mitems;
190   my $x = 0;
191   while (@$brrecs[$x] ne "") {
192     my $brrec =@$brrecs[$x]; 
193     $mitems[$x]=fmtstr($env,$brrec->{'branchcode'},"L6");
194     $mitems[$x]=$mitems[$x].fmtstr($env,$brrec->{'branchname'},"L20");
195     $x++;
196   }  
197   my $menu = new Cdk::Scroll ('Title'=>"  ",
198       'List'=>\@mitems,
199       'Height'=> 16,
200       'Width'=> 30);
201   # Activate the object.         
202   my ($menuItem) = $menu->activate();
203   # Check the results.
204   if (defined $menuItem) {      
205     my $brrec = @$brrecs[$menuItem];
206     $env->{'branchcode'} = $brrec->{'branchcode'};
207     $env->{'branchname'} = $brrec->{'branchname'};
208   }
209   undef $menu;
210   undef $titlebar;
211   return();
212   
213 }
214
215 sub prmenu {
216   my ($env,$prrecs)=@_;
217   $env->{'sysarea'}="Menu";
218   my $titlebar=titlepanel($env,"Koha","Select printer");
219   my @mitems;
220   my $x = 0;
221   while (@$prrecs[$x] ne "") {
222     my $prrec =@$prrecs[$x]; 
223     $mitems[$x]=fmtstr($env,$prrec->{'printername'},"L20");
224     $x++;
225   }  
226   my $menu = new Cdk::Scroll ('Title'=>"  ",
227       'List'=>\@mitems,
228       'Height'=> 16,
229       'Width'=> 30);
230   # Activate the object.         
231   my ($menuItem) = $menu->activate();
232   undef $menu;
233   undef $titlebar;
234   # Check the results.
235   if (defined $menuItem) {      
236     my $prrec = @$prrecs[$menuItem];
237     $env->{'queue'} = $prrec->{'printqueue'};
238     $env->{'printtype'} = $prrec->{'printtype'};
239   }
240   return();
241   
242 }
243
244
245 sub borrower_dialog {
246   my ($env)=@_;
247   my $result;
248   my $borrower;
249   my $book;
250   my @coltitles = ("Borrower","Item");
251   my @rowtitles = (" ");
252   my @coltypes  = ("UMIXED","UMIXED");
253   my @colwidths = (12,12);
254   my $matrix = new Cdk::Matrix (
255      'ColTitles'=> \@coltitles,
256      'RowTitles'=> \@rowtitles, 
257      'ColWidths'=> \@colwidths,
258      'ColTypes'=>  \@coltypes,
259      'Vrows'=>     1, 
260      'Vcols'=>     2);
261   borrbind($env,$matrix);
262   #$matrix->draw();
263   my ($rows,$cols,$info) = $matrix->activate(); 
264   if ((!defined $rows) && ($info->[0][0] eq "")) { 
265     $result = "Circ";
266   } else {
267     $borrower = $info->[0][0];
268     $book     = $info->[0][1];
269   }
270   $matrix->erase();
271   $matrix->unregister();
272   undef $matrix;
273   Cdk::refreshCdkScreen();
274   return ($borrower,$result,$book);
275 }
276
277 sub selborrower {
278   my ($env,$dbh,$borrows,$bornums)=@_;
279   my $result;
280   my $label = "Select a borrower";
281   my $scroll = new Cdk::Scroll ('Title'=>$label,
282     'List'=>\@$borrows,'Height'=>15,'Width'=>60);
283   my $returnValue = $scroll->activate ();
284   if (!defined $returnValue) {
285     #$result = "Circ";
286   } else {  
287     $result = substr(@$borrows[$returnValue],0,9);
288   }
289   $scroll->erase();
290   #$scroll->unregister();
291   undef $scroll;
292   Cdk::refreshCdkScreen();
293   return $result;
294 }
295
296 sub issuewindow {
297   my ($env,$title,$dbh,$items1,$items2,$borrower,$amountowing,$odues)=@_;
298   my @functs=("Due Date","Renewals","Payments","Print","Current","Previous");
299   my $titlepanel = titlepanel($env,"Issues","Issue an Item");
300   my $scroll2 = new Cdk::Scroll ('Title'=>"Previous Issues",
301     'List'=>\@$items1,'Height'=> 8,'Width'=>78,'Ypos'=>18);
302   my $scroll1 = new Cdk::Scroll ('Title'=>"Current Issues",
303     'List'=>\@$items2,'Height'=> 8,'Width'=>78,'Ypos'=>9);
304   my $funcmenu = new Cdk::Scroll ('Title'=>"",
305     'List'=>\@functs,'Height'=>5,'Width'=>12,'Ypos'=>3,'Xpos'=>28);
306   my $loanlength = new Cdk::Entry('Label'=>"Due Date:      ",
307     'Max'=>"30",'Width'=>"11",
308     'Xpos'=>0,'Ypos'=>5,'Type'=>"UMIXED");
309   my $x = 0;
310   while ($x < length($env->{'loanlength'})) {
311      $loanlength->inject('Input'=>substr($env->{'loanlength'},$x,1));
312      $x++;
313   }
314   my $borrbox = borrowerbox($env,$borrower,$amountowing);
315   my $entryBox = new Cdk::Entry('Label'=>"Item Barcode:  ",
316      'Max'=>"11",'Width'=>"11",
317      'Xpos'=>"0",'Ypos'=>3,'Type'=>"UMIXED");
318   $scroll2->draw();
319   $scroll1->draw();
320   $funcmenu->draw();
321   $loanlength->draw(); 
322   $borrbox->draw();   
323   #$env->{'loanlength'} = "";
324   #debug_msg($env,"clear len");
325   my $x;
326   my $barcode;
327   $entryBox->preProcess ('Function' => 
328     sub{prebook(@_,$env,$dbh,$funcmenu,$entryBox,$loanlength,
329     $scroll1,$scroll2,$borrower,$amountowing,$odues);});
330   $barcode = $entryBox->activate();
331   my $reason;
332   if (!defined $barcode) {
333     $reason="Finished user"
334   }
335   $borrbox->erase();
336   $entryBox->erase();
337   $scroll2->erase();
338   $scroll1->erase();
339   $funcmenu->erase();
340   $loanlength->erase();
341   undef $titlepanel;
342   undef $borrbox;
343   undef $entryBox;
344   undef $scroll2;
345   undef $scroll1;
346   undef $funcmenu;
347   undef $loanlength;
348   Cdk::refreshCdkScreen();
349   #debug_msg($env,"exiting");    
350   return $barcode,$reason;
351 }  
352 sub actfmenu {
353   my ($env,$dbh,$funcmenu,$entryBox,$loanlength,$scroll1,
354     $scroll2,$borrower,$amountowing,$odues) = @_;
355   my $funct =  $funcmenu->activate();
356   if (!defined $funct) {
357   } elsif ($funct == 0 ) {
358     actloanlength ($env,$entryBox,$loanlength,$scroll1,$scroll2);
359   } elsif ($funct == 1 ) { 
360     $entryBox->erase();
361     $scroll1->erase();
362     $scroll2->erase();
363     $loanlength->erase();
364     $funcmenu->erase();
365     #debug_msg($env,"");
366     C4::Circulation::Renewals::bulkrenew($env,$dbh,
367       $borrower->{'borrowernumber'},$amountowing,$borrower,$odues);
368   } elsif ($funct == 2 ) {
369     $entryBox->erase();
370     $scroll1->erase();
371     $scroll2->erase();
372     $loanlength->erase();
373     $funcmenu->erase();
374     C4::Accounts::reconcileaccount($env,$dbh,$borrower->{'borrowernumber'},
375     $amountowing,$borrower,$odues);
376   } elsif ($funct == 3 ) {
377     C4::Circulation::Borrissues::printallissues ($env,$borrower);
378   } elsif ($funct == 4 ) {
379     actscroll1 ($env,$entryBox,$loanlength,$scroll1,$scroll2);
380   } elsif ($funct == 5 ) {
381     actscroll2 ($env,$entryBox,$loanlength,$scroll1,$scroll2);
382   }
383   Cdk::refreshCdkScreen();
384   $entryBox->unregister();
385   $entryBox->register();
386   return
387 }  
388 sub actscroll1 {
389   my ($env,$entryBox,$loanlength,$scroll1,$scroll2) = @_;
390   $scroll1->activate();
391   return 1;
392 }
393 sub actscroll2 {
394   my ($env,$entryBox,$loanlength,$scroll1,$scroll2) = @_;
395   $scroll2->activate();
396   return 1;
397 }
398 sub actloanlength {
399   my ($env,$entryBox,$loanlength,$scroll1,$scroll2) = @_;
400   my $validdate = "N";
401   while ($validdate eq "N") {
402     my $loanlength = $loanlength->activate();
403     if (!defined $loanlength) {
404       $env->{'loanlength'} = "";
405       $validdate = "Y";
406     } elsif ($loanlength eq "") {
407       $env->{'loanlength'} = "";
408       $validdate = "Y";
409     } else {    
410       my $date = ParseDate($loanlength);
411       if ( $date > ParseDate('today')){
412         $validdate="Y";
413         my $fdate = substr($date,0,4).'-'.substr($date,4,2).'-'.substr($date,6,2);
414         #debug_msg($env,"$date $fdate");
415         $env->{'loanlength'} = $fdate;
416       } else { 
417         error_msg($env,"Invalid date"); 
418       }
419     }
420   }  
421   return;
422 }
423
424 sub prebook {
425   my ($input,$env,$dbh,$funcmenu,$entryBox,$loanlength,
426     $scroll1,$scroll2,$borrower,$amountowing,$odues)= @_;
427   # FIXME - $key_tab is undefined
428   if ($input eq $key_tab) {    
429     actfmenu ($env,$dbh,$funcmenu,$entryBox,$loanlength,$scroll1,
430        $scroll2,$borrower,$amountowing,$odues);
431     return 0;
432   }
433   return 1;
434 }
435                                                           
436 sub borrowerbox {
437   my ($env,$borrower,$amountowing,$odues) = @_;
438   my @borrinfo;
439   my $amountowing = fmtdec($env,$amountowing,"42");
440   my $line = "$borrower->{'cardnumber'} ";
441   $line = $line."$borrower->{'surname'}, ";
442   $line = $line."$borrower->{'title'} $borrower->{'firstname'}";
443   $borrinfo[0]=$line;
444   $line = "$borrower->{'streetaddress'}, $borrower->{'city'}";
445   $borrinfo[1]=$line;
446   $line = "$borrower->{'categorycode'}";
447   if ($borrower->{'gonenoaddress'} == 1) {
448     $line = $line." </R>GNA<!R>";
449   }
450   if ($borrower->{'lost'} == 1) {
451     $line = $line." </R>LOST<!R>";
452   }
453   if ($odues > 0) {
454     $line = $line." </R>ODUE<!R>";
455   }     
456   if ($borrower->{'borrowernotes'} ne "" ) {
457     $line = $line." </R>NOTES<!R>";
458   }
459   if ($amountowing > 0) {
460     $line = $line." </B>\$$amountowing";
461   }
462   $borrinfo[2]=$line;
463   if ($borrower->{'borrowernotes'} ne "" ) {
464     $borrinfo[3]=substr($borrower->{'borrowernotes'},0,40);     
465   }
466   my $borrbox = new Cdk::Label ('Message' =>\@borrinfo,
467     'Ypos'=>3, 'Xpos'=>"RIGHT");
468   return $borrbox;
469 }
470
471 sub returnwindow {
472   my ($env,$title,$item,$items,$borrower,$amountowing,$odues,$dbh,$resp)=@_;
473   #debug_msg($env,$borrower);
474   my $titlepanel = titlepanel($env,"Returns","Scan Item");
475   my @functs=("Payments","Renewal");
476   my $funcmenu = new Cdk::Scroll ('Title'=>"",
477      'List'=>\@functs,'Height'=>5,'Width'=>12,'Ypos'=>3,'Xpos'=>16);
478   my $returnlist = new Cdk::Scroll ('Title'=>"Items Returned",
479      'List'=>\@$items,'Height'=> 12,'Width'=>74,'Ypos'=>10,'Xpos'=>1);
480   $returnlist->draw();
481   $funcmenu->draw();
482   my $borrbox;
483   if ($borrower->{'cardnumber'} ne "") {    
484     $borrbox = borrowerbox($env,$borrower,$amountowing);  
485     $borrbox->draw();
486   } else {
487     if ($resp ne "") {
488       my @text;
489       @text[0] = $resp;
490       $borrbox = new Cdk::Label ('Message' =>\@text, 'Ypos'=>3, 'Xpos'=>"RIGHT");
491       $borrbox->draw();
492     }
493   }  
494   my $bookentry  =  new Cdk::Entry('Label'=>" ",
495      'Max'=>"11",'Width'=>"11",
496      'Xpos'=>"2",'Ypos'=>"3",'Title'=>"Item Barcode",
497      'Type'=>"UMIXED");
498   $bookentry->preProcess ('Function' =>sub{preretbook(@_,$env,$dbh,
499      $funcmenu,$bookentry,$borrower,$amountowing,
500      $odues,$titlepanel,$borrbox,$returnlist);});
501   my $barcode = $bookentry->activate();
502   my $reason;
503   if (!defined $barcode) {
504     $barcode="";
505     $reason="Circ";
506     $bookentry->erase();
507     $funcmenu->erase();
508     if ($borrbox ne "") {$borrbox->erase();}
509     $returnlist->erase();
510   } else {
511     $reason="";
512   }
513   undef $bookentry;
514   undef $funcmenu;
515   undef $borrbox;
516   undef $returnlist;
517   undef $titlepanel;
518   return($reason,$barcode);
519   }
520
521 sub preretbook {
522   my ($input,$env,$dbh,$funcmenu,$bookentry,$borrower,
523   $amountowing,$odues,$titlepanel,$borrbox,$returnlist)=@_;
524   if ($input eq $key_tab) {
525     actrfmenu($env,$dbh,$funcmenu,$bookentry,$borrower,
526     $amountowing,$odues,$titlepanel,$borrbox,$returnlist);
527     return 0;
528   }
529   return 1;
530   }
531
532 sub actrfmenu {
533   my ($env,$dbh,$funcmenu,$bookentry,$borrower,
534     $amountowing,$odues,$titlepanel,$borrbox,$returnlist)= @_;
535   my $funct =  $funcmenu->activate();
536   #debug_msg($env,"funtion $funct");
537   if (!defined $funct) {
538   } elsif ($funct == 1 ) {
539     if ($borrower->{'borrowernumber'} ne "") {
540        $funcmenu->erase();
541        $bookentry->erase();
542        $titlepanel->erase();
543        $borrbox->erase();
544        $returnlist->erase();
545        C4::Circulation::Renewals::bulkrenew($env,$dbh,
546        $borrower->{'borrowernumber'},$amountowing,$borrower,$odues);
547        Cdk::refreshCdkScreen();
548        $funcmenu->draw();
549        $bookentry->draw();
550        $titlepanel->draw();
551        $borrbox->draw();
552        $returnlist->draw();
553     }
554   } elsif ($funct == 0 ) {
555     if ($borrower->{'borrowernumber'} ne "") {
556        $funcmenu->erase();
557        $bookentry->erase();
558        $titlepanel->erase();
559        $borrbox->erase();
560        $returnlist->erase();
561        C4::Accounts::reconcileaccount($env,$dbh,$borrower->{'borrowernumber'},
562        $amountowing,$borrower,$odues);
563        $funcmenu->draw();
564        $bookentry->draw();
565        $titlepanel->draw();
566        $borrbox->draw();
567        $returnlist->draw();
568        #Cdk::refreshCdkScreen();
569     }
570   } 
571 }
572   
573 sub act {
574   my ($obj) = @_;
575   my $ans = $obj->activate();
576   return $ans;
577   }
578
579 sub borrbind {
580   my ($env,$entry) = @_; 
581   my $lastborr = $env->{"bcard"};
582   $entry->preProcess ('Function' => sub {preborr (@_, $env,$entry);});
583 }
584
585 sub preborr {
586   my ($input,$env, $entry) = @_;
587   if ($env->{"bcard"} ne "") {
588 #     error_msg($env,"hi there");  
589     # FIXME - $lastval is undefined
590     if ($input eq $lastval) {
591 #        error_msg($env,"its a ctrl-r");  
592       borfill($env,$entry);
593       return 0;
594     }
595   } 
596   return 1;
597 }  
598   
599   
600 sub borfill {
601   my ($env,$entry) = @_;
602   error_msg($env,"in borfill: $env->{'bcard'}");
603   my $lastborr = $env->{"bcard"};
604   my $i = 1;
605   $entry->inject('Input'=>$lastborr);
606   while ($i < 9) {
607 #    my $temp=substr($lastborr,$i,1);
608 #    $entry->inject('Input'=>$temp);
609     $i++;
610   }
611    
612 }
613                                
614 END { }       # module clean-up code here (global destructor)
615
616