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.

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