4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 use Date
::Calc qw
/Today Add_Delta_YM/;
30 use C4
::Dates qw
/format_date format_date_in_iso/;
32 use C4
::Biblio qw
/GetMarcBiblio GetRecordValue GetFrameworkCode/;
35 my $startdate = $input->param('from');
36 my $enddate = $input->param('to');
37 my $ratio = $input->param('ratio');
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
41 template_name
=> "circ/reserveratios.tmpl",
45 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
50 my ( $year, $month, $day ) = Today
();
51 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
52 # Find yesterday for the default shelf pull start and end dates
53 # A default of the prior years's holds is a reasonable way to pull holds
54 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM
($year, $month, $day, -1, 0));
56 # Predefine the start and end dates if they are not already defined
57 # Check if null, should string match, if so set start and end date to yesterday
58 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
59 $startdate = format_date
($datelastyear);
61 if (!defined($enddate) or $enddate !~ s/^\s*(\S+)\s*$/$1/) { # strip spaces, remove Taint
62 $enddate = format_date
($todaysdate);
64 if (!defined($ratio)) {
67 # Force to be a number
70 $ratio = 1; # prevent division by zero
73 my $dbh = C4
::Context
->dbh;
74 my $sqldatewhere = "";
75 $debug and warn format_date_in_iso
($startdate) . "\n" . format_date_in_iso
($enddate);
76 my @query_params = ();
78 $sqldatewhere .= " AND reservedate >= ?";
79 push @query_params, format_date_in_iso
($startdate);
82 $sqldatewhere .= " AND reservedate <= ?";
83 push @query_params, format_date_in_iso
($enddate);
88 reserves.borrowernumber as borrowernumber,
89 reserves.biblionumber,
90 reserves.branchcode as branch,
94 GROUP_CONCAT(DISTINCT items.itemcallnumber
95 ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
96 GROUP_CONCAT(DISTINCT holdingbranch
97 ORDER BY items.itemnumber SEPARATOR '<br/>') as listbranch,
98 GROUP_CONCAT(DISTINCT items.location
99 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
100 GROUP_CONCAT(DISTINCT items.itype
101 ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
106 count(DISTINCT reserves.borrowernumber) as reservecount,
107 count(DISTINCT items.itemnumber) as itemcount
109 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
110 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
112 notforloan = 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
116 if (C4
::Context
->preference('IndependentBranches')){
117 $strsth .= " AND items.holdingbranch=? ";
118 push @query_params, C4
::Context
->userenv->{'branch'};
121 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
123 $template->param(sql
=> $strsth);
124 my $sth = $dbh->prepare($strsth);
125 $sth->execute(@query_params);
127 my $ratio_atleast1 = ($ratio >= 1) ?
1 : 0;
129 while ( my $data = $sth->fetchrow_hashref ) {
130 my $thisratio = $data->{reservecount
} / $data->{itemcount
};
131 my $ratiocalc = ($thisratio / $ratio);
132 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
133 my $record = GetMarcBiblio
($data->{biblionumber
});
134 $data->{subtitle
} = GetRecordValue
('subtitle', $record, GetFrameworkCode
($data->{biblionumber
}));
138 reservedate
=> format_date
( $data->{reservedate
} ),
139 priority
=> $data->{priority
},
140 name
=> $data->{borrower
},
141 title
=> $data->{title
},
142 subtitle
=> $data->{subtitle
},
143 author
=> $data->{author
},
144 notes
=> $data->{notes
},
145 itemnum
=> $data->{itemnumber
},
146 biblionumber
=> $data->{biblionumber
},
147 holdingbranch
=> $data->{holdingbranch
},
148 listbranch
=> $data->{listbranch
},
149 branch
=> $data->{branch
},
150 itemcallnumber
=> $data->{itemcallnumber
},
151 location
=> $data->{l_location
},
152 itype
=> $data->{l_itype
},
153 reservecount
=> $data->{reservecount
},
154 itemcount
=> $data->{itemcount
},
155 ratiocalc
=> sprintf("%.0d", $ratio_atleast1 ?
($thisratio / $ratio) : $thisratio),
156 thisratio
=> sprintf("%.2f", $thisratio),
157 thisratio_atleast1
=> ($thisratio >= 1) ?
1 : 0,
158 listcall
=> $data->{listcall
}
164 ratio_atleast1
=> $ratio_atleast1,
165 todaysdate
=> format_date
($todaysdate),
169 reserveloop
=> \
@reservedata,
172 output_html_with_http_headers
$input, $cookie, $template->output;