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