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

(-)a/C4/Circulation.pm (-93 lines)
Lines 1490-1500 sub AddIssue { Link Here
1490
                UpdateTotalIssues( $item_object->biblionumber, 1 );
1490
                UpdateTotalIssues( $item_object->biblionumber, 1 );
1491
            }
1491
            }
1492
1492
1493
            ## If item was lost, it has now been found, reverse any list item charges if necessary.
1494
            if ( $item_object->itemlost ) {
1495
                $item_object->set_found;
1496
            }
1497
1498
            $item_object->issues( ( $item_object->issues || 0 ) + 1);
1493
            $item_object->issues( ( $item_object->issues || 0 ) + 1);
1499
            $item_object->holdingbranch(C4::Context->userenv->{'branch'});
1494
            $item_object->holdingbranch(C4::Context->userenv->{'branch'});
1500
            $item_object->itemlost(0);
1495
            $item_object->itemlost(0);
Lines 2512-2605 sub _FixOverduesOnReturn { Link Here
2512
    return $result;
2507
    return $result;
2513
}
2508
}
2514
2509
2515
=head2 _FixAccountForLostAndFound
2516
2517
  &_FixAccountForLostAndFound($itemnumber, [$barcode]);
2518
2519
Finds the most recent lost item charge for this item and refunds the borrower
2520
appropriatly, taking into account any payments or writeoffs already applied
2521
against the charge.
2522
2523
Internal function, not exported, called only by AddReturn.
2524
2525
=cut
2526
2527
sub _FixAccountForLostAndFound {
2528
    my $itemnumber     = shift or return;
2529
    my $item_id        = @_ ? shift : $itemnumber;  # Send the barcode if you want that logged in the description
2530
2531
    my $credit;
2532
2533
    # check for charge made for lost book
2534
    my $accountlines = Koha::Account::Lines->search(
2535
        {
2536
            itemnumber      => $itemnumber,
2537
            debit_type_code => 'LOST',
2538
            status          => [ undef, { '<>' => 'FOUND' } ]
2539
        },
2540
        {
2541
            order_by => { -desc => [ 'date', 'accountlines_id' ] }
2542
        }
2543
    );
2544
2545
    return unless $accountlines->count > 0;
2546
    my $accountline     = $accountlines->next;
2547
    my $total_to_refund = 0;
2548
2549
    return unless $accountline->borrowernumber;
2550
    my $patron = Koha::Patrons->find( $accountline->borrowernumber );
2551
    return unless $patron; # Patron has been deleted, nobody to credit the return to
2552
2553
    my $account = $patron->account;
2554
2555
    # Use cases
2556
    if ( $accountline->amount > $accountline->amountoutstanding ) {
2557
        # some amount has been cancelled. collect the offsets that are not writeoffs
2558
        # this works because the only way to subtract from this kind of a debt is
2559
        # using the UI buttons 'Pay' and 'Write off'
2560
        my $credits_offsets = Koha::Account::Offsets->search({
2561
            debit_id  => $accountline->id,
2562
            credit_id => { '!=' => undef }, # it is not the debit itself
2563
            type      => { '!=' => 'Writeoff' },
2564
            amount    => { '<'  => 0 } # credits are negative on the DB
2565
        });
2566
2567
        $total_to_refund = ( $credits_offsets->count > 0 )
2568
                            ? $credits_offsets->total * -1 # credits are negative on the DB
2569
                            : 0;
2570
    }
2571
2572
    my $credit_total = $accountline->amountoutstanding + $total_to_refund;
2573
2574
    if ( $credit_total > 0 ) {
2575
        my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
2576
        $credit = $account->add_credit(
2577
            {
2578
                amount      => $credit_total,
2579
                description => 'Item found ' . $item_id,
2580
                type        => 'LOST_FOUND',
2581
                interface   => C4::Context->interface,
2582
                library_id  => $branchcode,
2583
                item_id     => $itemnumber
2584
            }
2585
        );
2586
2587
        $credit->apply( { debits => [ $accountline ] } );
2588
    }
2589
2590
    # Update the account status
2591
    $accountline->discard_changes->status('FOUND');
2592
    $accountline->store;
2593
2594
    $accountline->item->paidfor('')->store({ log_action => 0 });
2595
2596
    if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) {
2597
        $account->reconcile_balance;
2598
    }
2599
2600
    return ($credit) ? $credit->id : undef;
2601
}
2602
2603
=head2 _GetCircControlBranch
2510
=head2 _GetCircControlBranch
2604
2511
2605
   my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower);
2512
   my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower);
(-)a/Koha/Item.pm (-16 / +102 lines)
Lines 168-173 sub store { Link Here
168
            $self->permanent_location( $self->location );
168
            $self->permanent_location( $self->location );
169
        }
169
        }
170
170
171
        # If item was lost, it has now been found, reverse any list item charges if necessary.
172
        if ( exists $updated_columns{itemlost}
173
                and $self->itemlost != $updated_columns{itemlost}
174
                and $updated_columns{itemlost} >= 1 ) {
175
            $self->_set_found_trigger;
176
            $self->paidfor('');
177
        }
