#!/usr/bin/perl -Tw
#
# LMS version 1.1.2b
#
#  (C) 2001-2003 LMS Developers
#
#  Please, see the doc/AUTHORS for more information about authors!
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License Version 2 as
#  published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.
#
#  $Id: lms-notify,v 1.25 2003/08/07 05:27:08 hunter Exp $

use strict;
use DBI;
use Config::IniFiles;
use Getopt::Long;
use Net::SMTP;
use vars qw($configfile $quiet $help $version);
use POSIX qw(strftime);

my $_version = '1.1.2b';
my $my_template;

my %options = (
	"--config-file|C=s"	=>	\$configfile,
	"--template-file|D=s"	=> 	\$my_template,
	"--quiet|q"		=>	\$quiet,
	"--help|h"		=>	\$help,
	"--version|v"		=>	\$version
);

Getopt::Long::config("no_ignore_case");
GetOptions(%options);

if($help)
{
	print STDERR <<EOF;
lms-notify, version $_version
(C) 2001-2003 LMS Developers

-C, --config-file=/etc/lms/lms.ini	alternate config file (default: /etc/lms/lms.ini);
-D, --template-file=/etc/lms/another.template.txt	alternate (default in confing file);
-h, --help			print this help and exit;
-v, --version			print version info and exit;
-q, --quiet			suppress any output, except errors;

EOF
	exit 0;
}

if($version)
{
	print STDERR <<EOF;
lms-notify, version $_version
(C) 2001-2003 LMS Developers

EOF
	exit 0;
}

if(!$configfile)
{
	$configfile = "/etc/lms/lms.ini";
}

if(!$quiet)
{
	print STDOUT "lms-notify, version $_version\n";
	print STDOUT "(C) Copyright 2002-2003 Rulez.PL Development Team\n";
	print STDOUT "(C) Copyright 2001-2002 NetX ACN\n";
	print STDOUT "Using file $configfile as config.\n";
}

if(! -r $configfile)
{
	print STDERR "Fatal error: Unable to read configuration file $configfile, exiting.\n";
	exit 1;
}

my $ini = new Config::IniFiles -file => $configfile;

my $dbtype = $ini->val('database', 'type') || 'mysql';
my $dbhost = $ini->val('database', 'host') || 'localhost';
my $dbuser = $ini->val('database', 'user') || 'root';
my $dbpasswd = $ini->val('database', 'password') || '';
my $dbname = $ini->val('database', 'database') || 'lms';

my $limit = $ini->val('notify', 'limit') || 0;
my $mtfile = $my_template || $ini->val('notify', 'mailtemplate');
my $mailfrom = $ini->val('notify', 'mailfrom') || '';
my $mailfname = $ini->val('notify', 'mailfname') || '';
my $mailsubject = $ini->val('notify', 'mailsubject') || '';
my $smtpserver = $ini->val('notify', 'smtp_server') || '127.0.0.1';
my $debugemail = $ini->val('notify', 'debug_email') || '';

if(!$mtfile)
{
	print STDERR "Fatal error: mailtemplate in $configfile unset! Can't continue, exiting.\n";
	exit 1;
}

if(! -r $mtfile)
{
	print STDERR "Fatal error: unable to read mail template file $mtfile, exiting!\n";
	exit 1;
}

if(!$mailfrom)
{
	print STDERR "Fatal error: mailfrom unset! Can't continue, exiting.\n";
	exit 1;
}

if(!$mailfname)
{
	print STDERR "Fatal error: mailfname unset! Can't continue, exiting.\n";
	exit 1;
}

if(!$mailsubject)
{
	print STDERR "Fatal error: mailsubject unset! Can't continue, exiting.\n";
	exit 1;
}
		
my $dbase;

if($dbtype eq "mysql")
{
	$dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
}
elsif($dbtype eq "postgres")
{
	$dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
}
else
{
	print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n";
	exit 1;
}

open(MTFILE, "$mtfile");
my @mtempl = <MTFILE>;
close MTFILE;

