3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Biblibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use C4
::Auth
qw(haspermission);
25 use C4
::Dates
qw(format_date format_date_in_iso);
27 use Date
::Calc
qw(:all);
28 use POSIX
qw(strftime);
30 use C4
::Log
; # logaction
32 use C4
::Serials
::Frequency
;
33 use C4
::Serials
::Numberpattern
;
35 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 $VERSION = 3.07.00.049; # set version for version checking
42 &NewSubscription &ModSubscription &DelSubscription
43 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
45 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
46 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
47 &GetSubscriptionHistoryFromSubscriptionId
49 &GetNextSeq &GetSeq &NewIssue &ItemizeSerials &GetSerials
50 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
51 &ReNewSubscription &GetLateOrMissingIssues
52 &GetSerialInformation &AddItem2Serial
53 &PrepareSerialsData &GetNextExpected &ModNextExpected
55 &UpdateClaimdateIssues
56 &GetSuppliersWithLateIssues &getsupplierbyserialid
57 &GetDistributedTo &SetDistributedTo
58 &getroutinglist &delroutingmember &addroutingmember
60 &check_routing &updateClaim &removeMissingIssue
63 &GetSubscriptionsFromBorrower
64 &subscriptionCurrentlyOnOrder
71 C4::Serials - Serials Module Functions
79 Functions for handling subscriptions, claims routing etc.
84 =head2 GetSuppliersWithLateIssues
86 $supplierlist = GetSuppliersWithLateIssues()
88 this function get all suppliers with late issues.
91 an array_ref of suppliers each entry is a hash_ref containing id and name
92 the array is in name order
96 sub GetSuppliersWithLateIssues
{
97 my $dbh = C4
::Context
->dbh;
99 SELECT DISTINCT id
, name
101 LEFT JOIN serial ON serial
.subscriptionid
=subscription
.subscriptionid
102 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
= aqbooksellers
.id
105 (planneddate
< now
() AND serial
.status
=1)
106 OR serial
.STATUS IN
(3, 4, 41, 42, 43, 44, 7)
108 AND subscription
.closed
= 0
110 return $dbh->selectall_arrayref($query, { Slice
=> {} });
113 =head2 GetSubscriptionHistoryFromSubscriptionId
115 $history = GetSubscriptionHistoryFromSubscriptionId($subscriptionid);
117 This function returns the subscription history as a hashref
121 sub GetSubscriptionHistoryFromSubscriptionId
{
122 my ($subscriptionid) = @_;
124 return unless $subscriptionid;
126 my $dbh = C4
::Context
->dbh;
129 FROM subscriptionhistory
130 WHERE subscriptionid
= ?
132 my $sth = $dbh->prepare($query);
133 $sth->execute($subscriptionid);
134 my $results = $sth->fetchrow_hashref;
140 =head2 GetSerialStatusFromSerialId
142 $sth = GetSerialStatusFromSerialId();
143 this function returns a statement handle
144 After this function, don't forget to execute it by using $sth->execute($serialid)
146 $sth = $dbh->prepare($query).
150 sub GetSerialStatusFromSerialId
{
151 my $dbh = C4
::Context
->dbh;
157 return $dbh->prepare($query);
160 =head2 GetSerialInformation
163 $data = GetSerialInformation($serialid);
164 returns a hash_ref containing :
165 items : items marcrecord (can be an array)
167 subscription table field
168 + information about subscription expiration
172 sub GetSerialInformation
{
174 my $dbh = C4
::Context
->dbh;
176 SELECT serial
.*, serial
.notes as sernotes
, serial
.status as serstatus
,subscription
.*,subscription
.subscriptionid as subsid
177 FROM serial LEFT JOIN subscription ON subscription
.subscriptionid
=serial
.subscriptionid
180 my $rq = $dbh->prepare($query);
181 $rq->execute($serialid);
182 my $data = $rq->fetchrow_hashref;
184 # create item information if we have serialsadditems for this subscription
185 if ( $data->{'serialsadditems'} ) {
186 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
187 $queryitem->execute($serialid);
188 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
190 if ( scalar(@
$itemnumbers) > 0 ) {
191 foreach my $itemnum (@
$itemnumbers) {
193 #It is ASSUMED that GetMarcItem ALWAYS WORK...
194 #Maybe GetMarcItem should return values on failure
195 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
196 my $itemprocessed = C4
::Items
::PrepareItemrecordDisplay
( $data->{'biblionumber'}, $itemnum->[0], $data );
197 $itemprocessed->{'itemnumber'} = $itemnum->[0];
198 $itemprocessed->{'itemid'} = $itemnum->[0];
199 $itemprocessed->{'serialid'} = $serialid;
200 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
201 push @
{ $data->{'items'} }, $itemprocessed;
204 my $itemprocessed = C4
::Items
::PrepareItemrecordDisplay
( $data->{'biblionumber'}, '', $data );
205 $itemprocessed->{'itemid'} = "N$serialid";
206 $itemprocessed->{'serialid'} = $serialid;
207 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
208 $itemprocessed->{'countitems'} = 0;
209 push @
{ $data->{'items'} }, $itemprocessed;
212 $data->{ "status" . $data->{'serstatus'} } = 1;
213 $data->{'subscriptionexpired'} = HasSubscriptionExpired
( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
214 $data->{'abouttoexpire'} = abouttoexpire
( $data->{'subscriptionid'} );
215 $data->{cannotedit
} = not can_edit_subscription
( $data );
219 =head2 AddItem2Serial
221 $rows = AddItem2Serial($serialid,$itemnumber);
222 Adds an itemnumber to Serial record
223 returns the number of rows affected
228 my ( $serialid, $itemnumber ) = @_;
230 return unless ($serialid and $itemnumber);
232 my $dbh = C4
::Context
->dbh;
233 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
234 $rq->execute( $serialid, $itemnumber );
238 =head2 UpdateClaimdateIssues
240 UpdateClaimdateIssues($serialids,[$date]);
242 Update Claimdate for issues in @$serialids list with date $date
247 sub UpdateClaimdateIssues
{
248 my ( $serialids, $date ) = @_;
250 return unless ($serialids);
252 my $dbh = C4
::Context
->dbh;
253 $date = strftime
( "%Y-%m-%d", localtime ) unless ($date);
258 claims_count = claims_count + 1
259 WHERE serialid in (" . join( ",", map { '?' } @
$serialids ) . ")
261 my $rq = $dbh->prepare($query);
262 $rq->execute($date, @
$serialids);
266 =head2 GetSubscription
268 $subs = GetSubscription($subscriptionid)
269 this function returns the subscription which has $subscriptionid as id.
271 a hashref. This hash containts
272 subscription, subscriptionhistory, aqbooksellers.name, biblio.title
276 sub GetSubscription
{
277 my ($subscriptionid) = @_;
278 my $dbh = C4
::Context
->dbh;
280 SELECT subscription
.*,
281 subscriptionhistory
.*,
282 aqbooksellers
.name AS aqbooksellername
,
283 biblio
.title AS bibliotitle
,
284 subscription
.biblionumber as bibnum
286 LEFT JOIN subscriptionhistory ON subscription
.subscriptionid
=subscriptionhistory
.subscriptionid
287 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
288 LEFT JOIN biblio ON biblio
.biblionumber
=subscription
.biblionumber
289 WHERE subscription
.subscriptionid
= ?
292 $debug and warn "query : $query\nsubsid :$subscriptionid";
293 my $sth = $dbh->prepare($query);
294 $sth->execute($subscriptionid);
295 my $subscription = $sth->fetchrow_hashref;
296 $subscription->{cannotedit
} = not can_edit_subscription
( $subscription );
297 return $subscription;
300 =head2 GetFullSubscription
302 $array_ref = GetFullSubscription($subscriptionid)
303 this function reads the serial table.
307 sub GetFullSubscription
{
308 my ($subscriptionid) = @_;
310 return unless ($subscriptionid);
312 my $dbh = C4
::Context
->dbh;
314 SELECT serial
.serialid
,
317 serial
.publisheddate
,
319 serial
.notes as notes
,
320 year
(IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
)) as year
,
321 aqbooksellers
.name as aqbooksellername
,
322 biblio
.title as bibliotitle
,
323 subscription
.branchcode AS branchcode
,
324 subscription
.subscriptionid AS subscriptionid
326 LEFT JOIN subscription ON
327 (serial
.subscriptionid
=subscription
.subscriptionid
)
328 LEFT JOIN aqbooksellers on subscription
.aqbooksellerid
=aqbooksellers
.id
329 LEFT JOIN biblio on biblio
.biblionumber
=subscription
.biblionumber
330 WHERE serial
.subscriptionid
= ?
332 IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
) DESC
,
333 serial
.subscriptionid
335 $debug and warn "GetFullSubscription query: $query";
336 my $sth = $dbh->prepare($query);
337 $sth->execute($subscriptionid);
338 my $subscriptions = $sth->fetchall_arrayref( {} );
339 for my $subscription ( @
$subscriptions ) {
340 $subscription->{cannotedit
} = not can_edit_subscription
( $subscription );
342 return $subscriptions;
345 =head2 PrepareSerialsData
347 $array_ref = PrepareSerialsData($serialinfomation)
348 where serialinformation is a hashref array
352 sub PrepareSerialsData
{
355 return unless ($lines);
361 my $aqbooksellername;
365 my $previousnote = "";
367 foreach my $subs (@
{$lines}) {
368 for my $datefield ( qw(publisheddate planneddate) ) {
369 # handle 0000-00-00 dates
370 if (defined $subs->{$datefield} and $subs->{$datefield} =~ m/^00/) {
371 $subs->{$datefield} = undef;
374 $subs->{ "status" . $subs->{'status'} } = 1;
375 if ( grep { $_ == $subs->{status
} } qw( 1 3 4 41 42 43 44 7 ) ) {
376 $subs->{"checked"} = 1;
379 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
380 $year = $subs->{'year'};
384 if ( $tmpresults{$year} ) {
385 push @
{ $tmpresults{$year}->{'serials'} }, $subs;
387 $tmpresults{$year} = {
389 'aqbooksellername' => $subs->{'aqbooksellername'},
390 'bibliotitle' => $subs->{'bibliotitle'},
391 'serials' => [$subs],
396 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
397 push @res, $tmpresults{$key};
402 =head2 GetSubscriptionsFromBiblionumber
404 $array_ref = GetSubscriptionsFromBiblionumber($biblionumber)
405 this function get the subscription list. it reads the subscription table.
407 reference to an array of subscriptions which have the biblionumber given on input arg.
408 each element of this array is a hashref containing
409 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
413 sub GetSubscriptionsFromBiblionumber
{
414 my ($biblionumber) = @_;
416 return unless ($biblionumber);
418 my $dbh = C4
::Context
->dbh;
420 SELECT subscription
.*,
422 subscriptionhistory
.*,
423 aqbooksellers
.name AS aqbooksellername
,
424 biblio
.title AS bibliotitle
426 LEFT JOIN subscriptionhistory ON subscription
.subscriptionid
=subscriptionhistory
.subscriptionid
427 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
428 LEFT JOIN biblio ON biblio
.biblionumber
=subscription
.biblionumber
429 LEFT JOIN branches ON branches
.branchcode
=subscription
.branchcode
430 WHERE subscription
.biblionumber
= ?
432 my $sth = $dbh->prepare($query);
433 $sth->execute($biblionumber);
435 while ( my $subs = $sth->fetchrow_hashref ) {
436 $subs->{startdate
} = format_date
( $subs->{startdate
} );
437 $subs->{histstartdate
} = format_date
( $subs->{histstartdate
} );
438 $subs->{histenddate
} = format_date
( $subs->{histenddate
} );
439 $subs->{opacnote
} =~ s/\n/\<br\/\
>/g
;
440 $subs->{missinglist
} =~ s/\n/\<br\/\
>/g
;
441 $subs->{recievedlist
} =~ s/\n/\<br\/\
>/g
;
442 $subs->{ "periodicity" . $subs->{periodicity
} } = 1;
443 $subs->{ "numberpattern" . $subs->{numberpattern
} } = 1;
444 $subs->{ "status" . $subs->{'status'} } = 1;
446 if ( $subs->{enddate
} eq '0000-00-00' ) {
447 $subs->{enddate
} = '';
449 $subs->{enddate
} = format_date
( $subs->{enddate
} );
451 $subs->{'abouttoexpire'} = abouttoexpire
( $subs->{'subscriptionid'} );
452 $subs->{'subscriptionexpired'} = HasSubscriptionExpired
( $subs->{'subscriptionid'} );
453 $subs->{cannotedit
} = not can_edit_subscription
( $subs );
459 =head2 GetFullSubscriptionsFromBiblionumber
461 $array_ref = GetFullSubscriptionsFromBiblionumber($biblionumber)
462 this function reads the serial table.
466 sub GetFullSubscriptionsFromBiblionumber
{
467 my ($biblionumber) = @_;
468 my $dbh = C4
::Context
->dbh;
470 SELECT serial
.serialid
,
473 serial
.publisheddate
,
475 serial
.notes as notes
,
476 year
(IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
)) as year
,
477 biblio
.title as bibliotitle
,
478 subscription
.branchcode AS branchcode
,
479 subscription
.subscriptionid AS subscriptionid
481 LEFT JOIN subscription ON
482 (serial
.subscriptionid
=subscription
.subscriptionid
)
483 LEFT JOIN aqbooksellers on subscription
.aqbooksellerid
=aqbooksellers
.id
484 LEFT JOIN biblio on biblio
.biblionumber
=subscription
.biblionumber
485 WHERE subscription
.biblionumber
= ?
487 IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
) DESC
,
488 serial
.subscriptionid
490 my $sth = $dbh->prepare($query);
491 $sth->execute($biblionumber);
492 my $subscriptions = $sth->fetchall_arrayref( {} );
493 for my $subscription ( @
$subscriptions ) {
494 $subscription->{cannotedit
} = not can_edit_subscription
( $subscription );
496 return $subscriptions;
499 =head2 SearchSubscriptions
501 @results = SearchSubscriptions($args);
503 This function returns a list of hashrefs, one for each subscription
504 that meets the conditions specified by the $args hashref.
506 The valid search fields are:
520 The expiration_date search field is special; it specifies the maximum
521 subscription expiration date.
525 sub SearchSubscriptions
{
530 subscription.notes AS publicnotes,
531 subscriptionhistory.*,
533 biblio.notes AS biblionotes,
539 LEFT JOIN subscriptionhistory USING(subscriptionid)
540 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
541 LEFT JOIN biblioitems ON biblioitems.biblionumber = subscription.biblionumber
542 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
546 if( $args->{biblionumber
} ) {
547 push @where_strs, "biblio.biblionumber = ?";
548 push @where_args, $args->{biblionumber
};
550 if( $args->{title
} ){
551 my @words = split / /, $args->{title
};
553 foreach my $word (@words) {
554 push @strs, "biblio.title LIKE ?";
555 push @args, "%$word%";
558 push @where_strs, '(' . join (' AND ', @strs) . ')';
559 push @where_args, @args;
563 push @where_strs, "biblioitems.issn LIKE ?";
564 push @where_args, "%$args->{issn}%";
567 push @where_strs, "biblioitems.ean LIKE ?";
568 push @where_args, "%$args->{ean}%";
570 if ( $args->{callnumber
} ) {
571 push @where_strs, "subscription.callnumber LIKE ?";
572 push @where_args, "%$args->{callnumber}%";
574 if( $args->{publisher
} ){
575 push @where_strs, "biblioitems.publishercode LIKE ?";
576 push @where_args, "%$args->{publisher}%";
578 if( $args->{bookseller
} ){
579 push @where_strs, "aqbooksellers.name LIKE ?";
580 push @where_args, "%$args->{bookseller}%";
582 if( $args->{branch
} ){
583 push @where_strs, "subscription.branchcode = ?";
584 push @where_args, "$args->{branch}";
586 if ( $args->{location
} ) {
587 push @where_strs, "subscription.location = ?";
588 push @where_args, "$args->{location}";
590 if ( $args->{expiration_date
} ) {
591 push @where_strs, "subscription.enddate <= ?";
592 push @where_args, "$args->{expiration_date}";
594 if( defined $args->{closed
} ){
595 push @where_strs, "subscription.closed = ?";
596 push @where_args, "$args->{closed}";
599 $query .= " WHERE " . join(" AND ", @where_strs);
602 $query .= " ORDER BY " . $args->{orderby
} if $args->{orderby
};
604 my $dbh = C4
::Context
->dbh;
605 my $sth = $dbh->prepare($query);
606 $sth->execute(@where_args);
607 my $results = $sth->fetchall_arrayref( {} );
610 for my $subscription ( @
$results ) {
611 $subscription->{cannotedit
} = not can_edit_subscription
( $subscription );
612 $subscription->{cannotdisplay
} = not can_show_subscription
( $subscription );
621 ($totalissues,@serials) = GetSerials($subscriptionid);
622 this function gets every serial not arrived for a given subscription
623 as well as the number of issues registered in the database (all types)
624 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
626 FIXME: We should return \@serials.
631 my ( $subscriptionid, $count ) = @_;
633 return unless $subscriptionid;
635 my $dbh = C4
::Context
->dbh;
637 # status = 2 is "arrived"
639 $count = 5 unless ($count);
641 my $query = "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
643 WHERE subscriptionid = ? AND status NOT IN (2, 4, 41, 42, 43, 44, 5)
644 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
645 my $sth = $dbh->prepare($query);
646 $sth->execute($subscriptionid);
648 while ( my $line = $sth->fetchrow_hashref ) {
649 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
650 for my $datefield ( qw( planneddate publisheddate) ) {
651 if ($line->{$datefield} && $line->{$datefield}!~m/^00/) {
652 $line->{$datefield} = format_date
( $line->{$datefield});
654 $line->{$datefield} = q{};
657 push @serials, $line;
660 # OK, now add the last 5 issues arrives/missing
661 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
663 WHERE subscriptionid = ?
664 AND (status in (2, 4, 41, 42, 43, 44, 5))
665 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
667 $sth = $dbh->prepare($query);
668 $sth->execute($subscriptionid);
669 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
671 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
672 for my $datefield ( qw( planneddate publisheddate) ) {
673 if ($line->{$datefield} && $line->{$datefield}!~m/^00/) {
674 $line->{$datefield} = format_date
( $line->{$datefield});
676 $line->{$datefield} = q{};
680 push @serials, $line;
683 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
684 $sth = $dbh->prepare($query);
685 $sth->execute($subscriptionid);
686 my ($totalissues) = $sth->fetchrow;
687 return ( $totalissues, @serials );
692 @serials = GetSerials2($subscriptionid,$status);
693 this function returns every serial waited for a given subscription
694 as well as the number of issues registered in the database (all types)
695 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
700 my ( $subscription, $status ) = @_;
702 return unless ($subscription and $status);
704 my $dbh = C4
::Context
->dbh;
706 SELECT serialid
,serialseq
, status
, planneddate
, publisheddate
,notes
, routingnotes
708 WHERE subscriptionid
=$subscription AND status IN
($status)
709 ORDER BY publisheddate
,serialid DESC
711 $debug and warn "GetSerials2 query: $query";
712 my $sth = $dbh->prepare($query);
716 while ( my $line = $sth->fetchrow_hashref ) {
717 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
718 # Format dates for display
719 for my $datefield ( qw( planneddate publisheddate ) ) {
720 if (!defined($line->{$datefield}) || $line->{$datefield} =~m/^00/) {
721 $line->{$datefield} = q{};
724 $line->{$datefield} = format_date
( $line->{$datefield} );
727 push @serials, $line;
732 =head2 GetLatestSerials
734 \@serials = GetLatestSerials($subscriptionid,$limit)
735 get the $limit's latest serials arrived or missing for a given subscription
737 a ref to an array which contains all of the latest serials stored into a hash.
741 sub GetLatestSerials
{
742 my ( $subscriptionid, $limit ) = @_;
744 return unless ($subscriptionid and $limit);
746 my $dbh = C4
::Context
->dbh;
748 # status = 2 is "arrived"
749 my $strsth = "SELECT serialid,serialseq, status, planneddate, publisheddate, notes
751 WHERE subscriptionid = ?
752 AND status IN (2, 4, 41, 42, 43, 44)
753 ORDER BY publisheddate DESC LIMIT 0,$limit
755 my $sth = $dbh->prepare($strsth);
756 $sth->execute($subscriptionid);
758 while ( my $line = $sth->fetchrow_hashref ) {
759 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
760 $line->{"planneddate"} = format_date
( $line->{"planneddate"} );
761 $line->{"publisheddate"} = format_date
( $line->{"publisheddate"} );
762 push @serials, $line;
768 =head2 GetDistributedTo
770 $distributedto=GetDistributedTo($subscriptionid)
771 This function returns the field distributedto for the subscription matching subscriptionid
775 sub GetDistributedTo
{
776 my $dbh = C4
::Context
->dbh;
778 my ($subscriptionid) = @_;
780 return unless ($subscriptionid);
782 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
783 my $sth = $dbh->prepare($query);
784 $sth->execute($subscriptionid);
785 return ($distributedto) = $sth->fetchrow;
791 $nextseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
792 $newinnerloop1, $newinnerloop2, $newinnerloop3
793 ) = GetNextSeq( $subscription, $pattern, $planneddate );
795 $subscription is a hashref containing all the attributes of the table
797 $pattern is a hashref containing all the attributes of the table
798 'subscription_numberpatterns'.
799 $planneddate is a C4::Dates object.
800 This function get the next issue for the subscription given on input arg
805 my ($subscription, $pattern, $planneddate) = @_;
807 return unless ($subscription and $pattern);
809 my ( $newlastvalue1, $newlastvalue2, $newlastvalue3,
810 $newinnerloop1, $newinnerloop2, $newinnerloop3 );
813 if ($subscription->{'skip_serialseq'}) {
814 my @irreg = split /;/, $subscription->{'irregularity'};
816 my $irregularities = {};
817 $irregularities->{$_} = 1 foreach(@irreg);
818 my $issueno = GetFictiveIssueNumber
($subscription, $planneddate) + 1;
819 while($irregularities->{$issueno}) {
826 my $numberingmethod = $pattern->{numberingmethod
};
828 if ($numberingmethod) {
829 $calculated = $numberingmethod;
830 my $locale = $subscription->{locale
};
831 $newlastvalue1 = $subscription->{lastvalue1
} || 0;
832 $newlastvalue2 = $subscription->{lastvalue2
} || 0;
833 $newlastvalue3 = $subscription->{lastvalue3
} || 0;
834 $newinnerloop1 = $subscription->{innerloop1
} || 0;
835 $newinnerloop2 = $subscription->{innerloop2
} || 0;
836 $newinnerloop3 = $subscription->{innerloop3
} || 0;
839 $calc{$_} = 1 if ($numberingmethod =~ /\{$_\}/);
842 for(my $i = 0; $i < $count; $i++) {
844 # check if we have to increase the new value.
846 if ($newinnerloop1 >= $pattern->{every1
}) {
848 $newlastvalue1 += $pattern->{add1
};
850 # reset counter if needed.
851 $newlastvalue1 = $pattern->{setto1
} if ($newlastvalue1 > $pattern->{whenmorethan1
});
854 # check if we have to increase the new value.
856 if ($newinnerloop2 >= $pattern->{every2
}) {
858 $newlastvalue2 += $pattern->{add2
};
860 # reset counter if needed.
861 $newlastvalue2 = $pattern->{setto2
} if ($newlastvalue2 > $pattern->{whenmorethan2
});
864 # check if we have to increase the new value.
866 if ($newinnerloop3 >= $pattern->{every3
}) {
868 $newlastvalue3 += $pattern->{add3
};
870 # reset counter if needed.
871 $newlastvalue3 = $pattern->{setto3
} if ($newlastvalue3 > $pattern->{whenmorethan3
});
875 my $newlastvalue1string = _numeration
( $newlastvalue1, $pattern->{numbering1
}, $locale );
876 $calculated =~ s/\{X\}/$newlastvalue1string/g;
879 my $newlastvalue2string = _numeration
( $newlastvalue2, $pattern->{numbering2
}, $locale );
880 $calculated =~ s/\{Y\}/$newlastvalue2string/g;
883 my $newlastvalue3string = _numeration
( $newlastvalue3, $pattern->{numbering3
}, $locale );
884 $calculated =~ s/\{Z\}/$newlastvalue3string/g;
889 $newlastvalue1, $newlastvalue2, $newlastvalue3,
890 $newinnerloop1, $newinnerloop2, $newinnerloop3);
895 $calculated = GetSeq($subscription, $pattern)
896 $subscription is a hashref containing all the attributes of the table 'subscription'
897 $pattern is a hashref containing all the attributes of the table 'subscription_numberpatterns'
898 this function transforms {X},{Y},{Z} to 150,0,0 for example.
900 the sequence in string format
905 my ($subscription, $pattern) = @_;
907 return unless ($subscription and $pattern);
909 my $locale = $subscription->{locale
};
911 my $calculated = $pattern->{numberingmethod
};
913 my $newlastvalue1 = $subscription->{'lastvalue1'} || 0;
914 $newlastvalue1 = _numeration
($newlastvalue1, $pattern->{numbering1
}, $locale) if ($pattern->{numbering1
}); # reset counter if needed.
915 $calculated =~ s/\{X\}/$newlastvalue1/g;
917 my $newlastvalue2 = $subscription->{'lastvalue2'} || 0;
918 $newlastvalue2 = _numeration
($newlastvalue2, $pattern->{numbering2
}, $locale) if ($pattern->{numbering2
}); # reset counter if needed.
919 $calculated =~ s/\{Y\}/$newlastvalue2/g;
921 my $newlastvalue3 = $subscription->{'lastvalue3'} || 0;
922 $newlastvalue3 = _numeration
($newlastvalue3, $pattern->{numbering3
}, $locale) if ($pattern->{numbering3
}); # reset counter if needed.
923 $calculated =~ s/\{Z\}/$newlastvalue3/g;
927 =head2 GetExpirationDate
929 $enddate = GetExpirationDate($subscriptionid, [$startdate])
931 this function return the next expiration date for a subscription given on input args.
938 sub GetExpirationDate
{
939 my ( $subscriptionid, $startdate ) = @_;
941 return unless ($subscriptionid);
943 my $dbh = C4
::Context
->dbh;
944 my $subscription = GetSubscription
($subscriptionid);
947 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
948 $enddate = $startdate || $subscription->{startdate
};
949 my @date = split( /-/, $enddate );
950 return if ( scalar(@date) != 3 || not check_date
(@date) );
951 my $frequency = C4
::Serials
::Frequency
::GetSubscriptionFrequency
($subscription->{periodicity
});
952 if ( $frequency and $frequency->{unit
} ) {
955 if ( my $length = $subscription->{numberlength
} ) {
957 #calculate the date of the last issue.
958 for ( my $i = 1 ; $i <= $length ; $i++ ) {
959 $enddate = GetNextDate
( $subscription, $enddate );
961 } elsif ( $subscription->{monthlength
} ) {
962 if ( $$subscription{startdate
} ) {
963 my @enddate = Add_Delta_YM
( $date[0], $date[1], $date[2], 0, $subscription->{monthlength
} );
964 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
966 } elsif ( $subscription->{weeklength
} ) {
967 if ( $$subscription{startdate
} ) {
968 my @date = split( /-/, $subscription->{startdate
} );
969 my @enddate = Add_Delta_Days
( $date[0], $date[1], $date[2], $subscription->{weeklength
} * 7 );
970 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
973 $enddate = $subscription->{enddate
};
977 return $subscription->{enddate
};
981 =head2 CountSubscriptionFromBiblionumber
983 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
984 this returns a count of the subscriptions for a given biblionumber
986 the number of subscriptions
990 sub CountSubscriptionFromBiblionumber
{
991 my ($biblionumber) = @_;
993 return unless ($biblionumber);
995 my $dbh = C4
::Context
->dbh;
996 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
997 my $sth = $dbh->prepare($query);
998 $sth->execute($biblionumber);
999 my $subscriptionsnumber = $sth->fetchrow;
1000 return $subscriptionsnumber;
1003 =head2 ModSubscriptionHistory
1005 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1007 this function modifies the history of a subscription. Put your new values on input arg.
1008 returns the number of rows affected
1012 sub ModSubscriptionHistory
{
1013 my ( $subscriptionid, $histstartdate, $enddate, $receivedlist, $missinglist, $opacnote, $librariannote ) = @_;
1015 return unless ($subscriptionid);
1017 my $dbh = C4
::Context
->dbh;
1018 my $query = "UPDATE subscriptionhistory
1019 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1020 WHERE subscriptionid=?
1022 my $sth = $dbh->prepare($query);
1023 $receivedlist =~ s/^; // if $receivedlist;
1024 $missinglist =~ s/^; // if $missinglist;
1025 $opacnote =~ s/^; // if $opacnote;
1026 $sth->execute( $histstartdate, $enddate, $receivedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
1030 =head2 ModSerialStatus
1032 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1034 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1035 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1039 sub ModSerialStatus
{
1040 my ( $serialid, $serialseq, $planneddate, $publisheddate, $status, $notes ) = @_;
1042 return unless ($serialid);
1044 #It is a usual serial
1045 # 1st, get previous status :
1046 my $dbh = C4
::Context
->dbh;
1047 my $query = "SELECT serial.subscriptionid,serial.status,subscription.periodicity
1048 FROM serial, subscription
1049 WHERE serial.subscriptionid=subscription.subscriptionid
1051 my $sth = $dbh->prepare($query);
1052 $sth->execute($serialid);
1053 my ( $subscriptionid, $oldstatus, $periodicity ) = $sth->fetchrow;
1054 my $frequency = GetSubscriptionFrequency
($periodicity);
1056 # change status & update subscriptionhistory
1058 if ( $status == 6 ) {
1059 DelIssue
( { 'serialid' => $serialid, 'subscriptionid' => $subscriptionid, 'serialseq' => $serialseq } );
1062 my $query = 'UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?';
1063 $sth = $dbh->prepare($query);
1064 $sth->execute( $serialseq, $publisheddate, $planneddate, $status, $notes, $serialid );
1065 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1066 $sth = $dbh->prepare($query);
1067 $sth->execute($subscriptionid);
1068 my $val = $sth->fetchrow_hashref;
1069 unless ( $val->{manualhistory
} ) {
1070 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1071 $sth = $dbh->prepare($query);
1072 $sth->execute($subscriptionid);
1073 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1075 if ( $status == 2 || ($oldstatus == 2 && $status != 2) ) {
1076 $recievedlist .= "; $serialseq"
1077 if ($recievedlist !~ /(^|;)\s*$serialseq(?=;|$)/);
1080 # in case serial has been previously marked as missing
1081 if (grep /$status/, (1,2,3,7)) {
1082 $missinglist=~ s/(^|;)\s*$serialseq(?=;|$)//g;
1085 my @missing_statuses = qw( 4 41 42 43 44 );
1086 $missinglist .= "; $serialseq"
1087 if ( ( grep { $_ == $status } @missing_statuses ) && ( $missinglist !~/(^|;)\s*$serialseq(?=;|$)/ ) );
1088 $missinglist .= "; not issued $serialseq"
1089 if ( $status == 5 && $missinglist !~ /(^|;)\s*$serialseq(?=;|$)/ );
1091 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1092 $sth = $dbh->prepare($query);
1093 $recievedlist =~ s/^; //;
1094 $missinglist =~ s/^; //;
1095 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1099 # create new waited entry if needed (ie : was a "waited" and has changed)
1100 if ( $oldstatus == 1 && $status != 1 ) {
1101 my $subscription = GetSubscription
($subscriptionid);
1102 my $pattern = C4
::Serials
::Numberpattern
::GetSubscriptionNumberpattern
($subscription->{numberpattern
});
1106 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
1107 $newinnerloop1, $newinnerloop2, $newinnerloop3
1109 = GetNextSeq
( $subscription, $pattern, $publisheddate );
1111 # next date (calculated from actual date & frequency parameters)
1112 my $nextpublisheddate = GetNextDate
($subscription, $publisheddate, 1);
1113 my $nextpubdate = $nextpublisheddate;
1114 NewIssue
( $newserialseq, $subscriptionid, $subscription->{'biblionumber'}, 1, $nextpubdate, $nextpubdate );
1115 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1116 WHERE subscriptionid = ?";
1117 $sth = $dbh->prepare($query);
1118 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1120 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1121 if ( $subscription->{letter
} && $status == 2 && $oldstatus != 2 ) {
1122 require C4
::Letters
;
1123 C4
::Letters
::SendAlerts
( 'issue', $subscription->{subscriptionid
}, $subscription->{letter
} );
1130 =head2 GetNextExpected
1132 $nextexpected = GetNextExpected($subscriptionid)
1134 Get the planneddate for the current expected issue of the subscription.
1140 planneddate => ISO date
1145 sub GetNextExpected
{
1146 my ($subscriptionid) = @_;
1148 my $dbh = C4
::Context
->dbh;
1152 WHERE subscriptionid
= ?
1156 my $sth = $dbh->prepare($query);
1158 # Each subscription has only one 'expected' issue, with serial.status==1.
1159 $sth->execute( $subscriptionid, 1 );
1160 my $nextissue = $sth->fetchrow_hashref;
1161 if ( !$nextissue ) {
1165 WHERE subscriptionid
= ?
1166 ORDER BY publisheddate DESC
1169 $sth = $dbh->prepare($query);
1170 $sth->execute($subscriptionid);
1171 $nextissue = $sth->fetchrow_hashref;
1173 foreach(qw
/planneddate publisheddate/) {
1174 if ( !defined $nextissue->{$_} ) {
1175 # or should this default to 1st Jan ???
1176 $nextissue->{$_} = strftime
( '%Y-%m-%d', localtime );
1178 $nextissue->{$_} = ($nextissue->{$_} ne '0000-00-00')
1186 =head2 ModNextExpected
1188 ModNextExpected($subscriptionid,$date)
1190 Update the planneddate for the current expected issue of the subscription.
1191 This will modify all future prediction results.
1193 C<$date> is an ISO date.
1199 sub ModNextExpected
{
1200 my ( $subscriptionid, $date ) = @_;
1201 my $dbh = C4
::Context
->dbh;
1203 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1204 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1206 # Each subscription has only one 'expected' issue, with serial.status==1.
1207 $sth->execute( $date, $date, $subscriptionid, 1 );
1212 =head2 GetSubscriptionIrregularities
1216 =item @irreg = &GetSubscriptionIrregularities($subscriptionid);
1217 get the list of irregularities for a subscription
1223 sub GetSubscriptionIrregularities
{
1224 my $subscriptionid = shift;
1226 return unless $subscriptionid;
1228 my $dbh = C4
::Context
->dbh;
1232 WHERE subscriptionid
= ?
1234 my $sth = $dbh->prepare($query);
1235 $sth->execute($subscriptionid);
1237 my ($result) = $sth->fetchrow_array;
1238 my @irreg = split /;/, $result;
1243 =head2 ModSubscription
1245 this function modifies a subscription. Put all new values on input args.
1246 returns the number of rows affected
1250 sub ModSubscription
{
1252 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
1253 $periodicity, $firstacquidate, $irregularity, $numberpattern, $locale,
1254 $numberlength, $weeklength, $monthlength, $lastvalue1, $innerloop1,
1255 $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, $status,
1256 $biblionumber, $callnumber, $notes, $letter, $manualhistory,
1257 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1258 $graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq
1261 my $dbh = C4
::Context
->dbh;
1262 my $query = "UPDATE subscription
1263 SET librarian=?, branchcode=?, aqbooksellerid=?, cost=?, aqbudgetid=?,
1264 startdate=?, periodicity=?, firstacquidate=?, irregularity=?,
1265 numberpattern=?, locale=?, numberlength=?, weeklength=?, monthlength=?,
1266 lastvalue1=?, innerloop1=?, lastvalue2=?, innerloop2=?,
1267 lastvalue3=?, innerloop3=?, status=?, biblionumber=?,
1268 callnumber=?, notes=?, letter=?, manualhistory=?,
1269 internalnotes=?, serialsadditems=?, staffdisplaycount=?,
1270 opacdisplaycount=?, graceperiod=?, location = ?, enddate=?,
1272 WHERE subscriptionid = ?";
1274 my $sth = $dbh->prepare($query);
1276 $auser, $branchcode, $aqbooksellerid, $cost,
1277 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1278 $irregularity, $numberpattern, $locale, $numberlength,
1279 $weeklength, $monthlength, $lastvalue1, $innerloop1,
1280 $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
1281 $status, $biblionumber, $callnumber, $notes,
1282 $letter, ($manualhistory ?
$manualhistory : 0),
1283 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1284 $graceperiod, $location, $enddate, $skip_serialseq,
1287 my $rows = $sth->rows;
1289 logaction
( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1293 =head2 NewSubscription
1295 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1296 $startdate,$periodicity,$numberlength,$weeklength,$monthlength,
1297 $lastvalue1,$innerloop1,$lastvalue2,$innerloop2,$lastvalue3,$innerloop3,
1298 $status, $notes, $letter, $firstacquidate, $irregularity, $numberpattern,
1299 $locale, $callnumber, $manualhistory, $internalnotes, $serialsadditems,
1300 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $skip_serialseq);
1302 Create a new subscription with value given on input args.
1305 the id of this new subscription
1309 sub NewSubscription
{
1311 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
1312 $startdate, $periodicity, $numberlength, $weeklength, $monthlength,
1313 $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3,
1314 $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity,
1315 $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes,
1316 $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod,
1317 $location, $enddate, $skip_serialseq
1319 my $dbh = C4
::Context
->dbh;
1321 #save subscription (insert into database)
1323 INSERT INTO subscription
1324 (librarian
, branchcode
, aqbooksellerid
, cost
, aqbudgetid
,
1325 biblionumber
, startdate
, periodicity
, numberlength
, weeklength
,
1326 monthlength
, lastvalue1
, innerloop1
, lastvalue2
, innerloop2
,
1327 lastvalue3
, innerloop3
, status
, notes
, letter
, firstacquidate
,
1328 irregularity
, numberpattern
, locale
, callnumber
,
1329 manualhistory
, internalnotes
, serialsadditems
, staffdisplaycount
,
1330 opacdisplaycount
, graceperiod
, location
, enddate
, skip_serialseq
)
1331 VALUES
(?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
1333 my $sth = $dbh->prepare($query);
1335 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
1336 $startdate, $periodicity, $numberlength, $weeklength,
1337 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
1338 $lastvalue3, $innerloop3, $status, $notes, $letter,
1339 $firstacquidate, $irregularity, $numberpattern, $locale, $callnumber,
1340 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
1341 $opacdisplaycount, $graceperiod, $location, $enddate, $skip_serialseq
1344 my $subscriptionid = $dbh->{'mysql_insertid'};
1346 $enddate = GetExpirationDate
( $subscriptionid, $startdate );
1350 WHERE subscriptionid
=?
1352 $sth = $dbh->prepare($query);
1353 $sth->execute( $enddate, $subscriptionid );
1356 # then create the 1st expected number
1358 INSERT INTO subscriptionhistory
1359 (biblionumber
, subscriptionid
, histstartdate
)
1362 $sth = $dbh->prepare($query);
1363 $sth->execute( $biblionumber, $subscriptionid, $startdate);
1365 # reread subscription to get a hash (for calculation of the 1st issue number)
1366 my $subscription = GetSubscription
($subscriptionid);
1367 my $pattern = C4
::Serials
::Numberpattern
::GetSubscriptionNumberpattern
($subscription->{numberpattern
});
1369 # calculate issue number
1370 my $serialseq = GetSeq
($subscription, $pattern) || q{};
1373 (serialseq
,subscriptionid
,biblionumber
,status
, planneddate
, publisheddate
)
1374 VALUES
(?
,?
,?
,?
,?
,?
)
1376 $sth = $dbh->prepare($query);
1377 $sth->execute( $serialseq, $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
1379 logaction
( "SERIAL", "ADD", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1381 #set serial flag on biblio if not already set.
1382 my $bib = GetBiblio
($biblionumber);
1383 if ( $bib and !$bib->{'serial'} ) {
1384 my $record = GetMarcBiblio
($biblionumber);
1385 my ( $tag, $subf ) = GetMarcFromKohaField
( 'biblio.serial', $bib->{'frameworkcode'} );
1387 eval { $record->field($tag)->update( $subf => 1 ); };
1389 ModBiblio
( $record, $biblionumber, $bib->{'frameworkcode'} );
1391 return $subscriptionid;
1394 =head2 ReNewSubscription
1396 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1398 this function renew a subscription with values given on input args.
1402 sub ReNewSubscription
{
1403 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1404 my $dbh = C4
::Context
->dbh;
1405 my $subscription = GetSubscription
($subscriptionid);
1409 LEFT JOIN biblioitems ON biblio
.biblionumber
=biblioitems
.biblionumber
1410 WHERE biblio
.biblionumber
=?
1412 my $sth = $dbh->prepare($query);
1413 $sth->execute( $subscription->{biblionumber
} );
1414 my $biblio = $sth->fetchrow_hashref;
1416 if ( C4
::Context
->preference("RenewSerialAddsSuggestion") ) {
1417 require C4
::Suggestions
;
1418 C4
::Suggestions
::NewSuggestion
(
1419 { 'suggestedby' => $user,
1420 'title' => $subscription->{bibliotitle
},
1421 'author' => $biblio->{author
},
1422 'publishercode' => $biblio->{publishercode
},
1423 'note' => $biblio->{note
},
1424 'biblionumber' => $subscription->{biblionumber
}
1429 # renew subscription
1432 SET startdate
=?
,numberlength
=?
,weeklength
=?
,monthlength
=?
,reneweddate
=NOW
()
1433 WHERE subscriptionid
=?
1435 $sth = $dbh->prepare($query);
1436 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1437 my $enddate = GetExpirationDate
($subscriptionid);
1438 $debug && warn "enddate :$enddate";
1442 WHERE subscriptionid
=?
1444 $sth = $dbh->prepare($query);
1445 $sth->execute( $enddate, $subscriptionid );
1447 UPDATE subscriptionhistory
1449 WHERE subscriptionid
=?
1451 $sth = $dbh->prepare($query);
1452 $sth->execute( $enddate, $subscriptionid );
1454 logaction
( "SERIAL", "RENEW", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1460 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1462 Create a new issue stored on the database.
1463 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1464 returns the serial id
1469 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
1470 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1472 return unless ($subscriptionid);
1474 my $dbh = C4
::Context
->dbh;
1477 (serialseq
,subscriptionid
,biblionumber
,status
,publisheddate
,planneddate
,notes
)
1478 VALUES
(?
,?
,?
,?
,?
,?
,?
)
1480 my $sth = $dbh->prepare($query);
1481 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
1482 my $serialid = $dbh->{'mysql_insertid'};
1484 SELECT missinglist
,recievedlist
1485 FROM subscriptionhistory
1486 WHERE subscriptionid
=?
1488 $sth = $dbh->prepare($query);
1489 $sth->execute($subscriptionid);
1490 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1492 if ( $status == 2 ) {
1493 ### TODO Add a feature that improves recognition and description.
1494 ### As such count (serialseq) i.e. : N18,2(N19),N20
1495 ### Would use substr and index But be careful to previous presence of ()
1496 $recievedlist .= "; $serialseq" unless (index($recievedlist,$serialseq)>0);
1498 if ( $status == 4 ) {
1499 $missinglist .= "; $serialseq" unless (index($missinglist,$serialseq)>0);
1502 UPDATE subscriptionhistory
1503 SET recievedlist
=?
, missinglist
=?
1504 WHERE subscriptionid
=?
1506 $sth = $dbh->prepare($query);
1507 $recievedlist =~ s/^; //;
1508 $missinglist =~ s/^; //;
1509 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1513 =head2 ItemizeSerials
1515 ItemizeSerials($serialid, $info);
1516 $info is a hashref containing barcode branch, itemcallnumber, status, location
1517 $serialid the serialid
1519 1 if the itemize is a succes.
1520 0 and @error otherwise. @error containts the list of errors found.
1524 sub ItemizeSerials
{
1525 my ( $serialid, $info ) = @_;
1527 return unless ($serialid);
1529 my $now = POSIX
::strftime
( "%Y-%m-%d", localtime );
1531 my $dbh = C4
::Context
->dbh;
1537 my $sth = $dbh->prepare($query);
1538 $sth->execute($serialid);
1539 my $data = $sth->fetchrow_hashref;
1540 if ( C4
::Context
->preference("RoutingSerials") ) {
1542 # check for existing biblioitem relating to serial issue
1543 my ( $count, @results ) = GetBiblioItemByBiblioNumber
( $data->{'biblionumber'} );
1545 for ( my $i = 0 ; $i < $count ; $i++ ) {
1546 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' (' . $data->{'planneddate'} . ')' ) {
1547 $bibitemno = $results[$i]->{'biblioitemnumber'};
1551 if ( $bibitemno == 0 ) {
1552 my $sth = $dbh->prepare( "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC" );
1553 $sth->execute( $data->{'biblionumber'} );
1554 my $biblioitem = $sth->fetchrow_hashref;
1555 $biblioitem->{'volumedate'} = $data->{planneddate
};
1556 $biblioitem->{'volumeddesc'} = $data->{serialseq
} . ' (' . format_date
( $data->{'planneddate'} ) . ')';
1557 $biblioitem->{'dewey'} = $info->{itemcallnumber
};
1561 my $fwk = GetFrameworkCode
( $data->{'biblionumber'} );
1562 if ( $info->{barcode
} ) {
1564 if ( is_barcode_in_use
( $info->{barcode
} ) ) {
1565 push @errors, 'barcode_not_unique';
1567 my $marcrecord = MARC
::Record
->new();
1568 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.barcode", $fwk );
1569 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{barcode
} );
1570 $marcrecord->insert_fields_ordered($newField);
1571 if ( $info->{branch
} ) {
1572 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.homebranch", $fwk );
1574 #warn "items.homebranch : $tag , $subfield";
1575 if ( $marcrecord->field($tag) ) {
1576 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch
} );
1578 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{branch
} );
1579 $marcrecord->insert_fields_ordered($newField);
1581 ( $tag, $subfield ) = GetMarcFromKohaField
( "items.holdingbranch", $fwk );
1583 #warn "items.holdingbranch : $tag , $subfield";
1584 if ( $marcrecord->field($tag) ) {
1585 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch
} );
1587 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{branch
} );
1588 $marcrecord->insert_fields_ordered($newField);
1591 if ( $info->{itemcallnumber
} ) {
1592 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.itemcallnumber", $fwk );
1594 if ( $marcrecord->field($tag) ) {
1595 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{itemcallnumber
} );
1597 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{itemcallnumber
} );
1598 $marcrecord->insert_fields_ordered($newField);
1601 if ( $info->{notes
} ) {
1602 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.itemnotes", $fwk );
1604 if ( $marcrecord->field($tag) ) {
1605 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{notes
} );
1607 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{notes
} );
1608 $marcrecord->insert_fields_ordered($newField);
1611 if ( $info->{location
} ) {
1612 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.location", $fwk );
1614 if ( $marcrecord->field($tag) ) {
1615 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{location
} );
1617 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{location
} );
1618 $marcrecord->insert_fields_ordered($newField);
1621 if ( $info->{status
} ) {
1622 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.notforloan", $fwk );
1624 if ( $marcrecord->field($tag) ) {
1625 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{status
} );
1627 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{status
} );
1628 $marcrecord->insert_fields_ordered($newField);
1631 if ( C4
::Context
->preference("RoutingSerials") ) {
1632 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.dateaccessioned", $fwk );
1633 if ( $marcrecord->field($tag) ) {
1634 $marcrecord->field($tag)->add_subfields( "$subfield" => $now );
1636 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $now );
1637 $marcrecord->insert_fields_ordered($newField);
1641 C4
::Items
::AddItemFromMarc
( $marcrecord, $data->{'biblionumber'} );
1644 return ( 0, @errors );
1648 =head2 HasSubscriptionStrictlyExpired
1650 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1652 the subscription has stricly expired when today > the end subscription date
1655 1 if true, 0 if false, -1 if the expiration date is not set.
1659 sub HasSubscriptionStrictlyExpired
{
1661 # Getting end of subscription date
1662 my ($subscriptionid) = @_;
1664 return unless ($subscriptionid);
1666 my $dbh = C4
::Context
->dbh;
1667 my $subscription = GetSubscription
($subscriptionid);
1668 my $expirationdate = $subscription->{enddate
} || GetExpirationDate
($subscriptionid);
1670 # If the expiration date is set
1671 if ( $expirationdate != 0 ) {
1672 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1674 # Getting today's date
1675 my ( $nowyear, $nowmonth, $nowday ) = Today
();
1677 # if today's date > expiration date, then the subscription has stricly expired
1678 if ( Delta_Days
( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1685 # There are some cases where the expiration date is not set
1686 # As we can't determine if the subscription has expired on a date-basis,
1692 =head2 HasSubscriptionExpired
1694 $has_expired = HasSubscriptionExpired($subscriptionid)
1696 the subscription has expired when the next issue to arrive is out of subscription limit.
1699 0 if the subscription has not expired
1700 1 if the subscription has expired
1701 2 if has subscription does not have a valid expiration date set
1705 sub HasSubscriptionExpired
{
1706 my ($subscriptionid) = @_;
1708 return unless ($subscriptionid);
1710 my $dbh = C4
::Context
->dbh;
1711 my $subscription = GetSubscription
($subscriptionid);
1712 my $frequency = C4
::Serials
::Frequency
::GetSubscriptionFrequency
($subscription->{periodicity
});
1713 if ( $frequency and $frequency->{unit
} ) {
1714 my $expirationdate = $subscription->{enddate
} || GetExpirationDate
($subscriptionid);
1715 if (!defined $expirationdate) {
1716 $expirationdate = q{};
1719 SELECT max
(planneddate
)
1721 WHERE subscriptionid
=?
1723 my $sth = $dbh->prepare($query);
1724 $sth->execute($subscriptionid);
1725 my ($res) = $sth->fetchrow;
1726 if (!$res || $res=~m/^0000/) {
1729 my @res = split( /-/, $res );
1730 my @endofsubscriptiondate = split( /-/, $expirationdate );
1731 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date
(@res) || not check_date
(@endofsubscriptiondate) );
1733 if ( ( @endofsubscriptiondate && Delta_Days
( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1738 if ( $subscription->{'numberlength'} ) {
1739 my $countreceived = countissuesfrom
( $subscriptionid, $subscription->{'startdate'} );
1740 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1746 return 0; # Notice that you'll never get here.
1749 =head2 SetDistributedto
1751 SetDistributedto($distributedto,$subscriptionid);
1752 This function update the value of distributedto for a subscription given on input arg.
1756 sub SetDistributedto
{
1757 my ( $distributedto, $subscriptionid ) = @_;
1758 my $dbh = C4
::Context
->dbh;
1762 WHERE subscriptionid
=?
1764 my $sth = $dbh->prepare($query);
1765 $sth->execute( $distributedto, $subscriptionid );
1769 =head2 DelSubscription
1771 DelSubscription($subscriptionid)
1772 this function deletes subscription which has $subscriptionid as id.
1776 sub DelSubscription
{
1777 my ($subscriptionid) = @_;
1778 my $dbh = C4
::Context
->dbh;
1779 $subscriptionid = $dbh->quote($subscriptionid);
1780 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1781 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1782 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1784 logaction
( "SERIAL", "DELETE", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1789 DelIssue($serialseq,$subscriptionid)
1790 this function deletes an issue which has $serialseq and $subscriptionid given on input arg.
1792 returns the number of rows affected
1797 my ($dataissue) = @_;
1798 my $dbh = C4
::Context
->dbh;
1799 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1804 AND subscriptionid
= ?
1806 my $mainsth = $dbh->prepare($query);
1807 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1809 #Delete element from subscription history
1810 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1811 my $sth = $dbh->prepare($query);
1812 $sth->execute( $dataissue->{'subscriptionid'} );
1813 my $val = $sth->fetchrow_hashref;
1814 unless ( $val->{manualhistory
} ) {
1816 SELECT
* FROM subscriptionhistory
1817 WHERE subscriptionid
= ?
1819 my $sth = $dbh->prepare($query);
1820 $sth->execute( $dataissue->{'subscriptionid'} );
1821 my $data = $sth->fetchrow_hashref;
1822 my $serialseq = $dataissue->{'serialseq'};
1823 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1824 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1825 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1826 $sth = $dbh->prepare($strsth);
1827 $sth->execute( $dataissue->{'subscriptionid'} );
1830 return $mainsth->rows;
1833 =head2 GetLateOrMissingIssues
1835 @issuelist = GetLateMissingIssues($supplierid,$serialid)
1837 this function selects missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1840 the issuelist as an array of hash refs. Each element of this array contains
1841 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1845 sub GetLateOrMissingIssues
{
1846 my ( $supplierid, $serialid, $order ) = @_;
1848 return unless ( $supplierid or $serialid );
1850 my $dbh = C4
::Context
->dbh;
1854 $byserial = "and serialid = " . $serialid;
1857 $order .= ", title";
1862 $sth = $dbh->prepare(
1864 serialid, aqbooksellerid, name,
1865 biblio.title, biblioitems.issn, planneddate, serialseq,
1866 serial.status, serial.subscriptionid, claimdate, claims_count,
1867 subscription.branchcode
1869 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1870 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1871 LEFT JOIN biblioitems ON subscription.biblionumber=biblioitems.biblionumber
1872 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1873 WHERE subscription.subscriptionid = serial.subscriptionid
1874 AND (serial.STATUS IN (4, 41, 42, 43, 44) OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1875 AND subscription.aqbooksellerid=$supplierid
1880 $sth = $dbh->prepare(
1882 serialid, aqbooksellerid, name,
1883 biblio.title, planneddate, serialseq,
1884 serial.status, serial.subscriptionid, claimdate, claims_count,
1885 subscription.branchcode
1887 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1888 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1889 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1890 WHERE subscription.subscriptionid = serial.subscriptionid
1891 AND (serial.STATUS IN (4, 41, 42, 43, 44) OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1898 while ( my $line = $sth->fetchrow_hashref ) {
1900 if ($line->{planneddate
} && $line->{planneddate
} !~/^0+\-/) {
1901 $line->{planneddateISO
} = $line->{planneddate
};
1902 $line->{planneddate
} = format_date
( $line->{planneddate
} );
1904 if ($line->{claimdate
} && $line->{claimdate
} !~/^0+\-/) {
1905 $line->{claimdateISO
} = $line->{claimdate
};
1906 $line->{claimdate
} = format_date
( $line->{claimdate
} );
1908 $line->{"status".$line->{status
}} = 1;
1909 push @issuelist, $line;
1914 =head2 removeMissingIssue
1916 removeMissingIssue($subscriptionid)
1918 this function removes an issue from being part of the missing string in
1919 subscriptionlist.missinglist column
1921 called when a missing issue is found from the serials-recieve.pl file
1925 sub removeMissingIssue
{
1926 my ( $sequence, $subscriptionid ) = @_;
1928 return unless ($sequence and $subscriptionid);
1930 my $dbh = C4
::Context
->dbh;
1931 my $sth = $dbh->prepare("SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1932 $sth->execute($subscriptionid);
1933 my $data = $sth->fetchrow_hashref;
1934 my $missinglist = $data->{'missinglist'};
1935 my $missinglistbefore = $missinglist;
1937 # warn $missinglist." before";
1938 $missinglist =~ s/($sequence)//;
1940 # warn $missinglist." after";
1941 if ( $missinglist ne $missinglistbefore ) {
1942 $missinglist =~ s/\|\s\|/\|/g;
1943 $missinglist =~ s/^\| //g;
1944 $missinglist =~ s/\|$//g;
1945 my $sth2 = $dbh->prepare(
1946 "UPDATE subscriptionhistory
1948 WHERE subscriptionid = ?"
1950 $sth2->execute( $missinglist, $subscriptionid );
1957 &updateClaim($serialid)
1959 this function updates the time when a claim is issued for late/missing items
1961 called from claims.pl file
1966 my ($serialid) = @_;
1967 my $dbh = C4
::Context
->dbh;
1970 SET claimdate
= NOW
(),
1971 claims_count
= claims_count
+ 1
1977 =head2 getsupplierbyserialid
1979 $result = getsupplierbyserialid($serialid)
1981 this function is used to find the supplier id given a serial id
1984 hashref containing serialid, subscriptionid, and aqbooksellerid
1988 sub getsupplierbyserialid
{
1989 my ($serialid) = @_;
1990 my $dbh = C4
::Context
->dbh;
1991 my $sth = $dbh->prepare(
1992 "SELECT serialid, serial.subscriptionid, aqbooksellerid
1994 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
1998 $sth->execute($serialid);
1999 my $line = $sth->fetchrow_hashref;
2000 my $result = $line->{'aqbooksellerid'};
2004 =head2 check_routing
2006 $result = &check_routing($subscriptionid)
2008 this function checks to see if a serial has a routing list and returns the count of routingid
2009 used to show either an 'add' or 'edit' link
2014 my ($subscriptionid) = @_;
2016 return unless ($subscriptionid);
2018 my $dbh = C4
::Context
->dbh;
2019 my $sth = $dbh->prepare(
2020 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2021 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2022 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2025 $sth->execute($subscriptionid);
2026 my $line = $sth->fetchrow_hashref;
2027 my $result = $line->{'routingids'};
2031 =head2 addroutingmember
2033 addroutingmember($borrowernumber,$subscriptionid)
2035 this function takes a borrowernumber and subscriptionid and adds the member to the
2036 routing list for that serial subscription and gives them a rank on the list
2037 of either 1 or highest current rank + 1
2041 sub addroutingmember
{
2042 my ( $borrowernumber, $subscriptionid ) = @_;
2044 return unless ($borrowernumber and $subscriptionid);
2047 my $dbh = C4
::Context
->dbh;
2048 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
2049 $sth->execute($subscriptionid);
2050 while ( my $line = $sth->fetchrow_hashref ) {
2051 if ( $line->{'rank'} > 0 ) {
2052 $rank = $line->{'rank'} + 1;
2057 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
2058 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2061 =head2 reorder_members
2063 reorder_members($subscriptionid,$routingid,$rank)
2065 this function is used to reorder the routing list
2067 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2068 - it gets all members on list puts their routingid's into an array
2069 - removes the one in the array that is $routingid
2070 - then reinjects $routingid at point indicated by $rank
2071 - then update the database with the routingids in the new order
2075 sub reorder_members
{
2076 my ( $subscriptionid, $routingid, $rank ) = @_;
2077 my $dbh = C4
::Context
->dbh;
2078 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
2079 $sth->execute($subscriptionid);
2081 while ( my $line = $sth->fetchrow_hashref ) {
2082 push( @result, $line->{'routingid'} );
2085 # To find the matching index
2087 my $key = -1; # to allow for 0 being a valid response
2088 for ( $i = 0 ; $i < @result ; $i++ ) {
2089 if ( $routingid == $result[$i] ) {
2090 $key = $i; # save the index
2095 # if index exists in array then move it to new position
2096 if ( $key > -1 && $rank > 0 ) {
2097 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
2098 my $moving_item = splice( @result, $key, 1 );
2099 splice( @result, $new_rank, 0, $moving_item );
2101 for ( my $j = 0 ; $j < @result ; $j++ ) {
2102 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
2108 =head2 delroutingmember
2110 delroutingmember($routingid,$subscriptionid)
2112 this function either deletes one member from routing list if $routingid exists otherwise
2113 deletes all members from the routing list
2117 sub delroutingmember
{
2119 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2120 my ( $routingid, $subscriptionid ) = @_;
2121 my $dbh = C4
::Context
->dbh;
2123 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2124 $sth->execute($routingid);
2125 reorder_members
( $subscriptionid, $routingid );
2127 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2128 $sth->execute($subscriptionid);
2133 =head2 getroutinglist
2135 @routinglist = getroutinglist($subscriptionid)
2137 this gets the info from the subscriptionroutinglist for $subscriptionid
2140 the routinglist as an array. Each element of the array contains a hash_ref containing
2141 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2145 sub getroutinglist
{
2146 my ($subscriptionid) = @_;
2147 my $dbh = C4
::Context
->dbh;
2148 my $sth = $dbh->prepare(
2149 'SELECT routingid, borrowernumber, ranking, biblionumber
2151 JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2152 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC'
2154 $sth->execute($subscriptionid);
2155 my $routinglist = $sth->fetchall_arrayref({});
2156 return @
{$routinglist};
2159 =head2 countissuesfrom
2161 $result = countissuesfrom($subscriptionid,$startdate)
2163 Returns a count of serial rows matching the given subsctiptionid
2164 with published date greater than startdate
2168 sub countissuesfrom
{
2169 my ( $subscriptionid, $startdate ) = @_;
2170 my $dbh = C4
::Context
->dbh;
2174 WHERE subscriptionid
=?
2175 AND serial
.publisheddate
>?
2177 my $sth = $dbh->prepare($query);
2178 $sth->execute( $subscriptionid, $startdate );
2179 my ($countreceived) = $sth->fetchrow;
2180 return $countreceived;
2185 $result = CountIssues($subscriptionid)
2187 Returns a count of serial rows matching the given subsctiptionid
2192 my ($subscriptionid) = @_;
2193 my $dbh = C4
::Context
->dbh;
2197 WHERE subscriptionid
=?
2199 my $sth = $dbh->prepare($query);
2200 $sth->execute($subscriptionid);
2201 my ($countreceived) = $sth->fetchrow;
2202 return $countreceived;
2207 $result = HasItems($subscriptionid)
2209 returns a count of items from serial matching the subscriptionid
2214 my ($subscriptionid) = @_;
2215 my $dbh = C4
::Context
->dbh;
2217 SELECT COUNT
(serialitems
.itemnumber
)
2219 LEFT JOIN serialitems USING
(serialid
)
2220 WHERE subscriptionid
=? AND serialitems
.serialid IS NOT NULL
2222 my $sth=$dbh->prepare($query);
2223 $sth->execute($subscriptionid);
2224 my ($countitems)=$sth->fetchrow_array();
2228 =head2 abouttoexpire
2230 $result = abouttoexpire($subscriptionid)
2232 this function alerts you to the penultimate issue for a serial subscription
2234 returns 1 - if this is the penultimate issue
2240 my ($subscriptionid) = @_;
2241 my $dbh = C4
::Context
->dbh;
2242 my $subscription = GetSubscription
($subscriptionid);
2243 my $per = $subscription->{'periodicity'};
2244 my $frequency = C4
::Serials
::Frequency
::GetSubscriptionFrequency
($per);
2245 if ($frequency and $frequency->{unit
}){
2246 my $expirationdate = GetExpirationDate
($subscriptionid);
2247 my ($res) = $dbh->selectrow_array('select max(planneddate) from serial where subscriptionid = ?', undef, $subscriptionid);
2248 my $nextdate = GetNextDate
($subscription, $res);
2249 if(Date
::Calc
::Delta_Days
(
2250 split( /-/, $nextdate ),
2251 split( /-/, $expirationdate )
2255 } elsif ($subscription->{numberlength
}>0) {
2256 return (countissuesfrom
($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength
}-1);
2261 sub in_array
{ # used in next sub down
2262 my ( $val, @elements ) = @_;
2263 foreach my $elem (@elements) {
2264 if ( $val == $elem ) {
2271 =head2 GetSubscriptionsFromBorrower
2273 ($count,@routinglist) = GetSubscriptionsFromBorrower($borrowernumber)
2275 this gets the info from subscriptionroutinglist for each $subscriptionid
2278 a count of the serial subscription routing lists to which a patron belongs,
2279 with the titles of those serial subscriptions as an array. Each element of the array
2280 contains a hash_ref with subscriptionID and title of subscription.
2284 sub GetSubscriptionsFromBorrower
{
2285 my ($borrowernumber) = @_;
2286 my $dbh = C4
::Context
->dbh;
2287 my $sth = $dbh->prepare(
2288 "SELECT subscription.subscriptionid, biblio.title
2290 JOIN biblio ON biblio.biblionumber = subscription.biblionumber
2291 JOIN subscriptionroutinglist USING (subscriptionid)
2292 WHERE subscriptionroutinglist.borrowernumber = ? ORDER BY title ASC
2295 $sth->execute($borrowernumber);
2298 while ( my $line = $sth->fetchrow_hashref ) {
2300 push( @routinglist, $line );
2302 return ( $count, @routinglist );
2306 =head2 GetFictiveIssueNumber
2308 $issueno = GetFictiveIssueNumber($subscription, $publishedate);
2310 Get the position of the issue published at $publisheddate, considering the
2311 first issue (at firstacquidate) is at position 1, the next is at position 2, etc...
2312 This issuenumber doesn't take into account irregularities, so, for instance, if the 3rd
2313 issue is declared as 'irregular' (will be skipped at receipt), the next issue number
2314 will be 4, not 3. It's why it is called 'fictive'. It is NOT a serial seq, and is not
2315 depending on how many rows are in serial table.
2316 The issue number calculation is based on subscription frequency, first acquisition
2317 date, and $publisheddate.
2321 sub GetFictiveIssueNumber
{
2322 my ($subscription, $publisheddate) = @_;
2324 my $frequency = GetSubscriptionFrequency
($subscription->{'periodicity'});
2325 my $unit = $frequency->{unit
} ?
lc $frequency->{'unit'} : undef;
2329 my ($year, $month, $day) = split /-/, $publisheddate;
2330 my ($fa_year, $fa_month, $fa_day) = split /-/, $subscription->{'firstacquidate'};
2334 if($unit eq 'day') {
2335 $delta = Delta_Days
($fa_year, $fa_month, $fa_day, $year, $month, $day);
2336 } elsif($unit eq 'week') {
2337 ($wkno, $year) = Week_of_Year
($year, $month, $day);
2338 my ($fa_wkno, $fa_yr) = Week_of_Year
($fa_year, $fa_month, $fa_day);
2339 $delta = ($fa_yr == $year) ?
($wkno - $fa_wkno) : ( ($year-$fa_yr-1)*52 + (52-$fa_wkno+$wkno) );
2340 } elsif($unit eq 'month') {
2341 $delta = ($fa_year == $year)
2342 ?
($month - $fa_month)
2343 : ( ($year-$fa_year-1)*12 + (12-$fa_month+$month) );
2344 } elsif($unit eq 'year') {
2345 $delta = $year - $fa_year;
2347 if($frequency->{'unitsperissue'} == 1) {
2348 $issueno = $delta * $frequency->{'issuesperunit'} + $subscription->{'countissuesperunit'};
2350 # Assuming issuesperunit == 1
2351 $issueno = int( ($delta + $frequency->{'unitsperissue'}) / $frequency->{'unitsperissue'} );
2359 $resultdate = GetNextDate($publisheddate,$subscription)
2361 this function it takes the publisheddate and will return the next issue's date
2362 and will skip dates if there exists an irregularity.
2363 $publisheddate has to be an ISO date
2364 $subscription is a hashref containing at least 'periodicity', 'firstacquidate', 'irregularity', and 'countissuesperunit'
2365 $updatecount is a boolean value which, when set to true, update the 'countissuesperunit' in database
2366 - eg if periodicity is monthly and $publisheddate is 2007-02-10 but if March and April is to be
2367 skipped then the returned date will be 2007-05-10
2370 $resultdate - then next date in the sequence (ISO date)
2372 Return undef if subscription is irregular
2377 my ( $subscription, $publisheddate, $updatecount ) = @_;
2379 return unless $subscription and $publisheddate;
2381 my $freqdata = GetSubscriptionFrequency
($subscription->{'periodicity'});
2383 if ($freqdata->{'unit'}) {
2384 my ( $year, $month, $day ) = split /-/, $publisheddate;
2386 # Process an irregularity Hash
2387 # Suppose that irregularities are stored in a string with this structure
2388 # irreg1;irreg2;irreg3
2389 # where irregX is the number of issue which will not be received
2390 # (the first issue takes the number 1, the 2nd the number 2 and so on)
2392 if ( $subscription->{irregularity
} ) {
2393 my @irreg = split /;/, $subscription->{'irregularity'} ;
2394 foreach my $irregularity (@irreg) {
2395 $irregularities{$irregularity} = 1;
2399 # Get the 'fictive' next issue number
2400 # It is used to check if next issue is an irregular issue.
2401 my $issueno = GetFictiveIssueNumber
($subscription, $publisheddate) + 1;
2403 # Then get the next date
2404 my $unit = lc $freqdata->{'unit'};
2405 if ($unit eq 'day') {
2406 while ($irregularities{$issueno}) {
2407 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2408 ($year,$month,$day) = Add_Delta_Days
($year,$month, $day , $freqdata->{'unitsperissue'} );
2409 $subscription->{'countissuesperunit'} = 1;
2411 $subscription->{'countissuesperunit'}++;
2415 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2416 ($year,$month,$day) = Add_Delta_Days
($year,$month, $day , $freqdata->{"unitsperissue"} );
2417 $subscription->{'countissuesperunit'} = 1;
2419 $subscription->{'countissuesperunit'}++;
2422 elsif ($unit eq 'week') {
2423 my ($wkno, $yr) = Week_of_Year
($year, $month, $day);
2424 while ($irregularities{$issueno}) {
2425 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2426 $subscription->{'countissuesperunit'} = 1;
2427 $wkno += $freqdata->{"unitsperissue"};
2432 my $dow = Day_of_Week
($year, $month, $day);
2433 ($year,$month,$day) = Monday_of_Week
($wkno, $yr);
2434 if($freqdata->{'issuesperunit'} == 1) {
2435 ($year, $month, $day) = Add_Delta_Days
($year, $month, $day, $dow - 1);
2438 $subscription->{'countissuesperunit'}++;
2442 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2443 $subscription->{'countissuesperunit'} = 1;
2444 $wkno += $freqdata->{"unitsperissue"};
2446 $wkno = $wkno % 52 ;
2449 my $dow = Day_of_Week
($year, $month, $day);
2450 ($year,$month,$day) = Monday_of_Week
($wkno, $yr);
2451 if($freqdata->{'issuesperunit'} == 1) {
2452 ($year, $month, $day) = Add_Delta_Days
($year, $month, $day, $dow - 1);
2455 $subscription->{'countissuesperunit'}++;
2458 elsif ($unit eq 'month') {
2459 while ($irregularities{$issueno}) {
2460 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2461 $subscription->{'countissuesperunit'} = 1;
2462 ($year,$month,$day) = Add_Delta_YM
($year,$month,$day, 0,$freqdata->{"unitsperissue"});
2463 unless($freqdata->{'issuesperunit'} == 1) {
2464 $day = 1; # Jumping to the first day of month, because we don't know what day is expected
2467 $subscription->{'countissuesperunit'}++;
2471 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2472 $subscription->{'countissuesperunit'} = 1;
2473 ($year,$month,$day) = Add_Delta_YM
($year,$month,$day, 0,$freqdata->{"unitsperissue"});
2474 unless($freqdata->{'issuesperunit'} == 1) {
2475 $day = 1; # Jumping to the first day of month, because we don't know what day is expected
2478 $subscription->{'countissuesperunit'}++;
2481 elsif ($unit eq 'year') {
2482 while ($irregularities{$issueno}) {
2483 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2484 $subscription->{'countissuesperunit'} = 1;
2485 ($year,$month,$day) = Add_Delta_YM
($year,$month,$day, $freqdata->{"unitsperissue"},0);
2486 unless($freqdata->{'issuesperunit'} == 1) {
2487 # Jumping to the first day of year, because we don't know what day is expected
2492 $subscription->{'countissuesperunit'}++;
2496 if ($subscription->{'countissuesperunit'} + 1 > $freqdata->{'issuesperunit'}){
2497 $subscription->{'countissuesperunit'} = 1;
2498 ($year,$month,$day) = Add_Delta_YM
($year,$month,$day, $freqdata->{"unitsperissue"},0);
2499 unless($freqdata->{'issuesperunit'} == 1) {
2500 # Jumping to the first day of year, because we don't know what day is expected
2505 $subscription->{'countissuesperunit'}++;
2509 my $dbh = C4
::Context
->dbh;
2512 SET countissuesperunit
= ?
2513 WHERE subscriptionid
= ?
2515 my $sth = $dbh->prepare($query);
2516 $sth->execute($subscription->{'countissuesperunit'}, $subscription->{'subscriptionid'});
2518 return sprintf("%04d-%02d-%02d", $year, $month, $day);
2524 $string = &_numeration($value,$num_type,$locale);
2526 _numeration returns the string corresponding to $value in the num_type
2536 my ($value, $num_type, $locale) = @_;
2541 if ( $num_type =~ /^dayname$/ ) {
2542 # 1970-11-01 was a Sunday
2543 $value = $value % 7;
2544 my $dt = DateTime
->new(
2550 $string = $dt->strftime("%A");
2551 } elsif ( $num_type =~ /^monthname$/ ) {
2552 $value = $value % 12;
2553 my $dt = DateTime
->new(
2555 month
=> $value + 1,
2558 $string = $dt->strftime("%B");
2559 } elsif ( $num_type =~ /^season$/ ) {
2560 my @seasons= qw( Spring Summer Fall Winter );
2561 $value = $value % 4;
2562 $string = $seasons[$value];
2570 =head2 is_barcode_in_use
2572 Returns number of occurence of the barcode in the items table
2573 Can be used as a boolean test of whether the barcode has
2574 been deployed as yet
2578 sub is_barcode_in_use
{
2579 my $barcode = shift;
2580 my $dbh = C4
::Context
->dbh;
2581 my $occurences = $dbh->selectall_arrayref(
2582 'SELECT itemnumber from items where barcode = ?',
2587 return @
{$occurences};
2590 =head2 CloseSubscription
2591 Close a subscription given a subscriptionid
2593 sub CloseSubscription
{
2594 my ( $subscriptionid ) = @_;
2595 return unless $subscriptionid;
2596 my $dbh = C4
::Context
->dbh;
2597 my $sth = $dbh->prepare( qq{
2600 WHERE subscriptionid
= ?
2602 $sth->execute( $subscriptionid );
2604 # Set status = missing when status = stopped
2605 $sth = $dbh->prepare( qq{
2608 WHERE subscriptionid
= ?
2611 $sth->execute( $subscriptionid );
2614 =head2 ReopenSubscription
2615 Reopen a subscription given a subscriptionid
2617 sub ReopenSubscription
{
2618 my ( $subscriptionid ) = @_;
2619 return unless $subscriptionid;
2620 my $dbh = C4
::Context
->dbh;
2621 my $sth = $dbh->prepare( qq{
2624 WHERE subscriptionid
= ?
2626 $sth->execute( $subscriptionid );
2628 # Set status = expected when status = stopped
2629 $sth = $dbh->prepare( qq{
2632 WHERE subscriptionid
= ?
2635 $sth->execute( $subscriptionid );
2638 =head2 subscriptionCurrentlyOnOrder
2640 $bool = subscriptionCurrentlyOnOrder( $subscriptionid );
2642 Return 1 if subscription is currently on order else 0.
2646 sub subscriptionCurrentlyOnOrder
{
2647 my ( $subscriptionid ) = @_;
2648 my $dbh = C4
::Context
->dbh;
2650 SELECT COUNT
(*) FROM aqorders
2651 WHERE subscriptionid
= ?
2652 AND datereceived IS NULL
2653 AND datecancellationprinted IS NULL
2655 my $sth = $dbh->prepare( $query );
2656 $sth->execute($subscriptionid);
2657 return $sth->fetchrow_array;
2660 =head2 can_edit_subscription
2662 $can = can_edit_subscription( $subscriptionid[, $userid] );
2664 Return 1 if the subscription can be edited by the current logged user (or a given $userid), else 0.
2668 sub can_edit_subscription
{
2669 my ( $subscription, $userid ) = @_;
2670 return _can_do_on_subscription
( $subscription, $userid, 'edit_subscription' );
2673 =head2 can_show_subscription
2675 $can = can_show_subscription( $subscriptionid[, $userid] );
2677 Return 1 if the subscription can be shown by the current logged user (or a given $userid), else 0.
2681 sub can_show_subscription
{
2682 my ( $subscription, $userid ) = @_;
2683 return _can_do_on_subscription
( $subscription, $userid, '*' );
2686 sub _can_do_on_subscription
{
2687 my ( $subscription, $userid, $permission ) = @_;
2688 return 0 unless C4
::Context
->userenv;
2689 my $flags = C4
::Context
->userenv->{flags
};
2690 $userid ||= C4
::Context
->userenv->{'id'};
2692 if ( C4
::Context
->preference('IndependentBranches') ) {
2694 if C4
::Context
->IsSuperLibrarian()
2696 C4
::Auth
::haspermission
( $userid, { serials
=> 'superserials' } )
2698 C4
::Auth
::haspermission
( $userid,
2699 { serials
=> $permission } )
2700 and ( not defined $subscription->{branchcode
}
2701 or $subscription->{branchcode
} eq ''
2702 or $subscription->{branchcode
} eq
2703 C4
::Context
->userenv->{'branch'} )
2708 if C4
::Context
->IsSuperLibrarian()
2710 C4
::Auth
::haspermission
( $userid, { serials
=> 'superserials' } )
2711 or C4
::Auth
::haspermission
(
2712 $userid, { serials
=> $permission }
2724 Koha Development Team <http://koha-community.org/>