Lines 627-632
sub add_debit {
Link Here
|
627 |
return $line; |
627 |
return $line; |
628 |
} |
628 |
} |
629 |
|
629 |
|
|
|
630 |
=head3 payout_amount |
631 |
|
632 |
my $debit = $account->payout_amount( |
633 |
{ |
634 |
payout_type => $payout_type, |
635 |
register_id => $register_id, |
636 |
staff_id => $staff_id, |
637 |
interface => 'intranet', |
638 |
amount => $amount, |
639 |
credits => $credit_lines |
640 |
} |
641 |
); |
642 |
|
643 |
This method allows an amount to be paid out from a patrons account against outstanding credits. |
644 |
|
645 |
$payout_type can be any of the defined payment_types: |
646 |
|
647 |
=cut |
648 |
|
649 |
sub payout_amount { |
650 |
my ( $self, $params ) = @_; |
651 |
|
652 |
# Check for mandatory parameters |
653 |
my @mandatory = |
654 |
( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' ); |
655 |
for my $param (@mandatory) { |
656 |
unless ( defined( $params->{$param} ) ) { |
657 |
Koha::Exceptions::MissingParameter->throw( |
658 |
error => "The $param parameter is mandatory" ); |
659 |
} |
660 |
} |
661 |
|
662 |
# amount should always be passed as a positive value |
663 |
my $amount = $params->{amount}; |
664 |
unless ( $amount > 0 ) { |
665 |
Koha::Exceptions::Account::AmountNotPositive->throw( |
666 |
error => 'Debit amount passed is not positive' ); |
667 |
} |
668 |
|
669 |
# amount should always be less than or equal to outstanding credit |
670 |
my $outstanding = 0; |
671 |
my $outstanding_credits = |
672 |
exists( $params->{credits} ) |
673 |
? $params->{credits} |
674 |
: $self->outstanding_credits->as_list; |
675 |
for my $credit ( @{$outstanding_credits} ) { |
676 |
$outstanding += $credit->amountoutstanding; |
677 |
} |
678 |
$outstanding = $outstanding * -1; |
679 |
Koha::Exceptions::ParameterTooHigh->throw( error => |
680 |
"Amount to payout ($amount) is higher than amountoutstanding ($outstanding)" |
681 |
) unless ( $outstanding >= $amount ); |
682 |
|
683 |
my $payout; |
684 |
my $schema = Koha::Database->new->schema; |
685 |
$schema->txn_do( |
686 |
sub { |
687 |
|
688 |
# A 'payout' is a 'debit' |
689 |
$payout = Koha::Account::Line->new( |
690 |
{ |
691 |
date => \'NOW()', |
692 |
amount => $amount, |
693 |
debit_type_code => 'PAYOUT', |
694 |
payment_type => $params->{payout_type}, |
695 |
amountoutstanding => $amount, |
696 |
manager_id => $params->{staff_id}, |
697 |
borrowernumber => $self->{patron_id}, |
698 |
interface => $params->{interface}, |
699 |
branchcode => $params->{branch}, |
700 |
register_id => $params->{cash_register} |
701 |
} |
702 |
)->store(); |
703 |
|
704 |
# Offset against credits |
705 |
for my $credit ( @{$outstanding_credits} ) { |
706 |
$credit->apply( |
707 |
{ debits => [$payout], offset_type => 'PAYOUT' } ); |
708 |
$payout->discard_changes; |
709 |
} |
710 |
|
711 |
# Set payout as paid |
712 |
$payout->status('PAID')->store; |
713 |
} |
714 |
); |
715 |
|
716 |
return $payout->discard_changes; |
717 |
} |
718 |
|
630 |
=head3 balance |
719 |
=head3 balance |
631 |
|
720 |
|
632 |
my $balance = $self->balance |
721 |
my $balance = $self->balance |
633 |
- |
|
|