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 format_date_in_iso);
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 &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 &getroutinglist &delroutingmember &addroutingmember
56 &check_routing &updateClaim &removeMissingIssue
61 =head2 GetSuppliersWithLateIssues
65 C4::Serials - Give functions for serializing.
73 Give all XYZ functions
79 %supplierlist = &GetSuppliersWithLateIssues
81 this function get all suppliers with late issues.
84 the supplierlist into a hash. this hash containts id & name of the supplier
90 sub GetSuppliersWithLateIssues {
91 my $dbh = C4::Context->dbh;
93 SELECT DISTINCT id, name
95 LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
96 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
97 WHERE subscription.subscriptionid = serial.subscriptionid
98 AND (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4)
101 my $sth = $dbh->prepare($query);
104 while ( my ( $id, $name ) = $sth->fetchrow ) {
105 $supplierlist{$id} = $name;
107 return %supplierlist;
114 @issuelist = &GetLateIssues($supplierid)
116 this function select late issues on database
119 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
120 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
127 my ($supplierid) = @_;
128 my $dbh = C4::Context->dbh;
132 SELECT name,title,planneddate,serialseq,serial.subscriptionid
134 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
135 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
136 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
137 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
138 AND subscription.aqbooksellerid=$supplierid
141 $sth = $dbh->prepare($query);
145 SELECT name,title,planneddate,serialseq,serial.subscriptionid
147 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
148 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
149 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
150 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
153 $sth = $dbh->prepare($query);
160 while ( my $line = $sth->fetchrow_hashref ) {
161 $odd++ unless $line->{title} eq $last_title;
162 $line->{title} = "" if $line->{title} eq $last_title;
163 $last_title = $line->{title} if ( $line->{title} );
164 $line->{planneddate} = format_date( $line->{planneddate} );
166 push @issuelist, $line;
168 return $count, @issuelist;
171 =head2 GetSubscriptionHistoryFromSubscriptionId
175 $sth = GetSubscriptionHistoryFromSubscriptionId()
176 this function just prepare the SQL request.
177 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
179 $sth = $dbh->prepare($query).
185 sub GetSubscriptionHistoryFromSubscriptionId() {
186 my $dbh = C4::Context->dbh;
189 FROM subscriptionhistory
190 WHERE subscriptionid = ?
192 return $dbh->prepare($query);
195 =head2 GetSerialStatusFromSerialId
199 $sth = GetSerialStatusFromSerialId();
200 this function just prepare the SQL request.
201 After this function, don't forget to execute it by using $sth->execute($serialid)
203 $sth = $dbh->prepare($query).
209 sub GetSerialStatusFromSerialId() {
210 my $dbh = C4::Context->dbh;
216 return $dbh->prepare($query);
219 =head2 GetSerialInformation
223 $data = GetSerialInformation($serialid);
224 returns a hash containing :
225 items : items marcrecord (can be an array)
227 subscription table field
228 + information about subscription expiration
234 sub GetSerialInformation {
236 my $dbh = C4::Context->dbh;
238 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
239 if (C4::Context->preference('IndependantBranches') &&
240 C4::Context->userenv &&
241 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
243 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
246 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
249 my $rq = $dbh->prepare($query);
250 $rq->execute($serialid);
251 my $data = $rq->fetchrow_hashref;
252 # create item information if we have serialsadditems for this subscription
253 if ( $data->{'serialsadditems'} ) {
254 my $queryitem=$dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
255 $queryitem->execute($serialid);
256 my $itemnumbers=$queryitem->fetchall_arrayref([0]);
257 if (scalar(@$itemnumbers)>0){
258 foreach my $itemnum (@$itemnumbers) {
259 #It is ASSUMED that GetMarcItem ALWAYS WORK...
260 #Maybe GetMarcItem should return values on failure
261 $debug and warn "itemnumber :$itemnum->[0], bibnum :".$data->{'biblionumber'};
263 PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0] , $data );
264 $itemprocessed->{'itemnumber'} = $itemnum->[0];
265 $itemprocessed->{'itemid'} = $itemnum->[0];
266 $itemprocessed->{'serialid'} = $serialid;
267 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
268 push @{ $data->{'items'} }, $itemprocessed;
273 PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
274 $itemprocessed->{'itemid'} = "N$serialid";
275 $itemprocessed->{'serialid'} = $serialid;
276 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
277 $itemprocessed->{'countitems'} = 0;
278 push @{ $data->{'items'} }, $itemprocessed;
281 $data->{ "status" . $data->{'serstatus'} } = 1;
282 $data->{'subscriptionexpired'} =
283 HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'}==1;
284 $data->{'abouttoexpire'} =
285 abouttoexpire( $data->{'subscriptionid'} );
289 =head2 AddItem2Serial
293 $data = AddItem2Serial($serialid,$itemnumber);
294 Adds an itemnumber to Serial record
301 my ( $serialid, $itemnumber ) = @_;
302 my $dbh = C4::Context->dbh;
303 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
304 $rq->execute($serialid, $itemnumber);
308 =head2 UpdateClaimdateIssues
312 UpdateClaimdateIssues($serialids,[$date]);
314 Update Claimdate for issues in @$serialids list with date $date
321 sub UpdateClaimdateIssues {
322 my ( $serialids, $date ) = @_;
323 my $dbh = C4::Context->dbh;
324 $date = strftime("%Y-%m-%d",localtime) unless ($date);
326 UPDATE serial SET claimdate=$date,status=7
327 WHERE serialid in ".join (",",@$serialids);
329 my $rq = $dbh->prepare($query);
334 =head2 GetSubscription
338 $subs = GetSubscription($subscriptionid)
339 this function get the subscription which has $subscriptionid as id.
341 a hashref. This hash containts
342 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
348 sub GetSubscription {
349 my ($subscriptionid) = @_;
350 my $dbh = C4::Context->dbh;
352 SELECT subscription.*,
353 subscriptionhistory.*,
354 subscriptionhistory.enddate as histenddate,
356 aqbooksellers.name AS aqbooksellername,
357 biblio.title AS bibliotitle,
358 subscription.biblionumber as bibnum);
359 if (C4::Context->preference('IndependantBranches') &&
360 C4::Context->userenv &&
361 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
363 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
367 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
368 LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid
369 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
370 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
371 WHERE subscription.subscriptionid = ?
373 # if (C4::Context->preference('IndependantBranches') &&
374 # C4::Context->userenv &&
375 # C4::Context->userenv->{'flags'} != 1){
376 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
377 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
379 $debug and warn "query : $query\nsubsid :$subscriptionid";
380 my $sth = $dbh->prepare($query);
381 $sth->execute($subscriptionid);
382 return $sth->fetchrow_hashref;
385 =head2 GetFullSubscription
389 \@res = GetFullSubscription($subscriptionid)
390 this function read on serial table.
396 sub GetFullSubscription {
397 my ($subscriptionid) = @_;
398 my $dbh = C4::Context->dbh;
400 SELECT serial.serialid,
403 serial.publisheddate,
405 serial.notes as notes,
406 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
407 aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,
408 biblio.title as bibliotitle,
409 subscription.branchcode AS branchcode,
410 subscription.subscriptionid AS subscriptionid |;
411 if (C4::Context->preference('IndependantBranches') &&
412 C4::Context->userenv &&
413 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
415 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
419 LEFT JOIN subscription ON
420 (serial.subscriptionid=subscription.subscriptionid )
421 LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid
422 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
423 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
424 WHERE serial.subscriptionid = ?
426 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
427 serial.subscriptionid
429 $debug and warn "GetFullSubscription query: $query";
430 my $sth = $dbh->prepare($query);
431 $sth->execute($subscriptionid);
432 return $sth->fetchall_arrayref({});
436 =head2 PrepareSerialsData
440 \@res = PrepareSerialsData($serialinfomation)
441 where serialinformation is a hashref array
447 sub PrepareSerialsData{
453 my $aqbooksellername;
457 my $previousnote = "";
459 foreach my $subs ( @$lines ) {
460 $subs->{'publisheddate'} =
461 ( $subs->{'publisheddate'}
462 ? format_date( $subs->{'publisheddate'} )
464 $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
465 $subs->{ "status" . $subs->{'status'} } = 1;
467 # $subs->{'notes'} = $subs->{'notes'} eq $previousnote?"":$subs->{notes};
468 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
469 $year = $subs->{'year'};
474 if ( $tmpresults{$year} ) {
475 push @{ $tmpresults{$year}->{'serials'} }, $subs;
478 $tmpresults{$year} = {
481 # 'startdate'=>format_date($subs->{'startdate'}),
482 'aqbooksellername' => $subs->{'aqbooksellername'},
483 'bibliotitle' => $subs->{'bibliotitle'},
484 'serials' => [$subs],
486 # 'branchcode' => $subs->{'branchcode'},
487 # 'subscriptionid' => $subs->{'subscriptionid'},
491 # $previousnote=$subs->{notes};
493 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
494 push @res, $tmpresults{$key};
496 $res[0]->{'first'}=1;
500 =head2 GetSubscriptionsFromBiblionumber
502 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
503 this function get the subscription list. it reads on subscription table.
505 table of subscription which has the biblionumber given on input arg.
506 each line of this table is a hashref. All hashes containt
507 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
511 sub GetSubscriptionsFromBiblionumber {
512 my ($biblionumber) = @_;
513 my $dbh = C4::Context->dbh;
515 SELECT subscription.*,
517 subscriptionhistory.*,
518 subscriptionhistory.enddate as histenddate,
520 aqbooksellers.name AS aqbooksellername,
521 biblio.title AS bibliotitle
523 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
524 LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid
525 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
526 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
527 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
528 WHERE subscription.biblionumber = ?
530 # if (C4::Context->preference('IndependantBranches') &&
531 # C4::Context->userenv &&
532 # C4::Context->userenv->{'flags'} != 1){
533 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
535 my $sth = $dbh->prepare($query);
536 $sth->execute($biblionumber);
538 while ( my $subs = $sth->fetchrow_hashref ) {
539 $subs->{startdate} = format_date( $subs->{startdate} );
540 $subs->{histstartdate} = format_date( $subs->{histstartdate} );
541 $subs->{histenddate} = format_date( $subs->{histenddate} );
542 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
543 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
544 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
545 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
546 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
547 $subs->{ "status" . $subs->{'status'} } = 1;
548 $subs->{'cannotedit'}=(C4::Context->preference('IndependantBranches') &&
549 C4::Context->userenv &&
550 C4::Context->userenv->{flags} !=1 &&
551 C4::Context->userenv->{branch} && $subs->{branchcode} &&
552 (C4::Context->userenv->{branch} ne $subs->{branchcode}));
553 if ( $subs->{enddate} eq '0000-00-00' ) {
554 $subs->{enddate} = '';
557 $subs->{enddate} = format_date( $subs->{enddate} );
559 $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
560 $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
566 =head2 GetFullSubscriptionsFromBiblionumber
570 \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
571 this function read on serial table.
577 sub GetFullSubscriptionsFromBiblionumber {
578 my ($biblionumber) = @_;
579 my $dbh = C4::Context->dbh;
581 SELECT serial.serialid,
584 serial.publisheddate,
586 serial.notes as notes,
587 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
588 aqbudget.bookfundid,aqbooksellers.name as aqbooksellername,
589 biblio.title as bibliotitle,
590 subscription.branchcode AS branchcode,
591 branches.branchname AS branchname,
592 subscription.subscriptionid AS subscriptionid|;
593 if (C4::Context->preference('IndependantBranches') &&
594 C4::Context->userenv &&
595 C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
597 , ((subscription.branchcode <>\"".C4::Context->userenv->{'branch'}."\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
602 LEFT JOIN subscription ON
603 (serial.subscriptionid=subscription.subscriptionid)
604 LEFT JOIN aqbudget ON subscription.aqbudgetid=aqbudget.aqbudgetid
605 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
606 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
607 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
608 WHERE subscription.biblionumber = ?
610 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
611 serial.subscriptionid
613 my $sth = $dbh->prepare($query);
614 $sth->execute($biblionumber);
615 return $sth->fetchall_arrayref({});
618 =head2 GetSubscriptions
622 @results = GetSubscriptions($title,$ISSN,$biblionumber);
623 this function get all subscriptions which has title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
625 a table of hashref. Each hash containt the subscription.
631 sub GetSubscriptions {
632 my ( $title, $ISSN, $biblionumber ) = @_;
633 #return unless $title or $ISSN or $biblionumber;
634 my $dbh = C4::Context->dbh;
638 SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
640 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
641 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
642 WHERE biblio.biblionumber=?
644 $query.=" ORDER BY title";
645 $debug and warn "GetSubscriptions query: $query";
646 $sth = $dbh->prepare($query);
647 $sth->execute($biblionumber);
650 if ( $ISSN and $title ) {
652 SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
654 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
655 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
656 WHERE (biblioitems.issn = ? or|. join('and ',map{"biblio.title LIKE \"%$_%\""}split (" ",$title))." )";
657 $query.=" ORDER BY title";
658 $debug and warn "GetSubscriptions query: $query";
659 $sth = $dbh->prepare($query);
660 $sth->execute( $ISSN );
665 SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
667 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
668 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
669 WHERE biblioitems.issn LIKE ?
671 $query.=" ORDER BY title";
672 $debug and warn "GetSubscriptions query: $query";
673 $sth = $dbh->prepare($query);
674 $sth->execute( "%" . $ISSN . "%" );
678 SELECT subscription.*,biblio.title,biblioitems.issn,biblio.biblionumber
680 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
681 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
683 ).($title?" and ":""). join('and ',map{"biblio.title LIKE \"%$_%\""} split (" ",$title) );
685 $query.=" ORDER BY title";
686 $debug and warn "GetSubscriptions query: $query";
687 $sth = $dbh->prepare($query);
693 my $previoustitle = "";
695 while ( my $line = $sth->fetchrow_hashref ) {
696 if ( $previoustitle eq $line->{title} ) {
701 $previoustitle = $line->{title};
704 $line->{toggle} = 1 if $odd == 1;
705 $line->{'cannotedit'}=(C4::Context->preference('IndependantBranches') &&
706 C4::Context->userenv &&
707 C4::Context->userenv->{flags} !=1 &&
708 C4::Context->userenv->{branch} && $line->{branchcode} &&
709 (C4::Context->userenv->{branch} ne $line->{branchcode}));
710 push @results, $line;
719 ($totalissues,@serials) = GetSerials($subscriptionid);
720 this function get every serial not arrived for a given subscription
721 as well as the number of issues registered in the database (all types)
722 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
724 FIXME: We should return \@serials.
731 my ($subscriptionid,$count) = @_;
732 my $dbh = C4::Context->dbh;
734 # status = 2 is "arrived"
736 $count=5 unless ($count);
739 "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
741 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
742 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
743 my $sth = $dbh->prepare($query);
744 $sth->execute($subscriptionid);
745 while ( my $line = $sth->fetchrow_hashref ) {
746 $line->{ "status" . $line->{status} } =
747 1; # fills a "statusX" value, used for template status select list
748 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
749 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
750 push @serials, $line;
752 # OK, now add the last 5 issues arrives/missing
754 "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
756 WHERE subscriptionid = ?
757 AND (status in (2,4,5))
758 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
760 $sth = $dbh->prepare($query);
761 $sth->execute($subscriptionid);
762 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
764 $line->{ "status" . $line->{status} } =
765 1; # fills a "statusX" value, used for template status select list
766 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
767 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
768 push @serials, $line;
771 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
772 $sth = $dbh->prepare($query);
773 $sth->execute($subscriptionid);
774 my ($totalissues) = $sth->fetchrow;
775 return ( $totalissues, @serials );
782 ($totalissues,@serials) = GetSerials2($subscriptionid,$status);
783 this function get every serial waited for a given subscription
784 as well as the number of issues registered in the database (all types)
785 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
791 my ($subscription,$status) = @_;
792 my $dbh = C4::Context->dbh;
794 SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
796 WHERE subscriptionid=$subscription AND status IN ($status)
797 ORDER BY publisheddate,serialid DESC
799 $debug and warn "GetSerials2 query: $query";
800 my $sth=$dbh->prepare($query);
803 while(my $line = $sth->fetchrow_hashref) {
804 $line->{"status".$line->{status}} = 1; # fills a "statusX" value, used for template status select list
805 $line->{"planneddate"} = format_date($line->{"planneddate"});
806 $line->{"publisheddate"} = format_date($line->{"publisheddate"});
809 my ($totalissues) = scalar(@serials);
810 return ($totalissues,@serials);
813 =head2 GetLatestSerials
817 \@serials = GetLatestSerials($subscriptionid,$limit)
818 get the $limit's latest serials arrived or missing for a given subscription
820 a ref to a table which it containts all of the latest serials stored into a hash.
826 sub GetLatestSerials {
827 my ( $subscriptionid, $limit ) = @_;
828 my $dbh = C4::Context->dbh;
830 # status = 2 is "arrived"
831 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
833 WHERE subscriptionid = ?
834 AND (status =2 or status=4)
835 ORDER BY publisheddate DESC LIMIT 0,$limit
837 my $sth = $dbh->prepare($strsth);
838 $sth->execute($subscriptionid);
840 while ( my $line = $sth->fetchrow_hashref ) {
841 $line->{ "status" . $line->{status} } =
842 1; # fills a "statusX" value, used for template status select list
843 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
844 push @serials, $line;
850 # WHERE subscriptionid=?
852 # $sth=$dbh->prepare($query);
853 # $sth->execute($subscriptionid);
854 # my ($totalissues) = $sth->fetchrow;
863 $val is a hashref containing all the attributes of the table 'subscription'
864 This function get the next issue for the subscription given on input arg
866 all the input params updated.
874 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
875 # $calculated = $val->{numberingmethod};
876 # # calculate the (expected) value of the next issue recieved.
877 # $newlastvalue1 = $val->{lastvalue1};
878 # # check if we have to increase the new value.
879 # $newinnerloop1 = $val->{innerloop1}+1;
880 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
881 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
882 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
883 # $calculated =~ s/\{X\}/$newlastvalue1/g;
885 # $newlastvalue2 = $val->{lastvalue2};
886 # # check if we have to increase the new value.
887 # $newinnerloop2 = $val->{innerloop2}+1;
888 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
889 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
890 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
891 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
893 # $newlastvalue3 = $val->{lastvalue3};
894 # # check if we have to increase the new value.
895 # $newinnerloop3 = $val->{innerloop3}+1;
896 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
897 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
898 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
899 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
900 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
906 $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3,
907 $newinnerloop1, $newinnerloop2, $newinnerloop3
909 my $pattern = $val->{numberpattern};
910 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
911 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
912 $calculated = $val->{numberingmethod};
913 $newlastvalue1 = $val->{lastvalue1};
914 $newlastvalue2 = $val->{lastvalue2};
915 $newlastvalue3 = $val->{lastvalue3};
916 $newlastvalue1 = $val->{lastvalue1};
917 # check if we have to increase the new value.
918 $newinnerloop1 = $val->{innerloop1} + 1;
919 $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
920 $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
921 $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
922 $calculated =~ s/\{X\}/$newlastvalue1/g;
924 $newlastvalue2 = $val->{lastvalue2};
925 # check if we have to increase the new value.
926 $newinnerloop2 = $val->{innerloop2} + 1;
927 $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
928 $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
929 $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
930 if ( $pattern == 6 ) {
931 if ( $val->{hemisphere} == 2 ) {
932 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
933 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
936 my $newlastvalue2seq = $seasons[$newlastvalue2];
937 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
941 $calculated =~ s/\{Y\}/$newlastvalue2/g;
945 $newlastvalue3 = $val->{lastvalue3};
946 # check if we have to increase the new value.
947 $newinnerloop3 = $val->{innerloop3} + 1;
948 $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
949 $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
950 $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
951 $calculated =~ s/\{Z\}/$newlastvalue3/g;
953 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3 ,
954 $newinnerloop1, $newinnerloop2, $newinnerloop3);
961 $calculated = GetSeq($val)
962 $val is a hashref containing all the attributes of the table 'subscription'
963 this function transforms {X},{Y},{Z} to 150,0,0 for example.
965 the sequence in integer format
973 my $pattern = $val->{numberpattern};
974 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
975 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
976 my $calculated = $val->{numberingmethod};
977 my $x = $val->{'lastvalue1'};
978 $calculated =~ s/\{X\}/$x/g;
979 my $newlastvalue2 = $val->{'lastvalue2'};
980 if ( $pattern == 6 ) {
981 if ( $val->{hemisphere} == 2 ) {
982 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
983 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
986 my $newlastvalue2seq = $seasons[$newlastvalue2];
987 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
991 $calculated =~ s/\{Y\}/$newlastvalue2/g;
993 my $z = $val->{'lastvalue3'};
994 $calculated =~ s/\{Z\}/$z/g;
998 =head2 GetExpirationDate
1000 $sensddate = GetExpirationDate($subscriptionid)
1002 this function return the expiration date for a subscription given on input args.
1009 sub GetExpirationDate {
1010 my ($subscriptionid) = @_;
1011 my $dbh = C4::Context->dbh;
1012 my $subscription = GetSubscription($subscriptionid);
1013 my $enddate = $subscription->{startdate};
1015 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1016 if (($subscription->{periodicity} % 16) >0){
1017 if ( $subscription->{numberlength} ) {
1018 #calculate the date of the last issue.
1019 my $length = $subscription->{numberlength};
1020 for ( my $i = 1 ; $i <= $length ; $i++ ) {
1021 $enddate = GetNextDate( $enddate, $subscription );
1024 elsif ( $subscription->{monthlength} ){
1025 my @date=split (/-/,$subscription->{startdate});
1026 my @enddate = Add_Delta_YM($date[0],$date[1],$date[2],0,$subscription->{monthlength});
1027 $enddate=sprintf("%04d-%02d-%02d",$enddate[0],$enddate[1],$enddate[2]);
1028 } elsif ( $subscription->{weeklength} ){
1029 my @date=split (/-/,$subscription->{startdate});
1030 my @enddate = Add_Delta_Days($date[0],$date[1],$date[2],$subscription->{weeklength}*7);
1031 $enddate=sprintf("%04d-%02d-%02d",$enddate[0],$enddate[1],$enddate[2]);
1039 =head2 CountSubscriptionFromBiblionumber
1043 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1044 this count the number of subscription for a biblionumber given.
1046 the number of subscriptions with biblionumber given on input arg.
1052 sub CountSubscriptionFromBiblionumber {
1053 my ($biblionumber) = @_;
1054 my $dbh = C4::Context->dbh;
1055 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1056 my $sth = $dbh->prepare($query);
1057 $sth->execute($biblionumber);
1058 my $subscriptionsnumber = $sth->fetchrow;
1059 return $subscriptionsnumber;
1062 =head2 ModSubscriptionHistory
1066 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1068 this function modify the history of a subscription. Put your new values on input arg.
1074 sub ModSubscriptionHistory {
1076 $subscriptionid, $histstartdate, $enddate, $recievedlist,
1077 $missinglist, $opacnote, $librariannote
1079 my $dbh = C4::Context->dbh;
1080 my $query = "UPDATE subscriptionhistory
1081 SET histstartdate=?,enddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1082 WHERE subscriptionid=?
1084 my $sth = $dbh->prepare($query);
1085 $recievedlist =~ s/^; //;
1086 $missinglist =~ s/^; //;
1087 $opacnote =~ s/^; //;
1089 $histstartdate, $enddate, $recievedlist, $missinglist,
1090 $opacnote, $librariannote, $subscriptionid
1095 =head2 ModSerialStatus
1099 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1101 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1102 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1108 sub ModSerialStatus {
1109 my ( $serialid, $serialseq, $planneddate,$publisheddate, $status, $notes )
1112 #It is a usual serial
1113 # 1st, get previous status :
1114 my $dbh = C4::Context->dbh;
1115 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1116 my $sth = $dbh->prepare($query);
1117 $sth->execute($serialid);
1118 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1120 # change status & update subscriptionhistory
1122 if ( $status eq 6 ) {
1123 DelIssue( {'serialid'=>$serialid, 'subscriptionid'=>$subscriptionid,'serialseq'=>$serialseq} );
1127 "UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?";
1128 $sth = $dbh->prepare($query);
1129 $sth->execute( $serialseq, $publisheddate, $planneddate, $status,
1130 $notes, $serialid );
1131 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1132 $sth = $dbh->prepare($query);
1133 $sth->execute($subscriptionid);
1134 my $val = $sth->fetchrow_hashref;
1135 unless ( $val->{manualhistory} ) {
1137 "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1138 $sth = $dbh->prepare($query);
1139 $sth->execute($subscriptionid);
1140 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1141 if ( $status eq 2 ) {
1143 $recievedlist .= "; $serialseq"
1144 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1147 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1148 $missinglist .= "; $serialseq"
1150 and not index( "$missinglist", "$serialseq" ) >= 0 );
1151 $missinglist .= "; not issued $serialseq"
1153 and index( "$missinglist", "$serialseq" ) >= 0 );
1155 "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1156 $sth = $dbh->prepare($query);
1157 $recievedlist =~ s/^; //;
1158 $missinglist =~ s/^; //;
1159 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1163 # create new waited entry if needed (ie : was a "waited" and has changed)
1164 if ( $oldstatus eq 1 && $status ne 1 ) {
1165 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1166 $sth = $dbh->prepare($query);
1167 $sth->execute($subscriptionid);
1168 my $val = $sth->fetchrow_hashref;
1173 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
1174 $newinnerloop1, $newinnerloop2, $newinnerloop3
1175 ) = GetNextSeq($val);
1176 # warn "Next Seq End";
1178 # next date (calculated from actual date & frequency parameters)
1179 # warn "publisheddate :$publisheddate ";
1180 my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1181 NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'},
1182 1, $nextpublisheddate, $nextpublisheddate );
1184 "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1185 WHERE subscriptionid = ?";
1186 $sth = $dbh->prepare($query);
1188 $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
1189 $newinnerloop2, $newinnerloop3, $subscriptionid
1192 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1193 if ( $val->{letter} && $status eq 2 && $oldstatus ne 2 ) {
1194 SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1199 =head2 GetNextExpected
1203 $nextexpected = GetNextExpected($subscriptionid)
1205 Get the planneddate for the current expected issue of the subscription.
1211 planneddate => C4::Dates object
1218 sub GetNextExpected($) {
1219 my ($subscriptionid) = @_;
1220 my $dbh = C4::Context->dbh;
1221 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1222 # Each subscription has only one 'expected' issue, with serial.status==1.
1223 $sth->execute( $subscriptionid, 1 );
1224 my ( $nextissue ) = $sth->fetchrow_hashref;
1226 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1227 $sth->execute( $subscriptionid );
1228 $nextissue = $sth->fetchrow_hashref;
1230 $nextissue->{planneddate} = C4::Dates->new($nextissue->{planneddate},'iso');
1234 =head2 ModNextExpected
1238 ModNextExpected($subscriptionid,$date)
1240 Update the planneddate for the current expected issue of the subscription.
1241 This will modify all future prediction results.
1243 C<$date> is a C4::Dates object.
1249 sub ModNextExpected($$) {
1250 my ($subscriptionid,$date) = @_;
1251 my $dbh = C4::Context->dbh;
1252 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1253 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1254 # Each subscription has only one 'expected' issue, with serial.status==1.
1255 $sth->execute( $date->output('iso'),$date->output('iso'), $subscriptionid, 1);
1260 =head2 ModSubscription
1264 this function modify a subscription. Put all new values on input args.
1270 sub ModSubscription {
1272 $auser, $branchcode, $aqbooksellerid, $cost,
1273 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1274 $dow, $irregularity, $numberpattern, $numberlength,
1275 $weeklength, $monthlength, $add1, $every1,
1276 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1277 $add2, $every2, $whenmorethan2, $setto2,
1278 $lastvalue2, $innerloop2, $add3, $every3,
1279 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1280 $numberingmethod, $status, $biblionumber, $callnumber,
1281 $notes, $letter, $hemisphere, $manualhistory,
1282 $internalnotes, $serialsadditems,
1285 # warn $irregularity;
1286 my $dbh = C4::Context->dbh;
1287 my $query = "UPDATE subscription
1288 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1289 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1290 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1291 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1292 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1293 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?, letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?
1294 WHERE subscriptionid = ?";
1295 # warn "query :".$query;
1296 my $sth = $dbh->prepare($query);
1298 $auser, $branchcode, $aqbooksellerid, $cost,
1299 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1300 $dow, "$irregularity", $numberpattern, $numberlength,
1301 $weeklength, $monthlength, $add1, $every1,
1302 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1303 $add2, $every2, $whenmorethan2, $setto2,
1304 $lastvalue2, $innerloop2, $add3, $every3,
1305 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1306 $numberingmethod, $status, $biblionumber, $callnumber,
1307 $notes, $letter, $hemisphere, ($manualhistory?$manualhistory:0),
1308 $internalnotes, $serialsadditems,
1311 my $rows=$sth->rows;
1314 logaction("SERIAL", "MODIFY", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1318 =head2 NewSubscription
1322 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1323 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1324 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1325 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1326 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1327 $numberingmethod, $status, $notes, $serialsadditems)
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,
1352 my $dbh = C4::Context->dbh;
1354 #save subscription (insert into database)
1356 INSERT INTO subscription
1357 (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1358 startdate,periodicity,dow,numberlength,weeklength,monthlength,
1359 add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1360 add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1361 add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1362 numberingmethod, status, notes, letter,firstacquidate,irregularity,
1363 numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems)
1364 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1366 my $sth = $dbh->prepare($query);
1368 $auser, $branchcode,
1369 $aqbooksellerid, $cost,
1370 $aqbudgetid, $biblionumber,
1371 format_date_in_iso($startdate), $periodicity,
1372 $dow, $numberlength,
1373 $weeklength, $monthlength,
1375 $whenmorethan1, $setto1,
1376 $lastvalue1, $innerloop1,
1378 $whenmorethan2, $setto2,
1379 $lastvalue2, $innerloop2,
1381 $whenmorethan3, $setto3,
1382 $lastvalue3, $innerloop3,
1383 $numberingmethod, "$status",
1385 format_date_in_iso($firstacquidate), $irregularity,
1386 $numberpattern, $callnumber,
1387 $hemisphere, $manualhistory,
1388 $internalnotes, $serialsadditems,
1391 #then create the 1st waited number
1392 my $subscriptionid = $dbh->{'mysql_insertid'};
1394 INSERT INTO subscriptionhistory
1395 (biblionumber, subscriptionid, histstartdate, opacnote, librariannote)
1398 $sth = $dbh->prepare($query);
1399 $sth->execute( $biblionumber, $subscriptionid,
1400 format_date_in_iso($startdate),
1401 $notes,$internalnotes );
1403 # reread subscription to get a hash (for calculation of the 1st issue number)
1407 WHERE subscriptionid = ?
1409 $sth = $dbh->prepare($query);
1410 $sth->execute($subscriptionid);
1411 my $val = $sth->fetchrow_hashref;
1413 # calculate issue number
1414 my $serialseq = GetSeq($val);
1417 (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1418 VALUES (?,?,?,?,?,?)
1420 $sth = $dbh->prepare($query);
1422 "$serialseq", $subscriptionid, $biblionumber, 1,
1423 format_date_in_iso($firstacquidate),
1424 format_date_in_iso($firstacquidate)
1427 logaction("SERIAL", "ADD", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1429 #set serial flag on biblio if not already set.
1430 my ($null, ($bib)) = GetBiblio($biblionumber);
1431 if( ! $bib->{'serial'} ) {
1432 my $record = GetMarcBiblio($biblionumber);
1433 my ($tag,$subf) = GetMarcFromKohaField('biblio.serial',$bib->{'frameworkcode'});
1436 $record->field($tag)->update( $subf => 1 );
1439 ModBiblio($record,$biblionumber,$bib->{'frameworkcode'});
1441 return $subscriptionid;
1444 =head2 ReNewSubscription
1448 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1450 this function renew a subscription with values given on input args.
1456 sub ReNewSubscription {
1457 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength,
1458 $monthlength, $note )
1460 my $dbh = C4::Context->dbh;
1461 my $subscription = GetSubscription($subscriptionid);
1465 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1466 WHERE biblio.biblionumber=?
1468 my $sth = $dbh->prepare($query);
1469 $sth->execute( $subscription->{biblionumber} );
1470 my $biblio = $sth->fetchrow_hashref;
1471 if (C4::Context->preference("RenewSerialAddsSuggestion")){
1473 $user, $subscription->{bibliotitle},
1474 $biblio->{author}, $biblio->{publishercode},
1475 $biblio->{note}, '',
1478 $subscription->{biblionumber}
1482 # renew subscription
1485 SET startdate=?,numberlength=?,weeklength=?,monthlength=?
1486 WHERE subscriptionid=?
1488 $sth = $dbh->prepare($query);
1489 $sth->execute( format_date_in_iso($startdate),
1490 $numberlength, $weeklength, $monthlength, $subscriptionid );
1492 logaction("SERIAL", "RENEW", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1499 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1501 Create a new issue stored on the database.
1502 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1509 my ( $serialseq, $subscriptionid, $biblionumber, $status,
1510 $planneddate, $publisheddate, $notes )
1512 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1514 my $dbh = C4::Context->dbh;
1517 (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1518 VALUES (?,?,?,?,?,?,?)
1520 my $sth = $dbh->prepare($query);
1521 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status,
1522 $publisheddate, $planneddate,$notes );
1523 my $serialid=$dbh->{'mysql_insertid'};
1525 SELECT missinglist,recievedlist
1526 FROM subscriptionhistory
1527 WHERE subscriptionid=?
1529 $sth = $dbh->prepare($query);
1530 $sth->execute($subscriptionid);
1531 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1533 if ( $status eq 2 ) {
1534 ### TODO Add a feature that improves recognition and description.
1535 ### As such count (serialseq) i.e. : N18,2(N19),N20
1536 ### Would use substr and index But be careful to previous presence of ()
1537 $recievedlist .= "; $serialseq" unless (index($recievedlist,$serialseq)>0);
1539 if ( $status eq 4 ) {
1540 $missinglist .= "; $serialseq" unless (index($missinglist,$serialseq)>0);
1543 UPDATE subscriptionhistory
1544 SET recievedlist=?, missinglist=?
1545 WHERE subscriptionid=?
1547 $sth = $dbh->prepare($query);
1548 $recievedlist =~ s/^; //;
1549 $missinglist =~ s/^; //;
1550 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1554 =head2 ItemizeSerials
1558 ItemizeSerials($serialid, $info);
1559 $info is a hashref containing barcode branch, itemcallnumber, status, location
1560 $serialid the serialid
1562 1 if the itemize is a succes.
1563 0 and @error else. @error containts the list of errors found.
1569 sub ItemizeSerials {
1570 my ( $serialid, $info ) = @_;
1571 my $now = POSIX::strftime( "%Y-%m-%d",localtime );
1573 my $dbh = C4::Context->dbh;
1579 my $sth = $dbh->prepare($query);
1580 $sth->execute($serialid);
1581 my $data = $sth->fetchrow_hashref;
1582 if ( C4::Context->preference("RoutingSerials") ) {
1584 # check for existing biblioitem relating to serial issue
1585 my ( $count, @results ) =
1586 GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1588 for ( my $i = 0 ; $i < $count ; $i++ ) {
1589 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' ('
1590 . $data->{'planneddate'}
1593 $bibitemno = $results[$i]->{'biblioitemnumber'};
1597 if ( $bibitemno == 0 ) {
1599 # warn "need to add new biblioitem so copy last one and make minor changes";
1602 "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC"
1604 $sth->execute( $data->{'biblionumber'} );
1605 my $biblioitem = $sth->fetchrow_hashref;
1606 $biblioitem->{'volumedate'} =
1607 format_date_in_iso( $data->{planneddate} );
1608 $biblioitem->{'volumeddesc'} =
1609 $data->{serialseq} . ' ('
1610 . format_date( $data->{'planneddate'} ) . ')';
1611 $biblioitem->{'dewey'} = $info->{itemcallnumber};
1613 #FIXME HDL : I don't understand why you need to call newbiblioitem, as the biblioitem should already be somewhere.
1614 # so I comment it, we can speak of it when you want
1615 # newbiblioitems has been removed from Biblio.pm, as it has a deprecated API now
1616 # if ( $info->{barcode} )
1617 # { # only make biblioitem if we are going to make item also
1618 # $bibitemno = newbiblioitem($biblioitem);
1623 my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1624 if ( $info->{barcode} ) {
1626 my $exists = itemdata( $info->{'barcode'} );
1627 push @errors, "barcode_not_unique" if ($exists);
1629 my $marcrecord = MARC::Record->new();
1630 my ( $tag, $subfield ) =
1631 GetMarcFromKohaField( "items.barcode", $fwk );
1633 MARC::Field->new( "$tag", '', '',
1634 "$subfield" => $info->{barcode} );
1635 $marcrecord->insert_fields_ordered($newField);
1636 if ( $info->{branch} ) {
1637 my ( $tag, $subfield ) =
1638 GetMarcFromKohaField( "items.homebranch",
1641 #warn "items.homebranch : $tag , $subfield";
1642 if ( $marcrecord->field($tag) ) {
1643 $marcrecord->field($tag)
1644 ->add_subfields( "$subfield" => $info->{branch} );
1648 MARC::Field->new( "$tag", '', '',
1649 "$subfield" => $info->{branch} );
1650 $marcrecord->insert_fields_ordered($newField);
1652 ( $tag, $subfield ) =
1653 GetMarcFromKohaField( "items.holdingbranch",
1656 #warn "items.holdingbranch : $tag , $subfield";
1657 if ( $marcrecord->field($tag) ) {
1658 $marcrecord->field($tag)
1659 ->add_subfields( "$subfield" => $info->{branch} );
1663 MARC::Field->new( "$tag", '', '',
1664 "$subfield" => $info->{branch} );
1665 $marcrecord->insert_fields_ordered($newField);
1668 if ( $info->{itemcallnumber} ) {
1669 my ( $tag, $subfield ) =
1670 GetMarcFromKohaField( "items.itemcallnumber",
1673 #warn "items.itemcallnumber : $tag , $subfield";
1674 if ( $marcrecord->field($tag) ) {
1675 $marcrecord->field($tag)
1676 ->add_subfields( "$subfield" => $info->{itemcallnumber} );
1680 MARC::Field->new( "$tag", '', '',
1681 "$subfield" => $info->{itemcallnumber} );
1682 $marcrecord->insert_fields_ordered($newField);
1685 if ( $info->{notes} ) {
1686 my ( $tag, $subfield ) =
1687 GetMarcFromKohaField( "items.itemnotes", $fwk );
1689 # warn "items.itemnotes : $tag , $subfield";
1690 if ( $marcrecord->field($tag) ) {
1691 $marcrecord->field($tag)
1692 ->add_subfields( "$subfield" => $info->{notes} );
1696 MARC::Field->new( "$tag", '', '',
1697 "$subfield" => $info->{notes} );
1698 $marcrecord->insert_fields_ordered($newField);
1701 if ( $info->{location} ) {
1702 my ( $tag, $subfield ) =
1703 GetMarcFromKohaField( "items.location", $fwk );
1705 # warn "items.location : $tag , $subfield";
1706 if ( $marcrecord->field($tag) ) {
1707 $marcrecord->field($tag)
1708 ->add_subfields( "$subfield" => $info->{location} );
1712 MARC::Field->new( "$tag", '', '',
1713 "$subfield" => $info->{location} );
1714 $marcrecord->insert_fields_ordered($newField);
1717 if ( $info->{status} ) {
1718 my ( $tag, $subfield ) =
1719 GetMarcFromKohaField( "items.notforloan",
1722 # warn "items.notforloan : $tag , $subfield";
1723 if ( $marcrecord->field($tag) ) {
1724 $marcrecord->field($tag)
1725 ->add_subfields( "$subfield" => $info->{status} );
1729 MARC::Field->new( "$tag", '', '',
1730 "$subfield" => $info->{status} );
1731 $marcrecord->insert_fields_ordered($newField);
1734 if ( C4::Context->preference("RoutingSerials") ) {
1735 my ( $tag, $subfield ) =
1736 GetMarcFromKohaField( "items.dateaccessioned",
1738 if ( $marcrecord->field($tag) ) {
1739 $marcrecord->field($tag)
1740 ->add_subfields( "$subfield" => $now );
1744 MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1745 $marcrecord->insert_fields_ordered($newField);
1748 AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1751 return ( 0, @errors );
1755 =head2 HasSubscriptionExpired
1759 $has_expired = HasSubscriptionExpired($subscriptionid)
1761 the subscription has expired when the next issue to arrive is out of subscription limit.
1764 0 if the subscription has not expired
1765 1 if the subscription has expired
1766 2 if has subscription does not have a valid expiration date set
1772 sub HasSubscriptionExpired {
1773 my ($subscriptionid) = @_;
1774 my $dbh = C4::Context->dbh;
1775 my $subscription = GetSubscription($subscriptionid);
1776 if (($subscription->{periodicity} % 16)>0){
1777 my $expirationdate = GetExpirationDate($subscriptionid);
1779 SELECT max(planneddate)
1781 WHERE subscriptionid=?
1783 my $sth = $dbh->prepare($query);
1784 $sth->execute($subscriptionid);
1785 my ($res) = $sth->fetchrow ;
1786 my @res=split (/-/,$res);
1787 my @endofsubscriptiondate=split(/-/,$expirationdate);
1788 return 2 if (scalar(@res)!=3 || scalar(@endofsubscriptiondate)!=3||not check_date(@res) || not check_date(@endofsubscriptiondate));
1789 return 1 if ( (@endofsubscriptiondate && Delta_Days($res[0],$res[1],$res[2],
1790 $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) <= 0)
1794 if ($subscription->{'numberlength'}){
1795 my $countreceived=countissuesfrom($subscriptionid,$subscription->{'startdate'});
1796 return 1 if ($countreceived >$subscription->{'numberlength'});
1802 return 0; # Notice that you'll never get here.
1805 =head2 DelSubscription
1809 DelSubscription($subscriptionid)
1810 this function delete the subscription which has $subscriptionid as id.
1816 sub DelSubscription {
1817 my ($subscriptionid) = @_;
1818 my $dbh = C4::Context->dbh;
1819 $subscriptionid = $dbh->quote($subscriptionid);
1820 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1822 "DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1823 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1825 logaction("SERIAL", "DELETE", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
1832 DelIssue($serialseq,$subscriptionid)
1833 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1840 my ( $dataissue) = @_;
1841 my $dbh = C4::Context->dbh;
1842 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1847 AND subscriptionid= ?
1849 my $mainsth = $dbh->prepare($query);
1850 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'});
1852 #Delete element from subscription history
1853 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1854 my $sth = $dbh->prepare($query);
1855 $sth->execute($dataissue->{'subscriptionid'});
1856 my $val = $sth->fetchrow_hashref;
1857 unless ( $val->{manualhistory} ) {
1859 SELECT * FROM subscriptionhistory
1860 WHERE subscriptionid= ?
1862 my $sth = $dbh->prepare($query);
1863 $sth->execute($dataissue->{'subscriptionid'});
1864 my $data = $sth->fetchrow_hashref;
1865 my $serialseq= $dataissue->{'serialseq'};
1866 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1867 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1868 my $strsth = "UPDATE subscriptionhistory SET "
1870 map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data )
1871 . " WHERE subscriptionid=?";
1872 $sth = $dbh->prepare($strsth);
1873 $sth->execute($dataissue->{'subscriptionid'});
1876 return $mainsth->rows;
1879 =head2 GetLateOrMissingIssues
1883 ($count,@issuelist) = &GetLateMissingIssues($supplierid,$serialid)
1885 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1888 a count of the number of missing issues
1889 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1890 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1896 sub GetLateOrMissingIssues {
1897 my ( $supplierid, $serialid,$order ) = @_;
1898 my $dbh = C4::Context->dbh;
1902 $byserial = "and serialid = " . $serialid;
1910 $sth = $dbh->prepare(
1919 serial.subscriptionid,
1922 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1923 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1924 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1925 WHERE subscription.subscriptionid = serial.subscriptionid
1926 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3))
1927 AND subscription.aqbooksellerid=$supplierid
1933 $sth = $dbh->prepare(
1942 serial.subscriptionid,
1945 LEFT JOIN subscription
1946 ON serial.subscriptionid=subscription.subscriptionid
1948 ON subscription.biblionumber=biblio.biblionumber
1949 LEFT JOIN aqbooksellers
1950 ON subscription.aqbooksellerid = aqbooksellers.id
1952 subscription.subscriptionid = serial.subscriptionid
1953 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3))
1963 while ( my $line = $sth->fetchrow_hashref ) {
1964 $odd++ unless $line->{title} eq $last_title;
1965 $last_title = $line->{title} if ( $line->{title} );
1966 $line->{planneddate} = format_date( $line->{planneddate} );
1967 $line->{claimdate} = format_date( $line->{claimdate} );
1968 $line->{"status".$line->{status}} = 1;
1969 $line->{'odd'} = 1 if $odd % 2;
1971 push @issuelist, $line;
1973 return $count, @issuelist;
1976 =head2 removeMissingIssue
1980 removeMissingIssue($subscriptionid)
1982 this function removes an issue from being part of the missing string in
1983 subscriptionlist.missinglist column
1985 called when a missing issue is found from the serials-recieve.pl file
1991 sub removeMissingIssue {
1992 my ( $sequence, $subscriptionid ) = @_;
1993 my $dbh = C4::Context->dbh;
1996 "SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1997 $sth->execute($subscriptionid);
1998 my $data = $sth->fetchrow_hashref;
1999 my $missinglist = $data->{'missinglist'};
2000 my $missinglistbefore = $missinglist;
2002 # warn $missinglist." before";
2003 $missinglist =~ s/($sequence)//;
2005 # warn $missinglist." after";
2006 if ( $missinglist ne $missinglistbefore ) {
2007 $missinglist =~ s/\|\s\|/\|/g;
2008 $missinglist =~ s/^\| //g;
2009 $missinglist =~ s/\|$//g;
2010 my $sth2 = $dbh->prepare(
2011 "UPDATE subscriptionhistory
2013 WHERE subscriptionid = ?"
2015 $sth2->execute( $missinglist, $subscriptionid );
2023 &updateClaim($serialid)
2025 this function updates the time when a claim is issued for late/missing items
2027 called from claims.pl file
2034 my ($serialid) = @_;
2035 my $dbh = C4::Context->dbh;
2036 my $sth = $dbh->prepare(
2037 "UPDATE serial SET claimdate = now()
2041 $sth->execute($serialid);
2044 =head2 getsupplierbyserialid
2048 ($result) = &getsupplierbyserialid($serialid)
2050 this function is used to find the supplier id given a serial id
2053 hashref containing serialid, subscriptionid, and aqbooksellerid
2059 sub getsupplierbyserialid {
2060 my ($serialid) = @_;
2061 my $dbh = C4::Context->dbh;
2062 my $sth = $dbh->prepare(
2063 "SELECT serialid, serial.subscriptionid, aqbooksellerid
2065 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
2069 $sth->execute($serialid);
2070 my $line = $sth->fetchrow_hashref;
2071 my $result = $line->{'aqbooksellerid'};
2075 =head2 check_routing
2079 ($result) = &check_routing($subscriptionid)
2081 this function checks to see if a serial has a routing list and returns the count of routingid
2082 used to show either an 'add' or 'edit' link
2089 my ($subscriptionid) = @_;
2090 my $dbh = C4::Context->dbh;
2091 my $sth = $dbh->prepare(
2092 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2093 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2094 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2097 $sth->execute($subscriptionid);
2098 my $line = $sth->fetchrow_hashref;
2099 my $result = $line->{'routingids'};
2103 =head2 addroutingmember
2107 &addroutingmember($borrowernumber,$subscriptionid)
2109 this function takes a borrowernumber and subscriptionid and add the member to the
2110 routing list for that serial subscription and gives them a rank on the list
2111 of either 1 or highest current rank + 1
2117 sub addroutingmember {
2118 my ( $borrowernumber, $subscriptionid ) = @_;
2120 my $dbh = C4::Context->dbh;
2123 "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?"
2125 $sth->execute($subscriptionid);
2126 while ( my $line = $sth->fetchrow_hashref ) {
2127 if ( $line->{'rank'} > 0 ) {
2128 $rank = $line->{'rank'} + 1;
2136 "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)"
2138 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2141 =head2 reorder_members
2145 &reorder_members($subscriptionid,$routingid,$rank)
2147 this function is used to reorder the routing list
2149 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2150 - it gets all members on list puts their routingid's into an array
2151 - removes the one in the array that is $routingid
2152 - then reinjects $routingid at point indicated by $rank
2153 - then update the database with the routingids in the new order
2159 sub reorder_members {
2160 my ( $subscriptionid, $routingid, $rank ) = @_;
2161 my $dbh = C4::Context->dbh;
2164 "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC"
2166 $sth->execute($subscriptionid);
2168 while ( my $line = $sth->fetchrow_hashref ) {
2169 push( @result, $line->{'routingid'} );
2172 # To find the matching index
2174 my $key = -1; # to allow for 0 being a valid response
2175 for ( $i = 0 ; $i < @result ; $i++ ) {
2176 if ( $routingid == $result[$i] ) {
2177 $key = $i; # save the index
2182 # if index exists in array then move it to new position
2183 if ( $key > -1 && $rank > 0 ) {
2184 my $new_rank = $rank -
2185 1; # $new_rank is what you want the new index to be in the array
2186 my $moving_item = splice( @result, $key, 1 );
2187 splice( @result, $new_rank, 0, $moving_item );
2189 for ( my $j = 0 ; $j < @result ; $j++ ) {
2191 $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '"
2193 . "' WHERE routingid = '"
2200 =head2 delroutingmember
2204 &delroutingmember($routingid,$subscriptionid)
2206 this function either deletes one member from routing list if $routingid exists otherwise
2207 deletes all members from the routing list
2213 sub delroutingmember {
2215 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2216 my ( $routingid, $subscriptionid ) = @_;
2217 my $dbh = C4::Context->dbh;
2221 "DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2222 $sth->execute($routingid);
2223 reorder_members( $subscriptionid, $routingid );
2228 "DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2229 $sth->execute($subscriptionid);
2233 =head2 getroutinglist
2237 ($count,@routinglist) = &getroutinglist($subscriptionid)
2239 this gets the info from the subscriptionroutinglist for $subscriptionid
2242 a count of the number of members on routinglist
2243 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2244 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2250 sub getroutinglist {
2251 my ($subscriptionid) = @_;
2252 my $dbh = C4::Context->dbh;
2253 my $sth = $dbh->prepare(
2254 "SELECT routingid, borrowernumber,
2255 ranking, biblionumber
2257 LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2258 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2261 $sth->execute($subscriptionid);
2264 while ( my $line = $sth->fetchrow_hashref ) {
2266 push( @routinglist, $line );
2268 return ( $count, @routinglist );
2271 =head2 countissuesfrom
2275 $result = &countissuesfrom($subscriptionid,$startdate)
2282 sub countissuesfrom {
2283 my ($subscriptionid,$startdate) = @_;
2284 my $dbh = C4::Context->dbh;
2288 WHERE subscriptionid=?
2289 AND serial.publisheddate>?
2291 my $sth=$dbh->prepare($query);
2292 $sth->execute($subscriptionid, $startdate);
2293 my ($countreceived)=$sth->fetchrow;
2294 return $countreceived;
2297 =head2 abouttoexpire
2301 $result = &abouttoexpire($subscriptionid)
2303 this function alerts you to the penultimate issue for a serial subscription
2305 returns 1 - if this is the penultimate issue
2313 my ($subscriptionid) = @_;
2314 my $dbh = C4::Context->dbh;
2315 my $subscription = GetSubscription($subscriptionid);
2316 my $per = $subscription->{'periodicity'};
2318 my $expirationdate = GetExpirationDate($subscriptionid);
2321 "select max(planneddate) from serial where subscriptionid=?");
2322 $sth->execute($subscriptionid);
2323 my ($res) = $sth->fetchrow ;
2324 # warn "date expiration : ".$expirationdate." date courante ".$res;
2325 my @res=split /-/,$res;
2326 @res=Date::Calc::Today if ($res[0]*$res[1]==0);
2327 my @endofsubscriptiondate=split/-/,$expirationdate;
2329 if ( $per == 1 ) {$x=7;}
2330 if ( $per == 2 ) {$x=7; }
2331 if ( $per == 3 ) {$x=14;}
2332 if ( $per == 4 ) { $x = 21; }
2333 if ( $per == 5 ) { $x = 31; }
2334 if ( $per == 6 ) { $x = 62; }
2335 if ( $per == 7 || $per == 8 ) { $x = 93; }
2336 if ( $per == 9 ) { $x = 190; }
2337 if ( $per == 10 ) { $x = 365; }
2338 if ( $per == 11 ) { $x = 730; }
2339 my @datebeforeend=Add_Delta_Days( $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2],
2340 - (3 * $x)) if (@endofsubscriptiondate && $endofsubscriptiondate[0]*$endofsubscriptiondate[1]*$endofsubscriptiondate[2]);
2341 # warn "DATE BEFORE END: $datebeforeend";
2342 return 1 if ( @res &&
2344 Delta_Days($res[0],$res[1],$res[2],
2345 $datebeforeend[0],$datebeforeend[1],$datebeforeend[2]) <= 0) &&
2346 (@endofsubscriptiondate &&
2347 Delta_Days($res[0],$res[1],$res[2],
2348 $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) >= 0) );
2350 } elsif ($subscription->{numberlength}>0) {
2351 return (countissuesfrom($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength}-1);
2357 ($resultdate) = &GetNextDate($planneddate,$subscription)
2359 this function is an extension of GetNextDate which allows for checking for irregularity
2361 it takes the planneddate and will return the next issue's date and will skip dates if there
2362 exists an irregularity
2363 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2364 skipped then the returned date will be 2007-05-10
2367 $resultdate - then next date in the sequence
2369 Return 0 if periodicity==0
2372 sub in_array { # used in next sub down
2373 my ($val,@elements) = @_;
2374 foreach my $elem(@elements) {
2382 sub GetNextDate(@) {
2383 my ( $planneddate, $subscription ) = @_;
2384 my @irreg = split( /\,/, $subscription->{irregularity} );
2386 #date supposed to be in ISO.
2388 my ( $year, $month, $day ) = split(/-/, $planneddate);
2389 return undef if not check_date($year, $month, $day);
2390 $month=1 unless ($month);
2391 $day=1 unless ($day);
2394 # warn "DOW $dayofweek";
2395 if ( $subscription->{periodicity} % 16 == 0 ) { # 'without regularity' || 'irregular'
2399 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2400 # renaming this pattern from 1/day to " n / week ".
2401 if ( $subscription->{periodicity} == 1 ) {
2402 my $dayofweek = eval{Day_of_Week( $year,$month, $day )};
2403 if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2405 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2406 $dayofweek = 0 if ( $dayofweek == 7 );
2407 if ( in_array( ($dayofweek + 1), @irreg ) ) {
2408 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 1 );
2412 @resultdate = Add_Delta_Days($year,$month, $day , 1 );
2416 if ( $subscription->{periodicity} == 2 ) {
2417 my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2418 if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2420 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2421 #FIXME: if two consecutive irreg, do we only skip one?
2422 if ( $irreg[$i] == (($wkno!=51)?($wkno +1) % 52 :52)) {
2423 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 7 );
2424 $wkno=(($wkno!=51)?($wkno +1) % 52 :52);
2427 @resultdate = Add_Delta_Days( $year,$month, $day, 7);
2431 if ( $subscription->{periodicity} == 3 ) {
2432 my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2433 if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
2435 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2436 if ( $irreg[$i] == (($wkno!=50)?($wkno +2) % 52 :52)) {
2437 ### BUGFIX was previously +1 ^
2438 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 14 );
2439 $wkno=(($wkno!=50)?($wkno +2) % 52 :52);
2442 @resultdate = Add_Delta_Days($year,$month, $day , 14 );
2446 if ( $subscription->{periodicity} == 4 ) {
2447 my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
2448 if ($@){warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@";}
2450 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2451 if ( $irreg[$i] == (($wkno!=49)?($wkno +3) % 52 :52)) {
2452 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 21 );
2453 $wkno=(($wkno!=49)?($wkno +3) % 52 :52);
2456 @resultdate = Add_Delta_Days($year,$month, $day , 21 );
2459 my $tmpmonth=$month;
2460 if ($year && $month && $day){
2461 if ( $subscription->{periodicity} == 5 ) {
2462 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2463 if ( $irreg[$i] == (($tmpmonth!=11)?($tmpmonth +1) % 12 :12)) {
2464 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,1,0 );
2465 $tmpmonth=(($tmpmonth!=11)?($tmpmonth +1) % 12 :12);
2468 @resultdate = Add_Delta_YMD($year,$month, $day ,0,1,0 );
2470 if ( $subscription->{periodicity} == 6 ) {
2471 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2472 if ( $irreg[$i] == (($tmpmonth!=10)?($tmpmonth +2) % 12 :12)) {
2473 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,2,0 );
2474 $tmpmonth=(($tmpmonth!=10)?($tmpmonth + 2) % 12 :12);
2477 @resultdate = Add_Delta_YMD($year,$month, $day, 0, 2,0 );
2479 if ( $subscription->{periodicity} == 7 ) {
2480 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2481 if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2482 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day, 0, 3,0 );
2483 $tmpmonth=(($tmpmonth!=9)?($tmpmonth + 3) % 12 :12);
2486 @resultdate = Add_Delta_YMD($year,$month, $day, 0, 3, 0);
2488 if ( $subscription->{periodicity} == 8 ) {
2489 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2490 if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2491 ($year,$month,$day) = Add_Delta_YMD($year,$month, $day, 0, 3,0 );
2492 $tmpmonth=(($tmpmonth!=9)?($tmpmonth + 3) % 12 :12);
2495 @resultdate = Add_Delta_YMD($year,$month, $day, 0, 3, 0);
2497 if ( $subscription->{periodicity} == 9 ) {
2498 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2499 if ( $irreg[$i] == (($tmpmonth!=9)?($tmpmonth +3) % 12 :12)) {
2500 ### BUFIX Seems to need more Than One ?
2501 ($year,$month,$day) = Add_Delta_YM($year,$month, $day, 0, 6 );
2502 $tmpmonth=(($tmpmonth!=6)?($tmpmonth + 6) % 12 :12);
2505 @resultdate = Add_Delta_YM($year,$month, $day, 0, 6);
2507 if ( $subscription->{periodicity} == 10 ) {
2508 @resultdate = Add_Delta_YM($year,$month, $day, 1, 0 );
2510 if ( $subscription->{periodicity} == 11 ) {
2511 @resultdate = Add_Delta_YM($year,$month, $day, 2, 0 );
2514 my $resultdate=sprintf("%04d-%02d-%02d",$resultdate[0],$resultdate[1],$resultdate[2]);
2516 # warn "dateNEXTSEQ : ".$resultdate;
2517 return "$resultdate";
2522 $item = &itemdata($barcode);
2524 Looks up the item with the given barcode, and returns a
2525 reference-to-hash containing information about that item. The keys of
2526 the hash are the fields from the C<items> and C<biblioitems> tables in
2534 my $dbh = C4::Context->dbh;
2535 my $sth = $dbh->prepare(
2536 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2539 $sth->execute($barcode);
2540 my $data = $sth->fetchrow_hashref;
2550 Koha Developement team <info@koha.org>