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.

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