-sub CalcDateDue {
- my ($startdate,$itemtype,$branch,$borrower) = @_;
- my $datedue;
- my $loanlength = GetLoanLength($borrower->{'categorycode'},$itemtype, $branch);
-
- # if globalDueDate ON the datedue is set to that date
- if ( C4::Context->preference('globalDueDate')
- && ( C4::Context->preference('globalDueDate') =~ C4::Dates->regexp('syspref') ) ) {
- $datedue = C4::Dates->new( C4::Context->preference('globalDueDate') );
- } else {
- # otherwise, calculate the datedue as normal
- if(C4::Context->preference('useDaysMode') eq 'Days') { # ignoring calendar
- my $timedue = time + ($loanlength) * 86400;
- #FIXME - assumes now even though we take a startdate
- my @datearr = localtime($timedue);
- $datedue = C4::Dates->new( sprintf("%04d-%02d-%02d", 1900 + $datearr[5], $datearr[4] + 1, $datearr[3]), 'iso');
- } else {
- my $calendar = C4::Calendar->new( branchcode => $branch );
- $datedue = $calendar->addDate($startdate, $loanlength);
- }
- }
-
- # if Hard Due Dates are used, retreive them and apply as necessary
- my ($hardduedate, $hardduedatecompare) = GetHardDueDate($borrower->{'categorycode'},$itemtype, $branch);
- if ( $hardduedate && $hardduedate->output('iso') && $hardduedate->output('iso') ne '0000-00-00') {
- # if the calculated due date is after the 'before' Hard Due Date (ceiling), override
- if ( $datedue->output( 'iso' ) gt $hardduedate->output( 'iso' ) && $hardduedatecompare == -1) {
- $datedue = $hardduedate;
- # if the calculated date is before the 'after' Hard Due Date (floor), override
- } elsif ( $datedue->output( 'iso' ) lt $hardduedate->output( 'iso' ) && $hardduedatecompare == 1) {
- $datedue = $hardduedate;
- # if the hard due date is set to 'exactly', overrride
- } elsif ( $hardduedatecompare == 0) {
- $datedue = $hardduedate;
- }
- # in all other cases, keep the date due as it is
- }
-
- # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
- if ( C4::Context->preference('ReturnBeforeExpiry') && $datedue->output('iso') gt $borrower->{dateexpiry} ) {
- $datedue = C4::Dates->new( $borrower->{dateexpiry}, 'iso' );
- }