fix odd billing currency error when using different currencies

This commit is contained in:
luis daniel lucio quiroz
2014-11-25 22:22:28 +00:00
parent fa7826da65
commit 2b43ddd76f
2 changed files with 70 additions and 18 deletions

View File

@@ -218,14 +218,15 @@
//billing information
if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/app/billing/app_config.php")){
$db2 = new database;
$lcr_currency = 'USD';
$destination_number = check_str(urldecode($row->caller_profile->destination_number));
$destination_number_serie = number_series($destination_number);
$lcr_currency = (strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD');
switch(check_str(urldecode($xml->variables->call_direction))){
case "outbound":
$destination_number = check_str(urldecode($xml->variables->lcr_query_digits));
$destination_number_serie = number_series($destination_number);
$database->fields['carrier_name'] = check_str(urldecode($xml->variables->lcr_carrier));
$sql_rate ="SELECT v_lcr.connect_increment, v_lcr.talk_increment, v_lcr.currency FROM v_lcr, v_carriers WHERE v_carriers.carrier_name = '".$xml->variables->lcr_carrier."' AND v_lcr.rate=".$xml->variables->lcr_rate." AND v_lcr.lcr_direction = '".check_str(urldecode($xml->variables->call_direction))."' AND digits IN ($destination_number_serie) AND v_lcr.carrier_uuid = v_carriers.carrier_uuid ORDER BY digits DESC, rate ASC limit 1";
$sql_user_rate = "SELECT connect_increment, talk_increment FROM v_lcr WHERE carrier_uuid IS NULL AND v_lcr.lcr_direction = '".check_str(urldecode($xml->variables->call_direction))."' AND digits IN ($destination_number_serie) ORDER BY digits DESC, rate ASC limit 1";
$sql_user_rate = "SELECT currency, connect_increment, talk_increment FROM v_lcr WHERE carrier_uuid IS NULL AND v_lcr.lcr_direction = '".check_str(urldecode($xml->variables->call_direction))."' AND digits IN ($destination_number_serie) ORDER BY digits DESC, rate ASC limit 1";
if ($debug) {
echo "sql_rate: $sql_rate\n";
echo "sql_user_rate: $sql_user_rate\n";
@@ -235,6 +236,9 @@
$db2->result = $db2->execute();
// print_r($db2->result);
$lcr_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):'USD');
$lcr_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
$lcr_rate = (strlen($xml->variables->lcr_rate)?$xml->variables->lcr_rate:0);
$lcr_first_increment = (strlen($db2->result[0]['connect_increment'])?check_str($db2->result[0]['connect_increment']):60);
$lcr_second_increment = (strlen($db2->result[0]['talk_increment'])?check_str($db2->result[0]['talk_increment']):60);
@@ -246,12 +250,17 @@
$lcr_user_rate = (strlen($xml->variables->lcr_user_rate)?$xml->variables->lcr_user_rate:0.01);
$lcr_user_first_increment = (strlen($db2->result[0]['connect_increment'])?check_str($db2->result[0]['connect_increment']):60);
$lcr_user_second_increment = (strlen($db2->result[0]['talk_increment'])?check_str($db2->result[0]['talk_increment']):60);
$lcr_user_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
unset($db2->sql);
unset($db2->result);
break;
case "inbound":
$sql_user_rate = "SELECT v_lcr.rate, v_lcr.connect_increment, v_lcr.talk_increment, v_lcr.currency FROM v_lcr WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.enabled='true' AND v_lcr.lcr_direction='inbound' AND v_lcr.digits IN (".number_series($n).") ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1";
$caller_number = check_str(urldecode($row->caller_profile->caller_id_number));
$caller_number_serie = number_series($caller_number);
$sql_user_rate = "SELECT v_lcr.currency, v_lcr.rate, v_lcr.connect_increment, v_lcr.talk_increment, v_lcr.currency FROM v_lcr WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.enabled='true' AND v_lcr.lcr_direction='inbound' AND v_lcr.digits IN (".$caller_number_serie.") ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1";
if ($debug) {
echo "sql_user_rate: $sql_user_rate\n";
}
@@ -260,10 +269,15 @@
$db2->result = $db2->execute();
// If selling rate is found, then we fill with data, otherwise rate will be 0
$lcr_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):'USD');
$lcr_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
$lcr_user_rate = (strlen($db2->result[0]['rate']))?strlen($db2->result[0]['rate']):0;
$lcr_user_first_increment = (strlen($db2->result[0]['connect_increment']))?strlen($db2->result[0]['connect_increment']):60;
$lcr_user_second_increment = (strlen($db2->result[0]['talk_increment']))?strlen($db2->result[0]['talk_increment']):60;
$lcr_user_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
// Actually, there is no way to detect what carrier is the calling comming from using current information
$lcr_rate = 0; $lcr_first_increment = 0; $lcr_second_increment = 0;
@@ -271,7 +285,9 @@
unset($db2->result);
break;
case "local":
$sql_user_rate = "SELECT v_lcr.rate, v_lcr.connect_increment, v_lcr.talk_increment, v_lcr.currency FROM v_lcr WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.enabled='true' AND v_lcr.lcr_direction='local' AND v_lcr.digits IN (".number_series($n).") ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1";
$destination_number = check_str(urldecode($xml->variables->lcr_query_digits));
$destination_number_serie = number_series($destination_number);
$sql_user_rate = "SELECT v_lcr.currency, v_lcr.rate, v_lcr.connect_increment, v_lcr.talk_increment, v_lcr.currency FROM v_lcr WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.enabled='true' AND v_lcr.lcr_direction='local' AND v_lcr.digits IN (".$destination_number_serie.") ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1";
if ($debug) {
echo "sql_user_rate: $sql_user_rate\n";
}
@@ -280,10 +296,16 @@
$db2->result = $db2->execute();
// If selling rate is found, then we fill with data, otherwise rate will be 0
$lcr_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):'USD');
$lcr_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
$lcr_user_rate = (strlen($db2->result[0]['rate']))?strlen($$db2->result[0]['rate']):0;
$lcr_user_first_increment = (strlen($db2->result[0]['connect_increment']))?strlen($db2->result[0]['connect_increment']):60;
$lcr_user_second_increment = (strlen($db2->result[0]['talk_increment']))?strlen($db2->result[0]['talk_increment']):60;
$lcr_user_currency = (strlen($db2->result[0]['currency'])?check_str($db2->result[0]['currency']):
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
// Actually, internal calls have 0 cost
$lcr_rate = 0; $lcr_first_increment = 0; $lcr_second_increment = 0;
@@ -297,7 +319,7 @@
$time = check_str(urldecode($xml->variables->billsec));
$call_buy = call_cost($lcr_rate, $lcr_first_increment, $lcr_second_increment, $time);
$call_sell = call_cost($lcr_user_rate, $lcr_user_first_increment, $lcr_user_second_increment, $time);
// Costs/Sell call is in original LCR currency, needs to be converted
// Costs/Sell call are in original LCR currency, they need to be converted
$database->fields['call_buy'] = check_str($call_buy);
$database->fields['call_sell'] = check_str($call_sell);
@@ -320,9 +342,10 @@
echo "t $time\n";
echo "b r:$lcr_rate - $lcr_first_increment - $lcr_first_increment = $call_buy\n";
echo "s r:$lcr_user_rate - $lcr_user_first_increment - $lcr_user_second_increment = $call_sell\n";
echo "lc $lcr_currency\n";
echo "ac $actual_currency\n";
echo "bc $billing_currency\n";
echo "lcr currency $lcr_currency\n";
echo "actual currency $actual_currency\n";
echo "user currency $lcr_user_currency\n";
echo "billing currency $billing_currency\n";
}
unset($database->sql);
@@ -341,7 +364,7 @@
}
// Lets convert rate from lcr_currency to billing_currency
$billing_call_sell = currency_convert($call_sell, $billing_currency, $actual_currency);
$billing_call_sell = currency_convert($call_sell, $billing_currency, $lcr_user_currency);
if ($debug) {
echo "bcs: $billing_call_sell $billing_currency\n";

View File

@@ -506,19 +506,48 @@ else {
$database->sql = "SELECT currency FROM v_billings WHERE type_value='$accountcode'";
$database->result = $database->execute();
$billing_currency = (strlen($database->result[0]['currency'])?$database->result[0]['currency']:'USD');
$billing_currency = (strlen($database->result[0]['currency'])?$database->result[0]['currency']:
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
);
unset($database->sql);
unset($database->result);
$sell_price = $row['call_sell'];
$lcr_direction = (strlen($row['direction'])?$row['direction']:"outbound");
$n = (($lcr_direction == "inbound")?$row['caller_id_number']:$row['destination_number']);
$xml_string = trim($row["xml"]);
$json_string = trim($row["json"]);
if (strlen($xml_string) > 0) {
$format = "xml";
}
if (strlen($json_string) > 0) {
$format = "json";
}
try {
if ($format == 'json') {
$array = json_decode($json_string,true);
}
if ($format == 'xml') {
$array = json_decode(json_encode((array)simplexml_load_string($xml_string)),true);
}
}
catch(Exception $e) {
echo $e->getMessage();
}
$n = (($lcr_direction == "inbound")?
check_str(urldecode($array["caller_profile"]["caller_id_number"])):
check_str(urldecode($array["variables"]["lcr_query_digits"]))
);
$database->table = "v_lcr";
$database->sql = "SELECT currency FROM v_lcr WHERE v_lcr.carrier_uuid= '' AND v_lcr.enabled='true' AND v_lcr.lcr_direction='$lcr_direction' AND v_lcr.digits IN (".number_series($n).") ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1";
$database->sql = "SELECT currency FROM v_lcr WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.enabled='true' AND v_lcr.lcr_direction='$lcr_direction' AND v_lcr.digits IN (".number_series($n).") ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1";
$database->result = $database->execute();
//print "<pre>"; print_r($database->result); print "[".$database->result[0]['currency']."]"; print "</pre>";
// print "<pre>"; print $database->sql . ":";print "[".$database->result[0]['currency']."]"; print_r($array); print "</pre>";
$lcr_currency = ((is_string($database->result[0]['currency']) && strlen($database->result[0]['currency']))?$database->result[0]['currency']:'USD'); //billed currency
$lcr_currency = ((is_string($database->result[0]['currency']) && strlen($database->result[0]['currency']))?$database->result[0]['currency']:
(strlen($_SESSION['billing']['currency']['text'])?$_SESSION['billing']['currency']['text']:'USD')
); //billed currency
unset($database->sql);
unset($database->result);
$price = currency_convert($sell_price, $billing_currency, $lcr_currency);
@@ -558,4 +587,4 @@ else {
//show the footer
require_once "resources/footer.php";
?>
?>