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
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
21 use C4::Dates qw(format_date);
22 use Date::Calc qw(:all);
23 use POSIX qw(strftime);
30 use C4::Log; # logaction
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 $VERSION = 3.01; # set version for version checking
40 &NewSubscription &ModSubscription &DelSubscription &GetSubscriptions
41 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
42 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
43 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
45 &GetNextSeq &NewIssue &ItemizeSerials &GetSerials
46 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
47 &ReNewSubscription &GetLateIssues &GetLateOrMissingIssues
48 &GetSerialInformation &AddItem2Serial
49 &PrepareSerialsData &GetNextExpected &ModNextExpected
51 &UpdateClaimdateIssues
52 &GetSuppliersWithLateIssues &getsupplierbyserialid
53 &GetDistributedTo &SetDistributedTo
54 &getroutinglist &delroutingmember &addroutingmember
56 &check_routing &updateClaim &removeMissingIssue
62 =head2 GetSuppliersWithLateIssues
66 C4::Serials - Give functions for serializing.
74 Give all XYZ functions
80 %supplierlist = &GetSuppliersWithLateIssues
82 this function get all suppliers with late issues.
85 the supplierlist into a hash. this hash containts id & name of the supplier
91 sub GetSuppliersWithLateIssues {
92 my $dbh = C4::Context->dbh;
94 SELECT DISTINCT id, name
96 LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
97 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
98 WHERE subscription.subscriptionid = serial.subscriptionid
99 AND (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4)
102 my $sth = $dbh->prepare($query);
105 while ( my ( $id, $name ) = $sth->fetchrow ) {
106 $supplierlist{$id} = $name;
108 return %supplierlist;
115 @issuelist = &GetLateIssues($supplierid)
117 this function select late issues on database
120 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
121 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
128 my ($supplierid) = @_;
129 my $dbh = C4::Context->dbh;
133 SELECT name,title,planneddate,serialseq,serial.subscriptionid
135 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
136 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
137 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
138 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
139 AND subscription.aqbooksellerid=$supplierid
142 $sth = $dbh->prepare($query);
145 SELECT name,title,planneddate,serialseq,serial.subscriptionid
147 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
148 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
149 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
150 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
153 $sth = $dbh->prepare($query);
160 while ( my $line = $sth->fetchrow_hashref ) {
161 $odd++ unless $line->{title} eq $last_title;
162 $line->{title} = "" if $line->{title} eq $last_title;
163 $last_title = $line->{title} if ( $line->{title} );
164 $line->{planneddate} = format_date( $line->{planneddate} );
166 push @issuelist, $line;
168 return $count, @issuelist;
171 =head2 GetSubscriptionHistoryFromSubscriptionId
175 $sth = GetSubscriptionHistoryFromSubscriptionId()
176 this function just prepare the SQL request.
177 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
179 $sth = $dbh->prepare($query).
185 sub GetSubscriptionHistoryFromSubscriptionId() {
186 my $dbh = C4::Context->dbh;
189 FROM subscriptionhistory
190 WHERE subscriptionid = ?
192 return $dbh->prepare($query);
195 =head2 GetSerialStatusFromSerialId
199 $sth = GetSerialStatusFromSerialId();
200 this function just prepare the SQL request.
201 After this function, don't forget to execute it by using $sth->execute($serialid)
203 $sth = $dbh->prepare($query).
209 sub GetSerialStatusFromSerialId() {
210 my $dbh = C4::Context->dbh;
216 return $dbh->prepare($query);
219 =head2 GetSerialInformation
223 $data = GetSerialInformation($serialid);
224 returns a hash containing :
225 items : items marcrecord (can be an array)
227 subscription table field
228 + information about subscription expiration
234 sub GetSerialInformation {
236 my $dbh = C4::Context->dbh;
238 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
239 if ( C4::Context->preference('IndependantBranches')
240 && C4::Context->userenv
241 && C4::Context->userenv->{'flags'} != 1
242 && C4::Context->userenv->{'branch'} ) {
244 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
247 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
250 my $rq = $dbh->prepare($query);
251 $rq->execute($serialid);
252 my $data = $rq->fetchrow_hashref;
254 # create item information if we have serialsadditems for this subscription
255 if ( $data->{'serialsadditems'} ) {
256 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
257 $queryitem->execute($serialid);
258 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
259 if ( scalar(@$itemnumbers) > 0 ) {
260 foreach my $itemnum (@$itemnumbers) {
262 #It is ASSUMED that GetMarcItem ALWAYS WORK...
263 #Maybe GetMarcItem should return values on failure
264 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
265 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0], $data );
266 $itemprocessed->{'itemnumber'} = $itemnum->[0];
267 $itemprocessed->{'itemid'} = $itemnum->[0];
268 $itemprocessed->{'serialid'} = $serialid;
269 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
270 push @{ $data->{'items'} }, $itemprocessed;
273 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
274 $itemprocessed->{'itemid'} = "N$serialid";
275 $itemprocessed->{'serialid'} = $serialid;
276 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
277 $itemprocessed->{'countitems'} = 0;
278 push @{ $data->{'items'} }, $itemprocessed;
281 $data->{ "status" . $data->{'serstatus'} } = 1;
282 $data->{'subscriptionexpired'} = HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
283 $data->{'abouttoexpire'} = abouttoexpire( $data->{'subscriptionid'} );
287 =head2 AddItem2Serial
291 $data = AddItem2Serial($serialid,$itemnumber);
292 Adds an itemnumber to Serial record
299 my ( $serialid, $itemnumber ) = @_;
300 my $dbh = C4::Context->dbh;
301 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
302 $rq->execute( $serialid, $itemnumber );
306 =head2 UpdateClaimdateIssues
310 UpdateClaimdateIssues($serialids,[$date]);
312 Update Claimdate for issues in @$serialids list with date $date
319 sub UpdateClaimdateIssues {
320 my ( $serialids, $date ) = @_;
321 my $dbh = C4::Context->dbh;
322 $date = strftime( "%Y-%m-%d", localtime ) unless ($date);
324 UPDATE serial SET claimdate=$date,status=7
325 WHERE serialid in (" . join( ",", @$serialids ) . ")";
326 my $rq = $dbh->prepare($query);
331 =head2 GetSubscription
335 $subs = GetSubscription($subscriptionid)
336 this function get the subscription which has $subscriptionid as id.
338 a hashref. This hash containts
339 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
345 sub GetSubscription {
346 my ($subscriptionid) = @_;
347 my $dbh = C4::Context->dbh;
349 SELECT subscription.*,
350 subscriptionhistory.*,
351 aqbooksellers.name AS aqbooksellername,
352 biblio.title AS bibliotitle,
353 subscription.biblionumber as bibnum);
354 if ( C4::Context->preference('IndependantBranches')
355 && C4::Context->userenv
356 && C4::Context->userenv->{'flags'} != 1
357 && C4::Context->userenv->{'branch'} ) {
359 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
363 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
364 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
365 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
366 WHERE subscription.subscriptionid = ?
369 # if (C4::Context->preference('IndependantBranches') &&
370 # C4::Context->userenv &&
371 # C4::Context->userenv->{'flags'} != 1){
372 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
373 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
375 $debug and warn "query : $query\nsubsid :$subscriptionid";
376 my $sth = $dbh->prepare($query);
377 $sth->execute($subscriptionid);
378 return $sth->fetchrow_hashref;
381 =head2 GetFullSubscription
385 \@res = GetFullSubscription($subscriptionid)
386 this function read on serial table.
392 sub GetFullSubscription {
393 my ($subscriptionid) = @_;
394 my $dbh = C4::Context->dbh;
396 SELECT serial.serialid,
399 serial.publisheddate,
401 serial.notes as notes,
402 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
403 aqbooksellers.name as aqbooksellername,
404 biblio.title as bibliotitle,
405 subscription.branchcode AS branchcode,
406 subscription.subscriptionid AS subscriptionid |;
407 if ( C4::Context->preference('IndependantBranches')
408 && C4::Context->userenv
409 && C4::Context->userenv->{'flags'} != 1
410 && C4::Context->userenv->{'branch'} ) {
412 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
416 LEFT JOIN subscription ON
417 (serial.subscriptionid=subscription.subscriptionid )
418 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
419 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
420 WHERE serial.subscriptionid = ?
422 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
423 serial.subscriptionid
425 $debug and warn "GetFullSubscription query: $query";
426 my $sth = $dbh->prepare($query);
427 $sth->execute($subscriptionid);
428 return $sth->fetchall_arrayref( {} );
431 =head2 PrepareSerialsData
435 \@res = PrepareSerialsData($serialinfomation)
436 where serialinformation is a hashref array
442 sub PrepareSerialsData {
448 my $aqbooksellername;
452 my $previousnote = "";
454 foreach my $subs (@$lines) {
455 $subs->{'publisheddate'} = (
456 $subs->{'publisheddate'}
457 ? format_date( $subs->{'publisheddate'} )
460 $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
461 $subs->{ "status" . $subs->{'status'} } = 1;
462 $subs->{"checked"} = $subs->{'status'} =~ /1|3|4|7/;
464 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
465 $year = $subs->{'year'};
469 if ( $tmpresults{$year} ) {
470 push @{ $tmpresults{$year}->{'serials'} }, $subs;
472 $tmpresults{$year} = {
474 'aqbooksellername' => $subs->{'aqbooksellername'},
475 'bibliotitle' => $subs->{'bibliotitle'},
476 'serials' => [$subs],
481 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
482 push @res, $tmpresults{$key};
484 $res[0]->{'first'} = 1;
488 =head2 GetSubscriptionsFromBiblionumber
490 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
491 this function get the subscription list. it reads on subscription table.
493 table of subscription which has the biblionumber given on input arg.
494 each line of this table is a hashref. All hashes containt
495 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
499 sub GetSubscriptionsFromBiblionumber {
500 my ($biblionumber) = @_;
501 my $dbh = C4::Context->dbh;
503 SELECT subscription.*,
505 subscriptionhistory.*,
506 aqbooksellers.name AS aqbooksellername,
507 biblio.title AS bibliotitle
509 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
510 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
511 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
512 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
513 WHERE subscription.biblionumber = ?
515 my $sth = $dbh->prepare($query);
516 $sth->execute($biblionumber);
518 while ( my $subs = $sth->fetchrow_hashref ) {
519 $subs->{startdate} = format_date( $subs->{startdate} );
520 $subs->{histstartdate} = format_date( $subs->{histstartdate} );
521 $subs->{histenddate} = format_date( $subs->{histenddate} );
522 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
523 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
524 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
525 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
526 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
527 $subs->{ "status" . $subs->{'status'} } = 1;
528 $subs->{'cannotedit'} =
529 ( C4::Context->preference('IndependantBranches')
530 && C4::Context->userenv
531 && C4::Context->userenv->{flags} % 2 != 1
532 && C4::Context->userenv->{branch}
533 && $subs->{branchcode}
534 && ( C4::Context->userenv->{branch} ne $subs->{branchcode} ) );
536 if ( $subs->{enddate} eq '0000-00-00' ) {
537 $subs->{enddate} = '';
539 $subs->{enddate} = format_date( $subs->{enddate} );
541 $subs->{'abouttoexpire'} = abouttoexpire( $subs->{'subscriptionid'} );
542 $subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
548 =head2 GetFullSubscriptionsFromBiblionumber
552 \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
553 this function read on serial table.
559 sub GetFullSubscriptionsFromBiblionumber {
560 my ($biblionumber) = @_;
561 my $dbh = C4::Context->dbh;
563 SELECT serial.serialid,
566 serial.publisheddate,
568 serial.notes as notes,
569 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
570 biblio.title as bibliotitle,
571 subscription.branchcode AS branchcode,
572 subscription.subscriptionid AS subscriptionid|;
573 if ( C4::Context->preference('IndependantBranches')
574 && C4::Context->userenv
575 && C4::Context->userenv->{'flags'} != 1
576 && C4::Context->userenv->{'branch'} ) {
578 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
583 LEFT JOIN subscription ON
584 (serial.subscriptionid=subscription.subscriptionid)
585 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
586 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
587 WHERE subscription.biblionumber = ?
589 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
590 serial.subscriptionid
592 my $sth = $dbh->prepare($query);
593 $sth->execute($biblionumber);
594 return $sth->fetchall_arrayref( {} );
597 =head2 GetSubscriptions
601 @results = GetSubscriptions($title,$ISSN,$biblionumber);
602 this function get all subscriptions which has title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
604 a table of hashref. Each hash containt the subscription.
610 sub GetSubscriptions {
611 my ( $string, $issn, $biblionumber ) = @_;
613 #return unless $title or $ISSN or $biblionumber;
614 my $dbh = C4::Context->dbh;
617 SELECT subscription.*, subscriptionhistory.*, biblio.title,biblioitems.issn,biblio.biblionumber
619 LEFT JOIN subscriptionhistory USING(subscriptionid)
620 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
621 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
626 $sqlwhere = " WHERE biblio.biblionumber=?";
627 push @bind_params, $biblionumber;
631 my @strings_to_search;
632 @strings_to_search = map { "%$_%" } split( / /, $string );
633 foreach my $index qw(biblio.title subscription.callnumber subscription.location subscription.notes subscription.internalnotes) {
634 push @bind_params, @strings_to_search;
635 my $tmpstring = "AND $index LIKE ? " x scalar(@strings_to_search);
636 $debug && warn "$tmpstring";
637 $tmpstring =~ s/^AND //;
638 push @sqlstrings, $tmpstring;
640 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
644 my @strings_to_search;
645 @strings_to_search = map { "%$_%" } split( / /, $issn );
646 foreach my $index qw(biblioitems.issn subscription.callnumber) {
647 push @bind_params, @strings_to_search;
648 my $tmpstring = "OR $index LIKE ? " x scalar(@strings_to_search);
649 $debug && warn "$tmpstring";
650 $tmpstring =~ s/^OR //;
651 push @sqlstrings, $tmpstring;
653 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
655 $sql .= "$sqlwhere ORDER BY title";
656 $debug and warn "GetSubscriptions query: $sql params : ", join( " ", @bind_params );
657 $sth = $dbh->prepare($sql);
658 $sth->execute(@bind_params);
660 my $previoustitle = "";
663 while ( my $line = $sth->fetchrow_hashref ) {
664 if ( $previoustitle eq $line->{title} ) {
668 $previoustitle = $line->{title};
671 $line->{toggle} = 1 if $odd == 1;
672 $line->{'cannotedit'} =
673 ( C4::Context->preference('IndependantBranches')
674 && C4::Context->userenv
675 && C4::Context->userenv->{flags} % 2 != 1
676 && C4::Context->userenv->{branch}
677 && $line->{branchcode}
678 && ( C4::Context->userenv->{branch} ne $line->{branchcode} ) );
679 push @results, $line;
688 ($totalissues,@serials) = GetSerials($subscriptionid);
689 this function get every serial not arrived for a given subscription
690 as well as the number of issues registered in the database (all types)
691 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
693 FIXME: We should return \@serials.
700 my ( $subscriptionid, $count ) = @_;
701 my $dbh = C4::Context->dbh;
703 # status = 2 is "arrived"
705 $count = 5 unless ($count);
707 my $query = "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
709 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
710 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
711 my $sth = $dbh->prepare($query);
712 $sth->execute($subscriptionid);
714 while ( my $line = $sth->fetchrow_hashref ) {
715 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
716 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
717 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
718 push @serials, $line;
721 # OK, now add the last 5 issues arrives/missing
722 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
724 WHERE subscriptionid = ?
725 AND (status in (2,4,5))
726 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
728 $sth = $dbh->prepare($query);
729 $sth->execute($subscriptionid);
730 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
732 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
733 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
734 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
735 push @serials, $line;
738 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
739 $sth = $dbh->prepare($query);
740 $sth->execute($subscriptionid);
741 my ($totalissues) = $sth->fetchrow;
742 return ( $totalissues, @serials );
749 ($totalissues,@serials) = GetSerials2($subscriptionid,$status);
750 this function get every serial waited for a given subscription
751 as well as the number of issues registered in the database (all types)
752 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
759 my ( $subscription, $status ) = @_;
760 my $dbh = C4::Context->dbh;
762 SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
764 WHERE subscriptionid=$subscription AND status IN ($status)
765 ORDER BY publisheddate,serialid DESC
767 $debug and warn "GetSerials2 query: $query";
768 my $sth = $dbh->prepare($query);
772 while ( my $line = $sth->fetchrow_hashref ) {
773 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
774 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
775 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
776 push @serials, $line;
778 my ($totalissues) = scalar(@serials);
779 return ( $totalissues, @serials );
782 =head2 GetLatestSerials
786 \@serials = GetLatestSerials($subscriptionid,$limit)
787 get the $limit's latest serials arrived or missing for a given subscription
789 a ref to a table which it containts all of the latest serials stored into a hash.
795 sub GetLatestSerials {
796 my ( $subscriptionid, $limit ) = @_;
797 my $dbh = C4::Context->dbh;
799 # status = 2 is "arrived"
800 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
802 WHERE subscriptionid = ?
803 AND (status =2 or status=4)
804 ORDER BY planneddate DESC LIMIT 0,$limit
806 my $sth = $dbh->prepare($strsth);
807 $sth->execute($subscriptionid);
809 while ( my $line = $sth->fetchrow_hashref ) {
810 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
811 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
812 push @serials, $line;
818 # WHERE subscriptionid=?
820 # $sth=$dbh->prepare($query);
821 # $sth->execute($subscriptionid);
822 # my ($totalissues) = $sth->fetchrow;
826 =head2 GetDistributedTo
830 $distributedto=GetDistributedTo($subscriptionid)
831 This function select the old previous value of distributedto in the database.
837 sub GetDistributedTo {
838 my $dbh = C4::Context->dbh;
840 my $subscriptionid = @_;
841 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
842 my $sth = $dbh->prepare($query);
843 $sth->execute($subscriptionid);
844 return ($distributedto) = $sth->fetchrow;
852 $val is a hashref containing all the attributes of the table 'subscription'
853 This function get the next issue for the subscription given on input arg
855 all the input params updated.
863 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
864 # $calculated = $val->{numberingmethod};
865 # # calculate the (expected) value of the next issue recieved.
866 # $newlastvalue1 = $val->{lastvalue1};
867 # # check if we have to increase the new value.
868 # $newinnerloop1 = $val->{innerloop1}+1;
869 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
870 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
871 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
872 # $calculated =~ s/\{X\}/$newlastvalue1/g;
874 # $newlastvalue2 = $val->{lastvalue2};
875 # # check if we have to increase the new value.
876 # $newinnerloop2 = $val->{innerloop2}+1;
877 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
878 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
879 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
880 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
882 # $newlastvalue3 = $val->{lastvalue3};
883 # # check if we have to increase the new value.
884 # $newinnerloop3 = $val->{innerloop3}+1;
885 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
886 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
887 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
888 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
889 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
894 my ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
895 my $pattern = $val->{numberpattern};
896 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
897 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
898 $calculated = $val->{numberingmethod};
899 $newlastvalue1 = $val->{lastvalue1};
900 $newlastvalue2 = $val->{lastvalue2};
901 $newlastvalue3 = $val->{lastvalue3};
902 $newlastvalue1 = $val->{lastvalue1};
904 # check if we have to increase the new value.
905 $newinnerloop1 = $val->{innerloop1} + 1;
906 $newinnerloop1 = 0 if ( $newinnerloop1 >= $val->{every1} );
907 $newlastvalue1 += $val->{add1} if ( $newinnerloop1 < 1 ); # <1 to be true when 0 or empty.
908 $newlastvalue1 = $val->{setto1} if ( $newlastvalue1 > $val->{whenmorethan1} ); # reset counter if needed.
909 $calculated =~ s/\{X\}/$newlastvalue1/g;
911 $newlastvalue2 = $val->{lastvalue2};
913 # check if we have to increase the new value.
914 $newinnerloop2 = $val->{innerloop2} + 1;
915 $newinnerloop2 = 0 if ( $newinnerloop2 >= $val->{every2} );
916 $newlastvalue2 += $val->{add2} if ( $newinnerloop2 < 1 ); # <1 to be true when 0 or empty.
917 $newlastvalue2 = $val->{setto2} if ( $newlastvalue2 > $val->{whenmorethan2} ); # reset counter if needed.
918 if ( $pattern == 6 ) {
919 if ( $val->{hemisphere} == 2 ) {
920 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
921 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
923 my $newlastvalue2seq = $seasons[$newlastvalue2];
924 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
927 $calculated =~ s/\{Y\}/$newlastvalue2/g;
930 $newlastvalue3 = $val->{lastvalue3};
932 # check if we have to increase the new value.
933 $newinnerloop3 = $val->{innerloop3} + 1;
934 $newinnerloop3 = 0 if ( $newinnerloop3 >= $val->{every3} );
935 $newlastvalue3 += $val->{add3} if ( $newinnerloop3 < 1 ); # <1 to be true when 0 or empty.
936 $newlastvalue3 = $val->{setto3} if ( $newlastvalue3 > $val->{whenmorethan3} ); # reset counter if needed.
937 $calculated =~ s/\{Z\}/$newlastvalue3/g;
939 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
946 $calculated = GetSeq($val)
947 $val is a hashref containing all the attributes of the table 'subscription'
948 this function transforms {X},{Y},{Z} to 150,0,0 for example.
950 the sequence in integer format
958 my $pattern = $val->{numberpattern};
959 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
960 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
961 my $calculated = $val->{numberingmethod};
962 my $x = $val->{'lastvalue1'};
963 $calculated =~ s/\{X\}/$x/g;
964 my $newlastvalue2 = $val->{'lastvalue2'};
966 if ( $pattern == 6 ) {
967 if ( $val->{hemisphere} == 2 ) {
968 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
969 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
971 my $newlastvalue2seq = $seasons[$newlastvalue2];
972 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
975 $calculated =~ s/\{Y\}/$newlastvalue2/g;
977 my $z = $val->{'lastvalue3'};
978 $calculated =~ s/\{Z\}/$z/g;
982 =head2 GetExpirationDate
984 $sensddate = GetExpirationDate($subscriptionid)
986 this function return the next expiration date for a subscription given on input args.
993 sub GetExpirationDate {
994 my ( $subscriptionid, $startdate ) = @_;
995 my $dbh = C4::Context->dbh;
996 my $subscription = GetSubscription($subscriptionid);
999 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1000 $enddate = $startdate || $subscription->{startdate};
1001 my @date = split( /-/, $enddate );
1002 return if ( scalar(@date) != 3 || not check_date(@date) );
1003 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
1006 if ( my $length = $subscription->{numberlength} ) {
1008 #calculate the date of the last issue.
1009 for ( my $i = 1 ; $i <= $length ; $i++ ) {
1010 $enddate = GetNextDate( $enddate, $subscription );
1012 } elsif ( $subscription->{monthlength} ) {
1013 if ( $$subscription{startdate} ) {
1014 my @enddate = Add_Delta_YM( $date[0], $date[1], $date[2], 0, $subscription->{monthlength} );
1015 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1017 } elsif ( $subscription->{weeklength} ) {
1018 if ( $$subscription{startdate} ) {
1019 my @date = split( /-/, $subscription->{startdate} );
1020 my @enddate = Add_Delta_Days( $date[0], $date[1], $date[2], $subscription->{weeklength} * 7 );
1021 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1030 =head2 CountSubscriptionFromBiblionumber
1034 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1035 this count the number of subscription for a biblionumber given.
1037 the number of subscriptions with biblionumber given on input arg.
1043 sub CountSubscriptionFromBiblionumber {
1044 my ($biblionumber) = @_;
1045 my $dbh = C4::Context->dbh;
1046 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1047 my $sth = $dbh->prepare($query);
1048 $sth->execute($biblionumber);
1049 my $subscriptionsnumber = $sth->fetchrow;
1050 return $subscriptionsnumber;
1053 =head2 ModSubscriptionHistory
1057 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1059 this function modify the history of a subscription. Put your new values on input arg.
1065 sub ModSubscriptionHistory {
1066 my ( $subscriptionid, $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote ) = @_;
1067 my $dbh = C4::Context->dbh;
1068 my $query = "UPDATE subscriptionhistory
1069 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1070 WHERE subscriptionid=?
1072 my $sth = $dbh->prepare($query);
1073 $recievedlist =~ s/^; //;
1074 $missinglist =~ s/^; //;
1075 $opacnote =~ s/^; //;
1076 $sth->execute( $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
1080 =head2 ModSerialStatus
1084 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1086 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1087 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1093 sub ModSerialStatus {
1094 my ( $serialid, $serialseq, $planneddate, $publisheddate, $status, $notes ) = @_;
1096 #It is a usual serial
1097 # 1st, get previous status :
1098 my $dbh = C4::Context->dbh;
1099 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1100 my $sth = $dbh->prepare($query);
1101 $sth->execute($serialid);
1102 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1104 # change status & update subscriptionhistory
1106 if ( $status eq 6 ) {
1107 DelIssue( { 'serialid' => $serialid, 'subscriptionid' => $subscriptionid, 'serialseq' => $serialseq } );
1109 my $query = "UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?";
1110 $sth = $dbh->prepare($query);
1111 $sth->execute( $serialseq, $publisheddate, $planneddate, $status, $notes, $serialid );
1112 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1113 $sth = $dbh->prepare($query);
1114 $sth->execute($subscriptionid);
1115 my $val = $sth->fetchrow_hashref;
1116 unless ( $val->{manualhistory} ) {
1117 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1118 $sth = $dbh->prepare($query);
1119 $sth->execute($subscriptionid);
1120 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1121 if ( $status eq 2 ) {
1123 $recievedlist .= "; $serialseq"
1124 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1127 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1128 $missinglist .= "; $serialseq"
1130 and not index( "$missinglist", "$serialseq" ) >= 0 );
1131 $missinglist .= "; $serialseq"
1133 and index( "$missinglist", "$serialseq" ) >= 0 );
1134 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1135 $sth = $dbh->prepare($query);
1136 $recievedlist =~ s/^; //;
1137 $missinglist =~ s/^; //;
1138 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1142 # create new waited entry if needed (ie : was a "waited" and has changed)
1143 if ( $oldstatus eq 1 && $status ne 1 ) {
1144 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1145 $sth = $dbh->prepare($query);
1146 $sth->execute($subscriptionid);
1147 my $val = $sth->fetchrow_hashref;
1151 my ( $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 ) = GetNextSeq($val);
1153 # warn "Next Seq End";
1155 # next date (calculated from actual date & frequency parameters)
1156 # warn "publisheddate :$publisheddate ";
1157 my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1158 NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate );
1159 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1160 WHERE subscriptionid = ?";
1161 $sth = $dbh->prepare($query);
1162 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1164 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1165 if ( $val->{letter} && $status eq 2 && $oldstatus ne 2 ) {
1166 SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1171 =head2 GetNextExpected
1175 $nextexpected = GetNextExpected($subscriptionid)
1177 Get the planneddate for the current expected issue of the subscription.
1183 planneddate => C4::Dates object
1190 sub GetNextExpected($) {
1191 my ($subscriptionid) = @_;
1192 my $dbh = C4::Context->dbh;
1193 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1195 # Each subscription has only one 'expected' issue, with serial.status==1.
1196 $sth->execute( $subscriptionid, 1 );
1197 my ($nextissue) = $sth->fetchrow_hashref;
1198 if ( not $nextissue ) {
1199 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1200 $sth->execute($subscriptionid);
1201 $nextissue = $sth->fetchrow_hashref;
1203 $nextissue->{planneddate} = C4::Dates->new( $nextissue->{planneddate}, 'iso' );
1208 =head2 ModNextExpected
1212 ModNextExpected($subscriptionid,$date)
1214 Update the planneddate for the current expected issue of the subscription.
1215 This will modify all future prediction results.
1217 C<$date> is a C4::Dates object.
1223 sub ModNextExpected($$) {
1224 my ( $subscriptionid, $date ) = @_;
1225 my $dbh = C4::Context->dbh;
1227 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1228 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1230 # Each subscription has only one 'expected' issue, with serial.status==1.
1231 $sth->execute( $date->output('iso'), $date->output('iso'), $subscriptionid, 1 );
1236 =head2 ModSubscription
1240 this function modify a subscription. Put all new values on input args.
1246 sub ModSubscription {
1247 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1248 $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $add1, $every1,
1249 $whenmorethan1, $setto1, $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2,
1250 $lastvalue2, $innerloop2, $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1251 $numberingmethod, $status, $biblionumber, $callnumber, $notes, $letter, $hemisphere, $manualhistory,
1252 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid
1255 # warn $irregularity;
1256 my $dbh = C4::Context->dbh;
1257 my $query = "UPDATE subscription
1258 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1259 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1260 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1261 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1262 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1263 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?,
1264 letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,
1265 staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1267 WHERE subscriptionid = ?";
1269 #warn "query :".$query;
1270 my $sth = $dbh->prepare($query);
1272 $auser, $branchcode, $aqbooksellerid, $cost,
1273 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1274 $dow, "$irregularity", $numberpattern, $numberlength,
1275 $weeklength, $monthlength, $add1, $every1,
1276 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1277 $add2, $every2, $whenmorethan2, $setto2,
1278 $lastvalue2, $innerloop2, $add3, $every3,
1279 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1280 $numberingmethod, $status, $biblionumber, $callnumber,
1281 $notes, $letter, $hemisphere, ( $manualhistory ? $manualhistory : 0 ),
1282 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1283 $graceperiod, $location, $enddate, $subscriptionid
1285 my $rows = $sth->rows;
1288 logaction( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1292 =head2 NewSubscription
1296 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1297 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1298 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1299 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1300 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1301 $numberingmethod, $status, $notes, $serialsadditems,
1302 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate);
1304 Create a new subscription with value given on input args.
1307 the id of this new subscription
1313 sub NewSubscription {
1314 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1315 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1316 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1317 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, $status,
1318 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1319 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1321 my $dbh = C4::Context->dbh;
1323 #save subscription (insert into database)
1325 INSERT INTO subscription
1326 (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1327 startdate,periodicity,dow,numberlength,weeklength,monthlength,
1328 add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1329 add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1330 add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1331 numberingmethod, status, notes, letter,firstacquidate,irregularity,
1332 numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
1333 staffdisplaycount,opacdisplaycount,graceperiod,location,enddate)
1334 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1336 my $sth = $dbh->prepare($query);
1338 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1339 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1340 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1341 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, "$status",
1342 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1343 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1346 #then create the 1st waited number
1347 my $subscriptionid = $dbh->{'mysql_insertid'};
1349 INSERT INTO subscriptionhistory
1350 (biblionumber, subscriptionid, histstartdate, opacnote, librariannote)
1353 $sth = $dbh->prepare($query);
1354 $sth->execute( $biblionumber, $subscriptionid, $startdate, $notes, $internalnotes );
1356 # reread subscription to get a hash (for calculation of the 1st issue number)
1360 WHERE subscriptionid = ?
1362 $sth = $dbh->prepare($query);
1363 $sth->execute($subscriptionid);
1364 my $val = $sth->fetchrow_hashref;
1366 # calculate issue number
1367 my $serialseq = GetSeq($val);
1370 (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1371 VALUES (?,?,?,?,?,?)
1373 $sth = $dbh->prepare($query);
1374 $sth->execute( "$serialseq", $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
1376 logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1378 #set serial flag on biblio if not already set.
1379 my ( $null, ($bib) ) = GetBiblio($biblionumber);
1380 if ( !$bib->{'serial'} ) {
1381 my $record = GetMarcBiblio($biblionumber);
1382 my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $bib->{'frameworkcode'} );
1384 eval { $record->field($tag)->update( $subf => 1 ); };
1386 ModBiblio( $record, $biblionumber, $bib->{'frameworkcode'} );
1388 return $subscriptionid;
1391 =head2 ReNewSubscription
1395 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1397 this function renew a subscription with values given on input args.
1403 sub ReNewSubscription {
1404 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1405 my $dbh = C4::Context->dbh;
1406 my $subscription = GetSubscription($subscriptionid);
1410 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1411 WHERE biblio.biblionumber=?
1413 my $sth = $dbh->prepare($query);
1414 $sth->execute( $subscription->{biblionumber} );
1415 my $biblio = $sth->fetchrow_hashref;
1417 if ( C4::Context->preference("RenewSerialAddsSuggestion") ) {
1420 { 'suggestedby' => $user,
1421 'title' => $subscription->{bibliotitle},
1422 'author' => $biblio->{author},
1423 'publishercode' => $biblio->{publishercode},
1424 'note' => $biblio->{note},
1425 'biblionumber' => $subscription->{biblionumber}
1430 # renew subscription
1433 SET startdate=?,numberlength=?,weeklength=?,monthlength=?
1434 WHERE subscriptionid=?
1436 $sth = $dbh->prepare($query);
1437 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1438 my $enddate = GetExpirationDate($subscriptionid);
1439 $debug && warn "enddate :$enddate";
1443 WHERE subscriptionid=?
1445 $sth = $dbh->prepare($query);
1446 $sth->execute( $enddate, $subscriptionid );
1448 UPDATE subscriptionhistory
1450 WHERE subscriptionid=?
1452 $sth = $dbh->prepare($query);
1453 $sth->execute( $enddate, $subscriptionid );
1455 logaction( "SERIAL", "RENEW", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1462 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1464 Create a new issue stored on the database.
1465 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1472 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
1473 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1475 my $dbh = C4::Context->dbh;
1478 (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1479 VALUES (?,?,?,?,?,?,?)
1481 my $sth = $dbh->prepare($query);
1482 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
1483 my $serialid = $dbh->{'mysql_insertid'};
1485 SELECT missinglist,recievedlist
1486 FROM subscriptionhistory
1487 WHERE subscriptionid=?
1489 $sth = $dbh->prepare($query);
1490 $sth->execute($subscriptionid);
1491 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1493 if ( $status eq 2 ) {
1494 ### TODO Add a feature that improves recognition and description.
1495 ### As such count (serialseq) i.e. : N18,2(N19),N20
1496 ### Would use substr and index But be careful to previous presence of ()
1497 $recievedlist .= "; $serialseq" unless ( index( $recievedlist, $serialseq ) > 0 );
1499 if ( $status eq 4 ) {
1500 $missinglist .= "; $serialseq" unless ( index( $missinglist, $serialseq ) > 0 );
1503 UPDATE subscriptionhistory
1504 SET recievedlist=?, missinglist=?
1505 WHERE subscriptionid=?
1507 $sth = $dbh->prepare($query);
1508 $recievedlist =~ s/^; //;
1509 $missinglist =~ s/^; //;
1510 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1514 =head2 ItemizeSerials
1518 ItemizeSerials($serialid, $info);
1519 $info is a hashref containing barcode branch, itemcallnumber, status, location
1520 $serialid the serialid
1522 1 if the itemize is a succes.
1523 0 and @error else. @error containts the list of errors found.
1529 sub ItemizeSerials {
1530 my ( $serialid, $info ) = @_;
1531 my $now = POSIX::strftime( "%Y-%m-%d", localtime );
1533 my $dbh = C4::Context->dbh;
1539 my $sth = $dbh->prepare($query);
1540 $sth->execute($serialid);
1541 my $data = $sth->fetchrow_hashref;
1542 if ( C4::Context->preference("RoutingSerials") ) {
1544 # check for existing biblioitem relating to serial issue
1545 my ( $count, @results ) = GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1547 for ( my $i = 0 ; $i < $count ; $i++ ) {
1548 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' (' . $data->{'planneddate'} . ')' ) {
1549 $bibitemno = $results[$i]->{'biblioitemnumber'};
1553 if ( $bibitemno == 0 ) {
1554 my $sth = $dbh->prepare( "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC" );
1555 $sth->execute( $data->{'biblionumber'} );
1556 my $biblioitem = $sth->fetchrow_hashref;
1557 $biblioitem->{'volumedate'} = $data->{planneddate};
1558 $biblioitem->{'volumeddesc'} = $data->{serialseq} . ' (' . format_date( $data->{'planneddate'} ) . ')';
1559 $biblioitem->{'dewey'} = $info->{itemcallnumber};
1563 my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1564 if ( $info->{barcode} ) {
1566 my $exists = itemdata( $info->{'barcode'} );
1567 push @errors, "barcode_not_unique" if ($exists);
1569 my $marcrecord = MARC::Record->new();
1570 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.barcode", $fwk );
1571 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{barcode} );
1572 $marcrecord->insert_fields_ordered($newField);
1573 if ( $info->{branch} ) {
1574 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.homebranch", $fwk );
1576 #warn "items.homebranch : $tag , $subfield";
1577 if ( $marcrecord->field($tag) ) {
1578 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1580 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1581 $marcrecord->insert_fields_ordered($newField);
1583 ( $tag, $subfield ) = GetMarcFromKohaField( "items.holdingbranch", $fwk );
1585 #warn "items.holdingbranch : $tag , $subfield";
1586 if ( $marcrecord->field($tag) ) {
1587 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1589 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1590 $marcrecord->insert_fields_ordered($newField);
1593 if ( $info->{itemcallnumber} ) {
1594 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemcallnumber", $fwk );
1596 if ( $marcrecord->field($tag) ) {
1597 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{itemcallnumber} );
1599 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{itemcallnumber} );
1600 $marcrecord->insert_fields_ordered($newField);
1603 if ( $info->{notes} ) {
1604 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemnotes", $fwk );
1606 if ( $marcrecord->field($tag) ) {
1607 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{notes} );
1609 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{notes} );
1610 $marcrecord->insert_fields_ordered($newField);
1613 if ( $info->{location} ) {
1614 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.location", $fwk );
1616 if ( $marcrecord->field($tag) ) {
1617 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{location} );
1619 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{location} );
1620 $marcrecord->insert_fields_ordered($newField);
1623 if ( $info->{status} ) {
1624 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.notforloan", $fwk );
1626 if ( $marcrecord->field($tag) ) {
1627 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{status} );
1629 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{status} );
1630 $marcrecord->insert_fields_ordered($newField);
1633 if ( C4::Context->preference("RoutingSerials") ) {
1634 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.dateaccessioned", $fwk );
1635 if ( $marcrecord->field($tag) ) {
1636 $marcrecord->field($tag)->add_subfields( "$subfield" => $now );
1638 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1639 $marcrecord->insert_fields_ordered($newField);
1642 AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1645 return ( 0, @errors );
1649 =head2 HasSubscriptionStrictlyExpired
1653 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1655 the subscription has stricly expired when today > the end subscription date
1658 1 if true, 0 if false, -1 if the expiration date is not set.
1664 sub HasSubscriptionStrictlyExpired {
1666 # Getting end of subscription date
1667 my ($subscriptionid) = @_;
1668 my $dbh = C4::Context->dbh;
1669 my $subscription = GetSubscription($subscriptionid);
1670 my $expirationdate = $subscription->{enddate} || GetExpirationDate($subscriptionid);
1672 # If the expiration date is set
1673 if ( $expirationdate != 0 ) {
1674 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1676 # Getting today's date
1677 my ( $nowyear, $nowmonth, $nowday ) = Today();
1679 # if today's date > expiration date, then the subscription has stricly expired
1680 if ( Delta_Days( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1687 # There are some cases where the expiration date is not set
1688 # As we can't determine if the subscription has expired on a date-basis,
1694 =head2 HasSubscriptionExpired
1698 $has_expired = HasSubscriptionExpired($subscriptionid)
1700 the subscription has expired when the next issue to arrive is out of subscription limit.
1703 0 if the subscription has not expired
1704 1 if the subscription has expired
1705 2 if has subscription does not have a valid expiration date set
1711 sub HasSubscriptionExpired {
1712 my ($subscriptionid) = @_;
1713 my $dbh = C4::Context->dbh;
1714 my $subscription = GetSubscription($subscriptionid);
1715 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
1716 my $expirationdate = $subscription->{enddate};
1718 SELECT max(planneddate)
1720 WHERE subscriptionid=?
1722 my $sth = $dbh->prepare($query);
1723 $sth->execute($subscriptionid);
1724 my ($res) = $sth->fetchrow;
1725 return 0 unless $res;
1726 my @res = split( /-/, $res );
1727 my @endofsubscriptiondate = split( /-/, $expirationdate );
1728 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date(@res) || not check_date(@endofsubscriptiondate) );
1730 if ( ( @endofsubscriptiondate && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1734 if ( $subscription->{'numberlength'} ) {
1735 my $countreceived = countissuesfrom( $subscriptionid, $subscription->{'startdate'} );
1736 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1742 return 0; # Notice that you'll never get here.
1745 =head2 SetDistributedto
1749 SetDistributedto($distributedto,$subscriptionid);
1750 This function update the value of distributedto for a subscription given on input arg.
1756 sub SetDistributedto {
1757 my ( $distributedto, $subscriptionid ) = @_;
1758 my $dbh = C4::Context->dbh;
1762 WHERE subscriptionid=?
1764 my $sth = $dbh->prepare($query);
1765 $sth->execute( $distributedto, $subscriptionid );
1768 =head2 DelSubscription
1772 DelSubscription($subscriptionid)
1773 this function delete the subscription which has $subscriptionid as id.
1779 sub DelSubscription {
1780 my ($subscriptionid) = @_;
1781 my $dbh = C4::Context->dbh;
1782 $subscriptionid = $dbh->quote($subscriptionid);
1783 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1784 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1785 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1787 logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1794 DelIssue($serialseq,$subscriptionid)
1795 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1802 my ($dataissue) = @_;
1803 my $dbh = C4::Context->dbh;
1804 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1809 AND subscriptionid= ?
1811 my $mainsth = $dbh->prepare($query);
1812 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1814 #Delete element from subscription history
1815 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1816 my $sth = $dbh->prepare($query);
1817 $sth->execute( $dataissue->{'subscriptionid'} );
1818 my $val = $sth->fetchrow_hashref;
1819 unless ( $val->{manualhistory} ) {
1821 SELECT * FROM subscriptionhistory
1822 WHERE subscriptionid= ?
1824 my $sth = $dbh->prepare($query);
1825 $sth->execute( $dataissue->{'subscriptionid'} );
1826 my $data = $sth->fetchrow_hashref;
1827 my $serialseq = $dataissue->{'serialseq'};
1828 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1829 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1830 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1831 $sth = $dbh->prepare($strsth);
1832 $sth->execute( $dataissue->{'subscriptionid'} );
1835 return $mainsth->rows;
1838 =head2 GetLateOrMissingIssues
1842 ($count,@issuelist) = &GetLateMissingIssues($supplierid,$serialid)
1844 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1847 a count of the number of missing issues
1848 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1849 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1855 sub GetLateOrMissingIssues {
1856 my ( $supplierid, $serialid, $order ) = @_;
1857 my $dbh = C4::Context->dbh;
1861 $byserial = "and serialid = " . $serialid;
1864 $order .= ", title";
1869 $sth = $dbh->prepare(
1871 serialid, aqbooksellerid, name,
1872 biblio.title, planneddate, serialseq,
1873 serial.status, serial.subscriptionid, claimdate
1875 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1876 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1877 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1878 WHERE subscription.subscriptionid = serial.subscriptionid
1879 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1880 AND subscription.aqbooksellerid=$supplierid
1885 $sth = $dbh->prepare(
1887 serialid, aqbooksellerid, name,
1888 biblio.title, planneddate, serialseq,
1889 serial.status, serial.subscriptionid, claimdate
1891 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1892 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1893 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1894 WHERE subscription.subscriptionid = serial.subscriptionid
1895 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1905 while ( my $line = $sth->fetchrow_hashref ) {
1906 $odd++ unless $line->{title} eq $last_title;
1907 $last_title = $line->{title} if ( $line->{title} );
1908 $line->{planneddate} = format_date( $line->{planneddate} );
1909 $line->{claimdate} = format_date( $line->{claimdate} );
1910 $line->{ "status" . $line->{status} } = 1;
1911 $line->{'odd'} = 1 if $odd % 2;
1913 push @issuelist, $line;
1915 return $count, @issuelist;
1918 =head2 removeMissingIssue
1922 removeMissingIssue($subscriptionid)
1924 this function removes an issue from being part of the missing string in
1925 subscriptionlist.missinglist column
1927 called when a missing issue is found from the serials-recieve.pl file
1933 sub removeMissingIssue {
1934 my ( $sequence, $subscriptionid ) = @_;
1935 my $dbh = C4::Context->dbh;
1936 my $sth = $dbh->prepare("SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1937 $sth->execute($subscriptionid);
1938 my $data = $sth->fetchrow_hashref;
1939 my $missinglist = $data->{'missinglist'};
1940 my $missinglistbefore = $missinglist;
1942 # warn $missinglist." before";
1943 $missinglist =~ s/($sequence)//;
1945 # warn $missinglist." after";
1946 if ( $missinglist ne $missinglistbefore ) {
1947 $missinglist =~ s/\|\s\|/\|/g;
1948 $missinglist =~ s/^\| //g;
1949 $missinglist =~ s/\|$//g;
1950 my $sth2 = $dbh->prepare(
1951 "UPDATE subscriptionhistory
1953 WHERE subscriptionid = ?"
1955 $sth2->execute( $missinglist, $subscriptionid );
1963 &updateClaim($serialid)
1965 this function updates the time when a claim is issued for late/missing items
1967 called from claims.pl file
1974 my ($serialid) = @_;
1975 my $dbh = C4::Context->dbh;
1976 my $sth = $dbh->prepare(
1977 "UPDATE serial SET claimdate = now()
1981 $sth->execute($serialid);
1984 =head2 getsupplierbyserialid
1988 ($result) = &getsupplierbyserialid($serialid)
1990 this function is used to find the supplier id given a serial id
1993 hashref containing serialid, subscriptionid, and aqbooksellerid
1999 sub getsupplierbyserialid {
2000 my ($serialid) = @_;
2001 my $dbh = C4::Context->dbh;
2002 my $sth = $dbh->prepare(
2003 "SELECT serialid, serial.subscriptionid, aqbooksellerid
2005 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
2009 $sth->execute($serialid);
2010 my $line = $sth->fetchrow_hashref;
2011 my $result = $line->{'aqbooksellerid'};
2015 =head2 check_routing
2019 ($result) = &check_routing($subscriptionid)
2021 this function checks to see if a serial has a routing list and returns the count of routingid
2022 used to show either an 'add' or 'edit' link
2029 my ($subscriptionid) = @_;
2030 my $dbh = C4::Context->dbh;
2031 my $sth = $dbh->prepare(
2032 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2033 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2034 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2037 $sth->execute($subscriptionid);
2038 my $line = $sth->fetchrow_hashref;
2039 my $result = $line->{'routingids'};
2043 =head2 addroutingmember
2047 &addroutingmember($borrowernumber,$subscriptionid)
2049 this function takes a borrowernumber and subscriptionid and add the member to the
2050 routing list for that serial subscription and gives them a rank on the list
2051 of either 1 or highest current rank + 1
2057 sub addroutingmember {
2058 my ( $borrowernumber, $subscriptionid ) = @_;
2060 my $dbh = C4::Context->dbh;
2061 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
2062 $sth->execute($subscriptionid);
2063 while ( my $line = $sth->fetchrow_hashref ) {
2064 if ( $line->{'rank'} > 0 ) {
2065 $rank = $line->{'rank'} + 1;
2070 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
2071 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2074 =head2 reorder_members
2078 &reorder_members($subscriptionid,$routingid,$rank)
2080 this function is used to reorder the routing list
2082 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2083 - it gets all members on list puts their routingid's into an array
2084 - removes the one in the array that is $routingid
2085 - then reinjects $routingid at point indicated by $rank
2086 - then update the database with the routingids in the new order
2092 sub reorder_members {
2093 my ( $subscriptionid, $routingid, $rank ) = @_;
2094 my $dbh = C4::Context->dbh;
2095 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
2096 $sth->execute($subscriptionid);
2098 while ( my $line = $sth->fetchrow_hashref ) {
2099 push( @result, $line->{'routingid'} );
2102 # To find the matching index
2104 my $key = -1; # to allow for 0 being a valid response
2105 for ( $i = 0 ; $i < @result ; $i++ ) {
2106 if ( $routingid == $result[$i] ) {
2107 $key = $i; # save the index
2112 # if index exists in array then move it to new position
2113 if ( $key > -1 && $rank > 0 ) {
2114 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
2115 my $moving_item = splice( @result, $key, 1 );
2116 splice( @result, $new_rank, 0, $moving_item );
2118 for ( my $j = 0 ; $j < @result ; $j++ ) {
2119 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
2124 =head2 delroutingmember
2128 &delroutingmember($routingid,$subscriptionid)
2130 this function either deletes one member from routing list if $routingid exists otherwise
2131 deletes all members from the routing list
2137 sub delroutingmember {
2139 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2140 my ( $routingid, $subscriptionid ) = @_;
2141 my $dbh = C4::Context->dbh;
2143 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2144 $sth->execute($routingid);
2145 reorder_members( $subscriptionid, $routingid );
2147 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2148 $sth->execute($subscriptionid);
2152 =head2 getroutinglist
2156 ($count,@routinglist) = &getroutinglist($subscriptionid)
2158 this gets the info from the subscriptionroutinglist for $subscriptionid
2161 a count of the number of members on routinglist
2162 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2163 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2169 sub getroutinglist {
2170 my ($subscriptionid) = @_;
2171 my $dbh = C4::Context->dbh;
2172 my $sth = $dbh->prepare(
2173 "SELECT routingid, borrowernumber, ranking, biblionumber
2175 LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2176 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2179 $sth->execute($subscriptionid);
2182 while ( my $line = $sth->fetchrow_hashref ) {
2184 push( @routinglist, $line );
2186 return ( $count, @routinglist );
2189 =head2 countissuesfrom
2193 $result = &countissuesfrom($subscriptionid,$startdate)
2200 sub countissuesfrom {
2201 my ( $subscriptionid, $startdate ) = @_;
2202 my $dbh = C4::Context->dbh;
2206 WHERE subscriptionid=?
2207 AND serial.publisheddate>?
2209 my $sth = $dbh->prepare($query);
2210 $sth->execute( $subscriptionid, $startdate );
2211 my ($countreceived) = $sth->fetchrow;
2212 return $countreceived;
2219 $result = &CountIssues($subscriptionid)
2227 my ($subscriptionid) = @_;
2228 my $dbh = C4::Context->dbh;
2232 WHERE subscriptionid=?
2234 my $sth = $dbh->prepare($query);
2235 $sth->execute($subscriptionid);
2236 my ($countreceived) = $sth->fetchrow;
2237 return $countreceived;
2244 $result = &HasItems($subscriptionid)
2252 my ($subscriptionid) = @_;
2253 my $dbh = C4::Context->dbh;
2255 SELECT COUNT(serialitems.itemnumber)
2257 LEFT JOIN serialitems USING(serialid)
2258 WHERE subscriptionid=? AND serialitems.serialid NOT NULL
2260 my $sth=$dbh->prepare($query);
2261 $sth->execute($subscriptionid);
2262 my ($countitems)=$sth->fetchrow;
2266 =head2 abouttoexpire
2270 $result = &abouttoexpire($subscriptionid)
2272 this function alerts you to the penultimate issue for a serial subscription
2274 returns 1 - if this is the penultimate issue
2282 my ($subscriptionid) = @_;
2283 my $dbh = C4::Context->dbh;
2284 my $subscription = GetSubscription($subscriptionid);
2285 my $per = $subscription->{'periodicity'};
2286 if ( $per % 16 > 0 ) {
2287 my $expirationdate = $subscription->{enddate};
2288 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
2289 $sth->execute($subscriptionid);
2290 my ($res) = $sth->fetchrow;
2291 my @res = split( /-/, $res );
2292 @res = Date::Calc::Today if ( $res[0] * $res[1] == 0 );
2293 my @endofsubscriptiondate = split( /-/, $expirationdate );
2295 if ( $per == 1 ) { $x = 7; }
2296 if ( $per == 2 ) { $x = 7; }
2297 if ( $per == 3 ) { $x = 14; }
2298 if ( $per == 4 ) { $x = 21; }
2299 if ( $per == 5 ) { $x = 31; }
2300 if ( $per == 6 ) { $x = 62; }
2301 if ( $per == 7 || $per == 8 ) { $x = 93; }
2302 if ( $per == 9 ) { $x = 190; }
2303 if ( $per == 10 ) { $x = 365; }
2304 if ( $per == 11 ) { $x = 730; }
2305 my @datebeforeend = Add_Delta_Days( $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2], -( 3 * $x ) )
2306 if ( @endofsubscriptiondate && $endofsubscriptiondate[0] * $endofsubscriptiondate[1] * $endofsubscriptiondate[2] );
2308 # warn "DATE BEFORE END: $datebeforeend";
2313 && Delta_Days( $res[0], $res[1], $res[2], $datebeforeend[0], $datebeforeend[1], $datebeforeend[2] ) <= 0 )
2314 && ( @endofsubscriptiondate
2315 && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) >= 0 )
2318 } elsif ( $subscription->{numberlength} > 0 ) {
2319 return ( countissuesfrom( $subscriptionid, $subscription->{'startdate'} ) >= $subscription->{numberlength} - 1 );
2327 ($resultdate) = &GetNextDate($planneddate,$subscription)
2329 this function is an extension of GetNextDate which allows for checking for irregularity
2331 it takes the planneddate and will return the next issue's date and will skip dates if there
2332 exists an irregularity
2333 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2334 skipped then the returned date will be 2007-05-10
2337 $resultdate - then next date in the sequence
2339 Return 0 if periodicity==0
2343 sub in_array { # used in next sub down
2344 my ( $val, @elements ) = @_;
2345 foreach my $elem (@elements) {
2346 if ( $val == $elem ) {
2353 sub GetNextDate(@) {
2354 my ( $planneddate, $subscription ) = @_;
2355 my @irreg = split( /\,/, $subscription->{irregularity} );
2357 #date supposed to be in ISO.
2359 my ( $year, $month, $day ) = split( /-/, $planneddate );
2360 $month = 1 unless ($month);
2361 $day = 1 unless ($day);
2364 # warn "DOW $dayofweek";
2365 if ( $subscription->{periodicity} % 16 == 0 ) { # 'without regularity' || 'irregular'
2370 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2371 # renaming this pattern from 1/day to " n / week ".
2372 if ( $subscription->{periodicity} == 1 ) {
2373 my $dayofweek = eval { Day_of_Week( $year, $month, $day ) };
2374 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2376 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2377 $dayofweek = 0 if ( $dayofweek == 7 );
2378 if ( in_array( ( $dayofweek + 1 ), @irreg ) ) {
2379 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 1 );
2383 @resultdate = Add_Delta_Days( $year, $month, $day, 1 );
2388 if ( $subscription->{periodicity} == 2 ) {
2389 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2390 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2392 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2394 #FIXME: if two consecutive irreg, do we only skip one?
2395 if ( $irreg[$i] == ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 ) ) {
2396 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 7 );
2397 $wkno = ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 );
2400 @resultdate = Add_Delta_Days( $year, $month, $day, 7 );
2405 if ( $subscription->{periodicity} == 3 ) {
2406 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2407 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2409 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2410 if ( $irreg[$i] == ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 ) ) {
2411 ### BUGFIX was previously +1 ^
2412 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 14 );
2413 $wkno = ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 );
2416 @resultdate = Add_Delta_Days( $year, $month, $day, 14 );
2421 if ( $subscription->{periodicity} == 4 ) {
2422 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2423 if ($@) { warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@"; }
2425 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2426 if ( $irreg[$i] == ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 ) ) {
2427 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 21 );
2428 $wkno = ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 );
2431 @resultdate = Add_Delta_Days( $year, $month, $day, 21 );
2434 my $tmpmonth = $month;
2435 if ( $year && $month && $day ) {
2436 if ( $subscription->{periodicity} == 5 ) {
2437 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2438 if ( $irreg[$i] == ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 ) ) {
2439 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2440 $tmpmonth = ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 );
2443 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2445 if ( $subscription->{periodicity} == 6 ) {
2446 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2447 if ( $irreg[$i] == ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 ) ) {
2448 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2449 $tmpmonth = ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 );
2452 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2454 if ( $subscription->{periodicity} == 7 ) {
2455 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2456 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2457 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2458 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2461 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2463 if ( $subscription->{periodicity} == 8 ) {
2464 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2465 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2466 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2467 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2470 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2472 if ( $subscription->{periodicity} == 9 ) {
2473 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2474 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2475 ### BUFIX Seems to need more Than One ?
2476 ( $year, $month, $day ) = Add_Delta_YM( $year, $month, $day, 0, 6 );
2477 $tmpmonth = ( ( $tmpmonth != 6 ) ? ( $tmpmonth + 6 ) % 12 : 12 );
2480 @resultdate = Add_Delta_YM( $year, $month, $day, 0, 6 );
2482 if ( $subscription->{periodicity} == 10 ) {
2483 @resultdate = Add_Delta_YM( $year, $month, $day, 1, 0 );
2485 if ( $subscription->{periodicity} == 11 ) {
2486 @resultdate = Add_Delta_YM( $year, $month, $day, 2, 0 );
2489 my $resultdate = sprintf( "%04d-%02d-%02d", $resultdate[0], $resultdate[1], $resultdate[2] );
2491 return "$resultdate";
2496 $item = &itemdata($barcode);
2498 Looks up the item with the given barcode, and returns a
2499 reference-to-hash containing information about that item. The keys of
2500 the hash are the fields from the C<items> and C<biblioitems> tables in
2508 my $dbh = C4::Context->dbh;
2509 my $sth = $dbh->prepare(
2510 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2513 $sth->execute($barcode);
2514 my $data = $sth->fetchrow_hashref;
2524 Koha Developement team <info@koha.org>