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

(-)a/C4/Circulation.pm (-42 / +41 lines)
Lines 1833-1843 sub AddReturn { Link Here
1833
    my $biblio   = $item_level_itypes ? undef : GetBiblioData( $item->{ biblionumber } ); # don't get bib data unless we need it
1833
    my $biblio   = $item_level_itypes ? undef : GetBiblioData( $item->{ biblionumber } ); # don't get bib data unless we need it
1834
    my $itemtype = $item_level_itypes ? $item->{itype} : $biblio->{itemtype};
1834
    my $itemtype = $item_level_itypes ? $item->{itype} : $biblio->{itemtype};
1835
1835
1836
    my $issue  = GetItemIssue($itemnumber);
1836
    my $issue  = Koha::Checkouts->find( { itemnumber => $itemnumber } );
1837
    if ($issue and $issue->{borrowernumber}) {
1837
    if ( $issue ) {
1838
        $borrower = C4::Members::GetMember( borrowernumber => $issue->{borrowernumber} )
1838
        $borrower = C4::Members::GetMember( borrowernumber => $issue->borrowernumber )
1839
            or die "Data inconsistency: barcode $barcode (itemnumber:$itemnumber) claims to be issued to non-existent borrowernumber '$issue->{borrowernumber}'\n"
1839
            or die "Data inconsistency: barcode $barcode (itemnumber:$itemnumber) claims to be issued to non-existent borrowernumber '" . $issue->borrowernumber . "'\n"
1840
                . Dumper($issue) . "\n";
1840
                . Dumper($issue->unblessed) . "\n";
1841
    } else {
1841
    } else {
1842
        $messages->{'NotIssued'} = $barcode;
1842
        $messages->{'NotIssued'} = $barcode;
1843
        # even though item is not on loan, it may still be transferred;  therefore, get current branch info
1843
        # even though item is not on loan, it may still be transferred;  therefore, get current branch info
Lines 1919-1925 sub AddReturn { Link Here
1919
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1919
    my $today = DateTime->now( time_zone => C4::Context->tz() );
1920
1920
1921
    if ($doreturn) {
1921
    if ($doreturn) {
1922
        my $datedue = $issue->{date_due};
1922
        my $is_overdue;
1923
        die "The item is not issed and cannot be returned" unless $issue; # Just in case...
1923
        $borrower or warn "AddReturn without current borrower";
1924
        $borrower or warn "AddReturn without current borrower";
1924
		my $circControlBranch;
1925
		my $circControlBranch;
1925
        if ($dropbox) {
1926
        if ($dropbox) {
Lines 1928-1938 sub AddReturn { Link Here
1928
            # FIXME: check issuedate > returndate, factoring in holidays
1929
            # FIXME: check issuedate > returndate, factoring in holidays
1929
1930
1930
            $circControlBranch = _GetCircControlBranch($item,$borrower);
1931
            $circControlBranch = _GetCircControlBranch($item,$borrower);
1931
            $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $dropboxdate ) == -1 ? 1 : 0;
1932
            $is_overdue = $issue->is_overdue( $dropboxdate );
1932
        }
1933
        }
1933
1934
1934
        if ($borrowernumber) {
1935
        if ($borrowernumber) {
1935
            if ( ( C4::Context->preference('CalculateFinesOnReturn') && $issue->{'overdue'} ) || $return_date ) {
1936
            if ( ( C4::Context->preference('CalculateFinesOnReturn') && $is_overdue ) || $return_date ) {
1936
                _CalculateAndUpdateFine( { issue => $issue, item => $item, borrower => $borrower, return_date => $return_date } );
1937
                _CalculateAndUpdateFine( { issue => $issue, item => $item, borrower => $borrower, return_date => $return_date } );
1937
            }
1938
            }
1938
1939
Lines 1954-1960 sub AddReturn { Link Here
1954
1955
1955
        }
1956
        }
1956
1957
1957
        ModItem({ onloan => undef }, $issue->{'biblionumber'}, $item->{'itemnumber'});
1958
        ModItem({ onloan => undef }, $item->{biblionumber}, $item->{'itemnumber'});
1958
    }
1959
    }
1959
1960
1960
    # the holdingbranch is updated if the document is returned to another location.
1961
    # the holdingbranch is updated if the document is returned to another location.
Lines 2014-2031 sub AddReturn { Link Here
2014
    if ($borrowernumber) {
2015
    if ($borrowernumber) {
2015
        my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
2016
        my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
2016
        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!";  # zero is OK, check defined
2017
        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!";  # zero is OK, check defined
2017
        
2018
2018
        if ( $issue->{overdue} && $issue->{date_due} ) {
2019
        if ( $issue and $issue->is_overdue ) {
2019
        # fix fine days
2020
        # fix fine days
2020
            $today = $dropboxdate if $dropbox;
2021
            $today = $dropboxdate if $dropbox;
2021
            my ($debardate,$reminder) = _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
2022
            my ($debardate,$reminder) = _debar_user_on_return( $borrower, $item, dt_from_string($issue->date_due), $today );
2022
            if ($reminder){
2023
            if ($reminder){
2023
                $messages->{'PrevDebarred'} = $debardate;
2024
                $messages->{'PrevDebarred'} = $debardate;
2024
            } else {
2025
            } else {
2025
                $messages->{'Debarred'} = $debardate if $debardate;
2026
                $messages->{'Debarred'} = $debardate if $debardate;
2026
            }
2027
            }
2027
        # there's no overdue on the item but borrower had been previously debarred
2028
        # there's no overdue on the item but borrower had been previously debarred
2028
        } elsif ( $issue->{date_due} and $borrower->{'debarred'} ) {
2029
        } elsif ( $issue->date_due and $borrower->{'debarred'} ) {
2029
             if ( $borrower->{debarred} eq "9999-12-31") {
2030
             if ( $borrower->{debarred} eq "9999-12-31") {
2030
                $messages->{'ForeverDebarred'} = $borrower->{'debarred'};
2031
                $messages->{'ForeverDebarred'} = $borrower->{'debarred'};
2031
             } else {
2032
             } else {
Lines 2613-2622 sub CanBookBeRenewed { Link Here
2613
    my $renews = 1;
2614
    my $renews = 1;
2614
2615
2615
    my $item      = GetItem($itemnumber)      or return ( 0, 'no_item' );
2616
    my $item      = GetItem($itemnumber)      or return ( 0, 'no_item' );
2616
    my $itemissue = GetItemIssue($itemnumber) or return ( 0, 'no_checkout' );
2617
    my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } ) or return ( 0, 'no_checkout' );
2617
    return ( 0, 'onsite_checkout' ) if $itemissue->{onsite_checkout};
2618
    return ( 0, 'onsite_checkout' ) if $issue->onsite_checkout;
2618
2619
2619
    $borrowernumber ||= $itemissue->{borrowernumber};
2620
    $borrowernumber ||= $issue->borrowernumber;
2620
    my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber )
2621
    my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber )
2621
      or return;
2622
      or return;
2622
2623
Lines 2698-2704 sub CanBookBeRenewed { Link Here
2698
    );
2699
    );
2699
2700
2700
    return ( 0, "too_many" )
2701
    return ( 0, "too_many" )
2701
      if not $issuing_rule or $issuing_rule->renewalsallowed <= $itemissue->{renewals};
2702
      if not $issuing_rule or $issuing_rule->renewalsallowed <= $issue->renewals;
2702
2703
2703
    my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
2704
    my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
2704
    my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
2705
    my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
Lines 2708-2723 sub CanBookBeRenewed { Link Here
2708
2709
2709
    if ( $restricted and $restrictionblockrenewing ) {
2710
    if ( $restricted and $restrictionblockrenewing ) {
2710
        return ( 0, 'restriction');
2711
        return ( 0, 'restriction');
2711
    } elsif ( ($hasoverdues and $overduesblockrenewing eq 'block') || ($itemissue->{overdue} and $overduesblockrenewing eq 'blockitem') ) {
2712
    } elsif ( ($hasoverdues and $overduesblockrenewing eq 'block') || ($issue->is_overdue and $overduesblockrenewing eq 'blockitem') ) {
2712
        return ( 0, 'overdue');
2713
        return ( 0, 'overdue');
2713
    }
2714
    }
2714
2715
2715
    if ( $itemissue->{auto_renew} ) {
2716
    if ( $issue->auto_renew ) {
2716
        if ( defined $issuing_rule->no_auto_renewal_after
2717
        if ( defined $issuing_rule->no_auto_renewal_after
2717
                and $issuing_rule->no_auto_renewal_after ne "" ) {
2718
                and $issuing_rule->no_auto_renewal_after ne "" ) {
2718
            # Get issue_date and add no_auto_renewal_after
2719
            # Get issue_date and add no_auto_renewal_after
2719
            # If this is greater than today, it's too late for renewal.
2720
            # If this is greater than today, it's too late for renewal.
2720
            my $maximum_renewal_date = dt_from_string($itemissue->{issuedate});
2721
            my $maximum_renewal_date = dt_from_string($issue->issuedate, 'sql');
2721
            $maximum_renewal_date->add(
2722
            $maximum_renewal_date->add(
2722
                $issuing_rule->lengthunit => $issuing_rule->no_auto_renewal_after
2723
                $issuing_rule->lengthunit => $issuing_rule->no_auto_renewal_after
2723
            );
2724
            );
Lines 2748-2756 sub CanBookBeRenewed { Link Here
2748
    {
2749
    {
2749
2750
2750
        # Calculate soonest renewal by subtracting 'No renewal before' from due date
2751
        # Calculate soonest renewal by subtracting 'No renewal before' from due date
2751
        my $soonestrenewal =
2752
        my $soonestrenewal = dt_from_string( $issue->date_due, 'sql' )->subtract(
2752
          $itemissue->{date_due}->clone()
2753
          ->subtract(
2754
            $issuing_rule->lengthunit => $issuing_rule->norenewalbefore );
2753
            $issuing_rule->lengthunit => $issuing_rule->norenewalbefore );
2755
2754
2756
        # Depending on syspref reset the exact time, only check the date
2755
        # Depending on syspref reset the exact time, only check the date
Lines 2762-2781 sub CanBookBeRenewed { Link Here
2762
2761
2763
        if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) )
2762
        if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) )
2764
        {
2763
        {
2765
            return ( 0, "auto_too_soon" ) if $itemissue->{auto_renew};
2764
            return ( 0, "auto_too_soon" ) if $issue->auto_renew;
2766
            return ( 0, "too_soon" );
2765
            return ( 0, "too_soon" );
2767
        }
2766
        }
2768
        elsif ( $itemissue->{auto_renew} ) {
2767
        elsif ( $issue->auto_renew ) {
2769
            return ( 0, "auto_renew" );
2768
            return ( 0, "auto_renew" );
2770
        }
2769
        }
2771
    }
2770
    }
2772
2771
2773
    # Fallback for automatic renewals:
2772
    # Fallback for automatic renewals:
2774
    # If norenewalbefore is undef, don't renew before due date.
2773
    # If norenewalbefore is undef, don't renew before due date.
2775
    if ( $itemissue->{auto_renew} ) {
2774
    if ( $issue->auto_renew ) {
2776
        my $now = dt_from_string;
2775
        my $now = dt_from_string;
2777
        return ( 0, "auto_renew" )
2776
        return ( 0, "auto_renew" )
2778
          if $now >= $itemissue->{date_due};
2777
          if $now >= dt_from_string( $issue->date_due, 'sql' );
2779
        return ( 0, "auto_too_soon" );
2778
        return ( 0, "auto_too_soon" );
2780
    }
2779
    }
2781
2780
Lines 2819-2829 sub AddRenewal { Link Here
2819
    my $dbh = C4::Context->dbh;
2818
    my $dbh = C4::Context->dbh;
2820
2819
2821
    # Find the issues record for this book
2820
    # Find the issues record for this book
2822
    my $issuedata  = GetItemIssue($itemnumber);
2821
    my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } );
2823
2822
2824
    return unless ( $issuedata );
2823
    return unless $issue;
2825
2824
2826
    $borrowernumber ||= $issuedata->{borrowernumber};
2825
    $borrowernumber ||= $issue->borrowernumber;
2827
2826
2828
    if ( defined $datedue && ref $datedue ne 'DateTime' ) {
2827
    if ( defined $datedue && ref $datedue ne 'DateTime' ) {
2829
        carp 'Invalid date passed to AddRenewal.';
2828
        carp 'Invalid date passed to AddRenewal.';
Lines 2832-2839 sub AddRenewal { Link Here
2832
2831
2833
    my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber ) or return;
2832
    my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber ) or return;
2834
2833
2835
    if ( C4::Context->preference('CalculateFinesOnReturn') && $issuedata->{overdue} ) {
2834
    if ( C4::Context->preference('CalculateFinesOnReturn') && $issue->is_overdue ) {
2836
        _CalculateAndUpdateFine( { issue => $issuedata, item => $item, borrower => $borrower } );
2835
        _CalculateAndUpdateFine( { issue => $issue, item => $item, borrower => $borrower } );
2837
    }
2836
    }
2838
    _FixOverduesOnReturn( $borrowernumber, $itemnumber );
2837
    _FixOverduesOnReturn( $borrowernumber, $itemnumber );
2839
2838
Lines 2845-2858 sub AddRenewal { Link Here
2845
        my $itemtype = (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'};
2844
        my $itemtype = (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'};
2846
2845
2847
        $datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ?
2846
        $datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ?
2848
                                        dt_from_string( $issuedata->{date_due} ) :
2847
                                        dt_from_string( $issue->date_due, 'sql' ) :
2849
                                        DateTime->now( time_zone => C4::Context->tz());
2848
                                        DateTime->now( time_zone => C4::Context->tz());
2850
        $datedue =  CalcDateDue($datedue, $itemtype, _GetCircControlBranch($item, $borrower), $borrower, 'is a renewal');
2849
        $datedue =  CalcDateDue($datedue, $itemtype, _GetCircControlBranch($item, $borrower), $borrower, 'is a renewal');
2851
    }
2850
    }
2852
2851
2853
    # Update the issues record to have the new due date, and a new count
2852
    # Update the issues record to have the new due date, and a new count
2854
    # of how many times it has been renewed.
2853
    # of how many times it has been renewed.
2855
    my $renews = $issuedata->{'renewals'} + 1;
2854
    my $renews = $issue->renewals + 1;
2856
    my $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = ?
2855
    my $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = ?
2857
                            WHERE borrowernumber=? 
2856
                            WHERE borrowernumber=? 
2858
                            AND itemnumber=?"
2857
                            AND itemnumber=?"
Lines 4015-4021 sub _CalculateAndUpdateFine { Link Here
4015
    unless ($item)     { carp "No item passed in!"     && return; }
4014
    unless ($item)     { carp "No item passed in!"     && return; }
4016
    unless ($issue)    { carp "No issue passed in!"    && return; }
4015
    unless ($issue)    { carp "No issue passed in!"    && return; }
4017
4016
4018
    my $datedue = $issue->{date_due};
4017
    my $datedue = dt_from_string( $issue->date_due );
4019
4018
4020
    # we only need to calculate and change the fines if we want to do that on return
4019
    # we only need to calculate and change the fines if we want to do that on return
4021
    # Should be on for hourly loans
4020
    # Should be on for hourly loans
Lines 4023-4029 sub _CalculateAndUpdateFine { Link Here
4023
    my $control_branchcode =
4022
    my $control_branchcode =
4024
        ( $control eq 'ItemHomeLibrary' ) ? $item->{homebranch}
4023
        ( $control eq 'ItemHomeLibrary' ) ? $item->{homebranch}
4025
      : ( $control eq 'PatronLibrary' )   ? $borrower->{branchcode}
4024
      : ( $control eq 'PatronLibrary' )   ? $borrower->{branchcode}
4026
      :                                     $issue->{branchcode};
4025
      :                                     $issue->branchcode;
4027
4026
4028
    my $date_returned = $return_date ? dt_from_string($return_date) : dt_from_string();
4027
    my $date_returned = $return_date ? dt_from_string($return_date) : dt_from_string();
4029
4028
Lines 4035-4043 sub _CalculateAndUpdateFine { Link Here
4035
    if ( C4::Context->preference('finesMode') eq 'production' ) {
4034
    if ( C4::Context->preference('finesMode') eq 'production' ) {
4036
        if ( $amount > 0 ) {
4035
        if ( $amount > 0 ) {
4037
            C4::Overdues::UpdateFine({
4036
            C4::Overdues::UpdateFine({
4038
                issue_id       => $issue->{issue_id},
4037
                issue_id       => $issue->issue_id,
4039
                itemnumber     => $issue->{itemnumber},
4038
                itemnumber     => $issue->itemnumber,
4040
                borrowernumber => $issue->{borrowernumber},
4039
                borrowernumber => $issue->borrowernumber,
4041
                amount         => $amount,
4040
                amount         => $amount,
4042
                type           => $type,
4041
                type           => $type,
4043
                due            => output_pref($datedue),
4042
                due            => output_pref($datedue),
Lines 4049-4057 sub _CalculateAndUpdateFine { Link Here
4049
            # so in this case, we need to drop those fines to 0
4048
            # so in this case, we need to drop those fines to 0
4050
4049
4051
            C4::Overdues::UpdateFine({
4050
            C4::Overdues::UpdateFine({
4052
                issue_id       => $issue->{issue_id},
4051
                issue_id       => $issue->issue_id,
4053
                itemnumber     => $issue->{itemnumber},
4052
                itemnumber     => $issue->itemnumber,
4054
                borrowernumber => $issue->{borrowernumber},
4053
                borrowernumber => $issue->borrowernumber,
4055
                amount         => 0,
4054
                amount         => 0,
4056
                type           => $type,
4055
                type           => $type,
4057
                due            => output_pref($datedue),
4056
                due            => output_pref($datedue),
(-)a/circ/returns.pl (-6 / +6 lines)
Lines 187-193 if ( $query->param('reserve_id') ) { Link Here
187
my $borrower;
187
my $borrower;
188
my $returned = 0;
188
my $returned = 0;
189
my $messages;
189
my $messages;
190
my $issueinformation;
190
my $issue;
191
my $itemnumber;
191
my $itemnumber;
192
my $barcode     = $query->param('barcode');
192
my $barcode     = $query->param('barcode');
193
my $exemptfine  = $query->param('exemptfine');
193
my $exemptfine  = $query->param('exemptfine');
Lines 309-329 if ($barcode) { Link Here
309
    );
309
    );
310
310
311
    # do the return
311
    # do the return
312
    ( $returned, $messages, $issueinformation, $borrower ) =
312
    ( $returned, $messages, $issue, $borrower ) =
313
      AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
313
      AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
314
314
315
    if ($returned) {
315
    if ($returned) {
316
        my $time_now = DateTime->now( time_zone => C4::Context->tz )->truncate( to => 'minute');
316
        my $time_now = DateTime->now( time_zone => C4::Context->tz )->truncate( to => 'minute');
317
        my $duedate = $issueinformation->{date_due}->strftime('%Y-%m-%d %H:%M');
317
        my $date_due_dt = dt_from_string( $issue->date_due, 'sql' );
318
        my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
318
        $returneditems{0}      = $barcode;
319
        $returneditems{0}      = $barcode;
319
        $riborrowernumber{0}   = $borrower->{'borrowernumber'};
320
        $riborrowernumber{0}   = $borrower->{'borrowernumber'};
320
        $riduedate{0}          = $duedate;
321
        $riduedate{0}          = $duedate;
321
        $input{borrowernumber} = $borrower->{'borrowernumber'};
322
        $input{borrowernumber} = $borrower->{'borrowernumber'};
322
        $input{duedate}        = $duedate;
323
        $input{duedate}        = $duedate;
323
        unless ( $dropboxmode ) {
324
        unless ( $dropboxmode ) {
324
            $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, DateTime->now()) == -1);
325
            $input{return_overdue} = 1 if (DateTime->compare($date_due_dt, DateTime->now()) == -1);
325
        } else {
326
        } else {
326
            $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, $dropboxdate) == -1);
327
            $input{return_overdue} = 1 if (DateTime->compare($date_due_dt, $dropboxdate) == -1);
327
        }
328
        }
328
        push( @inputloop, \%input );
329
        push( @inputloop, \%input );
329
330
330
- 

Return to bug 17680