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

(-)a/C4/Circulation.pm (-87 / +147 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'} )->{'returnbranch'}
1764
      || "homebranch";
1765
1766
    # get the proper branch to which to return the item
1767
    # if $hbr was "noreturn" or any other non-item table value, then it should 'float' (i.e. stay at this branch)
1768
    $hbr = $item->{$hbr} || $branch;
1769
1770
    my $borrowernumber = $borrower->{'borrowernumber'}
1771
      || undef;    # we don't know if we had a borrower or not
1757
1772
1758
    # check if the book is in a permanent collection....
1773
    # 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.
1774
    # FIXME -- This 'PE' attribute is largely undocumented.  afaict, there's no user interface that reflects this functionality.
1760
    if ( $hbr ) {
1775
    if ($hbr) {
1761
        my $branches = GetBranches();    # a potentially expensive call for a non-feature.
1776
1777
        # a potentially expensive call for a non-feature.
1778
        my $branches = GetBranches();
1762
        $branches->{$hbr}->{PE} and $messages->{'IsPermanent'} = $hbr;
1779
        $branches->{$hbr}->{PE} and $messages->{'IsPermanent'} = $hbr;
1763
    }
1780
    }
1764
1781
1765
    # check if the return is allowed at this branch
1782
    # check if the return is allowed at this branch
1766
    my ($returnallowed, $message) = CanBookBeReturned($item, $branch);
1783
    my ( $returnallowed, $message ) = CanBookBeReturned( $item, $branch );
1767
    unless ($returnallowed){
1784
    unless ($returnallowed) {
1768
        $messages->{'Wrongbranch'} = {
1785
        $messages->{'Wrongbranch'} = {
1769
            Wrongbranch => $branch,
1786
            Wrongbranch => $branch,
1770
            Rightbranch => $message
1787
            Rightbranch => $message
Lines 1773-1779 sub AddReturn { Link Here
1773
        return ( $doreturn, $messages, $issue, $borrower );
1790
        return ( $doreturn, $messages, $issue, $borrower );
1774
    }
1791
    }
1775
1792
1776
    if ( $item->{'wthdrawn'} ) { # book has been cancelled
1793
    if ( $item->{'wthdrawn'} ) {    # book has been cancelled
1777
        $messages->{'wthdrawn'} = 1;
1794
        $messages->{'wthdrawn'} = 1;
1778
        $doreturn = 0 if C4::Context->preference("BlockReturnOfWithdrawnItems");
1795
        $doreturn = 0 if C4::Context->preference("BlockReturnOfWithdrawnItems");
1779
    }
1796
    }
Lines 1781-1792 sub AddReturn { Link Here
1781
    # case of a return of document (deal with issues and holdingbranch)
1798
    # case of a return of document (deal with issues and holdingbranch)
1782
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1799
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1783
    if ($doreturn) {
1800
    if ($doreturn) {
1784
    my $datedue = $issue->{date_due};
1801
        my $datedue = $issue->{date_due};
1785
        $borrower or warn "AddReturn without current borrower";
1802
        $borrower or warn "AddReturn without current borrower";
1786
        if ($dropbox) {
1803
        if ($dropbox) {
1804
1787
            # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
1805
            # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
1788
            # FIXME: check issuedate > returndate, factoring in holidays
1806
            # FIXME: check issuedate > returndate, factoring in holidays
1789
            $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1807
            $issue->{'overdue'} =
1808
              DateTime->compare( $issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1790
        }
1809
        }
1791
1810
1792
        if ($borrowernumber) {
1811
        if ($borrowernumber) {
Lines 1818-1873 sub AddReturn { Link Here
1818
1837
1819
        }
1838
        }
1820
1839
1821
        ModItem({ onloan => undef }, $issue->{'biblionumber'}, $item->{'itemnumber'});
1840
        ModItem(
1841
            { onloan => undef },
1842
            $issue->{'biblionumber'},
1843
            $item->{'itemnumber'}
1844
        );
1822
    }
1845
    }
1823
1846
1824
    # the holdingbranch is updated if the document is returned to another location.
1847
    # 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
1848
    # this is always done regardless of whether the item was on loan or not
1826
    if ($item->{'holdingbranch'} ne $branch) {
1849
    if ( $item->{'holdingbranch'} ne $branch ) {
1827
        UpdateHoldingbranch($branch, $item->{'itemnumber'});
1850
        UpdateHoldingbranch( $branch, $item->{'itemnumber'} );
1828
        $item->{'holdingbranch'} = $branch; # update item data holdingbranch too
1851
        $item->{'holdingbranch'} = $branch; # update item data holdingbranch too
1829
    }
1852
    }
1830
    ModDateLastSeen( $item->{'itemnumber'} );
1853
    ModDateLastSeen( $item->{'itemnumber'} );
1831
1854
1832
    # check if we have a transfer for this document
1855
    # check if we have a transfer for this document
1833
    my ($datesent,$frombranch,$tobranch) = GetTransfers( $item->{'itemnumber'} );
1856
    my ( $datesent, $frombranch, $tobranch ) =
1857
      GetTransfers( $item->{'itemnumber'} );
1834
1858
1835
    # if we have a transfer to do, we update the line of transfers with the datearrived
1859
    # if we have a transfer to do, we update the line of transfers with the datearrived
1836
    if ($datesent) {
1860
    if ($datesent) {
1837
        if ( $tobranch eq $branch ) {
1861
        if ( $tobranch eq $branch ) {
1838
            my $sth = C4::Context->dbh->prepare(
1862
            my $sth = C4::Context->dbh->prepare("
1839
                "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1863
                UPDATE branchtransfers 
1840
            );
1864
                SET datearrived = now() 
1865
                WHERE itemnumber= ? 
1866
                  AND datearrived IS NULL
1867
            ");
1841
            $sth->execute( $item->{'itemnumber'} );
1868
            $sth->execute( $item->{'itemnumber'} );
1869
1870
            ShelfToCart( $item->{'itemnumber'} )
1871
              if ( C4::Context->preference("ReturnToShelvingCart") );
1872
1842
            # if we have a reservation with valid transfer, we can set it's status to 'W'
1873
            # 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") );
1874
            C4::Reserves::ModReserveStatus( $item->{'itemnumber'}, 'W' );
1844
            C4::Reserves::ModReserveStatus($item->{'itemnumber'}, 'W');
1875
        }
1845
        } else {
1876
        else {
1846
            $messages->{'WrongTransfer'}     = $tobranch;
1877
            $messages->{'WrongTransfer'}     = $tobranch;
1847
            $messages->{'WrongTransferItem'} = $item->{'itemnumber'};
1878
            $messages->{'WrongTransferItem'} = $item->{'itemnumber'};
1848
        }
1879
        }
1849
        $validTransfert = 1;
1880
        $validTransfert = 1;
1850
    } else {
1881
    }
1851
        ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1882
    else {
1883
        ShelfToCart( $item->{'itemnumber'} )
1884
          if ( C4::Context->preference("ReturnToShelvingCart") );
1852
    }
1885
    }
1853
1886
1854
    # fix up the accounts.....
1887
    # fix up the accounts.....
1855
    if ( $item->{'itemlost'} ) {
1888
    if ( $item->{'itemlost'} ) {
1856
        $messages->{'WasLost'} = 1;
1889
        $messages->{'WasLost'} = 1;
1857
1890
1858
        if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
1891
        if ( C4::Context->preference('RefundLostItemFeeOnReturn') ) {
1859
            _FixAccountForLostAndReturned($item->{'itemnumber'}, $borrowernumber, $barcode);    # can tolerate undef $borrowernumber
1892
            _FixAccountForLostAndReturned( $item->{'itemnumber'},
1893
                $borrowernumber, $barcode )
1894
              ;    # can tolerate undef $borrowernumber
1860
            $messages->{'LostItemFeeRefunded'} = 1;
1895
            $messages->{'LostItemFeeRefunded'} = 1;
1861
        }
1896
        }
1862
    }
1897
    }
1863
1898
1864
    # fix up the overdues in accounts...
1899
    # fix up the overdues in accounts...
1865
    if ($borrowernumber) {
1900
    if ($borrowernumber) {
1866
        my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
1901
1867
        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!";  # zero is OK, check defined
1902
        # zero is OK, check defined
1868
        
1903
        my $fix =
1904
          _FixOverduesOnReturn( $borrowernumber, $item->{itemnumber},
1905
            $exemptfine, $dropbox );
1906
1907
        warn "_FixOverduesOnReturn($borrowernumber, "
1908
          . "$item->{itemnumber}...) failed!"
1909
          unless defined($fix);
1910
1869
        if ( $issue->{overdue} && $issue->{date_due} ) {
1911
        if ( $issue->{overdue} && $issue->{date_due} ) {
1870
# fix fine days
1912
1913
            # fix fine days
1871
            my $debardate =
1914
            my $debardate =
1872
              _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1915
              _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1873
            $messages->{Debarred} = $debardate if ($debardate);
1916
            $messages->{Debarred} = $debardate if ($debardate);
Lines 1876-1932 sub AddReturn { Link Here
1876
1919
1877
    # find reserves.....
1920
    # find reserves.....
1878
    # if we don't have a reserve with the status W, we launch the Checkreserves routine
1921
    # if we don't have a reserve with the status W, we launch the Checkreserves routine
1879
    my ($resfound, $resrec);
1922
    my ( $resfound, $resrec );
1880
    ($resfound, $resrec, undef) = C4::Reserves::CheckReserves( $item->{'itemnumber'} ) unless ( $item->{'wthdrawn'} );
1923
    ( $resfound, $resrec, undef ) =
1924
      C4::Reserves::CheckReserves( $item->{'itemnumber'} )
1925
      unless ( $item->{'wthdrawn'} );
1881
    if ($resfound) {
1926
    if ($resfound) {
1882
          $resrec->{'ResFound'} = $resfound;
1927
        $resrec->{'ResFound'}   = $resfound;
1883
        $messages->{'ResFound'} = $resrec;
1928
        $messages->{'ResFound'} = $resrec;
1884
    }
1929
    }
1885
1930
1886
    # update stats?
1887
    # Record the fact that this book was returned.
1931
    # Record the fact that this book was returned.
1888
    UpdateStats(
1932
    UpdateStats( $branch, $stat_type, '0', '', $item->{'itemnumber'},
1889
        $branch, $stat_type, '0', '',
1933
        $biblio->{'itemtype'}, $borrowernumber, undef, $item->{'ccode'} );
1890
        $item->{'itemnumber'},
1891
        $biblio->{'itemtype'},
1892
        $borrowernumber, undef, $item->{'ccode'}
1893
    );
1894
1934
1895
    # Send a check-in slip. # NOTE: borrower may be undef.  probably shouldn't try to send messages then.
1935
    # Send a check-in slip. # NOTE: borrower may be undef.  probably shouldn't try to send messages then.
1896
    my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1936
    my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1897
    my %conditions = (
1937
    my %conditions        = (
1898
        branchcode   => $branch,
1938
        branchcode   => $branch,
1899
        categorycode => $borrower->{categorycode},
1939
        categorycode => $borrower->{categorycode},
1900
        item_type    => $item->{itype},
1940
        item_type    => $item->{itype},
1901
        notification => 'CHECKIN',
1941
        notification => 'CHECKIN',
1902
    );
1942
    );
1903
    if ($doreturn && $circulation_alert->is_enabled_for(\%conditions)) {
1943
    if ( $doreturn && $circulation_alert->is_enabled_for( \%conditions ) ) {
1904
        SendCirculationAlert({
1944
        SendCirculationAlert(
1905
            type     => 'CHECKIN',
1945
            {
1906
            item     => $item,
1946
                type     => 'CHECKIN',
1907
            borrower => $borrower,
1947
                item     => $item,
1908
            branch   => $branch,
1948
                borrower => $borrower,
1909
        });
1949
                branch   => $branch,
1950
            }
1951
        );
1910
    }
1952
    }
1911
    
1953
1912
    logaction("CIRCULATION", "RETURN", $borrowernumber, $item->{'itemnumber'})
1954
    logaction( "CIRCULATION", "RETURN", $borrowernumber, $item->{'itemnumber'} )
1913
        if C4::Context->preference("ReturnLog");
1955
      if C4::Context->preference("ReturnLog");
1914
    
1956
1915
    # FIXME: make this comment intelligible.
1957
    # 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
1958
    # 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 .
1959
    # if not, then we need to send it back to the home branch.
1918
1960
    if (    ( $doreturn or $messages->{'NotIssued'} )
1919
    if (($doreturn or $messages->{'NotIssued'}) and !$resfound and ($branch ne $hbr) and not $messages->{'WrongTransfer'}){
1961
        and !$resfound
1920
        if ( C4::Context->preference("AutomaticItemReturn"    ) or
1962
        and $branch ne $hbr
1921
            (C4::Context->preference("UseBranchTransferLimits") and
1963
        and !$messages->{'WrongTransfer'} )
1922
             ! IsBranchTransferAllowed($branch, $hbr, $item->{C4::Context->preference("BranchTransferLimitsType")} )
1964
    {
1923
           )) {
1965
        if (
1924
            $debug and warn sprintf "about to call ModItemTransfer(%s, %s, %s)", $item->{'itemnumber'},$branch, $hbr;
1966
            C4::Context->preference("AutomaticItemReturn")
1925
            $debug and warn "item: " . Dumper($item);
1967
            or (
1926
            ModItemTransfer($item->{'itemnumber'}, $branch, $hbr);
1968
                C4::Context->preference("UseBranchTransferLimits")
1969
                and !IsBranchTransferAllowed(
1970
                    $branch,
1971
                    $hbr,
1972
                    $item->{ C4::Context->preference("BranchTransferLimitsType")
1973
                      }
1974
                )
1975
            )
1976
          )
1977
        {
1978
            if ($debug) {
1979
                warn sprintf "about to call ModItemTransfer(%s, %s, %s)",
1980
                  $item->{'itemnumber'}, $branch, $hbr;
1981
                warn "item: " . Dumper($item);
1982
            }
1983
1984
            ModItemTransfer( $item->{'itemnumber'}, $branch, $hbr );
1927
            $messages->{'WasTransfered'} = 1;
1985
            $messages->{'WasTransfered'} = 1;
1928
        } else {
1986
        }
1929
            $messages->{'NeedsTransfer'} = 1;   # TODO: instead of 1, specify branchcode that the transfer SHOULD go to, $item->{homebranch}
1987
        else {
1988
1989
            # TODO: instead of 1, specify branchcode that the transfer SHOULD go to, $item->{homebranch}
1990
            $messages->{'NeedsTransfer'} = 1;
1930
        }
1991
        }
1931
    }
1992
    }
1932
    return ( $doreturn, $messages, $issue, $borrower );
1993
    return ( $doreturn, $messages, $issue, $borrower );
1933
- 

Return to bug 10374