@@ -, +, @@ and write-offs --- Koha/Account.pm | 28 ++++++++++++++++ Koha/Account/Offset.pm | 28 ++++++++++++++++ .../data/mysql/en/mandatory/sample_notices.sql | 5 +++ t/db_dependent/Accounts.t | 38 +++++++++++++++++++++- 4 files changed, 98 insertions(+), 1 deletion(-) --- a/Koha/Account.pm +++ a/Koha/Account.pm @@ -26,6 +26,7 @@ use List::MoreUtils qw( uniq ); use C4::Log qw( logaction ); use C4::Stats qw( UpdateStats ); +use Koha::Patrons; use Koha::Account::Lines; use Koha::Account::Offsets; use Koha::DateUtils qw( dt_from_string ); @@ -78,6 +79,8 @@ sub pay { my $userenv = C4::Context->userenv; + my $patron = Koha::Patrons->find( $self->{patron_id} ); + # We should remove accountno, it is no longer needed my $last = Koha::Account::Lines->search( { @@ -258,6 +261,31 @@ sub pay { ); } + require C4::Letters; + if ( + my $letter = C4::Letters::GetPreparedLetter( + module => 'circulation', + letter_code => uc("ACCOUNT_$type"), + message_transport_type => 'email', + lang => Koha::Patrons->find( $self->{patron_id} )->lang, + objects => { + patron => scalar Koha::Patrons->find( $self->{patron_id} ), + library => scalar Koha::Libraries->find( $self->{library_id} ), + offsets => \@account_offsets, + credit => $payment, + }, + ) + ) + { + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $self->{patron_id}, + message_transport_type => 'email', + } + ) or warn "can't enqueue letter $letter"; + } + return $payment->id; } --- a/Koha/Account/Offset.pm +++ a/Koha/Account/Offset.pm @@ -35,6 +35,34 @@ Account offsets are used to track the changes in account lines =cut +=head3 debit + +Returns the fine or fee associated with this offset. + +=cut + +sub debit { + my ( $self ) = @_; + + $self->{_debit} ||= Koha::Account::Lines->find( $self->debit_id ); + + return $self->{_debit}; +} + +=head3 credit + +Returns the payment or writeoff associated with this offset. + +=cut + +sub credit { + my ( $self ) = @_; + + $self->{_credit} ||= Koha::Account::Lines->find( $self->credit_id ); + + return $self->{_credit}; +} + =head3 _type =cut --- a/installer/data/mysql/en/mandatory/sample_notices.sql +++ a/installer/data/mysql/en/mandatory/sample_notices.sql @@ -175,3 +175,8 @@ INSERT INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, `title` ('circulation', 'AR_SLIP', '', 'Article request - print slip', 0, 'Article request', 'Article request:\r\n\r\n<> <> (<>),\r\n\r\nTitle: <>\r\nBarcode: <>\r\n\r\nArticle requested:\r\nTitle: <>\r\nAuthor: <>\r\nVolume: <>\r\nIssue: <>\r\nDate: <>\r\nPages: <>\r\nChapters: <>\r\nNotes: <>\r\n', 'print'), ('circulation', 'AR_PROCESSING', '', 'Article request - processing', 0, 'Article request processing', 'Dear <> <> (<>),\r\n\r\nWe are now processing your request for an article from <> (<>).\r\n\r\nArticle requested:\r\nTitle: <>\r\nAuthor: <>\r\nVolume: <>\r\nIssue: <>\r\nDate: <>\r\nPages: <>\r\nChapters: <>\r\nNotes: <>\r\n\r\nThank you!', 'email'), ('circulation', 'CHECKOUT_NOTE', '', 'Checkout note on item set by patron', '0', 'Checkout note', '<> <> has added a note to the item <> - <> (<>).','email'); + +INSERT INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, `title`, `content`, `message_transport_type`, `lang`) + VALUES + ('circulation', 'ACCOUNT_PAYMENT', '', 'Account Payment', 0, 'Account Payment', '[%- USE Price -%]\r\nA payment of [% credit.amount * -1 | $Price %] has been applied to your account.\r\n\r\nThis payment affected the following fees:\r\n[%- FOREACH o IN offsets %]\r\nDescription: [% o.debit.description %]\r\nAmount paid: [% o.amount * -1 | $Price %]\r\nAmount remaining: [% o.debit.amountoutstanding | $Price %]\r\n[% END %]', 'email', 'default'), + ('circulation', 'ACCOUNT_WRITEOFF', '', 'Account Writeoff', 0, 'Account Writeoff', '[%- USE Price -%]\r\nAn account writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.\r\n\r\nThis writeoff affected the following fees:\r\n[%- FOREACH o IN offsets %]\r\nDescription: [% o.debit.description %]\r\nAmount paid: [% o.amount * -1 | $Price %]\r\nAmount remaining: [% o.debit.amountoutstanding | $Price %]\r\n[% END %]', 'email', 'default'); --- a/t/db_dependent/Accounts.t +++ a/t/db_dependent/Accounts.t @@ -18,7 +18,7 @@ use Modern::Perl; -use Test::More tests => 25; +use Test::More tests => 26; use Test::MockModule; use Test::Warn; @@ -844,4 +844,40 @@ subtest "Koha::Account::non_issues_charges tests" => sub { is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" ); }; +subtest "Koha::Account::Offset tests" => sub { + + plan tests => 2; + + Koha::Account::Lines->delete(); + Koha::Patrons->delete(); + + # Create a borrower + my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; + my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; + + my $borrower = Koha::Patron->new( { + cardnumber => 'chelseahall', + surname => 'Hall', + firstname => 'Chelsea', + } ); + $borrower->categorycode( $categorycode ); + $borrower->branchcode( $branchcode ); + $borrower->store; + + my $account = Koha::Account->new({ patron_id => $borrower->id }); + + my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27 })->store(); + + my $id = $account->pay( + { + amount => 13, + } + ); + + my $offset = Koha::Account::Offsets->find( { credit_id => $id } ); + + is( $offset->credit->id, $id, 'Got correct credit for account offset' ); + is( $offset->debit->id, $line->id, 'Got correct debit for account offset' ); +}; + 1; --