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

(-)a/C4/Circulation.pm (-93 lines)
Lines 1508-1518 sub AddIssue { Link Here
1508
                UpdateTotalIssues( $item_object->biblionumber, 1 );
1508
                UpdateTotalIssues( $item_object->biblionumber, 1 );
1509
            }
1509
            }
1510
1510
1511
            ## If item was lost, it has now been found, reverse any list item charges if necessary.
1512
            if ( $item_object->itemlost ) {
1513
                $item_object->set_found;
1514
            }
1515
1516
            $item_object->issues( ( $item_object->issues || 0 ) + 1);
1511
            $item_object->issues( ( $item_object->issues || 0 ) + 1);
1517
            $item_object->holdingbranch(C4::Context->userenv->{'branch'});
1512
            $item_object->holdingbranch(C4::Context->userenv->{'branch'});
1518
            $item_object->itemlost(0);
1513
            $item_object->itemlost(0);
Lines 2487-2580 sub _FixOverduesOnReturn { Link Here
2487
    return $result;
2482
    return $result;
2488
}
2483
}
2489
2484
2490
=head2 _FixAccountForLostAndFound
2491
2492
  &_FixAccountForLostAndFound($itemnumber, [$barcode]);
2493
2494
Finds the most recent lost item charge for this item and refunds the borrower
2495
appropriatly, taking into account any payments or writeoffs already applied
2496
against the charge.
2497
2498
Internal function, not exported, called only by AddReturn.
2499
2500
=cut
2501
2502
sub _FixAccountForLostAndFound {
2503
    my $itemnumber     = shift or return;
2504
    my $item_id        = @_ ? shift : $itemnumber;  # Send the barcode if you want that logged in the description
2505
2506
    my $credit;
2507
2508
    # check for charge made for lost book
2509
    my $accountlines = Koha::Account::Lines->search(
2510
        {
2511
            itemnumber      => $itemnumber,
2512
            debit_type_code => 'LOST',
2513
            status          => [ undef, { '<>' => 'FOUND' } ]
2514
        },
2515
        {
2516
            order_by => { -desc => [ 'date', 'accountlines_id' ] }
2517
        }
2518
    );
2519
2520
    return unless $accountlines->count > 0;
2521
    my $accountline     = $accountlines->next;
2522
    my $total_to_refund = 0;
2523
2524
    return unless $accountline->borrowernumber;
2525
    my $patron = Koha::Patrons->find( $accountline->borrowernumber );
2526
    return unless $patron; # Patron has been deleted, nobody to credit the return to
2527
2528
    my $account = $patron->account;
2529
2530
    # Use cases
2531
    if ( $accountline->amount > $accountline->amountoutstanding ) {
2532
        # some amount has been cancelled. collect the offsets that are not writeoffs
2533
        # this works because the only way to subtract from this kind of a debt is
2534
        # using the UI buttons 'Pay' and 'Write off'
2535
        my $credits_offsets = Koha::Account::Offsets->search({
2536
            debit_id  => $accountline->id,
2537
            credit_id => { '!=' => undef }, # it is not the debit itself
2538
            type      => { '!=' => 'Writeoff' },
2539
            amount    => { '<'  => 0 } # credits are negative on the DB
2540
        });
2541
2542
        $total_to_refund = ( $credits_offsets->count > 0 )
2543
                            ? $credits_offsets->total * -1 # credits are negative on the DB
2544
                            : 0;
2545
    }
2546
2547
    my $credit_total = $accountline->amountoutstanding + $total_to_refund;
2548
2549
    if ( $credit_total > 0 ) {
2550
        my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
2551
        $credit = $account->add_credit(
2552
            {
2553
                amount      => $credit_total,
2554
                description => 'Item found ' . $item_id,
2555
                type        => 'LOST_FOUND',
2556
                interface   => C4::Context->interface,
2557
                library_id  => $branchcode,
2558
                item_id     => $itemnumber
2559
            }
2560
        );
2561
2562
        $credit->apply( { debits => [ $accountline ] } );
2563
    }
2564
2565
    # Update the account status
2566
    $accountline->discard_changes->status('FOUND');
2567
    $accountline->store;
2568
2569
    $accountline->item->paidfor('')->store({ log_action => 0 });
2570
2571
    if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) {
2572
        $account->reconcile_balance;
2573
    }
