[Asterisk-cvs] astcc astcc-admin.cgi,1.15,1.16

kpfleming kpfleming
Mon Aug 22 20:56:59 CDT 2005


Update of /usr/cvsroot/astcc
In directory mongoose.digium.com:/tmp/cvs-serv10848

Modified Files:
	astcc-admin.cgi 
Log Message:
add "calculate cost" button to web page (issue #4480)


Index: astcc-admin.cgi
===================================================================
RCS file: /usr/cvsroot/astcc/astcc-admin.cgi,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- astcc-admin.cgi	15 May 2005 05:51:28 -0000	1.15
+++ astcc-admin.cgi	23 Aug 2005 00:59:03 -0000	1.16
@@ -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.";
- at modes = ( "Home", "Brands", "Cards", "Trunks", "Routes", "Configure", "Users_Configure", "IAX_Friends", "SIP_Friends" );
+ at 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");
+ at 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 .= "<tr><td>CDR Text for maintainence fee</td><td>" . textfield(-name => 'maintname', -default => $config{maintname}) . "</td></tr>";
 	$body .= "<tr><td>Pin Length  (4-20) </td><td>" . textfield(-name => 'pinlength', -default => $config{pinlength}) . "</td></tr>";
 	$body .= "<tr><td>Require Pins (Yes/NO)</td><td>" . textfield(-name => 'pinstatus', -default => $config{pinstatus}) . "</td></tr>";
+	$body .= "<tr><td>Debug Mode</td><td>" . popup_menu(-name => "debug", -values => \@yesno, -default=> $config{debug}) . "</td></tr>";
 	$body .= "<tr><td align=left>&nbsp;" . submit(-name => 'action', -value => 'Save') . "</td><td align=right>" . submit( -name => 'action', -value => 'Create Database') . "&nbsp;</td></tr>";
 	$body .= "<tr><td align=left>&nbsp;" . submit(-name => 'action', -value => 'Update Database') . "&nbsp;</td></tr>";
 	$body .= "</table>";
@@ -990,7 +989,7 @@
 	my $body = start_form;
 	my $activity = "<br>";
 	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 .= "<tr><td>Admin Email</td><td>" . textfield(-name => 'emailadd', -default => $users_config{emailadd}) . "</td></tr>";
 	$body .= "<tr><td>Email Program</td><td>" . textfield(-name => 'mailprog', -default => $users_config{mailprog}) . "</td></tr>";
 	$body .= "<tr><td align=left>&nbsp;" . submit(-name => 'action', -value => 'Save') . "</td><td align=right>" . submit( -name => 'action', -value => 'Create Database') . "&nbsp;</td></tr>";
-	$body .= "<tr><td align=left>&nbsp;" . submit(-name => 'action', -value => 'Update Database') . "&nbsp;</td></tr>";
 	$body .= "</table>";
 	return $body;
 }
@@ -1340,6 +1332,66 @@
 	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 = "<table><tr><td>" . start_form . "$status</td></tr>";
+	$body .= "<tr><td>Brand:" . $brands .  " Phone Number&nbsp;&nbsp;" . hidden(-name => "mode", -value => "Cards") .
+		textfield(-name => "Number", -size => 20) . "&nbsp;&nbsp;&nbsp;&nbsp;" .
+		textfield(-name => "Minutes", -size => 4) . "&nbsp;&nbsp;Minutes&nbsp;&nbsp;" .
+		submit(-name => "action", -value => "Calc_Rate...") . "</td></tr>";
+	$body .= "</table>";
+	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 };
+	if ($adjtime < 0) {
+		$adjtime = 0;
+	}
+    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 +1402,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");	




More information about the svn-commits mailing list