From 9c6f634469de4364d71bc0eb7520d04ead61fd00 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 Jul 2016 08:30:13 +0100 Subject: [PATCH] Bug 16912: Koha::Patrons - Move AddEnrolmentFeeIfNeeded to ->add_enrolment_fee_if_needed This patch moves the code of the C4::Members::AddEnrolmentFeeIfNeeded subroutine to the Koha::Patron->add_enrolment_fee_if_needed method. The behavior should be unchanged. Test plan: 1/ Define enrolment fee for 2 patron categories 2/ Create a patron using the first category => The patron should be charged 3/ Change the patron category to the other one => The patron should be charged again Signed-off-by: Aleisha Amohia Signed-off-by: Nick Clemens Signed-off-by: Kyle M Hall --- C4/Members.pm | 44 +++++++------------------------------------- Koha/Patron.pm | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 38 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index d7556b759f..8d3c3cffe7 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -535,19 +535,19 @@ sub ModMember { $new_borrower->{dateexpiry} ||= undef if exists $new_borrower->{dateexpiry}; $new_borrower->{debarred} ||= undef if exists $new_borrower->{debarred}; $new_borrower->{sms_provider_id} ||= undef if exists $new_borrower->{sms_provider_id}; + $new_borrower->{guarantorid} ||= undef if exists $new_borrower->{guarantorid}; - my $rs = $schema->resultset('Borrower')->search({ - borrowernumber => $new_borrower->{borrowernumber}, - }); + my $patron = Koha::Patrons->find( $new_borrower->{borrowernumber} ); delete $new_borrower->{userid} if exists $new_borrower->{userid} and not $new_borrower->{userid}; - my $execute_success = $rs->update($new_borrower); - if ($execute_success ne '0E0') { # only proceed if the update was a success + my $execute_success = $patron->store if $patron->set($new_borrower); + + if ($execute_success) { # only proceed if the update was a success # If the patron changes to a category with enrollment fee, we add a fee if ( $data{categorycode} and $data{categorycode} ne $old_categorycode ) { if ( C4::Context->preference('FeeOnChangePatronCategory') ) { - AddEnrolmentFeeIfNeeded( $data{categorycode}, $data{borrowernumber} ); + $patron->add_enrolment_fee_if_needed; } } @@ -651,10 +651,9 @@ sub AddMember { }); } - # mysql_insertid is probably bad. not necessarily accurate and mysql-specific at best. logaction("MEMBERS", "CREATE", $data{'borrowernumber'}, "") if C4::Context->preference("BorrowersLog"); - AddEnrolmentFeeIfNeeded( $data{categorycode}, $data{borrowernumber} ); + $patron->add_enrolment_fee_if_needed; return $data{borrowernumber}; } @@ -1680,35 +1679,6 @@ sub AddMember_Opac { return ( $borrowernumber, $borrower{'password'} ); } -=head2 AddEnrolmentFeeIfNeeded - - AddEnrolmentFeeIfNeeded( $borrower->{categorycode}, $borrower->{borrowernumber} ); - -Add enrolment fee for a patron if needed. - -=cut - -sub AddEnrolmentFeeIfNeeded { - my ( $categorycode, $borrowernumber ) = @_; - # check for enrollment fee & add it if needed - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare(q{ - SELECT enrolmentfee - FROM categories - WHERE categorycode=? - }); - $sth->execute( $categorycode ); - if ( $sth->err ) { - warn sprintf('Database returned the following error: %s', $sth->errstr); - return; - } - my ($enrolmentfee) = $sth->fetchrow; - if ($enrolmentfee && $enrolmentfee > 0) { - # insert fee in patron debts - C4::Accounts::manualinvoice( $borrowernumber, '', '', 'A', $enrolmentfee ); - } -} - =head2 DeleteExpiredOpacRegistrations Delete accounts that haven't been upgraded from the 'temporary' category diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 24daaeeb96..2e08f335dd 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -305,7 +305,7 @@ sub renew_account { $self->dateexpiry($expiry_date)->store; - C4::Members::AddEnrolmentFeeIfNeeded( $self->categorycode, $self->borrowernumber ); + $self->add_enrolment_fee_if_needed; logaction( "MEMBERS", "RENEW", $self->borrowernumber, "Membership renewed" ) if C4::Context->preference("BorrowersLog"); return dt_from_string( $expiry_date )->truncate( to => 'day' ); @@ -426,6 +426,25 @@ sub article_requests_finished { return $self->{_article_requests_finished}; } +=head3 add_enrolment_fee_if_needed + +my $enrolment_fee = $patron->add_enrolment_fee_if_needed; + +Add enrolment fee for a patron if needed. + +=cut + +sub add_enrolment_fee_if_needed { + my ($self) = @_; + my $patron_category = Koha::Patron::Categories->find( $self->categorycode ); + my $enrolment_fee = $patron_category->enrolmentfee; + if ( $enrolment_fee && $enrolment_fee > 0 ) { + # insert fee in patron debts + C4::Accounts::manualinvoice( $self->borrowernumber, '', '', 'A', $enrolment_fee ); + } + return $enrolment_fee || 0; +} + =head3 type =cut -- 2.39.5