Lines 632-704
sub apply {
Link Here
|
632 |
|
632 |
|
633 |
my $schema = Koha::Database->new->schema; |
633 |
my $schema = Koha::Database->new->schema; |
634 |
|
634 |
|
635 |
$schema->txn_do( sub { |
635 |
$schema->txn_do( |
636 |
for my $debit ( @{$debits} ) { |
636 |
sub { |
|
|
637 |
for my $debit ( @{$debits} ) { |
637 |
|
638 |
|
638 |
unless ( $debit->is_debit ) { |
639 |
unless ( $debit->is_debit ) { |
639 |
Koha::Exceptions::Account::IsNotDebit->throw( |
640 |
Koha::Exceptions::Account::IsNotDebit->throw( |
640 |
error => 'Account line ' . $debit->id . 'is not a debit' |
641 |
error => 'Account line ' . $debit->id . 'is not a debit' ); |
641 |
); |
642 |
} |
642 |
} |
643 |
my $amount_to_cancel; |
643 |
my $amount_to_cancel; |
644 |
my $owed = $debit->amountoutstanding; |
644 |
my $owed = $debit->amountoutstanding; |
|
|
645 |
|
645 |
|
646 |
if ( $available_credit >= $owed ) { |
646 |
if ( $available_credit >= $owed ) { |
647 |
$amount_to_cancel = $owed; |
647 |
$amount_to_cancel = $owed; |
648 |
} |
648 |
} else { # $available_credit < $debit->amountoutstanding |
649 |
else { # $available_credit < $debit->amountoutstanding |
649 |
$amount_to_cancel = $available_credit; |
650 |
$amount_to_cancel = $available_credit; |
650 |
} |
651 |
} |
|
|
652 |
|
651 |
|
653 |
# record the account offset |
652 |
# record the account offset |
654 |
Koha::Account::Offset->new( |
653 |
Koha::Account::Offset->new( |
655 |
{ credit_id => $self->id, |
654 |
{ |
656 |
debit_id => $debit->id, |
655 |
credit_id => $self->id, |
657 |
amount => $amount_to_cancel * -1, |
656 |
debit_id => $debit->id, |
658 |
type => 'APPLY' |
657 |
amount => $amount_to_cancel * -1, |
|
|
658 |
type => 'APPLY' |
659 |
} |
660 |
)->store(); |
661 |
|
662 |
$available_credit -= $amount_to_cancel; |
663 |
|
664 |
$self->amountoutstanding( $available_credit * -1 )->store; |
665 |
$debit->amountoutstanding( $owed - $amount_to_cancel )->store; |
666 |
|
667 |
# Attempt to renew the item associated with this debit if |
668 |
# appropriate |
669 |
if ( $self->credit_type_code ne 'FORGIVEN' && $debit->is_renewable ) { |
670 |
my $outcome = $debit->renew_item( { interface => $params->{interface} } ); |
671 |
$self->add_message( |
672 |
{ |
673 |
type => 'info', |
674 |
message => 'renewal', |
675 |
payload => $outcome |
676 |
} |
677 |
) if $outcome; |
659 |
} |
678 |
} |
660 |
)->store(); |
679 |
$debit->discard_changes; # Refresh values from DB to clear floating point remainders |
661 |
|
680 |
|
662 |
$available_credit -= $amount_to_cancel; |
681 |
if ( $debit->debit_type_code |
|
|
682 |
&& $debit->debit_type_code eq 'LOST' |
683 |
&& $debit->amountoutstanding == 0 |
684 |
&& $debit->itemnumber |
685 |
&& !( $self->credit_type_code eq 'LOST_FOUND' && $self->itemnumber == $debit->itemnumber ) ) |
686 |
{ |
663 |
|
687 |
|
664 |
$self->amountoutstanding( $available_credit * -1 )->store; |
688 |
if ( C4::Context->preference('UpdateItemLostStatusWhenPaid') |
665 |
$debit->amountoutstanding( $owed - $amount_to_cancel )->store; |
689 |
&& !( $self->credit_type_code eq 'WRITEOFF' || $self->credit_type_code eq 'VOID' ) ) |
|
|
690 |
{ |
691 |
$debit->item->itemlost( C4::Context->preference('UpdateItemLostStatusWhenPaid') )->store(); |
692 |
} |
666 |
|
693 |
|
667 |
# Attempt to renew the item associated with this debit if |
694 |
if ( C4::Context->preference('UpdateItemLostStatusWhenWriteOff') |
668 |
# appropriate |
695 |
&& ( $self->credit_type_code eq 'WRITEOFF' || $self->credit_type_code eq 'VOID' ) ) |
669 |
if ( $self->credit_type_code ne 'FORGIVEN' && $debit->is_renewable ) { |
|
|
670 |
my $outcome = $debit->renew_item( { interface => $params->{interface} } ); |
671 |
$self->add_message( |
672 |
{ |
696 |
{ |
673 |
type => 'info', |
697 |
$debit->item->itemlost( C4::Context->preference('UpdateItemLostStatusWhenWriteOff') )->store(); |
674 |
message => 'renewal', |
|
|
675 |
payload => $outcome |
676 |
} |
698 |
} |
677 |
) if $outcome; |
|
|
678 |
} |
679 |
$debit->discard_changes; # Refresh values from DB to clear floating point remainders |
680 |
|
681 |
# Same logic exists in Koha::Account::pay |
682 |
if ( |
683 |
C4::Context->preference('MarkLostItemsAsReturned') =~ |
684 |
m|onpayment| |
685 |
&& $debit->debit_type_code |
686 |
&& $debit->debit_type_code eq 'LOST' |
687 |
&& $debit->amountoutstanding == 0 |
688 |
&& $debit->itemnumber |
689 |
&& !( |
690 |
$self->credit_type_code eq 'LOST_FOUND' |
691 |
&& $self->itemnumber == $debit->itemnumber |
692 |
) |
693 |
) |
694 |
{ |
695 |
C4::Circulation::ReturnLostItem( $self->borrowernumber, |
696 |
$debit->itemnumber ); |
697 |
} |
698 |
|
699 |
|
699 |
last if $available_credit == 0; |
700 |
if ( C4::Context->preference('MarkLostItemsAsReturned') =~ m|onpayment| ) { |
|
|
701 |
C4::Circulation::ReturnLostItem( |
702 |
$self->borrowernumber, |
703 |
$debit->itemnumber |
704 |
); |
705 |
} |
706 |
} |
707 |
last if $available_credit == 0; |
708 |
} |
700 |
} |
709 |
} |
701 |
}); |
710 |
); |
702 |
|
711 |
|
703 |
Koha::Patron::Debarments::del_restrictions_after_payment( { borrowernumber => $self->borrowernumber } ); |
712 |
Koha::Patron::Debarments::del_restrictions_after_payment( { borrowernumber => $self->borrowernumber } ); |
704 |
|
713 |
|