Continuing work on Z39.50 search tool. Daemon now forks up to 12 processes
[koha.git] / C4 / Circulation / Main.pm
1 package C4::Circulation::Main; #asummes C4/Circulation/Main
2
3 #package to deal with circulation 
4
5 use strict;
6 require Exporter;
7 use DBI;
8 use C4::Database;
9 use C4::Circulation::Issues;
10 use C4::Circulation::Returns;
11 use C4::Circulation::Renewals;
12 use C4::Circulation::Borrower;
13 use C4::Reserves;
14 use C4::Search;
15 use C4::InterfaceCDK;
16 use C4::Security;
17 use C4::Format;
18
19 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
20   
21 # set the version for version checking
22 $VERSION = 0.01;
23     
24 @ISA = qw(Exporter);
25 @EXPORT = qw(&pastitems &checkoverdues &previousissue 
26 &checkreserve &checkwaiting &scanbook &scanborrower &getbranch &getprinter);
27 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
28                   
29 # your exported package globals go here,
30 # as well as any optionally exported functions
31
32 @EXPORT_OK   = qw($Var1 %Hashit);
33
34
35 # non-exported package globals go here
36 use vars qw(@more $stuff);
37         
38 # initalize package globals, first exported ones
39
40 my $Var1   = '';
41 my %Hashit = ();
42                     
43 # then the others (which are still accessible as $Some::Module::stuff)
44 my $stuff  = '';
45 my @more   = ();
46         
47 # all file-scoped lexicals must be created before
48 # the functions below that use them.
49                 
50 # file-private lexicals go here
51 my $priv_var    = '';
52 my %secret_hash = ();
53                             
54 # here's a file-private function as a closure,
55 # callable as &$priv_func;  it cannot be prototyped.
56 my $priv_func = sub {
57   # stuff goes here.
58 };
59                                                     
60 # make all your functions, whether exported or not;
61
62 sub getbranch {
63   my ($env) = @_;
64   my $dbh = C4Connect;
65   my $query = "select * from branches order by branchcode";
66   my $sth = $dbh->prepare($query);
67   $sth->execute;
68   if ($sth->rows>1) {
69       my @branches;
70       while (my $data = $sth->fetchrow_hashref) {
71         push @branches,$data;
72       }
73       brmenu ($env,\@branches);
74   } else {
75       my $data = $sth->fetchrow_hashref;
76       $env->{'branchcode'}=$data->{'branchcode'};
77   }
78   my $query = "select * from branches  
79     where branchcode = '$env->{'branchcode'}'";
80   $sth = $dbh->prepare($query);
81   $sth->execute;
82   my $data = $sth->fetchrow_hashref;
83   $env->{'brdata'} = $data;
84   $env->{'branchname'} = $data->{'branchname'};
85   $sth->finish;
86   $dbh->disconnect;
87 }
88
89 sub getprinter {
90   my ($env) = @_;
91   my $dbh = C4Connect;
92   my $query = "select * from printers order by printername";
93   my $sth = $dbh->prepare($query);
94   $sth->execute;
95   if ($sth->rows>1) {
96       my @printers;
97       while (my $data = $sth->fetchrow_hashref) {
98         push @printers,$data;
99       }
100       prmenu ($env,\@printers);
101   } else {
102       my $data=$sth->fetchrow_hashref;
103       $env->{'queue'}=$data->{'printqueue'};
104       $env->{'printtype'}=$data->{'printtype'};
105   }
106   $sth->finish;
107   $dbh->disconnect;
108   }
109                       
110 sub pastitems{
111   #Get list of all items borrower has currently on issue
112   my ($env,$bornum,$dbh)=@_;
113   my $query1 = "select * from issues  where (borrowernumber=$bornum)
114     and (returndate is null) order by date_due";
115   my $sth=$dbh->prepare($query1);
116   $sth->execute;
117   my $i=0;
118   my @items;
119   my @items2;
120   while (my $data1=$sth->fetchrow_hashref) {
121     my $data = itemnodata($env,$dbh,$data1->{'itemnumber'}); #C4::Search
122     my @date = split("-",$data1->{'date_due'});
123     my $odate = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
124     my $line = C4::Circulation::Issues::formatitem($env,$data,$odate,"");
125     $items[$i]=$line;
126     $i++;
127   }
128   $sth->finish();
129   return(\@items,\@items2);
130 }
131
132 sub checkoverdues{
133   #checks whether a borrower has overdue items
134   my ($env,$bornum,$dbh)=@_;
135   my @datearr = localtime;
136   my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
137   my $query = "Select count(*) from issues where borrowernumber=$bornum and
138         returndate is NULL and date_due < '$today'";
139   my $sth=$dbh->prepare($query);
140   $sth->execute;
141   my $data = $sth->fetchrow_hashref;
142   $sth->finish;
143   return $data->{'count(*)'};
144 }
145
146 sub previousissue {
147   my ($env,$itemnum,$dbh,$bornum)=@_;
148   my $sth=$dbh->prepare("Select 
149      firstname,surname,issues.borrowernumber,cardnumber,returndate
150      from issues,borrowers where 
151      issues.itemnumber='$itemnum' and
152      issues.borrowernumber=borrowers.borrowernumber 
153      and issues.returndate is NULL");
154   $sth->execute;
155   my $borrower=$sth->fetchrow_hashref;
156   my $canissue = "Y";
157   $sth->finish;
158   my $newdate;
159   if ($borrower->{'borrowernumber'} ne ''){
160     if ($bornum eq $borrower->{'borrowernumber'}){
161       # no need to issue
162       my ($renewstatus) = C4::Circulation::Renewals::renewstatus($env,$dbh,$bornum,$itemnum);
163       my ($resbor,$resrec) = checkreserve($env,$dbh,$itemnum);
164       if ($renewstatus == "0") {
165         info_msg($env,"</S>Issued to this borrower - No renewals<!S>");
166         $canissue = "N";
167       } elsif ($resbor ne "") {
168         my $resp = C4::InterfaceCDK::msg_ny($env,"Book is issued to this borrower",
169           "and is reserved - Renew?");
170         if ($resp eq "Y") {
171           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
172           $canissue = "R";
173         } else {
174           $canissue = "N";
175         }
176       } else {
177         my $resp = C4::InterfaceCDK::msg_yn($env,"Book is issued to this borrower", "Renew?");
178         if ($resp eq "Y") {
179           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
180           $canissue = "R";
181         } else {
182           $canissue = "N";
183         }
184       }    
185     } else {
186       my $text="Issued to $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'})";    
187       my $resp = C4::InterfaceCDK::msg_yn($env,$text,"Mark as returned?");
188       if ( $resp eq "Y") {
189         &returnrecord($env,$dbh,$borrower->{'borrowernumber'},$itemnum);
190       } else {
191         $canissue = "N";
192       }
193     }
194   } 
195   return($borrower->{'borrowernumber'},$canissue,$newdate);
196 }
197
198
199 sub checkreserve{
200   # Check for reserves for biblio 
201   my ($env,$dbh,$itemnum)=@_;
202   my $resbor = "";
203   my $query = "select * from reserves,items 
204     where (items.itemnumber = '$itemnum')
205     and (reserves.cancellationdate is NULL)
206     and (items.biblionumber = reserves.biblionumber)
207     and ((reserves.found = 'W')
208     or (reserves.found is null)) 
209     order by priority";
210   my $sth = $dbh->prepare($query);
211   $sth->execute();
212   my $resrec;
213   if (my $data=$sth->fetchrow_hashref) {
214     $resrec=$data;
215     my $const = $data->{'constrainttype'};
216     if ($const eq "a") {
217       $resbor = $data->{'borrowernumber'}; 
218     } else {
219       my $found = 0;
220       my $cquery = "select * from reserveconstraints,items 
221          where (borrowernumber='$data->{'borrowernumber'}') 
222          and reservedate='$data->{'reservedate'}'
223          and reserveconstraints.biblionumber='$data->{'biblionumber'}'
224          and (items.itemnumber=$itemnum and 
225          items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
226       my $csth = $dbh->prepare($cquery);
227       $csth->execute;
228       if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
229       if ($const eq 'o') {
230         if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
231       } else {
232         if ($found eq 0) {$resbor = $data->{'borrowernumber'};} 
233       }
234       $csth->finish();
235     }     
236   }
237   $sth->finish;
238   return ($resbor,$resrec);
239 }
240
241 sub checkwaiting{
242   # check for reserves waiting
243   my ($env,$dbh,$bornum)=@_;
244   my @itemswaiting;
245   my $query = "select * from reserves
246     where (borrowernumber = '$bornum')
247     and (reserves.found='W') and cancellationdate is NULL";
248   my $sth = $dbh->prepare($query);
249   $sth->execute();
250   my $cnt=0;
251   if (my $data=$sth->fetchrow_hashref) {
252     @itemswaiting[$cnt] =$data;
253     $cnt ++
254   }
255   $sth->finish;
256   return ($cnt,\@itemswaiting);
257 }
258
259 sub scanbook {
260   my ($env,$interface)=@_;
261   #scan barcode
262   my ($number,$reason)=dialog("Book Barcode:");
263   $number=uc $number;
264   return ($number,$reason);
265 }
266
267 sub scanborrower {
268   my ($env,$interface)=@_;
269   #scan barcode
270   my ($number,$reason,$book)=C4::InterfaceCDK::borrower_dialog($env); #C4::InterfaceCDK
271   $number= $number;
272   $book=uc $book;
273   return ($number,$reason,$book);
274 }
275
276
277 END { }       # module clean-up code here (global destructor)