Index: astcc-admin.cgi
===================================================================
RCS file: /usr/cvsroot/astcc/astcc-admin.cgi,v
retrieving revision 1.15
diff -u -r1.15 astcc-admin.cgi
--- astcc-admin.cgi 15 May 2005 05:51:28 -0000 1.15
+++ astcc-admin.cgi 23 Jul 2005 15:27:08 -0000
@@ -20,13 +20,15 @@
use CGI qw/:standard/;
use DBI;
use POSIX qw(strftime);
+use POSIX qw(ceil floor);
print header;
my $copyright = "Asterisk™ Calling Card Manager by Mark Spencer, ©2004 Digium, Inc.";
-@modes = ( "Home", "Brands", "Cards", "Trunks", "Routes", "Configure", "Users_Configure", "IAX_Friends", "SIP_Friends" );
+@modes = ( "Home", "Calc_Charge", "Brands", "Cards", "Trunks", "Routes", "Configure", "Users_Configure", "IAX_Friends", "SIP_Friends" );
@languages = ("en", "de", "fr", "es");
@techs = ( "SIP", "IAX2", "Zap", "Local" );
@incs = ("6", "60");
+@yesno = ("YES", "NO");
my $dbh;
sub timestamp() {
@@ -94,11 +96,6 @@
return 0;
}
-sub users_update_db() {
- $dbh->do("ALTER TABLE cdrs ADD COLUMN callstart CHAR(24)");
- return 0;
-}
-
sub update_db() {
$dbh->do("ALTER TABLE cdrs ADD COLUMN callstart CHAR(24)");
$dbh->do("ALTER TABLE cards ADD COLUMN pin INTEGER");
@@ -944,6 +941,7 @@
$config{maintname} = param('maintname');
$config{pinstatus} = param('pinstatus');
$config{pinlength} = param('pinlength');
+ $config{debug} = param('debug');
&save_config();
$activity = "Configuration saved...";
if ($action eq "Create Database") {
@@ -980,6 +978,7 @@
$body .= "
CDR Text for maintainence fee | " . textfield(-name => 'maintname', -default => $config{maintname}) . " |
";
$body .= "Pin Length (4-20) | " . textfield(-name => 'pinlength', -default => $config{pinlength}) . " |
";
$body .= "Require Pins (Yes/NO) | " . textfield(-name => 'pinstatus', -default => $config{pinstatus}) . " |
";
+ $body .= "Debug Mode | " . popup_menu(-name => "debug", -values => \@yesno, -default=> $config{debug}) . " |
";
$body .= " " . submit(-name => 'action', -value => 'Save') . " | " . submit( -name => 'action', -value => 'Create Database') . " |
";
$body .= " " . submit(-name => 'action', -value => 'Update Database') . " |
";
$body .= "";
@@ -990,7 +989,7 @@
my $body = start_form;
my $activity = "
";
my $action = param('action');
- if (($action eq "Save") || ($action eq "Create Database") || ($action eq "Update Database")) {
+ if (($action eq "Save") || ($action eq "Create Database")) {
$users_config{dbuser} = param('dbuser');
$users_config{dbpass} = param('dbpass');
$users_config{dbname} = param('dbname');
@@ -1011,12 +1010,6 @@
} else {
$activity = "Database creation failed!";
}
- } elsif ($action eq "Update Database") {
- if (!&users_update_db()) {i
- $activity = "Database Updated!";
- } else {
- $activity = "Database Update failed!";
- }
}
&users_connect_db;
}
@@ -1036,7 +1029,6 @@
$body .= "Admin Email | " . textfield(-name => 'emailadd', -default => $users_config{emailadd}) . " |
";
$body .= "Email Program | " . textfield(-name => 'mailprog', -default => $users_config{mailprog}) . " |
";
$body .= " " . submit(-name => 'action', -value => 'Save') . " | " . submit( -name => 'action', -value => 'Create Database') . " |
";
- $body .= " " . submit(-name => 'action', -value => 'Update Database') . " |
";
$body .= "";
return $body;
}
@@ -1340,6 +1332,63 @@
return $body;
}
+sub build_calc_charge() {
+ my ($total, $used, $pennies, $count, $brand, $language, $inc, $markup);
+ return "Cannot calculate charges until database is configured" unless $dbh;
+ my $brands = &list_brands();
+ return "Please define at least one brand before creating cards" unless $brands;
+ if (param('action') eq "Calc_Rate...") {
+ $phoneno = param('Number');
+ $minutes = param('Minutes');
+ $seconds = $minutes * 60;
+ $brand = param('brand');
+
+ # Retrieve brand specifications
+ my $sth = $dbh->prepare("SELECT * FROM brands WHERE name = " . $dbh->quote($brand));
+ $sth->execute;
+ if ($row = $sth->fetchrow_hashref) {
+ ($language, $increment, $markup) = ($row->{language}, $row->{inc}, $row->{markup});
+ }
+ $sth->finish;
+ $charge = &calccost($phoneno, $seconds,$increment,$markup);
+ eval { $charge = $charge / 10000 };
+ $status = "Charge for $minutes to $phoneno \n";
+ $status .= "will be $charge \n";
+ }
+ $body = "" . start_form . "$status |
";
+ $body .= "Brand:" . $brands . " Phone Number " . hidden(-name => "mode", -value => "Cards") .
+ textfield(-name => "Number", -size => 20) . " " .
+ textfield(-name => "Minutes", -size => 4) . " Minutes " .
+ submit(-name => "action", -value => "Calc_Rate...") . " |
";
+ $body .= "
";
+ return $body;
+}
+
+
+sub calccost() {
+ my ($number, $answeredtime, $increment, $markup) = @_;
+ my $numdata = &getphone($number);
+ $adjcost = ($numdata->{cost} * (10000 + $markup)) / 10000;
+ $adjconn = ($numdata->{connectcost} * (10000 + $markup)) / 10000;
+ my $cost;
+ my $adjtime = eval { $adjtime = int((($answeredtime - $numdata->{includedseconds}) + $increment - 1) / $increment) * $increment;
+ return $adjtime };
+ print STDERR "Adjusted time is $adjtime, cost is $adjcost with $adjconn fee\n";
+ eval { $cost = int($adjcost * $adjtime / 60) };
+ $cost += $adjconn;
+ print STDERR "Total cost is $cost\n";
+ return $cost;
+}
+
+sub getphone() {
+ my ($number) = @_;
+ my $sth = $dbh->prepare("SELECT * FROM routes WHERE " . $dbh->quote($number) . " RLIKE pattern ORDER BY LENGTH(pattern) DESC");
+ $sth->execute;
+ $res = $sth->fetchrow_hashref;
+ $sth->finish;
+ return $res;
+}
+
sub build_body() {
my ($mode) = @_;
@@ -1350,6 +1399,7 @@
return &build_brands() if ($mode eq "Brands");
return &build_trunks() if ($mode eq "Trunks");
return &build_routes() if ($mode eq "Routes");
+ return &build_calc_charge() if ($mode eq "Calc_Charge");
if ($config{'friendsdb'} eq "YES") {
return &build_user_configure() if ($mode eq "Users_Configure");
return &build_iax_friends() if ($mode eq "IAX_Friends");