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