Bug 2505 - remove unnecessary -w and replace with use warnings;
[koha.git] / misc / cronjobs / stats / monthly_new_items_statistics.pl
1 #!/usr/bin/perl
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 use warnings;
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, ">addstats.csv" or die "Cannot open file addstats.csv: $!";
50 print OUTFILE "\"type\",\"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
62 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");
63
64 my ($row,$itemtype,$count);
65
66 $sth->execute();
67
68 while ($row = $sth->fetchrow_arrayref) {
69     $itemtype = $row->[0];
70     $count = $row->[1];
71
72     print OUTFILE "$itemtype,$count\n";
73 }
74 $sth->finish;
75 close OUTFILE;
76 $dbh->disconnect;
77
78 # send the outfile as an attachment to the library e-mail
79
80 my %attachmail = (
81          from => $from_address,
82          to => $to_addresses,
83          subject => 'New Items Statistics',
84         );
85
86
87 my $boundary = "====" . time() . "====";
88 $attachmail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
89
90 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";
91
92 my $attachfile = "addstats.csv"; 
93
94 open (F, $attachfile) or die "Cannot read $attachfile: $!";
95 binmode F; undef $/;
96 $attachmail{body} = encode_base64(<F>);
97 close F;
98
99 $boundary = '--'.$boundary;
100 $attachmail{body} = <<END_OF_BODY;
101 $boundary
102 Content-Type: text/plain; charset="iso-8859-1"
103 Content-Transfer-Encoding: quoted-printable
104
105 $attachmessage
106 $boundary
107 Content-Type: application/octet-stream; name="addstats.csv"
108 Content-Transfer-Encoding: base64
109 Content-Disposition: attachment; filename="addstats.csv"
110
111 $attachmail{body}
112 $boundary--
113 END_OF_BODY
114
115 sendmail(%attachmail) || print "Error: $Mail::Sendmail::error\n";
116