3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use C4::Dates qw(format_date format_date_in_iso);
23 use Date::Calc qw(:all);
24 use POSIX qw(strftime);
32 use C4::Log; # logaction
35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 $VERSION = 3.01; # set version for version checking
42 &NewSubscription &ModSubscription &DelSubscription &GetSubscriptions
43 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
44 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
45 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
47 &GetNextSeq &NewIssue &ItemizeSerials &GetSerials
48 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
49 &ReNewSubscription &GetLateIssues &GetLateOrMissingIssues
50 &GetSerialInformation &AddItem2Serial
51 &PrepareSerialsData &GetNextExpected &ModNextExpected
53 &UpdateClaimdateIssues
54 &GetSuppliersWithLateIssues &getsupplierbyserialid
55 &GetDistributedTo &SetDistributedTo
56 &getroutinglist &delroutingmember &addroutingmember
58 &check_routing &updateClaim &removeMissingIssue
67 C4::Serials - Serials Module Functions
75 Functions for handling subscriptions, claims routing etc.
80 =head2 GetSuppliersWithLateIssues
82 $supplierlist = GetSuppliersWithLateIssues()
84 this function get all suppliers with late issues.
87 an array_ref of suppliers each entry is a hash_ref containing id and name
88 the array is in name order
92 sub GetSuppliersWithLateIssues {
93 my $dbh = C4::Context->dbh;
95 SELECT DISTINCT aqbooksellerid as id, aqbooksellers.name as name
97 LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
98 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
99 WHERE id > 0 AND (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4) ORDER BY name|;
100 return $dbh->selectall_arrayref($query, { Slice => {} });
105 @issuelist = GetLateIssues($supplierid)
107 this function selects late issues from the database
110 the issuelist as an array. Each element of this array contains a hashi_ref containing
111 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
116 my ($supplierid) = @_;
117 my $dbh = C4::Context->dbh;
121 SELECT name,title,planneddate,serialseq,serial.subscriptionid
123 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
124 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
125 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
126 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
127 AND subscription.aqbooksellerid=$supplierid
130 $sth = $dbh->prepare($query);
133 SELECT name,title,planneddate,serialseq,serial.subscriptionid
135 LEFT JOIN serial ON subscription.subscriptionid = serial.subscriptionid
136 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
137 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
138 WHERE ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3)
141 $sth = $dbh->prepare($query);
147 while ( my $line = $sth->fetchrow_hashref ) {
148 $odd++ unless $line->{title} eq $last_title;
149 $line->{title} = "" if $line->{title} eq $last_title;
150 $last_title = $line->{title} if ( $line->{title} );
151 $line->{planneddate} = format_date( $line->{planneddate} );
152 push @issuelist, $line;
157 =head2 GetSubscriptionHistoryFromSubscriptionId
159 $sth = GetSubscriptionHistoryFromSubscriptionId()
160 this function prepares the SQL request and returns the statement handle
161 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
165 sub GetSubscriptionHistoryFromSubscriptionId() {
166 my $dbh = C4::Context->dbh;
169 FROM subscriptionhistory
170 WHERE subscriptionid = ?
172 return $dbh->prepare($query);
175 =head2 GetSerialStatusFromSerialId
177 $sth = GetSerialStatusFromSerialId();
178 this function returns a statement handle
179 After this function, don't forget to execute it by using $sth->execute($serialid)
181 $sth = $dbh->prepare($query).
185 sub GetSerialStatusFromSerialId() {
186 my $dbh = C4::Context->dbh;
192 return $dbh->prepare($query);
195 =head2 GetSerialInformation
198 $data = GetSerialInformation($serialid);
199 returns a hash_ref containing :
200 items : items marcrecord (can be an array)
202 subscription table field
203 + information about subscription expiration
207 sub GetSerialInformation {
209 my $dbh = C4::Context->dbh;
211 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
212 if ( C4::Context->preference('IndependantBranches')
213 && C4::Context->userenv
214 && C4::Context->userenv->{'flags'} != 1
215 && C4::Context->userenv->{'branch'} ) {
217 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
220 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
223 my $rq = $dbh->prepare($query);
224 $rq->execute($serialid);
225 my $data = $rq->fetchrow_hashref;
227 # create item information if we have serialsadditems for this subscription
228 if ( $data->{'serialsadditems'} ) {
229 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
230 $queryitem->execute($serialid);
231 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
232 if ( scalar(@$itemnumbers) > 0 ) {
233 foreach my $itemnum (@$itemnumbers) {
235 #It is ASSUMED that GetMarcItem ALWAYS WORK...
236 #Maybe GetMarcItem should return values on failure
237 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
238 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0], $data );
239 $itemprocessed->{'itemnumber'} = $itemnum->[0];
240 $itemprocessed->{'itemid'} = $itemnum->[0];
241 $itemprocessed->{'serialid'} = $serialid;
242 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
243 push @{ $data->{'items'} }, $itemprocessed;
246 my $itemprocessed = PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
247 $itemprocessed->{'itemid'} = "N$serialid";
248 $itemprocessed->{'serialid'} = $serialid;
249 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
250 $itemprocessed->{'countitems'} = 0;
251 push @{ $data->{'items'} }, $itemprocessed;
254 $data->{ "status" . $data->{'serstatus'} } = 1;
255 $data->{'subscriptionexpired'} = HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
256 $data->{'abouttoexpire'} = abouttoexpire( $data->{'subscriptionid'} );
260 =head2 AddItem2Serial
262 $rows = AddItem2Serial($serialid,$itemnumber);
263 Adds an itemnumber to Serial record
264 returns the number of rows affected
269 my ( $serialid, $itemnumber ) = @_;
270 my $dbh = C4::Context->dbh;
271 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
272 $rq->execute( $serialid, $itemnumber );
276 =head2 UpdateClaimdateIssues
278 UpdateClaimdateIssues($serialids,[$date]);
280 Update Claimdate for issues in @$serialids list with date $date
285 sub UpdateClaimdateIssues {
286 my ( $serialids, $date ) = @_;
287 my $dbh = C4::Context->dbh;
288 $date = strftime( "%Y-%m-%d", localtime ) unless ($date);
290 UPDATE serial SET claimdate=$date,status=7
291 WHERE serialid in (" . join( ",", @$serialids ) . ")";
292 my $rq = $dbh->prepare($query);
297 =head2 GetSubscription
299 $subs = GetSubscription($subscriptionid)
300 this function returns the subscription which has $subscriptionid as id.
302 a hashref. This hash containts
303 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
307 sub GetSubscription {
308 my ($subscriptionid) = @_;
309 my $dbh = C4::Context->dbh;
311 SELECT subscription.*,
312 subscriptionhistory.*,
313 aqbooksellers.name AS aqbooksellername,
314 biblio.title AS bibliotitle,
315 subscription.biblionumber as bibnum);
316 if ( C4::Context->preference('IndependantBranches')
317 && C4::Context->userenv
318 && C4::Context->userenv->{'flags'} != 1
319 && C4::Context->userenv->{'branch'} ) {
321 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
325 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
326 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
327 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
328 WHERE subscription.subscriptionid = ?
331 # if (C4::Context->preference('IndependantBranches') &&
332 # C4::Context->userenv &&
333 # C4::Context->userenv->{'flags'} != 1){
334 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
335 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
337 $debug and warn "query : $query\nsubsid :$subscriptionid";
338 my $sth = $dbh->prepare($query);
339 $sth->execute($subscriptionid);
340 return $sth->fetchrow_hashref;
343 =head2 GetFullSubscription
345 $array_ref = GetFullSubscription($subscriptionid)
346 this function reads the serial table.
350 sub GetFullSubscription {
351 my ($subscriptionid) = @_;
352 my $dbh = C4::Context->dbh;
354 SELECT serial.serialid,
357 serial.publisheddate,
359 serial.notes as notes,
360 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
361 aqbooksellers.name as aqbooksellername,
362 biblio.title as bibliotitle,
363 subscription.branchcode AS branchcode,
364 subscription.subscriptionid AS subscriptionid |;
365 if ( C4::Context->preference('IndependantBranches')
366 && C4::Context->userenv
367 && C4::Context->userenv->{'flags'} != 1
368 && C4::Context->userenv->{'branch'} ) {
370 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
374 LEFT JOIN subscription ON
375 (serial.subscriptionid=subscription.subscriptionid )
376 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
377 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
378 WHERE serial.subscriptionid = ?
380 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
381 serial.subscriptionid
383 $debug and warn "GetFullSubscription query: $query";
384 my $sth = $dbh->prepare($query);
385 $sth->execute($subscriptionid);
386 return $sth->fetchall_arrayref( {} );
389 =head2 PrepareSerialsData
391 $array_ref = PrepareSerialsData($serialinfomation)
392 where serialinformation is a hashref array
396 sub PrepareSerialsData {
402 my $aqbooksellername;
406 my $previousnote = "";
408 foreach my $subs (@$lines) {
409 $subs->{'publisheddate'} = (
410 $subs->{'publisheddate'}
411 ? format_date( $subs->{'publisheddate'} )
414 $subs->{'branchname'} = GetBranchName( $subs->{'branchcode'} );
415 $subs->{'planneddate'} = format_date( $subs->{'planneddate'} );
416 $subs->{ "status" . $subs->{'status'} } = 1;
417 $subs->{"checked"} = $subs->{'status'} =~ /1|3|4|7/;
419 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
420 $year = $subs->{'year'};
424 if ( $tmpresults{$year} ) {
425 push @{ $tmpresults{$year}->{'serials'} }, $subs;
427 $tmpresults{$year} = {
429 'aqbooksellername' => $subs->{'aqbooksellername'},
430 'bibliotitle' => $subs->{'bibliotitle'},
431 'serials' => [$subs],
436 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
437 push @res, $tmpresults{$key};
439 $res[0]->{'first'} = 1;
443 =head2 GetSubscriptionsFromBiblionumber
445 $array_ref = GetSubscriptionsFromBiblionumber($biblionumber)
446 this function get the subscription list. it reads the subscription table.
448 reference to an array of subscriptions which have the biblionumber given on input arg.
449 each element of this array is a hashref containing
450 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
454 sub GetSubscriptionsFromBiblionumber {
455 my ($biblionumber) = @_;
456 my $dbh = C4::Context->dbh;
458 SELECT subscription.*,
460 subscriptionhistory.*,
461 aqbooksellers.name AS aqbooksellername,
462 biblio.title AS bibliotitle
464 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
465 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
466 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
467 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
468 WHERE subscription.biblionumber = ?
470 my $sth = $dbh->prepare($query);
471 $sth->execute($biblionumber);
473 while ( my $subs = $sth->fetchrow_hashref ) {
474 $subs->{startdate} = format_date( $subs->{startdate} );
475 $subs->{histstartdate} = format_date( $subs->{histstartdate} );
476 $subs->{histenddate} = format_date( $subs->{histenddate} );
477 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
478 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
479 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
480 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
481 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
482 $subs->{ "status" . $subs->{'status'} } = 1;
483 $subs->{'cannotedit'} =
484 ( C4::Context->preference('IndependantBranches')
485 && C4::Context->userenv
486 && C4::Context->userenv->{flags} % 2 != 1
487 && C4::Context->userenv->{branch}
488 && $subs->{branchcode}
489 && ( C4::Context->userenv->{branch} ne $subs->{branchcode} ) );
491 if ( $subs->{enddate} eq '0000-00-00' ) {
492 $subs->{enddate} = '';
494 $subs->{enddate} = format_date( $subs->{enddate} );
496 $subs->{'abouttoexpire'} = abouttoexpire( $subs->{'subscriptionid'} );
497 $subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
503 =head2 GetFullSubscriptionsFromBiblionumber
505 $array_ref = GetFullSubscriptionsFromBiblionumber($biblionumber)
506 this function reads the serial table.
510 sub GetFullSubscriptionsFromBiblionumber {
511 my ($biblionumber) = @_;
512 my $dbh = C4::Context->dbh;
514 SELECT serial.serialid,
517 serial.publisheddate,
519 serial.notes as notes,
520 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
521 biblio.title as bibliotitle,
522 subscription.branchcode AS branchcode,
523 subscription.subscriptionid AS subscriptionid|;
524 if ( C4::Context->preference('IndependantBranches')
525 && C4::Context->userenv
526 && C4::Context->userenv->{'flags'} != 1
527 && C4::Context->userenv->{'branch'} ) {
529 , ((subscription.branchcode <>\"" . C4::Context->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
534 LEFT JOIN subscription ON
535 (serial.subscriptionid=subscription.subscriptionid)
536 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
537 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
538 WHERE subscription.biblionumber = ?
540 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
541 serial.subscriptionid
543 my $sth = $dbh->prepare($query);
544 $sth->execute($biblionumber);
545 return $sth->fetchall_arrayref( {} );
548 =head2 GetSubscriptions
550 @results = GetSubscriptions($title,$ISSN,$biblionumber);
551 this function gets all subscriptions which have title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
553 a table of hashref. Each hash containt the subscription.
557 sub GetSubscriptions {
558 my ( $string, $issn, $biblionumber ) = @_;
560 #return unless $title or $ISSN or $biblionumber;
561 my $dbh = C4::Context->dbh;
564 SELECT subscription.*, subscriptionhistory.*, biblio.title,biblioitems.issn,biblio.biblionumber
566 LEFT JOIN subscriptionhistory USING(subscriptionid)
567 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
568 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
573 $sqlwhere = " WHERE biblio.biblionumber=?";
574 push @bind_params, $biblionumber;
578 my @strings_to_search;
579 @strings_to_search = map { "%$_%" } split( / /, $string );
580 foreach my $index qw(biblio.title subscription.callnumber subscription.location subscription.notes subscription.internalnotes) {
581 push @bind_params, @strings_to_search;
582 my $tmpstring = "AND $index LIKE ? " x scalar(@strings_to_search);
583 $debug && warn "$tmpstring";
584 $tmpstring =~ s/^AND //;
585 push @sqlstrings, $tmpstring;
587 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
591 my @strings_to_search;
592 @strings_to_search = map { "%$_%" } split( / /, $issn );
593 foreach my $index qw(biblioitems.issn subscription.callnumber) {
594 push @bind_params, @strings_to_search;
595 my $tmpstring = "OR $index LIKE ? " x scalar(@strings_to_search);
596 $debug && warn "$tmpstring";
597 $tmpstring =~ s/^OR //;
598 push @sqlstrings, $tmpstring;
600 $sqlwhere .= ( $sqlwhere ? " AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
602 $sql .= "$sqlwhere ORDER BY title";
603 $debug and warn "GetSubscriptions query: $sql params : ", join( " ", @bind_params );
604 $sth = $dbh->prepare($sql);
605 $sth->execute(@bind_params);
607 my $previoustitle = "";
610 while ( my $line = $sth->fetchrow_hashref ) {
611 if ( $previoustitle eq $line->{title} ) {
615 $previoustitle = $line->{title};
618 $line->{toggle} = 1 if $odd == 1;
619 $line->{'cannotedit'} =
620 ( C4::Context->preference('IndependantBranches')
621 && C4::Context->userenv
622 && C4::Context->userenv->{flags} % 2 != 1
623 && C4::Context->userenv->{branch}
624 && $line->{branchcode}
625 && ( C4::Context->userenv->{branch} ne $line->{branchcode} ) );
626 push @results, $line;
633 ($totalissues,@serials) = GetSerials($subscriptionid);
634 this function gets every serial not arrived for a given subscription
635 as well as the number of issues registered in the database (all types)
636 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
638 FIXME: We should return \@serials.
643 my ( $subscriptionid, $count ) = @_;
644 my $dbh = C4::Context->dbh;
646 # status = 2 is "arrived"
648 $count = 5 unless ($count);
650 my $query = "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
652 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
653 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
654 my $sth = $dbh->prepare($query);
655 $sth->execute($subscriptionid);
657 while ( my $line = $sth->fetchrow_hashref ) {
658 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
659 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
660 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
661 push @serials, $line;
664 # OK, now add the last 5 issues arrives/missing
665 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
667 WHERE subscriptionid = ?
668 AND (status in (2,4,5))
669 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
671 $sth = $dbh->prepare($query);
672 $sth->execute($subscriptionid);
673 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
675 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
676 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
677 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
678 push @serials, $line;
681 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
682 $sth = $dbh->prepare($query);
683 $sth->execute($subscriptionid);
684 my ($totalissues) = $sth->fetchrow;
685 return ( $totalissues, @serials );
690 @serials = GetSerials2($subscriptionid,$status);
691 this function returns every serial waited for a given subscription
692 as well as the number of issues registered in the database (all types)
693 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
698 my ( $subscription, $status ) = @_;
699 my $dbh = C4::Context->dbh;
701 SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
703 WHERE subscriptionid=$subscription AND status IN ($status)
704 ORDER BY publisheddate,serialid DESC
706 $debug and warn "GetSerials2 query: $query";
707 my $sth = $dbh->prepare($query);
711 while ( my $line = $sth->fetchrow_hashref ) {
712 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
713 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
714 $line->{"publisheddate"} = format_date( $line->{"publisheddate"} );
715 push @serials, $line;
720 =head2 GetLatestSerials
722 \@serials = GetLatestSerials($subscriptionid,$limit)
723 get the $limit's latest serials arrived or missing for a given subscription
725 a ref to an array which contains all of the latest serials stored into a hash.
729 sub GetLatestSerials {
730 my ( $subscriptionid, $limit ) = @_;
731 my $dbh = C4::Context->dbh;
733 # status = 2 is "arrived"
734 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
736 WHERE subscriptionid = ?
737 AND (status =2 or status=4)
738 ORDER BY planneddate DESC LIMIT 0,$limit
740 my $sth = $dbh->prepare($strsth);
741 $sth->execute($subscriptionid);
743 while ( my $line = $sth->fetchrow_hashref ) {
744 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
745 $line->{"planneddate"} = format_date( $line->{"planneddate"} );
746 push @serials, $line;
752 =head2 GetDistributedTo
754 $distributedto=GetDistributedTo($subscriptionid)
755 This function returns the field distributedto for the subscription matching subscriptionid
759 sub GetDistributedTo {
760 my $dbh = C4::Context->dbh;
762 my $subscriptionid = @_;
763 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
764 my $sth = $dbh->prepare($query);
765 $sth->execute($subscriptionid);
766 return ($distributedto) = $sth->fetchrow;
772 $val is a hashref containing all the attributes of the table 'subscription'
773 This function get the next issue for the subscription given on input arg
775 a list containing all the input params updated.
781 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
782 # $calculated = $val->{numberingmethod};
783 # # calculate the (expected) value of the next issue recieved.
784 # $newlastvalue1 = $val->{lastvalue1};
785 # # check if we have to increase the new value.
786 # $newinnerloop1 = $val->{innerloop1}+1;
787 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
788 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
789 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
790 # $calculated =~ s/\{X\}/$newlastvalue1/g;
792 # $newlastvalue2 = $val->{lastvalue2};
793 # # check if we have to increase the new value.
794 # $newinnerloop2 = $val->{innerloop2}+1;
795 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
796 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
797 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
798 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
800 # $newlastvalue3 = $val->{lastvalue3};
801 # # check if we have to increase the new value.
802 # $newinnerloop3 = $val->{innerloop3}+1;
803 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
804 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
805 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
806 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
807 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
812 my ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
813 my $pattern = $val->{numberpattern};
814 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
815 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
816 $calculated = $val->{numberingmethod};
817 $newlastvalue1 = $val->{lastvalue1};
818 $newlastvalue2 = $val->{lastvalue2};
819 $newlastvalue3 = $val->{lastvalue3};
820 $newlastvalue1 = $val->{lastvalue1};
822 # check if we have to increase the new value.
823 $newinnerloop1 = $val->{innerloop1} + 1;
824 $newinnerloop1 = 0 if ( $newinnerloop1 >= $val->{every1} );
825 $newlastvalue1 += $val->{add1} if ( $newinnerloop1 < 1 ); # <1 to be true when 0 or empty.
826 $newlastvalue1 = $val->{setto1} if ( $newlastvalue1 > $val->{whenmorethan1} ); # reset counter if needed.
827 $calculated =~ s/\{X\}/$newlastvalue1/g;
829 $newlastvalue2 = $val->{lastvalue2};
831 # check if we have to increase the new value.
832 $newinnerloop2 = $val->{innerloop2} + 1;
833 $newinnerloop2 = 0 if ( $newinnerloop2 >= $val->{every2} );
834 $newlastvalue2 += $val->{add2} if ( $newinnerloop2 < 1 ); # <1 to be true when 0 or empty.
835 $newlastvalue2 = $val->{setto2} if ( $newlastvalue2 > $val->{whenmorethan2} ); # reset counter if needed.
836 if ( $pattern == 6 ) {
837 if ( $val->{hemisphere} == 2 ) {
838 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
839 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
841 my $newlastvalue2seq = $seasons[$newlastvalue2];
842 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
845 $calculated =~ s/\{Y\}/$newlastvalue2/g;
848 $newlastvalue3 = $val->{lastvalue3};
850 # check if we have to increase the new value.
851 $newinnerloop3 = $val->{innerloop3} + 1;
852 $newinnerloop3 = 0 if ( $newinnerloop3 >= $val->{every3} );
853 $newlastvalue3 += $val->{add3} if ( $newinnerloop3 < 1 ); # <1 to be true when 0 or empty.
854 $newlastvalue3 = $val->{setto3} if ( $newlastvalue3 > $val->{whenmorethan3} ); # reset counter if needed.
855 $calculated =~ s/\{Z\}/$newlastvalue3/g;
857 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
862 $calculated = GetSeq($val)
863 $val is a hashref containing all the attributes of the table 'subscription'
864 this function transforms {X},{Y},{Z} to 150,0,0 for example.
866 the sequence in integer format
872 my $pattern = $val->{numberpattern};
873 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
874 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
875 my $calculated = $val->{numberingmethod};
876 my $x = $val->{'lastvalue1'};
877 $calculated =~ s/\{X\}/$x/g;
878 my $newlastvalue2 = $val->{'lastvalue2'};
880 if ( $pattern == 6 ) {
881 if ( $val->{hemisphere} == 2 ) {
882 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
883 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
885 my $newlastvalue2seq = $seasons[$newlastvalue2];
886 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
889 $calculated =~ s/\{Y\}/$newlastvalue2/g;
891 my $z = $val->{'lastvalue3'};
892 $calculated =~ s/\{Z\}/$z/g;
896 =head2 GetExpirationDate
898 $sensddate = GetExpirationDate($subscriptionid, [$startdate])
900 this function return the next expiration date for a subscription given on input args.
907 sub GetExpirationDate {
908 my ( $subscriptionid, $startdate ) = @_;
909 my $dbh = C4::Context->dbh;
910 my $subscription = GetSubscription($subscriptionid);
913 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
914 $enddate = $startdate || $subscription->{startdate};
915 my @date = split( /-/, $enddate );
916 return if ( scalar(@date) != 3 || not check_date(@date) );
917 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
920 if ( my $length = $subscription->{numberlength} ) {
922 #calculate the date of the last issue.
923 for ( my $i = 1 ; $i <= $length ; $i++ ) {
924 $enddate = GetNextDate( $enddate, $subscription );
926 } elsif ( $subscription->{monthlength} ) {
927 if ( $$subscription{startdate} ) {
928 my @enddate = Add_Delta_YM( $date[0], $date[1], $date[2], 0, $subscription->{monthlength} );
929 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
931 } elsif ( $subscription->{weeklength} ) {
932 if ( $$subscription{startdate} ) {
933 my @date = split( /-/, $subscription->{startdate} );
934 my @enddate = Add_Delta_Days( $date[0], $date[1], $date[2], $subscription->{weeklength} * 7 );
935 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
944 =head2 CountSubscriptionFromBiblionumber
946 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
947 this returns a count of the subscriptions for a given biblionumber
949 the number of subscriptions
953 sub CountSubscriptionFromBiblionumber {
954 my ($biblionumber) = @_;
955 my $dbh = C4::Context->dbh;
956 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
957 my $sth = $dbh->prepare($query);
958 $sth->execute($biblionumber);
959 my $subscriptionsnumber = $sth->fetchrow;
960 return $subscriptionsnumber;
963 =head2 ModSubscriptionHistory
965 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
967 this function modifies the history of a subscription. Put your new values on input arg.
968 returns the number of rows affected
972 sub ModSubscriptionHistory {
973 my ( $subscriptionid, $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote ) = @_;
974 my $dbh = C4::Context->dbh;
975 my $query = "UPDATE subscriptionhistory
976 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
977 WHERE subscriptionid=?
979 my $sth = $dbh->prepare($query);
980 $recievedlist =~ s/^; //;
981 $missinglist =~ s/^; //;
982 $opacnote =~ s/^; //;
983 $sth->execute( $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
987 =head2 ModSerialStatus
989 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
991 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
992 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
996 sub ModSerialStatus {
997 my ( $serialid, $serialseq, $planneddate, $publisheddate, $status, $notes ) = @_;
999 #It is a usual serial
1000 # 1st, get previous status :
1001 my $dbh = C4::Context->dbh;
1002 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1003 my $sth = $dbh->prepare($query);
1004 $sth->execute($serialid);
1005 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1007 # change status & update subscriptionhistory
1009 if ( $status == 6 ) {
1010 DelIssue( {'serialid'=>$serialid, 'subscriptionid'=>$subscriptionid,'serialseq'=>$serialseq} );
1014 'UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?';
1015 $sth = $dbh->prepare($query);
1016 $sth->execute( $serialseq, $publisheddate, $planneddate, $status, $notes, $serialid );
1017 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1018 $sth = $dbh->prepare($query);
1019 $sth->execute($subscriptionid);
1020 my $val = $sth->fetchrow_hashref;
1021 unless ( $val->{manualhistory} ) {
1022 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1023 $sth = $dbh->prepare($query);
1024 $sth->execute($subscriptionid);
1025 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1026 if ( $status == 2 ) {
1028 $recievedlist .= "; $serialseq"
1029 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1032 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1033 $missinglist .= "; $serialseq"
1035 and not index( "$missinglist", "$serialseq" ) >= 0 );
1036 $missinglist .= "; not issued $serialseq"
1038 and index( "$missinglist", "$serialseq" ) >= 0 );
1039 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1040 $sth = $dbh->prepare($query);
1041 $recievedlist =~ s/^; //;
1042 $missinglist =~ s/^; //;
1043 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1047 # create new waited entry if needed (ie : was a "waited" and has changed)
1048 if ( $oldstatus == 1 && $status != 1 ) {
1049 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1050 $sth = $dbh->prepare($query);
1051 $sth->execute($subscriptionid);
1052 my $val = $sth->fetchrow_hashref;
1056 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
1057 $newinnerloop1, $newinnerloop2, $newinnerloop3
1058 ) = GetNextSeq($val);
1060 # next date (calculated from actual date & frequency parameters)
1061 my $nextpublisheddate = GetNextDate( $publisheddate, $val );
1062 NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate );
1063 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1064 WHERE subscriptionid = ?";
1065 $sth = $dbh->prepare($query);
1066 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1068 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1069 if ( $val->{letter} && $status == 2 && $oldstatus != 2 ) {
1070 SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} );
1076 =head2 GetNextExpected
1078 $nextexpected = GetNextExpected($subscriptionid)
1080 Get the planneddate for the current expected issue of the subscription.
1086 planneddate => C4::Dates object
1091 sub GetNextExpected($) {
1092 my ($subscriptionid) = @_;
1093 my $dbh = C4::Context->dbh;
1094 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1096 # Each subscription has only one 'expected' issue, with serial.status==1.
1097 $sth->execute( $subscriptionid, 1 );
1098 my ( $nextissue ) = $sth->fetchrow_hashref;
1100 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1101 $sth->execute( $subscriptionid );
1102 $nextissue = $sth->fetchrow_hashref;
1104 if (!defined $nextissue->{planneddate}) {
1105 # or should this default to 1st Jan ???
1106 $nextissue->{planneddate} = strftime('%Y-%m-%d',localtime);
1108 $nextissue->{planneddate} = C4::Dates->new($nextissue->{planneddate},'iso');
1113 =head2 ModNextExpected
1115 ModNextExpected($subscriptionid,$date)
1117 Update the planneddate for the current expected issue of the subscription.
1118 This will modify all future prediction results.
1120 C<$date> is a C4::Dates object.
1126 sub ModNextExpected($$) {
1127 my ( $subscriptionid, $date ) = @_;
1128 my $dbh = C4::Context->dbh;
1130 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1131 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1133 # Each subscription has only one 'expected' issue, with serial.status==1.
1134 $sth->execute( $date->output('iso'), $date->output('iso'), $subscriptionid, 1 );
1139 =head2 ModSubscription
1141 this function modifies a subscription. Put all new values on input args.
1142 returns the number of rows affected
1146 sub ModSubscription {
1147 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1148 $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $add1, $every1,
1149 $whenmorethan1, $setto1, $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2,
1150 $lastvalue2, $innerloop2, $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1151 $numberingmethod, $status, $biblionumber, $callnumber, $notes, $letter, $hemisphere, $manualhistory,
1152 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid
1155 # warn $irregularity;
1156 my $dbh = C4::Context->dbh;
1157 my $query = "UPDATE subscription
1158 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1159 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1160 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1161 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1162 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1163 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?,
1164 letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,
1165 staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1167 WHERE subscriptionid = ?";
1169 #warn "query :".$query;
1170 my $sth = $dbh->prepare($query);
1172 $auser, $branchcode, $aqbooksellerid, $cost,
1173 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1174 $dow, "$irregularity", $numberpattern, $numberlength,
1175 $weeklength, $monthlength, $add1, $every1,
1176 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1177 $add2, $every2, $whenmorethan2, $setto2,
1178 $lastvalue2, $innerloop2, $add3, $every3,
1179 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1180 $numberingmethod, $status, $biblionumber, $callnumber,
1181 $notes, $letter, $hemisphere, ( $manualhistory ? $manualhistory : 0 ),
1182 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1183 $graceperiod, $location, $enddate, $subscriptionid
1185 my $rows = $sth->rows;
1187 logaction( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1191 =head2 NewSubscription
1193 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1194 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1195 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1196 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1197 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1198 $numberingmethod, $status, $notes, $serialsadditems,
1199 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate);
1201 Create a new subscription with value given on input args.
1204 the id of this new subscription
1208 sub NewSubscription {
1209 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1210 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1211 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1212 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, $status,
1213 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1214 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1216 my $dbh = C4::Context->dbh;
1218 #save subscription (insert into database)
1220 INSERT INTO subscription
1221 (librarian,branchcode,aqbooksellerid,cost,aqbudgetid,biblionumber,
1222 startdate,periodicity,dow,numberlength,weeklength,monthlength,
1223 add1,every1,whenmorethan1,setto1,lastvalue1,innerloop1,
1224 add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
1225 add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
1226 numberingmethod, status, notes, letter,firstacquidate,irregularity,
1227 numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
1228 staffdisplaycount,opacdisplaycount,graceperiod,location,enddate)
1229 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1231 my $sth = $dbh->prepare($query);
1233 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1234 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1235 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1236 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, "$status",
1237 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1238 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1241 #then create the 1st waited number
1242 my $subscriptionid = $dbh->{'mysql_insertid'};
1244 INSERT INTO subscriptionhistory
1245 (biblionumber, subscriptionid, histstartdate, opacnote, librariannote)
1248 $sth = $dbh->prepare($query);
1249 $sth->execute( $biblionumber, $subscriptionid, $startdate, $notes, $internalnotes );
1251 # reread subscription to get a hash (for calculation of the 1st issue number)
1255 WHERE subscriptionid = ?
1257 $sth = $dbh->prepare($query);
1258 $sth->execute($subscriptionid);
1259 my $val = $sth->fetchrow_hashref;
1261 # calculate issue number
1262 my $serialseq = GetSeq($val);
1265 (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
1266 VALUES (?,?,?,?,?,?)
1268 $sth = $dbh->prepare($query);
1269 $sth->execute( "$serialseq", $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
1271 logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1273 #set serial flag on biblio if not already set.
1274 my ( $null, ($bib) ) = GetBiblio($biblionumber);
1275 if ( !$bib->{'serial'} ) {
1276 my $record = GetMarcBiblio($biblionumber);
1277 my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $bib->{'frameworkcode'} );
1279 eval { $record->field($tag)->update( $subf => 1 ); };
1281 ModBiblio( $record, $biblionumber, $bib->{'frameworkcode'} );
1283 return $subscriptionid;
1286 =head2 ReNewSubscription
1288 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1290 this function renew a subscription with values given on input args.
1294 sub ReNewSubscription {
1295 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1296 my $dbh = C4::Context->dbh;
1297 my $subscription = GetSubscription($subscriptionid);
1301 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1302 WHERE biblio.biblionumber=?
1304 my $sth = $dbh->prepare($query);
1305 $sth->execute( $subscription->{biblionumber} );
1306 my $biblio = $sth->fetchrow_hashref;
1308 if ( C4::Context->preference("RenewSerialAddsSuggestion") ) {
1311 { 'suggestedby' => $user,
1312 'title' => $subscription->{bibliotitle},
1313 'author' => $biblio->{author},
1314 'publishercode' => $biblio->{publishercode},
1315 'note' => $biblio->{note},
1316 'biblionumber' => $subscription->{biblionumber}
1321 # renew subscription
1324 SET startdate=?,numberlength=?,weeklength=?,monthlength=?
1325 WHERE subscriptionid=?
1327 $sth = $dbh->prepare($query);
1328 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1329 my $enddate = GetExpirationDate($subscriptionid);
1330 $debug && warn "enddate :$enddate";
1334 WHERE subscriptionid=?
1336 $sth = $dbh->prepare($query);
1337 $sth->execute( $enddate, $subscriptionid );
1339 UPDATE subscriptionhistory
1341 WHERE subscriptionid=?
1343 $sth = $dbh->prepare($query);
1344 $sth->execute( $enddate, $subscriptionid );
1346 logaction( "SERIAL", "RENEW", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1352 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1354 Create a new issue stored on the database.
1355 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1356 returns the serial id
1361 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
1362 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1364 my $dbh = C4::Context->dbh;
1367 (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
1368 VALUES (?,?,?,?,?,?,?)
1370 my $sth = $dbh->prepare($query);
1371 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
1372 my $serialid = $dbh->{'mysql_insertid'};
1374 SELECT missinglist,recievedlist
1375 FROM subscriptionhistory
1376 WHERE subscriptionid=?
1378 $sth = $dbh->prepare($query);
1379 $sth->execute($subscriptionid);
1380 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1382 if ( $status == 2 ) {
1383 ### TODO Add a feature that improves recognition and description.
1384 ### As such count (serialseq) i.e. : N18,2(N19),N20
1385 ### Would use substr and index But be careful to previous presence of ()
1386 $recievedlist .= "; $serialseq" unless (index($recievedlist,$serialseq)>0);
1388 if ( $status == 4 ) {
1389 $missinglist .= "; $serialseq" unless (index($missinglist,$serialseq)>0);
1392 UPDATE subscriptionhistory
1393 SET recievedlist=?, missinglist=?
1394 WHERE subscriptionid=?
1396 $sth = $dbh->prepare($query);
1397 $recievedlist =~ s/^; //;
1398 $missinglist =~ s/^; //;
1399 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1403 =head2 ItemizeSerials
1405 ItemizeSerials($serialid, $info);
1406 $info is a hashref containing barcode branch, itemcallnumber, status, location
1407 $serialid the serialid
1409 1 if the itemize is a succes.
1410 0 and @error otherwise. @error containts the list of errors found.
1414 sub ItemizeSerials {
1415 my ( $serialid, $info ) = @_;
1416 my $now = POSIX::strftime( "%Y-%m-%d", localtime );
1418 my $dbh = C4::Context->dbh;
1424 my $sth = $dbh->prepare($query);
1425 $sth->execute($serialid);
1426 my $data = $sth->fetchrow_hashref;
1427 if ( C4::Context->preference("RoutingSerials") ) {
1429 # check for existing biblioitem relating to serial issue
1430 my ( $count, @results ) = GetBiblioItemByBiblioNumber( $data->{'biblionumber'} );
1432 for ( my $i = 0 ; $i < $count ; $i++ ) {
1433 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' (' . $data->{'planneddate'} . ')' ) {
1434 $bibitemno = $results[$i]->{'biblioitemnumber'};
1438 if ( $bibitemno == 0 ) {
1439 my $sth = $dbh->prepare( "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC" );
1440 $sth->execute( $data->{'biblionumber'} );
1441 my $biblioitem = $sth->fetchrow_hashref;
1442 $biblioitem->{'volumedate'} = $data->{planneddate};
1443 $biblioitem->{'volumeddesc'} = $data->{serialseq} . ' (' . format_date( $data->{'planneddate'} ) . ')';
1444 $biblioitem->{'dewey'} = $info->{itemcallnumber};
1448 my $fwk = GetFrameworkCode( $data->{'biblionumber'} );
1449 if ( $info->{barcode} ) {
1451 my $exists = itemdata( $info->{'barcode'} );
1452 push @errors, "barcode_not_unique" if ($exists);
1454 my $marcrecord = MARC::Record->new();
1455 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.barcode", $fwk );
1456 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{barcode} );
1457 $marcrecord->insert_fields_ordered($newField);
1458 if ( $info->{branch} ) {
1459 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.homebranch", $fwk );
1461 #warn "items.homebranch : $tag , $subfield";
1462 if ( $marcrecord->field($tag) ) {
1463 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1465 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1466 $marcrecord->insert_fields_ordered($newField);
1468 ( $tag, $subfield ) = GetMarcFromKohaField( "items.holdingbranch", $fwk );
1470 #warn "items.holdingbranch : $tag , $subfield";
1471 if ( $marcrecord->field($tag) ) {
1472 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch} );
1474 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{branch} );
1475 $marcrecord->insert_fields_ordered($newField);
1478 if ( $info->{itemcallnumber} ) {
1479 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemcallnumber", $fwk );
1481 if ( $marcrecord->field($tag) ) {
1482 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{itemcallnumber} );
1484 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{itemcallnumber} );
1485 $marcrecord->insert_fields_ordered($newField);
1488 if ( $info->{notes} ) {
1489 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.itemnotes", $fwk );
1491 if ( $marcrecord->field($tag) ) {
1492 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{notes} );
1494 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{notes} );
1495 $marcrecord->insert_fields_ordered($newField);
1498 if ( $info->{location} ) {
1499 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.location", $fwk );
1501 if ( $marcrecord->field($tag) ) {
1502 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{location} );
1504 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{location} );
1505 $marcrecord->insert_fields_ordered($newField);
1508 if ( $info->{status} ) {
1509 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.notforloan", $fwk );
1511 if ( $marcrecord->field($tag) ) {
1512 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{status} );
1514 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $info->{status} );
1515 $marcrecord->insert_fields_ordered($newField);
1518 if ( C4::Context->preference("RoutingSerials") ) {
1519 my ( $tag, $subfield ) = GetMarcFromKohaField( "items.dateaccessioned", $fwk );
1520 if ( $marcrecord->field($tag) ) {
1521 $marcrecord->field($tag)->add_subfields( "$subfield" => $now );
1523 my $newField = MARC::Field->new( "$tag", '', '', "$subfield" => $now );
1524 $marcrecord->insert_fields_ordered($newField);
1527 AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
1530 return ( 0, @errors );
1534 =head2 HasSubscriptionStrictlyExpired
1536 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1538 the subscription has stricly expired when today > the end subscription date
1541 1 if true, 0 if false, -1 if the expiration date is not set.
1545 sub HasSubscriptionStrictlyExpired {
1547 # Getting end of subscription date
1548 my ($subscriptionid) = @_;
1549 my $dbh = C4::Context->dbh;
1550 my $subscription = GetSubscription($subscriptionid);
1551 my $expirationdate = $subscription->{enddate} || GetExpirationDate($subscriptionid);
1553 # If the expiration date is set
1554 if ( $expirationdate != 0 ) {
1555 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1557 # Getting today's date
1558 my ( $nowyear, $nowmonth, $nowday ) = Today();
1560 # if today's date > expiration date, then the subscription has stricly expired
1561 if ( Delta_Days( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1568 # There are some cases where the expiration date is not set
1569 # As we can't determine if the subscription has expired on a date-basis,
1575 =head2 HasSubscriptionExpired
1577 $has_expired = HasSubscriptionExpired($subscriptionid)
1579 the subscription has expired when the next issue to arrive is out of subscription limit.
1582 0 if the subscription has not expired
1583 1 if the subscription has expired
1584 2 if has subscription does not have a valid expiration date set
1588 sub HasSubscriptionExpired {
1589 my ($subscriptionid) = @_;
1590 my $dbh = C4::Context->dbh;
1591 my $subscription = GetSubscription($subscriptionid);
1592 if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
1593 my $expirationdate = $subscription->{enddate};
1594 if (!defined $expirationdate) {
1595 $expirationdate = q{};
1598 SELECT max(planneddate)
1600 WHERE subscriptionid=?
1602 my $sth = $dbh->prepare($query);
1603 $sth->execute($subscriptionid);
1604 my ($res) = $sth->fetchrow;
1605 return 0 unless $res;
1606 my @res = split( /-/, $res );
1607 my @endofsubscriptiondate = split( /-/, $expirationdate );
1608 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date(@res) || not check_date(@endofsubscriptiondate) );
1610 if ( ( @endofsubscriptiondate && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1614 if ( $subscription->{'numberlength'} ) {
1615 my $countreceived = countissuesfrom( $subscriptionid, $subscription->{'startdate'} );
1616 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1622 return 0; # Notice that you'll never get here.
1625 =head2 SetDistributedto
1627 SetDistributedto($distributedto,$subscriptionid);
1628 This function update the value of distributedto for a subscription given on input arg.
1632 sub SetDistributedto {
1633 my ( $distributedto, $subscriptionid ) = @_;
1634 my $dbh = C4::Context->dbh;
1638 WHERE subscriptionid=?
1640 my $sth = $dbh->prepare($query);
1641 $sth->execute( $distributedto, $subscriptionid );
1645 =head2 DelSubscription
1647 DelSubscription($subscriptionid)
1648 this function deletes subscription which has $subscriptionid as id.
1652 sub DelSubscription {
1653 my ($subscriptionid) = @_;
1654 my $dbh = C4::Context->dbh;
1655 $subscriptionid = $dbh->quote($subscriptionid);
1656 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1657 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1658 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1660 logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1665 DelIssue($serialseq,$subscriptionid)
1666 this function deletes an issue which has $serialseq and $subscriptionid given on input arg.
1668 returns the number of rows affected
1673 my ($dataissue) = @_;
1674 my $dbh = C4::Context->dbh;
1675 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1680 AND subscriptionid= ?
1682 my $mainsth = $dbh->prepare($query);
1683 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1685 #Delete element from subscription history
1686 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1687 my $sth = $dbh->prepare($query);
1688 $sth->execute( $dataissue->{'subscriptionid'} );
1689 my $val = $sth->fetchrow_hashref;
1690 unless ( $val->{manualhistory} ) {
1692 SELECT * FROM subscriptionhistory
1693 WHERE subscriptionid= ?
1695 my $sth = $dbh->prepare($query);
1696 $sth->execute( $dataissue->{'subscriptionid'} );
1697 my $data = $sth->fetchrow_hashref;
1698 my $serialseq = $dataissue->{'serialseq'};
1699 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1700 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1701 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1702 $sth = $dbh->prepare($strsth);
1703 $sth->execute( $dataissue->{'subscriptionid'} );
1706 return $mainsth->rows;
1709 =head2 GetLateOrMissingIssues
1711 @issuelist = GetLateMissingIssues($supplierid,$serialid)
1713 this function selects missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1716 the issuelist as an array of hash refs. Each element of this array contains
1717 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1721 sub GetLateOrMissingIssues {
1722 my ( $supplierid, $serialid, $order ) = @_;
1723 my $dbh = C4::Context->dbh;
1727 $byserial = "and serialid = " . $serialid;
1730 $order .= ", title";
1735 $sth = $dbh->prepare(
1737 serialid, aqbooksellerid, name,
1738 biblio.title, planneddate, serialseq,
1739 serial.status, serial.subscriptionid, claimdate
1741 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1742 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1743 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1744 WHERE subscription.subscriptionid = serial.subscriptionid
1745 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1746 AND subscription.aqbooksellerid=$supplierid
1751 $sth = $dbh->prepare(
1753 serialid, aqbooksellerid, name,
1754 biblio.title, planneddate, serialseq,
1755 serial.status, serial.subscriptionid, claimdate
1757 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1758 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1759 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1760 WHERE subscription.subscriptionid = serial.subscriptionid
1761 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1768 while ( my $line = $sth->fetchrow_hashref ) {
1770 if ($line->{planneddate} && $line->{planneddate} !~/^0+\-/) {
1771 $line->{planneddate} = format_date( $line->{planneddate} );
1773 if ($line->{claimdate} && $line->{claimdate} !~/^0+\-/) {
1774 $line->{claimdate} = format_date( $line->{claimdate} );
1776 $line->{"status".$line->{status}} = 1;
1777 push @issuelist, $line;
1782 =head2 removeMissingIssue
1784 removeMissingIssue($subscriptionid)
1786 this function removes an issue from being part of the missing string in
1787 subscriptionlist.missinglist column
1789 called when a missing issue is found from the serials-recieve.pl file
1793 sub removeMissingIssue {
1794 my ( $sequence, $subscriptionid ) = @_;
1795 my $dbh = C4::Context->dbh;
1796 my $sth = $dbh->prepare("SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1797 $sth->execute($subscriptionid);
1798 my $data = $sth->fetchrow_hashref;
1799 my $missinglist = $data->{'missinglist'};
1800 my $missinglistbefore = $missinglist;
1802 # warn $missinglist." before";
1803 $missinglist =~ s/($sequence)//;
1805 # warn $missinglist." after";
1806 if ( $missinglist ne $missinglistbefore ) {
1807 $missinglist =~ s/\|\s\|/\|/g;
1808 $missinglist =~ s/^\| //g;
1809 $missinglist =~ s/\|$//g;
1810 my $sth2 = $dbh->prepare(
1811 "UPDATE subscriptionhistory
1813 WHERE subscriptionid = ?"
1815 $sth2->execute( $missinglist, $subscriptionid );
1822 &updateClaim($serialid)
1824 this function updates the time when a claim is issued for late/missing items
1826 called from claims.pl file
1831 my ($serialid) = @_;
1832 my $dbh = C4::Context->dbh;
1833 my $sth = $dbh->prepare(
1834 "UPDATE serial SET claimdate = now()
1838 $sth->execute($serialid);
1842 =head2 getsupplierbyserialid
1844 $result = getsupplierbyserialid($serialid)
1846 this function is used to find the supplier id given a serial id
1849 hashref containing serialid, subscriptionid, and aqbooksellerid
1853 sub getsupplierbyserialid {
1854 my ($serialid) = @_;
1855 my $dbh = C4::Context->dbh;
1856 my $sth = $dbh->prepare(
1857 "SELECT serialid, serial.subscriptionid, aqbooksellerid
1859 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
1863 $sth->execute($serialid);
1864 my $line = $sth->fetchrow_hashref;
1865 my $result = $line->{'aqbooksellerid'};
1869 =head2 check_routing
1871 $result = &check_routing($subscriptionid)
1873 this function checks to see if a serial has a routing list and returns the count of routingid
1874 used to show either an 'add' or 'edit' link
1879 my ($subscriptionid) = @_;
1880 my $dbh = C4::Context->dbh;
1881 my $sth = $dbh->prepare(
1882 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
1883 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
1884 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
1887 $sth->execute($subscriptionid);
1888 my $line = $sth->fetchrow_hashref;
1889 my $result = $line->{'routingids'};
1893 =head2 addroutingmember
1895 addroutingmember($borrowernumber,$subscriptionid)
1897 this function takes a borrowernumber and subscriptionid and adds the member to the
1898 routing list for that serial subscription and gives them a rank on the list
1899 of either 1 or highest current rank + 1
1903 sub addroutingmember {
1904 my ( $borrowernumber, $subscriptionid ) = @_;
1906 my $dbh = C4::Context->dbh;
1907 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
1908 $sth->execute($subscriptionid);
1909 while ( my $line = $sth->fetchrow_hashref ) {
1910 if ( $line->{'rank'} > 0 ) {
1911 $rank = $line->{'rank'} + 1;
1916 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
1917 $sth->execute( $subscriptionid, $borrowernumber, $rank );
1920 =head2 reorder_members
1922 reorder_members($subscriptionid,$routingid,$rank)
1924 this function is used to reorder the routing list
1926 it takes the routingid of the member one wants to re-rank and the rank it is to move to
1927 - it gets all members on list puts their routingid's into an array
1928 - removes the one in the array that is $routingid
1929 - then reinjects $routingid at point indicated by $rank
1930 - then update the database with the routingids in the new order
1934 sub reorder_members {
1935 my ( $subscriptionid, $routingid, $rank ) = @_;
1936 my $dbh = C4::Context->dbh;
1937 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
1938 $sth->execute($subscriptionid);
1940 while ( my $line = $sth->fetchrow_hashref ) {
1941 push( @result, $line->{'routingid'} );
1944 # To find the matching index
1946 my $key = -1; # to allow for 0 being a valid response
1947 for ( $i = 0 ; $i < @result ; $i++ ) {
1948 if ( $routingid == $result[$i] ) {
1949 $key = $i; # save the index
1954 # if index exists in array then move it to new position
1955 if ( $key > -1 && $rank > 0 ) {
1956 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
1957 my $moving_item = splice( @result, $key, 1 );
1958 splice( @result, $new_rank, 0, $moving_item );
1960 for ( my $j = 0 ; $j < @result ; $j++ ) {
1961 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
1967 =head2 delroutingmember
1969 delroutingmember($routingid,$subscriptionid)
1971 this function either deletes one member from routing list if $routingid exists otherwise
1972 deletes all members from the routing list
1976 sub delroutingmember {
1978 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
1979 my ( $routingid, $subscriptionid ) = @_;
1980 my $dbh = C4::Context->dbh;
1982 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
1983 $sth->execute($routingid);
1984 reorder_members( $subscriptionid, $routingid );
1986 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
1987 $sth->execute($subscriptionid);
1992 =head2 getroutinglist
1994 ($count,@routinglist) = getroutinglist($subscriptionid)
1996 this gets the info from the subscriptionroutinglist for $subscriptionid
1999 a count of the number of members on routinglist
2000 the routinglist as an array. Each element of the array contains a hash_ref containing
2001 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2005 sub getroutinglist {
2006 my ($subscriptionid) = @_;
2007 my $dbh = C4::Context->dbh;
2008 my $sth = $dbh->prepare(
2009 "SELECT routingid, borrowernumber, ranking, biblionumber
2011 JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2012 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2015 $sth->execute($subscriptionid);
2018 while ( my $line = $sth->fetchrow_hashref ) {
2020 push( @routinglist, $line );
2022 return ( $count, @routinglist );
2025 =head2 countissuesfrom
2027 $result = countissuesfrom($subscriptionid,$startdate)
2029 Returns a count of serial rows matching the given subsctiptionid
2030 with published date greater than startdate
2034 sub countissuesfrom {
2035 my ( $subscriptionid, $startdate ) = @_;
2036 my $dbh = C4::Context->dbh;
2040 WHERE subscriptionid=?
2041 AND serial.publisheddate>?
2043 my $sth = $dbh->prepare($query);
2044 $sth->execute( $subscriptionid, $startdate );
2045 my ($countreceived) = $sth->fetchrow;
2046 return $countreceived;
2051 $result = CountIssues($subscriptionid)
2053 Returns a count of serial rows matching the given subsctiptionid
2058 my ($subscriptionid) = @_;
2059 my $dbh = C4::Context->dbh;
2063 WHERE subscriptionid=?
2065 my $sth = $dbh->prepare($query);
2066 $sth->execute($subscriptionid);
2067 my ($countreceived) = $sth->fetchrow;
2068 return $countreceived;
2073 $result = HasItems($subscriptionid)
2075 returns a count of items from serial matching the subscriptionid
2080 my ($subscriptionid) = @_;
2081 my $dbh = C4::Context->dbh;
2083 SELECT COUNT(serialitems.itemnumber)
2085 LEFT JOIN serialitems USING(serialid)
2086 WHERE subscriptionid=? AND serialitems.serialid NOT NULL
2088 my $sth=$dbh->prepare($query);
2089 $sth->execute($subscriptionid);
2090 my ($countitems)=$sth->fetchrow;
2094 =head2 abouttoexpire
2096 $result = abouttoexpire($subscriptionid)
2098 this function alerts you to the penultimate issue for a serial subscription
2100 returns 1 - if this is the penultimate issue
2106 my ($subscriptionid) = @_;
2107 my $dbh = C4::Context->dbh;
2108 my $subscription = GetSubscription($subscriptionid);
2109 my $per = $subscription->{'periodicity'};
2110 if ($per && $per % 16 > 0){
2111 my $expirationdate = GetExpirationDate($subscriptionid);
2112 my ($res) = $dbh->selectrow_array('select max(planneddate) from serial where subscriptionid = ?', undef, $subscriptionid);
2115 @res=split (/-/,$res);
2116 @res=Date::Calc::Today if ($res[0]*$res[1]==0);
2117 } else { # default an undefined value
2118 @res=Date::Calc::Today;
2120 my @endofsubscriptiondate=split(/-/,$expirationdate);
2121 my @per_list = (0, 7, 7, 14, 21, 31, 62, 93, 93, 190, 365, 730, 0, 0, 0, 0);
2123 @datebeforeend = Add_Delta_Days( $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2],
2124 - (3 * $per_list[$per])) if (@endofsubscriptiondate && $endofsubscriptiondate[0]*$endofsubscriptiondate[1]*$endofsubscriptiondate[2]);
2125 return 1 if ( @res &&
2127 Delta_Days($res[0],$res[1],$res[2],
2128 $datebeforeend[0],$datebeforeend[1],$datebeforeend[2]) <= 0) &&
2129 (@endofsubscriptiondate &&
2130 Delta_Days($res[0],$res[1],$res[2],
2131 $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) >= 0) );
2133 } elsif ($subscription->{numberlength}>0) {
2134 return (countissuesfrom($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength}-1);
2139 sub in_array { # used in next sub down
2140 my ( $val, @elements ) = @_;
2141 foreach my $elem (@elements) {
2142 if ( $val == $elem ) {
2151 $resultdate = GetNextDate($planneddate,$subscription)
2153 this function it takes the planneddate and will return the next issue's date and will skip dates if there
2154 exists an irregularity
2155 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2156 skipped then the returned date will be 2007-05-10
2159 $resultdate - then next date in the sequence
2161 Return 0 if periodicity==0
2165 sub GetNextDate(@) {
2166 my ( $planneddate, $subscription ) = @_;
2167 my @irreg = split( /\,/, $subscription->{irregularity} );
2169 #date supposed to be in ISO.
2171 my ( $year, $month, $day ) = split( /-/, $planneddate );
2172 $month = 1 unless ($month);
2173 $day = 1 unless ($day);
2176 # warn "DOW $dayofweek";
2177 if ( $subscription->{periodicity} % 16 == 0 ) { # 'without regularity' || 'irregular'
2182 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2183 # renaming this pattern from 1/day to " n / week ".
2184 if ( $subscription->{periodicity} == 1 ) {
2185 my $dayofweek = eval { Day_of_Week( $year, $month, $day ) };
2186 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2188 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2189 $dayofweek = 0 if ( $dayofweek == 7 );
2190 if ( in_array( ( $dayofweek + 1 ), @irreg ) ) {
2191 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 1 );
2195 @resultdate = Add_Delta_Days( $year, $month, $day, 1 );
2200 if ( $subscription->{periodicity} == 2 ) {
2201 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2202 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2204 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2206 #FIXME: if two consecutive irreg, do we only skip one?
2207 if ( $irreg[$i] == ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 ) ) {
2208 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 7 );
2209 $wkno = ( ( $wkno != 51 ) ? ( $wkno + 1 ) % 52 : 52 );
2212 @resultdate = Add_Delta_Days( $year, $month, $day, 7 );
2217 if ( $subscription->{periodicity} == 3 ) {
2218 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2219 if ($@) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2221 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2222 if ( $irreg[$i] == ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 ) ) {
2223 ### BUGFIX was previously +1 ^
2224 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 14 );
2225 $wkno = ( ( $wkno != 50 ) ? ( $wkno + 2 ) % 52 : 52 );
2228 @resultdate = Add_Delta_Days( $year, $month, $day, 14 );
2233 if ( $subscription->{periodicity} == 4 ) {
2234 my ( $wkno, $year ) = eval { Week_of_Year( $year, $month, $day ) };
2235 if ($@) { warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@"; }
2237 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2238 if ( $irreg[$i] == ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 ) ) {
2239 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, 21 );
2240 $wkno = ( ( $wkno != 49 ) ? ( $wkno + 3 ) % 52 : 52 );
2243 @resultdate = Add_Delta_Days( $year, $month, $day, 21 );
2246 my $tmpmonth = $month;
2247 if ( $year && $month && $day ) {
2248 if ( $subscription->{periodicity} == 5 ) {
2249 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2250 if ( $irreg[$i] == ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 ) ) {
2251 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2252 $tmpmonth = ( ( $tmpmonth != 11 ) ? ( $tmpmonth + 1 ) % 12 : 12 );
2255 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 1, 0 );
2257 if ( $subscription->{periodicity} == 6 ) {
2258 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2259 if ( $irreg[$i] == ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 ) ) {
2260 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2261 $tmpmonth = ( ( $tmpmonth != 10 ) ? ( $tmpmonth + 2 ) % 12 : 12 );
2264 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 2, 0 );
2266 if ( $subscription->{periodicity} == 7 ) {
2267 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2268 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2269 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2270 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2273 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2275 if ( $subscription->{periodicity} == 8 ) {
2276 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2277 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2278 ( $year, $month, $day ) = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2279 $tmpmonth = ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 );
2282 @resultdate = Add_Delta_YMD( $year, $month, $day, 0, 3, 0 );
2284 if ( $subscription->{periodicity} == 9 ) {
2285 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2286 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ? ( $tmpmonth + 3 ) % 12 : 12 ) ) {
2287 ### BUFIX Seems to need more Than One ?
2288 ( $year, $month, $day ) = Add_Delta_YM( $year, $month, $day, 0, 6 );
2289 $tmpmonth = ( ( $tmpmonth != 6 ) ? ( $tmpmonth + 6 ) % 12 : 12 );
2292 @resultdate = Add_Delta_YM( $year, $month, $day, 0, 6 );
2294 if ( $subscription->{periodicity} == 10 ) {
2295 @resultdate = Add_Delta_YM( $year, $month, $day, 1, 0 );
2297 if ( $subscription->{periodicity} == 11 ) {
2298 @resultdate = Add_Delta_YM( $year, $month, $day, 2, 0 );
2301 my $resultdate = sprintf( "%04d-%02d-%02d", $resultdate[0], $resultdate[1], $resultdate[2] );
2303 return "$resultdate";
2308 $item = itemdata($barcode);
2310 Looks up the item with the given barcode, and returns a
2311 reference-to-hash containing information about that item. The keys of
2312 the hash are the fields from the C<items> and C<biblioitems> tables in
2320 my $dbh = C4::Context->dbh;
2321 my $sth = $dbh->prepare(
2322 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2325 $sth->execute($barcode);
2326 my $data = $sth->fetchrow_hashref;
2336 Koha Development Team <info@koha.org>