2574
2575
    return ($credit) ? $credit->id : undef;
2576
}
2577
2578
=head2 _GetCircControlBranch
2485
=head2 _GetCircControlBranch
2579
2486
2580
   my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower);
2487
   my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower);
(-)a/Koha/Item.pm (-16 / +102 lines)
Lines 169-174 sub store { Link Here
169
            $self->permanent_location( $self->location );
169
            $self->permanent_location( $self->location );
170
        }
170
        }
171
171
172
        # If item was lost, it has now been found, reverse any list item charges if necessary.
173
        if ( exists $updated_columns{itemlost}
174
                and $self->itemlost != $updated_columns{itemlost}
175
                and $updated_columns{itemlost} >= 1 ) {
176
            $self->_set_found_trigger;
177
            $self->paidfor('');
178
        }
179
172
        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
180
        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
173
            unless $params->{skip_modzebra_update};
181
            unless $params->{skip_modzebra_update};
174
182
Lines 763-772 sub renewal_branchcode { Link Here
763
    return $branchcode;
771
    return $branchcode;
764
}
772
}
765
773
766
sub set_found {
774
=head3 _set_found_trigger
767
    my ($self, $params) = @_;
775
776
    $self->_set_found_trigger
777
778
Finds the most recent lost item charge for this item and refunds the patron
779
appropriatly, taking into account any payments or writeoffs already applied
780
against the charge.
768
781
769
    my $holdingbranch = $params->{holdingbranch} || $self->holdingbranch;
782
Internal function, not exported, called only by Koha::Item->store.
783
784
=cut
785
786
sub _set_found_trigger {
787
    my ( $self, $params ) = @_;
770
788
771
    ## If item was lost, it has now been found, reverse any list item charges if necessary.
789
    ## If item was lost, it has now been found, reverse any list item charges if necessary.
772
    my $refund = 1;
790
    my $refund = 1;
Lines 778-802 sub set_found { Link Here
778
          dt_from_string( $self->itemlost_on )->delta_days($today)
796
          dt_from_string( $self->itemlost_on )->delta_days($today)
779
          ->in_units('days');
797
          ->in_units('days');
780
798
781
        $refund = 0 unless ( $lost_age_in_days < $no_refund_after_days );
799
        return $self unless $lost_age_in_days < $no_refund_after_days;
800
    }
801
802
    return $self
803
      unless Koha::CirculationRules->get_lostreturn_policy(
804
        {
805
            current_branch => C4::Context->userenv->{branch},
806
            item           => $self,
807
        }
808
      );
809
810
    # check for charge made for lost book
811
    my $accountlines = Koha::Account::Lines->search(
812
        {
813
            itemnumber      => $self->itemnumber,
814
            debit_type_code => 'LOST',
815
            status          => [ undef, { '<>' => 'FOUND' } ]
816
        },
817
        {
818
            order_by => { -desc => [ 'date', 'accountlines_id' ] }
819
        }
820
    );
821
822
    return $self unless $accountlines->count > 0;
823
824
    my $accountline     = $accountlines->next;
825
    my $total_to_refund = 0;
826
827
    return $self unless $accountline->borrowernumber;
828
829
    my $patron = Koha::Patrons->find( $accountline->borrowernumber );
830
    return $self
831
      unless $patron;  # Patron has been deleted, nobody to credit the return to
832
                       # FIXME Should not we notify this somehwere
833
834
    my $account = $patron->account;
835
836
    # Use cases
837
    if ( $accountline->amount > $accountline->amountoutstanding ) {
838
839
    # some amount has been cancelled. collect the offsets that are not writeoffs
840
    # this works because the only way to subtract from this kind of a debt is
841
    # using the UI buttons 'Pay' and 'Write off'
842
        my $credits_offsets = Koha::Account::Offsets->search(
843
            {
844
                debit_id  => $accountline->id,
845
                credit_id => { '!=' => undef },     # it is not the debit itself
846
                type      => { '!=' => 'Writeoff' },
847
                amount => { '<' => 0 }    # credits are negative on the DB
848
            }
849
        );
850
851
        $total_to_refund = ( $credits_offsets->count > 0 )
852
          ? $credits_offsets->total * -1    # credits are negative on the DB
853
          : 0;
782
    }
854
    }
783
855
784
    my $refunded;
856
    my $credit_total = $accountline->amountoutstanding + $total_to_refund;
785
    if (
857
786
        $refund
858
    my $credit;
787
        && Koha::CirculationRules->get_lostreturn_policy(
859
    if ( $credit_total > 0 ) {
860
        my $branchcode =
861
          C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
862
        $credit = $account->add_credit(
788
            {
863
            {
789
                current_branch => C4::Context->userenv->{branch},
864
                amount      => $credit_total,
790
                item           => $self,
865
                description => 'Item found ' . $item_id,
866
                type        => 'LOST_FOUND',
867
                interface   => C4::Context->interface,
868
                library_id  => $branchcode,
869
                item_id     => $itemnumber
791
            }
870
            }
792
        )
871
        );
793
      )
872
794
    {
873
        $credit->apply( { debits => [$accountline] } );
795
        C4::Circulation::_FixAccountForLostAndFound( $self->itemnumber, $self->barcode );
796
        $refunded = 1;
797
    }
874
    }
798
875
799
    return $refunded;
876
    # Update the account status
877
    $accountline->discard_changes->status('FOUND')
878
      ; # FIXME JD Why discard_changes? $accountline has not been modified since last fetch
879
    $accountline->store;
880
881
    if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) {
882
        $account->reconcile_balance;
883
    }
