Bug 19280: Pass a Koha::Patron to CanBookBeIssued
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 8 Sep 2017 15:51:28 +0000 (12:51 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 9 Jan 2018 20:23:15 +0000 (17:23 -0300)
We need to make subroutine from C4 use more Koha::Object objects
Seeing bug 19276, starting here is a good start.

Test plan:
The tests should still pass.

Signed-off-by: Jon Knight <J.P.Knight@lboro.ac.uk>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
C4/Circulation.pm
C4/ILSDI/Services.pm
circ/circulation.pl
opac/sco/sco-main.pl
t/db_dependent/Circulation.t
t/db_dependent/Circulation/NoIssuesChargeGuarantees.t
t/db_dependent/Circulation/SwitchOnSiteCheckouts.t
t/db_dependent/Circulation/dateexpiry.t
t/db_dependent/DecreaseLoanHighHolds.t
t/db_dependent/Patron/Borrower_PrevCheckout.t
t/db_dependent/rollingloans.t

index a2a9e7b..f200907 100644 (file)
@@ -567,7 +567,7 @@ sub TooMany {
 
 =head2 CanBookBeIssued
 
-  ( $issuingimpossible, $needsconfirmation, [ $alerts ] ) =  CanBookBeIssued( $borrower,
+  ( $issuingimpossible, $needsconfirmation, [ $alerts ] ) =  CanBookBeIssued( $patron,
                       $barcode, $duedate, $inprocess, $ignore_reserves, $params );
 
 Check if a book can be issued.
@@ -580,7 +580,7 @@ data is keyed in lower case!
 
 =over 4
 
-=item C<$borrower> hash with borrower informations (from Koha::Patron->unblessed)
+=item C<$patron> is a Koha::Patron
 
 =item C<$barcode> is the bar code of the book being issued.
 
@@ -671,7 +671,7 @@ if the borrower borrows to much things
 =cut
 
 sub CanBookBeIssued {
-    my ( $borrower, $barcode, $duedate, $inprocess, $ignore_reserves, $params ) = @_;
+    my ( $patron, $barcode, $duedate, $inprocess, $ignore_reserves, $params ) = @_;
     my %needsconfirmation;    # filled with problems that needs confirmations
     my %issuingimpossible;    # filled with problems that causes the issue to be IMPOSSIBLE
     my %alerts;               # filled with messages that shouldn't stop issuing, but the librarian should be aware of.
@@ -685,6 +685,7 @@ sub CanBookBeIssued {
        my $biblioitem = GetBiblioItemData($item->{biblioitemnumber});
        $item->{'itemtype'}=$item->{'itype'}; 
     my $dbh             = C4::Context->dbh;
+    my $patron_unblessed = $patron->unblessed;
 
     # MANDATORY CHECKS - unless item exists, nothing else matters
     unless ( $item->{barcode} ) {
@@ -702,9 +703,9 @@ sub CanBookBeIssued {
     unless ( $duedate ) {
         my $issuedate = $now->clone();
 
-        my $branch = _GetCircControlBranch($item,$borrower);
+        my $branch = _GetCircControlBranch($item, $patron_unblessed);
         my $itype = ( C4::Context->preference('item-level_itypes') ) ? $item->{'itype'} : $biblioitem->{'itemtype'};
-        $duedate = CalcDateDue( $issuedate, $itype, $branch, $borrower );
+        $duedate = CalcDateDue( $issuedate, $itype, $branch, $patron_unblessed );
 
         # Offline circ calls AddIssue directly, doesn't run through here
         #  So issuingimpossible should be ok.
@@ -722,7 +723,6 @@ sub CanBookBeIssued {
     #
     # BORROWER STATUS
     #
-    my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
     if ( $patron->category->category_type eq 'X' && (  $item->{barcode}  )) {
        # stats only borrower -- add entry to statistics table, and return issuingimpossible{STATS} = 1  .
         &UpdateStats({
@@ -730,14 +730,14 @@ sub CanBookBeIssued {
                      type => 'localuse',
                      itemnumber => $item->{'itemnumber'},
                      itemtype => $item->{'itype'},
-                     borrowernumber => $borrower->{'borrowernumber'},
+                     borrowernumber => $patron->borrowernumber,
                      ccode => $item->{'ccode'}}
                     );
         ModDateLastSeen( $item->{'itemnumber'} );
         return( { STATS => 1 }, {});
     }
 
-    my $flags = C4::Members::patronflags( $borrower );
+    my $flags = C4::Members::patronflags( $patron_unblessed );
     if ( ref $flags ) {
         if ( $flags->{GNA} ) {
             $issuingimpossible{GNA} = 1;
@@ -749,10 +749,10 @@ sub CanBookBeIssued {
             $issuingimpossible{DEBARRED} = 1;
         }
     }
-    if ( !defined $borrower->{dateexpiry} || $borrower->{'dateexpiry'} eq '0000-00-00') {
+    if ( !defined $patron->dateexpiry || $patron->dateexpiry eq '0000-00-00') {
         $issuingimpossible{EXPIRED} = 1;
     } else {
-        my $expiry_dt = dt_from_string( $borrower->{dateexpiry}, 'sql', 'floating' );
+        my $expiry_dt = dt_from_string( $patron->dateexpiry, 'sql', 'floating' );
         $expiry_dt->truncate( to => 'day');
         my $today = $now->clone()->truncate(to => 'day');
         $today->set_time_zone( 'floating' );
@@ -767,7 +767,7 @@ sub CanBookBeIssued {
 
     # DEBTS
     my ($balance, $non_issue_charges, $other_charges) =
-      C4::Members::GetMemberAccountBalance( $borrower->{'borrowernumber'} );
+      C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
 
     my $amountlimit = C4::Context->preference("noissuescharge");
     my $allowfineoverride = C4::Context->preference("AllowFineOverride");
@@ -777,8 +777,7 @@ sub CanBookBeIssued {
     my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
     $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
     if ( defined $no_issues_charge_guarantees ) {
-        my $p = Koha::Patrons->find( $borrower->{borrowernumber} );
-        my @guarantees = $p->guarantees();
+        my @guarantees = $patron->guarantees();
         my $guarantees_non_issues_charges;
         foreach my $g ( @guarantees ) {
             my ( $b, $n, $o ) = C4::Members::GetMemberAccountBalance( $g->id );
@@ -817,7 +816,7 @@ sub CanBookBeIssued {
         $alerts{OTHER_CHARGES} = sprintf( "%.2f", $other_charges );
     }
 
-    $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
+    $patron = Koha::Patrons->find( $patron->borrowernumber ); # FIXME Refetch just in case, to avoid regressions. But must not be needed
     if ( my $debarred_date = $patron->is_debarred ) {
          # patron has accrued fine days or has a restriction. $count is a date
         if ($debarred_date eq '9999-12-31') {
@@ -839,7 +838,7 @@ sub CanBookBeIssued {
     #
     # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
     #
-    if ( $issue && $issue->borrowernumber eq $borrower->{'borrowernumber'} ){
+    if ( $issue && $issue->borrowernumber eq $patron->borrowernumber ){
 
         # Already issued to current borrower.
         # If it is an on-site checkout if it can be switched to a normal checkout
@@ -850,7 +849,7 @@ sub CanBookBeIssued {
             $messages{ONSITE_CHECKOUT_WILL_BE_SWITCHED} = 1;
         } else {
             my ($CanBookBeRenewed,$renewerror) = CanBookBeRenewed(
-                $borrower->{'borrowernumber'},
+                $patron->borrowernumber,
                 $item->{'itemnumber'},
             );
             if ( $CanBookBeRenewed == 0 ) {    # no more renewals allowed
@@ -892,8 +891,8 @@ sub CanBookBeIssued {
           C4::Context->preference('SwitchOnSiteCheckouts')
       and $issue
       and $issue->onsite_checkout
-      and $issue->borrowernumber == $borrower->{'borrowernumber'} ? 1 : 0 );
-    my $toomany = TooMany( $borrower, $item->{biblionumber}, $item, { onsite_checkout => $onsite_checkout, switch_onsite_checkout => $switch_onsite_checkout, } );
+      and $issue->borrowernumber == $patron->borrowernumber ? 1 : 0 );
+    my $toomany = TooMany( $patron_unblessed, $item->{biblionumber}, $item, { onsite_checkout => $onsite_checkout, switch_onsite_checkout => $switch_onsite_checkout, } );
     # if TooMany max_allowed returns 0 the user doesn't have permission to check out this book
     if ( $toomany && not exists $needsconfirmation{RENEW_ISSUE} ) {
         if ( $toomany->{max_allowed} == 0 ) {
@@ -913,7 +912,7 @@ sub CanBookBeIssued {
     #
     # CHECKPREVCHECKOUT: CHECK IF ITEM HAS EVER BEEN LENT TO PATRON
     #
-    $patron = Koha::Patrons->find($borrower->{borrowernumber});
+    $patron = Koha::Patrons->find( $patron->borrowernumber ); # FIXME Refetch just in case, to avoid regressions. But must not be needed
     my $wants_check = $patron->wants_check_for_previous_checkout;
     $needsconfirmation{PREVISSUE} = 1
         if ($wants_check and $patron->do_check_for_previous_checkout($item));
@@ -980,8 +979,8 @@ sub CanBookBeIssued {
                 $issuingimpossible{ITEMNOTSAMEBRANCH} = 1;
                 $issuingimpossible{'itemhomebranch'} = $item->{C4::Context->preference("HomeOrHoldingBranch")};
             }
-            $needsconfirmation{BORRNOTSAMEBRANCH} = $borrower->{'branchcode'}
-              if ( $borrower->{'branchcode'} ne $userenv->{branch} );
+            $needsconfirmation{BORRNOTSAMEBRANCH} = $patron->branchcode
+              if ( $patron->branchcode ne $userenv->{branch} );
         }
     }
     #
@@ -990,7 +989,7 @@ sub CanBookBeIssued {
     my $rentalConfirmation = C4::Context->preference("RentalFeesCheckoutConfirmation");
 
     if ( $rentalConfirmation ){
-        my ($rentalCharge) = GetIssuingCharges( $item->{'itemnumber'}, $borrower->{'borrowernumber'} );
+        my ($rentalCharge) = GetIssuingCharges( $item->{'itemnumber'}, $patron->borrowernumber );
         if ( $rentalCharge > 0 ){
             $needsconfirmation{RENTALCHARGE} = $rentalCharge;
         }
@@ -1001,7 +1000,7 @@ sub CanBookBeIssued {
         my ( $restype, $res ) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
         if ($restype) {
             my $resbor = $res->{'borrowernumber'};
-            if ( $resbor ne $borrower->{'borrowernumber'} ) {
+            if ( $resbor ne $patron->borrowernumber ) {
                 my $patron = Koha::Patrons->find( $resbor );
                 if ( $restype eq "Waiting" )
                 {
@@ -1031,7 +1030,7 @@ sub CanBookBeIssued {
 
     ## CHECK AGE RESTRICTION
     my $agerestriction  = $biblioitem->{'agerestriction'};
-    my ($restriction_age, $daysToAgeRestriction) = GetAgeRestriction( $agerestriction, $borrower );
+    my ($restriction_age, $daysToAgeRestriction) = GetAgeRestriction( $agerestriction, $patron->unblessed );
     if ( $daysToAgeRestriction && $daysToAgeRestriction > 0 ) {
         if ( C4::Context->preference('AgeRestrictionOverride') ) {
             $needsconfirmation{AGE_RESTRICTION} = "$agerestriction";
@@ -1043,7 +1042,7 @@ sub CanBookBeIssued {
 
     ## check for high holds decreasing loan period
     if ( C4::Context->preference('decreaseLoanHighHolds') ) {
-        my $check = checkHighHolds( $item, $borrower );
+        my $check = checkHighHolds( $item, $patron_unblessed );
 
         if ( $check->{exceeded} ) {
             if ($override_high_holds) {
@@ -1076,9 +1075,10 @@ sub CanBookBeIssued {
         require C4::Serials;
         my $is_a_subscription = C4::Serials::CountSubscriptionFromBiblionumber($biblionumber);
         unless ($is_a_subscription) {
+            # FIXME Should be $patron->checkouts($args);
             my $checkouts = Koha::Checkouts->search(
                 {
-                    borrowernumber => $borrower->{borrowernumber},
+                    borrowernumber => $patron->borrowernumber,
                     biblionumber   => $biblionumber,
                 },
                 {
index a0aec99..e7b3bca 100644 (file)
@@ -550,7 +550,7 @@ sub GetServices {
     my $barcode = $item->{'barcode'} || '';
     $barcode = barcodedecode($barcode) if ( $barcode && C4::Context->preference('itemBarcodeInputFilter') );
     if ($barcode) {
-        my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower, $barcode );
+        my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $barcode );
 
         # TODO push @availablefor, 'loan';
     }
index e46ced1..98f4a78 100755 (executable)
@@ -315,7 +315,7 @@ if (@$barcodes) {
     my $template_params = { barcode => $barcode };
     # always check for blockers on issuing
     my ( $error, $question, $alerts, $messages ) = CanBookBeIssued(
-        $patron->unblessed,
+        $patron,
         $barcode, $datedue,
         $inprocess,
         undef,
index d6e4176..4a14858 100755 (executable)
@@ -108,10 +108,10 @@ if (C4::Context->preference('SelfCheckoutByLogin') && !$patronid) {
     ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
 }
 
-my $borrower;
+my ( $borrower, $patron );
 if ( $patronid ) {
-    $borrower = Koha::Patrons->find( { cardnumber => $patronid } );
-    $borrower = $borrower->unblessed if $borrower;
+    $patron = Koha::Patrons->find( { cardnumber => $patronid } );
+    $borrower = $patron->unblessed if $patron;
 }
 
 my $currencySymbol = "";
@@ -130,11 +130,11 @@ if ($op eq "logout") {
 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
     my ($doreturn) = AddReturn( $barcode, $branch );
 }
-elsif ( $borrower and $op eq "checkout" ) {
+elsif ( $patron and $op eq "checkout" ) {
     my $impossible  = {};
     my $needconfirm = {};
     ( $impossible, $needconfirm ) = CanBookBeIssued(
-        $borrower,
+        $patron,
         $barcode,
         undef,
         0,
index f98ffcd..570ae3c 100755 (executable)
@@ -1198,8 +1198,8 @@ subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
     my $homebranch    = $builder->build( { source => 'Branch' } );
     my $holdingbranch = $builder->build( { source => 'Branch' } );
     my $otherbranch   = $builder->build( { source => 'Branch' } );
-    my $patron_1      = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
-    my $patron_2      = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
+    my $patron_1      = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
+    my $patron_2      = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
 
     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
     my $item = $builder->build(
@@ -1218,7 +1218,7 @@ subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
 
     set_userenv($holdingbranch);
 
-    my $issue = AddIssue( $patron_1, $item->{barcode} );
+    my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
     is( ref($issue), 'Koha::Schema::Result::Issue' );    # FIXME Should be Koha::Checkout
 
     my ( $error, $question, $alerts );
@@ -1360,7 +1360,7 @@ subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
     plan tests => 8;
 
     my $library = $builder->build( { source => 'Branch' } );
-    my $patron  = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
 
     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
     my $item_1 = $builder->build(
@@ -1395,7 +1395,7 @@ subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
 
     # Patron cannot issue item_1, they have overdues
     my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
-    my $issue = AddIssue( $patron, $item_1->{barcode}, $yesterday );    # Add an overdue
+    my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday );    # Add an overdue
 
     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
@@ -1409,12 +1409,12 @@ subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
 
     # Patron cannot issue item_1, they are debarred
     my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
-    Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->{borrowernumber}, expiration => $tomorrow } );
+    Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
     is( keys(%$question) + keys(%$alerts),  0, 'No key for question and alert ' . str($error, $question, $alerts) );
     is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
 
-    Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->{borrowernumber} } );
+    Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
     is( keys(%$question) + keys(%$alerts),  0, 'No key for question and alert ' . str($error, $question, $alerts) );
     is( $error->{USERBLOCKEDNOENDDATE},    '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
@@ -1458,7 +1458,7 @@ subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
         }
     );
 
-    my ( $error, $question, $alerts ) = CanBookBeIssued( $patron->unblessed, $item_1->{barcode} );
+    my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
     is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
 
     # TODO There are other tests to provide here
@@ -1570,7 +1570,7 @@ subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
     plan tests => 5;
 
     my $library = $builder->build( { source => 'Branch' } );
-    my $patron  = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
 
     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
     my $biblionumber = $biblioitem->{biblionumber};
@@ -1600,7 +1600,7 @@ subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
     );
 
     my ( $error, $question, $alerts );
-    my $issue = AddIssue( $patron, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
+    my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
 
     t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
@@ -2021,7 +2021,7 @@ subtest 'CanBookBeIssued | is_overdue' => sub {
     my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
     my $ten_days_go  = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
     my $library = $builder->build( { source => 'Branch' } );
-    my $patron  = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
 
     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
     my $item = $builder->build(
@@ -2038,7 +2038,7 @@ subtest 'CanBookBeIssued | is_overdue' => sub {
         }
     );
 
-    my $issue = AddIssue( $patron, $item->{barcode}, $five_days_go ); # date due was 10d ago
+    my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
     my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
     is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
     my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
index 0fe338b..6c0a267 100644 (file)
@@ -42,10 +42,10 @@ my $item = $builder->build(
     }
 );
 
-my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
-my $patron = $builder->build(
+my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
+my $patron = $builder->build_object(
     {
-        source => 'Borrower',
+        class => 'Koha::Patrons',
         value => {
             patron_category => $patron_category->{categorycode},
         }
@@ -55,7 +55,7 @@ my $guarantee = $builder->build(
     {
         source => 'Borrower',
         value  => {
-            guarantorid => $patron->{borrowernumber},
+            guarantorid => $patron->borrowernumber,
             patron_category => $patron_category->{categorycode},
         }
     }
index 1cb1a65..837ed7d 100644 (file)
@@ -50,15 +50,16 @@ my $branch = $builder->build({
     source => 'Branch',
 });
 
-my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
-my $patron = $builder->build({
-    source => 'Borrower',
+my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
+my $patron = $builder->build_object({
+    class => 'Koha::Patrons',
     value => {
         branchcode => $branch->{branchcode},
         debarred => undef,
         categorycode => $patron_category->{categorycode},
     },
 });
+my $patron_unblessed = $patron->unblessed;
 
 my $biblio = $builder->build({
     source => 'Biblio',
@@ -92,12 +93,12 @@ my $issuingrule = $builder->build({
 });
 
 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
-C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
+C4::Context->set_userenv($patron->borrowernumber, $patron->userid, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
 
 t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
 
 # Add onsite checkout
-C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
+C4::Circulation::AddIssue( $patron_unblessed, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
 
 my ( $impossible, $messages );
 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 0);
@@ -108,7 +109,7 @@ t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 1);
 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
 is( exists $impossible->{TOO_MANY}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
-C4::Circulation::AddIssue( $patron, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
+C4::Circulation::AddIssue( $patron_unblessed, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
 my $issue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
 is( $issue->onsite_checkout, 0, 'The issue should have been switched to a regular checkout' );
 my $five_days_after = dt_from_string->add( days => 5 )->set( hour => 23, minute => 59, second => 0 );
@@ -128,7 +129,7 @@ my $another_item = $builder->build({
     },
 });
 
-C4::Circulation::AddIssue( $patron, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
+C4::Circulation::AddIssue( $patron_unblessed, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 1 - Switch is allowed' );
 is( exists $impossible->{TOO_MANY}, '', 'Specific case 1 - Switch is allowed' );
index 97e22a8..d81b273 100644 (file)
@@ -47,15 +47,14 @@ subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
 
 sub can_book_be_issued {
     my $item    = $builder->build( { source => 'Item' } );
-    my $patron  = $builder->build(
-        {   source => 'Borrower',
+    my $patron  = $builder->build_object(
+        {   class  => 'Koha::Patrons',
             value  => {
                 dateexpiry => '9999-12-31',
                 categorycode => $patron_category->{categorycode},
             }
         }
     );
-    $patron->{flags} = C4::Members::patronflags( $patron );
     my $duration = gettimeofday();
     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
     $duration = gettimeofday() - $duration;
@@ -63,29 +62,27 @@ sub can_book_be_issued {
     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' );
 
     $item = $builder->build( { source => 'Item' } );
-    $patron = $builder->build(
-        {   source => 'Borrower',
+    $patron = $builder->build_object(
+        {   class  => 'Koha::Patrons',
             value  => {
                 dateexpiry => '0000-00-00',
                 categorycode => $patron_category->{categorycode},
             }
         }
     );
-    $patron->{flags} = C4::Members::patronflags( $patron );
     ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
     is( $issuingimpossible->{EXPIRED}, 1, 'The patron should be considered as expired if dateexpiry is 0000-00-00' );
 
     my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) );
     $item = $builder->build( { source => 'Item' } );
-    $patron = $builder->build(
-        {   source => 'Borrower',
+    $patron = $builder->build_object(
+        {   class  => 'Koha::Patrons',
             value  => {
                 dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ),
                 categorycode => $patron_category->{categorycode},
             },
         }
     );
-    $patron->{flags} = C4::Members::patronflags( $patron );
     ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
 
index 8ebe0ca..c970d91 100755 (executable)
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use C4::Circulation;
 use Koha::Database;
-use Koha::Patron;
+use Koha::Patrons;
 use Koha::Biblio;
 use Koha::Item;
 use Koha::Holds;
@@ -184,10 +184,11 @@ is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
 
 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
 
-my ( $un, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
+my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
+my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
 
-( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
+( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
 
 $schema->storage->txn_rollback();
index 956f58a..3c70525 100644 (file)
@@ -379,7 +379,7 @@ test_it($cpvPmappings, "PostReturn");
 
 # Our Patron
 my $CBBI_patron = $builder->build({source => 'Borrower'});
-$patron = Koha::Patrons->find( $CBBI_patron->{borrowernumber} )->unblessed;
+$patron = Koha::Patrons->find( $CBBI_patron->{borrowernumber} );
 # Our Items
 my $new_item = $builder->build({
     source => 'Item',
@@ -399,7 +399,7 @@ my $prev_item = $builder->build({
 });
 # Second is Checked Out
 BAIL_OUT("CanBookBeIssued Issue failed")
-    unless AddIssue($patron, $prev_item->{barcode});
+    unless AddIssue($patron->unblessed, $prev_item->{barcode});
 
 # Mappings
 my $CBBI_mappings = [
index 75ec17f..17ff8db 100644 (file)
@@ -46,9 +46,9 @@ SKIP: {
 sub try_issue {
     my ($cardnumber, $item ) = @_;
     my $issuedate = '2011-05-16';
-    my $borrower = Koha::Patrons->find( { cardnumber => $cardnumber } )->unblessed;
-    my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued( $borrower, $item );
-    my $issue = AddIssue($borrower, $item, undef, 0, $issuedate);
+    my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
+    my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued( $patron, $item );
+    my $issue = AddIssue($patron->unblessed, $item, undef, 0, $issuedate);
     return dt_from_string( $issue->due_date() );
 }