View | Details | Raw Unified | Return to bug 24300
Collapse All | Expand All

(-)a/Koha/Account.pm (-1 / +88 lines)
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} * -1;
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
    Koha::Exceptions::ParameterTooHigh->throw( error =>
679
"Amount to payout ($amount) is higher than amountoutstanding ($outstanding)"
680
    ) unless ( $outstanding >= $amount );
681
682
    my $payout;
683
    my $schema = Koha::Database->new->schema;
684
    $schema->txn_do(
685
        sub {
686
687
            # A 'payout' is a 'debit'
688
            $payout = Koha::Account::Line->new(
689
                {
690
                    date              => \'NOW()',
691
                    amount            => $amount,
692
                    debit_type_code   => 'PAYOUT',
693
                    payment_type      => $params->{payout_type},
694
                    amountoutstanding => $amount,
695
                    manager_id        => $params->{staff_id},
696
                    borrowernumber    => $self->borrowernumber,
697
                    interface         => $params->{interface},
698
                    branchcode        => $params->{branch},
699
                    register_id       => $params->{cash_register}
700
                }
701
            )->store();
702
703
            # Offset against credits
704
            for my $credit ( @{$outstanding_credits} ) {
705
                $credit->apply(
706
                    { debits => [$payout], offset_type => 'PAYOUT' } );
707
                $payout->discard_changes;
708
            }
709
710
            # Set payout as paid
711
            $payout->status('PAID')->store;
712
        }
713
    );
714
715
    return $payout->discard_changes;
716
}
717
630
=head3 balance
718
=head3 balance
631
719
632
my $balance = $self->balance
720
my $balance = $self->balance
633
- 

Return to bug 24300