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

(-)a/Koha/Circulation.pm (+80 lines)
Line 0 Link Here
1
package Koha::Circulation;
2
3
# Copyright 2011 Catalyst IT
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use 5.010001;
21
use strict;
22
use warnings;
23
24
use base qw(Class::Accessor);
25
26
__PACKAGE__->follow_best_practice;
27
__PACKAGE__->mk_accessors(qw( borrowernumber itemnumber borrower context));
28
29
=head1 NAME
30
31
Koha::Circulation
32
33
=cut
34
35
=head2 check_valid_barcode
36
  $exists = $circulation->check_valid_barcode( $barcode );
37
38
Checks if the barcode exists in the database
39
40
=cut
41
42
sub check_valid_barcode {
43
    my $self = shift;
44
    my $barcode = shift;
45
    my $context = $self->get_context;
46
    my $query = "SELECT count(barcode) FROM items WHERE barcode = ?";
47
    my $sth = $context->dbh->prepare($query);
48
    $sth->execute($barcode);
49
    my $exists = $sth->fetchrow;
50
    $sth->finish;
51
    return $exists
52
}
53
54
=head2 checked_out
55
56
  $checked_out = $circulation->checked_out($itemnumber);
57
58
Returns the row of the issues table if the item is currently checked out
59
  
60
C<$itemnumber> is the itemnumber.
61
  
62
C<$checked_out> is a hashref of the row from the issues table.
63
                                                  
64
=cut
65
    
