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.

830 lines
23 KiB

  1. package C4::Search; #asummes C4/Search
  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::InterfaceCDK;
  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(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
  14. &itemdata &bibdata &GetItems &borrdata &getacctlist &itemnodata &itemcount
  15. &OpacSearch &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
  16. &getboracctrecord &ItemType &itemissues &FrontSearch);
  17. %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
  18. # your exported package globals go here,
  19. # as well as any optionally exported functions
  20. @EXPORT_OK = qw($Var1 %Hashit);
  21. # non-exported package globals go here
  22. use vars qw(@more $stuff);
  23. # initalize package globals, first exported ones
  24. my $Var1 = '';
  25. my %Hashit = ();
  26. # then the others (which are still accessible as $Some::Module::stuff)
  27. my $stuff = '';
  28. my @more = ();
  29. # all file-scoped lexicals must be created before
  30. # the functions below that use them.
  31. # file-private lexicals go here
  32. my $priv_var = '';
  33. my %secret_hash = ();
  34. # here's a file-private function as a closure,
  35. # callable as &$priv_func; it cannot be prototyped.
  36. my $priv_func = sub {
  37. # stuff goes here.
  38. };
  39. # make all your functions, whether exported or not;
  40. sub NewBorrowerNumber {
  41. my $dbh=C4Connect;
  42. my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
  43. $sth->execute;
  44. my $data=$sth->fetchrow_hashref;
  45. $sth->finish;
  46. $data->{'max(borrowernumber)'}++;
  47. return($data->{'max(borrowernumber)'});
  48. }
  49. sub OpacSearch {
  50. my ($env,$type,$search,$num,$offset)=@_;
  51. my $dbh = &C4Connect;
  52. $search->{'keyword'}=~ s/'/\\'/g;
  53. my @key=split(' ',$search->{'keyword'});
  54. my $count=@key;
  55. my $i=1;
  56. my @results;
  57. my $query ="Select count(*) from biblio where
  58. ((title like '$key[0]%' or title like '% $key[0]%')";
  59. while ($i < $count){
  60. $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
  61. $i++;
  62. }
  63. $query=$query.") or ((author like '$key[0]%' or author like '% $key[0]%')";
  64. $i=1;
  65. while ($i < $count){
  66. $query=$query." and (author like '$key[$i]%' or author like '% $key[$i]%')";
  67. $i++;
  68. }
  69. $query=$query.") order by title";
  70. my $sth=$dbh->prepare($query);
  71. $sth->execute;
  72. my $data=$sth->fetchrow_hashref;
  73. my $count=$data->{'count(*)'};
  74. $sth->finish;
  75. $query=~ s/count\(\*\)/\*/;
  76. $query= $query." limit $offset,$num";
  77. $sth=$dbh->prepare($query);
  78. # print $query;
  79. $sth->execute;
  80. $i=0;
  81. while (my $data=$sth->fetchrow_hashref){
  82. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}";
  83. $i++;
  84. }
  85. $sth->finish;
  86. $dbh->disconnect;
  87. return($count,@results);
  88. }
  89. sub FrontSearch {
  90. my ($env,$type,$search,$num,$offset)=@_;
  91. my $dbh = &C4Connect;
  92. $search->{'front'}=~ s/ +$//;
  93. $search->{'front'}=~ s/'/\\'/;
  94. my @key=split(' ',$search->{'front'});
  95. my $count=@key;
  96. my $i=1;
  97. my @results;
  98. my $query ="Select * from biblio,bibliosubtitle where
  99. biblio.biblionumber=bibliosubtitle.biblionumber and
  100. ((title like '$key[0]%' or title like '% $key[0]%'
  101. or subtitle like '$key[0]%' or subtitle like '% $key[0]%'
  102. or author like '$key[0]%' or author like '% $key[0]%')";
  103. while ($i < $count){
  104. $query=$query." and (title like '%$key[$i]%' or subtitle like '%$key[$i]%')";
  105. $i++;
  106. }
  107. $query=$query.") group by biblio.biblionumber order by author,title";
  108. print $query;
  109. my $sth=$dbh->prepare($query);
  110. $sth->execute;
  111. $i=0;
  112. while (my $data=$sth->fetchrow_hashref){
  113. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  114. # print $results[$i];
  115. $i++;
  116. }
  117. $sth->finish;
  118. $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
  119. like '%$search->{'keyword'}%'");
  120. $sth->execute;
  121. while (my $data=$sth->fetchrow_hashref){
  122. my $sth2=$dbh->prepare("Select * from biblio where
  123. biblionumber=$data->{'biblionumber'}");
  124. $sth2->execute;
  125. while (my $data2=$sth2->fetchrow_hashref){
  126. $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data->{'copyrightdate'}";
  127. # print $results[$i];
  128. $i++;
  129. }
  130. $sth2->finish;
  131. }
  132. my $i2=1;
  133. @results=sort @results;
  134. my @res;
  135. my $count=@results;
  136. $i=1;
  137. $res[0]=$results[0];
  138. while ($i2 < $count){
  139. if ($results[$i2] ne $res[$i-1]){
  140. $res[$i]=$results[$i2];
  141. $i++;
  142. }
  143. $i2++;
  144. }
  145. $i2=0;
  146. my @res2;
  147. $count=@res;
  148. while ($i2 < $num && $i2 < $count){
  149. $res2[$i2]=$res[$i2+$offset];
  150. # print $res2[$i2];
  151. $i2++;
  152. }
  153. $sth->finish;
  154. $dbh->disconnect;
  155. return($i,@res2);
  156. }
  157. sub KeywordSearch {
  158. my ($env,$type,$search,$num,$offset)=@_;
  159. my $dbh = &C4Connect;
  160. $search->{'keyword'}=~ s/ +$//;
  161. $search->{'keyword'}=~ s/'/\\'/;
  162. my @key=split(' ',$search->{'keyword'});
  163. my $count=@key;
  164. my $i=1;
  165. my @results;
  166. my $query ="Select * from biblio,bibliosubtitle where
  167. biblio.biblionumber=bibliosubtitle.biblionumber and
  168. ((title like '%$key[0]%' or subtitle like '%$key[0]%')";
  169. while ($i < $count){
  170. $query=$query." and (title like '%$key[$i]%' or subtitle like '%$key[$i]%')";
  171. $i++;
  172. }
  173. $query=$query.") group by biblio.biblionumber order by author,title";
  174. # print $query;
  175. my $sth=$dbh->prepare($query);
  176. $sth->execute;
  177. $i=0;
  178. while (my $data=$sth->fetchrow_hashref){
  179. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  180. # print $results[$i];
  181. $i++;
  182. }
  183. $sth->finish;
  184. $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
  185. like '%$search->{'keyword'}%'");
  186. $sth->execute;
  187. while (my $data=$sth->fetchrow_hashref){
  188. my $sth2=$dbh->prepare("Select * from biblio where
  189. biblionumber=$data->{'biblionumber'}");
  190. $sth2->execute;
  191. while (my $data2=$sth2->fetchrow_hashref){
  192. $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data->{'copyrightdate'}";
  193. # print $results[$i];
  194. $i++;
  195. }
  196. $sth2->finish;
  197. }
  198. my $i2=1;
  199. @results=sort @results;
  200. my @res;
  201. my $count=@results;
  202. $i=1;
  203. $res[0]=$results[0];
  204. while ($i2 < $count){
  205. if ($results[$i2] ne $res[$i-1]){
  206. $res[$i]=$results[$i2];
  207. $i++;
  208. }
  209. $i2++;
  210. }
  211. $i2=0;
  212. my @res2;
  213. $count=@res;
  214. while ($i2 < $num && $i2 < $count){
  215. $res2[$i2]=$res[$i2+$offset];
  216. # print $res2[$i2];
  217. $i2++;
  218. }
  219. $sth->finish;
  220. $dbh->disconnect;
  221. return($i,@res2);
  222. }
  223. sub CatSearch {
  224. my ($env,$type,$search,$num,$offset)=@_;
  225. my $dbh = &C4Connect;
  226. my $query = '';
  227. my @results;
  228. $search->{'title'}=~ s/'/\\'/g;
  229. $search->{'author'}=~ s/'/\\'/g;
  230. my $title = lc($search->{'title'});
  231. if ($type eq 'loose') {
  232. if ($search->{'author'} ne ''){
  233. my @key=split(' ',$search->{'author'});
  234. my $count=@key;
  235. my $i=1;
  236. $query="select count(*) from
  237. biblio,biblioitems
  238. where biblioitems.biblionumber=biblio.biblionumber and
  239. (biblio.author like '%$key[0]%'";
  240. while ($i < $count){
  241. $query=$query." and author like '%$key[$i]%'";
  242. $i++;
  243. }
  244. $query=$query.")";
  245. if ($search->{'title'} ne ''){
  246. $query=$query. " and title like '%$search->{'title'}%'";
  247. }
  248. if ($search->{'class'} ne ''){
  249. $query.=" and biblioitems.itemtype='$search->{'class'}'";
  250. }
  251. } else {
  252. if ($search->{'title'} ne ''){
  253. my @key=split(' ',$search->{'title'});
  254. my $count=@key;
  255. my $i=1;
  256. $query="select count(*) from biblio,bibliosubtitle
  257. where
  258. (biblio.biblionumber=bibliosubtitle.biblionumber) and
  259. ((title like '$key[0]%' or title like '% $key[0]%'
  260. or subtitle like '$key[0]%' or title like '% $key[0]%')";
  261. while ($i<$count){
  262. $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or
  263. subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
  264. $i++;
  265. }
  266. $query=$query.")";
  267. # if ($search->{'class'} ne ''){
  268. # $query.=" and biblioitems.itemtype='$search->{'class'}'";
  269. # }
  270. } elsif ($search->{'class'} ne ''){
  271. $query="select count(*) from biblioitems,biblio where itemtype =
  272. '$search->{'class'}' and biblio.biblionumber=biblioitems.biblionumber";
  273. }
  274. }
  275. }
  276. if ($type eq 'subject'){
  277. $query="select distinct(subject) from bibliosubject where subject like
  278. '$search->{'subject'}%'";
  279. }
  280. if ($type eq 'precise'){
  281. $query="select count(*) from items,biblio ";
  282. if ($search->{'item'} ne ''){
  283. my $search2=uc $search->{'item'};
  284. $query=$query." where
  285. items.biblionumber=biblio.biblionumber
  286. and barcode='$search2'";
  287. }
  288. if ($search->{'isbn'} ne ''){
  289. my $search2=uc $search->{'isbn'};
  290. my $query1 = "select * from biblioitems where isbn='$search2'";
  291. my $sth1=$dbh->prepare($query1);
  292. $sth1->execute;
  293. my $i2=0;
  294. while (my $data=$sth1->fetchrow_hashref) {
  295. $query="select * from items,biblio where
  296. biblio.biblionumber = $data->{'biblionumber'}
  297. and items.biblionumber = biblio.biblionumber";
  298. my $sth=$dbh->prepare($query);
  299. # $sth->execute;
  300. # my $data=$sth->fetchrow_hashref;
  301. $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}";
  302. $i2++;
  303. # $sth->finish;
  304. }
  305. $sth1->finish;
  306. }
  307. }
  308. #print $query;
  309. my $sth=$dbh->prepare($query);
  310. # if ($search->{'isbn'} eq ''){
  311. $sth->execute;
  312. # } else {
  313. my $count=0;
  314. if ($type eq 'subject'){
  315. while (my $data=$sth->fetchrow_arrayref){
  316. $count++;
  317. }
  318. } else {
  319. my $data=$sth->fetchrow_arrayref;
  320. $count=$data->[0];
  321. }
  322. $sth->finish;
  323. $query=~ s/count\(\*\)/\*/g;
  324. if ($search->{'title'} || $search->{'author'}){
  325. $query=$query." group by biblio.biblionumber";
  326. }
  327. if ($type ne 'precise' && $type ne 'subject'){
  328. if ($search->{'author'} ne ''){
  329. $query=$query." order by author,title limit $offset,$num";
  330. } else {
  331. $query=$query." order by title limit $offset,$num";
  332. }
  333. } else {
  334. if ($type eq 'subject'){
  335. $query=$query." order by subject limit $offset,$num";
  336. }
  337. }
  338. $sth=$dbh->prepare($query);
  339. $sth->execute;
  340. my $i=0;
  341. my $i2=0;
  342. my $limit=$num+$offset;
  343. # if ($search->{'title'} ne '' || $search->{'author'} ne '' ){
  344. # while ((my $data=$sth->fetchrow_hashref) && $i < $limit){
  345. # if ($i >= $offset){
  346. #
  347. # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}";
  348. # $i2++;
  349. # }
  350. # $i++;
  351. # }
  352. # } else {
  353. while (my $data=$sth->fetchrow_hashref){
  354. if ($type ne 'subject'){
  355. $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  356. } elsif ($search->{'isbn'} ne ''){
  357. } else {
  358. $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}";
  359. }
  360. $i++;
  361. }
  362. # }
  363. $sth->finish;
  364. # print "$query\n";
  365. #only update stats if search is from opac
  366. # updatesearchstats($dbh,$query);
  367. $dbh->disconnect;
  368. if ($search->{'isbn'} ne ''){
  369. $count=1;
  370. }
  371. return($count,@results);
  372. }
  373. sub updatesearchstats{
  374. my ($dbh,$query)=@_;
  375. }
  376. sub subsearch {
  377. my ($env,$subject)=@_;
  378. my $dbh=C4Connect();
  379. my $query="Select * from biblio,bibliosubject where
  380. biblio.biblionumber=bibliosubject.biblionumber and
  381. bibliosubject.subject='$subject'";
  382. my $sth=$dbh->prepare($query);
  383. $sth->execute;
  384. my $i=0;
  385. # print $query;
  386. my @results;
  387. while (my $data=$sth->fetchrow_hashref){
  388. $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
  389. $i++;
  390. }
  391. $sth->finish;
  392. $dbh->disconnect;
  393. return(@results);
  394. }
  395. sub ItemInfo {
  396. my ($env,$biblionumber)=@_;
  397. my $dbh = &C4Connect;
  398. my $query="Select * from items,biblio,biblioitems,branches
  399. where (items.biblioitemnumber = biblioitems.biblioitemnumber)
  400. and biblioitems.biblionumber=biblio.biblionumber
  401. and biblio.biblionumber='$biblionumber' and branches.branchcode=
  402. items.holdingbranch and items.itemlost<>1";
  403. my $sth=$dbh->prepare($query);
  404. $sth->execute;
  405. my $i=0;
  406. my @results;
  407. while (my $data=$sth->fetchrow_hashref){
  408. my $iquery = "Select * from issues
  409. where itemnumber = '$data->{'itemnumber'}'
  410. and returndate is null";
  411. my $datedue = '';
  412. my $isth=$dbh->prepare($iquery);
  413. $isth->execute;
  414. if (my $idata=$isth->fetchrow_hashref){
  415. my @temp=split('-',$idata->{'date_due'});
  416. $datedue = "$temp[2]/$temp[1]/$temp[0]";
  417. }
  418. $isth->finish;
  419. my $class = $data->{'classification'};
  420. my $dewey = $data->{'dewey'};
  421. $dewey =~ s/0+$//;
  422. if ($dewey eq "000.") { $dewey = "";};
  423. if ($dewey < 10){$dewey='00'.$dewey;}
  424. if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
  425. if ($dewey <= 0){
  426. $dewey='';
  427. }
  428. $class = $class.$dewey;
  429. $class = $class.$data->{'subclass'};
  430. # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
  431. my @temp=split('-',$data->{'datelastseen'});
  432. my $date="$temp[2]/$temp[1]/$temp[0]";
  433. $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$class\t$data->{'itemnumber'}\t$data->{'itemtype'}\t$date\t$data->{'biblioitemnumber'}";
  434. $i++;
  435. }
  436. $sth->finish;
  437. $dbh->disconnect;
  438. return(@results);
  439. }
  440. sub GetItems {
  441. my ($env,$biblionumber)=@_;
  442. #debug_msg($env,"GetItems");
  443. my $dbh = &C4Connect;
  444. my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
  445. #debug_msg($env,$query);
  446. my $sth=$dbh->prepare($query);
  447. $sth->execute;
  448. #debug_msg($env,"executed query");
  449. my $i=0;
  450. my @results;
  451. while (my $data=$sth->fetchrow_hashref) {
  452. #debug_msg($env,$data->{'biblioitemnumber'});
  453. my $dewey = $data->{'dewey'};
  454. $dewey =~ s/0+$//;
  455. my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
  456. $line = $line."\t$data->{'classification'}\t$dewey";
  457. $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
  458. $line = $line."\t$data->{'volume'}\t$data->{number}";
  459. my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
  460. $isth->execute;
  461. while (my $idata = $isth->fetchrow_hashref) {
  462. my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
  463. if ($idata->{'notforloan'} == 1) {
  464. $iline = $iline."NFL ";
  465. }
  466. if ($idata->{'itemlost'} == 1) {
  467. $iline = $iline."LOST ";
  468. }
  469. $line = $line."\t$iline";
  470. }
  471. $isth->finish;
  472. $results[$i] = $line;
  473. $i++;
  474. }
  475. $sth->finish;
  476. $dbh->disconnect;
  477. return(@results);
  478. }
  479. sub itemdata {
  480. my ($barcode)=@_;
  481. my $dbh=C4Connect;
  482. my $query="Select * from items,biblioitems where barcode='$barcode'
  483. and items.biblioitemnumber=biblioitems.biblioitemnumber";
  484. # print $query;
  485. my $sth=$dbh->prepare($query);
  486. $sth->execute;
  487. my $data=$sth->fetchrow_hashref;
  488. $sth->finish;
  489. $dbh->disconnect;
  490. return($data);
  491. }
  492. sub bibdata {
  493. my ($bibnum,$type)=@_;
  494. my $dbh=C4Connect;
  495. my $query="Select * from biblio,biblioitems,bibliosubject,bibliosubtitle where biblio.biblionumber=$bibnum
  496. and biblioitems.biblionumber=$bibnum and
  497. (bibliosubject.biblionumber=$bibnum or bibliosubject.biblionumber=1) and
  498. (bibliosubtitle.biblionumber=$bibnum)";
  499. # print $query;
  500. my $sth=$dbh->prepare($query);
  501. $sth->execute;
  502. my $data=$sth->fetchrow_hashref;
  503. $sth->finish;
  504. $dbh->disconnect;
  505. return($data);
  506. }
  507. sub bibitemdata {
  508. my ($bibitem)=@_;
  509. my $dbh=C4Connect;
  510. my $query="Select * from biblio,biblioitems,itemtypes where biblio.biblionumber=
  511. biblioitems.biblionumber and biblioitemnumber=$bibitem and
  512. biblioitems.itemtype=itemtypes.itemtype";
  513. # print $query;
  514. my $sth=$dbh->prepare($query);
  515. $sth->execute;
  516. my $data=$sth->fetchrow_hashref;
  517. $sth->finish;
  518. $dbh->disconnect;
  519. return($data);
  520. }
  521. sub itemissues {
  522. my ($bibitem)=@_;
  523. my $dbh=C4Connect;
  524. my $query="Select * from items where
  525. items.biblioitemnumber=$bibitem";
  526. # print $query;
  527. my $sth=$dbh->prepare($query);
  528. $sth->execute;
  529. my $i=0;
  530. my @results;
  531. while (my $data=$sth->fetchrow_hashref){
  532. my $query2="select * from issues,borrowers where itemnumber=$data->{'itemnumber'}
  533. and returndate is NULL and issues.borrowernumber=borrowers.borrowernumber";
  534. my $sth2=$dbh->prepare($query2);
  535. $sth2->execute;
  536. if (my $data2=$sth2->fetchrow_hashref){
  537. $data->{'date_due'}=$data2->{'date_due'};
  538. $data->{'card'}=$data2->{'cardnumber'};
  539. } else {
  540. $data->{'date_due'}='Available';
  541. }
  542. $sth2->finish;
  543. $query2="select * from issues,borrowers where itemnumber=$data->{'itemnumber'}
  544. and issues.borrowernumber=borrowers.borrowernumber
  545. order by date_due desc";
  546. my $sth2=$dbh->prepare($query2);
  547. # print $query2;
  548. $sth2->execute;
  549. for (my $i2=0;$i2<2;$i2++){
  550. if (my $data2=$sth2->fetchrow_hashref){
  551. $data->{"timestamp$i2"}=$data2->{'timestamp'};
  552. $data->{"card$i2"}=$data2->{'cardnumber'};
  553. }
  554. }
  555. $sth2->finish;
  556. $results[$i]=$data;
  557. $i++;
  558. }
  559. $sth->finish;
  560. $dbh->disconnect;
  561. return(@results);
  562. }
  563. sub itemnodata {
  564. my ($env,$dbh,$itemnumber) = @_;
  565. my $query="Select * from biblio,items,biblioitems
  566. where items.itemnumber = '$itemnumber'
  567. and biblio.biblionumber = items.biblionumber
  568. and biblioitems.biblioitemnumber = items.biblioitemnumber";
  569. my $sth=$dbh->prepare($query);
  570. # print $query;
  571. $sth->execute;
  572. my $data=$sth->fetchrow_hashref;
  573. $sth->finish;
  574. return($data);
  575. }
  576. #used by member enquiries from the intranet
  577. #called by member.pl
  578. sub BornameSearch {
  579. my ($env,$searchstring,$type)=@_;
  580. my $dbh = &C4Connect;
  581. my @data=split(' ',$searchstring);
  582. my $count=@data;
  583. my $query="Select * from borrowers
  584. where ((surname like '%$data[0]%'
  585. or firstname like '%$data[0]%'
  586. or othernames like '%$data[0]%')
  587. ";
  588. for (my $i=1;$i<$count;$i++){
  589. $query=$query." and (surname like '%$data[$i]%'
  590. or firstname like '%$data[$i]%'
  591. or othernames like '%$data[$i]%')";
  592. }
  593. $query=$query.") or cardnumber = '$searchstring'
  594. order by surname,firstname";
  595. # print $query,"\n";
  596. my $sth=$dbh->prepare($query);
  597. $sth->execute;
  598. my @results;
  599. my $cnt=0;
  600. while (my $data=$sth->fetchrow_hashref){
  601. push(@results,$data);
  602. $cnt ++;
  603. }
  604. # $sth->execute;
  605. $sth->finish;
  606. $dbh->disconnect;
  607. return ($cnt,\@results);
  608. }
  609. sub borrdata {
  610. my ($cardnumber,$bornum)=@_;
  611. $cardnumber = uc $cardnumber;
  612. my $dbh=C4Connect;
  613. my $query;
  614. if ($bornum eq ''){
  615. $query="Select * from borrowers where cardnumber='$cardnumber'";
  616. } else {
  617. $query="Select * from borrowers where borrowernumber='$bornum'";
  618. }
  619. #print $query;
  620. my $sth=$dbh->prepare($query);
  621. $sth->execute;
  622. my $data=$sth->fetchrow_hashref;
  623. $sth->finish;
  624. $dbh->disconnect;
  625. return($data);
  626. }
  627. sub borrissues {
  628. my ($bornum)=@_;
  629. my $dbh=C4Connect;
  630. my $query;
  631. $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
  632. items.itemnumber=issues.itemnumber and
  633. items.biblionumber=biblio.biblionumber and issues.returndate is NULL";
  634. #print $query;
  635. my $sth=$dbh->prepare($query);
  636. $sth->execute;
  637. my @result;
  638. my $i=0;
  639. while (my $data=$sth->fetchrow_hashref){
  640. $result[$i]=$data;;
  641. $i++;
  642. }
  643. $sth->finish;
  644. $dbh->disconnect;
  645. return($i,\@result);
  646. }
  647. sub borrdata2 {
  648. my ($env,$bornum)=@_;
  649. my $dbh=C4Connect;
  650. my $query="Select count(*) from issues where borrowernumber='$bornum' and
  651. returndate is NULL";
  652. # print $query;
  653. my $sth=$dbh->prepare($query);
  654. $sth->execute;
  655. my $data=$sth->fetchrow_hashref;
  656. $sth->finish;
  657. $sth=$dbh->prepare("Select count(*) from issues where
  658. borrowernumber='$bornum' and date_due < now() and returndate is NULL");
  659. $sth->execute;
  660. my $data2=$sth->fetchrow_hashref;
  661. $sth->finish;
  662. $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
  663. borrowernumber='$bornum'");
  664. $sth->execute;
  665. my $data3=$sth->fetchrow_hashref;
  666. $sth->finish;
  667. $dbh->disconnect;
  668. return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
  669. }
  670. sub getacctlist {
  671. my ($env,$params) = @_;
  672. my $dbh=C4Connect;
  673. my @acctlines;
  674. my $numlines;
  675. my $query = "Select borrowernumber, accountno, date, amount, description,
  676. dispute, accounttype, amountoutstanding, barcode, title
  677. from accountlines,items,biblio
  678. where borrowernumber = $params->{'borrowernumber'} ";
  679. if ($params->{'acctno'} ne "") {
  680. my $query = $query." and accountlines.accountno = $params->{'acctno'} ";
  681. }
  682. my $query = $query." and accountlines.itemnumber = items.itemnumber
  683. and items.biblionumber = biblio.biblionumber
  684. and accountlines.amountoutstanding<>0 order by date";
  685. my $sth=$dbh->prepare($query);
  686. # print $query;
  687. $sth->execute;
  688. my $total=0;
  689. while (my $data=$sth->fetchrow_hashref){
  690. $acctlines[$numlines] = $data;
  691. $numlines++;
  692. $total = $total+ $data->{'amountoutstanding'};
  693. }
  694. return ($numlines,\@acctlines,$total);
  695. $sth->finish;
  696. $dbh->disconnect;
  697. }
  698. sub getboracctrecord {
  699. my ($env,$params) = @_;
  700. my $dbh=C4Connect;
  701. my @acctlines;
  702. my $numlines;
  703. my $query = "Select * from accountlines
  704. where borrowernumber = $params->{'borrowernumber'}
  705. order by date desc";
  706. my $sth=$dbh->prepare($query);
  707. # print $query;
  708. $sth->execute;
  709. my $total=0;
  710. while (my $data=$sth->fetchrow_hashref){
  711. $acctlines[$numlines] = $data;
  712. $numlines++;
  713. $total = $total+ $data->{'amountoutstanding'};
  714. }
  715. return ($numlines,\@acctlines,$total);
  716. $sth->finish;
  717. $dbh->disconnect;
  718. }
  719. sub itemcount {
  720. my ($env,$bibnum)=@_;
  721. my $dbh=C4Connect;
  722. my $query="Select * from items where
  723. biblionumber=$bibnum and itemlost <>1";
  724. my $sth=$dbh->prepare($query);
  725. # print $query;
  726. $sth->execute;
  727. my $count=0;
  728. my $lcount=0;
  729. my $nacount=0;
  730. my $fcount=0;
  731. my $scount=0;
  732. while (my $data=$sth->fetchrow_hashref){
  733. $count++;
  734. my $query2="select * from issues where itemnumber=
  735. '$data->{'itemnumber'}' and returndate is NULL";
  736. my $sth2=$dbh->prepare($query2);
  737. $sth2->execute;
  738. if (my $data2=$sth2->fetchrow_hashref){
  739. $nacount++;
  740. } else {
  741. if ($data->{'holdingbranch'} eq 'C'){
  742. $lcount++;
  743. }
  744. if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
  745. $fcount++;
  746. }
  747. if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
  748. $scount++;
  749. }
  750. }
  751. $sth2->finish;
  752. }
  753. $sth->finish;
  754. $dbh->disconnect;
  755. return ($count,$lcount,$nacount,$fcount,$scount);
  756. }
  757. sub ItemType {
  758. my ($type)=@_;
  759. my $dbh=C4Connect;
  760. my $query="select description from itemtypes where itemtype='$type'";
  761. my $sth=$dbh->prepare($query);
  762. $sth->execute;
  763. my $dat=$sth->fetchrow_hashref;
  764. $sth->finish;
  765. $dbh->disconnect;
  766. return ($dat->{'description'});
  767. }
  768. END { } # module clean-up code here (global destructor)