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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use C4::Auth qw(haspermission);
26 use Date::Calc qw(:all);
27 use POSIX qw(strftime);
29 use C4::Log; # logaction
31 use C4::Serials::Frequency;
32 use C4::Serials::Numberpattern;
33 use Koha::AdditionalField;
37 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
45 MISSING_NEVER_RECIEVED => 41,
46 MISSING_SOLD_OUT => 42,
47 MISSING_DAMAGED => 43,
55 use constant MISSING_STATUSES => (
56 MISSING, MISSING_NEVER_RECIEVED,
57 MISSING_SOLD_OUT, MISSING_DAMAGED,
62 $VERSION = 3.07.00.049; # set version for version checking
66 &NewSubscription &ModSubscription &DelSubscription
67 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
69 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
70 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
71 &GetSubscriptionHistoryFromSubscriptionId
73 &GetNextSeq &GetSeq &NewIssue &GetSerials
74 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
75 &ReNewSubscription &GetLateOrMissingIssues
76 &GetSerialInformation &AddItem2Serial
77 &PrepareSerialsData &GetNextExpected &ModNextExpected
79 &UpdateClaimdateIssues
80 &GetSuppliersWithLateIssues &getsupplierbyserialid
81 &GetDistributedTo &SetDistributedTo
82 &getroutinglist &delroutingmember &addroutingmember
84 &check_routing &updateClaim
87 &GetSubscriptionsFromBorrower
88 &subscriptionCurrentlyOnOrder
95 C4::Serials - Serials Module Functions
103 Functions for handling subscriptions, claims routing etc.
108 =head2 GetSuppliersWithLateIssues
110 $supplierlist = GetSuppliersWithLateIssues()
112 this function get all suppliers with late issues.
115 an array_ref of suppliers each entry is a hash_ref containing id and name
116 the array is in name order
120 sub GetSuppliersWithLateIssues {
121 my $dbh = C4::Context->dbh;
122 my $statuses = join(',', ( LATE, MISSING_STATUSES, CLAIMED ) );
124 SELECT DISTINCT id, name
126 LEFT JOIN serial ON serial.subscriptionid=subscription.subscriptionid
127 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
130 (planneddate < now() AND serial.status=1)
131 OR serial.STATUS IN ( $statuses )
133 AND subscription.closed = 0
135 return $dbh->selectall_arrayref($query, { Slice => {} });
138 =head2 GetSubscriptionHistoryFromSubscriptionId
140 $history = GetSubscriptionHistoryFromSubscriptionId($subscriptionid);
142 This function returns the subscription history as a hashref
146 sub GetSubscriptionHistoryFromSubscriptionId {
147 my ($subscriptionid) = @_;
149 return unless $subscriptionid;
151 my $dbh = C4::Context->dbh;
154 FROM subscriptionhistory
155 WHERE subscriptionid = ?
157 my $sth = $dbh->prepare($query);
158 $sth->execute($subscriptionid);
159 my $results = $sth->fetchrow_hashref;
165 =head2 GetSerialStatusFromSerialId
167 $sth = GetSerialStatusFromSerialId();
168 this function returns a statement handle
169 After this function, don't forget to execute it by using $sth->execute($serialid)
171 $sth = $dbh->prepare($query).
175 sub GetSerialStatusFromSerialId {
176 my $dbh = C4::Context->dbh;
182 return $dbh->prepare($query);
185 =head2 GetSerialInformation
188 $data = GetSerialInformation($serialid);
189 returns a hash_ref containing :
190 items : items marcrecord (can be an array)
192 subscription table field
193 + information about subscription expiration
197 sub GetSerialInformation {
199 my $dbh = C4::Context->dbh;
201 SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid
202 FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid
205 my $rq = $dbh->prepare($query);
206 $rq->execute($serialid);
207 my $data = $rq->fetchrow_hashref;
209 # create item information if we have serialsadditems for this subscription
210 if ( $data->{'serialsadditems'} ) {
211 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
212 $queryitem->execute($serialid);
213 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
215 if ( scalar(@$itemnumbers) > 0 ) {
216 foreach my $itemnum (@$itemnumbers) {
218 #It is ASSUMED that GetMarcItem ALWAYS WORK...
219 #Maybe GetMarcItem should return values on failure
220 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
221 my $itemprocessed = C4::Items::PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0], $data );
222 $itemprocessed->{'itemnumber'} = $itemnum->[0];
223 $itemprocessed->{'itemid'} = $itemnum->[0];
224 $itemprocessed->{'serialid'} = $serialid;
225 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
226 push @{ $data->{'items'} }, $itemprocessed;
229 my $itemprocessed = C4::Items::PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
230 $itemprocessed->{'itemid'} = "N$serialid";
231 $itemprocessed->{'serialid'} = $serialid;
232 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
233 $itemprocessed->{'countitems'} = 0;
234 push @{ $data->{'items'} }, $itemprocessed;
237 $data->{ "status" . $data->{'serstatus'} } = 1;
238 $data->{'subscriptionexpired'} = HasSubscriptionExpired( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
239 $data->{'abouttoexpire'} = abouttoexpire( $data->{'subscriptionid'} );
240 $data->{cannotedit} = not can_edit_subscription( $data );
244 =head2 AddItem2Serial
246 $rows = AddItem2Serial($serialid,$itemnumber);
247 Adds an itemnumber to Serial record
248 returns the number of rows affected
253 my ( $serialid, $itemnumber ) = @_;
255 return unless ($serialid and $itemnumber);
257 my $dbh = C4::Context->dbh;
258 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
259 $rq->execute( $serialid, $itemnumber );
263 =head2 UpdateClaimdateIssues
265 UpdateClaimdateIssues($serialids,[$date]);
267 Update Claimdate for issues in @$serialids list with date $date
272 sub UpdateClaimdateIssues {
273 my ( $serialids, $date ) = @_;
275 return unless ($serialids);
277 my $dbh = C4::Context->dbh;
278 $date = strftime( "%Y-%m-%d", localtime ) unless ($date);
283 claims_count = claims_count + 1
284 WHERE serialid in (" . join( ",", map { '?' } @$serialids ) . ")
286 my $rq = $dbh->prepare($query);
287 $rq->execute($date, CLAIMED, @$serialids);
291 =head2 GetSubscription
293 $subs = GetSubscription($subscriptionid)
294 this function returns the subscription which has $subscriptionid as id.
296 a hashref. This hash containts
297 subscription, subscriptionhistory, aqbooksellers.name, biblio.title
301 sub GetSubscription {
302 my ($subscriptionid) = @_;
303 my $dbh = C4::Context->dbh;
305 SELECT subscription.*,
306 subscriptionhistory.*,
307 aqbooksellers.name AS aqbooksellername,
308 biblio.title AS bibliotitle,
309 subscription.biblionumber as bibnum
311 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
312 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
313 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
314 WHERE subscription.subscriptionid = ?
317 $debug and warn "query : $query\nsubsid :$subscriptionid";
318 my $sth = $dbh->prepare($query);
319 $sth->execute($subscriptionid);
320 my $subscription = $sth->fetchrow_hashref;
322 $subscription->{cannotedit} = not can_edit_subscription( $subscription );
324 # Add additional fields to the subscription into a new key "additional_fields"
325 my $additional_field_values = Koha::AdditionalField->fetch_all_values({
326 tablename => 'subscription',
327 record_id => $subscriptionid,
329 $subscription->{additional_fields} = $additional_field_values->{$subscriptionid};
331 return $subscription;
334 =head2 GetFullSubscription
336 $array_ref = GetFullSubscription($subscriptionid)
337 this function reads the serial table.
341 sub GetFullSubscription {
342 my ($subscriptionid) = @_;
344 return unless ($subscriptionid);
346 my $dbh = C4::Context->dbh;
348 SELECT serial.serialid,
351 serial.publisheddate,
352 serial.publisheddatetext,
354 serial.notes as notes,
355 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
356 aqbooksellers.name as aqbooksellername,
357 biblio.title as bibliotitle,
358 subscription.branchcode AS branchcode,
359 subscription.subscriptionid AS subscriptionid
361 LEFT JOIN subscription ON
362 (serial.subscriptionid=subscription.subscriptionid )
363 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
364 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
365 WHERE serial.subscriptionid = ?
367 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
368 serial.subscriptionid
370 $debug and warn "GetFullSubscription query: $query";
371 my $sth = $dbh->prepare($query);
372 $sth->execute($subscriptionid);
373 my $subscriptions = $sth->fetchall_arrayref( {} );
374 for my $subscription ( @$subscriptions ) {
375 $subscription->{cannotedit} = not can_edit_subscription( $subscription );
377 return $subscriptions;
380 =head2 PrepareSerialsData
382 $array_ref = PrepareSerialsData($serialinfomation)
383 where serialinformation is a hashref array
387 sub PrepareSerialsData {
390 return unless ($lines);
396 my $aqbooksellername;
400 my $previousnote = "";
402 foreach my $subs (@{$lines}) {
403 for my $datefield ( qw(publisheddate planneddate) ) {
404 # handle 0000-00-00 dates
405 if (defined $subs->{$datefield} and $subs->{$datefield} =~ m/^00/) {
406 $subs->{$datefield} = undef;
409 $subs->{ "status" . $subs->{'status'} } = 1;
410 if ( grep { $_ == $subs->{status} } ( EXPECTED, LATE, MISSING_STATUSES, CLAIMED ) ) {
411 $subs->{"checked"} = 1;
414 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
415 $year = $subs->{'year'};
419 if ( $tmpresults{$year} ) {
420 push @{ $tmpresults{$year}->{'serials'} }, $subs;
422 $tmpresults{$year} = {
424 'aqbooksellername' => $subs->{'aqbooksellername'},
425 'bibliotitle' => $subs->{'bibliotitle'},
426 'serials' => [$subs],
431 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
432 push @res, $tmpresults{$key};
437 =head2 GetSubscriptionsFromBiblionumber
439 $array_ref = GetSubscriptionsFromBiblionumber($biblionumber)
440 this function get the subscription list. it reads the subscription table.
442 reference to an array of subscriptions which have the biblionumber given on input arg.
443 each element of this array is a hashref containing
444 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
448 sub GetSubscriptionsFromBiblionumber {
449 my ($biblionumber) = @_;
451 return unless ($biblionumber);
453 my $dbh = C4::Context->dbh;
455 SELECT subscription.*,
457 subscriptionhistory.*,
458 aqbooksellers.name AS aqbooksellername,
459 biblio.title AS bibliotitle
461 LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid
462 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
463 LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber
464 LEFT JOIN branches ON branches.branchcode=subscription.branchcode
465 WHERE subscription.biblionumber = ?
467 my $sth = $dbh->prepare($query);
468 $sth->execute($biblionumber);
470 while ( my $subs = $sth->fetchrow_hashref ) {
471 $subs->{startdate} = output_pref( { dt => dt_from_string( $subs->{startdate} ), dateonly => 1 } );
472 $subs->{histstartdate} = output_pref( { dt => dt_from_string( $subs->{histstartdate} ), dateonly => 1 } );
473 $subs->{histenddate} = output_pref( { dt => dt_from_string( $subs->{histenddate} ), dateonly => 1 } );
474 $subs->{opacnote} =~ s/\n/\<br\/\>/g;
475 $subs->{missinglist} =~ s/\n/\<br\/\>/g;
476 $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
477 $subs->{ "periodicity" . $subs->{periodicity} } = 1;
478 $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
479 $subs->{ "status" . $subs->{'status'} } = 1;
481 if ( $subs->{enddate} eq '0000-00-00' ) {
482 $subs->{enddate} = '';
484 $subs->{enddate} = output_pref( { dt => dt_from_string( $subs->{enddate}), dateonly => 1 } );
486 $subs->{'abouttoexpire'} = abouttoexpire( $subs->{'subscriptionid'} );
487 $subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
488 $subs->{cannotedit} = not can_edit_subscription( $subs );
494 =head2 GetFullSubscriptionsFromBiblionumber
496 $array_ref = GetFullSubscriptionsFromBiblionumber($biblionumber)
497 this function reads the serial table.
501 sub GetFullSubscriptionsFromBiblionumber {
502 my ($biblionumber) = @_;
503 my $dbh = C4::Context->dbh;
505 SELECT serial.serialid,
508 serial.publisheddate,
509 serial.publisheddatetext,
511 serial.notes as notes,
512 year(IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate)) as year,
513 biblio.title as bibliotitle,
514 subscription.branchcode AS branchcode,
515 subscription.subscriptionid AS subscriptionid
517 LEFT JOIN subscription ON
518 (serial.subscriptionid=subscription.subscriptionid)
519 LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id
520 LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber
521 WHERE subscription.biblionumber = ?
523 IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
524 serial.subscriptionid
526 my $sth = $dbh->prepare($query);
527 $sth->execute($biblionumber);
528 my $subscriptions = $sth->fetchall_arrayref( {} );
529 for my $subscription ( @$subscriptions ) {
530 $subscription->{cannotedit} = not can_edit_subscription( $subscription );
532 return $subscriptions;
535 =head2 SearchSubscriptions
537 @results = SearchSubscriptions($args);
539 This function returns a list of hashrefs, one for each subscription
540 that meets the conditions specified by the $args hashref.
542 The valid search fields are:
556 The expiration_date search field is special; it specifies the maximum
557 subscription expiration date.
561 sub SearchSubscriptions {
564 my $additional_fields = $args->{additional_fields} // [];
565 my $matching_record_ids_for_additional_fields = [];
566 if ( @$additional_fields ) {
567 $matching_record_ids_for_additional_fields = Koha::AdditionalField->get_matching_record_ids({
568 fields => $additional_fields,
569 tablename => 'subscription',
572 return () unless @$matching_record_ids_for_additional_fields;
577 subscription.notes AS publicnotes,
578 subscriptionhistory.*,
580 biblio.notes AS biblionotes,
586 LEFT JOIN subscriptionhistory USING(subscriptionid)
587 LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber
588 LEFT JOIN biblioitems ON biblioitems.biblionumber = subscription.biblionumber
589 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
591 $query .= q| WHERE 1|;
594 if( $args->{biblionumber} ) {
595 push @where_strs, "biblio.biblionumber = ?";
596 push @where_args, $args->{biblionumber};
599 if( $args->{title} ){
600 my @words = split / /, $args->{title};
602 foreach my $word (@words) {
603 push @strs, "biblio.title LIKE ?";
604 push @args, "%$word%";
607 push @where_strs, '(' . join (' AND ', @strs) . ')';
608 push @where_args, @args;
612 push @where_strs, "biblioitems.issn LIKE ?";
613 push @where_args, "%$args->{issn}%";
616 push @where_strs, "biblioitems.ean LIKE ?";
617 push @where_args, "%$args->{ean}%";
619 if ( $args->{callnumber} ) {
620 push @where_strs, "subscription.callnumber LIKE ?";
621 push @where_args, "%$args->{callnumber}%";
623 if( $args->{publisher} ){
624 push @where_strs, "biblioitems.publishercode LIKE ?";
625 push @where_args, "%$args->{publisher}%";
627 if( $args->{bookseller} ){
628 push @where_strs, "aqbooksellers.name LIKE ?";
629 push @where_args, "%$args->{bookseller}%";
631 if( $args->{branch} ){
632 push @where_strs, "subscription.branchcode = ?";
633 push @where_args, "$args->{branch}";
635 if ( $args->{location} ) {
636 push @where_strs, "subscription.location = ?";
637 push @where_args, "$args->{location}";
639 if ( $args->{expiration_date} ) {
640 push @where_strs, "subscription.enddate <= ?";
641 push @where_args, "$args->{expiration_date}";
643 if( defined $args->{closed} ){
644 push @where_strs, "subscription.closed = ?";
645 push @where_args, "$args->{closed}";
649 $query .= ' AND ' . join(' AND ', @where_strs);
651 if ( @$additional_fields ) {
652 $query .= ' AND subscriptionid IN ('
653 . join( ', ', @$matching_record_ids_for_additional_fields )
657 $query .= " ORDER BY " . $args->{orderby} if $args->{orderby};
659 my $dbh = C4::Context->dbh;
660 my $sth = $dbh->prepare($query);
661 $sth->execute(@where_args);
662 my $results = $sth->fetchall_arrayref( {} );
664 for my $subscription ( @$results ) {
665 $subscription->{cannotedit} = not can_edit_subscription( $subscription );
666 $subscription->{cannotdisplay} = not can_show_subscription( $subscription );
668 my $additional_field_values = Koha::AdditionalField->fetch_all_values({
669 record_id => $subscription->{subscriptionid},
670 tablename => 'subscription'
672 $subscription->{additional_fields} = $additional_field_values->{$subscription->{subscriptionid}};
681 ($totalissues,@serials) = GetSerials($subscriptionid);
682 this function gets every serial not arrived for a given subscription
683 as well as the number of issues registered in the database (all types)
684 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
686 FIXME: We should return \@serials.
691 my ( $subscriptionid, $count ) = @_;
693 return unless $subscriptionid;
695 my $dbh = C4::Context->dbh;
697 # status = 2 is "arrived"
699 $count = 5 unless ($count);
701 my $statuses = join( ',', ( ARRIVED, MISSING_STATUSES, NOT_ISSUED ) );
702 my $query = "SELECT serialid,serialseq, status, publisheddate,
703 publisheddatetext, planneddate,notes, routingnotes
705 WHERE subscriptionid = ? AND status NOT IN ( $statuses )
706 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
707 my $sth = $dbh->prepare($query);
708 $sth->execute($subscriptionid);
710 while ( my $line = $sth->fetchrow_hashref ) {
711 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
712 for my $datefield ( qw( planneddate publisheddate) ) {
713 if ($line->{$datefield} && $line->{$datefield}!~m/^00/) {
714 $line->{$datefield} = output_pref( { dt => dt_from_string( $line->{$datefield} ), dateonly => 1 } );
716 $line->{$datefield} = q{};
719 push @serials, $line;
722 # OK, now add the last 5 issues arrives/missing
723 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,
724 publisheddatetext, notes, routingnotes
726 WHERE subscriptionid = ?
727 AND status IN ( $statuses )
728 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
730 $sth = $dbh->prepare($query);
731 $sth->execute($subscriptionid);
732 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
734 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
735 for my $datefield ( qw( planneddate publisheddate) ) {
736 if ($line->{$datefield} && $line->{$datefield}!~m/^00/) {
737 $line->{$datefield} = output_pref( { dt => dt_from_string( $line->{$datefield} ), dateonly => 1 } );
739 $line->{$datefield} = q{};
743 push @serials, $line;
746 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
747 $sth = $dbh->prepare($query);
748 $sth->execute($subscriptionid);
749 my ($totalissues) = $sth->fetchrow;
750 return ( $totalissues, @serials );
755 @serials = GetSerials2($subscriptionid,$statuses);
756 this function returns every serial waited for a given subscription
757 as well as the number of issues registered in the database (all types)
758 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
760 $statuses is an arrayref of statuses and is mandatory.
765 my ( $subscription, $statuses ) = @_;
767 return unless ($subscription and @$statuses);
769 my $statuses_string = join ',', @$statuses;
771 my $dbh = C4::Context->dbh;
773 SELECT serialid,serialseq, status, planneddate, publisheddate,
774 publisheddatetext, notes, routingnotes
776 WHERE subscriptionid=$subscription AND status IN ($statuses_string)
777 ORDER BY publisheddate,serialid DESC
779 $debug and warn "GetSerials2 query: $query";
780 my $sth = $dbh->prepare($query);
784 while ( my $line = $sth->fetchrow_hashref ) {
785 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
786 # Format dates for display
787 for my $datefield ( qw( planneddate publisheddate ) ) {
788 if (!defined($line->{$datefield}) || $line->{$datefield} =~m/^00/) {
789 $line->{$datefield} = q{};
792 $line->{$datefield} = output_pref( { dt => dt_from_string( $line->{$datefield} ), dateonly => 1 } );
795 push @serials, $line;
800 =head2 GetLatestSerials
802 \@serials = GetLatestSerials($subscriptionid,$limit)
803 get the $limit's latest serials arrived or missing for a given subscription
805 a ref to an array which contains all of the latest serials stored into a hash.
809 sub GetLatestSerials {
810 my ( $subscriptionid, $limit ) = @_;
812 return unless ($subscriptionid and $limit);
814 my $dbh = C4::Context->dbh;
816 my $statuses = join( ',', ( ARRIVED, MISSING_STATUSES ) );
817 my $strsth = "SELECT serialid,serialseq, status, planneddate, publisheddate, notes
819 WHERE subscriptionid = ?
820 AND status IN ($statuses)
821 ORDER BY publisheddate DESC LIMIT 0,$limit
823 my $sth = $dbh->prepare($strsth);
824 $sth->execute($subscriptionid);
826 while ( my $line = $sth->fetchrow_hashref ) {
827 $line->{ "status" . $line->{status} } = 1; # fills a "statusX" value, used for template status select list
828 $line->{planneddate} = output_pref( { dt => dt_from_string( $line->{planneddate} ), dateonly => 1 } );
829 $line->{publisheddate} = output_pref( { dt => dt_from_string( $line->{publisheddate} ), dateonly => 1 } );
830 push @serials, $line;
836 =head2 GetDistributedTo
838 $distributedto=GetDistributedTo($subscriptionid)
839 This function returns the field distributedto for the subscription matching subscriptionid
843 sub GetDistributedTo {
844 my $dbh = C4::Context->dbh;
846 my ($subscriptionid) = @_;
848 return unless ($subscriptionid);
850 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
851 my $sth = $dbh->prepare($query);
852 $sth->execute($subscriptionid);
853 return ($distributedto) = $sth->fetchrow;
859 $nextseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
860 $newinnerloop1, $newinnerloop2, $newinnerloop3
861 ) = GetNextSeq( $subscription, $pattern, $planneddate );
863 $subscription is a hashref containing all the attributes of the table
865 $pattern is a hashref containing all the attributes of the table
866 'subscription_numberpatterns'.
867 $planneddate is a date string in iso format.
868 This function get the next issue for the subscription given on input arg
873 my ($subscription, $pattern, $planneddate) = @_;
875 return unless ($subscription and $pattern);
877 my ( $newlastvalue1, $newlastvalue2, $newlastvalue3,
878 $newinnerloop1, $newinnerloop2, $newinnerloop3 );
881 if ($subscription->{'skip_serialseq'}) {
882 my @irreg = split /;/, $subscription->{'irregularity'};
884 my $irregularities = {};
885 $irregularities->{$_} = 1 foreach(@irreg);
886 my $issueno = GetFictiveIssueNumber($subscription, $planneddate) + 1;
887 while($irregularities->{$issueno}) {
894 my $numberingmethod = $pattern->{numberingmethod};
896 if ($numberingmethod) {
897 $calculated = $numberingmethod;
898 my $locale = $subscription->{locale};
899 $newlastvalue1 = $subscription->{lastvalue1} || 0;
900 $newlastvalue2 = $subscription->{lastvalue2} || 0;
901 $newlastvalue3 = $subscription->{lastvalue3} || 0;
902 $newinnerloop1 = $subscription->{innerloop1} || 0;
903 $newinnerloop2 = $subscription->{innerloop2} || 0;
904 $newinnerloop3 = $subscription->{innerloop3} || 0;
907 $calc{$_} = 1 if ($numberingmethod =~ /\{$_\}/);
910 for(my $i = 0; $i < $count; $i++) {
912 # check if we have to increase the new value.
914 if ($newinnerloop1 >= $pattern->{every1}) {
916 $newlastvalue1 += $pattern->{add1};
918 # reset counter if needed.
919 $newlastvalue1 = $pattern->{setto1} if ($newlastvalue1 > $pattern->{whenmorethan1});
922 # check if we have to increase the new value.
924 if ($newinnerloop2 >= $pattern->{every2}) {
926 $newlastvalue2 += $pattern->{add2};
928 # reset counter if needed.
929 $newlastvalue2 = $pattern->{setto2} if ($newlastvalue2 > $pattern->{whenmorethan2});
932 # check if we have to increase the new value.
934 if ($newinnerloop3 >= $pattern->{every3}) {
936 $newlastvalue3 += $pattern->{add3};
938 # reset counter if needed.
939 $newlastvalue3 = $pattern->{setto3} if ($newlastvalue3 > $pattern->{whenmorethan3});
943 my $newlastvalue1string = _numeration( $newlastvalue1, $pattern->{numbering1}, $locale );
944 $calculated =~ s/\{X\}/$newlastvalue1string/g;
947 my $newlastvalue2string = _numeration( $newlastvalue2, $pattern->{numbering2}, $locale );
948 $calculated =~ s/\{Y\}/$newlastvalue2string/g;
951 my $newlastvalue3string = _numeration( $newlastvalue3, $pattern->{numbering3}, $locale );
952 $calculated =~ s/\{Z\}/$newlastvalue3string/g;
957 $newlastvalue1, $newlastvalue2, $newlastvalue3,
958 $newinnerloop1, $newinnerloop2, $newinnerloop3);
963 $calculated = GetSeq($subscription, $pattern)
964 $subscription is a hashref containing all the attributes of the table 'subscription'
965 $pattern is a hashref containing all the attributes of the table 'subscription_numberpatterns'
966 this function transforms {X},{Y},{Z} to 150,0,0 for example.
968 the sequence in string format
973 my ($subscription, $pattern) = @_;
975 return unless ($subscription and $pattern);
977 my $locale = $subscription->{locale};
979 my $calculated = $pattern->{numberingmethod};
981 my $newlastvalue1 = $subscription->{'lastvalue1'} || 0;
982 $newlastvalue1 = _numeration($newlastvalue1, $pattern->{numbering1}, $locale) if ($pattern->{numbering1}); # reset counter if needed.
983 $calculated =~ s/\{X\}/$newlastvalue1/g;
985 my $newlastvalue2 = $subscription->{'lastvalue2'} || 0;
986 $newlastvalue2 = _numeration($newlastvalue2, $pattern->{numbering2}, $locale) if ($pattern->{numbering2}); # reset counter if needed.
987 $calculated =~ s/\{Y\}/$newlastvalue2/g;
989 my $newlastvalue3 = $subscription->{'lastvalue3'} || 0;
990 $newlastvalue3 = _numeration($newlastvalue3, $pattern->{numbering3}, $locale) if ($pattern->{numbering3}); # reset counter if needed.
991 $calculated =~ s/\{Z\}/$newlastvalue3/g;
995 =head2 GetExpirationDate
997 $enddate = GetExpirationDate($subscriptionid, [$startdate])
999 this function return the next expiration date for a subscription given on input args.
1002 the enddate or undef
1006 sub GetExpirationDate {
1007 my ( $subscriptionid, $startdate ) = @_;
1009 return unless ($subscriptionid);
1011 my $dbh = C4::Context->dbh;
1012 my $subscription = GetSubscription($subscriptionid);
1015 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1016 $enddate = $startdate || $subscription->{startdate};
1017 my @date = split( /-/, $enddate );
1019 return if ( scalar(@date) != 3 || not check_date(@date) );
1021 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
1022 if ( $frequency and $frequency->{unit} ) {
1025 if ( my $length = $subscription->{numberlength} ) {
1027 #calculate the date of the last issue.
1028 for ( my $i = 1 ; $i <= $length ; $i++ ) {
1029 $enddate = GetNextDate( $subscription, $enddate );
1031 } elsif ( $subscription->{monthlength} ) {
1032 if ( $$subscription{startdate} ) {
1033 my @enddate = Add_Delta_YM( $date[0], $date[1], $date[2], 0, $subscription->{monthlength} );
1034 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1036 } elsif ( $subscription->{weeklength} ) {
1037 if ( $$subscription{startdate} ) {
1038 my @date = split( /-/, $subscription->{startdate} );
1039 my @enddate = Add_Delta_Days( $date[0], $date[1], $date[2], $subscription->{weeklength} * 7 );
1040 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1043 $enddate = $subscription->{enddate};
1047 return $subscription->{enddate};
1051 =head2 CountSubscriptionFromBiblionumber
1053 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1054 this returns a count of the subscriptions for a given biblionumber
1056 the number of subscriptions
1060 sub CountSubscriptionFromBiblionumber {
1061 my ($biblionumber) = @_;
1063 return unless ($biblionumber);
1065 my $dbh = C4::Context->dbh;
1066 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1067 my $sth = $dbh->prepare($query);
1068 $sth->execute($biblionumber);
1069 my $subscriptionsnumber = $sth->fetchrow;
1070 return $subscriptionsnumber;
1073 =head2 ModSubscriptionHistory
1075 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1077 this function modifies the history of a subscription. Put your new values on input arg.
1078 returns the number of rows affected
1082 sub ModSubscriptionHistory {
1083 my ( $subscriptionid, $histstartdate, $enddate, $receivedlist, $missinglist, $opacnote, $librariannote ) = @_;
1085 return unless ($subscriptionid);
1087 my $dbh = C4::Context->dbh;
1088 my $query = "UPDATE subscriptionhistory
1089 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1090 WHERE subscriptionid=?
1092 my $sth = $dbh->prepare($query);
1093 $receivedlist =~ s/^; // if $receivedlist;
1094 $missinglist =~ s/^; // if $missinglist;
1095 $opacnote =~ s/^; // if $opacnote;
1096 $sth->execute( $histstartdate, $enddate, $receivedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
1100 =head2 ModSerialStatus
1102 ModSerialStatus($serialid, $serialseq, $planneddate, $publisheddate,
1103 $publisheddatetext, $status, $notes);
1105 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1106 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1110 sub ModSerialStatus {
1111 my ($serialid, $serialseq, $planneddate, $publisheddate, $publisheddatetext,
1112 $status, $notes) = @_;
1114 return unless ($serialid);
1116 #It is a usual serial
1117 # 1st, get previous status :
1118 my $dbh = C4::Context->dbh;
1119 my $query = "SELECT serial.subscriptionid,serial.status,subscription.periodicity
1120 FROM serial, subscription
1121 WHERE serial.subscriptionid=subscription.subscriptionid
1123 my $sth = $dbh->prepare($query);
1124 $sth->execute($serialid);
1125 my ( $subscriptionid, $oldstatus, $periodicity ) = $sth->fetchrow;
1126 my $frequency = GetSubscriptionFrequency($periodicity);
1128 # change status & update subscriptionhistory
1130 if ( $status == DELETED ) {
1131 DelIssue( { 'serialid' => $serialid, 'subscriptionid' => $subscriptionid, 'serialseq' => $serialseq } );
1136 SET serialseq = ?, publisheddate = ?, publisheddatetext = ?,
1137 planneddate = ?, status = ?, notes = ?
1140 $sth = $dbh->prepare($query);
1141 $sth->execute( $serialseq, $publisheddate, $publisheddatetext,
1142 $planneddate, $status, $notes, $serialid );
1143 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1144 $sth = $dbh->prepare($query);
1145 $sth->execute($subscriptionid);
1146 my $val = $sth->fetchrow_hashref;
1147 unless ( $val->{manualhistory} ) {
1148 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1149 $sth = $dbh->prepare($query);
1150 $sth->execute($subscriptionid);
1151 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1153 if ( $status == ARRIVED || ($oldstatus == ARRIVED && $status != ARRIVED) ) {
1154 $recievedlist .= "; $serialseq"
1155 if ($recievedlist !~ /(^|;)\s*$serialseq(?=;|$)/);
1158 # in case serial has been previously marked as missing
1159 if (grep /$status/, (EXPECTED, ARRIVED, LATE, CLAIMED)) {
1160 $missinglist=~ s/(^|;)\s*$serialseq(?=;|$)//g;
1163 $missinglist .= "; $serialseq"
1164 if ( ( grep { $_ == $status } ( MISSING_STATUSES ) ) && ( $missinglist !~/(^|;)\s*$serialseq(?=;|$)/ ) );
1165 $missinglist .= "; not issued $serialseq"
1166 if ( $status == NOT_ISSUED && $missinglist !~ /(^|;)\s*$serialseq(?=;|$)/ );
1168 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1169 $sth = $dbh->prepare($query);
1170 $recievedlist =~ s/^; //;
1171 $missinglist =~ s/^; //;
1172 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1176 # create new waited entry if needed (ie : was a "waited" and has changed)
1177 if ( $oldstatus == EXPECTED && $status != EXPECTED ) {
1178 my $subscription = GetSubscription($subscriptionid);
1179 my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
1183 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
1184 $newinnerloop1, $newinnerloop2, $newinnerloop3
1186 = GetNextSeq( $subscription, $pattern, $publisheddate );
1188 # next date (calculated from actual date & frequency parameters)
1189 my $nextpublisheddate = GetNextDate($subscription, $publisheddate, 1);
1190 my $nextpubdate = $nextpublisheddate;
1191 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1192 WHERE subscriptionid = ?";
1193 $sth = $dbh->prepare($query);
1194 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1196 NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'}, 1, $nextpubdate, $nextpubdate );
1198 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1199 if ( $subscription->{letter} && $status == ARRIVED && $oldstatus != ARRIVED ) {
1200 require C4::Letters;
1201 C4::Letters::SendAlerts( 'issue', $serialid, $subscription->{letter} );
1208 =head2 GetNextExpected
1210 $nextexpected = GetNextExpected($subscriptionid)
1212 Get the planneddate for the current expected issue of the subscription.
1218 planneddate => ISO date
1223 sub GetNextExpected {
1224 my ($subscriptionid) = @_;
1226 my $dbh = C4::Context->dbh;
1230 WHERE subscriptionid = ?
1234 my $sth = $dbh->prepare($query);
1236 # Each subscription has only one 'expected' issue.
1237 $sth->execute( $subscriptionid, EXPECTED );
1238 my $nextissue = $sth->fetchrow_hashref;
1239 if ( !$nextissue ) {
1243 WHERE subscriptionid = ?
1244 ORDER BY publisheddate DESC
1247 $sth = $dbh->prepare($query);
1248 $sth->execute($subscriptionid);
1249 $nextissue = $sth->fetchrow_hashref;
1251 foreach(qw/planneddate publisheddate/) {
1252 if ( !defined $nextissue->{$_} ) {
1253 # or should this default to 1st Jan ???
1254 $nextissue->{$_} = strftime( '%Y-%m-%d', localtime );
1256 $nextissue->{$_} = ($nextissue->{$_} ne '0000-00-00')
1264 =head2 ModNextExpected
1266 ModNextExpected($subscriptionid,$date)
1268 Update the planneddate for the current expected issue of the subscription.
1269 This will modify all future prediction results.
1271 C<$date> is an ISO date.
1277 sub ModNextExpected {
1278 my ( $subscriptionid, $date ) = @_;
1279 my $dbh = C4::Context->dbh;
1281 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1282 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1284 # Each subscription has only one 'expected' issue.
1285 $sth->execute( $date, $date, $subscriptionid, EXPECTED );
1290 =head2 GetSubscriptionIrregularities
1294 =item @irreg = &GetSubscriptionIrregularities($subscriptionid);
1295 get the list of irregularities for a subscription
1301 sub GetSubscriptionIrregularities {
1302 my $subscriptionid = shift;
1304 return unless $subscriptionid;
1306 my $dbh = C4::Context->dbh;
1310 WHERE subscriptionid = ?
1312 my $sth = $dbh->prepare($query);
1313 $sth->execute($subscriptionid);
1315 my ($result) = $sth->fetchrow_array;
1316 my @irreg = split /;/, $result;
1321 =head2 ModSubscription
1323 this function modifies a subscription. Put all new values on input args.
1324 returns the number of rows affected
1328 sub ModSubscription {
1330 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
1331 $periodicity, $firstacquidate, $irregularity, $numberpattern, $locale,
1332 $numberlength, $weeklength, $monthlength, $lastvalue1, $innerloop1,
1333 $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, $status,
1334 $biblionumber, $callnumber, $notes, $letter, $manualhistory,
1335 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1336 $graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq
1339 my $dbh = C4::Context->dbh;
1340 my $query = "UPDATE subscription
1341 SET librarian=?, branchcode=?, aqbooksellerid=?, cost=?, aqbudgetid=?,
1342 startdate=?, periodicity=?, firstacquidate=?, irregularity=?,
1343 numberpattern=?, locale=?, numberlength=?, weeklength=?, monthlength=?,
1344 lastvalue1=?, innerloop1=?, lastvalue2=?, innerloop2=?,
1345 lastvalue3=?, innerloop3=?, status=?, biblionumber=?,
1346 callnumber=?, notes=?, letter=?, manualhistory=?,
1347 internalnotes=?, serialsadditems=?, staffdisplaycount=?,
1348 opacdisplaycount=?, graceperiod=?, location = ?, enddate=?,
1350 WHERE subscriptionid = ?";
1352 my $sth = $dbh->prepare($query);
1354 $auser, $branchcode, $aqbooksellerid, $cost,
1355 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1356 $irregularity, $numberpattern, $locale, $numberlength,
1357 $weeklength, $monthlength, $lastvalue1, $innerloop1,
1358 $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
1359 $status, $biblionumber, $callnumber, $notes,
1360 $letter, ($manualhistory ? $manualhistory : 0),
1361 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1362 $graceperiod, $location, $enddate, $skip_serialseq,
1365 my $rows = $sth->rows;
1367 logaction( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1371 =head2 NewSubscription
1373 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1374 $startdate,$periodicity,$numberlength,$weeklength,$monthlength,
1375 $lastvalue1,$innerloop1,$lastvalue2,$innerloop2,$lastvalue3,$innerloop3,
1376 $status, $notes, $letter, $firstacquidate, $irregularity, $numberpattern,
1377 $locale, $callnumber, $manualhistory, $internalnotes, $serialsadditems,
1378 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $skip_serialseq);
1380 Create a new subscription with value given on input args.
1383 the id of this new subscription
1387 sub NewSubscription {
1389 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
1390 $startdate, $periodicity, $numberlength, $weeklength, $monthlength,
1391 $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3,
1392 $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity,
1393 $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes,
1394 $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod,
1395 $location, $enddate, $skip_serialseq
1397 my $dbh = C4::Context->dbh;
1399 #save subscription (insert into database)
1401 INSERT INTO subscription
1402 (librarian, branchcode, aqbooksellerid, cost, aqbudgetid,
1403 biblionumber, startdate, periodicity, numberlength, weeklength,
1404 monthlength, lastvalue1, innerloop1, lastvalue2, innerloop2,
1405 lastvalue3, innerloop3, status, notes, letter, firstacquidate,
1406 irregularity, numberpattern, locale, callnumber,
1407 manualhistory, internalnotes, serialsadditems, staffdisplaycount,
1408 opacdisplaycount, graceperiod, location, enddate, skip_serialseq)
1409 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
1411 my $sth = $dbh->prepare($query);
1413 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
1414 $startdate, $periodicity, $numberlength, $weeklength,
1415 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
1416 $lastvalue3, $innerloop3, $status, $notes, $letter,
1417 $firstacquidate, $irregularity, $numberpattern, $locale, $callnumber,
1418 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
1419 $opacdisplaycount, $graceperiod, $location, $enddate, $skip_serialseq
1422 my $subscriptionid = $dbh->{'mysql_insertid'};
1424 $enddate = GetExpirationDate( $subscriptionid, $startdate );
1428 WHERE subscriptionid=?
1430 $sth = $dbh->prepare($query);
1431 $sth->execute( $enddate, $subscriptionid );
1434 # then create the 1st expected number
1436 INSERT INTO subscriptionhistory
1437 (biblionumber, subscriptionid, histstartdate)
1440 $sth = $dbh->prepare($query);
1441 $sth->execute( $biblionumber, $subscriptionid, $startdate);
1443 # reread subscription to get a hash (for calculation of the 1st issue number)
1444 my $subscription = GetSubscription($subscriptionid);
1445 my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
1447 # calculate issue number
1448 my $serialseq = GetSeq($subscription, $pattern) || q{};
1450 my $serial_rs = Koha::Database->new()->schema()->resultset('Serial');
1453 serialseq => $serialseq,
1454 serialseq_x => $subscription->{'lastvalue1'},
1455 serialseq_y => $subscription->{'lastvalue2'},
1456 serialseq_z => $subscription->{'lastvalue3'},
1457 subscriptionid => $subscriptionid,
1458 biblionumber => $biblionumber,
1460 planneddate => $firstacquidate,
1461 publisheddate => $firstacquidate,
1465 logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1467 #set serial flag on biblio if not already set.
1468 my $bib = GetBiblio($biblionumber);
1469 if ( $bib and !$bib->{'serial'} ) {
1470 my $record = GetMarcBiblio($biblionumber);
1471 my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $bib->{'frameworkcode'} );
1473 eval { $record->field($tag)->update( $subf => 1 ); };
1475 ModBiblio( $record, $biblionumber, $bib->{'frameworkcode'} );
1477 return $subscriptionid;
1480 =head2 ReNewSubscription
1482 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1484 this function renew a subscription with values given on input args.
1488 sub ReNewSubscription {
1489 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1490 my $dbh = C4::Context->dbh;
1491 my $subscription = GetSubscription($subscriptionid);
1495 LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber
1496 WHERE biblio.biblionumber=?
1498 my $sth = $dbh->prepare($query);
1499 $sth->execute( $subscription->{biblionumber} );
1500 my $biblio = $sth->fetchrow_hashref;
1502 if ( C4::Context->preference("RenewSerialAddsSuggestion") ) {
1503 require C4::Suggestions;
1504 C4::Suggestions::NewSuggestion(
1505 { 'suggestedby' => $user,
1506 'title' => $subscription->{bibliotitle},
1507 'author' => $biblio->{author},
1508 'publishercode' => $biblio->{publishercode},
1509 'note' => $biblio->{note},
1510 'biblionumber' => $subscription->{biblionumber}
1515 # renew subscription
1518 SET startdate=?,numberlength=?,weeklength=?,monthlength=?,reneweddate=NOW()
1519 WHERE subscriptionid=?
1521 $sth = $dbh->prepare($query);
1522 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1523 my $enddate = GetExpirationDate($subscriptionid);
1524 $debug && warn "enddate :$enddate";
1528 WHERE subscriptionid=?
1530 $sth = $dbh->prepare($query);
1531 $sth->execute( $enddate, $subscriptionid );
1533 UPDATE subscriptionhistory
1535 WHERE subscriptionid=?
1537 $sth = $dbh->prepare($query);
1538 $sth->execute( $enddate, $subscriptionid );
1540 logaction( "SERIAL", "RENEW", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1546 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1548 Create a new issue stored on the database.
1549 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1550 returns the serial id
1555 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate,
1556 $publisheddate, $publisheddatetext, $notes ) = @_;
1557 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1559 return unless ($subscriptionid);
1561 my $schema = Koha::Database->new()->schema();
1563 my $subscription = $schema->resultset('Subscription')->find( $subscriptionid );
1565 my $serial = $schema->resultset('Serial')->create(
1567 serialseq => $serialseq,
1568 serialseq_x => $subscription->lastvalue1(),
1569 serialseq_y => $subscription->lastvalue2(),
1570 serialseq_z => $subscription->lastvalue3(),
1571 subscriptionid => $subscriptionid,
1572 biblionumber => $biblionumber,
1574 planneddate => $planneddate,
1575 publisheddate => $publisheddate,
1576 publisheddatetext => $publisheddatetext,
1581 my $serialid = $serial->id();
1583 my $subscription_history = $schema->resultset('Subscriptionhistory')->find($subscriptionid);
1584 my $missinglist = $subscription_history->missinglist();
1585 my $recievedlist = $subscription_history->recievedlist();
1587 if ( $status == ARRIVED ) {
1588 ### TODO Add a feature that improves recognition and description.
1589 ### As such count (serialseq) i.e. : N18,2(N19),N20
1590 ### Would use substr and index But be careful to previous presence of ()
1591 $recievedlist .= "; $serialseq" unless ( index( $recievedlist, $serialseq ) > 0 );
1593 if ( grep { /^$status$/ } (MISSING_STATUSES) ) {
1594 $missinglist .= "; $serialseq" unless ( index( $missinglist, $serialseq ) > 0 );
1597 $recievedlist =~ s/^; //;
1598 $missinglist =~ s/^; //;
1600 $subscription_history->recievedlist($recievedlist);
1601 $subscription_history->missinglist($missinglist);
1602 $subscription_history->update();
1607 =head2 HasSubscriptionStrictlyExpired
1609 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1611 the subscription has stricly expired when today > the end subscription date
1614 1 if true, 0 if false, -1 if the expiration date is not set.
1618 sub HasSubscriptionStrictlyExpired {
1620 # Getting end of subscription date
1621 my ($subscriptionid) = @_;
1623 return unless ($subscriptionid);
1625 my $dbh = C4::Context->dbh;
1626 my $subscription = GetSubscription($subscriptionid);
1627 my $expirationdate = $subscription->{enddate} || GetExpirationDate($subscriptionid);
1629 # If the expiration date is set
1630 if ( $expirationdate != 0 ) {
1631 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1633 # Getting today's date
1634 my ( $nowyear, $nowmonth, $nowday ) = Today();
1636 # if today's date > expiration date, then the subscription has stricly expired
1637 if ( Delta_Days( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1644 # There are some cases where the expiration date is not set
1645 # As we can't determine if the subscription has expired on a date-basis,
1651 =head2 HasSubscriptionExpired
1653 $has_expired = HasSubscriptionExpired($subscriptionid)
1655 the subscription has expired when the next issue to arrive is out of subscription limit.
1658 0 if the subscription has not expired
1659 1 if the subscription has expired
1660 2 if has subscription does not have a valid expiration date set
1664 sub HasSubscriptionExpired {
1665 my ($subscriptionid) = @_;
1667 return unless ($subscriptionid);
1669 my $dbh = C4::Context->dbh;
1670 my $subscription = GetSubscription($subscriptionid);
1671 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
1672 if ( $frequency and $frequency->{unit} ) {
1673 my $expirationdate = $subscription->{enddate} || GetExpirationDate($subscriptionid);
1674 if (!defined $expirationdate) {
1675 $expirationdate = q{};
1678 SELECT max(planneddate)
1680 WHERE subscriptionid=?
1682 my $sth = $dbh->prepare($query);
1683 $sth->execute($subscriptionid);
1684 my ($res) = $sth->fetchrow;
1685 if (!$res || $res=~m/^0000/) {
1688 my @res = split( /-/, $res );
1689 my @endofsubscriptiondate = split( /-/, $expirationdate );
1690 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date(@res) || not check_date(@endofsubscriptiondate) );
1692 if ( ( @endofsubscriptiondate && Delta_Days( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1697 if ( $subscription->{'numberlength'} ) {
1698 my $countreceived = countissuesfrom( $subscriptionid, $subscription->{'startdate'} );
1699 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1705 return 0; # Notice that you'll never get here.
1708 =head2 SetDistributedto
1710 SetDistributedto($distributedto,$subscriptionid);
1711 This function update the value of distributedto for a subscription given on input arg.
1715 sub SetDistributedto {
1716 my ( $distributedto, $subscriptionid ) = @_;
1717 my $dbh = C4::Context->dbh;
1721 WHERE subscriptionid=?
1723 my $sth = $dbh->prepare($query);
1724 $sth->execute( $distributedto, $subscriptionid );
1728 =head2 DelSubscription
1730 DelSubscription($subscriptionid)
1731 this function deletes subscription which has $subscriptionid as id.
1735 sub DelSubscription {
1736 my ($subscriptionid) = @_;
1737 my $dbh = C4::Context->dbh;
1738 $dbh->do("DELETE FROM subscription WHERE subscriptionid=?", undef, $subscriptionid);
1739 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=?", undef, $subscriptionid);
1740 $dbh->do("DELETE FROM serial WHERE subscriptionid=?", undef, $subscriptionid);
1742 my $afs = Koha::AdditionalField->all({tablename => 'subscription'});
1743 foreach my $af (@$afs) {
1744 $af->delete_values({record_id => $subscriptionid});
1747 logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
1752 DelIssue($serialseq,$subscriptionid)
1753 this function deletes an issue which has $serialseq and $subscriptionid given on input arg.
1755 returns the number of rows affected
1760 my ($dataissue) = @_;
1761 my $dbh = C4::Context->dbh;
1762 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1767 AND subscriptionid= ?
1769 my $mainsth = $dbh->prepare($query);
1770 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1772 #Delete element from subscription history
1773 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1774 my $sth = $dbh->prepare($query);
1775 $sth->execute( $dataissue->{'subscriptionid'} );
1776 my $val = $sth->fetchrow_hashref;
1777 unless ( $val->{manualhistory} ) {
1779 SELECT * FROM subscriptionhistory
1780 WHERE subscriptionid= ?
1782 my $sth = $dbh->prepare($query);
1783 $sth->execute( $dataissue->{'subscriptionid'} );
1784 my $data = $sth->fetchrow_hashref;
1785 my $serialseq = $dataissue->{'serialseq'};
1786 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1787 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1788 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1789 $sth = $dbh->prepare($strsth);
1790 $sth->execute( $dataissue->{'subscriptionid'} );
1793 return $mainsth->rows;
1796 =head2 GetLateOrMissingIssues
1798 @issuelist = GetLateMissingIssues($supplierid,$serialid)
1800 this function selects missing issues on database - where serial.status = MISSING* or serial.status = LATE or planneddate<now
1803 the issuelist as an array of hash refs. Each element of this array contains
1804 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1808 sub GetLateOrMissingIssues {
1809 my ( $supplierid, $serialid, $order ) = @_;
1811 return unless ( $supplierid or $serialid );
1813 my $dbh = C4::Context->dbh;
1818 $byserial = "and serialid = " . $serialid;
1821 $order .= ", title";
1825 my $missing_statuses_string = join ',', (MISSING_STATUSES);
1827 $sth = $dbh->prepare(
1829 serialid, aqbooksellerid, name,
1830 biblio.title, biblioitems.issn, planneddate, serialseq,
1831 serial.status, serial.subscriptionid, claimdate, claims_count,
1832 subscription.branchcode
1834 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1835 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1836 LEFT JOIN biblioitems ON subscription.biblionumber=biblioitems.biblionumber
1837 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1838 WHERE subscription.subscriptionid = serial.subscriptionid
1839 AND (serial.STATUS IN ($missing_statuses_string) OR ((planneddate < now() AND serial.STATUS = ?) OR serial.STATUS = ? OR serial.STATUS = ?))
1840 AND subscription.aqbooksellerid=$supplierid
1845 $sth = $dbh->prepare(
1847 serialid, aqbooksellerid, name,
1848 biblio.title, planneddate, serialseq,
1849 serial.status, serial.subscriptionid, claimdate, claims_count,
1850 subscription.branchcode
1852 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1853 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1854 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1855 WHERE subscription.subscriptionid = serial.subscriptionid
1856 AND (serial.STATUS IN ($missing_statuses_string) OR ((planneddate < now() AND serial.STATUS = ?) OR serial.STATUS = ? OR serial.STATUS = ?))
1861 $sth->execute( EXPECTED, LATE, CLAIMED );
1863 while ( my $line = $sth->fetchrow_hashref ) {
1865 if ($line->{planneddate} && $line->{planneddate} !~/^0+\-/) {
1866 $line->{planneddateISO} = $line->{planneddate};
1867 $line->{planneddate} = output_pref( { dt => dt_from_string( $line->{"planneddate"} ), dateonly => 1 } );
1869 if ($line->{claimdate} && $line->{claimdate} !~/^0+\-/) {
1870 $line->{claimdateISO} = $line->{claimdate};
1871 $line->{claimdate} = output_pref( { dt => dt_from_string( $line->{"claimdate"} ), dateonly => 1 } );
1873 $line->{"status".$line->{status}} = 1;
1875 my $additional_field_values = Koha::AdditionalField->fetch_all_values({
1876 record_id => $line->{subscriptionid},
1877 tablename => 'subscription'
1879 %$line = ( %$line, additional_fields => $additional_field_values->{$line->{subscriptionid}} );
1881 push @issuelist, $line;
1888 &updateClaim($serialid)
1890 this function updates the time when a claim is issued for late/missing items
1892 called from claims.pl file
1897 my ($serialid) = @_;
1898 my $dbh = C4::Context->dbh;
1901 SET claimdate = NOW(),
1902 claims_count = claims_count + 1
1908 =head2 getsupplierbyserialid
1910 $result = getsupplierbyserialid($serialid)
1912 this function is used to find the supplier id given a serial id
1915 hashref containing serialid, subscriptionid, and aqbooksellerid
1919 sub getsupplierbyserialid {
1920 my ($serialid) = @_;
1921 my $dbh = C4::Context->dbh;
1922 my $sth = $dbh->prepare(
1923 "SELECT serialid, serial.subscriptionid, aqbooksellerid
1925 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
1929 $sth->execute($serialid);
1930 my $line = $sth->fetchrow_hashref;
1931 my $result = $line->{'aqbooksellerid'};
1935 =head2 check_routing
1937 $result = &check_routing($subscriptionid)
1939 this function checks to see if a serial has a routing list and returns the count of routingid
1940 used to show either an 'add' or 'edit' link
1945 my ($subscriptionid) = @_;
1947 return unless ($subscriptionid);
1949 my $dbh = C4::Context->dbh;
1950 my $sth = $dbh->prepare(
1951 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
1952 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
1953 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
1956 $sth->execute($subscriptionid);
1957 my $line = $sth->fetchrow_hashref;
1958 my $result = $line->{'routingids'};
1962 =head2 addroutingmember
1964 addroutingmember($borrowernumber,$subscriptionid)
1966 this function takes a borrowernumber and subscriptionid and adds the member to the
1967 routing list for that serial subscription and gives them a rank on the list
1968 of either 1 or highest current rank + 1
1972 sub addroutingmember {
1973 my ( $borrowernumber, $subscriptionid ) = @_;
1975 return unless ($borrowernumber and $subscriptionid);
1978 my $dbh = C4::Context->dbh;
1979 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
1980 $sth->execute($subscriptionid);
1981 while ( my $line = $sth->fetchrow_hashref ) {
1982 if ( $line->{'rank'} > 0 ) {
1983 $rank = $line->{'rank'} + 1;
1988 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
1989 $sth->execute( $subscriptionid, $borrowernumber, $rank );
1992 =head2 reorder_members
1994 reorder_members($subscriptionid,$routingid,$rank)
1996 this function is used to reorder the routing list
1998 it takes the routingid of the member one wants to re-rank and the rank it is to move to
1999 - it gets all members on list puts their routingid's into an array
2000 - removes the one in the array that is $routingid
2001 - then reinjects $routingid at point indicated by $rank
2002 - then update the database with the routingids in the new order
2006 sub reorder_members {
2007 my ( $subscriptionid, $routingid, $rank ) = @_;
2008 my $dbh = C4::Context->dbh;
2009 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
2010 $sth->execute($subscriptionid);
2012 while ( my $line = $sth->fetchrow_hashref ) {
2013 push( @result, $line->{'routingid'} );
2016 # To find the matching index
2018 my $key = -1; # to allow for 0 being a valid response
2019 for ( $i = 0 ; $i < @result ; $i++ ) {
2020 if ( $routingid == $result[$i] ) {
2021 $key = $i; # save the index
2026 # if index exists in array then move it to new position
2027 if ( $key > -1 && $rank > 0 ) {
2028 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
2029 my $moving_item = splice( @result, $key, 1 );
2030 splice( @result, $new_rank, 0, $moving_item );
2032 for ( my $j = 0 ; $j < @result ; $j++ ) {
2033 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
2039 =head2 delroutingmember
2041 delroutingmember($routingid,$subscriptionid)
2043 this function either deletes one member from routing list if $routingid exists otherwise
2044 deletes all members from the routing list
2048 sub delroutingmember {
2050 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2051 my ( $routingid, $subscriptionid ) = @_;
2052 my $dbh = C4::Context->dbh;
2054 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2055 $sth->execute($routingid);
2056 reorder_members( $subscriptionid, $routingid );
2058 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2059 $sth->execute($subscriptionid);
2064 =head2 getroutinglist
2066 @routinglist = getroutinglist($subscriptionid)
2068 this gets the info from the subscriptionroutinglist for $subscriptionid
2071 the routinglist as an array. Each element of the array contains a hash_ref containing
2072 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2076 sub getroutinglist {
2077 my ($subscriptionid) = @_;
2078 my $dbh = C4::Context->dbh;
2079 my $sth = $dbh->prepare(
2080 'SELECT routingid, borrowernumber, ranking, biblionumber
2082 JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2083 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC'
2085 $sth->execute($subscriptionid);
2086 my $routinglist = $sth->fetchall_arrayref({});
2087 return @{$routinglist};
2090 =head2 countissuesfrom
2092 $result = countissuesfrom($subscriptionid,$startdate)
2094 Returns a count of serial rows matching the given subsctiptionid
2095 with published date greater than startdate
2099 sub countissuesfrom {
2100 my ( $subscriptionid, $startdate ) = @_;
2101 my $dbh = C4::Context->dbh;
2105 WHERE subscriptionid=?
2106 AND serial.publisheddate>?
2108 my $sth = $dbh->prepare($query);
2109 $sth->execute( $subscriptionid, $startdate );
2110 my ($countreceived) = $sth->fetchrow;
2111 return $countreceived;
2116 $result = CountIssues($subscriptionid)
2118 Returns a count of serial rows matching the given subsctiptionid
2123 my ($subscriptionid) = @_;
2124 my $dbh = C4::Context->dbh;
2128 WHERE subscriptionid=?
2130 my $sth = $dbh->prepare($query);
2131 $sth->execute($subscriptionid);
2132 my ($countreceived) = $sth->fetchrow;
2133 return $countreceived;
2138 $result = HasItems($subscriptionid)
2140 returns a count of items from serial matching the subscriptionid
2145 my ($subscriptionid) = @_;
2146 my $dbh = C4::Context->dbh;
2148 SELECT COUNT(serialitems.itemnumber)
2150 LEFT JOIN serialitems USING(serialid)
2151 WHERE subscriptionid=? AND serialitems.serialid IS NOT NULL
2153 my $sth=$dbh->prepare($query);
2154 $sth->execute($subscriptionid);
2155 my ($countitems)=$sth->fetchrow_array();
2159 =head2 abouttoexpire
2161 $result = abouttoexpire($subscriptionid)
2163 this function alerts you to the penultimate issue for a serial subscription
2165 returns 1 - if this is the penultimate issue
2171 my ($subscriptionid) = @_;
2172 my $dbh = C4::Context->dbh;
2173 my $subscription = GetSubscription($subscriptionid);
2174 my $per = $subscription->{'periodicity'};
2175 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($per);
2176 if ($frequency and $frequency->{unit}){
2178 my $expirationdate = GetExpirationDate($subscriptionid);
2180 my ($res) = $dbh->selectrow_array('select max(planneddate) from serial where subscriptionid = ?', undef, $subscriptionid);
2181 my $nextdate = GetNextDate($subscription, $res);
2183 # only compare dates if both dates exist.
2184 if ($nextdate and $expirationdate) {
2185 if(Date::Calc::Delta_Days(
2186 split( /-/, $nextdate ),
2187 split( /-/, $expirationdate )
2193 } elsif ($subscription->{numberlength}>0) {
2194 return (countissuesfrom($subscriptionid,$subscription->{'startdate'}) >=$subscription->{numberlength}-1);
2200 sub in_array { # used in next sub down
2201 my ( $val, @elements ) = @_;
2202 foreach my $elem (@elements) {
2203 if ( $val == $elem ) {
2210 =head2 GetSubscriptionsFromBorrower
2212 ($count,@routinglist) = GetSubscriptionsFromBorrower($borrowernumber)
2214 this gets the info from subscriptionroutinglist for each $subscriptionid
2217 a count of the serial subscription routing lists to which a patron belongs,
2218 with the titles of those serial subscriptions as an array. Each element of the array
2219 contains a hash_ref with subscriptionID and title of subscription.
2223 sub GetSubscriptionsFromBorrower {
2224 my ($borrowernumber) = @_;
2225 my $dbh = C4::Context->dbh;
2226 my $sth = $dbh->prepare(
2227 "SELECT subscription.subscriptionid, biblio.title
2229 JOIN biblio ON biblio.biblionumber = subscription.biblionumber
2230 JOIN subscriptionroutinglist USING (subscriptionid)
2231 WHERE subscriptionroutinglist.borrowernumber = ? ORDER BY title ASC
2234 $sth->execute($borrowernumber);
2237 while ( my $line = $sth->fetchrow_hashref ) {
2239 push( @routinglist, $line );
2241 return ( $count, @routinglist );
2245 =head2 GetFictiveIssueNumber
2247 $issueno = GetFictiveIssueNumber($subscription, $publishedate);
2249 Get the position of the issue published at $publisheddate, considering the
2250 first issue (at firstacquidate) is at position 1, the next is at position 2, etc...
2251 This issuenumber doesn't take into account irregularities, so, for instance, if the 3rd
2252 issue is declared as 'irregular' (will be skipped at receipt), the next issue number
2253 will be 4, not 3. It's why it is called 'fictive'. It is NOT a serial seq, and is not
2254 depending on how many rows are in serial table.
2255 The issue number calculation is based on subscription frequency, first acquisition
2256 date, and $publisheddate.
2260 sub GetFictiveIssueNumber {
2261 my ($subscription, $publisheddate) = @_;
2263 my $frequency = GetSubscriptionFrequency($subscription->{'periodicity'});
2264 my $unit = $frequency->{unit} ? lc $frequency->{'unit'} : undef;
2268 my ($year, $month, $day) = split /-/, $publisheddate;
2269 my ($fa_year, $fa_month, $fa_day) = split /-/, $subscription->{'firstacquidate'};
2273 if($unit eq 'day') {
2274 $delta = Delta_Days($fa_year, $fa_month, $fa_day, $year, $month, $day);
2275 } elsif($unit eq 'week') {
2276 ($wkno, $year) = Week_of_Year($year, $month, $day);
2277 my ($fa_wkno, $fa_yr) = Week_of_Year($fa_year, $fa_month, $fa_day);
2278 $delta = ($fa_yr == $year) ? ($wkno - $fa_wkno) : ( ($year-$fa_yr-1)*52 + (52-$fa_wkno+$wkno) );
2279 } elsif($unit eq 'month') {
2280 $delta = ($fa_year == $year)
2281 ? ($month - $fa_month)
2282 : ( ($year-$fa_year-1)*12 + (12-$fa_month+$month) );
2283 } elsif($unit eq 'year') {
2284 $delta = $year - $fa_year;
2286 if($frequency->{'unitsperissue'} == 1) {
2287 $issueno = $delta * $frequency->{'issuesperunit'} + $subscription->{'countissuesperunit'};
2289 # Assuming issuesperunit == 1
2290 $issueno = int( ($delta + $frequency->{'unitsperissue'}) / $frequency->{'unitsperissue'} );
2296 sub _get_next_date_day {
2297 my ($subscription, $freqdata, $year, $month, $day) = @_;
2299 if ($subscription->{countissuesperunit} + 1 > $freqdata->{issuesperunit}){
2300 ($year,$month,$day) = Add_Delta_Days($year,$month, $day , $freqdata->{unitsperissue} );
2301 $subscription->{countissuesperunit} = 1;
2303 $subscription->{countissuesperunit}++;
2306 return ($year, $month, $day);
2309 sub _get_next_date_week {
2310 my ($subscription, $freqdata, $year, $month, $day) = @_;
2312 my ($wkno, $yr) = Week_of_Year($year, $month, $day);
2313 my $fa_dow = Day_of_Week(split /-/, $subscription->{firstacquidate});
2315 if ($subscription->{countissuesperunit} + 1 > $freqdata->{issuesperunit}){
2316 $subscription->{countissuesperunit} = 1;
2317 $wkno += $freqdata->{unitsperissue};
2322 ($year,$month,$day) = Monday_of_Week($wkno, $yr);
2323 ($year,$month,$day) = Add_Delta_Days($year, $month, $day, $fa_dow - 1);
2325 # Try to guess the next day of week
2326 my $delta_days = int((7 - ($fa_dow - 1)) / $freqdata->{issuesperunit});
2327 ($year,$month,$day) = Add_Delta_Days($year, $month, $day, $delta_days);
2328 $subscription->{countissuesperunit}++;
2331 return ($year, $month, $day);
2334 sub _get_next_date_month {
2335 my ($subscription, $freqdata, $year, $month, $day) = @_;
2338 (undef, undef, $fa_day) = split /-/, $subscription->{firstacquidate};
2340 if ($subscription->{countissuesperunit} + 1 > $freqdata->{issuesperunit}){
2341 $subscription->{countissuesperunit} = 1;
2342 ($year,$month,$day) = Add_Delta_YM($year,$month,$day, 0,
2343 $freqdata->{unitsperissue});
2344 my $days_in_month = Days_in_Month($year, $month);
2345 $day = $fa_day <= $days_in_month ? $fa_day : $days_in_month;
2347 # Try to guess the next day in month
2348 my $days_in_month = Days_in_Month($year, $month);
2349 my $delta_days = int(($days_in_month - ($fa_day - 1)) / $freqdata->{issuesperunit});
2350 ($year,$month,$day) = Add_Delta_Days($year, $month, $day, $delta_days);
2351 $subscription->{countissuesperunit}++;
2354 return ($year, $month, $day);
2357 sub _get_next_date_year {
2358 my ($subscription, $freqdata, $year, $month, $day) = @_;
2360 my ($fa_year, $fa_month, $fa_day) = split /-/, $subscription->{firstacquidate};
2362 if ($subscription->{countissuesperunit} + 1 > $freqdata->{issuesperunit}){
2363 $subscription->{countissuesperunit} = 1;
2364 ($year) = Add_Delta_YM($year,$month,$day, $freqdata->{"unitsperissue"},0);
2366 my $days_in_month = Days_in_Month($year, $month);
2367 $day = $fa_day <= $days_in_month ? $fa_day : $days_in_month;
2369 # Try to guess the next day in year
2370 my $days_in_year = Days_in_Year($year,12); #Sum the days of all the months of this year
2371 my $delta_days = int(($days_in_year - ($fa_day - 1)) / $freqdata->{issuesperunit});
2372 ($year,$month,$day) = Add_Delta_Days($year, $month, $day, $delta_days);
2373 $subscription->{countissuesperunit}++;
2376 return ($year, $month, $day);
2381 $resultdate = GetNextDate($publisheddate,$subscription)
2383 this function it takes the publisheddate and will return the next issue's date
2384 and will skip dates if there exists an irregularity.
2385 $publisheddate has to be an ISO date
2386 $subscription is a hashref containing at least 'periodicity', 'firstacquidate', 'irregularity', and 'countissuesperunit'
2387 $updatecount is a boolean value which, when set to true, update the 'countissuesperunit' in database
2388 - eg if periodicity is monthly and $publisheddate is 2007-02-10 but if March and April is to be
2389 skipped then the returned date will be 2007-05-10
2392 $resultdate - then next date in the sequence (ISO date)
2394 Return undef if subscription is irregular
2399 my ( $subscription, $publisheddate, $updatecount ) = @_;
2401 return unless $subscription and $publisheddate;
2403 my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
2405 if ($freqdata->{'unit'}) {
2406 my ( $year, $month, $day ) = split /-/, $publisheddate;
2408 # Process an irregularity Hash
2409 # Suppose that irregularities are stored in a string with this structure
2410 # irreg1;irreg2;irreg3
2411 # where irregX is the number of issue which will not be received
2412 # (the first issue takes the number 1, the 2nd the number 2 and so on)
2414 if ( $subscription->{irregularity} ) {
2415 my @irreg = split /;/, $subscription->{'irregularity'} ;
2416 foreach my $irregularity (@irreg) {
2417 $irregularities{$irregularity} = 1;
2421 # Get the 'fictive' next issue number
2422 # It is used to check if next issue is an irregular issue.
2423 my $issueno = GetFictiveIssueNumber($subscription, $publisheddate) + 1;
2425 # Then get the next date
2426 my $unit = lc $freqdata->{'unit'};
2427 if ($unit eq 'day') {
2428 while ($irregularities{$issueno}) {
2429 ($year, $month, $day) = _get_next_date_day($subscription,
2430 $freqdata, $year, $month, $day);
2433 ($year, $month, $day) = _get_next_date_day($subscription, $freqdata,
2434 $year, $month, $day);
2436 elsif ($unit eq 'week') {
2437 while ($irregularities{$issueno}) {
2438 ($year, $month, $day) = _get_next_date_week($subscription,
2439 $freqdata, $year, $month, $day);
2442 ($year, $month, $day) = _get_next_date_week($subscription,
2443 $freqdata, $year, $month, $day);
2445 elsif ($unit eq 'month') {
2446 while ($irregularities{$issueno}) {
2447 ($year, $month, $day) = _get_next_date_month($subscription,
2448 $freqdata, $year, $month, $day);
2451 ($year, $month, $day) = _get_next_date_month($subscription,
2452 $freqdata, $year, $month, $day);
2454 elsif ($unit eq 'year') {
2455 while ($irregularities{$issueno}) {
2456 ($year, $month, $day) = _get_next_date_year($subscription,
2457 $freqdata, $year, $month, $day);
2460 ($year, $month, $day) = _get_next_date_year($subscription,
2461 $freqdata, $year, $month, $day);
2465 my $dbh = C4::Context->dbh;
2468 SET countissuesperunit = ?
2469 WHERE subscriptionid = ?
2471 my $sth = $dbh->prepare($query);
2472 $sth->execute($subscription->{'countissuesperunit'}, $subscription->{'subscriptionid'});
2475 return sprintf("%04d-%02d-%02d", $year, $month, $day);
2481 $string = &_numeration($value,$num_type,$locale);
2483 _numeration returns the string corresponding to $value in the num_type
2493 my ($value, $num_type, $locale) = @_;
2498 if ( $num_type =~ /^dayname$/ ) {
2499 # 1970-11-01 was a Sunday
2500 $value = $value % 7;
2501 my $dt = DateTime->new(
2507 $string = $dt->strftime("%A");
2508 } elsif ( $num_type =~ /^monthname$/ ) {
2509 $value = $value % 12;
2510 my $dt = DateTime->new(
2512 month => $value + 1,
2515 $string = $dt->strftime("%B");
2516 } elsif ( $num_type =~ /^season$/ ) {
2517 my @seasons= qw( Spring Summer Fall Winter );
2518 $value = $value % 4;
2519 $string = $seasons[$value];
2527 =head2 is_barcode_in_use
2529 Returns number of occurrences of the barcode in the items table
2530 Can be used as a boolean test of whether the barcode has
2531 been deployed as yet
2535 sub is_barcode_in_use {
2536 my $barcode = shift;
2537 my $dbh = C4::Context->dbh;
2538 my $occurrences = $dbh->selectall_arrayref(
2539 'SELECT itemnumber from items where barcode = ?',
2544 return @{$occurrences};
2547 =head2 CloseSubscription
2548 Close a subscription given a subscriptionid
2550 sub CloseSubscription {
2551 my ( $subscriptionid ) = @_;
2552 return unless $subscriptionid;
2553 my $dbh = C4::Context->dbh;
2554 my $sth = $dbh->prepare( q{
2557 WHERE subscriptionid = ?
2559 $sth->execute( $subscriptionid );
2561 # Set status = missing when status = stopped
2562 $sth = $dbh->prepare( q{
2565 WHERE subscriptionid = ?
2568 $sth->execute( STOPPED, $subscriptionid, EXPECTED );
2571 =head2 ReopenSubscription
2572 Reopen a subscription given a subscriptionid
2574 sub ReopenSubscription {
2575 my ( $subscriptionid ) = @_;
2576 return unless $subscriptionid;
2577 my $dbh = C4::Context->dbh;
2578 my $sth = $dbh->prepare( q{
2581 WHERE subscriptionid = ?
2583 $sth->execute( $subscriptionid );
2585 # Set status = expected when status = stopped
2586 $sth = $dbh->prepare( q{
2589 WHERE subscriptionid = ?
2592 $sth->execute( EXPECTED, $subscriptionid, STOPPED );
2595 =head2 subscriptionCurrentlyOnOrder
2597 $bool = subscriptionCurrentlyOnOrder( $subscriptionid );
2599 Return 1 if subscription is currently on order else 0.
2603 sub subscriptionCurrentlyOnOrder {
2604 my ( $subscriptionid ) = @_;
2605 my $dbh = C4::Context->dbh;
2607 SELECT COUNT(*) FROM aqorders
2608 WHERE subscriptionid = ?
2609 AND datereceived IS NULL
2610 AND datecancellationprinted IS NULL
2612 my $sth = $dbh->prepare( $query );
2613 $sth->execute($subscriptionid);
2614 return $sth->fetchrow_array;
2617 =head2 can_claim_subscription
2619 $can = can_claim_subscription( $subscriptionid[, $userid] );
2621 Return 1 if the subscription can be claimed by the current logged user (or a given $userid), else 0.
2625 sub can_claim_subscription {
2626 my ( $subscription, $userid ) = @_;
2627 return _can_do_on_subscription( $subscription, $userid, 'claim_serials' );
2630 =head2 can_edit_subscription
2632 $can = can_edit_subscription( $subscriptionid[, $userid] );
2634 Return 1 if the subscription can be edited by the current logged user (or a given $userid), else 0.
2638 sub can_edit_subscription {
2639 my ( $subscription, $userid ) = @_;
2640 return _can_do_on_subscription( $subscription, $userid, 'edit_subscription' );
2643 =head2 can_show_subscription
2645 $can = can_show_subscription( $subscriptionid[, $userid] );
2647 Return 1 if the subscription can be shown by the current logged user (or a given $userid), else 0.
2651 sub can_show_subscription {
2652 my ( $subscription, $userid ) = @_;
2653 return _can_do_on_subscription( $subscription, $userid, '*' );
2656 sub _can_do_on_subscription {
2657 my ( $subscription, $userid, $permission ) = @_;
2658 return 0 unless C4::Context->userenv;
2659 my $flags = C4::Context->userenv->{flags};
2660 $userid ||= C4::Context->userenv->{'id'};
2662 if ( C4::Context->preference('IndependentBranches') ) {
2664 if C4::Context->IsSuperLibrarian()
2666 C4::Auth::haspermission( $userid, { serials => 'superserials' } )
2668 C4::Auth::haspermission( $userid,
2669 { serials => $permission } )
2670 and ( not defined $subscription->{branchcode}
2671 or $subscription->{branchcode} eq ''
2672 or $subscription->{branchcode} eq
2673 C4::Context->userenv->{'branch'} )
2678 if C4::Context->IsSuperLibrarian()
2680 C4::Auth::haspermission( $userid, { serials => 'superserials' } )
2681 or C4::Auth::haspermission(
2682 $userid, { serials => $permission }
2694 Koha Development Team <http://koha-community.org/>