Adding three statistics files that NPL uses, they don't
[koha-ffzg.git] / misc / cronjobs / stats / monthly_new_items_statistics.pl
1 #!/usr/bin/perl -w
2 #-----------------------------------
3 # Script Name: addstats.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 materials statistics for any given month and year.
10 #       The statistics are grouped by itemtype.
11
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 # UNCOMMENT the following lines if running from a command line
33 # 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) ";
34 # chomp($_ = <STDIN>);
35 # die unless (/^y/i);
36
37 # UNCOMMENT the following lines if getting old stats (but be aware that renewal numbers are affected by deletes)
38 # YOU WILL also need to modify the SQLs to use these dates
39 # my ($month,$year);
40 # print "Get statistics for which month (1 to 12)? ";
41 # chomp($month = <STDIN>);
42 # die if ($month < 1 || $month > 12);
43 # print "Get statistics for which year (2000 to 2050)? ";
44 # chomp($year = <STDIN>);
45 # die if ($year < 2000 || $year > 2050);
46
47 open OUTFILE, ">addstats.csv" or die "Cannot open file addstats.csv: $!";
48 print OUTFILE "\"type\",\"count\"\n";
49
50 use C4::Context;
51 use Mail::Sendmail;  # comment out 3 lines if not doing e-mail sending of file
52 use MIME::QuotedPrint;
53 use MIME::Base64;
54 # set the e-mail server -- comment out if not doing e-mail notices
55 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'localhost';
56 #                                         set your own mail server name here
57
58 my $dbh = C4::Context->dbh;
59
60 my $sth = $dbh->prepare ("SELECT biblioitems.ccode,COUNT(biblioitems.ccode) FROM items,biblioitems WHERE YEAR(items.dateaccessioned)=YEAR(SUBDATE(CURDATE(),INTERVAL 1 MONTH)) AND MONTH(items.dateaccessioned)=MONTH(SUBDATE(CURDATE(),INTERVAL 1 MONTH)) AND biblioitems.biblioitemnumber=items.biblioitemnumber GROUP BY biblioitems.ccode");
61
62 my ($row,$itemtype,$count);
63
64 $sth->execute();
65
66 while ($row = $sth->fetchrow_arrayref) {
67     $itemtype = $row->[0];
68     $count = $row->[1];
69
70     print OUTFILE "$itemtype,$count\n";
71 }
72 $sth->finish;
73 close OUTFILE;
74 $dbh->disconnect;
75
76 # send the outfile as an attachment to the library e-mail
77
78 my %attachmail = (
79          from => $from_address,
80          to => $to_addresses,
81          subject => 'New Items Statistics',
82         );
83
84
85 my $boundary = "====" . time() . "====";
86 $attachmail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
87
88 my $attachmessage = "Attached is the file of new materials statistics for the previous month. Please use this file to calculate the values for the adds spreadsheet.\n";
89
90 my $attachfile = "addstats.csv"; 
91
92 open (F, $attachfile) or die "Cannot read $attachfile: $!";
93 binmode F; undef $/;
94 $attachmail{body} = encode_base64(<F>);
95 close F;
96
97 $boundary = '--'.$boundary;
98 $attachmail{body} = <<END_OF_BODY;
99 $boundary
100 Content-Type: text/plain; charset="iso-8859-1"
101 Content-Transfer-Encoding: quoted-printable
102
103 $attachmessage
104 $boundary
105 Content-Type: application/octet-stream; name="addstats.csv"
106 Content-Transfer-Encoding: base64
107 Content-Disposition: attachment; filename="addstats.csv"
108
109 $attachmail{body}
110 $boundary--
111 END_OF_BODY
112
113 sendmail(%attachmail) || print "Error: $Mail::Sendmail::error\n";
114