<?php
# This has been designed to work with A2Billing v 1.4.1 but could be used as a starting point for other versions.
# After you have imported the rate card you must set the correct trunk on the card. This is not done by the import process.
# Make sure you change your database
# by SysAdminMan.net
# v1.0 - 4/8/09
# This is designed to work with callwithus.com consumer rates
# This is a text label for the rate card name
$supplier = "CallWithUs";
# Sell rates will be this % more than the buy rates
$markup = 50;
# Database settings for your a2billing datebase
# If you're not sure what these are you should be able to find them in /etc/a2billing.conf
$db = "mya2billing";
$dbuser = "a2billing db user";
$dbpass = "a2billing db password";
$datenow = date("Y-m-d");
echo "\n\n";
echo "Removing old download files\n\n";
echo exec('rm -f cwurates.zip');
echo exec('rm -f cwurates.csv');
echo "Downloading new rate file\n";
echo exec('wget http://www.callwithus.com/cwurates.zip');
echo "Extracting new rate file\n";
echo exec('unzip cwurates.zip');
echo "\n\n";
# Set some static variables for ratecard creation
$tariffname = "$supplier +$markup% ".date("Y-m-d G:i:s");
$description = "Ratecard imported with PHP script from SysAdminMan";
# Set some static variables for ratecard import
$id = "";
$idtariffplan = "";
$dialprefix = "";
$destination = "";
$buyrate = "";
$buyrateinitblock = "";
$buyrateincrement = "";
$rateinitial = "";
$initblock = 60;
$billingblock = 60;
$connectcharge = 0;
$disconnectcharge = 0;
$stepchargea = 0;
$chargea = 0;
$timechargea = 0;
$billingblocka = 0;
$stepchargeb = 0;
$chargeb = 0;
$timechargeb = 0;
$billingblockb = 0;
$stepchargec = 0;
$chargec = 0;
$timechargec = 0;
$billingblockc = 0;
$startdate = date("Y-m-d h:m:s");
$stopdate = "2020-01-01 00:00:00";
$starttime = 0;
$endtime = 10079;
$id_trunk = -1;
$musiconhold = "";
$id_outbound_cidgroup = -1;
# Setup database connection
$con = mysql_connect("localhost",$dbuser,$dbpass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db, $con);
# Create the rate card
echo "Creating new rate card - ".$tariffname."\n\n";
mysql_query("INSERT INTO cc_tariffplan (tariffname,description,startingdate,expirationdate) VALUES ('$tariffname','$description','$startdate','$stopdate')")
or die(mysql_error());
# Extract the id of the tariff plan we just created
echo "Retreiving ID of newly created rate card\n\n";
$query = mysql_query("SELECT id FROM cc_tariffplan where tariffname = '$tariffname'")
or die(mysql_error());
$row=mysql_fetch_assoc($query);
$idtariffplan = $row['id'];
# Read CSV file
echo "Reading downloaded rate file\n\n";
$file_handle = fopen("cwurates.csv", "r");
echo "Creating dial prefixes and rates. Please wait ...\n\n";
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024, ';');
# Set the required variables from the import file
$dialprefix = $line_of_text[0];
$destination = $line_of_text[1];
$buyrate = $line_of_text[2];
$buyrateinitblock = $line_of_text[3];
$buyrateincrement = $line_of_text[4];
$rateinitial = ($buyrate/100*$markup)+$buyrate;
# Create prefix and description in prefix table
mysql_query("INSERT IGNORE INTO cc_prefix (prefix,destination) VALUES ('$dialprefix','$destination')")
or die(mysql_error());
# Create rate in rate card
mysql_query("INSERT INTO cc_ratecard (id,idtariffplan,dialprefix,destination,buyrate,buyrateinitblock,buyrateincrement,rateinitial,initblock,billingblock,connectcharge,disconnectcharge,stepchargea,chargea,timechargea,billingblocka,stepchargeb,chargeb,timechargeb,billingblockb,stepchargec,chargec,timechargec,billingblockc,startdate,stopdate,starttime,endtime,id_trunk,musiconhold,id_outbound_cidgroup)
VALUES ('$id','$idtariffplan','$dialprefix','$dialprefix','$buyrate','$buyrateinitblock','$buyrateincrement','$rateinitial','$initblock','$billingblock','$connectcharge','$disconnectcharge','$stepchargea','$chargea','$timechargea','$billingblocka','$stepchargeb','$chargeb','$timechargeb','$billingblockb','$stepchargec','$chargec','$timechargec','$billingblockc','$startdate','$stopdate','$starttime','$endtime','$id_trunk','$musiconhold','$id_outbound_cidgroup')")
or die(mysql_error());
}
fclose($file_handle);
mysql_close($con);
?>