Bug 21241: (follow-up) Syspref to control fallback to SMS when no email is defined
[koha.git] / C4 / Reserves.pm
index 3c3e158..77880dc 100644 (file)
@@ -49,6 +49,7 @@ use Koha::Items;
 use Koha::ItemTypes;
 use Koha::Patrons;
 use Koha::CirculationRules;
+use Koha::Account::Lines;
 
 use List::MoreUtils qw( firstidx any );
 use Carp;
@@ -304,6 +305,7 @@ sub CanBookBeReserved{
          { status => notReservable },   if holds on this item are not allowed
          { status => libraryNotFound },   if given branchcode is not an existing library
          { status => libraryNotPickupLocation },   if given branchcode is not configured to be a pickup location
+         { status => cannotBeTransferred }, if branch transfer limit applies on given item and branchcode
 
 =cut
 
@@ -314,7 +316,7 @@ sub CanItemBeReserved {
     my $ruleitemtype;    # itemtype of the matching issuing rule
     my $allowedreserves  = 0; # Total number of holds allowed across all records
     my $holds_per_record = 1; # Total number of holds allowed for this one given record
-    my $holds_per_day    = 0; # Total number of holds allowed per day for the given patron
+    my $holds_per_day;        # Default to unlimited
 
     # we retrieve borrowers and items informations #
     # item->{itype} will come for biblioitems if necessery
@@ -474,12 +476,16 @@ sub CanItemBeReserved {
         my $destination = Koha::Libraries->find({
             branchcode => $pickup_branchcode,
         });
+
         unless ($destination) {
             return { status => 'libraryNotFound' };
         }
         unless ($destination->pickup_location) {
             return { status => 'libraryNotPickupLocation' };
         }
+        unless ($item->can_be_transferred({ to => $destination })) {
+            return 'cannotBeTransferred';
+        }
     }
 
     return { status => 'OK' };
@@ -566,13 +572,24 @@ sub GetOtherReserves {
 
 sub ChargeReserveFee {
     my ( $borrowernumber, $fee, $title ) = @_;
-    return if !$fee || $fee==0; # the last test is needed to include 0.00
-    my $accquery = qq{
-INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
-    };
-    my $dbh = C4::Context->dbh;
-    my $nextacctno = &getnextacctno( $borrowernumber );
-    $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
+
+    return if !$fee || $fee == 0;    # the last test is needed to include 0.00
+
+    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
+    my $nextacctno = C4::Accounts::getnextacctno($borrowernumber);
+
+    Koha::Account::Line->new(
+        {
+            borrowernumber    => $borrowernumber,
+            accountno         => $nextacctno,
+            date              => dt_from_string(),
+            amount            => $fee,
+            description       => "Reserve Charge - $title",
+            accounttype       => 'Res',
+            amountoutstanding => $fee,
+            branchcode        => $branchcode
+        }
+    )->store();
 }
 
 =head2 GetReserveFee