178
171
        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
179
        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
172
            unless $params->{skip_modzebra_update};
180
            unless $params->{skip_modzebra_update};
173
181
Lines 762-771 sub renewal_branchcode { Link Here
762
    return $branchcode;
770
    return $branchcode;
763
}
771
}
764
772
765
sub set_found {
773
=head3 _set_found_trigger
766
    my ($self, $params) = @_;
774
775
    $self->_set_found_trigger
776
777
Finds the most recent lost item charge for this item and refunds the patron
778
appropriatly, taking into account any payments or writeoffs already applied
779
against the charge.
767
780
768
    my $holdingbranch = $params->{holdingbranch} || $self->holdingbranch;
781
Internal function, not exported, called only by Koha::Item->store.
782
783
=cut
784
785
sub _set_found_trigger {
786
    my ( $self, $params ) = @_;
769
787
770
    ## If item was lost, it has now been found, reverse any list item charges if necessary.
788
    ## If item was lost, it has now been found, reverse any list item charges if necessary.
771
    my $refund = 1;
789
    my $refund = 1;
Lines 777-801 sub set_found { Link Here
777
          dt_from_string( $self->itemlost_on )->delta_days($today)
795
          dt_from_string( $self->itemlost_on )->delta_days($today)
778
          ->in_units('days');
796
          ->in_units('days');
779
797
780
        $refund = 0 unless ( $lost_age_in_days < $no_refund_after_days );
798
        return $self unless $lost_age_in_days < $no_refund_after_days;
799
    }
800
801
    return $self
802
      unless Koha::CirculationRules->get_lostreturn_policy(
803
        {
804
            current_branch => C4::Context->userenv->{branch},
805
            item           => $self,
806
        }
807
      );
808
809
    # check for charge made for lost book
810
    my $accountlines = Koha::Account::Lines->search(
811
        {
812
            itemnumber      => $self->itemnumber,
813
            debit_type_code => 'LOST',
814
            status          => [ undef, { '<>' => 'FOUND' } ]
815
        },
816
        {
817
            order_by => { -desc => [ 'date', 'accountlines_id' ] }
818
        }
819
    );
820
821
    return $self unless $accountlines->count > 0;
822
823
    my $accountline     = $accountlines->next;
824
    my $total_to_refund = 0;
825
826
    return $self unless $accountline->borrowernumber;
827
828
    my $patron = Koha::Patrons->find( $accountline->borrowernumber );
829
    return $self
830
      unless $patron;  # Patron has been deleted, nobody to credit the return to
831
                       # FIXME Should not we notify this somehwere
832
833
    my $account = $patron->account;
834
835
    # Use cases
836
    if ( $accountline->amount > $accountline->amountoutstanding ) {
837
838
    # some amount has been cancelled. collect the offsets that are not writeoffs
839
    # this works because the only way to subtract from this kind of a debt is
840
    # using the UI buttons 'Pay' and 'Write off'
841
        my $credits_offsets = Koha::Account::Offsets->search(
842
            {
843
                debit_id  => $accountline->id,
844
                credit_id => { '!=' => undef },     # it is not the debit itself
845
                type      => { '!=' => 'Writeoff' },
846
                amount => { '<' => 0 }    # credits are negative on the DB
847
            }
848
        );
849
850
        $total_to_refund = ( $credits_offsets->count > 0 )
851
          ? $credits_offsets->total * -1    # credits are negative on the DB
852
          : 0;
781
    }
853
    }
782
854
783
    my $refunded;
855
    my $credit_total = $accountline->amountoutstanding + $total_to_refund;
784
    if (
856
785
        $refund
857
    my $credit;
786
        && Koha::CirculationRules->get_lostreturn_policy(
858
    if ( $credit_total > 0 ) {
859
        my $branchcode =
860
          C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
861
        $credit = $account->add_credit(
787
            {
862
            {
788
                current_branch => C4::Context->userenv->{branch},
863
                amount      => $credit_total,
789
                item           => $self,
864
                description => 'Item found ' . $item_id,
865
                type        => 'LOST_FOUND',
866
                interface   => C4::Context->interface,
867
                library_id  => $branchcode,
868
                item_id     => $itemnumber
790
            }
869
            }
791
        )
870
        );
792
      )
871
793
    {
872
        $credit->apply( { debits => [$accountline] } );
794
        C4::Circulation::_FixAccountForLostAndFound( $self->itemnumber, $self->barcode );
795
        $refunded = 1;
796
    }
873
    }
797
874
798
    return $refunded;
875
    # Update the account status
876
    $accountline->discard_changes->status('FOUND')
877
      ; # FIXME JD Why discard_changes? $accountline has not been modified since last fetch
878
    $accountline->store;
879
880
    if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) {
881
        $account->reconcile_balance;
882
    }
883
884
    return $self;
799
}
885
}
800
886
801
=head3 to_api_mapping
887
=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