From 03b90477d460b41827c4a57bbae2d3829c24d775 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 30 Oct 2018 10:21:55 +0000 Subject: [PATCH] Bug 21002: Add ->add_debit method to Koha::Account --- Koha/Account.pm | 69 +++++++++++++++++++++++++++----------- Koha/Exceptions/Account.pm | 17 ++++++++++ 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index c058f2d886..83ada67f12 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -32,6 +32,7 @@ use Koha::Patrons; use Koha::Account::Lines; use Koha::Account::Offsets; use Koha::DateUtils qw( dt_from_string ); +use Koha::Exceptions::Account; =head1 NAME @@ -340,7 +341,7 @@ sub add_credit { my $schema = Koha::Database->new->schema; - my $account_type = $Koha::Account::account_type->{$type}; + my $account_type = $Koha::Account::account_type_credit->{$type}; $account_type .= $sip if defined $sip && $type eq 'payment'; @@ -423,18 +424,19 @@ my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit( user_id => $user_id, library_id => $library_id, sip => $sip, - payment_type => $payment_type, + invoice_type => $invoice_type, type => $debit_type, item_id => $item_id } ); $debit_type can be any of: - - 'credit' - - 'payment' - - 'forgiven' - - 'lost_item_return' - - 'writeoff' + - 'fine' + - 'lost' + - 'processing' + - 'management' + - 'sundry' + - 'card' =cut @@ -442,23 +444,33 @@ sub add_debit { my ( $self, $params ) = @_; - # amount is passed as a positive value, but we store debit as negative values - my $amount = $params->{amount} * -1; + # amount should always be a positive value + my $amount = $params->{amount}; + + unless ( $amount > 0 ) { + Koha::Exceptions::Account::AmountNotPositive->throw( + error => 'Debit amount passed is not positive' + ); + } + my $description = $params->{description} // q{}; my $note = $params->{note} // q{}; my $user_id = $params->{user_id}; my $library_id = $params->{library_id}; my $sip = $params->{sip}; - my $payment_type = $params->{payment_type}; - my $type = $params->{type} || 'payment'; + my $invoice_type = $params->{invoice_type}; + my $type = $params->{type}; my $item_id = $params->{item_id}; my $schema = Koha::Database->new->schema; - my $account_type = $Koha::Account::account_type->{$type}; - $account_type .= $sip - if defined $sip && - $type eq 'payment'; + unless ( exists($Koha::Account::account_type_debit->{$type}) ) { + Koha::Exceptions::Account::UnrecognisedType->throw( + error => 'Type of debit not recognised' + ); + } + + my $account_type = $Koha::Account::account_type_debit->{$type}; my $line; @@ -477,7 +489,7 @@ sub add_debit { description => $description, accounttype => $account_type, amountoutstanding => $amount, - payment_type => $payment_type, + invoice_type => $invoice_type, note => $note, manager_id => $user_id, itemnumber => $item_id @@ -499,7 +511,7 @@ sub add_debit { borrowernumber => $self->{patron_id}, accountno => $accountno, } - ) if grep { $type eq $_ } ('payment', 'writeoff') ; + ) if grep { $type eq $_ } ('renew', 'issue', 'localuse', 'return', 'onsite_checkout' ) ; if ( C4::Context->preference("FinesLog") ) { logaction( @@ -655,11 +667,11 @@ our $offset_type = { 'writeoff' => 'Writeoff' }; -=head3 $account_type +=head3 $account_type_credit =cut -our $account_type = { +our $account_type_credit = { 'credit' => 'C', 'forgiven' => 'FOR', 'lost_item_return' => 'CR', @@ -667,8 +679,27 @@ our $account_type = { 'writeoff' => 'W' }; +=head3 $account_type_debit + +=cut + +our $account_type_debit = { + 'new_card' => 'N', + 'fine' => 'F', + 'fine_updating' => 'FU', + 'account' => 'A', + 'lost' => 'L', + 'sundry' => 'M', + 'processing' => 'PF', + 'rent' => 'R', + 'reserve' => 'Res', + 'overdue' => 'O' +}; + =head1 AUTHOR Kyle M Hall +Tomás Cohen Arazi +Martin Renvoize =cut diff --git a/Koha/Exceptions/Account.pm b/Koha/Exceptions/Account.pm index 5cafe5f9ab..5382b3e5ce 100644 --- a/Koha/Exceptions/Account.pm +++ b/Koha/Exceptions/Account.pm @@ -33,6 +33,14 @@ use Exception::Class ( 'Koha::Exceptions::Account::NoAvailableCredit' => { isa => 'Koha::Exceptions::Account', description => 'No outstanding credit' + }, + 'Koha::Exceptions::Account::AmountNotPositive' => { + isa => 'Koha::Exceptions::Account', + description => 'Amount should be a positive decimal' + }, + 'Koha::Exceptions::Account::UnrecognisedType' => { + isa => 'Koha::Exceptions::Account', + description => 'Account type was not recognised' } ); @@ -61,6 +69,15 @@ debit and it isn't. Exception to be used when a credit has no amount outstanding and is required to be applied to outstanding debits. +=head2 Koha::Exceptions::Account::AmountNotPositive + +Exception to be used when a passed credit or debit amount is not a positive +decimal value. + +=head2 Koha::Exceptions::Account::UnrecognisedType + +Exception to be used when a passed credit or debit is not of a recognised type. + =cut 1; -- 2.19.1