Add pl-PL to syspref test
[koha.git] / C4 / Serials.pm
blobd72e98cc9afd76a6f6b6208476abdb8452b70749
1 package C4::Serials; #assumes C4/Serials.pm
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use C4::Dates qw(format_date);
22 use Date::Calc qw(:all);
23 use POSIX qw(strftime);
24 use C4::Suggestions;
25 use C4::Koha;
26 use C4::Biblio;
27 use C4::Branch;
28 use C4::Items;
29 use C4::Search;
30 use C4::Letters;
31 use C4::Log; # logaction
32 use C4::Debug;
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 BEGIN {
37 $VERSION = 3.01; # set version for version checking
38 require Exporter;
39 @ISA = qw(Exporter);
40 @EXPORT = qw(
41 &NewSubscription &ModSubscription &DelSubscription &GetSubscriptions
42 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
43 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
44 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
46 &GetNextSeq &NewIssue &ItemizeSerials &GetSerials
47 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
48 &ReNewSubscription &GetLateIssues &GetLateOrMissingIssues
49 &GetSerialInformation &AddItem2Serial
50 &PrepareSerialsData &GetNextExpected &ModNextExpected
52 &UpdateClaimdateIssues
53 &GetSuppliersWithLateIssues &getsupplierbyserialid
54 &GetDistributedTo &SetDistributedTo
55 &getroutinglist &delroutingmember &addroutingmember
56 &reorder_members
57 &check_routing &updateClaim &removeMissingIssue
58 &CountIssues
59 HasItems
64 =head2 GetSuppliersWithLateIssues
66 =head1 NAME
68 C4::Serials - Give functions for serializing.
70 =head1 SYNOPSIS
72 use C4::Serials;
74 =head1 DESCRIPTION
76 Give all XYZ functions
78 =head1 FUNCTIONS
80 =over 4
82 %supplierlist = &GetSuppliersWithLateIssues
84 this function get all suppliers with late issues.
86 return :
87 the supplierlist into a hash. this hash containts id & name of the supplier
88 Only valid suppliers are returned. Late subscriptions lacking a supplier are
89 ignored.
91 =back
93 =cut
95 sub GetSuppliersWithLateIssues {
96 my $dbh = C4::Context->dbh;
97 my $query = qq|
98 SELECT DISTINCT id, name
99 FROM subscription
100 LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
101 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
102 WHERE subscription.subscriptionid = serial.subscriptionid
103 AND (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4)
104 ORDER BY name
106 my $sth = $dbh->prepare($query);
107 $sth->execute;
108 my %supplierlist;
109 while ( my ( $id, $name ) = $sth->fetchrow ) {
110 next if !defined $id;
111 $supplierlist{$id} = $name;
113 return %supplierlist;
116 =head2 GetLateIssues
118 =over 4
120 @issuelist = &GetLateIssues($supplierid)
122 this function select late issues on database
124 return :
125 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
126 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
128 =back
130 =cut
132 sub GetLateIssues {
133 my ($supplierid) = @_;
134 my $dbh = C4::Context->dbh;
135 my $sth;
136 if ($supplierid) {
137 my $query = qq|
138 SELECT name,title,planneddate,serialseq,serial.subscriptionid
139 FROM subscription
140 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
141 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
142 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
143 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
144 AND subscription.aqbooksellerid=$supplierid
145 ORDER BY title
147 $sth = $dbh->prepare($query);
148 } else {
149 my $query = qq|
150 SELECT name,title,planneddate,serialseq,serial.subscriptionid
151 FROM subscription
152 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
153 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
154 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
155 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
156 ORDER BY title
158 $sth = $dbh->prepare($query);
160 $sth->execute;
161 my @issuelist;
162 my $last_title;
163 my $odd = 0;
164 my $count = 0;
165 while ( my $line = $sth->fetchrow_hashref ) {
166 $odd++ unless $line->{title} eq $last_title;
167 $line->{title} = "" if $line->{title} eq $last_title;
168 $last_title = $line->{title} if ( $line->{title} );
169 $line->{planneddate} = format_date( $line->{planneddate} );
170 $count++;
171 push @issuelist, $line;
173 return $count, @issuelist;
176 =head2 GetSubscriptionHistoryFromSubscriptionId
178 =over 4
180 $sth = GetSubscriptionHistoryFromSubscriptionId()
181 this function just prepare the SQL request.
182 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
183 return :
184 $sth = $dbh->prepare($query).
186 =back
188 =cut
190 sub GetSubscriptionHistoryFromSubscriptionId() {
191 my $dbh = C4::Context->dbh;
192 my $query = qq|
193 SELECT *
194 FROM subscriptionhistory
195 WHERE subscriptionid = ?
197 return $dbh->prepare($query);
200 =head2 GetSerialStatusFromSerialId
202 =over 4
204 $sth = GetSerialStatusFromSerialId();
205 this function just prepare the SQL request.
206 After this function, don't forget to execute it by using $sth->execute($serialid)
207 return :
208 $sth = $dbh->prepare($query).
210 =back
212 =cut
214 sub GetSerialStatusFromSerialId() {
215 my $dbh = C4::Context->dbh;
216 my $query = qq|
217 SELECT status
218 FROM serial
219 WHERE serialid = ?
221 return $dbh->prepare($query);
224 =head2 GetSerialInformation
226 =over 4
228 $data = GetSerialInformation($serialid);
229 returns a hash containing :
230 items : items marcrecord (can be an array)
231 serial table field
232 subscription table field
233 + information about subscription expiration
235 =back
237 =cut
239 sub GetSerialInformation {
240 my ($serialid) = @_;
241 my $dbh = C4::Context->dbh;
242 my $query = qq|
243 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
244 if ( C4::Context->preference('IndependantBranches')
245 && C4::Context->userenv
246 && C4::Context->userenv->{'flags'} != 1
247 && C4::Context->userenv->{'branch'} ) {
248 $query .= "
249 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
251 $query .= qq|
252 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
253 WHERE serialid = ?
255 my $rq = $dbh->prepare($query);
256 $rq->execute($serialid);
257 my $data = $rq->fetchrow_hashref;
259 # create item information if we have serialsadditems for this subscription
260 if ( $data->{'serialsadditems'} ) {
261 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
262 $queryitem->execute($serialid);
263 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
264 if ( scalar(@$itemnumbers) > 0 ) {
265 foreach my $itemnum (@$itemnumbers) {
267 #It is ASSUMED that GetMarcItem ALWAYS WORK...
268 #Maybe GetMarcItem should return values on failure
269 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
270 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0], $data );
271 $itemprocessed->{'itemnumber'} = $itemnum->[0];
272 $itemprocessed->{'itemid'} = $itemnum->[0];
273 $itemprocessed->{'serialid'} = $serialid;
274 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
275 push @{ $data->{'items'} }, $itemprocessed;
277 } else {
278 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
279 $itemprocessed->{'itemid'} = "N$serialid";
280 $itemprocessed->{'serialid'} = $serialid;
281 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
282 $itemprocessed->{'countitems'} = 0;
283 push @{ $data->{'items'} }, $itemprocessed;
286 $data->{ "status" . $data->{'serstatus'} } = 1;
287 $data->{'subscriptionexpired'} = HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
288 $data->{'abouttoexpire'} = abouttoexpire( $data->{'subscriptionid'} );
289 return $data;
292 =head2 AddItem2Serial
294 =over 4
296 $data = AddItem2Serial($serialid,$itemnumber);
297 Adds an itemnumber to Serial record
299 =back
301 =cut
303 sub AddItem2Serial {
304 my ( $serialid, $itemnumber ) = @_;
305 my $dbh = C4::Context->dbh;
306 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
307 $rq->execute( $serialid, $itemnumber );
308 return $rq->rows;
311 =head2 UpdateClaimdateIssues
313 =over 4
315 UpdateClaimdateIssues($serialids,[$date]);
317 Update Claimdate for issues in @$serialids list with date $date
318 (Take Today if none)
320 =back
322 =cut
324 sub UpdateClaimdateIssues {
325 my ( $serialids, $date ) = @_;
326 my $dbh = C4::Context->dbh;
327 $date = strftime( "%Y-%m-%d", localtime ) unless ($date);
328 my $query = "
329 UPDATE serial SET claimdate=$date,status=7
330 WHERE serialid in (" . join( ",", @$serialids ) . ")";
331 my $rq = $dbh->prepare($query);
332 $rq->execute;
333 return $rq->rows;
336 =head2 GetSubscription
338 =over 4
340 $subs = GetSubscription($subscriptionid)
341 this function get the subscription which has $subscriptionid as id.
342 return :
343 a hashref. This hash containts
344 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
346 =back
348 =cut
350 sub GetSubscription {
351 my ($subscriptionid) = @_;
352 my $dbh = C4::Context->dbh;
353 my $query = qq(
354 SELECT subscription.*,
355 subscriptionhistory.*,
356 aqbooksellers.name AS aqbooksellername,
357 biblio.title AS bibliotitle,
358 subscription.biblionumber as bibnum);
359 if ( C4::Context->preference('IndependantBranches')
360 && C4::Context->userenv
361 && C4::Context->userenv->{'flags'} != 1
362 && C4::Context->userenv->{'branch'} ) {
363 $query .= "
364 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
366 $query .= qq(
367 FROM subscription
368 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
369 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
370 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
371 WHERE subscription.subscriptionid = ?
374 # if (C4::Context->preference('IndependantBranches') &&
375 # C4::Context->userenv &&
376 # C4::Context->userenv->{'flags'} != 1){
377 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
378 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
380 $debug and warn "query : $query\nsubsid :$subscriptionid";
381 my $sth = $dbh->prepare($query);
382 $sth->execute($subscriptionid);
383 return $sth->fetchrow_hashref;
386 =head2 GetFullSubscription
388 =over 4
390 \@res = GetFullSubscription($subscriptionid)
391 this function read on serial table.
393 =back
395 =cut
397 sub GetFullSubscription {
398 my ($subscriptionid) = @_;
399 my $dbh = C4::Context->dbh;
400 my $query = qq|
401 SELECT serial.serialid,
402 serial.serialseq,
403 serial.planneddate,
404 serial.publisheddate,
405 serial.status,
406 serial.notes as notes,
407 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
408 aqbooksellers.name as aqbooksellername,
409 biblio.title as bibliotitle,
410 subscription.branchcode AS branchcode,
411 subscription.subscriptionid AS subscriptionid |;
412 if ( C4::Context->preference('IndependantBranches')
413 && C4::Context->userenv
414 && C4::Context->userenv->{'flags'} != 1
415 && C4::Context->userenv->{'branch'} ) {
416 $query .= "
417 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
419 $query .= qq|
420 FROM serial
421 LEFT JOIN subscription ON
422 (serial.subscriptionid=subscription.subscriptionid )
423 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
424 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
425 WHERE serial.subscriptionid = ?
426 ORDER BY year DESC,
427 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
428 serial.subscriptionid
430 $debug and warn "GetFullSubscription query: $query";
431 my $sth = $dbh->prepare($query);
432 $sth->execute($subscriptionid);
433 return $sth->fetchall_arrayref( {} );
436 =head2 PrepareSerialsData
438 =over 4
440 \@res = PrepareSerialsData($serialinfomation)
441 where serialinformation is a hashref array
443 =back
445 =cut
447 sub PrepareSerialsData {
448 my ($lines) = @_;
449 my %tmpresults;
450 my $year;
451 my @res;
452 my $startdate;
453 my $aqbooksellername;
454 my $bibliotitle;
455 my @loopissues;
456 my $first;
457 my $previousnote = "";
459 foreach my $subs (@$lines) {
460 $subs->{'publisheddate'} = (
461 $subs->{'publisheddate'}
462 ? format_date( $subs->{'publisheddate'} )
463 : "XXX"
465 $subs->{'branchname'} = GetBranchName( $subs->{'branchcode'} );
466 $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
467 $subs->{ "status" . $subs->{'status'} } = 1;
468 $subs->{"checked"} = $subs->{'status'} =~ /1|3|4|7/;
470 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
471 $year = $subs->{'year'};
472 } else {
473 $year = "manage";
475 if ( $tmpresults{$year} ) {
476 push @{ $tmpresults{$year}->{'serials'} }, $subs;
477 } else {
478 $tmpresults{$year} = {
479 'year' => $year,
480 'aqbooksellername' => $subs->{'aqbooksellername'},
481 'bibliotitle' => $subs->{'bibliotitle'},
482 'serials' => [$subs],
483 'first' => $first,
487 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
488 push @res, $tmpresults{$key};
490 $res[0]->{'first'} = 1;
491 return \@res;
494 =head2 GetSubscriptionsFromBiblionumber
496 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
497 this function get the subscription list. it reads on subscription table.
498 return :
499 table of subscription which has the biblionumber given on input arg.
500 each line of this table is a hashref. All hashes containt
501 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
503 =cut
505 sub GetSubscriptionsFromBiblionumber {
506 my ($biblionumber) = @_;
507 my $dbh = C4::Context->dbh;
508 my $query = qq(
509 SELECT subscription.*,
510 branches.branchname,
511 subscriptionhistory.*,
512 aqbooksellers.name AS aqbooksellername,
513 biblio.title AS bibliotitle
514 FROM subscription
515 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
516 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
517 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
518 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
519 WHERE subscription.biblionumber = ?
521 my $sth = $dbh->prepare($query);
522 $sth->execute($biblionumber);
523 my @res;
524 while ( my $subs = $sth->fetchrow_hashref ) {
525 $subs->{startdate} = format_date( $subs->{startdate} );
526 $subs->{histstartdate} = format_date( $subs->{histstartdate} );
527 $subs->{histenddate} = format_date( $subs->{histenddate} );
528 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
529 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
530 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
531 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
532 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
533 $subs->{ "status" . $subs->{'status'} } = 1;
534 $subs->{'cannotedit'} =
535 ( C4::Context->preference('IndependantBranches')
536 && C4::Context->userenv
537 && C4::Context->userenv->{flags} % 2 != 1
538 && C4::Context->userenv->{branch}
539 && $subs->{branchcode}
540 && ( C4::Context->userenv->{branch} ne $subs->{branchcode} ) );
542 if ( $subs->{enddate} eq '0000-00-00' ) {
543 $subs->{enddate} = '';
544 } else {
545 $subs->{enddate} = format_date( $subs->{enddate} );
547 $subs->{'abouttoexpire'} = abouttoexpire( $subs->{'subscriptionid'} );
548 $subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
549 push @res, $subs;
551 return \@res;
554 =head2 GetFullSubscriptionsFromBiblionumber
556 =over 4
558 \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
559 this function read on serial table.
561 =back
563 =cut
565 sub GetFullSubscriptionsFromBiblionumber {
566 my ($biblionumber) = @_;
567 my $dbh = C4::Context->dbh;
568 my $query = qq|
569 SELECT serial.serialid,
570 serial.serialseq,
571 serial.planneddate,
572 serial.publisheddate,
573 serial.status,
574 serial.notes as notes,
575 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
576 biblio.title as bibliotitle,
577 subscription.branchcode AS branchcode,
578 subscription.subscriptionid AS subscriptionid|;
579 if ( C4::Context->preference('IndependantBranches')
580 && C4::Context->userenv
581 && C4::Context->userenv->{'flags'} != 1
582 && C4::Context->userenv->{'branch'} ) {
583 $query .= "
584 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
587 $query .= qq|
588 FROM serial
589 LEFT JOIN subscription ON
590 (serial.subscriptionid=subscription.subscriptionid)
591 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
592 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
593 WHERE subscription.biblionumber = ?
594 ORDER BY year DESC,
595 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
596 serial.subscriptionid
598 my $sth = $dbh->prepare($query);
599 $sth->execute($biblionumber);
600 return $sth->fetchall_arrayref( {} );
603 =head2 GetSubscriptions
605 =over 4
607 @results = GetSubscriptions($title,$ISSN,$biblionumber);
608 this function get all subscriptions which has title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
609 return:
610 a table of hashref. Each hash containt the subscription.
612 =back
614 =cut
616 sub GetSubscriptions {
617 my ( $string, $issn, $biblionumber ) = @_;
619 #return unless $title or $ISSN or $biblionumber;
620 my $dbh = C4::Context->dbh;
621 my $sth;
622 my $sql = qq(
623 SELECT subscription.*, subscriptionhistory.*, biblio.title,biblioitems.issn,biblio.biblionumber
624 FROM subscription
625 LEFT JOIN subscriptionhistory USING(subscriptionid)
626 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
627 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
629 my @bind_params;
630 my $sqlwhere;
631 if ($biblionumber) {
632 $sqlwhere = " WHERE biblio.biblionumber=?";
633 push @bind_params, $biblionumber;
635 if ($string) {
636 my @sqlstrings;
637 my @strings_to_search;
638 @strings_to_search = map { "%$_%" } split( / /, $string );
639 foreach my $index qw(biblio.title subscription.callnumber subscription.location subscription.notes subscription.internalnotes) {
640 push @bind_params, @strings_to_search;
641 my $tmpstring = "AND $index LIKE ? " x scalar(@strings_to_search);
642 $debug && warn "$tmpstring";
643 $tmpstring =~ s/^AND //;
644 push @sqlstrings, $tmpstring;
646 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
648 if ($issn) {
649 my @sqlstrings;
650 my @strings_to_search;
651 @strings_to_search = map { "%$_%" } split( / /, $issn );
652 foreach my $index qw(biblioitems.issn subscription.callnumber) {
653 push @bind_params, @strings_to_search;
654 my $tmpstring = "OR $index LIKE ? " x scalar(@strings_to_search);
655 $debug && warn "$tmpstring";
656 $tmpstring =~ s/^OR //;
657 push @sqlstrings, $tmpstring;
659 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
661 $sql .= "$sqlwhere ORDER BY title";
662 $debug and warn "GetSubscriptions query: $sql params : ", join( " ", @bind_params );
663 $sth = $dbh->prepare($sql);
664 $sth->execute(@bind_params);
665 my @results;
666 my $previoustitle = "";
667 my $odd = 1;
669 while ( my $line = $sth->fetchrow_hashref ) {
670 if ( $previoustitle eq $line->{title} ) {
671 $line->{title} = "";
672 $line->{issn} = "";
673 } else {
674 $previoustitle = $line->{title};
675 $odd = -$odd;
677 $line->{toggle} = 1 if $odd == 1;
678 $line->{'cannotedit'} =
679 ( C4::Context->preference('IndependantBranches')
680 && C4::Context->userenv
681 && C4::Context->userenv->{flags} % 2 != 1
682 && C4::Context->userenv->{branch}
683 && $line->{branchcode}
684 && ( C4::Context->userenv->{branch} ne $line->{branchcode} ) );
685 push @results, $line;
687 return @results;
690 =head2 GetSerials
692 =over 4
694 ($totalissues,@serials) = GetSerials($subscriptionid);
695 this function get every serial not arrived for a given subscription
696 as well as the number of issues registered in the database (all types)
697 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
699 FIXME: We should return \@serials.
701 =back
703 =cut
705 sub GetSerials {
706 my ( $subscriptionid, $count ) = @_;
707 my $dbh = C4::Context->dbh;
709 # status = 2 is "arrived"
710 my $counter = 0;
711 $count = 5 unless ($count);
712 my @serials;
713 my $query = "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
714 FROM serial
715 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
716 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
717 my $sth = $dbh->prepare($query);
718 $sth->execute($subscriptionid);
720 while ( my $line = $sth->fetchrow_hashref ) {
721 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
722 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
723 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
724 push @serials, $line;
727 # OK, now add the last 5 issues arrives/missing
728 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
729 FROM serial
730 WHERE subscriptionid = ?
731 AND (status in (2,4,5))
732 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
734 $sth = $dbh->prepare($query);
735 $sth->execute($subscriptionid);
736 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
737 $counter++;
738 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
739 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
740 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
741 push @serials, $line;
744 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
745 $sth = $dbh->prepare($query);
746 $sth->execute($subscriptionid);
747 my ($totalissues) = $sth->fetchrow;
748 return ( $totalissues, @serials );
751 =head2 GetSerials2
753 =over 4
755 ($totalissues,@serials) = GetSerials2($subscriptionid,$status);
756 this function get every serial waited for a given subscription
757 as well as the number of issues registered in the database (all types)
758 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
760 =back
762 =cut
764 sub GetSerials2 {
765 my ( $subscription, $status ) = @_;
766 my $dbh = C4::Context->dbh;
767 my $query = qq|
768 SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
769 FROM serial
770 WHERE subscriptionid=$subscription AND status IN ($status)
771 ORDER BY publisheddate,serialid DESC
773 $debug and warn "GetSerials2 query: $query";
774 my $sth = $dbh->prepare($query);
775 $sth->execute;
776 my @serials;
778 while ( my $line = $sth->fetchrow_hashref ) {
779 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
780 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
781 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
782 push @serials, $line;
784 my ($totalissues) = scalar(@serials);
785 return ( $totalissues, @serials );
788 =head2 GetLatestSerials
790 =over 4
792 \@serials = GetLatestSerials($subscriptionid,$limit)
793 get the $limit's latest serials arrived or missing for a given subscription
794 return :
795 a ref to a table which it containts all of the latest serials stored into a hash.
797 =back
799 =cut
801 sub GetLatestSerials {
802 my ( $subscriptionid, $limit ) = @_;
803 my $dbh = C4::Context->dbh;
805 # status = 2 is "arrived"
806 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
807 FROM serial
808 WHERE subscriptionid = ?
809 AND (status =2 or status=4)
810 ORDER BY planneddate DESC LIMIT 0,$limit
812 my $sth = $dbh->prepare($strsth);
813 $sth->execute($subscriptionid);
814 my @serials;
815 while ( my $line = $sth->fetchrow_hashref ) {
816 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
817 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
818 push @serials, $line;
821 # my $query = qq|
822 # SELECT count(*)
823 # FROM serial
824 # WHERE subscriptionid=?
825 # |;
826 # $sth=$dbh->prepare($query);
827 # $sth->execute($subscriptionid);
828 # my ($totalissues) = $sth->fetchrow;
829 return \@serials;
832 =head2 GetDistributedTo
834 =over 4
836 $distributedto=GetDistributedTo($subscriptionid)
837 This function select the old previous value of distributedto in the database.
839 =back
841 =cut
843 sub GetDistributedTo {
844 my $dbh = C4::Context->dbh;
845 my $distributedto;
846 my $subscriptionid = @_;
847 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
848 my $sth = $dbh->prepare($query);
849 $sth->execute($subscriptionid);
850 return ($distributedto) = $sth->fetchrow;
853 =head2 GetNextSeq
855 =over 4
857 GetNextSeq($val)
858 $val is a hashref containing all the attributes of the table 'subscription'
859 This function get the next issue for the subscription given on input arg
860 return:
861 all the input params updated.
863 =back
865 =cut
867 # sub GetNextSeq {
868 # my ($val) =@_;
869 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
870 # $calculated = $val->{numberingmethod};
871 # # calculate the (expected) value of the next issue recieved.
872 # $newlastvalue1 = $val->{lastvalue1};
873 # # check if we have to increase the new value.
874 # $newinnerloop1 = $val->{innerloop1}+1;
875 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
876 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
877 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
878 # $calculated =~ s/\{X\}/$newlastvalue1/g;
880 # $newlastvalue2 = $val->{lastvalue2};
881 # # check if we have to increase the new value.
882 # $newinnerloop2 = $val->{innerloop2}+1;
883 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
884 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
885 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
886 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
888 # $newlastvalue3 = $val->{lastvalue3};
889 # # check if we have to increase the new value.
890 # $newinnerloop3 = $val->{innerloop3}+1;
891 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
892 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
893 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
894 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
895 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
898 sub GetNextSeq {
899 my ($val) = @_;
900 my ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
901 my $pattern = $val->{numberpattern};
902 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
903 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
904 $calculated = $val->{numberingmethod};
905 $newlastvalue1 = $val->{lastvalue1};
906 $newlastvalue2 = $val->{lastvalue2};
907 $newlastvalue3 = $val->{lastvalue3};
908 $newlastvalue1 = $val->{lastvalue1};
910 # check if we have to increase the new value.
911 $newinnerloop1 = $val->{innerloop1} + 1;
912 $newinnerloop1 = 0 if ( $newinnerloop1 >= $val->{every1} );
913 $newlastvalue1 += $val->{add1} if ( $newinnerloop1 < 1 ); # <1 to be true when 0 or empty.
914 $newlastvalue1 = $val->{setto1} if ( $newlastvalue1 > $val->{whenmorethan1} ); # reset counter if needed.
915 $calculated =~ s/\{X\}/$newlastvalue1/g;
917 $newlastvalue2 = $val->{lastvalue2};
919 # check if we have to increase the new value.
920 $newinnerloop2 = $val->{innerloop2} + 1;
921 $newinnerloop2 = 0 if ( $newinnerloop2 >= $val->{every2} );
922 $newlastvalue2 += $val->{add2} if ( $newinnerloop2 < 1 ); # <1 to be true when 0 or empty.
923 $newlastvalue2 = $val->{setto2} if ( $newlastvalue2 > $val->{whenmorethan2} ); # reset counter if needed.
924 if ( $pattern == 6 ) {
925 if ( $val->{hemisphere} == 2 ) {
926 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
927 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
928 } else {
929 my $newlastvalue2seq = $seasons[$newlastvalue2];
930 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
932 } else {
933 $calculated =~ s/\{Y\}/$newlastvalue2/g;
936 $newlastvalue3 = $val->{lastvalue3};
938 # check if we have to increase the new value.
939 $newinnerloop3 = $val->{innerloop3} + 1;
940 $newinnerloop3 = 0 if ( $newinnerloop3 >= $val->{every3} );
941 $newlastvalue3 += $val->{add3} if ( $newinnerloop3 < 1 ); # <1 to be true when 0 or empty.
942 $newlastvalue3 = $val->{setto3} if ( $newlastvalue3 > $val->{whenmorethan3} ); # reset counter if needed.
943 $calculated =~ s/\{Z\}/$newlastvalue3/g;
945 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
948 =head2 GetSeq
950 =over 4
952 $calculated = GetSeq($val)
953 $val is a hashref containing all the attributes of the table 'subscription'
954 this function transforms {X},{Y},{Z} to 150,0,0 for example.
955 return:
956 the sequence in integer format
958 =back
960 =cut
962 sub GetSeq {
963 my ($val) = @_;
964 my $pattern = $val->{numberpattern};
965 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
966 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
967 my $calculated = $val->{numberingmethod};
968 my $x = $val->{'lastvalue1'};
969 $calculated =~ s/\{X\}/$x/g;
970 my $newlastvalue2 = $val->{'lastvalue2'};
972 if ( $pattern == 6 ) {
973 if ( $val->{hemisphere} == 2 ) {
974 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
975 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
976 } else {
977 my $newlastvalue2seq = $seasons[$newlastvalue2];
978 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
980 } else {
981 $calculated =~ s/\{Y\}/$newlastvalue2/g;
983 my $z = $val->{'lastvalue3'};
984 $calculated =~ s/\{Z\}/$z/g;
985 return $calculated;
988 =head2 GetExpirationDate
990 $sensddate = GetExpirationDate($subscriptionid)
992 this function return the next expiration date for a subscription given on input args.
994 return
995 the enddate
997 =cut
999 sub GetExpirationDate {
1000 my ( $subscriptionid, $startdate ) = @_;
1001 my $dbh = C4::Context->dbh;
1002 my $subscription = GetSubscription($subscriptionid);
1003 my $enddate;
1005 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1006 $enddate = $startdate || $subscription->{startdate};
1007 my @date = split( /-/, $enddate );
1008 return if ( scalar(@date) != 3 || not check_date(@date) );
1009 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
1011 # If Not Irregular
1012 if ( my $length = $subscription->{numberlength} ) {
1014 #calculate the date of the last issue.
1015 for ( my $i = 1 ; $i <= $length ; $i++ ) {
1016 $enddate = GetNextDate( $enddate, $subscription );
1018 } elsif ( $subscription->{monthlength} ) {
1019 if ( $$subscription{startdate} ) {
1020 my @enddate = Add_Delta_YM( $date[0], $date[1], $date[2], 0, $subscription->{monthlength} );
1021 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1023 } elsif ( $subscription->{weeklength} ) {
1024 if ( $$subscription{startdate} ) {
1025 my @date = split( /-/, $subscription->{startdate} );
1026 my @enddate = Add_Delta_Days( $date[0], $date[1], $date[2], $subscription->{weeklength} * 7 );
1027 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1030 return $enddate;
1031 } else {
1032 return;
1036 =head2 CountSubscriptionFromBiblionumber
1038 =over 4
1040 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1041 this count the number of subscription for a biblionumber given.
1042 return :
1043 the number of subscriptions with biblionumber given on input arg.
1045 =back
1047 =cut
1049 sub CountSubscriptionFromBiblionumber {
1050 my ($biblionumber) = @_;
1051 my $dbh = C4::Context->dbh;
1052 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1053 my $sth = $dbh->prepare($query);
1054 $sth->execute($biblionumber);
1055 my $subscriptionsnumber = $sth->fetchrow;
1056 return $subscriptionsnumber;
1059 =head2 ModSubscriptionHistory
1061 =over 4
1063 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1065 this function modify the history of a subscription. Put your new values on input arg.
1067 =back
1069 =cut
1071 sub ModSubscriptionHistory {
1072 my ( $subscriptionid, $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote ) = @_;
1073 my $dbh = C4::Context->dbh;
1074 my $query = "UPDATE subscriptionhistory
1075 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1076 WHERE subscriptionid=?
1078 my $sth = $dbh->prepare($query);
1079 $recievedlist =~ s/^; //;
1080 $missinglist =~ s/^; //;
1081 $opacnote =~ s/^; //;
1082 $sth->execute( $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
1083 return $sth->rows;
1086 =head2 ModSerialStatus
1088 =over 4
1090 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1092 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1093 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1095 =back
1097 =cut
1099 sub ModSerialStatus {
1100 my ( $serialid, $serialseq, $planneddate, $publisheddate, $status, $notes ) = @_;
1102 #It is a usual serial
1103 # 1st, get previous status :
1104 my $dbh = C4::Context->dbh;
1105 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1106 my $sth = $dbh->prepare($query);
1107 $sth->execute($serialid);
1108 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1110 # change status & update subscriptionhistory
1111 my $val;
1112 if ( $status eq 6 ) {
1113 DelIssue( { 'serialid' => $serialid, 'subscriptionid' => $subscriptionid, 'serialseq' => $serialseq } );
1114 } else {
1115 my $query = "UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?";
1116 $sth = $dbh->prepare($query);
1117 $sth->execute( $serialseq, $publisheddate, $planneddate, $status, $notes, $serialid );
1118 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1119 $sth = $dbh->prepare($query);
1120 $sth->execute($subscriptionid);
1121 my $val = $sth->fetchrow_hashref;
1122 unless ( $val->{manualhistory} ) {
1123 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1124 $sth = $dbh->prepare($query);
1125 $sth->execute($subscriptionid);
1126 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1127 if ( $status eq 2 ) {
1129 $recievedlist .= "; $serialseq"
1130 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1133 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1134 $missinglist .= "; $serialseq"
1135 if ( $status eq 4
1136 and not index( "$missinglist", "$serialseq" ) >= 0 );
1137 $missinglist .= "; $serialseq"
1138 if ( $status eq 5
1139 and index( "$missinglist", "$serialseq" ) >= 0 );
1140 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1141 $sth = $dbh->prepare($query);
1142 $recievedlist =~ s/^; //;
1143 $missinglist =~ s/^; //;
1144 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1148 # create new waited entry if needed (ie : was a "waited" and has changed)
1149 if ( $oldstatus eq 1 && $status ne 1 ) {
1150 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1151 $sth = $dbh->prepare($query);
1152 $sth->execute($subscriptionid);
1153 my $val = $sth->fetchrow_hashref;
1155 # next issue number
1156 # warn "Next Seq";
1157 my ( $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 ) = GetNextSeq($val);
1159 # warn "Next Seq End";
1161 # next date (calculated from actual date & frequency parameters)
1162 # warn "publisheddate :$publisheddate ";
1163 my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1164 NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate );
1165 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1166 WHERE subscriptionid = ?";
1167 $sth = $dbh->prepare($query);
1168 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1170 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1171 if ( $val->{letter} && $status eq 2 && $oldstatus ne 2 ) {
1172 SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1177 =head2 GetNextExpected
1179 =over 4
1181 $nextexpected = GetNextExpected($subscriptionid)
1183 Get the planneddate for the current expected issue of the subscription.
1185 returns a hashref:
1187 $nextexepected = {
1188 serialid => int
1189 planneddate => C4::Dates object
1192 =back
1194 =cut
1196 sub GetNextExpected($) {
1197 my ($subscriptionid) = @_;
1198 my $dbh = C4::Context->dbh;
1199 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1201 # Each subscription has only one 'expected' issue, with serial.status==1.
1202 $sth->execute( $subscriptionid, 1 );
1203 my ($nextissue) = $sth->fetchrow_hashref;
1204 if ( not $nextissue ) {
1205 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1206 $sth->execute($subscriptionid);
1207 $nextissue = $sth->fetchrow_hashref;
1209 $nextissue->{planneddate} = C4::Dates->new( $nextissue->{planneddate}, 'iso' );
1210 return $nextissue;
1214 =head2 ModNextExpected
1216 =over 4
1218 ModNextExpected($subscriptionid,$date)
1220 Update the planneddate for the current expected issue of the subscription.
1221 This will modify all future prediction results.
1223 C<$date> is a C4::Dates object.
1225 =back
1227 =cut
1229 sub ModNextExpected($$) {
1230 my ( $subscriptionid, $date ) = @_;
1231 my $dbh = C4::Context->dbh;
1233 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1234 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1236 # Each subscription has only one 'expected' issue, with serial.status==1.
1237 $sth->execute( $date->output('iso'), $date->output('iso'), $subscriptionid, 1 );
1238 return 0;
1242 =head2 ModSubscription
1244 =over 4
1246 this function modify a subscription. Put all new values on input args.
1248 =back
1250 =cut
1252 sub ModSubscription {
1253 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1254 $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $add1, $every1,
1255 $whenmorethan1, $setto1, $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2,
1256 $lastvalue2, $innerloop2, $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1257 $numberingmethod, $status, $biblionumber, $callnumber, $notes, $letter, $hemisphere, $manualhistory,
1258 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid
1259 ) = @_;
1261 # warn $irregularity;
1262 my $dbh = C4::Context->dbh;
1263 my $query = "UPDATE subscription
1264 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1265 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1266 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1267 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1268 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1269 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?,
1270 letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,
1271 staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1272 ,enddate=?
1273 WHERE subscriptionid = ?";
1275 #warn "query :".$query;
1276 my $sth = $dbh->prepare($query);
1277 $sth->execute(
1278 $auser, $branchcode, $aqbooksellerid, $cost,
1279 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1280 $dow, "$irregularity", $numberpattern, $numberlength,
1281 $weeklength, $monthlength, $add1, $every1,
1282 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1283 $add2, $every2, $whenmorethan2, $setto2,
1284 $lastvalue2, $innerloop2, $add3, $every3,
1285 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1286 $numberingmethod, $status, $biblionumber, $callnumber,
1287 $notes, $letter, $hemisphere, ( $manualhistory ? $manualhistory : 0 ),
1288 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1289 $graceperiod, $location, $enddate, $subscriptionid
1291 my $rows = $sth->rows;
1292 $sth->finish;
1294 logaction( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1295 return $rows;
1298 =head2 NewSubscription
1300 =over 4
1302 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1303 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1304 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1305 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1306 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1307 $numberingmethod, $status, $notes, $serialsadditems,
1308 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate);
1310 Create a new subscription with value given on input args.
1312 return :
1313 the id of this new subscription
1315 =back
1317 =cut
1319 sub NewSubscription {
1320 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1321 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1322 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1323 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, $status,
1324 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1325 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1326 ) = @_;
1327 my $dbh = C4::Context->dbh;
1329 #save subscription (insert into database)
1330 my $query = qq|
1331 INSERT INTO subscription
1332 (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1333 startdate,periodicity,dow,numberlength,weeklength,monthlength,
1334 add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1335 add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1336 add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1337 numberingmethod, status, notes, letter,firstacquidate,irregularity,
1338 numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
1339 staffdisplaycount,opacdisplaycount,graceperiod,location,enddate)
1340 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1342 my $sth = $dbh->prepare($query);
1343 $sth->execute(
1344 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1345 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1346 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1347 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, "$status",
1348 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1349 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1352 #then create the 1st waited number
1353 my $subscriptionid = $dbh->{'mysql_insertid'};
1354 $query = qq(
1355 INSERT INTO subscriptionhistory
1356 (biblionumber, subscriptionid, histstartdate, opacnote, librariannote)
1357 VALUES (?,?,?,?,?)
1359 $sth = $dbh->prepare($query);
1360 $sth->execute( $biblionumber, $subscriptionid, $startdate, $notes, $internalnotes );
1362 # reread subscription to get a hash (for calculation of the 1st issue number)
1363 $query = qq(
1364 SELECT *
1365 FROM subscription
1366 WHERE subscriptionid = ?
1368 $sth = $dbh->prepare($query);
1369 $sth->execute($subscriptionid);
1370 my $val = $sth->fetchrow_hashref;
1372 # calculate issue number
1373 my $serialseq = GetSeq($val);
1374 $query = qq|
1375 INSERT INTO serial
1376 (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1377 VALUES (?,?,?,?,?,?)
1379 $sth = $dbh->prepare($query);
1380 $sth->execute( "$serialseq", $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
1382 logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1384 #set serial flag on biblio if not already set.
1385 my ( $null, ($bib) ) = GetBiblio($biblionumber);
1386 if ( !$bib->{'serial'} ) {
1387 my $record = GetMarcBiblio($biblionumber);
1388 my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $bib->{'frameworkcode'} );
1389 if ($tag) {
1390 eval { $record->field($tag)->update( $subf => 1 ); };
1392 ModBiblio( $record, $biblionumber, $bib->{'frameworkcode'} );
1394 return $subscriptionid;
1397 =head2 ReNewSubscription
1399 =over 4
1401 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1403 this function renew a subscription with values given on input args.
1405 =back
1407 =cut
1409 sub ReNewSubscription {
1410 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1411 my $dbh = C4::Context->dbh;
1412 my $subscription = GetSubscription($subscriptionid);
1413 my $query = qq|
1414 SELECT *
1415 FROM biblio
1416 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1417 WHERE biblio.biblionumber=?
1419 my $sth = $dbh->prepare($query);
1420 $sth->execute( $subscription->{biblionumber} );
1421 my $biblio = $sth->fetchrow_hashref;
1423 if ( C4::Context->preference("RenewSerialAddsSuggestion") ) {
1425 NewSuggestion(
1426 { 'suggestedby' => $user,
1427 'title' => $subscription->{bibliotitle},
1428 'author' => $biblio->{author},
1429 'publishercode' => $biblio->{publishercode},
1430 'note' => $biblio->{note},
1431 'biblionumber' => $subscription->{biblionumber}
1436 # renew subscription
1437 $query = qq|
1438 UPDATE subscription
1439 SET startdate=?,numberlength=?,weeklength=?,monthlength=?
1440 WHERE subscriptionid=?
1442 $sth = $dbh->prepare($query);
1443 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1444 my $enddate = GetExpirationDate($subscriptionid);
1445 $debug && warn "enddate :$enddate";
1446 $query = qq|
1447 UPDATE subscription
1448 SET enddate=?
1449 WHERE subscriptionid=?
1451 $sth = $dbh->prepare($query);
1452 $sth->execute( $enddate, $subscriptionid );
1453 $query = qq|
1454 UPDATE subscriptionhistory
1455 SET histenddate=?
1456 WHERE subscriptionid=?
1458 $sth = $dbh->prepare($query);
1459 $sth->execute( $enddate, $subscriptionid );
1461 logaction( "SERIAL", "RENEW", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1464 =head2 NewIssue
1466 =over 4
1468 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1470 Create a new issue stored on the database.
1471 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1473 =back
1475 =cut
1477 sub NewIssue {
1478 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
1479 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1481 my $dbh = C4::Context->dbh;
1482 my $query = qq|
1483 INSERT INTO serial
1484 (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1485 VALUES (?,?,?,?,?,?,?)
1487 my $sth = $dbh->prepare($query);
1488 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
1489 my $serialid = $dbh->{'mysql_insertid'};
1490 $query = qq|
1491 SELECT missinglist,recievedlist
1492 FROM subscriptionhistory
1493 WHERE subscriptionid=?
1495 $sth = $dbh->prepare($query);
1496 $sth->execute($subscriptionid);
1497 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1499 if ( $status eq 2 ) {
1500 ### TODO Add a feature that improves recognition and description.
1501 ### As such count (serialseq) i.e. : N18,2(N19),N20
1502 ### Would use substr and index But be careful to previous presence of ()
1503 $recievedlist .= "; $serialseq" unless ( index( $recievedlist, $serialseq ) > 0 );
1505 if ( $status eq 4 ) {
1506 $missinglist .= "; $serialseq" unless ( index( $missinglist, $serialseq ) > 0 );
1508 $query = qq|
1509 UPDATE subscriptionhistory
1510 SET recievedlist=?, missinglist=?
1511 WHERE subscriptionid=?
1513 $sth = $dbh->prepare($query);
1514 $recievedlist =~ s/^; //;
1515 $missinglist =~ s/^; //;
1516 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1517 return $serialid;
1520 =head2 ItemizeSerials
1522 =over 4
1524 ItemizeSerials($serialid, $info);
1525 $info is a hashref containing barcode branch, itemcallnumber, status, location
1526 $serialid the serialid
1527 return :
1528 1 if the itemize is a succes.
1529 0 and @error else. @error containts the list of errors found.
1531 =back
1533 =cut
1535 sub ItemizeSerials {
1536 my ( $serialid, $info ) = @_;
1537 my $now = POSIX::strftime( "%Y-%m-%d", localtime );
1539 my $dbh = C4::Context->dbh;
1540 my $query = qq|
1541 SELECT *
1542 FROM serial
1543 WHERE serialid=?
1545 my $sth = $dbh->prepare($query);
1546 $sth->execute($serialid);
1547 my $data = $sth->fetchrow_hashref;
1548 if ( C4::Context->preference("RoutingSerials") ) {
1550 # check for existing biblioitem relating to serial issue
1551 my ( $count, @results ) = GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1552 my $bibitemno = 0;
1553 for ( my $i = 0 ; $i < $count ; $i++ ) {
1554 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' (' . $data->{'planneddate'} . ')' ) {
1555 $bibitemno = $results[$i]->{'biblioitemnumber'};
1556 last;
1559 if ( $bibitemno == 0 ) {
1560 my $sth = $dbh->prepare( "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC" );
1561 $sth->execute( $data->{'biblionumber'} );
1562 my $biblioitem = $sth->fetchrow_hashref;
1563 $biblioitem->{'volumedate'} = $data->{planneddate};
1564 $biblioitem->{'volumeddesc'} = $data->{serialseq} . ' (' . format_date( $data->{'planneddate'} ) . ')';
1565 $biblioitem->{'dewey'} = $info->{itemcallnumber};
1569 my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1570 if ( $info->{barcode} ) {
1571 my @errors;
1572 my $exists = itemdata( $info->{'barcode'} );
1573 push @errors, "barcode_not_unique" if ($exists);
1574 unless ($exists) {
1575 my $marcrecord = MARC::Record->new();
1576 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.barcode", $fwk );
1577 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{barcode} );
1578 $marcrecord->insert_fields_ordered($newField);
1579 if ( $info->{branch} ) {
1580 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.homebranch", $fwk );
1582 #warn "items.homebranch : $tag , $subfield";
1583 if ( $marcrecord->field($tag) ) {
1584 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1585 } else {
1586 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1587 $marcrecord->insert_fields_ordered($newField);
1589 ( $tag, $subfield ) = GetMarcFromKohaField( "items.holdingbranch", $fwk );
1591 #warn "items.holdingbranch : $tag , $subfield";
1592 if ( $marcrecord->field($tag) ) {
1593 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1594 } else {
1595 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1596 $marcrecord->insert_fields_ordered($newField);
1599 if ( $info->{itemcallnumber} ) {
1600 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemcallnumber", $fwk );
1602 if ( $marcrecord->field($tag) ) {
1603 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{itemcallnumber} );
1604 } else {
1605 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{itemcallnumber} );
1606 $marcrecord->insert_fields_ordered($newField);
1609 if ( $info->{notes} ) {
1610 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemnotes", $fwk );
1612 if ( $marcrecord->field($tag) ) {
1613 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{notes} );
1614 } else {
1615 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{notes} );
1616 $marcrecord->insert_fields_ordered($newField);
1619 if ( $info->{location} ) {
1620 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.location", $fwk );
1622 if ( $marcrecord->field($tag) ) {
1623 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{location} );
1624 } else {
1625 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{location} );
1626 $marcrecord->insert_fields_ordered($newField);
1629 if ( $info->{status} ) {
1630 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.notforloan", $fwk );
1632 if ( $marcrecord->field($tag) ) {
1633 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{status} );
1634 } else {
1635 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{status} );
1636 $marcrecord->insert_fields_ordered($newField);
1639 if ( C4::Context->preference("RoutingSerials") ) {
1640 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.dateaccessioned", $fwk );
1641 if ( $marcrecord->field($tag) ) {
1642 $marcrecord->field($tag)->add_subfields( "$subfield" => $now );
1643 } else {
1644 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1645 $marcrecord->insert_fields_ordered($newField);
1648 AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1649 return 1;
1651 return ( 0, @errors );
1655 =head2 HasSubscriptionStrictlyExpired
1657 =over 4
1659 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1661 the subscription has stricly expired when today > the end subscription date
1663 return :
1664 1 if true, 0 if false, -1 if the expiration date is not set.
1666 =back
1668 =cut
1670 sub HasSubscriptionStrictlyExpired {
1672 # Getting end of subscription date
1673 my ($subscriptionid) = @_;
1674 my $dbh = C4::Context->dbh;
1675 my $subscription = GetSubscription($subscriptionid);
1676 my $expirationdate = $subscription->{enddate} || GetExpirationDate($subscriptionid);
1678 # If the expiration date is set
1679 if ( $expirationdate != 0 ) {
1680 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1682 # Getting today's date
1683 my ( $nowyear, $nowmonth, $nowday ) = Today();
1685 # if today's date > expiration date, then the subscription has stricly expired
1686 if ( Delta_Days( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1687 return 1;
1688 } else {
1689 return 0;
1691 } else {
1693 # There are some cases where the expiration date is not set
1694 # As we can't determine if the subscription has expired on a date-basis,
1695 # we return -1;
1696 return -1;
1700 =head2 HasSubscriptionExpired
1702 =over 4
1704 $has_expired = HasSubscriptionExpired($subscriptionid)
1706 the subscription has expired when the next issue to arrive is out of subscription limit.
1708 return :
1709 0 if the subscription has not expired
1710 1 if the subscription has expired
1711 2 if has subscription does not have a valid expiration date set
1713 =back
1715 =cut
1717 sub HasSubscriptionExpired {
1718 my ($subscriptionid) = @_;
1719 my $dbh = C4::Context->dbh;
1720 my $subscription = GetSubscription($subscriptionid);
1721 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
1722 my $expirationdate = $subscription->{enddate};
1723 my $query = qq|
1724 SELECT max(planneddate)
1725 FROM serial
1726 WHERE subscriptionid=?
1728 my $sth = $dbh->prepare($query);
1729 $sth->execute($subscriptionid);
1730 my ($res) = $sth->fetchrow;
1731 return 0 unless $res;
1732 my @res = split( /-/, $res );
1733 my @endofsubscriptiondate = split( /-/, $expirationdate );
1734 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date(@res) || not check_date(@endofsubscriptiondate) );
1735 return 1
1736 if ( ( @endofsubscriptiondate && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1737 || ( !$res ) );
1738 return 0;
1739 } else {
1740 if ( $subscription->{'numberlength'} ) {
1741 my $countreceived = countissuesfrom( $subscriptionid, $subscription->{'startdate'} );
1742 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1743 return 0;
1744 } else {
1745 return 0;
1748 return 0; # Notice that you'll never get here.
1751 =head2 SetDistributedto
1753 =over 4
1755 SetDistributedto($distributedto,$subscriptionid);
1756 This function update the value of distributedto for a subscription given on input arg.
1758 =back
1760 =cut
1762 sub SetDistributedto {
1763 my ( $distributedto, $subscriptionid ) = @_;
1764 my $dbh = C4::Context->dbh;
1765 my $query = qq|
1766 UPDATE subscription
1767 SET distributedto=?
1768 WHERE subscriptionid=?
1770 my $sth = $dbh->prepare($query);
1771 $sth->execute( $distributedto, $subscriptionid );
1774 =head2 DelSubscription
1776 =over 4
1778 DelSubscription($subscriptionid)
1779 this function delete the subscription which has $subscriptionid as id.
1781 =back
1783 =cut
1785 sub DelSubscription {
1786 my ($subscriptionid) = @_;
1787 my $dbh = C4::Context->dbh;
1788 $subscriptionid = $dbh->quote($subscriptionid);
1789 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1790 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1791 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1793 logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1796 =head2 DelIssue
1798 =over 4
1800 DelIssue($serialseq,$subscriptionid)
1801 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1803 =back
1805 =cut
1807 sub DelIssue {
1808 my ($dataissue) = @_;
1809 my $dbh = C4::Context->dbh;
1810 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1812 my $query = qq|
1813 DELETE FROM serial
1814 WHERE serialid= ?
1815 AND subscriptionid= ?
1817 my $mainsth = $dbh->prepare($query);
1818 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1820 #Delete element from subscription history
1821 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1822 my $sth = $dbh->prepare($query);
1823 $sth->execute( $dataissue->{'subscriptionid'} );
1824 my $val = $sth->fetchrow_hashref;
1825 unless ( $val->{manualhistory} ) {
1826 my $query = qq|
1827 SELECT * FROM subscriptionhistory
1828 WHERE subscriptionid= ?
1830 my $sth = $dbh->prepare($query);
1831 $sth->execute( $dataissue->{'subscriptionid'} );
1832 my $data = $sth->fetchrow_hashref;
1833 my $serialseq = $dataissue->{'serialseq'};
1834 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1835 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1836 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1837 $sth = $dbh->prepare($strsth);
1838 $sth->execute( $dataissue->{'subscriptionid'} );
1841 return $mainsth->rows;
1844 =head2 GetLateOrMissingIssues
1846 =over 4
1848 ($count,@issuelist) = &GetLateMissingIssues($supplierid,$serialid)
1850 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1852 return :
1853 a count of the number of missing issues
1854 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1855 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1857 =back
1859 =cut
1861 sub GetLateOrMissingIssues {
1862 my ( $supplierid, $serialid, $order ) = @_;
1863 my $dbh = C4::Context->dbh;
1864 my $sth;
1865 my $byserial = '';
1866 if ($serialid) {
1867 $byserial = "and serialid = " . $serialid;
1869 if ($order) {
1870 $order .= ", title";
1871 } else {
1872 $order = "title";
1874 if ($supplierid) {
1875 $sth = $dbh->prepare(
1876 "SELECT
1877 serialid, aqbooksellerid, name,
1878 biblio.title, planneddate, serialseq,
1879 serial.status, serial.subscriptionid, claimdate
1880 FROM serial
1881 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1882 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1883 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1884 WHERE subscription.subscriptionid = serial.subscriptionid
1885 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1886 AND subscription.aqbooksellerid=$supplierid
1887 $byserial
1888 ORDER BY $order"
1890 } else {
1891 $sth = $dbh->prepare(
1892 "SELECT
1893 serialid, aqbooksellerid, name,
1894 biblio.title, planneddate, serialseq,
1895 serial.status, serial.subscriptionid, claimdate
1896 FROM serial
1897 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1898 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1899 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1900 WHERE subscription.subscriptionid = serial.subscriptionid
1901 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1902 $byserial
1903 ORDER BY $order"
1906 $sth->execute;
1907 my @issuelist;
1908 my $last_title;
1909 my $odd = 0;
1910 my $count = 0;
1911 while ( my $line = $sth->fetchrow_hashref ) {
1912 $odd++ unless $line->{title} eq $last_title;
1913 $last_title = $line->{title} if ( $line->{title} );
1914 $line->{planneddate} = format_date( $line->{planneddate} );
1915 $line->{claimdate} = format_date( $line->{claimdate} );
1916 $line->{ "status" . $line->{status} } = 1;
1917 $line->{'odd'} = 1 if $odd % 2;
1918 $count++;
1919 push @issuelist, $line;
1921 return $count, @issuelist;
1924 =head2 removeMissingIssue
1926 =over 4
1928 removeMissingIssue($subscriptionid)
1930 this function removes an issue from being part of the missing string in
1931 subscriptionlist.missinglist column
1933 called when a missing issue is found from the serials-recieve.pl file
1935 =back
1937 =cut
1939 sub removeMissingIssue {
1940 my ( $sequence, $subscriptionid ) = @_;
1941 my $dbh = C4::Context->dbh;
1942 my $sth = $dbh->prepare("SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1943 $sth->execute($subscriptionid);
1944 my $data = $sth->fetchrow_hashref;
1945 my $missinglist = $data->{'missinglist'};
1946 my $missinglistbefore = $missinglist;
1948 # warn $missinglist." before";
1949 $missinglist =~ s/($sequence)//;
1951 # warn $missinglist." after";
1952 if ( $missinglist ne $missinglistbefore ) {
1953 $missinglist =~ s/\|\s\|/\|/g;
1954 $missinglist =~ s/^\| //g;
1955 $missinglist =~ s/\|$//g;
1956 my $sth2 = $dbh->prepare(
1957 "UPDATE subscriptionhistory
1958 SET missinglist = ?
1959 WHERE subscriptionid = ?"
1961 $sth2->execute( $missinglist, $subscriptionid );
1965 =head2 updateClaim
1967 =over 4
1969 &updateClaim($serialid)
1971 this function updates the time when a claim is issued for late/missing items
1973 called from claims.pl file
1975 =back
1977 =cut
1979 sub updateClaim {
1980 my ($serialid) = @_;
1981 my $dbh = C4::Context->dbh;
1982 my $sth = $dbh->prepare(
1983 "UPDATE serial SET claimdate = now()
1984 WHERE serialid = ?
1987 $sth->execute($serialid);
1990 =head2 getsupplierbyserialid
1992 =over 4
1994 ($result) = &getsupplierbyserialid($serialid)
1996 this function is used to find the supplier id given a serial id
1998 return :
1999 hashref containing serialid, subscriptionid, and aqbooksellerid
2001 =back
2003 =cut
2005 sub getsupplierbyserialid {
2006 my ($serialid) = @_;
2007 my $dbh = C4::Context->dbh;
2008 my $sth = $dbh->prepare(
2009 "SELECT serialid, serial.subscriptionid, aqbooksellerid
2010 FROM serial
2011 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
2012 WHERE serialid = ?
2015 $sth->execute($serialid);
2016 my $line = $sth->fetchrow_hashref;
2017 my $result = $line->{'aqbooksellerid'};
2018 return $result;
2021 =head2 check_routing
2023 =over 4
2025 ($result) = &check_routing($subscriptionid)
2027 this function checks to see if a serial has a routing list and returns the count of routingid
2028 used to show either an 'add' or 'edit' link
2030 =back
2032 =cut
2034 sub check_routing {
2035 my ($subscriptionid) = @_;
2036 my $dbh = C4::Context->dbh;
2037 my $sth = $dbh->prepare(
2038 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2039 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2040 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2043 $sth->execute($subscriptionid);
2044 my $line = $sth->fetchrow_hashref;
2045 my $result = $line->{'routingids'};
2046 return $result;
2049 =head2 addroutingmember
2051 =over 4
2053 &addroutingmember($borrowernumber,$subscriptionid)
2055 this function takes a borrowernumber and subscriptionid and add the member to the
2056 routing list for that serial subscription and gives them a rank on the list
2057 of either 1 or highest current rank + 1
2059 =back
2061 =cut
2063 sub addroutingmember {
2064 my ( $borrowernumber, $subscriptionid ) = @_;
2065 my $rank;
2066 my $dbh = C4::Context->dbh;
2067 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
2068 $sth->execute($subscriptionid);
2069 while ( my $line = $sth->fetchrow_hashref ) {
2070 if ( $line->{'rank'} > 0 ) {
2071 $rank = $line->{'rank'} + 1;
2072 } else {
2073 $rank = 1;
2076 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
2077 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2080 =head2 reorder_members
2082 =over 4
2084 &reorder_members($subscriptionid,$routingid,$rank)
2086 this function is used to reorder the routing list
2088 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2089 - it gets all members on list puts their routingid's into an array
2090 - removes the one in the array that is $routingid
2091 - then reinjects $routingid at point indicated by $rank
2092 - then update the database with the routingids in the new order
2094 =back
2096 =cut
2098 sub reorder_members {
2099 my ( $subscriptionid, $routingid, $rank ) = @_;
2100 my $dbh = C4::Context->dbh;
2101 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
2102 $sth->execute($subscriptionid);
2103 my @result;
2104 while ( my $line = $sth->fetchrow_hashref ) {
2105 push( @result, $line->{'routingid'} );
2108 # To find the matching index
2109 my $i;
2110 my $key = -1; # to allow for 0 being a valid response
2111 for ( $i = 0 ; $i < @result ; $i++ ) {
2112 if ( $routingid == $result[$i] ) {
2113 $key = $i; # save the index
2114 last;
2118 # if index exists in array then move it to new position
2119 if ( $key > -1 && $rank > 0 ) {
2120 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
2121 my $moving_item = splice( @result, $key, 1 );
2122 splice( @result, $new_rank, 0, $moving_item );
2124 for ( my $j = 0 ; $j < @result ; $j++ ) {
2125 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
2126 $sth->execute;
2130 =head2 delroutingmember
2132 =over 4
2134 &delroutingmember($routingid,$subscriptionid)
2136 this function either deletes one member from routing list if $routingid exists otherwise
2137 deletes all members from the routing list
2139 =back
2141 =cut
2143 sub delroutingmember {
2145 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2146 my ( $routingid, $subscriptionid ) = @_;
2147 my $dbh = C4::Context->dbh;
2148 if ($routingid) {
2149 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2150 $sth->execute($routingid);
2151 reorder_members( $subscriptionid, $routingid );
2152 } else {
2153 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2154 $sth->execute($subscriptionid);
2158 =head2 getroutinglist
2160 =over 4
2162 ($count,@routinglist) = &getroutinglist($subscriptionid)
2164 this gets the info from the subscriptionroutinglist for $subscriptionid
2166 return :
2167 a count of the number of members on routinglist
2168 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2169 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2171 =back
2173 =cut
2175 sub getroutinglist {
2176 my ($subscriptionid) = @_;
2177 my $dbh = C4::Context->dbh;
2178 my $sth = $dbh->prepare(
2179 "SELECT routingid, borrowernumber, ranking, biblionumber
2180 FROM subscription
2181 LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2182 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2185 $sth->execute($subscriptionid);
2186 my @routinglist;
2187 my $count = 0;
2188 while ( my $line = $sth->fetchrow_hashref ) {
2189 $count++;
2190 push( @routinglist, $line );
2192 return ( $count, @routinglist );
2195 =head2 countissuesfrom
2197 =over 4
2199 $result = &countissuesfrom($subscriptionid,$startdate)
2202 =back
2204 =cut
2206 sub countissuesfrom {
2207 my ( $subscriptionid, $startdate ) = @_;
2208 my $dbh = C4::Context->dbh;
2209 my $query = qq|
2210 SELECT count(*)
2211 FROM serial
2212 WHERE subscriptionid=?
2213 AND serial.publisheddate>?
2215 my $sth = $dbh->prepare($query);
2216 $sth->execute( $subscriptionid, $startdate );
2217 my ($countreceived) = $sth->fetchrow;
2218 return $countreceived;
2221 =head2 CountIssues
2223 =over 4
2225 $result = &CountIssues($subscriptionid)
2228 =back
2230 =cut
2232 sub CountIssues {
2233 my ($subscriptionid) = @_;
2234 my $dbh = C4::Context->dbh;
2235 my $query = qq|
2236 SELECT count(*)
2237 FROM serial
2238 WHERE subscriptionid=?
2240 my $sth = $dbh->prepare($query);
2241 $sth->execute($subscriptionid);
2242 my ($countreceived) = $sth->fetchrow;
2243 return $countreceived;
2246 =head2 HasItems
2248 =over 4
2250 $result = &HasItems($subscriptionid)
2253 =back
2255 =cut
2257 sub HasItems {
2258 my ($subscriptionid) = @_;
2259 my $dbh = C4::Context->dbh;
2260 my $query = qq|
2261 SELECT COUNT(serialitems.itemnumber)
2262 FROM serial
2263 LEFT JOIN serialitems USING(serialid)
2264 WHERE subscriptionid=? AND serialitems.serialid NOT NULL
2266 my $sth=$dbh->prepare($query);
2267 $sth->execute($subscriptionid);
2268 my ($countitems)=$sth->fetchrow;
2269 return $countitems;
2272 =head2 abouttoexpire
2274 =over 4
2276 $result = &abouttoexpire($subscriptionid)
2278 this function alerts you to the penultimate issue for a serial subscription
2280 returns 1 - if this is the penultimate issue
2281 returns 0 - if not
2283 =back
2285 =cut
2287 sub abouttoexpire {
2288 my ($subscriptionid) = @_;
2289 my $dbh = C4::Context->dbh;
2290 my $subscription = GetSubscription($subscriptionid);
2291 my $per = $subscription->{'periodicity'};
2292 if ( $per % 16 > 0 ) {
2293 my $expirationdate = $subscription->{enddate};
2294 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
2295 $sth->execute($subscriptionid);
2296 my ($res) = $sth->fetchrow;
2297 my @res = split( /-/, $res );
2298 @res = Date::Calc::Today if ( $res[0] * $res[1] == 0 );
2299 my @endofsubscriptiondate = split( /-/, $expirationdate );
2300 my $x;
2301 if ( $per == 1 ) { $x = 7; }
2302 if ( $per == 2 ) { $x = 7; }
2303 if ( $per == 3 ) { $x = 14; }
2304 if ( $per == 4 ) { $x = 21; }
2305 if ( $per == 5 ) { $x = 31; }
2306 if ( $per == 6 ) { $x = 62; }
2307 if ( $per == 7 || $per == 8 ) { $x = 93; }
2308 if ( $per == 9 ) { $x = 190; }
2309 if ( $per == 10 ) { $x = 365; }
2310 if ( $per == 11 ) { $x = 730; }
2311 my @datebeforeend = Add_Delta_Days( $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2], -( 3 * $x ) )
2312 if ( @endofsubscriptiondate && $endofsubscriptiondate[0] * $endofsubscriptiondate[1] * $endofsubscriptiondate[2] );
2314 # warn "DATE BEFORE END: $datebeforeend";
2315 return 1
2316 if (
2317 @res
2318 && ( @datebeforeend
2319 && Delta_Days( $res[0], $res[1], $res[2], $datebeforeend[0], $datebeforeend[1], $datebeforeend[2] ) <= 0 )
2320 && ( @endofsubscriptiondate
2321 && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) >= 0 )
2323 return 0;
2324 } elsif ( $subscription->{numberlength} > 0 ) {
2325 return ( countissuesfrom( $subscriptionid, $subscription->{'startdate'} ) >= $subscription->{numberlength} - 1 );
2326 } else {
2327 return 0;
2331 =head2 GetNextDate
2333 ($resultdate) = &GetNextDate($planneddate,$subscription)
2335 this function is an extension of GetNextDate which allows for checking for irregularity
2337 it takes the planneddate and will return the next issue's date and will skip dates if there
2338 exists an irregularity
2339 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2340 skipped then the returned date will be 2007-05-10
2342 return :
2343 $resultdate - then next date in the sequence
2345 Return 0 if periodicity==0
2347 =cut
2349 sub in_array { # used in next sub down
2350 my ( $val, @elements ) = @_;
2351 foreach my $elem (@elements) {
2352 if ( $val == $elem ) {
2353 return 1;
2356 return 0;
2359 sub GetNextDate(@) {
2360 my ( $planneddate, $subscription ) = @_;
2361 my @irreg = split( /\,/, $subscription->{irregularity} );
2363 #date supposed to be in ISO.
2365 my ( $year, $month, $day ) = split( /-/, $planneddate );
2366 $month = 1 unless ($month);
2367 $day = 1 unless ($day);
2368 my @resultdate;
2370 # warn "DOW $dayofweek";
2371 if ( $subscription->{periodicity} % 16 == 0 ) { # 'without regularity' || 'irregular'
2372 return 0;
2375 # daily : n / week
2376 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2377 # renaming this pattern from 1/day to " n / week ".
2378 if ( $subscription->{periodicity} == 1 ) {
2379 my $dayofweek = eval { Day_of_Week( $year, $month, $day ) };
2380 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2381 else {
2382 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2383 $dayofweek = 0 if ( $dayofweek == 7 );
2384 if ( in_array( ( $dayofweek + 1 ), @irreg ) ) {
2385 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 1 );
2386 $dayofweek++;
2389 @resultdate = Add_Delta_Days( $year, $month, $day, 1 );
2393 # 1 week
2394 if ( $subscription->{periodicity} == 2 ) {
2395 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2396 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2397 else {
2398 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2400 #FIXME: if two consecutive irreg, do we only skip one?
2401 if ( $irreg[$i] == ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 ) ) {
2402 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 7 );
2403 $wkno = ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 );
2406 @resultdate = Add_Delta_Days( $year, $month, $day, 7 );
2410 # 1 / 2 weeks
2411 if ( $subscription->{periodicity} == 3 ) {
2412 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2413 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2414 else {
2415 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2416 if ( $irreg[$i] == ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 ) ) {
2417 ### BUGFIX was previously +1 ^
2418 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 14 );
2419 $wkno = ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 );
2422 @resultdate = Add_Delta_Days( $year, $month, $day, 14 );
2426 # 1 / 3 weeks
2427 if ( $subscription->{periodicity} == 4 ) {
2428 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2429 if ($@) { warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@"; }
2430 else {
2431 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2432 if ( $irreg[$i] == ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 ) ) {
2433 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 21 );
2434 $wkno = ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 );
2437 @resultdate = Add_Delta_Days( $year, $month, $day, 21 );
2440 my $tmpmonth = $month;
2441 if ( $year && $month && $day ) {
2442 if ( $subscription->{periodicity} == 5 ) {
2443 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2444 if ( $irreg[$i] == ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 ) ) {
2445 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2446 $tmpmonth = ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 );
2449 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2451 if ( $subscription->{periodicity} == 6 ) {
2452 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2453 if ( $irreg[$i] == ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 ) ) {
2454 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2455 $tmpmonth = ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 );
2458 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2460 if ( $subscription->{periodicity} == 7 ) {
2461 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2462 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2463 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2464 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2467 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2469 if ( $subscription->{periodicity} == 8 ) {
2470 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2471 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2472 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2473 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2476 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2478 if ( $subscription->{periodicity} == 9 ) {
2479 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2480 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2481 ### BUFIX Seems to need more Than One ?
2482 ( $year, $month, $day ) = Add_Delta_YM( $year, $month, $day, 0, 6 );
2483 $tmpmonth = ( ( $tmpmonth != 6 ) ? ( $tmpmonth + 6 ) % 12 : 12 );
2486 @resultdate = Add_Delta_YM( $year, $month, $day, 0, 6 );
2488 if ( $subscription->{periodicity} == 10 ) {
2489 @resultdate = Add_Delta_YM( $year, $month, $day, 1, 0 );
2491 if ( $subscription->{periodicity} == 11 ) {
2492 @resultdate = Add_Delta_YM( $year, $month, $day, 2, 0 );
2495 my $resultdate = sprintf( "%04d-%02d-%02d", $resultdate[0], $resultdate[1], $resultdate[2] );
2497 return "$resultdate";
2500 =head2 itemdata
2502 $item = &itemdata($barcode);
2504 Looks up the item with the given barcode, and returns a
2505 reference-to-hash containing information about that item. The keys of
2506 the hash are the fields from the C<items> and C<biblioitems> tables in
2507 the Koha database.
2509 =cut
2512 sub itemdata {
2513 my ($barcode) = @_;
2514 my $dbh = C4::Context->dbh;
2515 my $sth = $dbh->prepare(
2516 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2517 WHERE barcode=?"
2519 $sth->execute($barcode);
2520 my $data = $sth->fetchrow_hashref;
2521 $sth->finish;
2522 return ($data);
2526 __END__
2528 =head1 AUTHOR
2530 Koha Developement team <info@koha.org>
2532 =cut