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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use C4::Dates qw(format_date format_date_in_iso);
23 use Date::Calc qw(:all);
24 use POSIX qw(strftime);
32 use C4::Log; # logaction
35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 $VERSION = 3.01; # set version for version checking
42 &NewSubscription &ModSubscription &DelSubscription &GetSubscriptions
43 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
44 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
45 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
47 &GetNextSeq &NewIssue &ItemizeSerials &GetSerials
48 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
49 &ReNewSubscription &GetLateIssues &GetLateOrMissingIssues
50 &GetSerialInformation &AddItem2Serial
51 &PrepareSerialsData &GetNextExpected &ModNextExpected
53 &UpdateClaimdateIssues
54 &GetSuppliersWithLateIssues &getsupplierbyserialid
55 &GetDistributedTo &SetDistributedTo
56 &getroutinglist &delroutingmember &addroutingmember
58 &check_routing &updateClaim &removeMissingIssue
67 C4::Serials - Give functions for serializing.
75 Give all XYZ functions
79 =head2 GetSuppliersWithLateIssues
83 %supplierlist = &GetSuppliersWithLateIssues
85 this function get all suppliers with late issues.
88 an array_ref of suppliers each entry is a hash_ref containing id and name
89 the array is in name order
95 sub GetSuppliersWithLateIssues {
96 my $dbh = C4::Context->dbh;
98 SELECT DISTINCT id, name
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)
106 return $dbh->selectall_arrayref($query, { Slice => {} });
113 @issuelist = &GetLateIssues($supplierid)
115 this function select late issues on database
118 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
119 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
126 my ($supplierid) = @_;
127 my $dbh = C4::Context->dbh;
131 SELECT name,title,planneddate,serialseq,serial.subscriptionid
133 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
134 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
135 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
136 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
137 AND subscription.aqbooksellerid=$supplierid
140 $sth = $dbh->prepare($query);
143 SELECT name,title,planneddate,serialseq,serial.subscriptionid
145 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
146 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
147 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
148 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
151 $sth = $dbh->prepare($query);
157 while ( my $line = $sth->fetchrow_hashref ) {
158 $odd++ unless $line->{title} eq $last_title;
159 $line->{title} = "" if $line->{title} eq $last_title;
160 $last_title = $line->{title} if ( $line->{title} );
161 $line->{planneddate} = format_date( $line->{planneddate} );
162 push @issuelist, $line;
167 =head2 GetSubscriptionHistoryFromSubscriptionId
171 $sth = GetSubscriptionHistoryFromSubscriptionId()
172 this function just prepare the SQL request.
173 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
175 $sth = $dbh->prepare($query).
181 sub GetSubscriptionHistoryFromSubscriptionId() {
182 my $dbh = C4::Context->dbh;
185 FROM subscriptionhistory
186 WHERE subscriptionid = ?
188 return $dbh->prepare($query);
191 =head2 GetSerialStatusFromSerialId
195 $sth = GetSerialStatusFromSerialId();
196 this function just prepare the SQL request.
197 After this function, don't forget to execute it by using $sth->execute($serialid)
199 $sth = $dbh->prepare($query).
205 sub GetSerialStatusFromSerialId() {
206 my $dbh = C4::Context->dbh;
212 return $dbh->prepare($query);
215 =head2 GetSerialInformation
219 $data = GetSerialInformation($serialid);
220 returns a hash containing :
221 items : items marcrecord (can be an array)
223 subscription table field
224 + information about subscription expiration
230 sub GetSerialInformation {
232 my $dbh = C4::Context->dbh;
234 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
235 if ( C4::Context->preference('IndependantBranches')
236 && C4::Context->userenv
237 && C4::Context->userenv->{'flags'} != 1
238 && C4::Context->userenv->{'branch'} ) {
240 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
243 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
246 my $rq = $dbh->prepare($query);
247 $rq->execute($serialid);
248 my $data = $rq->fetchrow_hashref;
250 # create item information if we have serialsadditems for this subscription
251 if ( $data->{'serialsadditems'} ) {
252 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
253 $queryitem->execute($serialid);
254 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
255 if ( scalar(@$itemnumbers) > 0 ) {
256 foreach my $itemnum (@$itemnumbers) {
258 #It is ASSUMED that GetMarcItem ALWAYS WORK...
259 #Maybe GetMarcItem should return values on failure
260 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
261 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0], $data );
262 $itemprocessed->{'itemnumber'} = $itemnum->[0];
263 $itemprocessed->{'itemid'} = $itemnum->[0];
264 $itemprocessed->{'serialid'} = $serialid;
265 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
266 push @{ $data->{'items'} }, $itemprocessed;
269 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
270 $itemprocessed->{'itemid'} = "N$serialid";
271 $itemprocessed->{'serialid'} = $serialid;
272 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
273 $itemprocessed->{'countitems'} = 0;
274 push @{ $data->{'items'} }, $itemprocessed;
277 $data->{ "status" . $data->{'serstatus'} } = 1;
278 $data->{'subscriptionexpired'} = HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
279 $data->{'abouttoexpire'} = abouttoexpire( $data->{'subscriptionid'} );
283 =head2 AddItem2Serial
287 $data = AddItem2Serial($serialid,$itemnumber);
288 Adds an itemnumber to Serial record
295 my ( $serialid, $itemnumber ) = @_;
296 my $dbh = C4::Context->dbh;
297 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
298 $rq->execute( $serialid, $itemnumber );
302 =head2 UpdateClaimdateIssues
306 UpdateClaimdateIssues($serialids,[$date]);
308 Update Claimdate for issues in @$serialids list with date $date
315 sub UpdateClaimdateIssues {
316 my ( $serialids, $date ) = @_;
317 my $dbh = C4::Context->dbh;
318 $date = strftime( "%Y-%m-%d", localtime ) unless ($date);
320 UPDATE serial SET claimdate=$date,status=7
321 WHERE serialid in (" . join( ",", @$serialids ) . ")";
322 my $rq = $dbh->prepare($query);
327 =head2 GetSubscription
331 $subs = GetSubscription($subscriptionid)
332 this function get the subscription which has $subscriptionid as id.
334 a hashref. This hash containts
335 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
341 sub GetSubscription {
342 my ($subscriptionid) = @_;
343 my $dbh = C4::Context->dbh;
345 SELECT subscription.*,
346 subscriptionhistory.*,
347 aqbooksellers.name AS aqbooksellername,
348 biblio.title AS bibliotitle,
349 subscription.biblionumber as bibnum);
350 if ( C4::Context->preference('IndependantBranches')
351 && C4::Context->userenv
352 && C4::Context->userenv->{'flags'} != 1
353 && C4::Context->userenv->{'branch'} ) {
355 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
359 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
360 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
361 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
362 WHERE subscription.subscriptionid = ?
365 # if (C4::Context->preference('IndependantBranches') &&
366 # C4::Context->userenv &&
367 # C4::Context->userenv->{'flags'} != 1){
368 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
369 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
371 $debug and warn "query : $query\nsubsid :$subscriptionid";
372 my $sth = $dbh->prepare($query);
373 $sth->execute($subscriptionid);
374 return $sth->fetchrow_hashref;
377 =head2 GetFullSubscription
381 \@res = GetFullSubscription($subscriptionid)
382 this function read on serial table.
388 sub GetFullSubscription {
389 my ($subscriptionid) = @_;
390 my $dbh = C4::Context->dbh;
392 SELECT serial.serialid,
395 serial.publisheddate,
397 serial.notes as notes,
398 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
399 aqbooksellers.name as aqbooksellername,
400 biblio.title as bibliotitle,
401 subscription.branchcode AS branchcode,
402 subscription.subscriptionid AS subscriptionid |;
403 if ( C4::Context->preference('IndependantBranches')
404 && C4::Context->userenv
405 && C4::Context->userenv->{'flags'} != 1
406 && C4::Context->userenv->{'branch'} ) {
408 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
412 LEFT JOIN subscription ON
413 (serial.subscriptionid=subscription.subscriptionid )
414 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
415 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
416 WHERE serial.subscriptionid = ?
418 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
419 serial.subscriptionid
421 $debug and warn "GetFullSubscription query: $query";
422 my $sth = $dbh->prepare($query);
423 $sth->execute($subscriptionid);
424 return $sth->fetchall_arrayref( {} );
427 =head2 PrepareSerialsData
431 \@res = PrepareSerialsData($serialinfomation)
432 where serialinformation is a hashref array
438 sub PrepareSerialsData {
444 my $aqbooksellername;
448 my $previousnote = "";
450 foreach my $subs (@$lines) {
451 $subs->{'publisheddate'} = (
452 $subs->{'publisheddate'}
453 ? format_date( $subs->{'publisheddate'} )
456 $subs->{'branchname'} = GetBranchName( $subs->{'branchcode'} );
457 $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
458 $subs->{ "status" . $subs->{'status'} } = 1;
459 $subs->{"checked"} = $subs->{'status'} =~ /1|3|4|7/;
461 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
462 $year = $subs->{'year'};
466 if ( $tmpresults{$year} ) {
467 push @{ $tmpresults{$year}->{'serials'} }, $subs;
469 $tmpresults{$year} = {
471 'aqbooksellername' => $subs->{'aqbooksellername'},
472 'bibliotitle' => $subs->{'bibliotitle'},
473 'serials' => [$subs],
478 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
479 push @res, $tmpresults{$key};
481 $res[0]->{'first'} = 1;
485 =head2 GetSubscriptionsFromBiblionumber
487 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
488 this function get the subscription list. it reads on subscription table.
490 table of subscription which has the biblionumber given on input arg.
491 each line of this table is a hashref. All hashes containt
492 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
496 sub GetSubscriptionsFromBiblionumber {
497 my ($biblionumber) = @_;
498 my $dbh = C4::Context->dbh;
500 SELECT subscription.*,
502 subscriptionhistory.*,
503 aqbooksellers.name AS aqbooksellername,
504 biblio.title AS bibliotitle
506 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
507 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
508 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
509 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
510 WHERE subscription.biblionumber = ?
512 my $sth = $dbh->prepare($query);
513 $sth->execute($biblionumber);
515 while ( my $subs = $sth->fetchrow_hashref ) {
516 $subs->{startdate} = format_date( $subs->{startdate} );
517 $subs->{histstartdate} = format_date( $subs->{histstartdate} );
518 $subs->{histenddate} = format_date( $subs->{histenddate} );
519 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
520 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
521 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
522 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
523 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
524 $subs->{ "status" . $subs->{'status'} } = 1;
525 $subs->{'cannotedit'} =
526 ( C4::Context->preference('IndependantBranches')
527 && C4::Context->userenv
528 && C4::Context->userenv->{flags} % 2 != 1
529 && C4::Context->userenv->{branch}
530 && $subs->{branchcode}
531 && ( C4::Context->userenv->{branch} ne $subs->{branchcode} ) );
533 if ( $subs->{enddate} eq '0000-00-00' ) {
534 $subs->{enddate} = '';
536 $subs->{enddate} = format_date( $subs->{enddate} );
538 $subs->{'abouttoexpire'} = abouttoexpire( $subs->{'subscriptionid'} );
539 $subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
545 =head2 GetFullSubscriptionsFromBiblionumber
549 \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
550 this function read on serial table.
556 sub GetFullSubscriptionsFromBiblionumber {
557 my ($biblionumber) = @_;
558 my $dbh = C4::Context->dbh;
560 SELECT serial.serialid,
563 serial.publisheddate,
565 serial.notes as notes,
566 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
567 biblio.title as bibliotitle,
568 subscription.branchcode AS branchcode,
569 subscription.subscriptionid AS subscriptionid|;
570 if ( C4::Context->preference('IndependantBranches')
571 && C4::Context->userenv
572 && C4::Context->userenv->{'flags'} != 1
573 && C4::Context->userenv->{'branch'} ) {
575 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
580 LEFT JOIN subscription ON
581 (serial.subscriptionid=subscription.subscriptionid)
582 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
583 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
584 WHERE subscription.biblionumber = ?
586 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
587 serial.subscriptionid
589 my $sth = $dbh->prepare($query);
590 $sth->execute($biblionumber);
591 return $sth->fetchall_arrayref( {} );
594 =head2 GetSubscriptions
598 @results = GetSubscriptions($title,$ISSN,$biblionumber);
599 this function get all subscriptions which has title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
601 a table of hashref. Each hash containt the subscription.
607 sub GetSubscriptions {
608 my ( $string, $issn, $biblionumber ) = @_;
610 #return unless $title or $ISSN or $biblionumber;
611 my $dbh = C4::Context->dbh;
614 SELECT subscription.*, subscriptionhistory.*, biblio.title,biblioitems.issn,biblio.biblionumber
616 LEFT JOIN subscriptionhistory USING(subscriptionid)
617 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
618 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
623 $sqlwhere = " WHERE biblio.biblionumber=?";
624 push @bind_params, $biblionumber;
628 my @strings_to_search;
629 @strings_to_search = map { "%$_%" } split( / /, $string );
630 foreach my $index qw(biblio.title subscription.callnumber subscription.location subscription.notes subscription.internalnotes) {
631 push @bind_params, @strings_to_search;
632 my $tmpstring = "AND $index LIKE ? " x scalar(@strings_to_search);
633 $debug && warn "$tmpstring";
634 $tmpstring =~ s/^AND //;
635 push @sqlstrings, $tmpstring;
637 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
641 my @strings_to_search;
642 @strings_to_search = map { "%$_%" } split( / /, $issn );
643 foreach my $index qw(biblioitems.issn subscription.callnumber) {
644 push @bind_params, @strings_to_search;
645 my $tmpstring = "OR $index LIKE ? " x scalar(@strings_to_search);
646 $debug && warn "$tmpstring";
647 $tmpstring =~ s/^OR //;
648 push @sqlstrings, $tmpstring;
650 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
652 $sql .= "$sqlwhere ORDER BY title";
653 $debug and warn "GetSubscriptions query: $sql params : ", join( " ", @bind_params );
654 $sth = $dbh->prepare($sql);
655 $sth->execute(@bind_params);
657 my $previoustitle = "";
660 while ( my $line = $sth->fetchrow_hashref ) {
661 if ( $previoustitle eq $line->{title} ) {
665 $previoustitle = $line->{title};
668 $line->{toggle} = 1 if $odd == 1;
669 $line->{'cannotedit'} =
670 ( C4::Context->preference('IndependantBranches')
671 && C4::Context->userenv
672 && C4::Context->userenv->{flags} % 2 != 1
673 && C4::Context->userenv->{branch}
674 && $line->{branchcode}
675 && ( C4::Context->userenv->{branch} ne $line->{branchcode} ) );
676 push @results, $line;
685 ($totalissues,@serials) = GetSerials($subscriptionid);
686 this function get every serial not arrived for a given subscription
687 as well as the number of issues registered in the database (all types)
688 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
690 FIXME: We should return \@serials.
697 my ( $subscriptionid, $count ) = @_;
698 my $dbh = C4::Context->dbh;
700 # status = 2 is "arrived"
702 $count = 5 unless ($count);
704 my $query = "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
706 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
707 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
708 my $sth = $dbh->prepare($query);
709 $sth->execute($subscriptionid);
711 while ( my $line = $sth->fetchrow_hashref ) {
712 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
713 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
714 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
715 push @serials, $line;
718 # OK, now add the last 5 issues arrives/missing
719 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
721 WHERE subscriptionid = ?
722 AND (status in (2,4,5))
723 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
725 $sth = $dbh->prepare($query);
726 $sth->execute($subscriptionid);
727 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
729 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
730 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
731 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
732 push @serials, $line;
735 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
736 $sth = $dbh->prepare($query);
737 $sth->execute($subscriptionid);
738 my ($totalissues) = $sth->fetchrow;
739 return ( $totalissues, @serials );
746 @serials = GetSerials2($subscriptionid,$status);
747 this function gets every serial waited for a given subscription
748 as well as the number of issues registered in the database (all types)
749 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
756 my ( $subscription, $status ) = @_;
757 my $dbh = C4::Context->dbh;
759 SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
761 WHERE subscriptionid=$subscription AND status IN ($status)
762 ORDER BY publisheddate,serialid DESC
764 $debug and warn "GetSerials2 query: $query";
765 my $sth = $dbh->prepare($query);
769 while ( my $line = $sth->fetchrow_hashref ) {
770 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
771 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
772 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
773 push @serials, $line;
778 =head2 GetLatestSerials
782 \@serials = GetLatestSerials($subscriptionid,$limit)
783 get the $limit's latest serials arrived or missing for a given subscription
785 a ref to a table which it containts all of the latest serials stored into a hash.
791 sub GetLatestSerials {
792 my ( $subscriptionid, $limit ) = @_;
793 my $dbh = C4::Context->dbh;
795 # status = 2 is "arrived"
796 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
798 WHERE subscriptionid = ?
799 AND (status =2 or status=4)
800 ORDER BY planneddate DESC LIMIT 0,$limit
802 my $sth = $dbh->prepare($strsth);
803 $sth->execute($subscriptionid);
805 while ( my $line = $sth->fetchrow_hashref ) {
806 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
807 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
808 push @serials, $line;
814 =head2 GetDistributedTo
818 $distributedto=GetDistributedTo($subscriptionid)
819 This function select the old previous value of distributedto in the database.
825 sub GetDistributedTo {
826 my $dbh = C4::Context->dbh;
828 my $subscriptionid = @_;
829 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
830 my $sth = $dbh->prepare($query);
831 $sth->execute($subscriptionid);
832 return ($distributedto) = $sth->fetchrow;
840 $val is a hashref containing all the attributes of the table 'subscription'
841 This function get the next issue for the subscription given on input arg
843 all the input params updated.
851 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
852 # $calculated = $val->{numberingmethod};
853 # # calculate the (expected) value of the next issue recieved.
854 # $newlastvalue1 = $val->{lastvalue1};
855 # # check if we have to increase the new value.
856 # $newinnerloop1 = $val->{innerloop1}+1;
857 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
858 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
859 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
860 # $calculated =~ s/\{X\}/$newlastvalue1/g;
862 # $newlastvalue2 = $val->{lastvalue2};
863 # # check if we have to increase the new value.
864 # $newinnerloop2 = $val->{innerloop2}+1;
865 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
866 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
867 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
868 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
870 # $newlastvalue3 = $val->{lastvalue3};
871 # # check if we have to increase the new value.
872 # $newinnerloop3 = $val->{innerloop3}+1;
873 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
874 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
875 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
876 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
877 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
882 my ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
883 my $pattern = $val->{numberpattern};
884 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
885 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
886 $calculated = $val->{numberingmethod};
887 $newlastvalue1 = $val->{lastvalue1};
888 $newlastvalue2 = $val->{lastvalue2};
889 $newlastvalue3 = $val->{lastvalue3};
890 $newlastvalue1 = $val->{lastvalue1};
892 # check if we have to increase the new value.
893 $newinnerloop1 = $val->{innerloop1} + 1;
894 $newinnerloop1 = 0 if ( $newinnerloop1 >= $val->{every1} );
895 $newlastvalue1 += $val->{add1} if ( $newinnerloop1 < 1 ); # <1 to be true when 0 or empty.
896 $newlastvalue1 = $val->{setto1} if ( $newlastvalue1 > $val->{whenmorethan1} ); # reset counter if needed.
897 $calculated =~ s/\{X\}/$newlastvalue1/g;
899 $newlastvalue2 = $val->{lastvalue2};
901 # check if we have to increase the new value.
902 $newinnerloop2 = $val->{innerloop2} + 1;
903 $newinnerloop2 = 0 if ( $newinnerloop2 >= $val->{every2} );
904 $newlastvalue2 += $val->{add2} if ( $newinnerloop2 < 1 ); # <1 to be true when 0 or empty.
905 $newlastvalue2 = $val->{setto2} if ( $newlastvalue2 > $val->{whenmorethan2} ); # reset counter if needed.
906 if ( $pattern == 6 ) {
907 if ( $val->{hemisphere} == 2 ) {
908 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
909 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
911 my $newlastvalue2seq = $seasons[$newlastvalue2];
912 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
915 $calculated =~ s/\{Y\}/$newlastvalue2/g;
918 $newlastvalue3 = $val->{lastvalue3};
920 # check if we have to increase the new value.
921 $newinnerloop3 = $val->{innerloop3} + 1;
922 $newinnerloop3 = 0 if ( $newinnerloop3 >= $val->{every3} );
923 $newlastvalue3 += $val->{add3} if ( $newinnerloop3 < 1 ); # <1 to be true when 0 or empty.
924 $newlastvalue3 = $val->{setto3} if ( $newlastvalue3 > $val->{whenmorethan3} ); # reset counter if needed.
925 $calculated =~ s/\{Z\}/$newlastvalue3/g;
927 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
934 $calculated = GetSeq($val)
935 $val is a hashref containing all the attributes of the table 'subscription'
936 this function transforms {X},{Y},{Z} to 150,0,0 for example.
938 the sequence in integer format
946 my $pattern = $val->{numberpattern};
947 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
948 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
949 my $calculated = $val->{numberingmethod};
950 my $x = $val->{'lastvalue1'};
951 $calculated =~ s/\{X\}/$x/g;
952 my $newlastvalue2 = $val->{'lastvalue2'};
954 if ( $pattern == 6 ) {
955 if ( $val->{hemisphere} == 2 ) {
956 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
957 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
959 my $newlastvalue2seq = $seasons[$newlastvalue2];
960 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
963 $calculated =~ s/\{Y\}/$newlastvalue2/g;
965 my $z = $val->{'lastvalue3'};
966 $calculated =~ s/\{Z\}/$z/g;
970 =head2 GetExpirationDate
972 $sensddate = GetExpirationDate($subscriptionid)
974 this function return the next expiration date for a subscription given on input args.
981 sub GetExpirationDate {
982 my ( $subscriptionid, $startdate ) = @_;
983 my $dbh = C4::Context->dbh;
984 my $subscription = GetSubscription($subscriptionid);
987 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
988 $enddate = $startdate || $subscription->{startdate};
989 my @date = split( /-/, $enddate );
990 return if ( scalar(@date) != 3 || not check_date(@date) );
991 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
994 if ( my $length = $subscription->{numberlength} ) {
996 #calculate the date of the last issue.
997 for ( my $i = 1 ; $i <= $length ; $i++ ) {
998 $enddate = GetNextDate( $enddate, $subscription );
1000 } elsif ( $subscription->{monthlength} ) {
1001 if ( $$subscription{startdate} ) {
1002 my @enddate = Add_Delta_YM( $date[0], $date[1], $date[2], 0, $subscription->{monthlength} );
1003 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1005 } elsif ( $subscription->{weeklength} ) {
1006 if ( $$subscription{startdate} ) {
1007 my @date = split( /-/, $subscription->{startdate} );
1008 my @enddate = Add_Delta_Days( $date[0], $date[1], $date[2], $subscription->{weeklength} * 7 );
1009 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1018 =head2 CountSubscriptionFromBiblionumber
1022 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1023 this count the number of subscription for a biblionumber given.
1025 the number of subscriptions with biblionumber given on input arg.
1031 sub CountSubscriptionFromBiblionumber {
1032 my ($biblionumber) = @_;
1033 my $dbh = C4::Context->dbh;
1034 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1035 my $sth = $dbh->prepare($query);
1036 $sth->execute($biblionumber);
1037 my $subscriptionsnumber = $sth->fetchrow;
1038 return $subscriptionsnumber;
1041 =head2 ModSubscriptionHistory
1045 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1047 this function modify the history of a subscription. Put your new values on input arg.
1053 sub ModSubscriptionHistory {
1054 my ( $subscriptionid, $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote ) = @_;
1055 my $dbh = C4::Context->dbh;
1056 my $query = "UPDATE subscriptionhistory
1057 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1058 WHERE subscriptionid=?
1060 my $sth = $dbh->prepare($query);
1061 $recievedlist =~ s/^; //;
1062 $missinglist =~ s/^; //;
1063 $opacnote =~ s/^; //;
1064 $sth->execute( $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
1068 =head2 ModSerialStatus
1072 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1074 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1075 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1081 sub ModSerialStatus {
1082 my ( $serialid, $serialseq, $planneddate, $publisheddate, $status, $notes ) = @_;
1084 #It is a usual serial
1085 # 1st, get previous status :
1086 my $dbh = C4::Context->dbh;
1087 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1088 my $sth = $dbh->prepare($query);
1089 $sth->execute($serialid);
1090 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1092 # change status & update subscriptionhistory
1094 if ( $status == 6 ) {
1095 DelIssue( {'serialid'=>$serialid, 'subscriptionid'=>$subscriptionid,'serialseq'=>$serialseq} );
1099 'UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?';
1100 $sth = $dbh->prepare($query);
1101 $sth->execute( $serialseq, $publisheddate, $planneddate, $status, $notes, $serialid );
1102 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1103 $sth = $dbh->prepare($query);
1104 $sth->execute($subscriptionid);
1105 my $val = $sth->fetchrow_hashref;
1106 unless ( $val->{manualhistory} ) {
1107 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1108 $sth = $dbh->prepare($query);
1109 $sth->execute($subscriptionid);
1110 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1111 if ( $status == 2 ) {
1113 $recievedlist .= "; $serialseq"
1114 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1117 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1118 $missinglist .= "; $serialseq"
1120 and not index( "$missinglist", "$serialseq" ) >= 0 );
1121 $missinglist .= "; not issued $serialseq"
1123 and index( "$missinglist", "$serialseq" ) >= 0 );
1124 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1125 $sth = $dbh->prepare($query);
1126 $recievedlist =~ s/^; //;
1127 $missinglist =~ s/^; //;
1128 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1132 # create new waited entry if needed (ie : was a "waited" and has changed)
1133 if ( $oldstatus == 1 && $status != 1 ) {
1134 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1135 $sth = $dbh->prepare($query);
1136 $sth->execute($subscriptionid);
1137 my $val = $sth->fetchrow_hashref;
1141 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
1142 $newinnerloop1, $newinnerloop2, $newinnerloop3
1143 ) = GetNextSeq($val);
1145 # next date (calculated from actual date & frequency parameters)
1146 my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1147 NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate );
1148 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1149 WHERE subscriptionid = ?";
1150 $sth = $dbh->prepare($query);
1151 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1153 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1154 if ( $val->{letter} && $status == 2 && $oldstatus != 2 ) {
1155 SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1160 =head2 GetNextExpected
1164 $nextexpected = GetNextExpected($subscriptionid)
1166 Get the planneddate for the current expected issue of the subscription.
1172 planneddate => C4::Dates object
1179 sub GetNextExpected($) {
1180 my ($subscriptionid) = @_;
1181 my $dbh = C4::Context->dbh;
1182 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1184 # Each subscription has only one 'expected' issue, with serial.status==1.
1185 $sth->execute( $subscriptionid, 1 );
1186 my ( $nextissue ) = $sth->fetchrow_hashref;
1188 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1189 $sth->execute( $subscriptionid );
1190 $nextissue = $sth->fetchrow_hashref;
1192 if (!defined $nextissue->{planneddate}) {
1193 # or should this default to 1st Jan ???
1194 $nextissue->{planneddate} = strftime('%Y-%m-%d',localtime);
1196 $nextissue->{planneddate} = C4::Dates->new($nextissue->{planneddate},'iso');
1201 =head2 ModNextExpected
1205 ModNextExpected($subscriptionid,$date)
1207 Update the planneddate for the current expected issue of the subscription.
1208 This will modify all future prediction results.
1210 C<$date> is a C4::Dates object.
1216 sub ModNextExpected($$) {
1217 my ( $subscriptionid, $date ) = @_;
1218 my $dbh = C4::Context->dbh;
1220 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1221 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1223 # Each subscription has only one 'expected' issue, with serial.status==1.
1224 $sth->execute( $date->output('iso'), $date->output('iso'), $subscriptionid, 1 );
1229 =head2 ModSubscription
1233 this function modify a subscription. Put all new values on input args.
1239 sub ModSubscription {
1240 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1241 $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $add1, $every1,
1242 $whenmorethan1, $setto1, $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2,
1243 $lastvalue2, $innerloop2, $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1244 $numberingmethod, $status, $biblionumber, $callnumber, $notes, $letter, $hemisphere, $manualhistory,
1245 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid
1248 # warn $irregularity;
1249 my $dbh = C4::Context->dbh;
1250 my $query = "UPDATE subscription
1251 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1252 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1253 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1254 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1255 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1256 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?,
1257 letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,
1258 staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1260 WHERE subscriptionid = ?";
1262 #warn "query :".$query;
1263 my $sth = $dbh->prepare($query);
1265 $auser, $branchcode, $aqbooksellerid, $cost,
1266 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1267 $dow, "$irregularity", $numberpattern, $numberlength,
1268 $weeklength, $monthlength, $add1, $every1,
1269 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1270 $add2, $every2, $whenmorethan2, $setto2,
1271 $lastvalue2, $innerloop2, $add3, $every3,
1272 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1273 $numberingmethod, $status, $biblionumber, $callnumber,
1274 $notes, $letter, $hemisphere, ( $manualhistory ? $manualhistory : 0 ),
1275 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1276 $graceperiod, $location, $enddate, $subscriptionid
1278 my $rows = $sth->rows;
1281 logaction( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1285 =head2 NewSubscription
1289 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1290 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1291 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1292 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1293 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1294 $numberingmethod, $status, $notes, $serialsadditems,
1295 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate);
1297 Create a new subscription with value given on input args.
1300 the id of this new subscription
1306 sub NewSubscription {
1307 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1308 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1309 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1310 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, $status,
1311 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1312 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1314 my $dbh = C4::Context->dbh;
1316 #save subscription (insert into database)
1318 INSERT INTO subscription
1319 (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1320 startdate,periodicity,dow,numberlength,weeklength,monthlength,
1321 add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1322 add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1323 add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1324 numberingmethod, status, notes, letter,firstacquidate,irregularity,
1325 numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
1326 staffdisplaycount,opacdisplaycount,graceperiod,location,enddate)
1327 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1329 my $sth = $dbh->prepare($query);
1331 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1332 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1333 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1334 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, "$status",
1335 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1336 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1339 #then create the 1st waited number
1340 my $subscriptionid = $dbh->{'mysql_insertid'};
1342 INSERT INTO subscriptionhistory
1343 (biblionumber, subscriptionid, histstartdate, opacnote, librariannote)
1346 $sth = $dbh->prepare($query);
1347 $sth->execute( $biblionumber, $subscriptionid, $startdate, $notes, $internalnotes );
1349 # reread subscription to get a hash (for calculation of the 1st issue number)
1353 WHERE subscriptionid = ?
1355 $sth = $dbh->prepare($query);
1356 $sth->execute($subscriptionid);
1357 my $val = $sth->fetchrow_hashref;
1359 # calculate issue number
1360 my $serialseq = GetSeq($val);
1363 (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1364 VALUES (?,?,?,?,?,?)
1366 $sth = $dbh->prepare($query);
1367 $sth->execute( "$serialseq", $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
1369 logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1371 #set serial flag on biblio if not already set.
1372 my ( $null, ($bib) ) = GetBiblio($biblionumber);
1373 if ( !$bib->{'serial'} ) {
1374 my $record = GetMarcBiblio($biblionumber);
1375 my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $bib->{'frameworkcode'} );
1377 eval { $record->field($tag)->update( $subf => 1 ); };
1379 ModBiblio( $record, $biblionumber, $bib->{'frameworkcode'} );
1381 return $subscriptionid;
1384 =head2 ReNewSubscription
1388 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1390 this function renew a subscription with values given on input args.
1396 sub ReNewSubscription {
1397 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1398 my $dbh = C4::Context->dbh;
1399 my $subscription = GetSubscription($subscriptionid);
1403 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1404 WHERE biblio.biblionumber=?
1406 my $sth = $dbh->prepare($query);
1407 $sth->execute( $subscription->{biblionumber} );
1408 my $biblio = $sth->fetchrow_hashref;
1410 if ( C4::Context->preference("RenewSerialAddsSuggestion") ) {
1413 { 'suggestedby' => $user,
1414 'title' => $subscription->{bibliotitle},
1415 'author' => $biblio->{author},
1416 'publishercode' => $biblio->{publishercode},
1417 'note' => $biblio->{note},
1418 'biblionumber' => $subscription->{biblionumber}
1423 # renew subscription
1426 SET startdate=?,numberlength=?,weeklength=?,monthlength=?
1427 WHERE subscriptionid=?
1429 $sth = $dbh->prepare($query);
1430 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1431 my $enddate = GetExpirationDate($subscriptionid);
1432 $debug && warn "enddate :$enddate";
1436 WHERE subscriptionid=?
1438 $sth = $dbh->prepare($query);
1439 $sth->execute( $enddate, $subscriptionid );
1441 UPDATE subscriptionhistory
1443 WHERE subscriptionid=?
1445 $sth = $dbh->prepare($query);
1446 $sth->execute( $enddate, $subscriptionid );
1448 logaction( "SERIAL", "RENEW", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1455 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1457 Create a new issue stored on the database.
1458 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1465 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
1466 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1468 my $dbh = C4::Context->dbh;
1471 (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1472 VALUES (?,?,?,?,?,?,?)
1474 my $sth = $dbh->prepare($query);
1475 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
1476 my $serialid = $dbh->{'mysql_insertid'};
1478 SELECT missinglist,recievedlist
1479 FROM subscriptionhistory
1480 WHERE subscriptionid=?
1482 $sth = $dbh->prepare($query);
1483 $sth->execute($subscriptionid);
1484 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1486 if ( $status == 2 ) {
1487 ### TODO Add a feature that improves recognition and description.
1488 ### As such count (serialseq) i.e. : N18,2(N19),N20
1489 ### Would use substr and index But be careful to previous presence of ()
1490 $recievedlist .= "; $serialseq" unless (index($recievedlist,$serialseq)>0);
1492 if ( $status == 4 ) {
1493 $missinglist .= "; $serialseq" unless (index($missinglist,$serialseq)>0);
1496 UPDATE subscriptionhistory
1497 SET recievedlist=?, missinglist=?
1498 WHERE subscriptionid=?
1500 $sth = $dbh->prepare($query);
1501 $recievedlist =~ s/^; //;
1502 $missinglist =~ s/^; //;
1503 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1507 =head2 ItemizeSerials
1511 ItemizeSerials($serialid, $info);
1512 $info is a hashref containing barcode branch, itemcallnumber, status, location
1513 $serialid the serialid
1515 1 if the itemize is a succes.
1516 0 and @error else. @error containts the list of errors found.
1522 sub ItemizeSerials {
1523 my ( $serialid, $info ) = @_;
1524 my $now = POSIX::strftime( "%Y-%m-%d", localtime );
1526 my $dbh = C4::Context->dbh;
1532 my $sth = $dbh->prepare($query);
1533 $sth->execute($serialid);
1534 my $data = $sth->fetchrow_hashref;
1535 if ( C4::Context->preference("RoutingSerials") ) {
1537 # check for existing biblioitem relating to serial issue
1538 my ( $count, @results ) = GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1540 for ( my $i = 0 ; $i < $count ; $i++ ) {
1541 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' (' . $data->{'planneddate'} . ')' ) {
1542 $bibitemno = $results[$i]->{'biblioitemnumber'};
1546 if ( $bibitemno == 0 ) {
1547 my $sth = $dbh->prepare( "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC" );
1548 $sth->execute( $data->{'biblionumber'} );
1549 my $biblioitem = $sth->fetchrow_hashref;
1550 $biblioitem->{'volumedate'} = $data->{planneddate};
1551 $biblioitem->{'volumeddesc'} = $data->{serialseq} . ' (' . format_date( $data->{'planneddate'} ) . ')';
1552 $biblioitem->{'dewey'} = $info->{itemcallnumber};
1556 my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1557 if ( $info->{barcode} ) {
1559 my $exists = itemdata( $info->{'barcode'} );
1560 push @errors, "barcode_not_unique" if ($exists);
1562 my $marcrecord = MARC::Record->new();
1563 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.barcode", $fwk );
1564 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{barcode} );
1565 $marcrecord->insert_fields_ordered($newField);
1566 if ( $info->{branch} ) {
1567 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.homebranch", $fwk );
1569 #warn "items.homebranch : $tag , $subfield";
1570 if ( $marcrecord->field($tag) ) {
1571 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1573 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1574 $marcrecord->insert_fields_ordered($newField);
1576 ( $tag, $subfield ) = GetMarcFromKohaField( "items.holdingbranch", $fwk );
1578 #warn "items.holdingbranch : $tag , $subfield";
1579 if ( $marcrecord->field($tag) ) {
1580 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1582 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1583 $marcrecord->insert_fields_ordered($newField);
1586 if ( $info->{itemcallnumber} ) {
1587 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemcallnumber", $fwk );
1589 if ( $marcrecord->field($tag) ) {
1590 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{itemcallnumber} );
1592 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{itemcallnumber} );
1593 $marcrecord->insert_fields_ordered($newField);
1596 if ( $info->{notes} ) {
1597 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemnotes", $fwk );
1599 if ( $marcrecord->field($tag) ) {
1600 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{notes} );
1602 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{notes} );
1603 $marcrecord->insert_fields_ordered($newField);
1606 if ( $info->{location} ) {
1607 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.location", $fwk );
1609 if ( $marcrecord->field($tag) ) {
1610 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{location} );
1612 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{location} );
1613 $marcrecord->insert_fields_ordered($newField);
1616 if ( $info->{status} ) {
1617 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.notforloan", $fwk );
1619 if ( $marcrecord->field($tag) ) {
1620 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{status} );
1622 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{status} );
1623 $marcrecord->insert_fields_ordered($newField);
1626 if ( C4::Context->preference("RoutingSerials") ) {
1627 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.dateaccessioned", $fwk );
1628 if ( $marcrecord->field($tag) ) {
1629 $marcrecord->field($tag)->add_subfields( "$subfield" => $now );
1631 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1632 $marcrecord->insert_fields_ordered($newField);
1635 AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1638 return ( 0, @errors );
1642 =head2 HasSubscriptionStrictlyExpired
1646 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1648 the subscription has stricly expired when today > the end subscription date
1651 1 if true, 0 if false, -1 if the expiration date is not set.
1657 sub HasSubscriptionStrictlyExpired {
1659 # Getting end of subscription date
1660 my ($subscriptionid) = @_;
1661 my $dbh = C4::Context->dbh;
1662 my $subscription = GetSubscription($subscriptionid);
1663 my $expirationdate = $subscription->{enddate} || GetExpirationDate($subscriptionid);
1665 # If the expiration date is set
1666 if ( $expirationdate != 0 ) {
1667 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1669 # Getting today's date
1670 my ( $nowyear, $nowmonth, $nowday ) = Today();
1672 # if today's date > expiration date, then the subscription has stricly expired
1673 if ( Delta_Days( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1680 # There are some cases where the expiration date is not set
1681 # As we can't determine if the subscription has expired on a date-basis,
1687 =head2 HasSubscriptionExpired
1691 $has_expired = HasSubscriptionExpired($subscriptionid)
1693 the subscription has expired when the next issue to arrive is out of subscription limit.
1696 0 if the subscription has not expired
1697 1 if the subscription has expired
1698 2 if has subscription does not have a valid expiration date set
1704 sub HasSubscriptionExpired {
1705 my ($subscriptionid) = @_;
1706 my $dbh = C4::Context->dbh;
1707 my $subscription = GetSubscription($subscriptionid);
1708 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
1709 my $expirationdate = $subscription->{enddate};
1711 SELECT max(planneddate)
1713 WHERE subscriptionid=?
1715 my $sth = $dbh->prepare($query);
1716 $sth->execute($subscriptionid);
1717 my ($res) = $sth->fetchrow;
1718 return 0 unless $res;
1719 my @res = split( /-/, $res );
1720 my @endofsubscriptiondate = split( /-/, $expirationdate );
1721 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date(@res) || not check_date(@endofsubscriptiondate) );
1723 if ( ( @endofsubscriptiondate && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1727 if ( $subscription->{'numberlength'} ) {
1728 my $countreceived = countissuesfrom( $subscriptionid, $subscription->{'startdate'} );
1729 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1735 return 0; # Notice that you'll never get here.
1738 =head2 SetDistributedto
1742 SetDistributedto($distributedto,$subscriptionid);
1743 This function update the value of distributedto for a subscription given on input arg.
1749 sub SetDistributedto {
1750 my ( $distributedto, $subscriptionid ) = @_;
1751 my $dbh = C4::Context->dbh;
1755 WHERE subscriptionid=?
1757 my $sth = $dbh->prepare($query);
1758 $sth->execute( $distributedto, $subscriptionid );
1761 =head2 DelSubscription
1765 DelSubscription($subscriptionid)
1766 this function delete the subscription which has $subscriptionid as id.
1772 sub DelSubscription {
1773 my ($subscriptionid) = @_;
1774 my $dbh = C4::Context->dbh;
1775 $subscriptionid = $dbh->quote($subscriptionid);
1776 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1777 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1778 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1780 logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1787 DelIssue($serialseq,$subscriptionid)
1788 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1795 my ($dataissue) = @_;
1796 my $dbh = C4::Context->dbh;
1797 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1802 AND subscriptionid= ?
1804 my $mainsth = $dbh->prepare($query);
1805 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1807 #Delete element from subscription history
1808 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1809 my $sth = $dbh->prepare($query);
1810 $sth->execute( $dataissue->{'subscriptionid'} );
1811 my $val = $sth->fetchrow_hashref;
1812 unless ( $val->{manualhistory} ) {
1814 SELECT * FROM subscriptionhistory
1815 WHERE subscriptionid= ?
1817 my $sth = $dbh->prepare($query);
1818 $sth->execute( $dataissue->{'subscriptionid'} );
1819 my $data = $sth->fetchrow_hashref;
1820 my $serialseq = $dataissue->{'serialseq'};
1821 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1822 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1823 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1824 $sth = $dbh->prepare($strsth);
1825 $sth->execute( $dataissue->{'subscriptionid'} );
1828 return $mainsth->rows;
1831 =head2 GetLateOrMissingIssues
1835 @issuelist = &GetLateMissingIssues($supplierid,$serialid)
1837 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1840 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1841 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1847 sub GetLateOrMissingIssues {
1848 my ( $supplierid, $serialid, $order ) = @_;
1849 my $dbh = C4::Context->dbh;
1853 $byserial = "and serialid = " . $serialid;
1856 $order .= ", title";
1861 $sth = $dbh->prepare(
1863 serialid, aqbooksellerid, name,
1864 biblio.title, planneddate, serialseq,
1865 serial.status, serial.subscriptionid, claimdate
1867 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1868 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1869 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1870 WHERE subscription.subscriptionid = serial.subscriptionid
1871 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1872 AND subscription.aqbooksellerid=$supplierid
1877 $sth = $dbh->prepare(
1879 serialid, aqbooksellerid, name,
1880 biblio.title, planneddate, serialseq,
1881 serial.status, serial.subscriptionid, claimdate
1883 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1884 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1885 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1886 WHERE subscription.subscriptionid = serial.subscriptionid
1887 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1894 while ( my $line = $sth->fetchrow_hashref ) {
1895 if ($line->{planneddate}) {
1896 $line->{planneddate} = format_date( $line->{planneddate} );
1898 if ($line->{claimdate}) {
1899 $line->{claimdate} = format_date( $line->{claimdate} );
1901 $line->{"status".$line->{status}} = 1;
1902 push @issuelist, $line;
1907 =head2 removeMissingIssue
1911 removeMissingIssue($subscriptionid)
1913 this function removes an issue from being part of the missing string in
1914 subscriptionlist.missinglist column
1916 called when a missing issue is found from the serials-recieve.pl file
1922 sub removeMissingIssue {
1923 my ( $sequence, $subscriptionid ) = @_;
1924 my $dbh = C4::Context->dbh;
1925 my $sth = $dbh->prepare("SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1926 $sth->execute($subscriptionid);
1927 my $data = $sth->fetchrow_hashref;
1928 my $missinglist = $data->{'missinglist'};
1929 my $missinglistbefore = $missinglist;
1931 # warn $missinglist." before";
1932 $missinglist =~ s/($sequence)//;
1934 # warn $missinglist." after";
1935 if ( $missinglist ne $missinglistbefore ) {
1936 $missinglist =~ s/\|\s\|/\|/g;
1937 $missinglist =~ s/^\| //g;
1938 $missinglist =~ s/\|$//g;
1939 my $sth2 = $dbh->prepare(
1940 "UPDATE subscriptionhistory
1942 WHERE subscriptionid = ?"
1944 $sth2->execute( $missinglist, $subscriptionid );
1952 &updateClaim($serialid)
1954 this function updates the time when a claim is issued for late/missing items
1956 called from claims.pl file
1963 my ($serialid) = @_;
1964 my $dbh = C4::Context->dbh;
1965 my $sth = $dbh->prepare(
1966 "UPDATE serial SET claimdate = now()
1970 $sth->execute($serialid);
1973 =head2 getsupplierbyserialid
1977 ($result) = &getsupplierbyserialid($serialid)
1979 this function is used to find the supplier id given a serial id
1982 hashref containing serialid, subscriptionid, and aqbooksellerid
1988 sub getsupplierbyserialid {
1989 my ($serialid) = @_;
1990 my $dbh = C4::Context->dbh;
1991 my $sth = $dbh->prepare(
1992 "SELECT serialid, serial.subscriptionid, aqbooksellerid
1994 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
1998 $sth->execute($serialid);
1999 my $line = $sth->fetchrow_hashref;
2000 my $result = $line->{'aqbooksellerid'};
2004 =head2 check_routing
2008 ($result) = &check_routing($subscriptionid)
2010 this function checks to see if a serial has a routing list and returns the count of routingid
2011 used to show either an 'add' or 'edit' link
2018 my ($subscriptionid) = @_;
2019 my $dbh = C4::Context->dbh;
2020 my $sth = $dbh->prepare(
2021 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2022 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2023 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2026 $sth->execute($subscriptionid);
2027 my $line = $sth->fetchrow_hashref;
2028 my $result = $line->{'routingids'};
2032 =head2 addroutingmember
2036 &addroutingmember($borrowernumber,$subscriptionid)
2038 this function takes a borrowernumber and subscriptionid and add the member to the
2039 routing list for that serial subscription and gives them a rank on the list
2040 of either 1 or highest current rank + 1
2046 sub addroutingmember {
2047 my ( $borrowernumber, $subscriptionid ) = @_;
2049 my $dbh = C4::Context->dbh;
2050 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
2051 $sth->execute($subscriptionid);
2052 while ( my $line = $sth->fetchrow_hashref ) {
2053 if ( $line->{'rank'} > 0 ) {
2054 $rank = $line->{'rank'} + 1;
2059 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
2060 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2063 =head2 reorder_members
2067 &reorder_members($subscriptionid,$routingid,$rank)
2069 this function is used to reorder the routing list
2071 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2072 - it gets all members on list puts their routingid's into an array
2073 - removes the one in the array that is $routingid
2074 - then reinjects $routingid at point indicated by $rank
2075 - then update the database with the routingids in the new order
2081 sub reorder_members {
2082 my ( $subscriptionid, $routingid, $rank ) = @_;
2083 my $dbh = C4::Context->dbh;
2084 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
2085 $sth->execute($subscriptionid);
2087 while ( my $line = $sth->fetchrow_hashref ) {
2088 push( @result, $line->{'routingid'} );
2091 # To find the matching index
2093 my $key = -1; # to allow for 0 being a valid response
2094 for ( $i = 0 ; $i < @result ; $i++ ) {
2095 if ( $routingid == $result[$i] ) {
2096 $key = $i; # save the index
2101 # if index exists in array then move it to new position
2102 if ( $key > -1 && $rank > 0 ) {
2103 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
2104 my $moving_item = splice( @result, $key, 1 );
2105 splice( @result, $new_rank, 0, $moving_item );
2107 for ( my $j = 0 ; $j < @result ; $j++ ) {
2108 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
2113 =head2 delroutingmember
2117 &delroutingmember($routingid,$subscriptionid)
2119 this function either deletes one member from routing list if $routingid exists otherwise
2120 deletes all members from the routing list
2126 sub delroutingmember {
2128 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2129 my ( $routingid, $subscriptionid ) = @_;
2130 my $dbh = C4::Context->dbh;
2132 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2133 $sth->execute($routingid);
2134 reorder_members( $subscriptionid, $routingid );
2136 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2137 $sth->execute($subscriptionid);
2141 =head2 getroutinglist
2145 ($count,@routinglist) = &getroutinglist($subscriptionid)
2147 this gets the info from the subscriptionroutinglist for $subscriptionid
2150 a count of the number of members on routinglist
2151 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2152 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2158 sub getroutinglist {
2159 my ($subscriptionid) = @_;
2160 my $dbh = C4::Context->dbh;
2161 my $sth = $dbh->prepare(
2162 "SELECT routingid, borrowernumber, ranking, biblionumber
2164 LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2165 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2168 $sth->execute($subscriptionid);
2171 while ( my $line = $sth->fetchrow_hashref ) {
2173 push( @routinglist, $line );
2175 return ( $count, @routinglist );
2178 =head2 countissuesfrom
2182 $result = &countissuesfrom($subscriptionid,$startdate)
2189 sub countissuesfrom {
2190 my ( $subscriptionid, $startdate ) = @_;
2191 my $dbh = C4::Context->dbh;
2195 WHERE subscriptionid=?
2196 AND serial.publisheddate>?
2198 my $sth = $dbh->prepare($query);
2199 $sth->execute( $subscriptionid, $startdate );
2200 my ($countreceived) = $sth->fetchrow;
2201 return $countreceived;
2208 $result = &CountIssues($subscriptionid)
2216 my ($subscriptionid) = @_;
2217 my $dbh = C4::Context->dbh;
2221 WHERE subscriptionid=?
2223 my $sth = $dbh->prepare($query);
2224 $sth->execute($subscriptionid);
2225 my ($countreceived) = $sth->fetchrow;
2226 return $countreceived;
2233 $result = &HasItems($subscriptionid)
2241 my ($subscriptionid) = @_;
2242 my $dbh = C4::Context->dbh;
2244 SELECT COUNT(serialitems.itemnumber)
2246 LEFT JOIN serialitems USING(serialid)
2247 WHERE subscriptionid=? AND serialitems.serialid NOT NULL
2249 my $sth=$dbh->prepare($query);
2250 $sth->execute($subscriptionid);
2251 my ($countitems)=$sth->fetchrow;
2255 =head2 abouttoexpire
2259 $result = &abouttoexpire($subscriptionid)
2261 this function alerts you to the penultimate issue for a serial subscription
2263 returns 1 - if this is the penultimate issue
2271 my ($subscriptionid) = @_;
2272 my $dbh = C4::Context->dbh;
2273 my $subscription = GetSubscription($subscriptionid);
2274 my $per = $subscription->{'periodicity'};
2275 if ($per && $per % 16 > 0){
2276 my $expirationdate = GetExpirationDate($subscriptionid);
2277 my ($res) = $dbh->selectrow_array('select max(planneddate) from serial where subscriptionid = ?', undef, $subscriptionid);
2280 @res=split (/-/,$res);
2281 @res=Date::Calc::Today if ($res[0]*$res[1]==0);
2282 } else { # default an undefined value
2283 @res=Date::Calc::Today;
2285 my @endofsubscriptiondate=split(/-/,$expirationdate);
2286 my @per_list = (0, 7, 7, 14, 21, 31, 62, 93, 93, 190, 365, 730, 0, 0, 0, 0);
2288 @datebeforeend = Add_Delta_Days( $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2],
2289 - (3 * $per_list[$per])) if (@endofsubscriptiondate && $endofsubscriptiondate[0]*$endofsubscriptiondate[1]*$endofsubscriptiondate[2]);
2290 return 1 if ( @res &&
2292 Delta_Days($res[0],$res[1],$res[2],
2293 $datebeforeend[0],$datebeforeend[1],$datebeforeend[2]) <= 0) &&
2294 (@endofsubscriptiondate &&
2295 Delta_Days($res[0],$res[1],$res[2],
2296 $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) >= 0) );
2298 } elsif ($subscription->{numberlength}>0) {
2299 return (countissuesfrom($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength}-1);
2306 ($resultdate) = &GetNextDate($planneddate,$subscription)
2308 this function is an extension of GetNextDate which allows for checking for irregularity
2310 it takes the planneddate and will return the next issue's date and will skip dates if there
2311 exists an irregularity
2312 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2313 skipped then the returned date will be 2007-05-10
2316 $resultdate - then next date in the sequence
2318 Return 0 if periodicity==0
2322 sub in_array { # used in next sub down
2323 my ( $val, @elements ) = @_;
2324 foreach my $elem (@elements) {
2325 if ( $val == $elem ) {
2332 sub GetNextDate(@) {
2333 my ( $planneddate, $subscription ) = @_;
2334 my @irreg = split( /\,/, $subscription->{irregularity} );
2336 #date supposed to be in ISO.
2338 my ( $year, $month, $day ) = split( /-/, $planneddate );
2339 $month = 1 unless ($month);
2340 $day = 1 unless ($day);
2343 # warn "DOW $dayofweek";
2344 if ( $subscription->{periodicity} % 16 == 0 ) { # 'without regularity' || 'irregular'
2349 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2350 # renaming this pattern from 1/day to " n / week ".
2351 if ( $subscription->{periodicity} == 1 ) {
2352 my $dayofweek = eval { Day_of_Week( $year, $month, $day ) };
2353 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2355 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2356 $dayofweek = 0 if ( $dayofweek == 7 );
2357 if ( in_array( ( $dayofweek + 1 ), @irreg ) ) {
2358 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 1 );
2362 @resultdate = Add_Delta_Days( $year, $month, $day, 1 );
2367 if ( $subscription->{periodicity} == 2 ) {
2368 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2369 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2371 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2373 #FIXME: if two consecutive irreg, do we only skip one?
2374 if ( $irreg[$i] == ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 ) ) {
2375 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 7 );
2376 $wkno = ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 );
2379 @resultdate = Add_Delta_Days( $year, $month, $day, 7 );
2384 if ( $subscription->{periodicity} == 3 ) {
2385 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2386 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2388 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2389 if ( $irreg[$i] == ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 ) ) {
2390 ### BUGFIX was previously +1 ^
2391 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 14 );
2392 $wkno = ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 );
2395 @resultdate = Add_Delta_Days( $year, $month, $day, 14 );
2400 if ( $subscription->{periodicity} == 4 ) {
2401 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2402 if ($@) { warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@"; }
2404 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2405 if ( $irreg[$i] == ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 ) ) {
2406 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 21 );
2407 $wkno = ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 );
2410 @resultdate = Add_Delta_Days( $year, $month, $day, 21 );
2413 my $tmpmonth = $month;
2414 if ( $year && $month && $day ) {
2415 if ( $subscription->{periodicity} == 5 ) {
2416 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2417 if ( $irreg[$i] == ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 ) ) {
2418 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2419 $tmpmonth = ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 );
2422 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2424 if ( $subscription->{periodicity} == 6 ) {
2425 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2426 if ( $irreg[$i] == ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 ) ) {
2427 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2428 $tmpmonth = ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 );
2431 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2433 if ( $subscription->{periodicity} == 7 ) {
2434 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2435 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2436 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2437 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2440 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2442 if ( $subscription->{periodicity} == 8 ) {
2443 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2444 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2445 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2446 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2449 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2451 if ( $subscription->{periodicity} == 9 ) {
2452 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2453 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2454 ### BUFIX Seems to need more Than One ?
2455 ( $year, $month, $day ) = Add_Delta_YM( $year, $month, $day, 0, 6 );
2456 $tmpmonth = ( ( $tmpmonth != 6 ) ? ( $tmpmonth + 6 ) % 12 : 12 );
2459 @resultdate = Add_Delta_YM( $year, $month, $day, 0, 6 );
2461 if ( $subscription->{periodicity} == 10 ) {
2462 @resultdate = Add_Delta_YM( $year, $month, $day, 1, 0 );
2464 if ( $subscription->{periodicity} == 11 ) {
2465 @resultdate = Add_Delta_YM( $year, $month, $day, 2, 0 );
2468 my $resultdate = sprintf( "%04d-%02d-%02d", $resultdate[0], $resultdate[1], $resultdate[2] );
2470 return "$resultdate";
2475 $item = &itemdata($barcode);
2477 Looks up the item with the given barcode, and returns a
2478 reference-to-hash containing information about that item. The keys of
2479 the hash are the fields from the C<items> and C<biblioitems> tables in
2487 my $dbh = C4::Context->dbh;
2488 my $sth = $dbh->prepare(
2489 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2492 $sth->execute($barcode);
2493 my $data = $sth->fetchrow_hashref;
2503 Koha Developement team <info@koha.org>