Kumara - predecessor to Koha
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
10 KiB

  1. package C4::Reserves2; #asummes C4/Reserves2
  2. #requires DBI.pm to be installed
  3. #uses DBD:Pg
  4. use strict;
  5. require Exporter;
  6. use DBI;
  7. use C4::Database;
  8. #use C4::Accounts;
  9. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  10. # set the version for version checking
  11. $VERSION = 0.01;
  12. @ISA = qw(Exporter);
  13. @EXPORT = qw(&FindReserves &CreateReserve &updatereserves &getreservetitle);
  14. %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
  15. # your exported package globals go here,
  16. # as well as any optionally exported functions
  17. @EXPORT_OK = qw($Var1 %Hashit);
  18. # non-exported package globals go here
  19. use vars qw(@more $stuff);
  20. # initalize package globals, first exported ones
  21. my $Var1 = '';
  22. my %Hashit = ();
  23. # then the others (which are still accessible as $Some::Module::stuff)
  24. my $stuff = '';
  25. my @more = ();
  26. # all file-scoped lexicals must be created before
  27. # the functions below that use them.
  28. # file-private lexicals go here
  29. my $priv_var = '';
  30. my %secret_hash = ();
  31. # here's a file-private function as a closure,
  32. # callable as &$priv_func; it cannot be prototyped.
  33. my $priv_func = sub {
  34. # stuff goes here.
  35. };
  36. # make all your functions, whether exported or not;
  37. sub FindReserves {
  38. my ($bib,$bor)=@_;
  39. my $dbh=C4Connect;
  40. my $query="Select *,reserves.branchcode
  41. from reserves,borrowers,biblio ";
  42. if ($bib ne ''){
  43. if ($bor ne ''){
  44. $query=$query." where reserves.biblionumber=$bib and
  45. reserves.borrowernumber=borrowers.borrowernumber and
  46. biblio.biblionumber=$bib and cancellationdate is NULL and found
  47. <> 'F'";
  48. } else {
  49. $query=$query." where reserves.borrowernumber=borrowers.borrowernumber
  50. and biblio.biblionumber=$bib and reserves.biblionumber=$bib
  51. and cancellationdate is NULL and found <> 'F'";
  52. }
  53. } else {
  54. $query=$query." where borrowers.borrowernumber=$bor and
  55. reserves.borrowernumber=borrowers.borrowernumber and reserves.biblionumber
  56. =biblio.biblionumber and cancellationdate is NULL and found <> 'F'";
  57. }
  58. $query.=" order by priority";
  59. my $sth=$dbh->prepare($query);
  60. $sth->execute;
  61. my $i=0;
  62. my @results;
  63. while (my $data=$sth->fetchrow_hashref){
  64. $results[$i]=$data;
  65. $i++;
  66. }
  67. # print $query;
  68. $sth->finish;
  69. $dbh->disconnect;
  70. return($i,\@results);
  71. }
  72. sub CreateReserve {
  73. my ($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$priority,$notes) = @_;
  74. my $fee=CalcReserveFee($env,$borrnum,$biblionumber,$constraint,$bibitems);
  75. my $dbh = &C4Connect;
  76. my $const = lc substr($constraint,0,1);
  77. my @datearr = localtime(time);
  78. my $resdate =(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
  79. #eval {
  80. # updates take place here
  81. if ($fee > 0) {
  82. # print $fee;
  83. my $nextacctno = &getnextacctno($env,$borrnum,$dbh);
  84. my $updquery = "insert into accountlines
  85. (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
  86. values
  87. ($borrnum,$nextacctno,now(),$fee,'Reserve Charge','Res',$fee)";
  88. my $usth = $dbh->prepare($updquery);
  89. $usth->execute;
  90. $usth->finish;
  91. }
  92. #if ($const eq 'a'){
  93. my $query="insert into reserves
  94. (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,priority,reservenotes)
  95. values
  96. ('$borrnum','$biblionumber','$resdate','$branch','$const','$priority','$notes')";
  97. my $sth = $dbh->prepare($query);
  98. $sth->execute();
  99. $sth->finish;
  100. #}
  101. if (($const eq "o") || ($const eq "e")) {
  102. my $numitems = @$bibitems;
  103. my $i = 0;
  104. while ($i < $numitems) {
  105. my $biblioitem = @$bibitems[$i];
  106. my $query = "insert into
  107. reserveconstraints
  108. (borrowernumber,biblionumber,reservedate,biblioitemnumber)
  109. values
  110. ('$borrnum','$biblionumber','$resdate','$biblioitem')";
  111. my $sth = $dbh->prepare($query);
  112. $sth->execute();
  113. $sth->finish;
  114. $i++;
  115. }
  116. }
  117. # print $query;
  118. $dbh->disconnect();
  119. return();
  120. }
  121. sub CalcReserveFee {
  122. my ($env,$borrnum,$biblionumber,$constraint,$bibitems) = @_;
  123. #check for issues;
  124. my $dbh = &C4Connect;
  125. my $const = lc substr($constraint,0,1);
  126. my $query = "select * from borrowers,categories
  127. where (borrowernumber = '$borrnum')
  128. and (borrowers.categorycode = categories.categorycode)";
  129. my $sth = $dbh->prepare($query);
  130. $sth->execute;
  131. my $data = $sth->fetchrow_hashref;
  132. $sth->finish();
  133. my $fee = $data->{'reservefee'};
  134. my $cntitems = @->$bibitems;
  135. if ($fee > 0) {
  136. # check for items on issue
  137. # first find biblioitem records
  138. my @biblioitems;
  139. my $query1 = "select * from biblio,biblioitems
  140. where (biblio.biblionumber = '$biblionumber')
  141. and (biblio.biblionumber = biblioitems.biblionumber)";
  142. my $sth1 = $dbh->prepare($query1);
  143. $sth1->execute();
  144. while (my $data1=$sth1->fetchrow_hashref) {
  145. if ($const eq "a") {
  146. push @biblioitems,$data1;
  147. } else {
  148. my $found = 0;
  149. my $x = 0;
  150. while ($x < $cntitems) {
  151. if (@$bibitems->{'biblioitemnumber'} == $data->{'biblioitemnumber'}) {
  152. $found = 1;
  153. }
  154. $x++;
  155. }
  156. if ($const eq 'o') {if ($found == 1) {push @biblioitems,$data;}
  157. } else {if ($found == 0) {push @biblioitems,$data;} }
  158. }
  159. }
  160. $sth1->finish;
  161. my $cntitemsfound = @biblioitems;
  162. my $issues = 0;
  163. my $x = 0;
  164. my $allissued = 1;
  165. while ($x < $cntitemsfound) {
  166. my $bitdata = @biblioitems[$x];
  167. my $query2 = "select * from items
  168. where biblioitemnumber = '$bitdata->{'biblioitemnumber'}'";
  169. my $sth2 = $dbh->prepare($query2);
  170. $sth2->execute;
  171. while (my $itdata=$sth2->fetchrow_hashref) {
  172. my $query3 = "select * from issues
  173. where itemnumber = '$itdata->{'itemnumber'}' and
  174. returndate is null";
  175. my $sth3 = $dbh->prepare($query3);
  176. $sth3->execute();
  177. if (my $isdata=$sth3->fetchrow_hashref) { } else
  178. {$allissued = 0; }
  179. }
  180. $x++;
  181. }
  182. if ($allissued == 0) {
  183. my $rquery = "select * from reserves
  184. where biblionumber = '$biblionumber'";
  185. my $rsth = $dbh->prepare($rquery);
  186. $rsth->execute();
  187. if (my $rdata = $rsth->fetchrow_hashref) { } else {
  188. $fee = 0;
  189. }
  190. }
  191. }
  192. # print "fee $fee";
  193. $dbh->disconnect();
  194. return $fee;
  195. }
  196. sub getnextacctno {
  197. my ($env,$bornumber,$dbh)=@_;
  198. my $nextaccntno = 1;
  199. my $query = "select * from accountlines
  200. where (borrowernumber = '$bornumber')
  201. order by accountno desc";
  202. my $sth = $dbh->prepare($query);
  203. $sth->execute;
  204. if (my $accdata=$sth->fetchrow_hashref){
  205. $nextaccntno = $accdata->{'accountno'} + 1;
  206. }
  207. $sth->finish;
  208. return($nextaccntno);
  209. }
  210. sub updatereserves{
  211. #subroutine to update a reserve
  212. my ($rank,$biblio,$borrower,$del,$branch)=@_;
  213. my $dbh=C4Connect;
  214. my $query="Update reserves ";
  215. if ($del ==0){
  216. $query.="set priority='$rank',branchcode='$branch' where
  217. biblionumber=$biblio and borrowernumber=$borrower";
  218. } else {
  219. $query="Select * from reserves where biblionumber=$biblio and
  220. borrowernumber=$borrower";
  221. my $sth=$dbh->prepare($query);
  222. $sth->execute;
  223. my $data=$sth->fetchrow_hashref;
  224. $sth->finish;
  225. $query="Select * from reserves where biblionumber=$biblio and
  226. priority > '$data->{'priority'}' and cancellationdate is NULL
  227. order by priority";
  228. my $sth2=$dbh->prepare($query) || die $dbh->errstr;
  229. $sth2->execute || die $sth2->errstr;
  230. while (my $data=$sth2->fetchrow_hashref){
  231. $data->{'priority'}--;
  232. $query="Update reserves set priority=$data->{'priority'} where
  233. biblionumber=$data->{'biblionumber'} and
  234. borrowernumber=$data->{'borrowernumber'}";
  235. my $sth3=$dbh->prepare($query);
  236. $sth3->execute || die $sth3->errstr;
  237. $sth3->finish;
  238. }
  239. $sth2->finish;
  240. $query="update reserves set cancellationdate=now() where biblionumber=$biblio
  241. and borrowernumber=$borrower";
  242. }
  243. my $sth=$dbh->prepare($query);
  244. $sth->execute;
  245. $sth->finish;
  246. $dbh->disconnect;
  247. }
  248. sub getreservetitle {
  249. my ($biblio,$bor,$date,$timestamp)=@_;
  250. my $dbh=C4Connect;
  251. my $query="Select * from reserveconstraints,biblioitems where
  252. reserveconstraints.biblioitemnumber=biblioitems.biblioitemnumber
  253. and reserveconstraints.biblionumber=$biblio and reserveconstraints.borrowernumber
  254. = $bor and reserveconstraints.reservedate='$date' and
  255. reserveconstraints.timestamp=$timestamp";
  256. my $sth=$dbh->prepare($query);
  257. $sth->execute;
  258. my $data=$sth->fetchrow_hashref;
  259. $sth->finish;
  260. $dbh->disconnect;
  261. # print $query;
  262. return($data);
  263. }
  264. END { } # module clean-up code here (global destructor)