Bug 20819: Add consent to self-registration process
[koha.git] / C4 / Serials.pm
index 3a9b1d6..0eb699f 100644 (file)
@@ -78,14 +78,12 @@ BEGIN {
       &PrepareSerialsData &GetNextExpected    &ModNextExpected
       &GetPreviousSerialid
 
-      &GetSuppliersWithLateIssues             &getsupplierbyserialid
-      &GetDistributedTo   &SetDistributedTo
+      &GetSuppliersWithLateIssues
       &getroutinglist     &delroutingmember   &addroutingmember
       &reorder_members
       &check_routing &updateClaim
       &CountIssues
       HasItems
-      &GetSubscriptionsFromBorrower
       &subscriptionCurrentlyOnOrder
 
     );
@@ -163,26 +161,6 @@ sub GetSubscriptionHistoryFromSubscriptionId {
     return $results;
 }
 
-=head2 GetSerialStatusFromSerialId
-
-$sth = GetSerialStatusFromSerialId();
-this function returns a statement handle
-After this function, don't forget to execute it by using $sth->execute($serialid)
-return :
-$sth = $dbh->prepare($query).
-
-=cut
-
-sub GetSerialStatusFromSerialId {
-    my $dbh   = C4::Context->dbh;
-    my $query = qq|
-        SELECT status
-        FROM   serial
-        WHERE  serialid = ?
-    |;
-    return $dbh->prepare($query);
-}
-
 =head2 GetSerialInformation
 
 $data = GetSerialInformation($serialid);
@@ -265,7 +243,7 @@ sub AddItem2Serial {
 $subs = GetSubscription($subscriptionid)
 this function returns the subscription which has $subscriptionid as id.
 return :
-a hashref. This hash containts
+a hashref. This hash contains
 subscription, subscriptionhistory, aqbooksellers.name, biblio.title
 
 =cut
@@ -844,28 +822,6 @@ sub GetPreviousSerialid {
     return $return;
 }
 
-
-
-=head2 GetDistributedTo
-
-$distributedto=GetDistributedTo($subscriptionid)
-This function returns the field distributedto for the subscription matching subscriptionid
-
-=cut
-
-sub GetDistributedTo {
-    my $dbh = C4::Context->dbh;
-    my $distributedto;
-    my ($subscriptionid) = @_;
-
-    return unless ($subscriptionid);
-
-    my $query          = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
-    my $sth            = $dbh->prepare($query);
-    $sth->execute($subscriptionid);
-    return ($distributedto) = $sth->fetchrow;
-}
-
 =head2 GetNextSeq
 
     my (
@@ -1453,8 +1409,8 @@ sub NewSubscription {
     # then create the 1st expected number
     $query = qq(
         INSERT INTO subscriptionhistory
-            (biblionumber, subscriptionid, histstartdate)
-        VALUES (?,?,?)
+            (biblionumber, subscriptionid, histstartdate, missinglist, recievedlist)
+        VALUES (?,?,?, '', '')
         );
     $sth = $dbh->prepare($query);
     $sth->execute( $biblionumber, $subscriptionid, $startdate);
@@ -1483,14 +1439,14 @@ sub NewSubscription {
     logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
 
     #set serial flag on biblio if not already set.
-    my $bib = GetBiblio($biblionumber);
-    if ( $bib and !$bib->{'serial'} ) {
-        my $record = GetMarcBiblio($biblionumber);
-        my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $bib->{'frameworkcode'} );
+    my $biblio = Koha::Biblios->find( $biblionumber );
+    if ( $biblio and !$biblio->serial ) {
+        my $record = GetMarcBiblio({ biblionumber => $biblionumber });
+        my ( $tag, $subf ) = GetMarcFromKohaField( 'biblio.serial', $biblio->frameworkcode );
         if ($tag) {
             eval { $record->field($tag)->update( $subf => 1 ); };
         }
-        ModBiblio( $record, $biblionumber, $bib->{'frameworkcode'} );
+        ModBiblio( $record, $biblionumber, $biblio->frameworkcode );
     }
     return $subscriptionid;
 }
@@ -1530,6 +1486,9 @@ sub ReNewSubscription {
         );
     }
 
+    $numberlength ||= 0; # Should not we raise an exception instead?
+    $weeklength   ||= 0;
+
     # renew subscription
     $query = qq|
         UPDATE subscription
@@ -1716,26 +1675,6 @@ sub HasSubscriptionExpired {
     return 0;    # Notice that you'll never get here.
 }
 
-=head2 SetDistributedto
-
-SetDistributedto($distributedto,$subscriptionid);
-This function update the value of distributedto for a subscription given on input arg.
-
-=cut
-
-sub SetDistributedto {
-    my ( $distributedto, $subscriptionid ) = @_;
-    my $dbh   = C4::Context->dbh;
-    my $query = qq|
-        UPDATE subscription
-        SET    distributedto=?
-        WHERE  subscriptionid=?
-    |;
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $distributedto, $subscriptionid );
-    return;
-}
-
 =head2 DelSubscription
 
 DelSubscription($subscriptionid)
@@ -1920,33 +1859,6 @@ sub updateClaim {
         {}, CLAIMED, @$serialids );
 }
 
