1 package C4
::Serials
; #assumes C4/Serials.pm
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
21 use C4
::Dates
qw(format_date);
22 use Date
::Calc
qw(:all);
23 use POSIX
qw(strftime);
31 use C4
::Log
; # logaction
34 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37 $VERSION = 3.01; # set version for version checking
41 &NewSubscription &ModSubscription &DelSubscription &GetSubscriptions
42 &GetSubscription &CountSubscriptionFromBiblionumber &GetSubscriptionsFromBiblionumber
43 &GetFullSubscriptionsFromBiblionumber &GetFullSubscription &ModSubscriptionHistory
44 &HasSubscriptionStrictlyExpired &HasSubscriptionExpired &GetExpirationDate &abouttoexpire
46 &GetNextSeq &NewIssue &ItemizeSerials &GetSerials
47 &GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
48 &ReNewSubscription &GetLateIssues &GetLateOrMissingIssues
49 &GetSerialInformation &AddItem2Serial
50 &PrepareSerialsData &GetNextExpected &ModNextExpected
52 &UpdateClaimdateIssues
53 &GetSuppliersWithLateIssues &getsupplierbyserialid
54 &GetDistributedTo &SetDistributedTo
55 &getroutinglist &delroutingmember &addroutingmember
57 &check_routing &updateClaim &removeMissingIssue
64 =head2 GetSuppliersWithLateIssues
68 C4::Serials - Give functions for serializing.
76 Give all XYZ functions
82 %supplierlist = &GetSuppliersWithLateIssues
84 this function get all suppliers with late issues.
87 the supplierlist into a hash. this hash containts id & name of the supplier
88 Only valid suppliers are returned. Late subscriptions lacking a supplier are
95 sub GetSuppliersWithLateIssues
{
96 my $dbh = C4
::Context
->dbh;
98 SELECT DISTINCT id
, name
100 LEFT JOIN serial ON serial
.subscriptionid
=subscription
.subscriptionid
101 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
= aqbooksellers
.id
102 WHERE subscription
.subscriptionid
= serial
.subscriptionid
103 AND
(planneddate
< now
() OR serial
.STATUS
= 3 OR serial
.STATUS
= 4)
106 my $sth = $dbh->prepare($query);
109 while ( my ( $id, $name ) = $sth->fetchrow ) {
110 next if !defined $id;
111 $supplierlist{$id} = $name;
113 return %supplierlist;
120 @issuelist = &GetLateIssues($supplierid)
122 this function select late issues on database
125 the issuelist into an table. Each line of this table containts a ref to a hash which it containts
126 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
133 my ($supplierid) = @_;
134 my $dbh = C4
::Context
->dbh;
138 SELECT name
,title
,planneddate
,serialseq
,serial
.subscriptionid
140 LEFT JOIN serial ON subscription
.subscriptionid
= serial
.subscriptionid
141 LEFT JOIN biblio ON biblio
.biblionumber
= subscription
.biblionumber
142 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
= aqbooksellers
.id
143 WHERE
((planneddate
< now
() AND serial
.STATUS
=1) OR serial
.STATUS
= 3)
144 AND subscription
.aqbooksellerid
=$supplierid
147 $sth = $dbh->prepare($query);
150 SELECT name
,title
,planneddate
,serialseq
,serial
.subscriptionid
152 LEFT JOIN serial ON subscription
.subscriptionid
= serial
.subscriptionid
153 LEFT JOIN biblio ON biblio
.biblionumber
= subscription
.biblionumber
154 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
= aqbooksellers
.id
155 WHERE
((planneddate
< now
() AND serial
.STATUS
=1) OR serial
.STATUS
= 3)
158 $sth = $dbh->prepare($query);
165 while ( my $line = $sth->fetchrow_hashref ) {
166 $odd++ unless $line->{title
} eq $last_title;
167 $line->{title
} = "" if $line->{title
} eq $last_title;
168 $last_title = $line->{title
} if ( $line->{title
} );
169 $line->{planneddate
} = format_date
( $line->{planneddate
} );
171 push @issuelist, $line;
173 return $count, @issuelist;
176 =head2 GetSubscriptionHistoryFromSubscriptionId
180 $sth = GetSubscriptionHistoryFromSubscriptionId()
181 this function just prepare the SQL request.
182 After this function, don't forget to execute it by using $sth->execute($subscriptionid)
184 $sth = $dbh->prepare($query).
190 sub GetSubscriptionHistoryFromSubscriptionId
() {
191 my $dbh = C4
::Context
->dbh;
194 FROM subscriptionhistory
195 WHERE subscriptionid
= ?
197 return $dbh->prepare($query);
200 =head2 GetSerialStatusFromSerialId
204 $sth = GetSerialStatusFromSerialId();
205 this function just prepare the SQL request.
206 After this function, don't forget to execute it by using $sth->execute($serialid)
208 $sth = $dbh->prepare($query).
214 sub GetSerialStatusFromSerialId
() {
215 my $dbh = C4
::Context
->dbh;
221 return $dbh->prepare($query);
224 =head2 GetSerialInformation
228 $data = GetSerialInformation($serialid);
229 returns a hash containing :
230 items : items marcrecord (can be an array)
232 subscription table field
233 + information about subscription expiration
239 sub GetSerialInformation
{
241 my $dbh = C4
::Context
->dbh;
243 SELECT serial
.*, serial
.notes as sernotes
, serial
.status as serstatus
,subscription
.*,subscription
.subscriptionid as subsid
|;
244 if ( C4
::Context
->preference('IndependantBranches')
245 && C4
::Context
->userenv
246 && C4
::Context
->userenv->{'flags'} != 1
247 && C4
::Context
->userenv->{'branch'} ) {
249 , ((subscription.branchcode <>\"" . C4
::Context
->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
252 FROM serial LEFT JOIN subscription ON subscription
.subscriptionid
=serial
.subscriptionid
255 my $rq = $dbh->prepare($query);
256 $rq->execute($serialid);
257 my $data = $rq->fetchrow_hashref;
259 # create item information if we have serialsadditems for this subscription
260 if ( $data->{'serialsadditems'} ) {
261 my $queryitem = $dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
262 $queryitem->execute($serialid);
263 my $itemnumbers = $queryitem->fetchall_arrayref( [0] );
264 if ( scalar(@
$itemnumbers) > 0 ) {
265 foreach my $itemnum (@
$itemnumbers) {
267 #It is ASSUMED that GetMarcItem ALWAYS WORK...
268 #Maybe GetMarcItem should return values on failure
269 $debug and warn "itemnumber :$itemnum->[0], bibnum :" . $data->{'biblionumber'};
270 my $itemprocessed = PrepareItemrecordDisplay
( $data->{'biblionumber'}, $itemnum->[0], $data );
271 $itemprocessed->{'itemnumber'} = $itemnum->[0];
272 $itemprocessed->{'itemid'} = $itemnum->[0];
273 $itemprocessed->{'serialid'} = $serialid;
274 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
275 push @
{ $data->{'items'} }, $itemprocessed;
278 my $itemprocessed = PrepareItemrecordDisplay
( $data->{'biblionumber'}, '', $data );
279 $itemprocessed->{'itemid'} = "N$serialid";
280 $itemprocessed->{'serialid'} = $serialid;
281 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
282 $itemprocessed->{'countitems'} = 0;
283 push @
{ $data->{'items'} }, $itemprocessed;
286 $data->{ "status" . $data->{'serstatus'} } = 1;
287 $data->{'subscriptionexpired'} = HasSubscriptionExpired
( $data->{'subscriptionid'} ) && $data->{'status'} == 1;
288 $data->{'abouttoexpire'} = abouttoexpire
( $data->{'subscriptionid'} );
292 =head2 AddItem2Serial
296 $data = AddItem2Serial($serialid,$itemnumber);
297 Adds an itemnumber to Serial record
304 my ( $serialid, $itemnumber ) = @_;
305 my $dbh = C4
::Context
->dbh;
306 my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
307 $rq->execute( $serialid, $itemnumber );
311 =head2 UpdateClaimdateIssues
315 UpdateClaimdateIssues($serialids,[$date]);
317 Update Claimdate for issues in @$serialids list with date $date
324 sub UpdateClaimdateIssues
{
325 my ( $serialids, $date ) = @_;
326 my $dbh = C4
::Context
->dbh;
327 $date = strftime
( "%Y-%m-%d", localtime ) unless ($date);
329 UPDATE serial SET claimdate=$date,status=7
330 WHERE serialid in (" . join( ",", @
$serialids ) . ")";
331 my $rq = $dbh->prepare($query);
336 =head2 GetSubscription
340 $subs = GetSubscription($subscriptionid)
341 this function get the subscription which has $subscriptionid as id.
343 a hashref. This hash containts
344 subscription, subscriptionhistory, aqbudget.bookfundid, biblio.title
350 sub GetSubscription
{
351 my ($subscriptionid) = @_;
352 my $dbh = C4
::Context
->dbh;
354 SELECT subscription
.*,
355 subscriptionhistory
.*,
356 aqbooksellers
.name AS aqbooksellername
,
357 biblio
.title AS bibliotitle
,
358 subscription
.biblionumber as bibnum
);
359 if ( C4
::Context
->preference('IndependantBranches')
360 && C4
::Context
->userenv
361 && C4
::Context
->userenv->{'flags'} != 1
362 && C4
::Context
->userenv->{'branch'} ) {
364 , ((subscription.branchcode <>\"" . C4
::Context
->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
368 LEFT JOIN subscriptionhistory ON subscription
.subscriptionid
=subscriptionhistory
.subscriptionid
369 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
370 LEFT JOIN biblio ON biblio
.biblionumber
=subscription
.biblionumber
371 WHERE subscription
.subscriptionid
= ?
374 # if (C4::Context->preference('IndependantBranches') &&
375 # C4::Context->userenv &&
376 # C4::Context->userenv->{'flags'} != 1){
377 # # $debug and warn "flags: ".C4::Context->userenv->{'flags'};
378 # $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
380 $debug and warn "query : $query\nsubsid :$subscriptionid";
381 my $sth = $dbh->prepare($query);
382 $sth->execute($subscriptionid);
383 return $sth->fetchrow_hashref;
386 =head2 GetFullSubscription
390 \@res = GetFullSubscription($subscriptionid)
391 this function read on serial table.
397 sub GetFullSubscription
{
398 my ($subscriptionid) = @_;
399 my $dbh = C4
::Context
->dbh;
401 SELECT serial
.serialid
,
404 serial
.publisheddate
,
406 serial
.notes as notes
,
407 year
(IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
)) as year
,
408 aqbooksellers
.name as aqbooksellername
,
409 biblio
.title as bibliotitle
,
410 subscription
.branchcode AS branchcode
,
411 subscription
.subscriptionid AS subscriptionid
|;
412 if ( C4
::Context
->preference('IndependantBranches')
413 && C4
::Context
->userenv
414 && C4
::Context
->userenv->{'flags'} != 1
415 && C4
::Context
->userenv->{'branch'} ) {
417 , ((subscription.branchcode <>\"" . C4
::Context
->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
421 LEFT JOIN subscription ON
422 (serial
.subscriptionid
=subscription
.subscriptionid
)
423 LEFT JOIN aqbooksellers on subscription
.aqbooksellerid
=aqbooksellers
.id
424 LEFT JOIN biblio on biblio
.biblionumber
=subscription
.biblionumber
425 WHERE serial
.subscriptionid
= ?
427 IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
) DESC
,
428 serial
.subscriptionid
430 $debug and warn "GetFullSubscription query: $query";
431 my $sth = $dbh->prepare($query);
432 $sth->execute($subscriptionid);
433 return $sth->fetchall_arrayref( {} );
436 =head2 PrepareSerialsData
440 \@res = PrepareSerialsData($serialinfomation)
441 where serialinformation is a hashref array
447 sub PrepareSerialsData
{
453 my $aqbooksellername;
457 my $previousnote = "";
459 foreach my $subs (@
$lines) {
460 $subs->{'publisheddate'} = (
461 $subs->{'publisheddate'}
462 ? format_date
( $subs->{'publisheddate'} )
465 $subs->{'branchname'} = GetBranchName
( $subs->{'branchcode'} );
466 $subs->{'planneddate'} = format_date
( $subs->{'planneddate'} );
467 $subs->{ "status" . $subs->{'status'} } = 1;
468 $subs->{"checked"} = $subs->{'status'} =~ /1|3|4|7/;
470 if ( $subs->{'year'} && $subs->{'year'} ne "" ) {
471 $year = $subs->{'year'};
475 if ( $tmpresults{$year} ) {
476 push @
{ $tmpresults{$year}->{'serials'} }, $subs;
478 $tmpresults{$year} = {
480 'aqbooksellername' => $subs->{'aqbooksellername'},
481 'bibliotitle' => $subs->{'bibliotitle'},
482 'serials' => [$subs],
487 foreach my $key ( sort { $b cmp $a } keys %tmpresults ) {
488 push @res, $tmpresults{$key};
490 $res[0]->{'first'} = 1;
494 =head2 GetSubscriptionsFromBiblionumber
496 \@res = GetSubscriptionsFromBiblionumber($biblionumber)
497 this function get the subscription list. it reads on subscription table.
499 table of subscription which has the biblionumber given on input arg.
500 each line of this table is a hashref. All hashes containt
501 startdate, histstartdate,opacnote,missinglist,recievedlist,periodicity,status & enddate
505 sub GetSubscriptionsFromBiblionumber
{
506 my ($biblionumber) = @_;
507 my $dbh = C4
::Context
->dbh;
509 SELECT subscription
.*,
511 subscriptionhistory
.*,
512 aqbooksellers
.name AS aqbooksellername
,
513 biblio
.title AS bibliotitle
515 LEFT JOIN subscriptionhistory ON subscription
.subscriptionid
=subscriptionhistory
.subscriptionid
516 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
517 LEFT JOIN biblio ON biblio
.biblionumber
=subscription
.biblionumber
518 LEFT JOIN branches ON branches
.branchcode
=subscription
.branchcode
519 WHERE subscription
.biblionumber
= ?
521 my $sth = $dbh->prepare($query);
522 $sth->execute($biblionumber);
524 while ( my $subs = $sth->fetchrow_hashref ) {
525 $subs->{startdate
} = format_date
( $subs->{startdate
} );
526 $subs->{histstartdate
} = format_date
( $subs->{histstartdate
} );
527 $subs->{histenddate
} = format_date
( $subs->{histenddate
} );
528 $subs->{opacnote
} =~ s/\n/\<br\/\
>/g
;
529 $subs->{missinglist
} =~ s/\n/\<br\/\
>/g
;
530 $subs->{recievedlist
} =~ s/\n/\<br\/\
>/g
;
531 $subs->{ "periodicity" . $subs->{periodicity
} } = 1;
532 $subs->{ "numberpattern" . $subs->{numberpattern
} } = 1;
533 $subs->{ "status" . $subs->{'status'} } = 1;
534 $subs->{'cannotedit'} =
535 ( C4
::Context
->preference('IndependantBranches')
536 && C4
::Context
->userenv
537 && C4
::Context
->userenv->{flags
} % 2 != 1
538 && C4
::Context
->userenv->{branch
}
539 && $subs->{branchcode
}
540 && ( C4
::Context
->userenv->{branch
} ne $subs->{branchcode
} ) );
542 if ( $subs->{enddate
} eq '0000-00-00' ) {
543 $subs->{enddate
} = '';
545 $subs->{enddate
} = format_date
( $subs->{enddate
} );
547 $subs->{'abouttoexpire'} = abouttoexpire
( $subs->{'subscriptionid'} );
548 $subs->{'subscriptionexpired'} = HasSubscriptionExpired
( $subs->{'subscriptionid'} );
554 =head2 GetFullSubscriptionsFromBiblionumber
558 \@res = GetFullSubscriptionsFromBiblionumber($biblionumber)
559 this function read on serial table.
565 sub GetFullSubscriptionsFromBiblionumber
{
566 my ($biblionumber) = @_;
567 my $dbh = C4
::Context
->dbh;
569 SELECT serial
.serialid
,
572 serial
.publisheddate
,
574 serial
.notes as notes
,
575 year
(IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
)) as year
,
576 biblio
.title as bibliotitle
,
577 subscription
.branchcode AS branchcode
,
578 subscription
.subscriptionid AS subscriptionid
|;
579 if ( C4
::Context
->preference('IndependantBranches')
580 && C4
::Context
->userenv
581 && C4
::Context
->userenv->{'flags'} != 1
582 && C4
::Context
->userenv->{'branch'} ) {
584 , ((subscription.branchcode <>\"" . C4
::Context
->userenv->{'branch'} . "\") and subscription.branchcode <>\"\" and subscription.branchcode IS NOT NULL) as cannotedit ";
589 LEFT JOIN subscription ON
590 (serial
.subscriptionid
=subscription
.subscriptionid
)
591 LEFT JOIN aqbooksellers on subscription
.aqbooksellerid
=aqbooksellers
.id
592 LEFT JOIN biblio on biblio
.biblionumber
=subscription
.biblionumber
593 WHERE subscription
.biblionumber
= ?
595 IF
(serial
.publisheddate
="00-00-0000",serial
.planneddate
,serial
.publisheddate
) DESC
,
596 serial
.subscriptionid
598 my $sth = $dbh->prepare($query);
599 $sth->execute($biblionumber);
600 return $sth->fetchall_arrayref( {} );
603 =head2 GetSubscriptions
607 @results = GetSubscriptions($title,$ISSN,$biblionumber);
608 this function get all subscriptions which has title like $title,ISSN like $ISSN and biblionumber like $biblionumber.
610 a table of hashref. Each hash containt the subscription.
616 sub GetSubscriptions
{
617 my ( $string, $issn, $biblionumber ) = @_;
619 #return unless $title or $ISSN or $biblionumber;
620 my $dbh = C4
::Context
->dbh;
623 SELECT subscription
.*, subscriptionhistory
.*, biblio
.title
,biblioitems
.issn
,biblio
.biblionumber
625 LEFT JOIN subscriptionhistory USING
(subscriptionid
)
626 LEFT JOIN biblio ON biblio
.biblionumber
= subscription
.biblionumber
627 LEFT JOIN biblioitems ON biblio
.biblionumber
= biblioitems
.biblionumber
632 $sqlwhere = " WHERE biblio.biblionumber=?";
633 push @bind_params, $biblionumber;
637 my @strings_to_search;
638 @strings_to_search = map { "%$_%" } split( / /, $string );
639 foreach my $index qw(biblio.title subscription.callnumber subscription.location subscription.notes subscription.internalnotes) {
640 push @bind_params, @strings_to_search;
641 my $tmpstring = "AND $index LIKE ? " x
scalar(@strings_to_search);
642 $debug && warn "$tmpstring";
643 $tmpstring =~ s/^AND //;
644 push @sqlstrings, $tmpstring;
646 $sqlwhere .= ( $sqlwhere ?
" AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
650 my @strings_to_search;
651 @strings_to_search = map { "%$_%" } split( / /, $issn );
652 foreach my $index qw(biblioitems.issn subscription.callnumber) {
653 push @bind_params, @strings_to_search;
654 my $tmpstring = "OR $index LIKE ? " x
scalar(@strings_to_search);
655 $debug && warn "$tmpstring";
656 $tmpstring =~ s/^OR //;
657 push @sqlstrings, $tmpstring;
659 $sqlwhere .= ( $sqlwhere ?
" AND " : " WHERE " ) . "(" . join( ") OR (", @sqlstrings ) . ")";
661 $sql .= "$sqlwhere ORDER BY title";
662 $debug and warn "GetSubscriptions query: $sql params : ", join( " ", @bind_params );
663 $sth = $dbh->prepare($sql);
664 $sth->execute(@bind_params);
666 my $previoustitle = "";
669 while ( my $line = $sth->fetchrow_hashref ) {
670 if ( $previoustitle eq $line->{title
} ) {
674 $previoustitle = $line->{title
};
677 $line->{toggle
} = 1 if $odd == 1;
678 $line->{'cannotedit'} =
679 ( C4
::Context
->preference('IndependantBranches')
680 && C4
::Context
->userenv
681 && C4
::Context
->userenv->{flags
} % 2 != 1
682 && C4
::Context
->userenv->{branch
}
683 && $line->{branchcode
}
684 && ( C4
::Context
->userenv->{branch
} ne $line->{branchcode
} ) );
685 push @results, $line;
694 ($totalissues,@serials) = GetSerials($subscriptionid);
695 this function get every serial not arrived for a given subscription
696 as well as the number of issues registered in the database (all types)
697 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
699 FIXME: We should return \@serials.
706 my ( $subscriptionid, $count ) = @_;
707 my $dbh = C4
::Context
->dbh;
709 # status = 2 is "arrived"
711 $count = 5 unless ($count);
713 my $query = "SELECT serialid,serialseq, status, publisheddate, planneddate,notes, routingnotes
715 WHERE subscriptionid = ? AND status NOT IN (2,4,5)
716 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC";
717 my $sth = $dbh->prepare($query);
718 $sth->execute($subscriptionid);
720 while ( my $line = $sth->fetchrow_hashref ) {
721 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
722 $line->{"publisheddate"} = format_date
( $line->{"publisheddate"} );
723 $line->{"planneddate"} = format_date
( $line->{"planneddate"} );
724 push @serials, $line;
727 # OK, now add the last 5 issues arrives/missing
728 $query = "SELECT serialid,serialseq, status, planneddate, publisheddate,notes, routingnotes
730 WHERE subscriptionid = ?
731 AND (status in (2,4,5))
732 ORDER BY IF(publisheddate<>'0000-00-00',publisheddate,planneddate) DESC
734 $sth = $dbh->prepare($query);
735 $sth->execute($subscriptionid);
736 while ( ( my $line = $sth->fetchrow_hashref ) && $counter < $count ) {
738 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
739 $line->{"planneddate"} = format_date
( $line->{"planneddate"} );
740 $line->{"publisheddate"} = format_date
( $line->{"publisheddate"} );
741 push @serials, $line;
744 $query = "SELECT count(*) FROM serial WHERE subscriptionid=?";
745 $sth = $dbh->prepare($query);
746 $sth->execute($subscriptionid);
747 my ($totalissues) = $sth->fetchrow;
748 return ( $totalissues, @serials );
755 ($totalissues,@serials) = GetSerials2($subscriptionid,$status);
756 this function get 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)
765 my ( $subscription, $status ) = @_;
766 my $dbh = C4
::Context
->dbh;
768 SELECT serialid
,serialseq
, status
, planneddate
, publisheddate
,notes
, routingnotes
770 WHERE subscriptionid
=$subscription AND status IN
($status)
771 ORDER BY publisheddate
,serialid DESC
773 $debug and warn "GetSerials2 query: $query";
774 my $sth = $dbh->prepare($query);
778 while ( my $line = $sth->fetchrow_hashref ) {
779 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
780 $line->{"planneddate"} = format_date
( $line->{"planneddate"} );
781 $line->{"publisheddate"} = format_date
( $line->{"publisheddate"} );
782 push @serials, $line;
784 my ($totalissues) = scalar(@serials);
785 return ( $totalissues, @serials );
788 =head2 GetLatestSerials
792 \@serials = GetLatestSerials($subscriptionid,$limit)
793 get the $limit's latest serials arrived or missing for a given subscription
795 a ref to a table which it containts all of the latest serials stored into a hash.
801 sub GetLatestSerials
{
802 my ( $subscriptionid, $limit ) = @_;
803 my $dbh = C4
::Context
->dbh;
805 # status = 2 is "arrived"
806 my $strsth = "SELECT serialid,serialseq, status, planneddate, notes
808 WHERE subscriptionid = ?
809 AND (status =2 or status=4)
810 ORDER BY planneddate DESC LIMIT 0,$limit
812 my $sth = $dbh->prepare($strsth);
813 $sth->execute($subscriptionid);
815 while ( my $line = $sth->fetchrow_hashref ) {
816 $line->{ "status" . $line->{status
} } = 1; # fills a "statusX" value, used for template status select list
817 $line->{"planneddate"} = format_date
( $line->{"planneddate"} );
818 push @serials, $line;
824 # WHERE subscriptionid=?
826 # $sth=$dbh->prepare($query);
827 # $sth->execute($subscriptionid);
828 # my ($totalissues) = $sth->fetchrow;
832 =head2 GetDistributedTo
836 $distributedto=GetDistributedTo($subscriptionid)
837 This function select the old previous value of distributedto in the database.
843 sub GetDistributedTo
{
844 my $dbh = C4
::Context
->dbh;
846 my $subscriptionid = @_;
847 my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
848 my $sth = $dbh->prepare($query);
849 $sth->execute($subscriptionid);
850 return ($distributedto) = $sth->fetchrow;
858 $val is a hashref containing all the attributes of the table 'subscription'
859 This function get the next issue for the subscription given on input arg
861 all the input params updated.
869 # my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
870 # $calculated = $val->{numberingmethod};
871 # # calculate the (expected) value of the next issue recieved.
872 # $newlastvalue1 = $val->{lastvalue1};
873 # # check if we have to increase the new value.
874 # $newinnerloop1 = $val->{innerloop1}+1;
875 # $newinnerloop1=0 if ($newinnerloop1 >= $val->{every1});
876 # $newlastvalue1 += $val->{add1} if ($newinnerloop1<1); # <1 to be true when 0 or empty.
877 # $newlastvalue1=$val->{setto1} if ($newlastvalue1>$val->{whenmorethan1}); # reset counter if needed.
878 # $calculated =~ s/\{X\}/$newlastvalue1/g;
880 # $newlastvalue2 = $val->{lastvalue2};
881 # # check if we have to increase the new value.
882 # $newinnerloop2 = $val->{innerloop2}+1;
883 # $newinnerloop2=0 if ($newinnerloop2 >= $val->{every2});
884 # $newlastvalue2 += $val->{add2} if ($newinnerloop2<1); # <1 to be true when 0 or empty.
885 # $newlastvalue2=$val->{setto2} if ($newlastvalue2>$val->{whenmorethan2}); # reset counter if needed.
886 # $calculated =~ s/\{Y\}/$newlastvalue2/g;
888 # $newlastvalue3 = $val->{lastvalue3};
889 # # check if we have to increase the new value.
890 # $newinnerloop3 = $val->{innerloop3}+1;
891 # $newinnerloop3=0 if ($newinnerloop3 >= $val->{every3});
892 # $newlastvalue3 += $val->{add3} if ($newinnerloop3<1); # <1 to be true when 0 or empty.
893 # $newlastvalue3=$val->{setto3} if ($newlastvalue3>$val->{whenmorethan3}); # reset counter if needed.
894 # $calculated =~ s/\{Z\}/$newlastvalue3/g;
895 # return ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
900 my ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
901 my $pattern = $val->{numberpattern
};
902 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
903 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
904 $calculated = $val->{numberingmethod
};
905 $newlastvalue1 = $val->{lastvalue1
};
906 $newlastvalue2 = $val->{lastvalue2
};
907 $newlastvalue3 = $val->{lastvalue3
};
908 $newlastvalue1 = $val->{lastvalue1
};
910 # check if we have to increase the new value.
911 $newinnerloop1 = $val->{innerloop1
} + 1;
912 $newinnerloop1 = 0 if ( $newinnerloop1 >= $val->{every1
} );
913 $newlastvalue1 += $val->{add1
} if ( $newinnerloop1 < 1 ); # <1 to be true when 0 or empty.
914 $newlastvalue1 = $val->{setto1
} if ( $newlastvalue1 > $val->{whenmorethan1
} ); # reset counter if needed.
915 $calculated =~ s/\{X\}/$newlastvalue1/g;
917 $newlastvalue2 = $val->{lastvalue2
};
919 # check if we have to increase the new value.
920 $newinnerloop2 = $val->{innerloop2
} + 1;
921 $newinnerloop2 = 0 if ( $newinnerloop2 >= $val->{every2
} );
922 $newlastvalue2 += $val->{add2
} if ( $newinnerloop2 < 1 ); # <1 to be true when 0 or empty.
923 $newlastvalue2 = $val->{setto2
} if ( $newlastvalue2 > $val->{whenmorethan2
} ); # reset counter if needed.
924 if ( $pattern == 6 ) {
925 if ( $val->{hemisphere
} == 2 ) {
926 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
927 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
929 my $newlastvalue2seq = $seasons[$newlastvalue2];
930 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
933 $calculated =~ s/\{Y\}/$newlastvalue2/g;
936 $newlastvalue3 = $val->{lastvalue3
};
938 # check if we have to increase the new value.
939 $newinnerloop3 = $val->{innerloop3
} + 1;
940 $newinnerloop3 = 0 if ( $newinnerloop3 >= $val->{every3
} );
941 $newlastvalue3 += $val->{add3
} if ( $newinnerloop3 < 1 ); # <1 to be true when 0 or empty.
942 $newlastvalue3 = $val->{setto3
} if ( $newlastvalue3 > $val->{whenmorethan3
} ); # reset counter if needed.
943 $calculated =~ s/\{Z\}/$newlastvalue3/g;
945 return ( $calculated, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 );
952 $calculated = GetSeq($val)
953 $val is a hashref containing all the attributes of the table 'subscription'
954 this function transforms {X},{Y},{Z} to 150,0,0 for example.
956 the sequence in integer format
964 my $pattern = $val->{numberpattern
};
965 my @seasons = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
966 my @southern_seasons = ( '', 'Summer', 'Autumn', 'Winter', 'Spring' );
967 my $calculated = $val->{numberingmethod
};
968 my $x = $val->{'lastvalue1'};
969 $calculated =~ s/\{X\}/$x/g;
970 my $newlastvalue2 = $val->{'lastvalue2'};
972 if ( $pattern == 6 ) {
973 if ( $val->{hemisphere
} == 2 ) {
974 my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
975 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
977 my $newlastvalue2seq = $seasons[$newlastvalue2];
978 $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
981 $calculated =~ s/\{Y\}/$newlastvalue2/g;
983 my $z = $val->{'lastvalue3'};
984 $calculated =~ s/\{Z\}/$z/g;
988 =head2 GetExpirationDate
990 $sensddate = GetExpirationDate($subscriptionid)
992 this function return the next expiration date for a subscription given on input args.
999 sub GetExpirationDate
{
1000 my ( $subscriptionid, $startdate ) = @_;
1001 my $dbh = C4
::Context
->dbh;
1002 my $subscription = GetSubscription
($subscriptionid);
1005 # we don't do the same test if the subscription is based on X numbers or on X weeks/months
1006 $enddate = $startdate || $subscription->{startdate
};
1007 my @date = split( /-/, $enddate );
1008 return if ( scalar(@date) != 3 || not check_date
(@date) );
1009 if ( ( $subscription->{periodicity
} % 16 ) > 0 ) {
1012 if ( my $length = $subscription->{numberlength
} ) {
1014 #calculate the date of the last issue.
1015 for ( my $i = 1 ; $i <= $length ; $i++ ) {
1016 $enddate = GetNextDate
( $enddate, $subscription );
1018 } elsif ( $subscription->{monthlength
} ) {
1019 if ( $$subscription{startdate
} ) {
1020 my @enddate = Add_Delta_YM
( $date[0], $date[1], $date[2], 0, $subscription->{monthlength
} );
1021 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1023 } elsif ( $subscription->{weeklength
} ) {
1024 if ( $$subscription{startdate
} ) {
1025 my @date = split( /-/, $subscription->{startdate
} );
1026 my @enddate = Add_Delta_Days
( $date[0], $date[1], $date[2], $subscription->{weeklength
} * 7 );
1027 $enddate = sprintf( "%04d-%02d-%02d", $enddate[0], $enddate[1], $enddate[2] );
1036 =head2 CountSubscriptionFromBiblionumber
1040 $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber)
1041 this count the number of subscription for a biblionumber given.
1043 the number of subscriptions with biblionumber given on input arg.
1049 sub CountSubscriptionFromBiblionumber
{
1050 my ($biblionumber) = @_;
1051 my $dbh = C4
::Context
->dbh;
1052 my $query = "SELECT count(*) FROM subscription WHERE biblionumber=?";
1053 my $sth = $dbh->prepare($query);
1054 $sth->execute($biblionumber);
1055 my $subscriptionsnumber = $sth->fetchrow;
1056 return $subscriptionsnumber;
1059 =head2 ModSubscriptionHistory
1063 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
1065 this function modify the history of a subscription. Put your new values on input arg.
1071 sub ModSubscriptionHistory
{
1072 my ( $subscriptionid, $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote ) = @_;
1073 my $dbh = C4
::Context
->dbh;
1074 my $query = "UPDATE subscriptionhistory
1075 SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=?
1076 WHERE subscriptionid=?
1078 my $sth = $dbh->prepare($query);
1079 $recievedlist =~ s/^; //;
1080 $missinglist =~ s/^; //;
1081 $opacnote =~ s/^; //;
1082 $sth->execute( $histstartdate, $enddate, $recievedlist, $missinglist, $opacnote, $librariannote, $subscriptionid );
1086 =head2 ModSerialStatus
1090 ModSerialStatus($serialid,$serialseq, $planneddate,$publisheddate,$status,$notes)
1092 This function modify the serial status. Serial status is a number.(eg 2 is "arrived")
1093 Note : if we change from "waited" to something else,then we will have to create a new "waited" entry
1099 sub ModSerialStatus
{
1100 my ( $serialid, $serialseq, $planneddate, $publisheddate, $status, $notes ) = @_;
1102 #It is a usual serial
1103 # 1st, get previous status :
1104 my $dbh = C4
::Context
->dbh;
1105 my $query = "SELECT subscriptionid,status FROM serial WHERE serialid=?";
1106 my $sth = $dbh->prepare($query);
1107 $sth->execute($serialid);
1108 my ( $subscriptionid, $oldstatus ) = $sth->fetchrow;
1110 # change status & update subscriptionhistory
1112 if ( $status eq 6 ) {
1113 DelIssue
( { 'serialid' => $serialid, 'subscriptionid' => $subscriptionid, 'serialseq' => $serialseq } );
1115 my $query = "UPDATE serial SET serialseq=?,publisheddate=?,planneddate=?,status=?,notes=? WHERE serialid = ?";
1116 $sth = $dbh->prepare($query);
1117 $sth->execute( $serialseq, $publisheddate, $planneddate, $status, $notes, $serialid );
1118 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1119 $sth = $dbh->prepare($query);
1120 $sth->execute($subscriptionid);
1121 my $val = $sth->fetchrow_hashref;
1122 unless ( $val->{manualhistory
} ) {
1123 $query = "SELECT missinglist,recievedlist FROM subscriptionhistory WHERE subscriptionid=?";
1124 $sth = $dbh->prepare($query);
1125 $sth->execute($subscriptionid);
1126 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1127 if ( $status eq 2 ) {
1129 $recievedlist .= "; $serialseq"
1130 unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
1133 # warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
1134 $missinglist .= "; $serialseq"
1136 and not index( "$missinglist", "$serialseq" ) >= 0 );
1137 $missinglist .= "; $serialseq"
1139 and index( "$missinglist", "$serialseq" ) >= 0 );
1140 $query = "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE subscriptionid=?";
1141 $sth = $dbh->prepare($query);
1142 $recievedlist =~ s/^; //;
1143 $missinglist =~ s/^; //;
1144 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1148 # create new waited entry if needed (ie : was a "waited" and has changed)
1149 if ( $oldstatus eq 1 && $status ne 1 ) {
1150 my $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1151 $sth = $dbh->prepare($query);
1152 $sth->execute($subscriptionid);
1153 my $val = $sth->fetchrow_hashref;
1157 my ( $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3 ) = GetNextSeq
($val);
1159 # warn "Next Seq End";
1161 # next date (calculated from actual date & frequency parameters)
1162 # warn "publisheddate :$publisheddate ";
1163 my $nextpublisheddate = GetNextDate
( $publisheddate, $val );
1164 NewIssue
( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate );
1165 $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
1166 WHERE subscriptionid = ?";
1167 $sth = $dbh->prepare($query);
1168 $sth->execute( $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1, $newinnerloop2, $newinnerloop3, $subscriptionid );
1170 # check if an alert must be sent... (= a letter is defined & status became "arrived"
1171 if ( $val->{letter
} && $status eq 2 && $oldstatus ne 2 ) {
1172 SendAlerts
( 'issue', $val->{subscriptionid
}, $val->{letter
} );
1177 =head2 GetNextExpected
1181 $nextexpected = GetNextExpected($subscriptionid)
1183 Get the planneddate for the current expected issue of the subscription.
1189 planneddate => C4::Dates object
1196 sub GetNextExpected
($) {
1197 my ($subscriptionid) = @_;
1198 my $dbh = C4
::Context
->dbh;
1199 my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
1201 # Each subscription has only one 'expected' issue, with serial.status==1.
1202 $sth->execute( $subscriptionid, 1 );
1203 my ($nextissue) = $sth->fetchrow_hashref;
1204 if ( not $nextissue ) {
1205 $sth = $dbh->prepare('SELECT serialid,planneddate FROM serial WHERE subscriptionid = ? ORDER BY planneddate DESC LIMIT 1');
1206 $sth->execute($subscriptionid);
1207 $nextissue = $sth->fetchrow_hashref;
1209 $nextissue->{planneddate
} = C4
::Dates
->new( $nextissue->{planneddate
}, 'iso' );
1214 =head2 ModNextExpected
1218 ModNextExpected($subscriptionid,$date)
1220 Update the planneddate for the current expected issue of the subscription.
1221 This will modify all future prediction results.
1223 C<$date> is a C4::Dates object.
1229 sub ModNextExpected
($$) {
1230 my ( $subscriptionid, $date ) = @_;
1231 my $dbh = C4
::Context
->dbh;
1233 #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
1234 my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
1236 # Each subscription has only one 'expected' issue, with serial.status==1.
1237 $sth->execute( $date->output('iso'), $date->output('iso'), $subscriptionid, 1 );
1242 =head2 ModSubscription
1246 this function modify a subscription. Put all new values on input args.
1252 sub ModSubscription
{
1253 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1254 $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $add1, $every1,
1255 $whenmorethan1, $setto1, $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2,
1256 $lastvalue2, $innerloop2, $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1257 $numberingmethod, $status, $biblionumber, $callnumber, $notes, $letter, $hemisphere, $manualhistory,
1258 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid
1261 # warn $irregularity;
1262 my $dbh = C4
::Context
->dbh;
1263 my $query = "UPDATE subscription
1264 SET librarian=?, branchcode=?,aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
1265 periodicity=?,firstacquidate=?,dow=?,irregularity=?, numberpattern=?, numberlength=?,weeklength=?,monthlength=?,
1266 add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
1267 add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
1268 add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
1269 numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?,
1270 letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,
1271 staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
1273 WHERE subscriptionid = ?";
1275 #warn "query :".$query;
1276 my $sth = $dbh->prepare($query);
1278 $auser, $branchcode, $aqbooksellerid, $cost,
1279 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
1280 $dow, "$irregularity", $numberpattern, $numberlength,
1281 $weeklength, $monthlength, $add1, $every1,
1282 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
1283 $add2, $every2, $whenmorethan2, $setto2,
1284 $lastvalue2, $innerloop2, $add3, $every3,
1285 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
1286 $numberingmethod, $status, $biblionumber, $callnumber,
1287 $notes, $letter, $hemisphere, ( $manualhistory ?
$manualhistory : 0 ),
1288 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
1289 $graceperiod, $location, $enddate, $subscriptionid
1291 my $rows = $sth->rows;
1294 logaction
( "SERIAL", "MODIFY", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1298 =head2 NewSubscription
1302 $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
1303 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
1304 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
1305 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
1306 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
1307 $numberingmethod, $status, $notes, $serialsadditems,
1308 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate);
1310 Create a new subscription with value given on input args.
1313 the id of this new subscription
1319 sub NewSubscription
{
1320 my ($auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1321 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1322 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1323 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, $status,
1324 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1325 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1327 my $dbh = C4
::Context
->dbh;
1329 #save subscription (insert into database)
1331 INSERT INTO subscription
1332 (librarian
,branchcode
,aqbooksellerid
,cost
,aqbudgetid
,biblionumber
,
1333 startdate
,periodicity
,dow
,numberlength
,weeklength
,monthlength
,
1334 add1
,every1
,whenmorethan1
,setto1
,lastvalue1
,innerloop1
,
1335 add2
,every2
,whenmorethan2
,setto2
,lastvalue2
,innerloop2
,
1336 add3
,every3
,whenmorethan3
,setto3
,lastvalue3
,innerloop3
,
1337 numberingmethod
, status
, notes
, letter
,firstacquidate
,irregularity
,
1338 numberpattern
, callnumber
, hemisphere
,manualhistory
,internalnotes
,serialsadditems
,
1339 staffdisplaycount
,opacdisplaycount
,graceperiod
,location
,enddate
)
1340 VALUES
(?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
1342 my $sth = $dbh->prepare($query);
1344 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity,
1345 $dow, $numberlength, $weeklength, $monthlength, $add1, $every1, $whenmorethan1, $setto1,
1346 $lastvalue1, $innerloop1, $add2, $every2, $whenmorethan2, $setto2, $lastvalue2, $innerloop2,
1347 $add3, $every3, $whenmorethan3, $setto3, $lastvalue3, $innerloop3, $numberingmethod, "$status",
1348 $notes, $letter, $firstacquidate, $irregularity, $numberpattern, $callnumber, $hemisphere, $manualhistory,
1349 $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate
1352 #then create the 1st waited number
1353 my $subscriptionid = $dbh->{'mysql_insertid'};
1355 INSERT INTO subscriptionhistory
1356 (biblionumber
, subscriptionid
, histstartdate
, opacnote
, librariannote
)
1359 $sth = $dbh->prepare($query);
1360 $sth->execute( $biblionumber, $subscriptionid, $startdate, $notes, $internalnotes );
1362 # reread subscription to get a hash (for calculation of the 1st issue number)
1366 WHERE subscriptionid
= ?
1368 $sth = $dbh->prepare($query);
1369 $sth->execute($subscriptionid);
1370 my $val = $sth->fetchrow_hashref;
1372 # calculate issue number
1373 my $serialseq = GetSeq
($val);
1376 (serialseq
,subscriptionid
,biblionumber
,status
, planneddate
, publisheddate
)
1377 VALUES
(?
,?
,?
,?
,?
,?
)
1379 $sth = $dbh->prepare($query);
1380 $sth->execute( "$serialseq", $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
1382 logaction
( "SERIAL", "ADD", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1384 #set serial flag on biblio if not already set.
1385 my ( $null, ($bib) ) = GetBiblio
($biblionumber);
1386 if ( !$bib->{'serial'} ) {
1387 my $record = GetMarcBiblio
($biblionumber);
1388 my ( $tag, $subf ) = GetMarcFromKohaField
( 'biblio.serial', $bib->{'frameworkcode'} );
1390 eval { $record->field($tag)->update( $subf => 1 ); };
1392 ModBiblio
( $record, $biblionumber, $bib->{'frameworkcode'} );
1394 return $subscriptionid;
1397 =head2 ReNewSubscription
1401 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
1403 this function renew a subscription with values given on input args.
1409 sub ReNewSubscription
{
1410 my ( $subscriptionid, $user, $startdate, $numberlength, $weeklength, $monthlength, $note ) = @_;
1411 my $dbh = C4
::Context
->dbh;
1412 my $subscription = GetSubscription
($subscriptionid);
1416 LEFT JOIN biblioitems ON biblio
.biblionumber
=biblioitems
.biblionumber
1417 WHERE biblio
.biblionumber
=?
1419 my $sth = $dbh->prepare($query);
1420 $sth->execute( $subscription->{biblionumber
} );
1421 my $biblio = $sth->fetchrow_hashref;
1423 if ( C4
::Context
->preference("RenewSerialAddsSuggestion") ) {
1426 { 'suggestedby' => $user,
1427 'title' => $subscription->{bibliotitle
},
1428 'author' => $biblio->{author
},
1429 'publishercode' => $biblio->{publishercode
},
1430 'note' => $biblio->{note
},
1431 'biblionumber' => $subscription->{biblionumber
}
1436 # renew subscription
1439 SET startdate
=?
,numberlength
=?
,weeklength
=?
,monthlength
=?
1440 WHERE subscriptionid
=?
1442 $sth = $dbh->prepare($query);
1443 $sth->execute( $startdate, $numberlength, $weeklength, $monthlength, $subscriptionid );
1444 my $enddate = GetExpirationDate
($subscriptionid);
1445 $debug && warn "enddate :$enddate";
1449 WHERE subscriptionid
=?
1451 $sth = $dbh->prepare($query);
1452 $sth->execute( $enddate, $subscriptionid );
1454 UPDATE subscriptionhistory
1456 WHERE subscriptionid
=?
1458 $sth = $dbh->prepare($query);
1459 $sth->execute( $enddate, $subscriptionid );
1461 logaction
( "SERIAL", "RENEW", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1468 NewIssue($serialseq,$subscriptionid,$biblionumber,$status, $planneddate, $publisheddate, $notes)
1470 Create a new issue stored on the database.
1471 Note : we have to update the recievedlist and missinglist on subscriptionhistory for this subscription.
1478 my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
1479 ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
1481 my $dbh = C4
::Context
->dbh;
1484 (serialseq
,subscriptionid
,biblionumber
,status
,publisheddate
,planneddate
,notes
)
1485 VALUES
(?
,?
,?
,?
,?
,?
,?
)
1487 my $sth = $dbh->prepare($query);
1488 $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
1489 my $serialid = $dbh->{'mysql_insertid'};
1491 SELECT missinglist
,recievedlist
1492 FROM subscriptionhistory
1493 WHERE subscriptionid
=?
1495 $sth = $dbh->prepare($query);
1496 $sth->execute($subscriptionid);
1497 my ( $missinglist, $recievedlist ) = $sth->fetchrow;
1499 if ( $status eq 2 ) {
1500 ### TODO Add a feature that improves recognition and description.
1501 ### As such count (serialseq) i.e. : N18,2(N19),N20
1502 ### Would use substr and index But be careful to previous presence of ()
1503 $recievedlist .= "; $serialseq" unless ( index( $recievedlist, $serialseq ) > 0 );
1505 if ( $status eq 4 ) {
1506 $missinglist .= "; $serialseq" unless ( index( $missinglist, $serialseq ) > 0 );
1509 UPDATE subscriptionhistory
1510 SET recievedlist
=?
, missinglist
=?
1511 WHERE subscriptionid
=?
1513 $sth = $dbh->prepare($query);
1514 $recievedlist =~ s/^; //;
1515 $missinglist =~ s/^; //;
1516 $sth->execute( $recievedlist, $missinglist, $subscriptionid );
1520 =head2 ItemizeSerials
1524 ItemizeSerials($serialid, $info);
1525 $info is a hashref containing barcode branch, itemcallnumber, status, location
1526 $serialid the serialid
1528 1 if the itemize is a succes.
1529 0 and @error else. @error containts the list of errors found.
1535 sub ItemizeSerials
{
1536 my ( $serialid, $info ) = @_;
1537 my $now = POSIX
::strftime
( "%Y-%m-%d", localtime );
1539 my $dbh = C4
::Context
->dbh;
1545 my $sth = $dbh->prepare($query);
1546 $sth->execute($serialid);
1547 my $data = $sth->fetchrow_hashref;
1548 if ( C4
::Context
->preference("RoutingSerials") ) {
1550 # check for existing biblioitem relating to serial issue
1551 my ( $count, @results ) = GetBiblioItemByBiblioNumber
( $data->{'biblionumber'} );
1553 for ( my $i = 0 ; $i < $count ; $i++ ) {
1554 if ( $results[$i]->{'volumeddesc'} eq $data->{'serialseq'} . ' (' . $data->{'planneddate'} . ')' ) {
1555 $bibitemno = $results[$i]->{'biblioitemnumber'};
1559 if ( $bibitemno == 0 ) {
1560 my $sth = $dbh->prepare( "SELECT * FROM biblioitems WHERE biblionumber = ? ORDER BY biblioitemnumber DESC" );
1561 $sth->execute( $data->{'biblionumber'} );
1562 my $biblioitem = $sth->fetchrow_hashref;
1563 $biblioitem->{'volumedate'} = $data->{planneddate
};
1564 $biblioitem->{'volumeddesc'} = $data->{serialseq
} . ' (' . format_date
( $data->{'planneddate'} ) . ')';
1565 $biblioitem->{'dewey'} = $info->{itemcallnumber
};
1569 my $fwk = GetFrameworkCode
( $data->{'biblionumber'} );
1570 if ( $info->{barcode
} ) {
1572 my $exists = itemdata
( $info->{'barcode'} );
1573 push @errors, "barcode_not_unique" if ($exists);
1575 my $marcrecord = MARC
::Record
->new();
1576 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.barcode", $fwk );
1577 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{barcode
} );
1578 $marcrecord->insert_fields_ordered($newField);
1579 if ( $info->{branch
} ) {
1580 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.homebranch", $fwk );
1582 #warn "items.homebranch : $tag , $subfield";
1583 if ( $marcrecord->field($tag) ) {
1584 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch
} );
1586 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{branch
} );
1587 $marcrecord->insert_fields_ordered($newField);
1589 ( $tag, $subfield ) = GetMarcFromKohaField
( "items.holdingbranch", $fwk );
1591 #warn "items.holdingbranch : $tag , $subfield";
1592 if ( $marcrecord->field($tag) ) {
1593 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{branch
} );
1595 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{branch
} );
1596 $marcrecord->insert_fields_ordered($newField);
1599 if ( $info->{itemcallnumber
} ) {
1600 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.itemcallnumber", $fwk );
1602 if ( $marcrecord->field($tag) ) {
1603 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{itemcallnumber
} );
1605 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{itemcallnumber
} );
1606 $marcrecord->insert_fields_ordered($newField);
1609 if ( $info->{notes
} ) {
1610 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.itemnotes", $fwk );
1612 if ( $marcrecord->field($tag) ) {
1613 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{notes
} );
1615 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{notes
} );
1616 $marcrecord->insert_fields_ordered($newField);
1619 if ( $info->{location
} ) {
1620 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.location", $fwk );
1622 if ( $marcrecord->field($tag) ) {
1623 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{location
} );
1625 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{location
} );
1626 $marcrecord->insert_fields_ordered($newField);
1629 if ( $info->{status
} ) {
1630 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.notforloan", $fwk );
1632 if ( $marcrecord->field($tag) ) {
1633 $marcrecord->field($tag)->add_subfields( "$subfield" => $info->{status
} );
1635 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $info->{status
} );
1636 $marcrecord->insert_fields_ordered($newField);
1639 if ( C4
::Context
->preference("RoutingSerials") ) {
1640 my ( $tag, $subfield ) = GetMarcFromKohaField
( "items.dateaccessioned", $fwk );
1641 if ( $marcrecord->field($tag) ) {
1642 $marcrecord->field($tag)->add_subfields( "$subfield" => $now );
1644 my $newField = MARC
::Field
->new( "$tag", '', '', "$subfield" => $now );
1645 $marcrecord->insert_fields_ordered($newField);
1648 AddItemFromMarc
( $marcrecord, $data->{'biblionumber'} );
1651 return ( 0, @errors );
1655 =head2 HasSubscriptionStrictlyExpired
1659 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid)
1661 the subscription has stricly expired when today > the end subscription date
1664 1 if true, 0 if false, -1 if the expiration date is not set.
1670 sub HasSubscriptionStrictlyExpired
{
1672 # Getting end of subscription date
1673 my ($subscriptionid) = @_;
1674 my $dbh = C4
::Context
->dbh;
1675 my $subscription = GetSubscription
($subscriptionid);
1676 my $expirationdate = $subscription->{enddate
} || GetExpirationDate
($subscriptionid);
1678 # If the expiration date is set
1679 if ( $expirationdate != 0 ) {
1680 my ( $endyear, $endmonth, $endday ) = split( '-', $expirationdate );
1682 # Getting today's date
1683 my ( $nowyear, $nowmonth, $nowday ) = Today
();
1685 # if today's date > expiration date, then the subscription has stricly expired
1686 if ( Delta_Days
( $nowyear, $nowmonth, $nowday, $endyear, $endmonth, $endday ) < 0 ) {
1693 # There are some cases where the expiration date is not set
1694 # As we can't determine if the subscription has expired on a date-basis,
1700 =head2 HasSubscriptionExpired
1704 $has_expired = HasSubscriptionExpired($subscriptionid)
1706 the subscription has expired when the next issue to arrive is out of subscription limit.
1709 0 if the subscription has not expired
1710 1 if the subscription has expired
1711 2 if has subscription does not have a valid expiration date set
1717 sub HasSubscriptionExpired
{
1718 my ($subscriptionid) = @_;
1719 my $dbh = C4
::Context
->dbh;
1720 my $subscription = GetSubscription
($subscriptionid);
1721 if ( ( $subscription->{periodicity
} % 16 ) > 0 ) {
1722 my $expirationdate = $subscription->{enddate
};
1724 SELECT max
(planneddate
)
1726 WHERE subscriptionid
=?
1728 my $sth = $dbh->prepare($query);
1729 $sth->execute($subscriptionid);
1730 my ($res) = $sth->fetchrow;
1731 return 0 unless $res;
1732 my @res = split( /-/, $res );
1733 my @endofsubscriptiondate = split( /-/, $expirationdate );
1734 return 2 if ( scalar(@res) != 3 || scalar(@endofsubscriptiondate) != 3 || not check_date
(@res) || not check_date
(@endofsubscriptiondate) );
1736 if ( ( @endofsubscriptiondate && Delta_Days
( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) <= 0 )
1740 if ( $subscription->{'numberlength'} ) {
1741 my $countreceived = countissuesfrom
( $subscriptionid, $subscription->{'startdate'} );
1742 return 1 if ( $countreceived > $subscription->{'numberlength'} );
1748 return 0; # Notice that you'll never get here.
1751 =head2 SetDistributedto
1755 SetDistributedto($distributedto,$subscriptionid);
1756 This function update the value of distributedto for a subscription given on input arg.
1762 sub SetDistributedto
{
1763 my ( $distributedto, $subscriptionid ) = @_;
1764 my $dbh = C4
::Context
->dbh;
1768 WHERE subscriptionid
=?
1770 my $sth = $dbh->prepare($query);
1771 $sth->execute( $distributedto, $subscriptionid );
1774 =head2 DelSubscription
1778 DelSubscription($subscriptionid)
1779 this function delete the subscription which has $subscriptionid as id.
1785 sub DelSubscription
{
1786 my ($subscriptionid) = @_;
1787 my $dbh = C4
::Context
->dbh;
1788 $subscriptionid = $dbh->quote($subscriptionid);
1789 $dbh->do("DELETE FROM subscription WHERE subscriptionid=$subscriptionid");
1790 $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
1791 $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
1793 logaction
( "SERIAL", "DELETE", $subscriptionid, "" ) if C4
::Context
->preference("SubscriptionLog");
1800 DelIssue($serialseq,$subscriptionid)
1801 this function delete an issue which has $serialseq and $subscriptionid given on input arg.
1808 my ($dataissue) = @_;
1809 my $dbh = C4
::Context
->dbh;
1810 ### TODO Add itemdeletion. Would need to get itemnumbers. Should be in a pref ?
1815 AND subscriptionid
= ?
1817 my $mainsth = $dbh->prepare($query);
1818 $mainsth->execute( $dataissue->{'serialid'}, $dataissue->{'subscriptionid'} );
1820 #Delete element from subscription history
1821 $query = "SELECT * FROM subscription WHERE subscriptionid = ?";
1822 my $sth = $dbh->prepare($query);
1823 $sth->execute( $dataissue->{'subscriptionid'} );
1824 my $val = $sth->fetchrow_hashref;
1825 unless ( $val->{manualhistory
} ) {
1827 SELECT
* FROM subscriptionhistory
1828 WHERE subscriptionid
= ?
1830 my $sth = $dbh->prepare($query);
1831 $sth->execute( $dataissue->{'subscriptionid'} );
1832 my $data = $sth->fetchrow_hashref;
1833 my $serialseq = $dataissue->{'serialseq'};
1834 $data->{'missinglist'} =~ s/\b$serialseq\b//;
1835 $data->{'recievedlist'} =~ s/\b$serialseq\b//;
1836 my $strsth = "UPDATE subscriptionhistory SET " . join( ",", map { join( "=", $_, $dbh->quote( $data->{$_} ) ) } keys %$data ) . " WHERE subscriptionid=?";
1837 $sth = $dbh->prepare($strsth);
1838 $sth->execute( $dataissue->{'subscriptionid'} );
1841 return $mainsth->rows;
1844 =head2 GetLateOrMissingIssues
1848 ($count,@issuelist) = &GetLateMissingIssues($supplierid,$serialid)
1850 this function select missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
1853 a count of the number of missing issues
1854 the issuelist into a table. Each line of this table containts a ref to a hash which it containts
1855 name,title,planneddate,serialseq,serial.subscriptionid from tables : subscription, serial & biblio
1861 sub GetLateOrMissingIssues
{
1862 my ( $supplierid, $serialid, $order ) = @_;
1863 my $dbh = C4
::Context
->dbh;
1867 $byserial = "and serialid = " . $serialid;
1870 $order .= ", title";
1875 $sth = $dbh->prepare(
1877 serialid, aqbooksellerid, name,
1878 biblio.title, planneddate, serialseq,
1879 serial.status, serial.subscriptionid, claimdate
1881 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1882 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1883 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1884 WHERE subscription.subscriptionid = serial.subscriptionid
1885 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1886 AND subscription.aqbooksellerid=$supplierid
1891 $sth = $dbh->prepare(
1893 serialid, aqbooksellerid, name,
1894 biblio.title, planneddate, serialseq,
1895 serial.status, serial.subscriptionid, claimdate
1897 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
1898 LEFT JOIN biblio ON subscription.biblionumber=biblio.biblionumber
1899 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
1900 WHERE subscription.subscriptionid = serial.subscriptionid
1901 AND (serial.STATUS = 4 OR ((planneddate < now() AND serial.STATUS =1) OR serial.STATUS = 3 OR serial.STATUS = 7))
1911 while ( my $line = $sth->fetchrow_hashref ) {
1912 $odd++ unless $line->{title
} eq $last_title;
1913 $last_title = $line->{title
} if ( $line->{title
} );
1914 $line->{planneddate
} = format_date
( $line->{planneddate
} );
1915 $line->{claimdate
} = format_date
( $line->{claimdate
} );
1916 $line->{ "status" . $line->{status
} } = 1;
1917 $line->{'odd'} = 1 if $odd % 2;
1919 push @issuelist, $line;
1921 return $count, @issuelist;
1924 =head2 removeMissingIssue
1928 removeMissingIssue($subscriptionid)
1930 this function removes an issue from being part of the missing string in
1931 subscriptionlist.missinglist column
1933 called when a missing issue is found from the serials-recieve.pl file
1939 sub removeMissingIssue
{
1940 my ( $sequence, $subscriptionid ) = @_;
1941 my $dbh = C4
::Context
->dbh;
1942 my $sth = $dbh->prepare("SELECT * FROM subscriptionhistory WHERE subscriptionid = ?");
1943 $sth->execute($subscriptionid);
1944 my $data = $sth->fetchrow_hashref;
1945 my $missinglist = $data->{'missinglist'};
1946 my $missinglistbefore = $missinglist;
1948 # warn $missinglist." before";
1949 $missinglist =~ s/($sequence)//;
1951 # warn $missinglist." after";
1952 if ( $missinglist ne $missinglistbefore ) {
1953 $missinglist =~ s/\|\s\|/\|/g;
1954 $missinglist =~ s/^\| //g;
1955 $missinglist =~ s/\|$//g;
1956 my $sth2 = $dbh->prepare(
1957 "UPDATE subscriptionhistory
1959 WHERE subscriptionid = ?"
1961 $sth2->execute( $missinglist, $subscriptionid );
1969 &updateClaim($serialid)
1971 this function updates the time when a claim is issued for late/missing items
1973 called from claims.pl file
1980 my ($serialid) = @_;
1981 my $dbh = C4
::Context
->dbh;
1982 my $sth = $dbh->prepare(
1983 "UPDATE serial SET claimdate = now()
1987 $sth->execute($serialid);
1990 =head2 getsupplierbyserialid
1994 ($result) = &getsupplierbyserialid($serialid)
1996 this function is used to find the supplier id given a serial id
1999 hashref containing serialid, subscriptionid, and aqbooksellerid
2005 sub getsupplierbyserialid
{
2006 my ($serialid) = @_;
2007 my $dbh = C4
::Context
->dbh;
2008 my $sth = $dbh->prepare(
2009 "SELECT serialid, serial.subscriptionid, aqbooksellerid
2011 LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
2015 $sth->execute($serialid);
2016 my $line = $sth->fetchrow_hashref;
2017 my $result = $line->{'aqbooksellerid'};
2021 =head2 check_routing
2025 ($result) = &check_routing($subscriptionid)
2027 this function checks to see if a serial has a routing list and returns the count of routingid
2028 used to show either an 'add' or 'edit' link
2035 my ($subscriptionid) = @_;
2036 my $dbh = C4
::Context
->dbh;
2037 my $sth = $dbh->prepare(
2038 "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist
2039 ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2040 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2043 $sth->execute($subscriptionid);
2044 my $line = $sth->fetchrow_hashref;
2045 my $result = $line->{'routingids'};
2049 =head2 addroutingmember
2053 &addroutingmember($borrowernumber,$subscriptionid)
2055 this function takes a borrowernumber and subscriptionid and add the member to the
2056 routing list for that serial subscription and gives them a rank on the list
2057 of either 1 or highest current rank + 1
2063 sub addroutingmember
{
2064 my ( $borrowernumber, $subscriptionid ) = @_;
2066 my $dbh = C4
::Context
->dbh;
2067 my $sth = $dbh->prepare( "SELECT max(ranking) rank FROM subscriptionroutinglist WHERE subscriptionid = ?" );
2068 $sth->execute($subscriptionid);
2069 while ( my $line = $sth->fetchrow_hashref ) {
2070 if ( $line->{'rank'} > 0 ) {
2071 $rank = $line->{'rank'} + 1;
2076 $sth = $dbh->prepare( "INSERT INTO subscriptionroutinglist (subscriptionid,borrowernumber,ranking) VALUES (?,?,?)" );
2077 $sth->execute( $subscriptionid, $borrowernumber, $rank );
2080 =head2 reorder_members
2084 &reorder_members($subscriptionid,$routingid,$rank)
2086 this function is used to reorder the routing list
2088 it takes the routingid of the member one wants to re-rank and the rank it is to move to
2089 - it gets all members on list puts their routingid's into an array
2090 - removes the one in the array that is $routingid
2091 - then reinjects $routingid at point indicated by $rank
2092 - then update the database with the routingids in the new order
2098 sub reorder_members
{
2099 my ( $subscriptionid, $routingid, $rank ) = @_;
2100 my $dbh = C4
::Context
->dbh;
2101 my $sth = $dbh->prepare( "SELECT * FROM subscriptionroutinglist WHERE subscriptionid = ? ORDER BY ranking ASC" );
2102 $sth->execute($subscriptionid);
2104 while ( my $line = $sth->fetchrow_hashref ) {
2105 push( @result, $line->{'routingid'} );
2108 # To find the matching index
2110 my $key = -1; # to allow for 0 being a valid response
2111 for ( $i = 0 ; $i < @result ; $i++ ) {
2112 if ( $routingid == $result[$i] ) {
2113 $key = $i; # save the index
2118 # if index exists in array then move it to new position
2119 if ( $key > -1 && $rank > 0 ) {
2120 my $new_rank = $rank - 1; # $new_rank is what you want the new index to be in the array
2121 my $moving_item = splice( @result, $key, 1 );
2122 splice( @result, $new_rank, 0, $moving_item );
2124 for ( my $j = 0 ; $j < @result ; $j++ ) {
2125 my $sth = $dbh->prepare( "UPDATE subscriptionroutinglist SET ranking = '" . ( $j + 1 ) . "' WHERE routingid = '" . $result[$j] . "'" );
2130 =head2 delroutingmember
2134 &delroutingmember($routingid,$subscriptionid)
2136 this function either deletes one member from routing list if $routingid exists otherwise
2137 deletes all members from the routing list
2143 sub delroutingmember
{
2145 # if $routingid exists then deletes that row otherwise deletes all with $subscriptionid
2146 my ( $routingid, $subscriptionid ) = @_;
2147 my $dbh = C4
::Context
->dbh;
2149 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE routingid = ?");
2150 $sth->execute($routingid);
2151 reorder_members
( $subscriptionid, $routingid );
2153 my $sth = $dbh->prepare("DELETE FROM subscriptionroutinglist WHERE subscriptionid = ?");
2154 $sth->execute($subscriptionid);
2158 =head2 getroutinglist
2162 ($count,@routinglist) = &getroutinglist($subscriptionid)
2164 this gets the info from the subscriptionroutinglist for $subscriptionid
2167 a count of the number of members on routinglist
2168 the routinglist into a table. Each line of this table containts a ref to a hash which containts
2169 routingid - a unique id, borrowernumber, ranking, and biblionumber of subscription
2175 sub getroutinglist
{
2176 my ($subscriptionid) = @_;
2177 my $dbh = C4
::Context
->dbh;
2178 my $sth = $dbh->prepare(
2179 "SELECT routingid, borrowernumber, ranking, biblionumber
2181 LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
2182 WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
2185 $sth->execute($subscriptionid);
2188 while ( my $line = $sth->fetchrow_hashref ) {
2190 push( @routinglist, $line );
2192 return ( $count, @routinglist );
2195 =head2 countissuesfrom
2199 $result = &countissuesfrom($subscriptionid,$startdate)
2206 sub countissuesfrom
{
2207 my ( $subscriptionid, $startdate ) = @_;
2208 my $dbh = C4
::Context
->dbh;
2212 WHERE subscriptionid
=?
2213 AND serial
.publisheddate
>?
2215 my $sth = $dbh->prepare($query);
2216 $sth->execute( $subscriptionid, $startdate );
2217 my ($countreceived) = $sth->fetchrow;
2218 return $countreceived;
2225 $result = &CountIssues($subscriptionid)
2233 my ($subscriptionid) = @_;
2234 my $dbh = C4
::Context
->dbh;
2238 WHERE subscriptionid
=?
2240 my $sth = $dbh->prepare($query);
2241 $sth->execute($subscriptionid);
2242 my ($countreceived) = $sth->fetchrow;
2243 return $countreceived;
2250 $result = &HasItems($subscriptionid)
2258 my ($subscriptionid) = @_;
2259 my $dbh = C4
::Context
->dbh;
2261 SELECT COUNT
(serialitems
.itemnumber
)
2263 LEFT JOIN serialitems USING
(serialid
)
2264 WHERE subscriptionid
=? AND serialitems
.serialid NOT NULL
2266 my $sth=$dbh->prepare($query);
2267 $sth->execute($subscriptionid);
2268 my ($countitems)=$sth->fetchrow;
2272 =head2 abouttoexpire
2276 $result = &abouttoexpire($subscriptionid)
2278 this function alerts you to the penultimate issue for a serial subscription
2280 returns 1 - if this is the penultimate issue
2288 my ($subscriptionid) = @_;
2289 my $dbh = C4
::Context
->dbh;
2290 my $subscription = GetSubscription
($subscriptionid);
2291 my $per = $subscription->{'periodicity'};
2292 if ( $per % 16 > 0 ) {
2293 my $expirationdate = $subscription->{enddate
};
2294 my $sth = $dbh->prepare("select max(planneddate) from serial where subscriptionid=?");
2295 $sth->execute($subscriptionid);
2296 my ($res) = $sth->fetchrow;
2297 my @res = split( /-/, $res );
2298 @res = Date
::Calc
::Today
if ( $res[0] * $res[1] == 0 );
2299 my @endofsubscriptiondate = split( /-/, $expirationdate );
2301 if ( $per == 1 ) { $x = 7; }
2302 if ( $per == 2 ) { $x = 7; }
2303 if ( $per == 3 ) { $x = 14; }
2304 if ( $per == 4 ) { $x = 21; }
2305 if ( $per == 5 ) { $x = 31; }
2306 if ( $per == 6 ) { $x = 62; }
2307 if ( $per == 7 || $per == 8 ) { $x = 93; }
2308 if ( $per == 9 ) { $x = 190; }
2309 if ( $per == 10 ) { $x = 365; }
2310 if ( $per == 11 ) { $x = 730; }
2311 my @datebeforeend = Add_Delta_Days
( $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2], -( 3 * $x ) )
2312 if ( @endofsubscriptiondate && $endofsubscriptiondate[0] * $endofsubscriptiondate[1] * $endofsubscriptiondate[2] );
2314 # warn "DATE BEFORE END: $datebeforeend";
2319 && Delta_Days
( $res[0], $res[1], $res[2], $datebeforeend[0], $datebeforeend[1], $datebeforeend[2] ) <= 0 )
2320 && ( @endofsubscriptiondate
2321 && Delta_Days
( $res[0], $res[1], $res[2], $endofsubscriptiondate[0], $endofsubscriptiondate[1], $endofsubscriptiondate[2] ) >= 0 )
2324 } elsif ( $subscription->{numberlength
} > 0 ) {
2325 return ( countissuesfrom
( $subscriptionid, $subscription->{'startdate'} ) >= $subscription->{numberlength
} - 1 );
2333 ($resultdate) = &GetNextDate($planneddate,$subscription)
2335 this function is an extension of GetNextDate which allows for checking for irregularity
2337 it takes the planneddate and will return the next issue's date and will skip dates if there
2338 exists an irregularity
2339 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be
2340 skipped then the returned date will be 2007-05-10
2343 $resultdate - then next date in the sequence
2345 Return 0 if periodicity==0
2349 sub in_array
{ # used in next sub down
2350 my ( $val, @elements ) = @_;
2351 foreach my $elem (@elements) {
2352 if ( $val == $elem ) {
2359 sub GetNextDate
(@
) {
2360 my ( $planneddate, $subscription ) = @_;
2361 my @irreg = split( /\,/, $subscription->{irregularity
} );
2363 #date supposed to be in ISO.
2365 my ( $year, $month, $day ) = split( /-/, $planneddate );
2366 $month = 1 unless ($month);
2367 $day = 1 unless ($day);
2370 # warn "DOW $dayofweek";
2371 if ( $subscription->{periodicity
} % 16 == 0 ) { # 'without regularity' || 'irregular'
2376 # Since we're interpreting irregularity here as which days of the week to skip an issue,
2377 # renaming this pattern from 1/day to " n / week ".
2378 if ( $subscription->{periodicity
} == 1 ) {
2379 my $dayofweek = eval { Day_of_Week
( $year, $month, $day ) };
2380 if ($@
) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2382 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2383 $dayofweek = 0 if ( $dayofweek == 7 );
2384 if ( in_array
( ( $dayofweek + 1 ), @irreg ) ) {
2385 ( $year, $month, $day ) = Add_Delta_Days
( $year, $month, $day, 1 );
2389 @resultdate = Add_Delta_Days
( $year, $month, $day, 1 );
2394 if ( $subscription->{periodicity
} == 2 ) {
2395 my ( $wkno, $year ) = eval { Week_of_Year
( $year, $month, $day ) };
2396 if ($@
) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2398 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2400 #FIXME: if two consecutive irreg, do we only skip one?
2401 if ( $irreg[$i] == ( ( $wkno != 51 ) ?
( $wkno + 1 ) % 52 : 52 ) ) {
2402 ( $year, $month, $day ) = Add_Delta_Days
( $year, $month, $day, 7 );
2403 $wkno = ( ( $wkno != 51 ) ?
( $wkno + 1 ) % 52 : 52 );
2406 @resultdate = Add_Delta_Days
( $year, $month, $day, 7 );
2411 if ( $subscription->{periodicity
} == 3 ) {
2412 my ( $wkno, $year ) = eval { Week_of_Year
( $year, $month, $day ) };
2413 if ($@
) { warn "year month day : $year $month $day $subscription->{subscriptionid} : $@"; }
2415 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2416 if ( $irreg[$i] == ( ( $wkno != 50 ) ?
( $wkno + 2 ) % 52 : 52 ) ) {
2417 ### BUGFIX was previously +1 ^
2418 ( $year, $month, $day ) = Add_Delta_Days
( $year, $month, $day, 14 );
2419 $wkno = ( ( $wkno != 50 ) ?
( $wkno + 2 ) % 52 : 52 );
2422 @resultdate = Add_Delta_Days
( $year, $month, $day, 14 );
2427 if ( $subscription->{periodicity
} == 4 ) {
2428 my ( $wkno, $year ) = eval { Week_of_Year
( $year, $month, $day ) };
2429 if ($@
) { warn "année mois jour : $year $month $day $subscription->{subscriptionid} : $@"; }
2431 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2432 if ( $irreg[$i] == ( ( $wkno != 49 ) ?
( $wkno + 3 ) % 52 : 52 ) ) {
2433 ( $year, $month, $day ) = Add_Delta_Days
( $year, $month, $day, 21 );
2434 $wkno = ( ( $wkno != 49 ) ?
( $wkno + 3 ) % 52 : 52 );
2437 @resultdate = Add_Delta_Days
( $year, $month, $day, 21 );
2440 my $tmpmonth = $month;
2441 if ( $year && $month && $day ) {
2442 if ( $subscription->{periodicity
} == 5 ) {
2443 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2444 if ( $irreg[$i] == ( ( $tmpmonth != 11 ) ?
( $tmpmonth + 1 ) % 12 : 12 ) ) {
2445 ( $year, $month, $day ) = Add_Delta_YMD
( $year, $month, $day, 0, 1, 0 );
2446 $tmpmonth = ( ( $tmpmonth != 11 ) ?
( $tmpmonth + 1 ) % 12 : 12 );
2449 @resultdate = Add_Delta_YMD
( $year, $month, $day, 0, 1, 0 );
2451 if ( $subscription->{periodicity
} == 6 ) {
2452 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2453 if ( $irreg[$i] == ( ( $tmpmonth != 10 ) ?
( $tmpmonth + 2 ) % 12 : 12 ) ) {
2454 ( $year, $month, $day ) = Add_Delta_YMD
( $year, $month, $day, 0, 2, 0 );
2455 $tmpmonth = ( ( $tmpmonth != 10 ) ?
( $tmpmonth + 2 ) % 12 : 12 );
2458 @resultdate = Add_Delta_YMD
( $year, $month, $day, 0, 2, 0 );
2460 if ( $subscription->{periodicity
} == 7 ) {
2461 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2462 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ?
( $tmpmonth + 3 ) % 12 : 12 ) ) {
2463 ( $year, $month, $day ) = Add_Delta_YMD
( $year, $month, $day, 0, 3, 0 );
2464 $tmpmonth = ( ( $tmpmonth != 9 ) ?
( $tmpmonth + 3 ) % 12 : 12 );
2467 @resultdate = Add_Delta_YMD
( $year, $month, $day, 0, 3, 0 );
2469 if ( $subscription->{periodicity
} == 8 ) {
2470 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2471 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ?
( $tmpmonth + 3 ) % 12 : 12 ) ) {
2472 ( $year, $month, $day ) = Add_Delta_YMD
( $year, $month, $day, 0, 3, 0 );
2473 $tmpmonth = ( ( $tmpmonth != 9 ) ?
( $tmpmonth + 3 ) % 12 : 12 );
2476 @resultdate = Add_Delta_YMD
( $year, $month, $day, 0, 3, 0 );
2478 if ( $subscription->{periodicity
} == 9 ) {
2479 for ( my $i = 0 ; $i < @irreg ; $i++ ) {
2480 if ( $irreg[$i] == ( ( $tmpmonth != 9 ) ?
( $tmpmonth + 3 ) % 12 : 12 ) ) {
2481 ### BUFIX Seems to need more Than One ?
2482 ( $year, $month, $day ) = Add_Delta_YM
( $year, $month, $day, 0, 6 );
2483 $tmpmonth = ( ( $tmpmonth != 6 ) ?
( $tmpmonth + 6 ) % 12 : 12 );
2486 @resultdate = Add_Delta_YM
( $year, $month, $day, 0, 6 );
2488 if ( $subscription->{periodicity
} == 10 ) {
2489 @resultdate = Add_Delta_YM
( $year, $month, $day, 1, 0 );
2491 if ( $subscription->{periodicity
} == 11 ) {
2492 @resultdate = Add_Delta_YM
( $year, $month, $day, 2, 0 );
2495 my $resultdate = sprintf( "%04d-%02d-%02d", $resultdate[0], $resultdate[1], $resultdate[2] );
2497 return "$resultdate";
2502 $item = &itemdata($barcode);
2504 Looks up the item with the given barcode, and returns a
2505 reference-to-hash containing information about that item. The keys of
2506 the hash are the fields from the C<items> and C<biblioitems> tables in
2514 my $dbh = C4
::Context
->dbh;
2515 my $sth = $dbh->prepare(
2516 "Select * from items LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
2519 $sth->execute($barcode);
2520 my $data = $sth->fetchrow_hashref;
2530 Koha Developement team <info@koha.org>