66
sub checked_out {
67
    my $self = shift;
68
    my $itemnumber = shift;
69
    return unless $itemnumber;
70
    my $context = $self->get_context;
71
    my $sth = $context->dbh->prepare("SELECT * FROM issues WHERE issues.itemnumber = ?");
72
    $sth->execute($itemnumber);
73
    my $data = $sth->fetchrow_hashref;
74
    return $data;
75
}
76
77
78
1;
79
__END__
80
(-)a/Koha/Circulation/Issue.pm (-1 / +251 lines)
Line 0 Link Here
0
- 
1
package Koha::Circulation::Issue;
2
3
# Copyright 2011 Catalyst IT
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use 5.010001;
21
use strict;
22
use warnings;
23
24
use base 'Koha::Circulation';
25
26
use Koha::Dates;
27
use Koha::Calendar;
28
29
sub checkout {
30
    my $self = shift;
31
    my ($item,$issuedate,$branch,$datedue) = @_;
32
    my $context = $self->get_context;
33
    my $borrower = $self->get_borrower;
34
    my $checked_out = $self->checked_out($item->{'itemnumber'});
35
    if ($checked_out) {
36
        # Item is on loan
37
        if ($checked_out->{'borrowernumber'} == $borrower->{'borrowernumber'}){
38
            # Item is on loan to the current borrower so we attempt a renewal
39
            $self->renew($item,$branch,$datedue,$issuedate);
40
        }
41
        else {
42
            # on loan to another, so we must return it first
43
            $self->checkin($item);
44
        }
45
    }
46
    my $sth = $context->dbh->prepare("INSERT INTO issues
47
                    (borrowernumber, itemnumber,issuedate, date_due, branchcode)
48
                VALUES (?,?,?,?,?)");
49
    $sth->execute(
50
            $borrower->{'borrowernumber'},      # borrowernumber
51
            $item->{'itemnumber'},              # itemnumber
52
            $issuedate->output('iso'),          # issuedate
53
            $datedue->output('iso'),            # date_due
54
            C4::Context->userenv->{'branch'}    # branchcode
55
        );
56
    
57
}
58
59
sub checkin {
60
    my $self = shift;
61
    my $item = shift;
62
    my $context = $self->get_context;
63
    my $branch = $context->userenv->{'branch'};
64
    
65
    
66
    
67
}
68
69
=head2 calc_date_due
70
  
71
$newdatedue = $issue->calc_date_due($startdate,$itemtype,$branchcode);
72
73
this function calculates the due date given the start date and configured circulation rules,
74
checking against the holidays calendar as per the 'useDaysMode' syspref.
75
C<$startdate>   = C4::Dates object representing start date of loan period (assumed to be today)
76
C<$itemtype>  = itemtype code of item in question
77
C<$branch>  = location whose calendar to use
78
79
=cut
80
81
sub calc_date_due {
82
    my $self = shift;
83
    my ($startdate,$itemtype,$branch) = @_;
84
    my $datedue;
85
    my $loanlength = $self->get_loan_length($itemtype,$branch);
86
    my $context = $self->get_context;
87
    my $borrower = $self->get_borrower;
88
89
    # if globalDueDate ON the datedue is set to that date
90
    # FIXME we can't use C4::Dates here
91
    if ($context->preference('globalDueDate') && $context->preference('globalDueDate') =~ C4::Dates->regexp('syspref') ){
92
        $datedue = Koha::Dates->new( $context->preference('globalDueDate') );
93
    }
94
    else {
95
        # calculate date normally
96
        if ($context->preference('useDaysMode') eq 'Days') {  # ignoring calendar
97
            my $timedue = time + ($loanlength) * 60;  #Loanlength is stored as minutes 
98
                                                         #FIXME also ignores startdate
99
            my @datearr = localtime($timedue);
100
            $datedue = Koha::Dates->new( sprintf("%04d-%02d-%02d %02d:%02d:%02d", 1900 + $datearr[5], $datearr[4] + 1, $datearr[3], $datearr[2], $datearr[1], $datearr[0]), 'iso');
101
        }
102
        else {
103
            my $calendar = Koha::Calendar->new(  branchcode => $branch, context => $self->get_context );
104
            $datedue = $calendar->addDate($startdate, $loanlength * 60); # loanlength is expresed in minutes in the db
105
        }
106
    }
107
    my ($hardduedate, $hardduedatecompare) = $self->get_hard_due_date($itemtype,$branch);
108
    if ($hardduedate && $hardduedate->output('iso') && $hardduedate->output('iso') !~ /^0000\-00\-00/){
109
        # if the calculated due date is after the 'before' Hard Due Date (ceiling), override
110
        if ( $datedue->output( 'iso' ) gt $hardduedate->output( 'iso' ) && $hardduedatecompare == -1) {
111
            $datedue = $hardduedate;
112
        }
113
        elsif ($datedue->output('iso') lt $hardduedate->output( 'iso' ) && $hardduedatecompare == 1) {
114
            $datedue = $hardduedate;
115
        }
116
        elsif ( $hardduedatecompare == 0) {
117
            $datedue = $hardduedate;
118
        }
119
    }
120
    if ($context->preference('ReturnBeforeExpiry') && $datedue->output('iso') gt $borrower->{dateexpiry} ) {
121
         $datedue = Koha::Dates->new( $borrower->{dateexpiry}, 'iso' );
122
    }
123
    return $datedue;
124
}
125
126
=head2 get_hard_due_date
127
128
  my ($hardduedate,$hardduedatecompare) = $issue->get_hard_due_date($itemtype,branchcode)
129
130
Get the Hard Due Date and it's comparison for an itemtype, a borrower type and a branch
131
132
=cut
133
134
sub get_hard_due_date {
135
    my $self = shift;
136
    my ( $itemtype, $branchcode ) = @_;
137
    my $context = $self->get_context;
138
    my $borrower = $self->get_borrower;
139
    my $sth =
140
      $context->dbh->prepare(
141
"SELECT hardduedate, hardduedatecompare 
142
  FROM issuingrules WHERE categorycode=? AND itemtype=? AND branchcode=?"
143
      );
144
    $sth->execute( $borrower->{'categorycode'}, $itemtype, $branchcode );
145
    my $results = $sth->fetchrow_hashref;
146
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
147
      if defined($results) && $results->{hardduedate} ne 'NULL';
148
149
    $sth->execute( $borrower->{'categorycode'}, "*", $branchcode );
150
    $results = $sth->fetchrow_hashref;
151
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
152
      if defined($results) && $results->{hardduedate} ne 'NULL';
153
154
    $sth->execute( "*", $itemtype, $branchcode );
155
    $results = $sth->fetchrow_hashref;
156
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
157
      if defined($results) && $results->{hardduedate} ne 'NULL';
158
159
    $sth->execute( "*", "*", $branchcode );
160
    $results = $sth->fetchrow_hashref;
161
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
162
      if defined($results) && $results->{hardduedate} ne 'NULL';
163
164
    $sth->execute( $borrower->{'categorycode'}, $itemtype, "*" );
165
    $results = $sth->fetchrow_hashref;
166
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
167
      if defined($results) && $results->{hardduedate} ne 'NULL';
168
169
    $sth->execute( $borrower->{'categorycode'}, "*", "*" );
170
    $results = $sth->fetchrow_hashref;
171
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
172
      if defined($results) && $results->{hardduedate} ne 'NULL';
173
174
    $sth->execute( "*", $itemtype, "*" );
175
    $results = $sth->fetchrow_hashref;
176
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
177
      if defined($results) && $results->{hardduedate} ne 'NULL';
178
179
    $sth->execute( "*", "*", "*" );
180
    $results = $sth->fetchrow_hashref;
181
    return (Koha::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
182
      if defined($results) && $results->{hardduedate} ne 'NULL';
183
184
    # if no rule is set => return undefined
185
    return (undef, undef);
186
}
187
188
=head2 get_loan_length
189
190
  my $loanlength = $issue->get_loan_length($itemtype,branchcode)
191
192
Get loan length for an itemtype, a borrower type and a branch
193
194
=cut
195
196
sub get_loan_length {
197
    my $self = shift;
198
    my ($itemtype,$branchcode) = @_;
199
    my $context = $self->get_context;
200
    my $borrower = $self->get_borrower;
201
    my $sth = $context->dbh->prepare("SELECT issuelength FROM issuingrules WHERE categorycode=?
202
       AND itemtype=? AND branchcode=? AND issuelength IS NOT NULL");
203
    $sth->execute( $borrower->{'categorycode'}, $itemtype, $branchcode );
204
   
205
    my $loanlength = $sth->fetchrow_hashref;
206
    return $loanlength->{issuelength}
207
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
208
209
    $sth->execute( $borrower->{'categorycode'}, "*", $branchcode );
210
    $loanlength = $sth->fetchrow_hashref;
211
    return $loanlength->{issuelength}
212
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
213
214
    $sth->execute( "*", $itemtype, $branchcode );
215
    $loanlength = $sth->fetchrow_hashref;
216
    return $loanlength->{issuelength}
217
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
218
219
    $sth->execute( "*", "*", $branchcode );
220
    $loanlength = $sth->fetchrow_hashref;
221
    return $loanlength->{issuelength}
222
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
223
224
    $sth->execute( $borrower->{'categorycode'}, $itemtype, "*" );
225
    $loanlength = $sth->fetchrow_hashref;
226
    return $loanlength->{issuelength}
227
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
228
    
229
    $sth->execute( $borrower->{'categorycode'}, "*", "*" );
230
    $loanlength = $sth->fetchrow_hashref;
231
    return $loanlength->{issuelength}
232
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
233
234
    $sth->execute( "*", $itemtype, "*" );
235
    $loanlength = $sth->fetchrow_hashref;
236
    return $loanlength->{issuelength}
237
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
238
239
    $sth->execute( "*", "*", "*" );
240
    $loanlength = $sth->fetchrow_hashref;
241
    return $loanlength->{issuelength}
242
      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
243
244
    # if no rule is set => 21 days (hardcoded)
245
    return 21;
246
}
247
248
249
1;
250
__END__
251

Return to bug 6431