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 <aleishaamohia@hotmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2016-07-12 08:30:13 +01:00 committed by Kyle M Hall
parent eff7d113a0
commit 9c6f634469
2 changed files with 27 additions and 38 deletions

View file

@ -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

View file

@ -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