-=head2 getsupplierbyserialid
-
-$result = getsupplierbyserialid($serialid)
-
-this function is used to find the supplier id given a serial id
-
-return :
-hashref containing serialid, subscriptionid, and aqbooksellerid
-
-=cut
-
-sub getsupplierbyserialid {
-    my ($serialid) = @_;
-    my $dbh        = C4::Context->dbh;
-    my $sth        = $dbh->prepare(
-        "SELECT serialid, serial.subscriptionid, aqbooksellerid
-         FROM serial 
-            LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
-            WHERE serialid = ?
-        "
-    );
-    $sth->execute($serialid);
-    my $line   = $sth->fetchrow_hashref;
-    my $result = $line->{'aqbooksellerid'};
-    return $result;
-}
-
 =head2 check_routing
 
 $result = &check_routing($subscriptionid)
@@ -2212,51 +2124,6 @@ sub abouttoexpire {
     return 0;
 }
 
-sub in_array {    # used in next sub down
-    my ( $val, @elements ) = @_;
-    foreach my $elem (@elements) {
-        if ( $val == $elem ) {
-            return 1;
-        }
-    }
-    return 0;
-}
-
-=head2 GetSubscriptionsFromBorrower
-
-($count,@routinglist) = GetSubscriptionsFromBorrower($borrowernumber)
-
-this gets the info from subscriptionroutinglist for each $subscriptionid
-
-return :
-a count of the serial subscription routing lists to which a patron belongs,
-with the titles of those serial subscriptions as an array. Each element of the array
-contains a hash_ref with subscriptionID and title of subscription.
-
-=cut
-
-sub GetSubscriptionsFromBorrower {
-    my ($borrowernumber) = @_;
-    my $dbh              = C4::Context->dbh;
-    my $sth              = $dbh->prepare(
-        "SELECT subscription.subscriptionid, biblio.title
-            FROM subscription
-            JOIN biblio ON biblio.biblionumber = subscription.biblionumber
-            JOIN subscriptionroutinglist USING (subscriptionid)
-            WHERE subscriptionroutinglist.borrowernumber = ? ORDER BY title ASC
-                               "
-    );
-    $sth->execute($borrowernumber);
-    my @routinglist;
-    my $count = 0;
-    while ( my $line = $sth->fetchrow_hashref ) {
-        $count++;
-        push( @routinglist, $line );
-    }
-    return ( $count, @routinglist );
-}
-
-
 =head2 GetFictiveIssueNumber
 
 $issueno = GetFictiveIssueNumber($subscription, $publishedate);
@@ -2270,6 +2137,8 @@ depending on how many rows are in serial table.
 The issue number calculation is based on subscription frequency, first acquisition
 date, and $publisheddate.
 
+Returns undef when called for irregular frequencies.
+
 The routine is used to skip irregularities when calculating the next issue
 date (in GetNextDate) or the next issue number (in GetNextSeq).
 
@@ -2280,26 +2149,24 @@ sub GetFictiveIssueNumber {
 
     my $frequency = GetSubscriptionFrequency($subscription->{'periodicity'});
     my $unit = $frequency->{unit} ? lc $frequency->{'unit'} : undef;
-    my $issueno = 0;
+    return if !$unit;
+    my $issueno;
 
-    if($unit) {
-        my ($year, $month, $day) = split /-/, $publisheddate;
-        my ($fa_year, $fa_month, $fa_day) = split /-/, $subscription->{'firstacquidate'};
-        my $wkno;
-        my $delta = _delta_units( [$fa_year, $fa_month, $fa_day], [$year, $month, $day], $unit );
+    my ( $year, $month, $day ) = split /-/, $publisheddate;
+    my ( $fa_year, $fa_month, $fa_day ) = split /-/, $subscription->{'firstacquidate'};
+    my $delta = _delta_units( [$fa_year, $fa_month, $fa_day], [$year, $month, $day], $unit );
 
-        if($frequency->{'unitsperissue'} == 1) {
-            $issueno = $delta * $frequency->{'issuesperunit'} + $subscription->{'countissuesperunit'};
-        } else {
-            # Assuming issuesperunit == 1
-            $issueno = int( ($delta + $frequency->{'unitsperissue'}) / $frequency->{'unitsperissue'} );
-        }
+    if( $frequency->{'unitsperissue'} == 1 ) {
+        $issueno = $delta * $frequency->{'issuesperunit'} + $subscription->{'countissuesperunit'};
+    } else { # issuesperunit == 1
+        $issueno = 1 + int( $delta / $frequency->{'unitsperissue'} );
     }
     return $issueno;
 }
 
 sub _delta_units {
     my ( $date1, $date2, $unit ) = @_;
+    # date1 and date2 are array refs in the form [ yy, mm, dd ]
 
     if( $unit eq 'day' ) {
         return Delta_Days( @$date1, @$date2 );
@@ -2580,26 +2447,6 @@ sub _numeration {
     return $string;
 }
 
-=head2 is_barcode_in_use
-
-Returns number of occurrences of the barcode in the items table
-Can be used as a boolean test of whether the barcode has
-been deployed as yet
-
-=cut
-
-sub is_barcode_in_use {
-    my $barcode = shift;
-    my $dbh       = C4::Context->dbh;
-    my $occurrences = $dbh->selectall_arrayref(
-        'SELECT itemnumber from items where barcode = ?',
-        {}, $barcode
-
-    );
-
-    return @{$occurrences};
-}
-
 =head2 CloseSubscription
 
 Close a subscription given a subscriptionid