From c39915c5c10ff1b63939b11b21cce3c3395ddefd 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 --- C4/Members.pm | 48 +++++++++--------------------------------------- Koha/Patron.pm | 22 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index e5aa463..58109ab 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -590,19 +590,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; } } @@ -690,8 +690,8 @@ sub AddMember { $new_member->{checkprevcheckout} ||= 'inherit'; delete $new_member->{borrowernumber}; - my $rs = $schema->resultset('Borrower'); - $data{borrowernumber} = $rs->create($new_member)->id; + my $patron = Koha::Patron->new($new_member)->store; + $data{borrowernumber} = $patron->id; # If NorwegianPatronDBEnable is enabled, we set syncstatus to something that a # cronjob will use for syncing with NL @@ -705,10 +705,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}; } @@ -1968,35 +1967,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 HasOverdues =cut diff --git a/Koha/Patron.pm b/Koha/Patron.pm index fc5496f..4ec1474 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -229,12 +229,32 @@ 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' ); } + +=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.8.1