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

(-)a/Koha/Account.pm (-1 / +95 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
    # Check for mandatory register
663
    Koha::Exceptions::Account::RegisterRequired->throw()
664
      if ( C4::Context->preference("UseCashRegisters")
665
        && ( $params->{payout_type} eq 'CASH' )
666
        && !defined($params->{cash_register}) );
667
668
    # Amount should always be passed as a positive value
669
    my $amount = $params->{amount};
670
    unless ( $amount > 0 ) {
671
        Koha::Exceptions::Account::AmountNotPositive->throw(
672
            error => 'Debit amount passed is not positive' );
673
    }
674
675
    # Amount should always be less than or equal to outstanding credit
676
    my $outstanding = 0;
677
    my $outstanding_credits =
678
      exists( $params->{credits} )
679
      ? $params->{credits}
680
      : $self->outstanding_credits->as_list;
681
    for my $credit ( @{$outstanding_credits} ) {
682
        $outstanding += $credit->amountoutstanding;
683
    }
684
    $outstanding = $outstanding * -1;
685
    Koha::Exceptions::ParameterTooHigh->throw( error =>
686
"Amount to payout ($amount) is higher than amountoutstanding ($outstanding)"
687
    ) unless ( $outstanding >= $amount );
688
689
    my $payout;
690
    my $schema = Koha::Database->new->schema;
691
    $schema->txn_do(
692
        sub {
693
694
            # A 'payout' is a 'debit'
695
            $payout = Koha::Account::Line->new(
696
                {
697
                    date              => \'NOW()',
698
                    amount            => $amount,
699
                    debit_type_code   => 'PAYOUT',
700
                    payment_type      => $params->{payout_type},
701
                    amountoutstanding => $amount,
702
                    manager_id        => $params->{staff_id},
703
                    borrowernumber    => $self->{patron_id},
704
                    interface         => $params->{interface},
705
                    branchcode        => $params->{branch},
706
                    register_id       => $params->{cash_register}
707
                }
708
            )->store();
709
710
            # Offset against credits
711
            for my $credit ( @{$outstanding_credits} ) {
712
                $credit->apply(
713
                    { debits => [$payout], offset_type => 'PAYOUT' } );
714
                $payout->discard_changes;
715
            }
716
717
            # Set payout as paid
718
            $payout->status('PAID')->store;
719
        }
720
    );
721
722
    return $payout;
723
}
724
630
=head3 balance
725
=head3 balance
631
726
632
my $balance = $self->balance
727
my $balance = $self->balance
633
- 

Return to bug 24300