2 #-----------------------------------
3 # Script Name: addstats.pl
6 # Author: Stephen Hedges (shedges@skemotah.com)
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.
13 # 1.0 2006/02/24: original version
14 #-----------------------------------
15 # Contributed 2003-6 by Skemotah Solutions
17 # This file is part of Koha.
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
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.
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
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>);
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
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);
47 open OUTFILE, ">addstats.csv" or die "Cannot open file addstats.csv: $!";
48 print OUTFILE "\"type\",\"count\"\n";
51 use Mail::Sendmail; # comment out 3 lines if not doing e-mail sending of file
52 use MIME::QuotedPrint;
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
58 my $dbh = C4::Context->dbh;
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");
62 my ($row,$itemtype,$count);
66 while ($row = $sth->fetchrow_arrayref) {
67 $itemtype = $row->[0];
70 print OUTFILE "$itemtype,$count\n";
76 # send the outfile as an attachment to the library e-mail
79 from => $from_address,
81 subject => 'New Items Statistics',
85 my $boundary = "====" . time() . "====";
86 $attachmail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
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";
90 my $attachfile = "addstats.csv";
92 open (F, $attachfile) or die "Cannot read $attachfile: $!";
94 $attachmail{body} = encode_base64(<F>);
97 $boundary = '--'.$boundary;
98 $attachmail{body} = <<END_OF_BODY;
100 Content-Type: text/plain; charset="iso-8859-1"
101 Content-Transfer-Encoding: quoted-printable
105 Content-Type: application/octet-stream; name="addstats.csv"
106 Content-Transfer-Encoding: base64
107 Content-Disposition: attachment; filename="addstats.csv"
113 sendmail(%attachmail) || print "Error: $Mail::Sendmail::error\n";