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

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

Return to bug 24381