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

(-)a/Koha/Account.pm (-1 / +92 lines)
Lines 485-490 sub add_credit { Link Here
485
    return $line;
485
    return $line;
486
}
486
}
487
487
488
=head3 offset_amount
489
490
    my $credit = $account->offset_amount(
491
        {
492
            amount          => $amount,
493
            credit_type     => $credit_type,
494
            payment_type    => $payment_type,
495
            cash_register   => $register_id,
496
            interface       => $interface,
497
            library_id      => $branchcode,
498
            user_id         => $staff_id,
499
            debts           => $debit_lines,
500
            description     => $description,
501
            note            => $note
502
        }
503
    );
504
505
This method allows an amount to be offset into a patrons account and immediately applied against debts.
506
507
You can optionally pass a debts parameter which consists of an arrayref of Koha::Account::Line debit lines.
508
509
$credit_type can be any of:
510
  - 'PAYMENT'
511
  - 'WRITEOFF'
512
  - 'FORGIVEN'
513
514
=cut
515
516
sub offset_amount {
517
    my ( $self, $params ) = @_;
518
519
    # check for mandatory params
520
    my @mandatory = ( 'interface', 'amount', 'credit_type' );
521
    for my $param (@mandatory) {
522
        unless ( defined( $params->{$param} ) ) {
523
            Koha::Exceptions::MissingParameter->throw(
524
                error => "The $param parameter is mandatory" );
525
        }
526
    }
527
528
    # amount should always be passed as a positive value
529
    my $amount = $params->{amount} * -1;
530
    unless ( $amount < 0 ) {
531
        Koha::Exceptions::Account::AmountNotPositive->throw(
532
            error => 'Debit amount passed is not positive' );
533
    }
534
535
    my $credit;
536
    my $schema = Koha::Database->new->schema;
537
    try {
538
        $schema->txn_do(
539
            sub {
540
541
                # Add payin credit
542
                $credit = $self->add_credit($params);
543
544
                # Offset debts passed first
545
                if ( exists( $params->{debts} ) ) {
546
                    $credit->apply(
547
                        {
548
                            debits      => $params->{debts},
549
                            offset_type => $params->{credit_type}
550
                        }
551
                    );
552
                    $credit->discard_changes;
553
                }
554
555
                # Offset against remaining balance if AutoReconcile
556
                $credit->apply(
557
                    {
558
                        debits      => [ $self->outstanding_debits->as_list ],
559
                        offset_type => $params->{credit_type}
560
                    }
561
                ) if ( C4::Context->preference("AccountAutoReconcile") );
562
            }
563
        );
564
    }
565
    catch {
566
        if ( ref($_) eq 'Koha::Exceptions::Object::FKConstraint' ) {
567
            if ( $_->broken_fk eq 'credit_type_code' ) {
568
                Koha::Exceptions::Account::UnrecognisedType->throw(
569
                    error => 'Type of credit not recognised' );
570
            }
571
            else {
572
                $_->rethrow;
573
            }
574
        }
575
    };
576
577
    return $credit->discard_changes;
578
}
579
488
=head3 add_debit
580
=head3 add_debit
489
581
490
This method allows adding debits to a patron's account
582
This method allows adding debits to a patron's account
491
- 

Return to bug 27636