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.

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