Bug 13869: Wrong capitalization of 'Save Report' in guided reports
[koha.git] / C4 / Overdues.pm
index af83e0f..1797923 100644 (file)
@@ -23,6 +23,8 @@ use strict;
 #use warnings; FIXME - Bug 2505
 use Date::Calc qw/Today Date_to_Days/;
 use Date::Manip qw/UnixDate/;
+use List::MoreUtils qw( uniq );
+
 use C4::Circulation;
 use C4::Context;
 use C4::Accounts;
@@ -50,6 +52,7 @@ BEGIN {
         &GetOverduesForBranch
         &RemoveNotifyLine
         &AddNotifyLine
+        &GetOverdueMessageTransportTypes
        );
        # subs to remove
        push @EXPORT, qw(
@@ -106,14 +109,14 @@ sub Getoverdues {
     my $statement;
     if ( C4::Context->preference('item-level_itypes') ) {
         $statement = "
-   SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode
+   SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode, items.itemlost
      FROM issues 
 LEFT JOIN items       USING (itemnumber)
     WHERE date_due < NOW()
 ";
     } else {
         $statement = "
-   SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode
+   SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode, items.itemlost
      FROM issues 
 LEFT JOIN items       USING (itemnumber)
 LEFT JOIN biblioitems USING (biblioitemnumber)
@@ -279,15 +282,19 @@ C<$branchcode> is the branch whose calendar to use for finding holidays.
 =cut
 
 sub _get_chargeable_units {
-    my ($unit, $dt1, $dt2, $branchcode) = @_;
+    my ($unit, $date_due, $date_returned, $branchcode) = @_;
+
+    # If the due date is later than the return date
+    return 0 unless ( $date_returned > $date_due );
+
     my $charge_units = 0;
     my $charge_duration;
     if ($unit eq 'hours') {
         if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
             my $calendar = Koha::Calendar->new( branchcode => $branchcode );
-            $charge_duration = $calendar->hours_between( $dt1, $dt2 );
+            $charge_duration = $calendar->hours_between( $date_due, $date_returned );
         } else {
-            $charge_duration = $dt2->delta_ms( $dt1 );
+            $charge_duration = $date_returned->delta_ms( $date_due );
         }
         if($charge_duration->in_units('hours') == 0 && $charge_duration->in_units('seconds') > 0){
             return 1;
@@ -297,9 +304,9 @@ sub _get_chargeable_units {
     else { # days
         if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
             my $calendar = Koha::Calendar->new( branchcode => $branchcode );
-            $charge_duration = $calendar->days_between( $dt1, $dt2 );
+            $charge_duration = $calendar->days_between( $date_due, $date_returned );
         } else {
-            $charge_duration = $dt2->delta_days( $dt1 );
+            $charge_duration = $date_returned->delta_days( $date_due );
         }
         return $charge_duration->in_units('days');
     }
@@ -648,9 +655,16 @@ sub GetFine {
     my $dbh   = C4::Context->dbh();
     my $query = q|SELECT sum(amountoutstanding) as fineamount FROM accountlines
     where accounttype like 'F%'
-  AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?|;
+  AND amountoutstanding > 0 AND borrowernumber=?|;
+    my @query_param;
+    push @query_param, $borrowernumber;
+    if (defined $itemnum )
+    {
+        $query .= " AND itemnumber=?";
+        push @query_param, $itemnum;
+    }
     my $sth = $dbh->prepare($query);
-    $sth->execute( $itemnum, $borrowernumber );
+    $sth->execute( @query_param );
     my $fine = $sth->fetchrow_hashref();
     if ($fine->{fineamount}) {
         return $fine->{fineamount};
@@ -749,14 +763,18 @@ returns a list of branch codes for branches with overdue rules defined.
 
 sub GetBranchcodesWithOverdueRules {
     my $dbh               = C4::Context->dbh;
-    my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL AND branchcode <> '' ORDER BY branchcode");
-    $rqoverduebranches->execute;
-    my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
-    if (!$branches[0]) {
-       my $availbranches = C4::Branch::GetBranches();
-       @branches = keys %$availbranches;
+    my $branchcodes = $dbh->selectcol_arrayref(q|
+        SELECT DISTINCT(branchcode)
+        FROM overduerules
+        WHERE delay1 IS NOT NULL
+        ORDER BY branchcode
+    |);
+    if ( $branchcodes->[0] eq '' ) {
+        # If a default rule exists, all branches should be returned
+        my $availbranches = C4::Branch::GetBranches();
+        return keys %$availbranches;
     }
-    return @branches;
+    return @$branchcodes;
 }
 
 =head2 CheckItemNotify
@@ -906,6 +924,36 @@ sub RemoveNotifyLine {
     return 1;
 }
 
+=head2 GetOverdueMessageTransportTypes
+
+    my $message_transport_types = GetOverdueMessageTransportTypes( $branchcode, $categorycode, $letternumber);
+
+    return a arrayref with all message_transport_type for given branchcode, categorycode and letternumber(1,2 or 3)
+
+=cut
+
+sub GetOverdueMessageTransportTypes {
+    my ( $branchcode, $categorycode, $letternumber ) = @_;
+    return unless $categorycode and $letternumber;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("
+        SELECT message_transport_type FROM overduerules_transport_types
+        WHERE branchcode = ? AND categorycode = ? AND letternumber = ?
+    ");
+    $sth->execute( $branchcode, $categorycode, $letternumber );
+    my @mtts;
+    while ( my $mtt = $sth->fetchrow ) {
+        push @mtts, $mtt;
+    }
+
+    # Put 'print' in first if exists
+    # It avoid to sent a print notice with an email or sms template is no email or sms is defined
+    @mtts = uniq( 'print', @mtts )
+        if grep {/^print$/} @mtts;
+
+    return \@mtts;
+}
+
 1;
 __END__