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
22 use C4::Dates qw(format_date);
23 use Date::Calc qw(:all);
24 use POSIX qw(strftime);
31 use C4::Log; # logaction
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37 $VERSION = 3.01; # set version for version checking
41 &NewSubscription &ModSubscription &DelSubscription &GetSubscriptions
42 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
43 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
44 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
46 &GetNextSeq &NewIssue &ItemizeSerials &GetSerials
47 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
48 &ReNewSubscription &GetLateIssues &GetLateOrMissingIssues
49 &GetSerialInformation &AddItem2Serial
50 &PrepareSerialsData &GetNextExpected &ModNextExpected
52 &UpdateClaimdateIssues
53 &GetSuppliersWithLateIssues &getsupplierbyserialid
54 &GetDistributedTo &SetDistributedTo
55 &getroutinglist &delroutingmember &addroutingmember
57 &check_routing &updateClaim &removeMissingIssue
63 =head2 GetSuppliersWithLateIssues
67 C4::Serials - Give functions for serializing.
75 Give all XYZ functions
81 %supplierlist = &GetSuppliersWithLateIssues
83 this function get all suppliers with late issues.
86 the supplierlist into a hash. this hash containts id & name of the supplier
92 sub GetSuppliersWithLateIssues {
93 my $dbh = C4::Context->dbh;
95 SELECT DISTINCT id, name
97 LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
98 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
99 WHERE subscription.subscriptionid = serial.subscriptionid
100 AND (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4)
103 my $sth = $dbh->prepare($query);
106 while ( my ( $id, $name ) = $sth->fetchrow ) {
107 $supplierlist{$id} = $name;
109 return %supplierlist;
116 @issuelist = &GetLateIssues($supplierid)
118 this function select late issues on database
121 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
122 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
129 my ($supplierid) = @_;
130 my $dbh = C4::Context->dbh;
134 SELECT name,title,planneddate,serialseq,serial.subscriptionid
136 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
137 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
138 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
139 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
140 AND subscription.aqbooksellerid=$supplierid
143 $sth = $dbh->prepare($query);
147 SELECT name,title,planneddate,serialseq,serial.subscriptionid
149 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
150 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
151 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
152 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
155 $sth = $dbh->prepare($query);
162 while ( my $line = $sth->fetchrow_hashref ) {
163 $odd++ unless $line->{title} eq $last_title;
164 $line->{title} = "" if $line->{title} eq $last_title;
165 $last_title = $line->{title} if ( $line->{title} );
166 $line->{planneddate} = format_date( $line->{planneddate} );
168 push @issuelist, $line;
170 return $count, @issuelist;
173 =head2 GetSubscriptionHistoryFromSubscriptionId
177 $sth = GetSubscriptionHistoryFromSubscriptionId()
178 this function just prepare the SQL request.
179 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
181 $sth = $dbh->prepare($query).
187 sub GetSubscriptionHistoryFromSubscriptionId() {
188 my $dbh = C4::Context->dbh;
191 FROM subscriptionhistory
192 WHERE subscriptionid = ?
194 return $dbh->prepare($query);
197 =head2 GetSerialStatusFromSerialId
201 $sth = GetSerialStatusFromSerialId();
202 this function just prepare the SQL request.
203 After this function, don't forget to execute it by using $sth->execute($serialid)
205 $sth = $dbh->prepare($query).
211 sub GetSerialStatusFromSerialId() {
212 my $dbh = C4::Context->dbh;
218 return $dbh->prepare($query);
221 =head2 GetSerialInformation
225 $data = GetSerialInformation($serialid);
226 returns a hash containing :
227 items : items marcrecord (can be an array)
229 subscription table field
230 + information about subscription expiration
236 sub GetSerialInformation {
238 my $dbh = C4::Context->dbh;
240 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
241 if (C4::Context->preference('IndependantBranches') &&
242 C4::Context->userenv &&
243 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
245 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
248 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
251 my $rq = $dbh->prepare($query);
252 $rq->execute($serialid);
253 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) {
261 #It is ASSUMED that GetMarcItem ALWAYS WORK...
262 #Maybe GetMarcItem should return values on failure
263 $debug and warn "itemnumber :$itemnum->[0], bibnum :".$data->{'biblionumber'};
265 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;
275 PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
276 $itemprocessed->{'itemid'} = "N$serialid";
277 $itemprocessed->{'serialid'} = $serialid;
278 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
279 $itemprocessed->{'countitems'} = 0;
280 push @{ $data->{'items'} }, $itemprocessed;
283 $data->{ "status" . $data->{'serstatus'} } = 1;
284 $data->{'subscriptionexpired'} =
285 HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'}==1;
286 $data->{'abouttoexpire'} =
287 abouttoexpire( $data->{'subscriptionid'} );
291 =head2 AddItem2Serial
295 $data = AddItem2Serial($serialid,$itemnumber);
296 Adds an itemnumber to Serial record
303 my ( $serialid, $itemnumber ) = @_;
304 my $dbh = C4::Context->dbh;
305 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
306 $rq->execute($serialid, $itemnumber);
310 =head2 UpdateClaimdateIssues
314 UpdateClaimdateIssues($serialids,[$date]);
316 Update Claimdate for issues in @$serialids list with date $date
323 sub UpdateClaimdateIssues {
324 my ( $serialids, $date ) = @_;
325 my $dbh = C4::Context->dbh;
326 $date = strftime("%Y-%m-%d",localtime) unless ($date);
328 UPDATE serial SET claimdate=$date,status=7
329 WHERE serialid in (".join (",",@$serialids) .")";
330 my $rq = $dbh->prepare($query);
335 =head2 GetSubscription
339 $subs = GetSubscription($subscriptionid)
340 this function get the subscription which has $subscriptionid as id.
342 a hashref. This hash containts
343 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
349 sub GetSubscription {
350 my ($subscriptionid) = @_;
351 my $dbh = C4::Context->dbh;
353 SELECT subscription.*,
354 subscriptionhistory.*,
355 aqbooksellers.name AS aqbooksellername,
356 biblio.title AS bibliotitle,
357 subscription.biblionumber as bibnum);
358 if (C4::Context->preference('IndependantBranches') &&
359 C4::Context->userenv &&
360 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
362 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
366 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
367 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
368 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
369 WHERE subscription.subscriptionid = ?
371 # if (C4::Context->preference('IndependantBranches') &&
372 # C4::Context->userenv &&
373 # C4::Context->userenv->{'flags'} != 1){
374 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
375 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
377 $debug and warn "query : $query\nsubsid :$subscriptionid";
378 my $sth = $dbh->prepare($query);
379 $sth->execute($subscriptionid);
380 return $sth->fetchrow_hashref;
383 =head2 GetFullSubscription
387 \@res = GetFullSubscription($subscriptionid)
388 this function read on serial table.
394 sub GetFullSubscription {
395 my ($subscriptionid) = @_;
396 my $dbh = C4::Context->dbh;
398 SELECT serial.serialid,
401 serial.publisheddate,
403 serial.notes as notes,
404 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
405 aqbooksellers.name as aqbooksellername,
406 biblio.title as bibliotitle,
407 subscription.branchcode AS branchcode,
408 subscription.subscriptionid AS subscriptionid |;
409 if (C4::Context->preference('IndependantBranches') &&
410 C4::Context->userenv &&
411 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
413 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
417 LEFT JOIN subscription ON
418 (serial.subscriptionid=subscription.subscriptionid )
419 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
420 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
421 WHERE serial.subscriptionid = ?
423 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
424 serial.subscriptionid
426 $debug and warn "GetFullSubscription query: $query";
427 my $sth = $dbh->prepare($query);
428 $sth->execute($subscriptionid);
429 return $sth->fetchall_arrayref({});
433 =head2 PrepareSerialsData
437 \@res = PrepareSerialsData($serialinfomation)
438 where serialinformation is a hashref array
444 sub PrepareSerialsData{
450 my $aqbooksellername;
454 my $previousnote = "";
456 foreach my $subs ( @$lines ) {
457 $subs->{'publisheddate'} =
458 ( $subs->{'publisheddate'}
459 ? format_date( $subs->{'publisheddate'} )
461 $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
462 $subs->{ "status" . $subs->{'status'} } = 1;
463 $subs->{ "checked" } = $subs->{'status'} =~/1|3|4|7/;
465 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
466 $year = $subs->{'year'};
471 if ( $tmpresults{$year} ) {
472 push @{ $tmpresults{$year}->{'serials'} }, $subs;
475 $tmpresults{$year} = {
477 'aqbooksellername' => $subs->{'aqbooksellername'},
478 'bibliotitle' => $subs->{'bibliotitle'},
479 'serials' => [$subs],
484 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
485 push @res, $tmpresults{$key};
487 $res[0]->{'first'}=1;
491 =head2 GetSubscriptionsFromBiblionumber
493 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
494 this function get the subscription list. it reads on subscription table.
496 table of subscription which has the biblionumber given on input arg.
497 each line of this table is a hashref. All hashes containt
498 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
502 sub GetSubscriptionsFromBiblionumber {
503 my ($biblionumber) = @_;
504 my $dbh = C4::Context->dbh;
506 SELECT subscription.*,
508 subscriptionhistory.*,
509 aqbooksellers.name AS aqbooksellername,
510 biblio.title AS bibliotitle
512 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
513 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
514 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
515 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
516 WHERE subscription.biblionumber = ?
518 my $sth = $dbh->prepare($query);
519 $sth->execute($biblionumber);
521 while ( my $subs = $sth->fetchrow_hashref ) {
522 $subs->{startdate} = format_date( $subs->{startdate} );
523 $subs->{histstartdate} = format_date( $subs->{histstartdate} );
524 $subs->{histenddate} = format_date( $subs->{histenddate} );
525 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
526 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
527 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
528 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
529 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
530 $subs->{ "status" . $subs->{'status'} } = 1;
531 $subs->{'cannotedit'}=(C4::Context->preference('IndependantBranches') &&
532 C4::Context->userenv &&
533 C4::Context->userenv->{flags} % 2 !=1 &&
534 C4::Context->userenv->{branch} && $subs->{branchcode} &&
535 (C4::Context->userenv->{branch} ne $subs->{branchcode}));
536 if ( $subs->{enddate} eq '0000-00-00' ) {
537 $subs->{enddate} = '';
540 $subs->{enddate} = format_date( $subs->{enddate} );
542 $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
543 $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
549 =head2 GetFullSubscriptionsFromBiblionumber
553 \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
554 this function read on serial table.
560 sub GetFullSubscriptionsFromBiblionumber {
561 my ($biblionumber) = @_;
562 my $dbh = C4::Context->dbh;
564 SELECT serial.serialid,
567 serial.publisheddate,
569 serial.notes as notes,
570 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
571 biblio.title as bibliotitle,
572 subscription.branchcode AS branchcode,
573 subscription.subscriptionid AS subscriptionid|;
574 if (C4::Context->preference('IndependantBranches') &&
575 C4::Context->userenv &&
576 C4::Context->userenv->{'flags'} != 1 && 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) = @_;
612 #return unless $title or $ISSN or $biblionumber;
613 my $dbh = C4::Context->dbh;
616 SELECT subscription.*, subscriptionhistory.*, biblio.title,biblioitems.issn,biblio.biblionumber
618 LEFT JOIN subscriptionhistory USING(subscriptionid)
619 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
620 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
625 $sqlwhere=" WHERE biblio.biblionumber=?";
626 push @bind_params,$biblionumber;
630 my @strings_to_search;
631 @strings_to_search=map {"%$_%"} split (/ /,$string);
632 foreach my $index qw(biblio.title subscription.callnumber subscription.location subscription.notes subscription.internalnotes){
633 push @bind_params,@strings_to_search;
634 my $tmpstring= "AND $index LIKE ? "x scalar(@strings_to_search);
635 $debug && warn "$tmpstring";
636 $tmpstring=~s/^AND //;
637 push @sqlstrings,$tmpstring;
639 $sqlwhere.= ($sqlwhere?" AND ":" WHERE ")."(".join(") OR (",@sqlstrings).")";
643 my @strings_to_search;
644 @strings_to_search=map {"%$_%"} split (/ /,$issn);
645 foreach my $index qw(biblioitems.issn subscription.callnumber){
646 push @bind_params,@strings_to_search;
647 my $tmpstring= "OR $index LIKE ? "x scalar(@strings_to_search);
648 $debug && warn "$tmpstring";
649 $tmpstring=~s/^OR //;
650 push @sqlstrings,$tmpstring;
652 $sqlwhere.= ($sqlwhere?" AND ":" WHERE ")."(".join(") OR (",@sqlstrings).")";
654 $sql.="$sqlwhere ORDER BY title";
655 $debug and warn "GetSubscriptions query: $sql params : ", join (" ",@bind_params);
656 $sth = $dbh->prepare($sql);
657 $sth->execute(@bind_params);
659 my $previoustitle = "";
661 while ( my $line = $sth->fetchrow_hashref ) {
662 if ( $previoustitle eq $line->{title} ) {
667 $previoustitle = $line->{title};
670 $line->{toggle} = 1 if $odd == 1;
671 $line->{'cannotedit'}=(C4::Context->preference('IndependantBranches') &&
672 C4::Context->userenv &&
673 C4::Context->userenv->{flags} % 2 !=1 &&
674 C4::Context->userenv->{branch} && $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);
705 "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
707 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
708 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
709 my $sth = $dbh->prepare($query);
710 $sth->execute($subscriptionid);
711 while ( my $line = $sth->fetchrow_hashref ) {
712 $line->{ "status" . $line->{status} } =
713 1; # fills a "statusX" value, used for template status select list
714 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
715 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
716 push @serials, $line;
718 # OK, now add the last 5 issues arrives/missing
720 "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
722 WHERE subscriptionid = ?
723 AND (status in (2,4,5))
724 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
726 $sth = $dbh->prepare($query);
727 $sth->execute($subscriptionid);
728 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
730 $line->{ "status" . $line->{status} } =
731 1; # fills a "statusX" value, used for template status select list
732 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
733 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
734 push @serials, $line;
737 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
738 $sth = $dbh->prepare($query);
739 $sth->execute($subscriptionid);
740 my ($totalissues) = $sth->fetchrow;
741 return ( $totalissues, @serials );
748 ($totalissues,@serials) = GetSerials2($subscriptionid,$status);
749 this function get every serial waited for a given subscription
750 as well as the number of issues registered in the database (all types)
751 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
757 my ($subscription,$status) = @_;
758 my $dbh = C4::Context->dbh;
760 SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
762 WHERE subscriptionid=$subscription AND status IN ($status)
763 ORDER BY publisheddate,serialid DESC
765 $debug and warn "GetSerials2 query: $query";
766 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"});
775 my ($totalissues) = scalar(@serials);
776 return ($totalissues,@serials);
779 =head2 GetLatestSerials
783 \@serials = GetLatestSerials($subscriptionid,$limit)
784 get the $limit's latest serials arrived or missing for a given subscription
786 a ref to a table which it containts all of the latest serials stored into a hash.
792 sub GetLatestSerials {
793 my ( $subscriptionid, $limit ) = @_;
794 my $dbh = C4::Context->dbh;
796 # status = 2 is "arrived"
797 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
799 WHERE subscriptionid = ?
800 AND (status =2 or status=4)
801 ORDER BY planneddate DESC LIMIT 0,$limit
803 my $sth = $dbh->prepare($strsth);
804 $sth->execute($subscriptionid);
806 while ( my $line = $sth->fetchrow_hashref ) {
807 $line->{ "status" . $line->{status} } =
808 1; # fills a "statusX" value, used for template status select list
809 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
810 push @serials, $line;
816 # WHERE subscriptionid=?
818 # $sth=$dbh->prepare($query);
819 # $sth->execute($subscriptionid);
820 # my ($totalissues) = $sth->fetchrow;
824 =head2 GetDistributedTo
828 $distributedto=GetDistributedTo($subscriptionid)
829 This function select the old previous value of distributedto in the database.
835 sub GetDistributedTo {
836 my $dbh = C4::Context->dbh;
838 my $subscriptionid = @_;
839 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
840 my $sth = $dbh->prepare($query);
841 $sth->execute($subscriptionid);
842 return ($distributedto) = $sth->fetchrow;
850 $val is a hashref containing all the attributes of the table 'subscription'
851 This function get the next issue for the subscription given on input arg
853 all the input params updated.
861 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
862 # $calculated = $val->{numberingmethod};
863 # # calculate the (expected) value of the next issue recieved.
864 # $newlastvalue1 = $val->{lastvalue1};
865 # # check if we have to increase the new value.
866 # $newinnerloop1 = $val->{innerloop1}+1;
867 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
868 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
869 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
870 # $calculated =~ s/\{X\}/$newlastvalue1/g;
872 # $newlastvalue2 = $val->{lastvalue2};
873 # # check if we have to increase the new value.
874 # $newinnerloop2 = $val->{innerloop2}+1;
875 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
876 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
877 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
878 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
880 # $newlastvalue3 = $val->{lastvalue3};
881 # # check if we have to increase the new value.
882 # $newinnerloop3 = $val->{innerloop3}+1;
883 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
884 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
885 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
886 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
887 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
893 $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3,
894 $newinnerloop1, $newinnerloop2, $newinnerloop3
896 my $pattern = $val->{numberpattern};
897 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
898 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
899 $calculated = $val->{numberingmethod};
900 $newlastvalue1 = $val->{lastvalue1};
901 $newlastvalue2 = $val->{lastvalue2};
902 $newlastvalue3 = $val->{lastvalue3};
903 $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};
912 # check if we have to increase the new value.
913 $newinnerloop2 = $val->{innerloop2} + 1;
914 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
915 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
916 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
917 if ( $pattern == 6 ) {
918 if ( $val->{hemisphere} == 2 ) {
919 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
920 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
923 my $newlastvalue2seq = $seasons[$newlastvalue2];
924 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
928 $calculated =~ s/\{Y\}/$newlastvalue2/g;
932 $newlastvalue3 = $val->{lastvalue3};
933 # check if we have to increase the new value.
934 $newinnerloop3 = $val->{innerloop3} + 1;
935 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
936 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
937 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
938 $calculated =~ s/\{Z\}/$newlastvalue3/g;
940 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3 ,
941 $newinnerloop1, $newinnerloop2, $newinnerloop3);
948 $calculated = GetSeq($val)
949 $val is a hashref containing all the attributes of the table 'subscription'
950 this function transforms {X},{Y},{Z} to 150,0,0 for example.
952 the sequence in integer format
960 my $pattern = $val->{numberpattern};
961 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
962 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
963 my $calculated = $val->{numberingmethod};
964 my $x = $val->{'lastvalue1'};
965 $calculated =~ s/\{X\}/$x/g;
966 my $newlastvalue2 = $val->{'lastvalue2'};
967 if ( $pattern == 6 ) {
968 if ( $val->{hemisphere} == 2 ) {
969 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
970 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
973 my $newlastvalue2seq = $seasons[$newlastvalue2];
974 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
978 $calculated =~ s/\{Y\}/$newlastvalue2/g;
980 my $z = $val->{'lastvalue3'};
981 $calculated =~ s/\{Z\}/$z/g;
985 =head2 GetExpirationDate
987 $sensddate = GetExpirationDate($subscriptionid)
989 this function return the expiration date for a subscription given on input args.
996 sub GetExpirationDate {
997 my ($subscriptionid) = @_;
998 my $dbh = C4::Context->dbh;
999 my $subscription = GetSubscription($subscriptionid);
1000 my $enddate = $$subscription{enddate}||$$subscription{histenddate};
1002 return $enddate if ($enddate && $enddate ne "0000-00-00");
1004 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1005 $enddate=$$subscription{startdate};
1006 my @date=split (/-/,$$subscription{startdate});
1007 return if (scalar(@date)!=3 ||not check_date(@date));
1008 if (($subscription->{periodicity} % 16) >0){
1009 if ( $subscription->{numberlength} ) {
1010 #calculate the date of the last issue.
1011 my $length = $subscription->{numberlength};
1012 for ( my $i = 1 ; $i <= $length ; $i++ ) {
1013 $enddate = GetNextDate( $enddate, $subscription );
1016 elsif ( $subscription->{monthlength} ){
1017 if ($$subscription{startdate}){
1018 my @enddate = Add_Delta_YM($date[0],$date[1],$date[2],0,$subscription->{monthlength});
1019 $enddate=sprintf("%04d-%02d-%02d",$enddate[0],$enddate[1],$enddate[2]);
1021 } elsif ( $subscription->{weeklength} ){
1022 if ($$subscription{startdate}){
1023 my @date=split (/-/,$subscription->{startdate});
1024 my @enddate = Add_Delta_Days($date[0],$date[1],$date[2],$subscription->{weeklength}*7);
1025 $enddate=sprintf("%04d-%02d-%02d",$enddate[0],$enddate[1],$enddate[2]);
1034 =head2 CountSubscriptionFromBiblionumber
1038 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1039 this count the number of subscription for a biblionumber given.
1041 the number of subscriptions with biblionumber given on input arg.
1047 sub CountSubscriptionFromBiblionumber {
1048 my ($biblionumber) = @_;
1049 my $dbh = C4::Context->dbh;
1050 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1051 my $sth = $dbh->prepare($query);
1052 $sth->execute($biblionumber);
1053 my $subscriptionsnumber = $sth->fetchrow;
1054 return $subscriptionsnumber;
1057 =head2 ModSubscriptionHistory
1061 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1063 this function modify the history of a subscription. Put your new values on input arg.
1069 sub ModSubscriptionHistory {
1071 $subscriptionid, $histstartdate, $enddate, $recievedlist,
1072 $missinglist, $opacnote, $librariannote
1074 my $dbh = C4::Context->dbh;
1075 my $query = "UPDATE subscriptionhistory
1076 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1077 WHERE subscriptionid=?
1079 my $sth = $dbh->prepare($query);
1080 $recievedlist =~ s/^; //;
1081 $missinglist =~ s/^; //;
1082 $opacnote =~ s/^; //;
1084 $histstartdate, $enddate, $recievedlist, $missinglist,
1085 $opacnote, $librariannote, $subscriptionid
1090 =head2 ModSerialStatus
1094 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1096 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1097 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1103 sub ModSerialStatus {
1104 my ( $serialid, $serialseq, $planneddate,$publisheddate, $status, $notes )
1107 #It is a usual serial
1108 # 1st, get previous status :
1109 my $dbh = C4::Context->dbh;
1110 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1111 my $sth = $dbh->prepare($query);
1112 $sth->execute($serialid);
1113 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1115 # change status & update subscriptionhistory
1117 if ( $status eq 6 ) {
1118 DelIssue( {'serialid'=>$serialid, 'subscriptionid'=>$subscriptionid,'serialseq'=>$serialseq} );
1122 "UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?";
1123 $sth = $dbh->prepare($query);
1124 $sth->execute( $serialseq, $publisheddate, $planneddate, $status,
1125 $notes, $serialid );
1126 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1127 $sth = $dbh->prepare($query);
1128 $sth->execute($subscriptionid);
1129 my $val = $sth->fetchrow_hashref;
1130 unless ( $val->{manualhistory} ) {
1132 "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1133 $sth = $dbh->prepare($query);
1134 $sth->execute($subscriptionid);
1135 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1136 if ( $status eq 2 ) {
1138 $recievedlist .= "; $serialseq"
1139 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1142 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1143 $missinglist .= "; $serialseq"
1145 and not index( "$missinglist", "$serialseq" ) >= 0 );
1146 $missinglist .= "; not issued $serialseq"
1148 and index( "$missinglist", "$serialseq" ) >= 0 );
1150 "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1151 $sth = $dbh->prepare($query);
1152 $recievedlist =~ s/^; //;
1153 $missinglist =~ s/^; //;
1154 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1158 # create new waited entry if needed (ie : was a "waited" and has changed)
1159 if ( $oldstatus eq 1 && $status ne 1 ) {
1160 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1161 $sth = $dbh->prepare($query);
1162 $sth->execute($subscriptionid);
1163 my $val = $sth->fetchrow_hashref;
1168 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
1169 $newinnerloop1, $newinnerloop2, $newinnerloop3
1170 ) = GetNextSeq($val);
1171 # warn "Next Seq End";
1173 # next date (calculated from actual date & frequency parameters)
1174 # warn "publisheddate :$publisheddate ";
1175 my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1176 NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'},
1177 1, $nextpublisheddate, $nextpublisheddate );
1179 "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1180 WHERE subscriptionid = ?";
1181 $sth = $dbh->prepare($query);
1183 $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
1184 $newinnerloop2, $newinnerloop3, $subscriptionid
1187 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1188 if ( $val->{letter} && $status eq 2 && $oldstatus ne 2 ) {
1189 SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1194 =head2 GetNextExpected
1198 $nextexpected = GetNextExpected($subscriptionid)
1200 Get the planneddate for the current expected issue of the subscription.
1206 planneddate => C4::Dates object
1213 sub GetNextExpected($) {
1214 my ($subscriptionid) = @_;
1215 my $dbh = C4::Context->dbh;
1216 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1217 # Each subscription has only one 'expected' issue, with serial.status==1.
1218 $sth->execute( $subscriptionid, 1 );
1219 my ( $nextissue ) = $sth->fetchrow_hashref;
1221 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1222 $sth->execute( $subscriptionid );
1223 $nextissue = $sth->fetchrow_hashref;
1225 $nextissue->{planneddate} = C4::Dates->new($nextissue->{planneddate},'iso');
1229 =head2 ModNextExpected
1233 ModNextExpected($subscriptionid,$date)
1235 Update the planneddate for the current expected issue of the subscription.
1236 This will modify all future prediction results.
1238 C<$date> is a C4::Dates object.
1244 sub ModNextExpected($$) {
1245 my ($subscriptionid,$date) = @_;
1246 my $dbh = C4::Context->dbh;
1247 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1248 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1249 # Each subscription has only one 'expected' issue, with serial.status==1.
1250 $sth->execute( $date->output('iso'),$date->output('iso'), $subscriptionid, 1);
1255 =head2 ModSubscription
1259 this function modify a subscription. Put all new values on input args.
1265 sub ModSubscription {
1267 $auser, $branchcode, $aqbooksellerid, $cost,
1268 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1269 $dow, $irregularity, $numberpattern, $numberlength,
1270 $weeklength, $monthlength, $add1, $every1,
1271 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1272 $add2, $every2, $whenmorethan2, $setto2,
1273 $lastvalue2, $innerloop2, $add3, $every3,
1274 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1275 $numberingmethod, $status, $biblionumber, $callnumber,
1276 $notes, $letter, $hemisphere, $manualhistory,
1277 $internalnotes, $serialsadditems,
1278 $staffdisplaycount,$opacdisplaycount, $graceperiod, $location,$enddate,$subscriptionid
1280 # warn $irregularity;
1281 my $dbh = C4::Context->dbh;
1282 my $query = "UPDATE subscription
1283 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1284 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1285 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1286 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1287 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1288 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?,
1289 letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,
1290 staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1292 WHERE subscriptionid = ?";
1293 #warn "query :".$query;
1294 my $sth = $dbh->prepare($query);
1296 $auser, $branchcode, $aqbooksellerid, $cost,
1297 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1298 $dow, "$irregularity", $numberpattern, $numberlength,
1299 $weeklength, $monthlength, $add1, $every1,
1300 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1301 $add2, $every2, $whenmorethan2, $setto2,
1302 $lastvalue2, $innerloop2, $add3, $every3,
1303 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1304 $numberingmethod, $status, $biblionumber, $callnumber,
1305 $notes, $letter, $hemisphere, ($manualhistory?$manualhistory:0),
1306 $internalnotes, $serialsadditems,
1307 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location,$enddate,
1310 my $rows=$sth->rows;
1313 logaction("SERIAL", "MODIFY", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1317 =head2 NewSubscription
1321 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1322 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1323 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1324 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1325 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1326 $numberingmethod, $status, $notes, $serialsadditems,
1327 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate);
1329 Create a new subscription with value given on input args.
1332 the id of this new subscription
1338 sub NewSubscription {
1340 $auser, $branchcode, $aqbooksellerid, $cost,
1341 $aqbudgetid, $biblionumber, $startdate, $periodicity,
1342 $dow, $numberlength, $weeklength, $monthlength,
1343 $add1, $every1, $whenmorethan1, $setto1,
1344 $lastvalue1, $innerloop1, $add2, $every2,
1345 $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1346 $add3, $every3, $whenmorethan3, $setto3,
1347 $lastvalue3, $innerloop3, $numberingmethod, $status,
1348 $notes, $letter, $firstacquidate, $irregularity,
1349 $numberpattern, $callnumber, $hemisphere, $manualhistory,
1350 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1351 $graceperiod, $location,$enddate
1353 my $dbh = C4::Context->dbh;
1355 #save subscription (insert into database)
1357 INSERT INTO subscription
1358 (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1359 startdate,periodicity,dow,numberlength,weeklength,monthlength,
1360 add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1361 add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1362 add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1363 numberingmethod, status, notes, letter,firstacquidate,irregularity,
1364 numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
1365 staffdisplaycount,opacdisplaycount,graceperiod,location,enddate)
1366 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1368 my $sth = $dbh->prepare($query);
1370 $auser, $branchcode,
1371 $aqbooksellerid, $cost,
1372 $aqbudgetid, $biblionumber,
1373 $startdate, $periodicity,
1374 $dow, $numberlength,
1375 $weeklength, $monthlength,
1377 $whenmorethan1, $setto1,
1378 $lastvalue1, $innerloop1,
1380 $whenmorethan2, $setto2,
1381 $lastvalue2, $innerloop2,
1383 $whenmorethan3, $setto3,
1384 $lastvalue3, $innerloop3,
1385 $numberingmethod, "$status",
1387 $firstacquidate, $irregularity,
1388 $numberpattern, $callnumber,
1389 $hemisphere, $manualhistory,
1390 $internalnotes, $serialsadditems,
1391 $staffdisplaycount, $opacdisplaycount,
1392 $graceperiod, $location,
1396 #then create the 1st waited number
1397 my $subscriptionid = $dbh->{'mysql_insertid'};
1399 INSERT INTO subscriptionhistory
1400 (biblionumber, subscriptionid, histstartdate, opacnote, librariannote)
1403 $sth = $dbh->prepare($query);
1404 $sth->execute( $biblionumber, $subscriptionid,
1406 $notes,$internalnotes );
1408 # reread subscription to get a hash (for calculation of the 1st issue number)
1412 WHERE subscriptionid = ?
1414 $sth = $dbh->prepare($query);
1415 $sth->execute($subscriptionid);
1416 my $val = $sth->fetchrow_hashref;
1418 # calculate issue number
1419 my $serialseq = GetSeq($val);
1422 (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1423 VALUES (?,?,?,?,?,?)
1425 $sth = $dbh->prepare($query);
1427 "$serialseq", $subscriptionid, $biblionumber, 1,
1432 logaction("SERIAL", "ADD", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1434 #set serial flag on biblio if not already set.
1435 my ($null, ($bib)) = GetBiblio($biblionumber);
1436 if( ! $bib->{'serial'} ) {
1437 my $record = GetMarcBiblio($biblionumber);
1438 my ($tag,$subf) = GetMarcFromKohaField('biblio.serial',$bib->{'frameworkcode'});
1441 $record->field($tag)->update( $subf => 1 );
1444 ModBiblio($record,$biblionumber,$bib->{'frameworkcode'});
1446 return $subscriptionid;
1449 =head2 ReNewSubscription
1453 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1455 this function renew a subscription with values given on input args.
1461 sub ReNewSubscription {
1462 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength,
1463 $monthlength, $note )
1465 my $dbh = C4::Context->dbh;
1466 my $subscription = GetSubscription($subscriptionid);
1470 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1471 WHERE biblio.biblionumber=?
1473 my $sth = $dbh->prepare($query);
1474 $sth->execute( $subscription->{biblionumber} );
1475 my $biblio = $sth->fetchrow_hashref;
1476 if (C4::Context->preference("RenewSerialAddsSuggestion")){
1479 'suggestedby' => $user,
1480 'title' => $subscription->{bibliotitle},
1481 'author' => $biblio->{author},
1482 'publishercode' => $biblio->{publishercode},
1483 'note' => $biblio->{note},
1484 'biblionumber' => $subscription->{biblionumber}
1488 # renew subscription
1491 SET startdate=?,numberlength=?,weeklength=?,monthlength=?
1492 WHERE subscriptionid=?
1494 $sth = $dbh->prepare($query);
1495 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1497 logaction("SERIAL", "RENEW", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1504 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1506 Create a new issue stored on the database.
1507 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1514 my ( $serialseq, $subscriptionid, $biblionumber, $status,
1515 $planneddate, $publisheddate, $notes )
1517 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1519 my $dbh = C4::Context->dbh;
1522 (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1523 VALUES (?,?,?,?,?,?,?)
1525 my $sth = $dbh->prepare($query);
1526 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status,
1527 $publisheddate, $planneddate,$notes );
1528 my $serialid=$dbh->{'mysql_insertid'};
1530 SELECT missinglist,recievedlist
1531 FROM subscriptionhistory
1532 WHERE subscriptionid=?
1534 $sth = $dbh->prepare($query);
1535 $sth->execute($subscriptionid);
1536 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1538 if ( $status eq 2 ) {
1539 ### TODO Add a feature that improves recognition and description.
1540 ### As such count (serialseq) i.e. : N18,2(N19),N20
1541 ### Would use substr and index But be careful to previous presence of ()
1542 $recievedlist .= "; $serialseq" unless (index($recievedlist,$serialseq)>0);
1544 if ( $status eq 4 ) {
1545 $missinglist .= "; $serialseq" unless (index($missinglist,$serialseq)>0);
1548 UPDATE subscriptionhistory
1549 SET recievedlist=?, missinglist=?
1550 WHERE subscriptionid=?
1552 $sth = $dbh->prepare($query);
1553 $recievedlist =~ s/^; //;
1554 $missinglist =~ s/^; //;
1555 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1559 =head2 ItemizeSerials
1563 ItemizeSerials($serialid, $info);
1564 $info is a hashref containing barcode branch, itemcallnumber, status, location
1565 $serialid the serialid
1567 1 if the itemize is a succes.
1568 0 and @error else. @error containts the list of errors found.
1574 sub ItemizeSerials {
1575 my ( $serialid, $info ) = @_;
1576 my $now = POSIX::strftime( "%Y-%m-%d",localtime );
1578 my $dbh = C4::Context->dbh;
1584 my $sth = $dbh->prepare($query);
1585 $sth->execute($serialid);
1586 my $data = $sth->fetchrow_hashref;
1587 if ( C4::Context->preference("RoutingSerials") ) {
1589 # check for existing biblioitem relating to serial issue
1590 my ( $count, @results ) =
1591 GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1593 for ( my $i = 0 ; $i < $count ; $i++ ) {
1594 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' ('
1595 . $data->{'planneddate'}
1598 $bibitemno = $results[$i]->{'biblioitemnumber'};
1602 if ( $bibitemno == 0 ) {
1605 "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC"
1607 $sth->execute( $data->{'biblionumber'} );
1608 my $biblioitem = $sth->fetchrow_hashref;
1609 $biblioitem->{'volumedate'} =
1610 $data->{planneddate} ;
1611 $biblioitem->{'volumeddesc'} =
1612 $data->{serialseq} . ' ('
1613 . format_date( $data->{'planneddate'} ) . ')';
1614 $biblioitem->{'dewey'} = $info->{itemcallnumber};
1618 my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1619 if ( $info->{barcode} ) {
1621 my $exists = itemdata( $info->{'barcode'} );
1622 push @errors, "barcode_not_unique" if ($exists);
1624 my $marcrecord = MARC::Record->new();
1625 my ( $tag, $subfield ) =
1626 GetMarcFromKohaField( "items.barcode", $fwk );
1628 MARC::Field->new( "$tag", '', '',
1629 "$subfield" => $info->{barcode} );
1630 $marcrecord->insert_fields_ordered($newField);
1631 if ( $info->{branch} ) {
1632 my ( $tag, $subfield ) =
1633 GetMarcFromKohaField( "items.homebranch",
1636 #warn "items.homebranch : $tag , $subfield";
1637 if ( $marcrecord->field($tag) ) {
1638 $marcrecord->field($tag)
1639 ->add_subfields( "$subfield" => $info->{branch} );
1643 MARC::Field->new( "$tag", '', '',
1644 "$subfield" => $info->{branch} );
1645 $marcrecord->insert_fields_ordered($newField);
1647 ( $tag, $subfield ) =
1648 GetMarcFromKohaField( "items.holdingbranch",
1651 #warn "items.holdingbranch : $tag , $subfield";
1652 if ( $marcrecord->field($tag) ) {
1653 $marcrecord->field($tag)
1654 ->add_subfields( "$subfield" => $info->{branch} );
1658 MARC::Field->new( "$tag", '', '',
1659 "$subfield" => $info->{branch} );
1660 $marcrecord->insert_fields_ordered($newField);
1663 if ( $info->{itemcallnumber} ) {
1664 my ( $tag, $subfield ) =
1665 GetMarcFromKohaField( "items.itemcallnumber",
1668 if ( $marcrecord->field($tag) ) {
1669 $marcrecord->field($tag)
1670 ->add_subfields( "$subfield" => $info->{itemcallnumber} );
1674 MARC::Field->new( "$tag", '', '',
1675 "$subfield" => $info->{itemcallnumber} );
1676 $marcrecord->insert_fields_ordered($newField);
1679 if ( $info->{notes} ) {
1680 my ( $tag, $subfield ) =
1681 GetMarcFromKohaField( "items.itemnotes", $fwk );
1683 if ( $marcrecord->field($tag) ) {
1684 $marcrecord->field($tag)
1685 ->add_subfields( "$subfield" => $info->{notes} );
1689 MARC::Field->new( "$tag", '', '',
1690 "$subfield" => $info->{notes} );
1691 $marcrecord->insert_fields_ordered($newField);
1694 if ( $info->{location} ) {
1695 my ( $tag, $subfield ) =
1696 GetMarcFromKohaField( "items.location", $fwk );
1698 if ( $marcrecord->field($tag) ) {
1699 $marcrecord->field($tag)
1700 ->add_subfields( "$subfield" => $info->{location} );
1704 MARC::Field->new( "$tag", '', '',
1705 "$subfield" => $info->{location} );
1706 $marcrecord->insert_fields_ordered($newField);
1709 if ( $info->{status} ) {
1710 my ( $tag, $subfield ) =
1711 GetMarcFromKohaField( "items.notforloan",
1714 if ( $marcrecord->field($tag) ) {
1715 $marcrecord->field($tag)
1716 ->add_subfields( "$subfield" => $info->{status} );
1720 MARC::Field->new( "$tag", '', '',
1721 "$subfield" => $info->{status} );
1722 $marcrecord->insert_fields_ordered($newField);
1725 if ( C4::Context->preference("RoutingSerials") ) {
1726 my ( $tag, $subfield ) =
1727 GetMarcFromKohaField( "items.dateaccessioned",
1729 if ( $marcrecord->field($tag) ) {
1730 $marcrecord->field($tag)
1731 ->add_subfields( "$subfield" => $now );
1735 MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1736 $marcrecord->insert_fields_ordered($newField);
1739 AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1742 return ( 0, @errors );
1746 =head2 HasSubscriptionStrictlyExpired
1750 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1752 the subscription has stricly expired when today > the end subscription date
1755 1 if true, 0 if false, -1 if the expiration date is not set.
1760 sub HasSubscriptionStrictlyExpired {
1761 # Getting end of subscription date
1762 my ($subscriptionid) = @_;
1763 my $dbh = C4::Context->dbh;
1764 my $subscription = GetSubscription($subscriptionid);
1765 my $expirationdate = GetExpirationDate($subscriptionid);
1767 # If the expiration date is set
1768 if ($expirationdate != 0) {
1769 my ($endyear, $endmonth, $endday) = split('-', $expirationdate);
1771 # Getting today's date
1772 my ($nowyear, $nowmonth, $nowday) = Today();
1774 # if today's date > expiration date, then the subscription has stricly expired
1775 if (Delta_Days($nowyear, $nowmonth, $nowday,
1776 $endyear, $endmonth, $endday) < 0) {
1782 # There are some cases where the expiration date is not set
1783 # As we can't determine if the subscription has expired on a date-basis,
1789 =head2 HasSubscriptionExpired
1793 $has_expired = HasSubscriptionExpired($subscriptionid)
1795 the subscription has expired when the next issue to arrive is out of subscription limit.
1798 0 if the subscription has not expired
1799 1 if the subscription has expired
1800 2 if has subscription does not have a valid expiration date set
1806 sub HasSubscriptionExpired {
1807 my ($subscriptionid) = @_;
1808 my $dbh = C4::Context->dbh;
1809 my $subscription = GetSubscription($subscriptionid);
1810 if (($subscription->{periodicity} % 16)>0){
1811 my $expirationdate = GetExpirationDate($subscriptionid);
1813 SELECT max(planneddate)
1815 WHERE subscriptionid=?
1817 my $sth = $dbh->prepare($query);
1818 $sth->execute($subscriptionid);
1819 my ($res) = $sth->fetchrow ;
1820 return 0 unless $res;
1821 my @res=split (/-/,$res);
1822 my @endofsubscriptiondate=split(/-/,$expirationdate);
1823 return 2 if (scalar(@res)!=3 || scalar(@endofsubscriptiondate)!=3||not check_date(@res) || not check_date(@endofsubscriptiondate));
1824 return 1 if ( (@endofsubscriptiondate && Delta_Days($res[0],$res[1],$res[2],
1825 $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) <= 0)
1829 if ($subscription->{'numberlength'}){
1830 my $countreceived=countissuesfrom($subscriptionid,$subscription->{'startdate'});
1831 return 1 if ($countreceived >$subscription->{'numberlength'});
1837 return 0; # Notice that you'll never get here.
1840 =head2 SetDistributedto
1844 SetDistributedto($distributedto,$subscriptionid);
1845 This function update the value of distributedto for a subscription given on input arg.
1851 sub SetDistributedto {
1852 my ( $distributedto, $subscriptionid ) = @_;
1853 my $dbh = C4::Context->dbh;
1857 WHERE subscriptionid=?
1859 my $sth = $dbh->prepare($query);
1860 $sth->execute( $distributedto, $subscriptionid );
1863 =head2 DelSubscription
1867 DelSubscription($subscriptionid)
1868 this function delete the subscription which has $subscriptionid as id.
1874 sub DelSubscription {
1875 my ($subscriptionid) = @_;
1876 my $dbh = C4::Context->dbh;
1877 $subscriptionid = $dbh->quote($subscriptionid);
1878 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1880 "DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1881 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1883 logaction("SERIAL", "DELETE", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1890 DelIssue($serialseq,$subscriptionid)
1891 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1898 my ( $dataissue) = @_;
1899 my $dbh = C4::Context->dbh;
1900 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1905 AND subscriptionid= ?
1907 my $mainsth = $dbh->prepare($query);
1908 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'});
1910 #Delete element from subscription history
1911 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1912 my $sth = $dbh->prepare($query);
1913 $sth->execute($dataissue->{'subscriptionid'});
1914 my $val = $sth->fetchrow_hashref;
1915 unless ( $val->{manualhistory} ) {
1917 SELECT * FROM subscriptionhistory
1918 WHERE subscriptionid= ?
1920 my $sth = $dbh->prepare($query);
1921 $sth->execute($dataissue->{'subscriptionid'});
1922 my $data = $sth->fetchrow_hashref;
1923 my $serialseq= $dataissue->{'serialseq'};
1924 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1925 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1926 my $strsth = "UPDATE subscriptionhistory SET "
1928 map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data )
1929 . " WHERE subscriptionid=?";
1930 $sth = $dbh->prepare($strsth);
1931 $sth->execute($dataissue->{'subscriptionid'});
1934 return $mainsth->rows;
1937 =head2 GetLateOrMissingIssues
1941 ($count,@issuelist) = &GetLateMissingIssues($supplierid,$serialid)
1943 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1946 a count of the number of missing issues
1947 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1948 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1954 sub GetLateOrMissingIssues {
1955 my ( $supplierid, $serialid,$order ) = @_;
1956 my $dbh = C4::Context->dbh;
1960 $byserial = "and serialid = " . $serialid;
1968 $sth = $dbh->prepare(
1970 serialid, aqbooksellerid, name,
1971 biblio.title, planneddate, serialseq,
1972 serial.status, serial.subscriptionid, claimdate
1974 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1975 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1976 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1977 WHERE subscription.subscriptionid = serial.subscriptionid
1978 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1979 AND subscription.aqbooksellerid=$supplierid
1985 $sth = $dbh->prepare(
1987 serialid, aqbooksellerid, name,
1988 biblio.title, planneddate, serialseq,
1989 serial.status, serial.subscriptionid, claimdate
1991 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1992 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1993 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1994 WHERE subscription.subscriptionid = serial.subscriptionid
1995 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
2005 while ( my $line = $sth->fetchrow_hashref ) {
2006 $odd++ unless $line->{title} eq $last_title;
2007 $last_title = $line->{title} if ( $line->{title} );
2008 $line->{planneddate} = format_date( $line->{planneddate} );
2009 $line->{claimdate} = format_date( $line->{claimdate} );
2010 $line->{"status".$line->{status}} = 1;
2011 $line->{'odd'} = 1 if $odd % 2;
2013 push @issuelist, $line;
2015 return $count, @issuelist;
2018 =head2 removeMissingIssue
2022 removeMissingIssue($subscriptionid)
2024 this function removes an issue from being part of the missing string in
2025 subscriptionlist.missinglist column
2027 called when a missing issue is found from the serials-recieve.pl file
2033 sub removeMissingIssue {
2034 my ( $sequence, $subscriptionid ) = @_;
2035 my $dbh = C4::Context->dbh;
2038 "SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
2039 $sth->execute($subscriptionid);
2040 my $data = $sth->fetchrow_hashref;
2041 my $missinglist = $data->{'missinglist'};
2042 my $missinglistbefore = $missinglist;
2044 # warn $missinglist." before";
2045 $missinglist =~ s/($sequence)//;
2047 # warn $missinglist." after";
2048 if ( $missinglist ne $missinglistbefore ) {
2049 $missinglist =~ s/\|\s\|/\|/g;
2050 $missinglist =~ s/^\| //g;
2051 $missinglist =~ s/\|$//g;
2052 my $sth2 = $dbh->prepare(
2053 "UPDATE subscriptionhistory
2055 WHERE subscriptionid = ?"
2057 $sth2->execute( $missinglist, $subscriptionid );
2065 &updateClaim($serialid)
2067 this function updates the time when a claim is issued for late/missing items
2069 called from claims.pl file
2076 my ($serialid) = @_;
2077 my $dbh = C4::Context->dbh;
2078 my $sth = $dbh->prepare(
2079 "UPDATE serial SET claimdate = now()
2083 $sth->execute($serialid);
2086 =head2 getsupplierbyserialid
2090 ($result) = &getsupplierbyserialid($serialid)
2092 this function is used to find the supplier id given a serial id
2095 hashref containing serialid, subscriptionid, and aqbooksellerid
2101 sub getsupplierbyserialid {
2102 my ($serialid) = @_;
2103 my $dbh = C4::Context->dbh;
2104 my $sth = $dbh->prepare(
2105 "SELECT serialid, serial.subscriptionid, aqbooksellerid
2107 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
2111 $sth->execute($serialid);
2112 my $line = $sth->fetchrow_hashref;
2113 my $result = $line->{'aqbooksellerid'};
2117 =head2 check_routing
2121 ($result) = &check_routing($subscriptionid)
2123 this function checks to see if a serial has a routing list and returns the count of routingid
2124 used to show either an 'add' or 'edit' link
2131 my ($subscriptionid) = @_;
2132 my $dbh = C4::Context->dbh;
2133 my $sth = $dbh->prepare(
2134 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2135 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2136 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2139 $sth->execute($subscriptionid);
2140 my $line = $sth->fetchrow_hashref;
2141 my $result = $line->{'routingids'};
2145 =head2 addroutingmember
2149 &addroutingmember($borrowernumber,$subscriptionid)
2151 this function takes a borrowernumber and subscriptionid and add the member to the
2152 routing list for that serial subscription and gives them a rank on the list
2153 of either 1 or highest current rank + 1
2159 sub addroutingmember {
2160 my ( $borrowernumber, $subscriptionid ) = @_;
2162 my $dbh = C4::Context->dbh;
2165 "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?"
2167 $sth->execute($subscriptionid);
2168 while ( my $line = $sth->fetchrow_hashref ) {
2169 if ( $line->{'rank'} > 0 ) {
2170 $rank = $line->{'rank'} + 1;
2178 "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)"
2180 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2183 =head2 reorder_members
2187 &reorder_members($subscriptionid,$routingid,$rank)
2189 this function is used to reorder the routing list
2191 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2192 - it gets all members on list puts their routingid's into an array
2193 - removes the one in the array that is $routingid
2194 - then reinjects $routingid at point indicated by $rank
2195 - then update the database with the routingids in the new order
2201 sub reorder_members {
2202 my ( $subscriptionid, $routingid, $rank ) = @_;
2203 my $dbh = C4::Context->dbh;
2206 "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC"
2208 $sth->execute($subscriptionid);
2210 while ( my $line = $sth->fetchrow_hashref ) {
2211 push( @result, $line->{'routingid'} );
2214 # To find the matching index
2216 my $key = -1; # to allow for 0 being a valid response
2217 for ( $i = 0 ; $i < @result ; $i++ ) {
2218 if ( $routingid == $result[$i] ) {
2219 $key = $i; # save the index
2224 # if index exists in array then move it to new position
2225 if ( $key > -1 && $rank > 0 ) {
2226 my $new_rank = $rank -
2227 1; # $new_rank is what you want the new index to be in the array
2228 my $moving_item = splice( @result, $key, 1 );
2229 splice( @result, $new_rank, 0, $moving_item );
2231 for ( my $j = 0 ; $j < @result ; $j++ ) {
2233 $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '"
2235 . "' WHERE routingid = '"
2242 =head2 delroutingmember
2246 &delroutingmember($routingid,$subscriptionid)
2248 this function either deletes one member from routing list if $routingid exists otherwise
2249 deletes all members from the routing list
2255 sub delroutingmember {
2257 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2258 my ( $routingid, $subscriptionid ) = @_;
2259 my $dbh = C4::Context->dbh;
2263 "DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2264 $sth->execute($routingid);
2265 reorder_members( $subscriptionid, $routingid );
2270 "DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2271 $sth->execute($subscriptionid);
2275 =head2 getroutinglist
2279 ($count,@routinglist) = &getroutinglist($subscriptionid)
2281 this gets the info from the subscriptionroutinglist for $subscriptionid
2284 a count of the number of members on routinglist
2285 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2286 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2292 sub getroutinglist {
2293 my ($subscriptionid) = @_;
2294 my $dbh = C4::Context->dbh;
2295 my $sth = $dbh->prepare(
2296 "SELECT routingid, borrowernumber, ranking, biblionumber
2298 LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2299 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2302 $sth->execute($subscriptionid);
2305 while ( my $line = $sth->fetchrow_hashref ) {
2307 push( @routinglist, $line );
2309 return ( $count, @routinglist );
2312 =head2 countissuesfrom
2316 $result = &countissuesfrom($subscriptionid,$startdate)
2323 sub countissuesfrom {
2324 my ($subscriptionid,$startdate) = @_;
2325 my $dbh = C4::Context->dbh;
2329 WHERE subscriptionid=?
2330 AND serial.publisheddate>?
2332 my $sth=$dbh->prepare($query);
2333 $sth->execute($subscriptionid, $startdate);
2334 my ($countreceived)=$sth->fetchrow;
2335 return $countreceived;
2342 $result = &CountIssues($subscriptionid)
2350 my ($subscriptionid) = @_;
2351 my $dbh = C4::Context->dbh;
2355 WHERE subscriptionid=?
2357 my $sth=$dbh->prepare($query);
2358 $sth->execute($subscriptionid);
2359 my ($countreceived)=$sth->fetchrow;
2360 return $countreceived;
2363 =head2 abouttoexpire
2367 $result = &abouttoexpire($subscriptionid)
2369 this function alerts you to the penultimate issue for a serial subscription
2371 returns 1 - if this is the penultimate issue
2379 my ($subscriptionid) = @_;
2380 my $dbh = C4::Context->dbh;
2381 my $subscription = GetSubscription($subscriptionid);
2382 my $per = $subscription->{'periodicity'};
2384 my $expirationdate = GetExpirationDate($subscriptionid);
2387 "select max(planneddate) from serial where subscriptionid=?");
2388 $sth->execute($subscriptionid);
2389 my ($res) = $sth->fetchrow ;
2390 my @res=split (/-/,$res);
2391 @res=Date::Calc::Today if ($res[0]*$res[1]==0);
2392 my @endofsubscriptiondate=split(/-/,$expirationdate);
2394 if ( $per == 1 ) {$x=7;}
2395 if ( $per == 2 ) {$x=7; }
2396 if ( $per == 3 ) {$x=14;}
2397 if ( $per == 4 ) { $x = 21; }
2398 if ( $per == 5 ) { $x = 31; }
2399 if ( $per == 6 ) { $x = 62; }
2400 if ( $per == 7 || $per == 8 ) { $x = 93; }
2401 if ( $per == 9 ) { $x = 190; }
2402 if ( $per == 10 ) { $x = 365; }
2403 if ( $per == 11 ) { $x = 730; }
2404 my @datebeforeend=Add_Delta_Days( $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2],
2405 - (3 * $x)) if (@endofsubscriptiondate && $endofsubscriptiondate[0]*$endofsubscriptiondate[1]*$endofsubscriptiondate[2]);
2406 # warn "DATE BEFORE END: $datebeforeend";
2407 return 1 if ( @res &&
2409 Delta_Days($res[0],$res[1],$res[2],
2410 $datebeforeend[0],$datebeforeend[1],$datebeforeend[2]) <= 0) &&
2411 (@endofsubscriptiondate &&
2412 Delta_Days($res[0],$res[1],$res[2],
2413 $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) >= 0) );
2415 } elsif ($subscription->{numberlength}>0) {
2416 return (countissuesfrom($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength}-1);
2423 ($resultdate) = &GetNextDate($planneddate,$subscription)
2425 this function is an extension of GetNextDate which allows for checking for irregularity
2427 it takes the planneddate and will return the next issue's date and will skip dates if there
2428 exists an irregularity
2429 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2430 skipped then the returned date will be 2007-05-10
2433 $resultdate - then next date in the sequence
2435 Return 0 if periodicity==0
2438 sub in_array { # used in next sub down
2439 my ($val,@elements) = @_;
2440 foreach my $elem(@elements) {
2448 sub GetNextDate(@) {
2449 my ( $planneddate, $subscription ) = @_;
2450 my @irreg = split( /\,/, $subscription->{irregularity} );
2452 #date supposed to be in ISO.
2454 my ( $year, $month, $day ) = split(/-/, $planneddate);
2455 $month=1 unless ($month);
2456 $day=1 unless ($day);
2459 # warn "DOW $dayofweek";
2460 if ( $subscription->{periodicity} % 16 == 0 ) { # 'without regularity' || 'irregular'
2464 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2465 # renaming this pattern from 1/day to " n / week ".
2466 if ( $subscription->{periodicity} == 1 ) {
2467 my $dayofweek = eval{Day_of_Week( $year,$month, $day )};
2468 if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2470 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2471 $dayofweek = 0 if ( $dayofweek == 7 );
2472 if ( in_array( ($dayofweek + 1), @irreg ) ) {
2473 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 1 );
2477 @resultdate = Add_Delta_Days($year,$month, $day , 1 );
2481 if ( $subscription->{periodicity} == 2 ) {
2482 my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2483 if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2485 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2486 #FIXME: if two consecutive irreg, do we only skip one?
2487 if ( $irreg[$i] == (($wkno!=51)?($wkno +1) % 52 :52)) {
2488 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 7 );
2489 $wkno=(($wkno!=51)?($wkno +1) % 52 :52);
2492 @resultdate = Add_Delta_Days( $year,$month, $day, 7);
2496 if ( $subscription->{periodicity} == 3 ) {
2497 my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2498 if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2500 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2501 if ( $irreg[$i] == (($wkno!=50)?($wkno +2) % 52 :52)) {
2502 ### BUGFIX was previously +1 ^
2503 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 14 );
2504 $wkno=(($wkno!=50)?($wkno +2) % 52 :52);
2507 @resultdate = Add_Delta_Days($year,$month, $day , 14 );
2511 if ( $subscription->{periodicity} == 4 ) {
2512 my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2513 if ($@){warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@";}
2515 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2516 if ( $irreg[$i] == (($wkno!=49)?($wkno +3) % 52 :52)) {
2517 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 21 );
2518 $wkno=(($wkno!=49)?($wkno +3) % 52 :52);
2521 @resultdate = Add_Delta_Days($year,$month, $day , 21 );
2524 my $tmpmonth=$month;
2525 if ($year && $month && $day){
2526 if ( $subscription->{periodicity} == 5 ) {
2527 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2528 if ( $irreg[$i] == (($tmpmonth!=11)?($tmpmonth +1) % 12 :12)) {
2529 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,1,0 );
2530 $tmpmonth=(($tmpmonth!=11)?($tmpmonth +1) % 12 :12);
2533 @resultdate = Add_Delta_YMD($year,$month, $day ,0,1,0 );
2535 if ( $subscription->{periodicity} == 6 ) {
2536 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2537 if ( $irreg[$i] == (($tmpmonth!=10)?($tmpmonth +2) % 12 :12)) {
2538 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,2,0 );
2539 $tmpmonth=(($tmpmonth!=10)?($tmpmonth + 2) % 12 :12);
2542 @resultdate = Add_Delta_YMD($year,$month, $day, 0, 2,0 );
2544 if ( $subscription->{periodicity} == 7 ) {
2545 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2546 if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2547 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day, 0, 3,0 );
2548 $tmpmonth=(($tmpmonth!=9)?($tmpmonth + 3) % 12 :12);
2551 @resultdate = Add_Delta_YMD($year,$month, $day, 0, 3, 0);
2553 if ( $subscription->{periodicity} == 8 ) {
2554 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2555 if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2556 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day, 0, 3,0 );
2557 $tmpmonth=(($tmpmonth!=9)?($tmpmonth + 3) % 12 :12);
2560 @resultdate = Add_Delta_YMD($year,$month, $day, 0, 3, 0);
2562 if ( $subscription->{periodicity} == 9 ) {
2563 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2564 if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2565 ### BUFIX Seems to need more Than One ?
2566 ($year,$month,$day) = Add_Delta_YM($year,$month, $day, 0, 6 );
2567 $tmpmonth=(($tmpmonth!=6)?($tmpmonth + 6) % 12 :12);
2570 @resultdate = Add_Delta_YM($year,$month, $day, 0, 6);
2572 if ( $subscription->{periodicity} == 10 ) {
2573 @resultdate = Add_Delta_YM($year,$month, $day, 1, 0 );
2575 if ( $subscription->{periodicity} == 11 ) {
2576 @resultdate = Add_Delta_YM($year,$month, $day, 2, 0 );
2579 my $resultdate=sprintf("%04d-%02d-%02d",$resultdate[0],$resultdate[1],$resultdate[2]);
2581 return "$resultdate";
2586 $item = &itemdata($barcode);
2588 Looks up the item with the given barcode, and returns a
2589 reference-to-hash containing information about that item. The keys of
2590 the hash are the fields from the C<items> and C<biblioitems> tables in
2598 my $dbh = C4::Context->dbh;
2599 my $sth = $dbh->prepare(
2600 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2603 $sth->execute($barcode);
2604 my $data = $sth->fetchrow_hashref;
2614 Koha Developement team <info@koha.org>