Adding three statistics files that NPL uses, they don't
[koha.git] / misc / cronjobs / stats / monthly_new_patron_statistics.pl
1 #!/usr/bin/perl -w
2 #-----------------------------------
3 # Script Name: borrstats.pl
4 # Script Version: 1.0
5 # Date:  2006/02/24
6 # Author:  Stephen Hedges (shedges@skemotah.com)
7 # Description: 
8 #       This script creates a comma-separated value file of
9 #       new borrower statistics for any given month and year.
10 #       The statistics are grouped by borrower type, then by
11 #       branch.
12 # Revision History:
13 #    1.0  2006/02/24: 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, ">borrstats.csv" or die "Cannot open file borrstats.csv: $!";
50 print OUTFILE "\"type\",\"branch\",\"count\"\n";
51
52 use C4::Context;
53 use Mail::Sendmail;  # comment out 3 lines if not doing e-mail sending of file
54 use MIME::QuotedPrint;
55 use MIME::Base64;
56 # set the e-mail server -- comment out if not doing e-mail notices
57 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'localhost';
58 #                                         set your own mail server name here
59
60 my $dbh = C4::Context->dbh;
61 my $sth1 = $dbh->prepare ("SELECT categorycode FROM categories ORDER BY categorycode");
62 my $sth2 = $dbh->prepare ("SELECT branchcode,COUNT(branchcode) FROM borrowers WHERE categorycode=? AND YEAR(dateenrolled)=YEAR(SUBDATE(CURDATE(),INTERVAL 1 MONTH)) AND MONTH(dateenrolled)=MONTH(SUBDATE(CURDATE(),INTERVAL 1 MONTH)) GROUP BY branchcode");
63
64 my ($rowc,$rowb,$categorycode,$branchcode,$count,$line);
65
66 $sth1->execute();
67
68 while ($rowc = $sth1->fetchrow_arrayref) {
69     $categorycode = $rowc->[0];
70     $line = "\"$categorycode\"";
71     
72     $sth2->execute($categorycode);
73
74     while ($rowb = $sth2->fetchrow_arrayref) {
75         $branchcode = $rowb->[0];
76         $count = $rowb->[1];
77
78         $line = $line . ",\"$branchcode\",\"$count\"";
79     }
80     $sth2->finish;
81
82     $line = $line . "\n";
83     print OUTFILE "$line";
84 }
85 $sth1->finish;
86 close OUTFILE;
87 $dbh->disconnect;
88
89 # send the outfile as an attachment to the library e-mail
90
91 my %attachmail = (
92          from => $from_address,
93          to => $to_addresses,
94          subject => 'New Patrons Statistics',
95         );
96
97
98 my $boundary = "====" . time() . "====";
99 $attachmail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
100
101 my $attachmessage = "Attached is the file of new borrower statistics for the previous month. Please open the statistics spreadsheet for Page 2, open this file in a new spreadsheet, and paste the numbers from this file into the statistics spreadsheet.\n";
102
103 my $attachfile = "borrstats.csv"; 
104
105 open (F, $attachfile) or die "Cannot read $attachfile: $!";
106 binmode F; undef $/;
107 $attachmail{body} = encode_base64(<F>);
108 close F;
109
110 $boundary = '--'.$boundary;
111 $attachmail{body} = <<END_OF_BODY;
112 $boundary
113 Content-Type: text/plain; charset="iso-8859-1"
114 Content-Transfer-Encoding: quoted-printable
115
116 $attachmessage
117 $boundary
118 Content-Type: application/octet-stream; name="borrstats.csv"
119 Content-Transfer-Encoding: base64
120 Content-Disposition: attachment; filename="borrstats.csv"
121
122 $attachmail{body}
123 $boundary--
124 END_OF_BODY
125
126 sendmail(%attachmail) || print "Error: $Mail::Sendmail::error\n";
127