#!/usr/bin/perl

use CGI;
use DBI;
##use strict;
unshift(@INC, '.');
use ipac_cfg;

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";             
my $cust_id = "0";
my $tar_id;
my $price = "0.00000012345";
my $pr_type;
my $active_nocash;
my $dbh;
my $refer=$q->referer();
my $sth;
my $detailed;
my $oldp;
my @row;
my %pr_types = ('0'=>'free', '1'=>'per byte', '15'=>'per month');
my %yes_no = ('0'=>'no', '1'=>'yes');

my $user = $ENV{REMOTE_USER};

if ($ENV{HTTPS} ne "on" || $user ne "admin") {
    print $q->header(-type=>"text/html;", -status=>'403 Forbidden'),
	    $q->start_html('Forbidden'),
	    $q->h1('You dont have permission to access this script'),
	    $q->end_html;
    exit;
}

if ($q->param('cust') && $q->param('tar')) {
	$cust_id = $q->param('cust');
	$tar_id = $q->param('tar');
} else {
	exit;
}

if ($q->param('active') ne "") {
	$oldp=$q->param('refer');
	print "Location: $oldp\n\n";
}

print  $q->header,
       $q->start_html(-title=>'Edit tariff');

print "<table><tr><td>".
	"<IMG BORDER=0 ALT=\"ipac-ng logo\"".
	"SRC=\"$path/poweredby.png\"></td><td>".
	"<center><font color=\"#ff0000\" size=6>ipac-ng edit tariff</font></center>".
	"</td></tr></table><A HREF=\"http://sf.net/projects/ipac-ng\">".
    	"ipac-ng home page</A>&nbsp;&nbsp;";
print "<A HREF=\"$path/stat\">Go to main page</A>&nbsp;&nbsp;";
print $ipac_cfg::menu;

$dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,
	                             { RaiseError => 1, AutoCommit => 0 });

my $sth = $dbh->prepare("SELECT login,tariff_name,price,pr_type,last_paid, ".
		    "active_nocash,tariffs.detailed ".
		    "from customers,tariffs,tariffs_names where ".
		    "customers.cust_id=tariffs.cust_id and ".
		    "tariffs.tariff_id=tariffs_names.tariff_id and ".
		    "tariffs.cust_id = ? and tariffs.tariff_id = ?");
$sth->execute($cust_id, $tar_id) or die $sth->errstr;

print "<table align=center CELLPADDING=2 CELLSPACING=1 BORDER=1>";
print "<tr>", $q->td(['Login', 'Tariff', 'Price', 'Type', 'Last paid', 
	    'Allow debt', 'Detailed logging']);
print "</tr>";
@row = $sth->fetchrow_array;
$sth->finish;
print $q->start_form;
    
print "<tr>";
print $q->td(["&nbsp;$row[0]",
	    "&nbsp;$row[1]",
	    $q->textfield('price', $row[2], 10, 15, -override=>1),
	    $q->popup_menu('pr_type', ['0', '1', '15'], $row[3], \%pr_types),
	    "&nbsp;$row[4]",
	    $q->popup_menu('active', ['0', '1'], $row[5], \%yes_no),
	    $q->popup_menu('detail', ['0', '1'], $row[6], \%yes_no)]),
	$q->hidden('cust', $cust_id),
	$q->hidden('refer', $refer),
	$q->hidden('tar', $tar_id);
print "</tr></table>";
print "<center>", $q->submit(), "</center>";
print $q->endform();

$price = $q->param('price');
$pr_type = $q->param('pr_type');
$active_nocash = $q->param('active');
$detailed=$q->param('detail');

if ($price eq "") {
	print $q->end_html;
	$dbh->disconnect or warn $dbh->errstr;
	exit;
}

$dbh->do(q{update tariffs set price=?, pr_type=?, active_nocash=?, detailed=?
	    where cust_id=? and tariff_id=?}, undef,
	    $price, $pr_type, $active_nocash, $detailed, $cust_id, $tar_id)
						    or die $dbh->errstr;
$dbh->commit();
$dbh->disconnect  or warn $dbh->errstr;
reconfig();
print $q->end_html;
