Merge commit 'biblibre/3.2_biblibre' into to-push
[koha.git] / misc / cronjobs / stats / monthly_circulation_statistics.pl
1 #!/usr/bin/perl -w
2 #-----------------------------------
3 # Script Name: circstats.pl
4 # Script Version: 1.0
5 # Date:  2006/02/07
6 # Author:  Stephen Hedges (shedges@skemotah.com)
7 # Description: 
8 #       This script creates a comma-separated value file of
9 #       circulation statistics for any given month and year.
10 #       The statistics are grouped by itemtype, then by branch,
11 #       then by issues and renewals.
12 # Revision History:
13 #    1.0  2006/02/07: original version
14 #-----------------------------------
15 # Contributed 2003-6 by Skemotah Solutions
16 #
17 # This file is part of Koha.
18 #
19 # Koha is free software; you can redistribute it and/or modify it under the
20 # terms of the GNU General Public License as published by the Free Software
21 # Foundation; either version 2 of the License, or (at your option) any later
22 # version.
23 #
24 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
25 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
26 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
27 #
28 # You should have received a copy of the GNU General Public License along with
29 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
30 # Suite 330, Boston, MA  02111-1307 USA
31
32 # use strict;
33
34 # UNCOMMENT the following lines if running from a command line
35 # print "THIS SCRIPT produces a comma-separated values file of circulation statistics for a given month and year.\n\nDo you wish to continue? (y/n) ";
36 # chomp($_ = <STDIN>);
37 # die unless (/^y/i);
38
39 # UNCOMMENT the following lines if getting old stats (but be aware that renewal numbers are affected by deletes)
40 # YOU WILL also need to modify the SQLs to use these dates
41 # my ($month,$year);
42 # print "Get statistics for which month (1 to 12)? ";
43 # chomp($month = <STDIN>);
44 # die if ($month < 1 || $month > 12);
45 # print "Get statistics for which year (2000 to 2050)? ";
46 # chomp($year = <STDIN>);
47 # die if ($year < 2000 || $year > 2050);
48
49 open OUTFILE, ">circstats.csv" or die "Cannot open file circstats.csv: $!";
50 print OUTFILE "\"ccode\",\"branch\",\"issues\",\"renewals\"\n";
51
52 use C4::Context;
53 use C4::Koha;
54 use Mail::Sendmail;  # comment out 3 lines if not doing e-mail sending of file
55 use MIME::QuotedPrint;
56 use MIME::Base64;
57 # set the e-mail server -- comment out if not doing e-mail notices
58 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'localhost';
59 #                                         set your own mail server name here
60
61 my $dbh = C4::Context->dbh;
62 #my $sth1 = $dbh->prepare ("SELECT itemtype FROM itemtypes ORDER BY itemtype");
63 my $sth2 = $dbh->prepare ("SELECT branchcode, branchname FROM branches ORDER BY branchcode");
64
65 # number of checkouts for this library
66 my $sth3 = $dbh->prepare ("SELECT COUNT(*) FROM biblioitems,items,statistics WHERE biblioitems.biblioitemnumber=items.biblioitemnumber AND statistics.itemnumber=items.itemnumber AND items.ccode=? AND YEAR(statistics.datetime)=YEAR(SUBDATE(CURDATE(),INTERVAL 1 MONTH)) AND MONTH(statistics.datetime)=MONTH(SUBDATE(CURDATE(),INTERVAL 1 MONTH)) AND statistics.branch=? AND statistics.type='issue' GROUP BY ccode");
67
68 # number of renewals for this library
69 my $sth4 = $dbh->prepare ("SELECT COUNT(statistics.itemnumber) FROM statistics,items,biblioitems
70         WHERE YEAR(statistics.datetime)=YEAR(SUBDATE(CURDATE(),INTERVAL 1 MONTH))
71         AND MONTH(statistics.datetime)=MONTH(SUBDATE(CURDATE(),INTERVAL 1 MONTH))
72         AND statistics.itemnumber=items.itemnumber
73         AND biblioitems.ccode=?
74         AND homebranch=?
75         AND biblioitems.biblioitemnumber=items.biblioitemnumber
76         AND statistics.type='renew'
77         GROUP BY statistics.type");                                                                                         
78
79 # find the itemnumbers
80 my ($rowt,$rowb,$rowi,$rowr,$itemtype,$branchcode,$branchname,$issues,$renews,$line);
81
82 #$sth1->execute();
83 my ($ccode_count,@ccode_results) = GetCcodes;
84
85 #for my $ccode (@ccode_results);
86 # loop through every itemtype
87 #while ($rowt = $sth1->fetchrow_arrayref) {
88 for (my $i=0;$i<scalar(@ccode_results);$i++) {
89 #for my $ccode (@ccode_results) {
90     unless (!$ccode_results[$i]) {
91 #       use Data::Dumper;
92 #       warn Dumper($ccode_results[$i]);
93     $itemtype = $ccode_results[$i]{'authorised_value'}; #$ccode->{authorised_value}; #rowt->[0];
94     $line = "\"$itemtype\"";
95 #       warn "$itemtype\n";
96         # find branchnames
97     $sth2->execute();
98
99         # find the number of issues per itemtype in this branch
100     while ($rowb = $sth2->fetchrow_arrayref) {
101                 $branchcode = $rowb->[0];
102                 $branchname = $rowb->[1];
103                 $sth3->execute($itemtype,$branchcode); # find issues by itemtype per branch
104                 $rowi = $sth3->fetchrow_arrayref;
105                 $issues = $rowi->[0]; # count
106                 unless ($issues) {$issues=""}
107                 $sth3->finish;
108
109                 $sth4->execute($itemtype,$branchcode); # find reserves by itemtype per branch
110                 $rowr = $sth4->fetchrow_arrayref; # count
111                 $renews = $rowr->[0];
112                 unless ($renews) {$renews=""}
113                 $sth4->finish;
114
115                 # put the data in this line
116                 $line = $line . ",\"$branchname\",\"$issues\",\"$renews\"";
117 #               warn "LINE: $branchname $issues $renews\n";
118     }
119     $sth2->finish;
120
121     $line = $line . "\n";
122     print OUTFILE "$line";
123         }
124 }
125 #$sth1->finish;
126 close OUTFILE;
127 $dbh->disconnect;
128
129 # send the outfile as an attachment to the library e-mail
130
131 my %attachmail = (
132          from => $from_address,
133          to => $to_addresses,
134          subject => 'Circulation Statistics',
135         );
136
137
138 my $boundary = "====" . time() . "====";
139 $attachmail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
140
141 my $attachmessage = "Attached is the file of circulation statistics for the previous month. Please open the statistics spreadsheet template for Page 1, open this file in a new spreadsheet, and paste the numbers from this file into the template.\n";
142
143 my $attachfile = "circstats.csv"; 
144
145 open (F, $attachfile) or die "Cannot read $attachfile: $!";
146 binmode F; undef $/;
147 $attachmail{body} = encode_base64(<F>);
148 close F;
149
150 $boundary = '--'.$boundary;
151 $attachmail{body} = <<END_OF_BODY;
152 $boundary
153 Content-Type: text/plain; charset="iso-8859-1"
154 Content-Transfer-Encoding: quoted-printable
155
156 $attachmessage
157 $boundary
158 Content-Type: application/octet-stream; name="circstats.csv"
159 Content-Transfer-Encoding: base64
160 Content-Disposition: attachment; filename="circstats.csv"
161
162 $attachmail{body}
163 $boundary--
164 END_OF_BODY
165
166 sendmail(%attachmail) || print "Error: $Mail::Sendmail::error\n";
167