my $dbq = $dbase->prepare("SELECT users.id, lastname, users.name, email, phone1,  tariffs.description FROM users,tariffs 
		WHERE users.tariff=tariffs.id");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
	my $balance = 0;
	my $sdbq = $dbase->prepare("SELECT SUM(value) AS sum FROM cash WHERE userid='$row->{'id'}' AND type='3'");
	$sdbq->execute();
	if(my $srow = $sdbq->fetchrow_hashref())
	{
		$balance = $balance + (defined $srow->{'sum'} ? $srow->{'sum'} : 0);
	}
	$sdbq = $dbase->prepare("SELECT SUM(value) AS sum FROM cash WHERE userid='$row->{'id'}' AND type>='4'");
	$sdbq->execute();
	if(my $srow = $sdbq->fetchrow_hashref())
	{
		$balance = $balance - (defined $srow->{'sum'} ? $srow->{'sum'} : 0);
	}
	if($balance < $limit)
	{
		if(!$quiet)
		{
			print STDOUT "$row->{'id'}\t($balance)\t$row->{'lastname'} $row->{'name'}\n";
		}
		if($row->{'email'})
		{
			if(!$quiet)
			{
				print STDOUT "Sending e-mail to $row->{'email'}\n";
			}
			my @desttempl = @mtempl;
			my $subject = $mailsubject;
			$subject =~ s/\%B/$balance/g;
			{ my $amount = -$balance; $subject =~ s/\%b/$amount/g; } 
			
			my $smtp = Net::SMTP->new("$smtpserver");
			$smtp->mail("$mailfrom");

			if($debugemail)
			{
				$smtp->to($debugemail);
			}else{
				$smtp->to($row->{'email'});
			}

			$smtp->data();

			$smtp->datasend("From: $mailfname <$mailfrom>\n");
			$smtp->datasend("To: $row->{'name'} $row->{'lastname'} <$row->{'email'}>\n");
			$smtp->datasend("Subject: $subject\n");
			$smtp->datasend("User-Agent: lms\-notify v. $_version\n");
			$smtp->datasend("Content-type: text/plain; charset=ISO-8859-2\n");
			$smtp->datasend("Mime-Version: 1.0\n");
			$smtp->datasend("Content-transfer-encoding: 8bit\n");
			$smtp->datasend("\n");

			foreach my $line (@desttempl)
			{
				$line =~ s/\%B/$balance/g;
				{ 	
				my $amount = -$balance;
				$line =~ s/\%b/$amount/g; 	
				}

				{ 
				my $year = strftime( "%Y", localtime());
				$line =~ s/\%date-y/$year/; 
				}

		                {
               			my $phone = $row->{'phone1'};
			        $line =~ s/\%phone1/$phone/;
				}
				
				{
				my $month = strftime ( "%m", localtime()) ; 
				$line =~ s/\%date-m/$month/;
				}

				
				{
				my $month_name = strftime ( "%B", localtime()) ; 
				$line =~ s/\%date_month_name/$month_name/;
				}
				
				{
				my $desc = $row->{'description'}; 
				$line =~ s/\%desc/$desc/;
				}

				if ($line =~ /\%saldo/) {
					my $saldo_dbq = $dbase->prepare("SELECT SUM(value) FROM cash WHERE 
					userid='$row->{'id'}' AND (type>='4' )") ; 
					$saldo_dbq->execute();
					my $ow_s = $saldo_dbq->fetchrow_hashref();
					$line =~ s/\%saldo/$ow_s->{'SUM(value)'}/;
				}


				if ($line =~ /\%abonament/) {
					my $saldo_dbq = $dbase->prepare("SELECT users.id,tariffs.value,tariff FROM users,tariffs WHERE 
					users.id='$row->{'id'}' AND tariffs.id=users.tariff") ; 
					$saldo_dbq->execute();
					my $ow_s = $saldo_dbq->fetchrow_hashref();
					$line =~ s/\%abonament/$ow_s->{'value'}/;
				}

				if ($line =~ /\%last_10_in_a_table/ ) {
					my $last10_dbq = $dbase->prepare("SELECT comment,value,type,time FROM cash WHERE 
							userid='$row->{'id'}' AND (type>='3' ) ORDER BY time DESC LIMIT 0,10") ;
				# ok, now we are going to rise up system's load
					$last10_dbq->execute();
 					$smtp->datasend("Data       | Opis                                                 | Warto\n");
					$smtp->datasend("-----------+------------------------------------------------------+---------\n");
					while (my $row_s = $last10_dbq->fetchrow_hashref())
						{
						my $aqq=($row_s->{'type'}==3)?-1:1;
						my $op_amount=sprintf("%8.2f", $aqq*$row_s->{'value'});
						my @typy = ('a', 'b', 'Abonament', 'Wpata', 'Instalacja', 'Inne');
						my $op_type=@typy[$row_s->{'type'}] || "$row_s->{'type'}";
						my $for_what = sprintf("%-52s",$row_s->{'comment'} || $op_type);
						my $op_time=strftime( "%Y-%m-%d", localtime($row_s->{'time'}) );

						$smtp->datasend("$op_time | $for_what | $op_amount \n");
						}
					$smtp->datasend("-----------+------------------------------------------------------+---------\n");
				} else {$smtp->datasend("$line"); }
			}
			$smtp->dataend();
			$smtp->quit;
		}else{
			if(!$quiet)
			{
				print STDOUT "This user don't have any e-mail in database, call him if You can ($row->{'phone1'}).\n";
			}
		}
	}
	
}
$dbase->disconnect();
