Re: [Koha] How to receive reports to email
Hi, I am not referring to check-out and check-in messages through email for a single or multiple transactions of a particular user. We are getting email notifications to check-in/check-out/reminders/ etc. I need to keep print copy of daily transactions (cumulative) report for check-out & check-in transactions that is from library opening hours to closing hours every day. Currently I am running sql for date range for check-in & check-out. Instead of running check-out or desired report using sql and saving to csv or some other format has to be performed manually. But, I am looking for using same sql report automatically through an email on every day basis. So that, I could easily get the print done from single mail without running sql manually and also I could assign printing and filing job to other library staff in the library who do not have access to Koha but access to Library Email. Therefore, I am looking for email communication on complete transactions (cumulative) report for check-out or check-in in a single mail on daily basis. With regards Satish Librarian Govt. Engineering College, Hassan. Karnataka
Hi, There is bug (3935) registered for the same problem already
Hi If already have an report for this, you can define it as public and create a small script -in the language of your choice- that runs it and insert an entry in cron jobs. You can even create the whole report in your script accessing directly to your database. Regards, Alvaro |-----------------------------------------------------------------------------------------------------------------| Envíe y Reciba Datos y mensajes de Texto (SMS) hacia y desde cualquier celular y Nextel en el Perú, México y en mas de 180 paises. Use aplicaciones 2 vias via SMS y GPRS online Visitenos en www.perusms.com Le jeu. 14 févr. 2019 à 06:07, SATISH <lis4satish@gmail.com> a écrit :
Hi,
There is bug (3935) registered for the same problem already _______________________________________________ Koha mailing list http://koha-community.org Koha@lists.katipo.co.nz https://lists.katipo.co.nz/mailman/listinfo/koha
On Thu, Feb 14, 2019 at 12:30:59PM +0530, SATISH wrote:
I need to keep print copy of daily transactions (cumulative) report for check-out & check-in transactions that is from library opening hours to closing hours every day. Currently I am running sql for date range for check-in & check-out.
[...]
But, I am looking for using same sql report automatically through an email on every day basis.
Here's one way to do it. Start with a file that contains the SQL for the report, preceded by a suitable e-mail header: --------------------- /path/to/your/reports/foobar --------------------- From: some.email.address@example.com To: some.other.email.address@example.com, etc.etc.etc@example.com Subject: The report you asked for Content-Type: text/plain SELECT ... FROM ... WHERE ... ------------------------------------------------------------------------ Then write a Perl script that runs the report and sends the results in tab-delimited format: ----------------------- /path/to/run-reports.pl ------------------------ #!/usr/bin/perl use strict; use warnings; use C4::Context; my $dbh = connect_to_database(); # Expand file globs if (@ARGV == 1 && $ARGV[0] =~ /[*]/) { @ARGV = glob(@ARGV); } die "No reports to run" if !@ARGV; # Run report(s) foreach my $file (@ARGV) { open STDIN, '<', $file or die "Can't open $file: $!"; my $header = read_header(); my $sql = read_sql(); my $sender = find_sender($header); my $sth = prepare_sql($sql); my $fh = start_sendmail($sender); print $fh $header; print $fh columns_header($sth); while (my @row = $sth->fetchrow_array) { print $fh tab_delimited(@row); } close $fh or die "Close sendmail handle: $!"; close STDIN; } # Functions sub connect_to_database { my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; return $dbh; } sub find_sendmail { foreach (qw(/sbin/sendmail /usr/sbin/sendmail /bin/sendmail /usr/bin/sendmail)) { return $_ if -x $_; } die "Can't find sendmail"; } sub read_header { my $header = ''; while (<>) { $header .= $_; last if /^\r?$/; } return $header; } sub find_sender { my ($header) = @_; foreach (split /\n/, $header) { return email($1) if /^From:\s+(.+)/; } die "No sender found in e-mail header"; } sub email { local $_ = shift; return $1 if /^([^@\s]+\@[^@\s]+)\b/; return $1 if /^.+ <([^<>\s]+)>/; die "Can't find sender e-mail address"; } sub read_sql { # Read the SQL local $/; my $sql = <>; return $sql; } sub prepare_sql { my $sql = shift; my $sth = $dbh->prepare($sql); $sth->execute(@_); return $sth; } sub start_sendmail { my ($sender) = @_; my $sendmail = find_sendmail(); open my $fh, '|-', $sendmail, qw(-oi -oem -t -f), $sender or die "Can't exec $sendmail: $!"; return $fh; } sub columns_header { my ($sth) = @_; return join("\t", @{ $sth->{'NAME'} }) . "\n"; } sub tab_delimited { return join("\t", map { defined $_ ? $_ : '' } @_) . "\n"; } ------------------------------------------------------------------------ Then use koha-shell to run them: [as root] # koha-shell YOURINSTANCE -c '/path/to/run-reports.pl /path/to/your/reports/*' I'll leave the rest to you -- cron, etc. Paul. -- Paul Hoffman <paul@flo.org> Software Manager Fenway Library Organization 550 Huntington Ave. Boston, MA 02115 617-989-5032
Hi, I think it might not be well known, but Koha has already a command line script that you can run as a cronjob to email you the results of any report: http://git.koha-community.org/gitweb/?p=koha.git;a=blob;f=misc/cronjobs/runr... Hope this helps! Katrin On 14.02.19 18:33, Paul Hoffman wrote:
I need to keep print copy of daily transactions (cumulative) report for check-out & check-in transactions that is from library opening hours to closing hours every day. Currently I am running sql for date range for check-in & check-out.
[...]
But, I am looking for using same sql report automatically through an email on every day basis. Here's one way to do it. Start with a file that contains the SQL for
On Thu, Feb 14, 2019 at 12:30:59PM +0530, SATISH wrote: the report, preceded by a suitable e-mail header:
--------------------- /path/to/your/reports/foobar --------------------- From: some.email.address@example.com To: some.other.email.address@example.com, etc.etc.etc@example.com Subject: The report you asked for Content-Type: text/plain
SELECT ... FROM ... WHERE ... ------------------------------------------------------------------------
Then write a Perl script that runs the report and sends the results in tab-delimited format:
----------------------- /path/to/run-reports.pl ------------------------ #!/usr/bin/perl
use strict; use warnings;
use C4::Context;
my $dbh = connect_to_database();
# Expand file globs if (@ARGV == 1 && $ARGV[0] =~ /[*]/) { @ARGV = glob(@ARGV); } die "No reports to run" if !@ARGV;
# Run report(s) foreach my $file (@ARGV) { open STDIN, '<', $file or die "Can't open $file: $!"; my $header = read_header(); my $sql = read_sql(); my $sender = find_sender($header); my $sth = prepare_sql($sql); my $fh = start_sendmail($sender); print $fh $header; print $fh columns_header($sth); while (my @row = $sth->fetchrow_array) { print $fh tab_delimited(@row); } close $fh or die "Close sendmail handle: $!"; close STDIN; }
# Functions
sub connect_to_database { my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; return $dbh; }
sub find_sendmail { foreach (qw(/sbin/sendmail /usr/sbin/sendmail /bin/sendmail /usr/bin/sendmail)) { return $_ if -x $_; } die "Can't find sendmail"; }
sub read_header { my $header = ''; while (<>) { $header .= $_; last if /^\r?$/; } return $header; }
sub find_sender { my ($header) = @_; foreach (split /\n/, $header) { return email($1) if /^From:\s+(.+)/; } die "No sender found in e-mail header"; }
sub email { local $_ = shift; return $1 if /^([^@\s]+\@[^@\s]+)\b/; return $1 if /^.+ <([^<>\s]+)>/; die "Can't find sender e-mail address"; }
sub read_sql { # Read the SQL local $/; my $sql = <>; return $sql; }
sub prepare_sql { my $sql = shift; my $sth = $dbh->prepare($sql); $sth->execute(@_); return $sth; }
sub start_sendmail { my ($sender) = @_; my $sendmail = find_sendmail(); open my $fh, '|-', $sendmail, qw(-oi -oem -t -f), $sender or die "Can't exec $sendmail: $!"; return $fh; }
sub columns_header { my ($sth) = @_; return join("\t", @{ $sth->{'NAME'} }) . "\n"; }
sub tab_delimited { return join("\t", map { defined $_ ? $_ : '' } @_) . "\n"; } ------------------------------------------------------------------------
Then use koha-shell to run them:
[as root] # koha-shell YOURINSTANCE -c '/path/to/run-reports.pl /path/to/your/reports/*'
I'll leave the rest to you -- cron, etc.
Paul.
participants (4)
-
Alvaro Cornejo -
Katrin Fischer -
Paul Hoffman -
SATISH