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

(-)a/C4/Circulation.pm (-41 / +49 lines)
Lines 1643-1650 sub AddReturn { Link Here
1643
    }
1643
    }
1644
1644
1645
    # case of a return of document (deal with issues and holdingbranch)
1645
    # case of a return of document (deal with issues and holdingbranch)
1646
    if ($doreturn) {
1647
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1646
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1647
    if ($doreturn) {
1648
    my $datedue = $issue->{date_due};
1648
    my $datedue = $issue->{date_due};
1649
        $borrower or warn "AddReturn without current borrower";
1649
        $borrower or warn "AddReturn without current borrower";
1650
		my $circControlBranch;
1650
		my $circControlBranch;
Lines 1654-1660 sub AddReturn { Link Here
1654
            # FIXME: check issuedate > returndate, factoring in holidays
1654
            # FIXME: check issuedate > returndate, factoring in holidays
1655
            #$circControlBranch = _GetCircControlBranch($item,$borrower) unless ( $item->{'issuedate'} eq C4::Dates->today('iso') );;
1655
            #$circControlBranch = _GetCircControlBranch($item,$borrower) unless ( $item->{'issuedate'} eq C4::Dates->today('iso') );;
1656
            $circControlBranch = _GetCircControlBranch($item,$borrower);
1656
            $circControlBranch = _GetCircControlBranch($item,$borrower);
1657
        my $datedue = $issue->{date_due};
1658
        $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1657
        $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1659
        }
1658
        }
1660
1659
Lines 1718-1726 sub AddReturn { Link Here
1718
        my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
1717
        my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
1719
        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!";  # zero is OK, check defined
1718
        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!";  # zero is OK, check defined
1720
        
1719
        
1721
        # fix fine days
1720
        if ( $issue->{overdue} && $issue->{date_due} ) {
1722
        my $debardate = _FixFineDaysOnReturn( $borrower, $item, $issue->{date_due} );
1721
# fix fine days
1723
        $messages->{'Debarred'} = $debardate if ($debardate);
1722
            my $debardate =
1723
              _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1724
            $messages->{Debarred} = $debardate if ($debardate);
1725
        }
1724
    }
1726
    }
1725
1727
1726
    # find reserves.....
1728
    # find reserves.....
Lines 1845-1870 sub MarkIssueReturned { Link Here
1845
    $sth_del->execute($borrowernumber, $itemnumber);
1847
    $sth_del->execute($borrowernumber, $itemnumber);
1846
}
1848
}
1847
1849
1848
=head2 _FixFineDaysOnReturn
1850
=head2 _debar_user_on_return
1849
1851
1850
    &_FixFineDaysOnReturn($borrower, $item, $datedue);
1852
    _debar_user_on_return($borrower, $item, $datedue, today);
1851
1853
1852
C<$borrower> borrower hashref
1854
C<$borrower> borrower hashref
1853
1855
1854
C<$item> item hashref
1856
C<$item> item hashref
1855
1857
1856
C<$datedue> date due
1858
C<$datedue> date due DateTime object
1857
1859
1858
Internal function, called only by AddReturn that calculate and update the user fine days, and debars him
1860
C<$today> DateTime object representing the return time
1861
1862
Internal function, called only by AddReturn that calculates and updates
1863
 the user fine days, and debars him if necessary.