884
885
    return $self;
800
}
886
}
801
887
802
=head3 to_api_mapping
888
=head3 to_api_mapping
(-)a/catalogue/updateitem.pl (-3 lines)
Lines 68-76 elsif ( $op eq "set_public_note" ) { # i.e., itemnotes parameter passed from for Link Here
68
        $item->itemnotes($itemnotes);
68
        $item->itemnotes($itemnotes);
69
    }
69
    }
70
} elsif ( $op eq "set_lost" && $itemlost ne $item_data_hashref->{'itemlost'}) {
70
} elsif ( $op eq "set_lost" && $itemlost ne $item_data_hashref->{'itemlost'}) {
71
    $item->set_found
72
        if !$itemlost && $item->itemlost && $item->itemlost ge '1';
73
74
    $item->itemlost($itemlost);
71
    $item->itemlost($itemlost);
75
} elsif ( $op eq "set_withdrawn" && $withdrawn ne $item_data_hashref->{'withdrawn'}) {
72
} elsif ( $op eq "set_withdrawn" && $withdrawn ne $item_data_hashref->{'withdrawn'}) {
76
    $item->withdrawn($withdrawn);
73
    $item->withdrawn($withdrawn);
(-)a/cataloguing/additem.pl (-3 lines)
Lines 751-758 if ($op eq "additem") { Link Here
751
        my $newitemlost = $newitem->{itemlost};
751
        my $newitemlost = $newitem->{itemlost};
752
        if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) {
752
        if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) {
753
            LostItem( $item->itemnumber, 'additem' )
753
            LostItem( $item->itemnumber, 'additem' )
754
        } elsif ( !$newitemlost && $olditemlost && $olditemlost ge '1' ) {
755
            $item->set_found;
756
        }
754
        }
757
    }
755
    }
758
    $nextop="additem";
756
    $nextop="additem";
759
- 

Return to bug 18501