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

(-)a/C4/Circulation.pm (-87 / +148 lines)
Lines 1710-1770 patron who last borrowed the book. Link Here
1710
sub AddReturn {
1710
sub AddReturn {
1711
    my ( $barcode, $branch, $exemptfine, $dropbox ) = @_;
1711
    my ( $barcode, $branch, $exemptfine, $dropbox ) = @_;
1712
1712
1713
    if ($branch and not GetBranchDetail($branch)) {
1713
    if ( $branch and not GetBranchDetail($branch) ) {
1714
        warn "AddReturn error: branch '$branch' not found.  Reverting to " . C4::Context->userenv->{'branch'};
1714
        warn "AddReturn error: branch '$branch' not found.  Reverting to "
1715
          . C4::Context->userenv->{'branch'};
1715
        undef $branch;
1716
        undef $branch;
1716
    }
1717
    }
1717
    $branch = C4::Context->userenv->{'branch'} unless $branch;  # we trust userenv to be a safe fallback/default
1718
1719
    $branch = C4::Context->userenv->{'branch'}
1720
      unless $branch;    # we trust userenv to be a safe fallback/default
1721
1718
    my $messages;
1722
    my $messages;
1719
    my $borrower;
1723
    my $borrower;
1720
    my $biblio;
1724
    my $biblio;
1721
    my $doreturn       = 1;
1725
    my $doreturn       = 1;
1722
    my $validTransfert = 0;
1726
    my $validTransfert = 0;
1723
    my $stat_type = 'return';    
1727
    my $stat_type      = 'return';
1724
1728
1725
    # get information on item
1729
    # get information on item
1726
    my $itemnumber = GetItemnumberFromBarcode( $barcode );
1730
    my $itemnumber = GetItemnumberFromBarcode($barcode);
1727
    unless ($itemnumber) {
1731
1728
        return (0, { BadBarcode => $barcode }); # no barcode means no item or borrower.  bail out.
1732
    # no barcode means no item or borrower.  bail out.
1729
    }
1733
    return ( 0, { BadBarcode => $barcode } ) unless ($itemnumber);
1730
    my $issue  = GetItemIssue($itemnumber);
1734
1731
#   warn Dumper($iteminformation);
1735
    my $issue = GetItemIssue($itemnumber);
1732
    if ($issue and $issue->{borrowernumber}) {
1736
1733
        $borrower = C4::Members::GetMemberDetails($issue->{borrowernumber})
1737
    if ( $issue and $issue->{borrowernumber} ) {
1734
            or die "Data inconsistency: barcode $barcode (itemnumber:$itemnumber) claims to be issued to non-existant borrowernumber '$issue->{borrowernumber}'\n"
1738
        $borrower = C4::Members::GetMemberDetails( $issue->{borrowernumber} )
1735
                . Dumper($issue) . "\n";
1739
          or die
1736
    } else {
1740
            "Data inconsistency: barcode $barcode (itemnumber:$itemnumber) claims to be"
1741
          . " issued to non-existant borrowernumber '$issue->{borrowernumber}'\n"
1742
          . Dumper($issue) . "\n";
1743
    }
1744
    else {
1737
        $messages->{'NotIssued'} = $barcode;
1745
        $messages->{'NotIssued'} = $barcode;
1746
1738
        # even though item is not on loan, it may still be transferred;  therefore, get current branch info
1747
        # even though item is not on loan, it may still be transferred;  therefore, get current branch info
1739
        $doreturn = 0;
1748
        $doreturn = 0;
1749
1740
        # No issue, no borrowernumber.  ONLY if $doreturn, *might* you have a $borrower later.
1750
        # No issue, no borrowernumber.  ONLY if $doreturn, *might* you have a $borrower later.
1741
        # Record this as a local use, instead of a return, if the RecordLocalUseOnReturn is on
1751
        # Record this as a local use, instead of a return, if the RecordLocalUseOnReturn is on
1742
        if (C4::Context->preference("RecordLocalUseOnReturn")) {
1752
        if ( C4::Context->preference("RecordLocalUseOnReturn") ) {
1743
           $messages->{'LocalUse'} = 1;
1753
            $messages->{'LocalUse'} = 1;
1744
           $stat_type = 'localuse';
1754
            $stat_type = 'localuse';
1745
        }
1755
        }
1746
    }
1756
    }
1747
1757
1748
    my $item = GetItem($itemnumber) or die "GetItem($itemnumber) failed";
1758
    my $item = GetItem($itemnumber) or die "GetItem($itemnumber) failed";
1749
        # full item data, but no borrowernumber or checkout info (no issue)
1750
        # we know GetItem should work because GetItemnumberFromBarcode worked
1751
    my $hbr      = GetBranchItemRule($item->{'homebranch'}, $item->{'itype'})->{'returnbranch'} || "homebranch";
1752
        # get the proper branch to which to return the item
1753
    $hbr = $item->{$hbr} || $branch ;
1754
        # if $hbr was "noreturn" or any other non-item table value, then it should 'float' (i.e. stay at this branch)
1755
1759
1756
    my $borrowernumber = $borrower->{'borrowernumber'} || undef;    # we don't know if we had a borrower or not
1760
    # full item data, but no borrowernumber or checkout info (no issue)
1761
    # we know GetItem should work because GetItemnumberFromBarcode worked
1762
    my $hbr =
1763
      GetBranchItemRule( $item->{'homebranch'}, $item->{'itype'} )
1764
      ->{'returnbranch'}
1765
      || "homebranch";
1766
1767
    # get the proper branch to which to return the item
1768
    # if $hbr was "noreturn" or any other non-item table value, then it should 'float' (i.e. stay at this branch)
1769
    $hbr = $item->{$hbr} || $branch;
1770
1771
    my $borrowernumber = $borrower->{'borrowernumber'}
1772
      || undef;    # we don't know if we had a borrower or not
1757
1773
1758
    # check if the book is in a permanent collection....
1774
    # check if the book is in a permanent collection....
1759
    # FIXME -- This 'PE' attribute is largely undocumented.  afaict, there's no user interface that reflects this functionality.
1775
    # FIXME -- This 'PE' attribute is largely undocumented.  afaict, there's no user interface that reflects this functionality.
1760
    if ( $hbr ) {
1776
    if ($hbr) {
1761
        my $branches = GetBranches();    # a potentially expensive call for a non-feature.
1777
1778
        # a potentially expensive call for a non-feature.
1779
        my $branches = GetBranches();
1762
        $branches->{$hbr}->{PE} and $messages->{'IsPermanent'} = $hbr;
1780
        $branches->{$hbr}->{PE} and $messages->{'IsPermanent'} = $hbr;
1763
    }
1781
    }
1764
1782
1765
    # check if the return is allowed at this branch
1783
    # check if the return is allowed at this branch
1766
    my ($returnallowed, $message) = CanBookBeReturned($item, $branch);
1784
    my ( $returnallowed, $message ) = CanBookBeReturned( $item, $branch );
1767
    unless ($returnallowed){
1785
    unless ($returnallowed) {
1768
        $messages->{'Wrongbranch'} = {
1786
        $messages->{'Wrongbranch'} = {
1769
            Wrongbranch => $branch,
1787
            Wrongbranch => $branch,
1770
            Rightbranch => $message
1788
            Rightbranch => $message
Lines 1773-1779 sub AddReturn { Link Here
1773
        return ( $doreturn, $messages, $issue, $borrower );
1791
        return ( $doreturn, $messages, $issue, $borrower );
1774
    }
1792
    }
1775
1793
1776
    if ( $item->{'wthdrawn'} ) { # book has been cancelled
1794
    if ( $item->{'wthdrawn'} ) {    # book has been cancelled
1777
        $messages->{'wthdrawn'} = 1;
1795
        $messages->{'wthdrawn'} = 1;
1778
        $doreturn = 0 if C4::Context->preference("BlockReturnOfWithdrawnItems");
1796
        $doreturn = 0 if C4::Context->preference("BlockReturnOfWithdrawnItems");
1779
    }
1797
    }
Lines 1781-1792 sub AddReturn { Link Here
1781
    # case of a return of document (deal with issues and holdingbranch)
1799
    # case of a return of document (deal with issues and holdingbranch)
1782
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1800
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1783
    if ($doreturn) {
1801
    if ($doreturn) {
1784
    my $datedue = $issue->{date_due};
1802
        my $datedue = $issue->{date_due};
1785
        $borrower or warn "AddReturn without current borrower";
1803
        $borrower or warn "AddReturn without current borrower";
1786
        if ($dropbox) {
1804
        if ($dropbox) {
1805
1787
            # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
1806
            # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
1788
            # FIXME: check issuedate > returndate, factoring in holidays
1807
            # FIXME: check issuedate > returndate, factoring in holidays
1789
            $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1808
            $issue->{'overdue'} =
1809
              DateTime->compare( $issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1790
        }
1810
        }
1791
1811
1792
        if ($borrowernumber) {
1812
        if ($borrowernumber) {
Lines 1818-1873 sub AddReturn { Link Here
1818
1838
1819
        }
1839
        }
1820
1840
1821
        ModItem({ onloan => undef }, $issue->{'biblionumber'}, $item->{'itemnumber'});
1841
        ModItem(
1842
            { onloan => undef },
1843
            $issue->{'biblionumber'},
1844
            $item->{'itemnumber'}
1845
        );
1822
    }
1846
    }
1823
1847
1824
    # the holdingbranch is updated if the document is returned to another location.
1848
    # the holdingbranch is updated if the document is returned to another location.
1825
    # this is always done regardless of whether the item was on loan or not
1849
    # this is always done regardless of whether the item was on loan or not
1826
    if ($item->{'holdingbranch'} ne $branch) {
1850
    if ( $item->{'holdingbranch'} ne $branch ) {
1827
        UpdateHoldingbranch($branch, $item->{'itemnumber'});
1851
        UpdateHoldingbranch( $branch, $item->{'itemnumber'} );
1828
        $item->{'holdingbranch'} = $branch; # update item data holdingbranch too
1852
        $item->{'holdingbranch'} = $branch; # update item data holdingbranch too
1829
    }
1853
    }
1830
    ModDateLastSeen( $item->{'itemnumber'} );
1854
    ModDateLastSeen( $item->{'itemnumber'} );
1831
1855
1832
    # check if we have a transfer for this document
1856
    # check if we have a transfer for this document
1833
    my ($datesent,$frombranch,$tobranch) = GetTransfers( $item->{'itemnumber'} );
1857
    my ( $datesent, $frombranch, $tobranch ) =
1858
      GetTransfers( $item->{'itemnumber'} );
1834
1859
1835
    # if we have a transfer to do, we update the line of transfers with the datearrived
1860
    # if we have a transfer to do, we update the line of transfers with the datearrived
1836
    if ($datesent) {
1861
    if ($datesent) {
1837
        if ( $tobranch eq $branch ) {
1862
        if ( $tobranch eq $branch ) {
1838
            my $sth = C4::Context->dbh->prepare(
1863
            my $sth = C4::Context->dbh->prepare("
1839
                "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1864
                UPDATE branchtransfers 
1840
            );
1865
                SET datearrived = now() 
1866
                WHERE itemnumber= ? 
1867
                  AND datearrived IS NULL
1868
            ");
1841
            $sth->execute( $item->{'itemnumber'} );
1869
            $sth->execute( $item->{'itemnumber'} );
1870
1871
            ShelfToCart( $item->{'itemnumber'} )
1872
              if ( C4::Context->preference("ReturnToShelvingCart") );
1873
1842
            # if we have a reservation with valid transfer, we can set it's status to 'W'
1874
            # if we have a reservation with valid transfer, we can set it's status to 'W'
1843
            ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1875
            C4::Reserves::ModReserveStatus( $item->{'itemnumber'}, 'W' );
1844
            C4::Reserves::ModReserveStatus($item->{'itemnumber'}, 'W');
1876
        }
1845
        } else {
1877
        else {
1846
            $messages->{'WrongTransfer'}     = $tobranch;
1878
            $messages->{'WrongTransfer'}     = $tobranch;
1847
            $messages->{'WrongTransferItem'} = $item->{'itemnumber'};
1879
            $messages->{'WrongTransferItem'} = $item->{'itemnumber'};
1848
        }
1880
        }
1849
        $validTransfert = 1;
1881
        $validTransfert = 1;
1850
    } else {
1882
    }
1851
        ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1883
    else {
1884
        ShelfToCart( $item->{'itemnumber'} )
1885
          if ( C4::Context->preference("ReturnToShelvingCart") );
1852
    }
1886
    }
1853
1887
1854
    # fix up the accounts.....
1888
    # fix up the accounts.....
1855
    if ( $item->{'itemlost'} ) {
1889
    if ( $item->{'itemlost'} ) {
1856
        $messages->{'WasLost'} = 1;
1890
        $messages->{'WasLost'} = 1;
1857
1891
1858
        if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
1892
        if ( C4::Context->preference('RefundLostItemFeeOnReturn') ) {
1859
            _FixAccountForLostAndReturned($item->{'itemnumber'}, $borrowernumber, $barcode);    # can tolerate undef $borrowernumber
1893
            _FixAccountForLostAndReturned( $item->{'itemnumber'},
1894
                $borrowernumber, $barcode )
1895
              ;    # can tolerate undef $borrowernumber
1860
            $messages->{'LostItemFeeRefunded'} = 1;
1896
            $messages->{'LostItemFeeRefunded'} = 1;
1861
        }
1897
        }
1862
    }
1898
    }
1863
1899
1864
    # fix up the overdues in accounts...
1900
    # fix up the overdues in accounts...
1865
    if ($borrowernumber) {
1901
    if ($borrowernumber) {
1866
        my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
1902
1867
        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!";  # zero is OK, check defined
1903
        # zero is OK, check defined
1868
        
1904
        my $fix =
1905
          _FixOverduesOnReturn( $borrowernumber, $item->{itemnumber},
1906
            $exemptfine, $dropbox );
1907
1908
        warn "_FixOverduesOnReturn($borrowernumber, "
1909
          . "$item->{itemnumber}...) failed!"
1910
          unless defined($fix);
1911
1869
        if ( $issue->{overdue} && $issue->{date_due} ) {
1912
        if ( $issue->{overdue} && $issue->{date_due} ) {
1870
# fix fine days
1913
1914
            # fix fine days
1871
            my $debardate =
1915
            my $debardate =
1872
              _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1916
              _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1873
            $messages->{Debarred} = $debardate if ($debardate);
1917
            $messages->{Debarred} = $debardate if ($debardate);
Lines 1876-1932 sub AddReturn { Link Here
1876
1920
1877
    # find reserves.....
1921
    # find reserves.....
1878
    # if we don't have a reserve with the status W, we launch the Checkreserves routine
1922
    # if we don't have a reserve with the status W, we launch the Checkreserves routine
1879
    my ($resfound, $resrec);
1923
    my ( $resfound, $resrec );
1880
    ($resfound, $resrec, undef) = C4::Reserves::CheckReserves( $item->{'itemnumber'} ) unless ( $item->{'wthdrawn'} );
1924
    ( $resfound, $resrec, undef ) =
1925
      C4::Reserves::CheckReserves( $item->{'itemnumber'} )
1926
      unless ( $item->{'wthdrawn'} );
1881
    if ($resfound) {
1927
    if ($resfound) {
1882
          $resrec->{'ResFound'} = $resfound;
1928
        $resrec->{'ResFound'}   = $resfound;
1883
        $messages->{'ResFound'} = $resrec;
1929
        $messages->{'ResFound'} = $resrec;
1884
    }
1930
    }
1885
1931
1886
    # update stats?
1887
    # Record the fact that this book was returned.
1932
    # Record the fact that this book was returned.
1888
    UpdateStats(
1933
    UpdateStats( $branch, $stat_type, '0', '', $item->{'itemnumber'},
1889
        $branch, $stat_type, '0', '',
1934
        $biblio->{'itemtype'}, $borrowernumber, undef, $item->{'ccode'} );
1890
        $item->{'itemnumber'},
1891
        $biblio->{'itemtype'},
1892
        $borrowernumber, undef, $item->{'ccode'}
1893
    );
1894
1935
1895
    # Send a check-in slip. # NOTE: borrower may be undef.  probably shouldn't try to send messages then.
1936
    # Send a check-in slip. # NOTE: borrower may be undef.  probably shouldn't try to send messages then.
1896
    my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1937
    my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1897
    my %conditions = (
1938
    my %conditions        = (
1898
        branchcode   => $branch,
1939
        branchcode   => $branch,
1899
        categorycode => $borrower->{categorycode},
1940
        categorycode => $borrower->{categorycode},
1900
        item_type    => $item->{itype},
1941
        item_type    => $item->{itype},
1901
        notification => 'CHECKIN',
1942
        notification => 'CHECKIN',
1902
    );
1943
    );
1903
    if ($doreturn && $circulation_alert->is_enabled_for(\%conditions)) {
1944
    if ( $doreturn && $circulation_alert->is_enabled_for( \%conditions ) ) {
1904
        SendCirculationAlert({
1945
        SendCirculationAlert(
1905
            type     => 'CHECKIN',
1946
            {
1906
            item     => $item,
1947
                type     => 'CHECKIN',
1907
            borrower => $borrower,
1948
                item     => $item,
1908
            branch   => $branch,
1949
                borrower => $borrower,
1909
        });
1950
                branch   => $branch,
1951
            }
1952
        );
1910
    }
1953
    }
1911
    
1954
1912
    logaction("CIRCULATION", "RETURN", $borrowernumber, $item->{'itemnumber'})
1955
    logaction( "CIRCULATION", "RETURN", $borrowernumber, $item->{'itemnumber'} )
1913
        if C4::Context->preference("ReturnLog");
1956
      if C4::Context->preference("ReturnLog");
1914
    
1957
1915
    # FIXME: make this comment intelligible.
1958
    # If this logged in branch is not the holdingbranch, add the message WasTransferred
1916
    #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1959
    # Then, check to see if this item has either a reserve here or a valid transfer, 
1917
    #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1960
    # if not, then we need to send it back to the home branch.
1918
1961
    if (    ( $doreturn or $messages->{'NotIssued'} )
1919
    if (($doreturn or $messages->{'NotIssued'}) and !$resfound and ($branch ne $hbr) and not $messages->{'WrongTransfer'}){
1962
        and !$resfound
1920
        if ( C4::Context->preference("AutomaticItemReturn"    ) or
1963
        and $branch ne $hbr
1921
            (C4::Context->preference("UseBranchTransferLimits") and
1964
        and !$messages->{'WrongTransfer'} )
1922
             ! IsBranchTransferAllowed($branch, $hbr, $item->{C4::Context->preference("BranchTransferLimitsType")} )
1965
    {
1923
           )) {
1966
        if (
1924
            $debug and warn sprintf "about to call ModItemTransfer(%s, %s, %s)", $item->{'itemnumber'},$branch, $hbr;
1967
            C4::Context->preference("AutomaticItemReturn")
1925
            $debug and warn "item: " . Dumper($item);
1968
            or (
1926
            ModItemTransfer($item->{'itemnumber'}, $branch, $hbr);
1969
                C4::Context->preference("UseBranchTransferLimits")
1970
                and !IsBranchTransferAllowed(
1971
                    $branch,
1972
                    $hbr,
1973
                    $item->{ C4::Context->preference("BranchTransferLimitsType")
1974
                      }
1975
                )
1976
            )
1977
          )
1978
        {
1979
            if ($debug) {
1980
                warn sprintf "about to call ModItemTransfer(%s, %s, %s)",
1981
                  $item->{'itemnumber'}, $branch, $hbr;
1982
                warn "item: " . Dumper($item);
1983
            }
1984
1985
            ModItemTransfer( $item->{'itemnumber'}, $branch, $hbr );
1927
            $messages->{'WasTransfered'} = 1;
1986
            $messages->{'WasTransfered'} = 1;
1928
        } else {
1987
        }
1929
            $messages->{'NeedsTransfer'} = 1;   # TODO: instead of 1, specify branchcode that the transfer SHOULD go to, $item->{homebranch}
1988
        else {
1989
1990
            # TODO: instead of 1, specify branchcode that the transfer SHOULD go to, $item->{homebranch}
1991
            $messages->{'NeedsTransfer'} = 1;
1930
        }
1992
        }
1931
    }
1993
    }
1932
    return ( $doreturn, $messages, $issue, $borrower );
1994
    return ( $doreturn, $messages, $issue, $borrower );
1933
- 

Return to bug 10374