1864
1865
Should only be called for overdue returns
1859
1866
1860
=cut
1867
=cut
1861
1868
1862
sub _FixFineDaysOnReturn {
1869
sub _debar_user_on_return {
1863
    my ( $borrower, $item, $datedue ) = @_;
1870
    my ( $borrower, $item, $dt_due, $dt_today ) = @_;
1864
    return unless ($datedue);
1865
    
1866
    my $dt_due =  dt_from_string( $datedue );
1867
    my $dt_today = DateTime->now( time_zone => C4::Context->tz() );
1868
1871
1869
    my $branchcode = _GetCircControlBranch( $item, $borrower );
1872
    my $branchcode = _GetCircControlBranch( $item, $borrower );
1870
    my $calendar = Koha::Calendar->new( branchcode => $branchcode );
1873
    my $calendar = Koha::Calendar->new( branchcode => $branchcode );
Lines 1873-1907 sub _FixFineDaysOnReturn { Link Here
1873
    my $deltadays = $calendar->days_between( $dt_due, $dt_today );
1876
    my $deltadays = $calendar->days_between( $dt_due, $dt_today );
1874
1877
1875
    my $circcontrol = C4::Context::preference('CircControl');
1878
    my $circcontrol = C4::Context::preference('CircControl');
1876
    my $issuingrule = GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode );
1879
    my $issuingrule =
1877
    my $finedays    = $issuingrule->{finedays};
1880
      GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode );
1878
    my $unit        = $issuingrule->{lengthunit};
1881
    my $finedays = $issuingrule->{finedays};
1879
1882
    my $unit     = $issuingrule->{lengthunit};
1880
    # exit if no finedays defined
1883
1881
    return unless $finedays;
1884
    if ($finedays) {
1882
    # finedays is in days, so hourly loans must multiply by 24
1885
1883
    # thus 1 hour late equals 1 day suspension * finedays rate
1886
        # finedays is in days, so hourly loans must multiply by 24
1884
    $finedays       = $finedays * 24 if ($unit eq 'hours');
1887
        # thus 1 hour late equals 1 day suspension * finedays rate
1885
1888
        $finedays = $finedays * 24 if ( $unit eq 'hours' );
1886
    # grace period is measured in the same units as the loan
1889
1887
    my $grace = DateTime::Duration->new( $unit => $issuingrule->{firstremind} );
1890
        # grace period is measured in the same units as the loan
1888
1891
        my $grace =
1889
    if ( ( $deltadays - $grace )->is_positive ) { # you can't compare DateTime::Durations with logical operators
1892
          DateTime::Duration->new( $unit => $issuingrule->{firstremind} );
1890
        my $new_debar_dt = $dt_today->clone()->add_duration( $deltadays * $finedays );
1893
        if ( $deltadays->subtract($grace)->is_positive() ) {
1891
        my $borrower_debar_dt = dt_from_string( $borrower->{debarred} );
1894
1892
        # check to see if the current debar date is a valid date
1895
            my $new_debar_dt =
1893
        if ( $borrower->{debarred} && $borrower_debar_dt ) {
1896
              $dt_today->clone()->add_duration( $deltadays * $finedays );
1894
        # if so, is it before the new date?  update only if true
1897
            if ( $borrower->{debarred} ) {
1895
            if ( DateTime->compare( $borrower_debar_dt, $new_debar_dt ) == -1 ) {
1898
                my $borrower_debar_dt = dt_from_string( $borrower->{debarred} );
1896
                C4::Members::DebarMember( $borrower->{borrowernumber}, $new_debar_dt->ymd() );
1899
1897
                return $new_debar_dt->ymd();
1900
                # Update patron only if new date > old
1901
                if ( DateTime->compare( $borrower_debar_dt, $new_debar_dt ) !=
1902
                    -1 )
1903
                {
1904
                    return;
1905
                }
1906
1898
            }
1907
            }
1899
        # if the borrower's debar date is not set or valid, debar them
1908
            C4::Members::DebarMember( $borrower->{borrowernumber},
1900
        } else {
1909
                $new_debar_dt->ymd() );
1901
            C4::Members::DebarMember( $borrower->{borrowernumber}, $new_debar_dt->ymd() );
1902
            return $new_debar_dt->ymd();
1910
            return $new_debar_dt->ymd();
1903
        }
1911
        }
1904
    }
1912
    }
1913
    return;
1905
}
1914
}
1906
1915
1907
=head2 _FixOverduesOnReturn
1916
=head2 _FixOverduesOnReturn
1908
- 

Return to bug 8251