Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minimize negative balance due to change sin 1.4-beta2 #524

Merged
merged 8 commits into from Jun 25, 2016
3 changes: 3 additions & 0 deletions db/playsms-upgrade_1.3.1_to_1.4.sql
Expand Up @@ -12,3 +12,6 @@ UPDATE `playsms_tblSMSOutgoing` A INNER JOIN `playsms_tblUser` B ON A.uid=B.uid

-- version
UPDATE `playsms_tblRegistry` SET `registry_value` = '1.4' WHERE `registry_group` = 'core' AND `registry_family` = 'config' AND `registry_key` = 'playsms_version' ;

-- tblUser
ALTER TABLE `playsms_tblUser` ADD `adhoc_credit` DECIMAL(13,3) NOT NULL DEFAULT '0.000' AFTER `credit`;
1 change: 1 addition & 0 deletions db/playsms.sql
Expand Up @@ -1509,6 +1509,7 @@ CREATE TABLE `playsms_tblUser` (
`country` int(11) NOT NULL DEFAULT '0',
`zipcode` varchar(10) NOT NULL DEFAULT '',
`credit` decimal(13,3) NOT NULL DEFAULT '0.000',
`adhoc_credit` decimal(13,3) NOT NULL DEFAULT '0.000',
`datetime_timezone` varchar(30) NOT NULL DEFAULT '',
`language_module` varchar(10) NOT NULL DEFAULT '',
`fwd_to_mobile` int(11) NOT NULL DEFAULT '0',
Expand Down
2 changes: 1 addition & 1 deletion web/lib/fn_billing.php
Expand Up @@ -26,7 +26,7 @@
* @param float $credit
* @return boolean TRUE if posted
*/
function billing_post($smslog_id,$rate,$credit,$count,$charge) {
function billing_post($smslog_id,$rate,$count,$charge) {
$ret = core_call_hook();
return $ret;
}
Expand Down
11 changes: 1 addition & 10 deletions web/plugin/core/sendsms/fn.php
Expand Up @@ -478,16 +478,7 @@ function sendsms_process($smslog_id, $sms_sender, $sms_footer, $sms_to, $sms_msg
if (rate_cansend($username, strlen($sms_msg . $sms_footer), $unicode, $sms_to)) {
$p_status = 0;
} else {
_log("end with fail not enough credit smslog_id:" . $smslog_id, 2, "sendsms_process");
$ret['status'] = true;

// set TRUE to stop queue
$ret['to'] = $sms_to;
$ret['smslog_id'] = $smslog_id;
$ret['p_status'] = 2;

// set failed
return $ret;
$p_status = 2;
}

// message entering this proc already stripslashed, we need to addslashes it before saving to db
Expand Down
73 changes: 72 additions & 1 deletion web/plugin/feature/credit/fn.php
Expand Up @@ -245,7 +245,7 @@ function _credit_update_credit($uid, $balance) {
$balance = (float) $balance;

if ($c_uid = (int) $uid) {
$db_query = "UPDATE " . _DB_PREF_ . "_tblUser SET c_timestamp='" . time() . "', credit='$balance' WHERE uid='$c_uid' AND flag_deleted='0'";
$db_query = "UPDATE " . _DB_PREF_ . "_tblUser SET c_timestamp='" . time() . "', credit='$balance', adhoc_credit='$balance' WHERE uid='$c_uid' AND flag_deleted='0'";
dba_query($db_query);
}
}
Expand Down Expand Up @@ -289,3 +289,74 @@ function credit_hook_rate_update() {

_credit_rate_update();
}

// low credit notification
function _credit_getbyuid($uid) {
$balance = 0;

$username = user_uid2username($uid);
if ($username) {
$balance = credit_hook_rate_getusercredit($username);
}

return $balance;
}

function _credit_low_notif() {
global $core_config;

$db_query = "SELECT uid, parent_uid FROM " . _DB_PREF_ . "_tblUser WHERE flag_deleted='0'";
$db_result = dba_query($db_query);
while ($db_row = dba_fetch_array($db_result)) {

// sender's
$uid = $db_row['uid'];
$balance = _credit_getbyuid($uid);

// if balance under credit lowest limit and never been notified then notify admins, parent_uid and uid
$credit_lowest_limit = (float) $core_config['main']['credit_lowest_limit'];
$reg = registry_search($uid, 'feature', 'credit', 'lowest_limit_notif');
$notified = ($reg['feature']['credit']['lowest_limit_notif'] ? TRUE : FALSE);

if ($balance && $credit_lowest_limit && ($balance <= $credit_lowest_limit) && !$notified) {

// set notified
registry_update($uid, 'feature', 'credit', array(
'lowest_limit_notif' => TRUE
));

// notif admins
$admins = user_getallwithstatus(2);
foreach ($admins as $admin) {
$credit_message_to_admins = sprintf(_('Username %s with account ID %d has reached lowest credit limit of %s'), $username, $uid, $credit_lowest_limit);
recvsms_inbox_add(core_get_datetime(), _SYSTEM_SENDER_ID_, $admin['username'], $credit_message_to_admins);
}

// get parent
if ($parent_uid = $db_row['parent_uid']) {
// notif parent_uid if exists
if ($username_parent = user_uid2username($parent_uid)) {
$credit_message_to_parent = sprintf(_('Your subuser with username %s and account ID %d has reached lowest credit limit of %s'), $username, $uid, $credit_lowest_limit);
recvsms_inbox_add(core_get_datetime(), _SYSTEM_SENDER_ID_, $username_parent, $credit_message_to_parent);
}
}

// notif uid
$sender_username = ($username_parent ? $username_parent : _SYSTEM_SENDER_ID_);
$credit_message_to_self = sprintf(_('You have reached lowest credit limit of %s'), $credit_lowest_limit);
recvsms_inbox_add(core_get_datetime(), $sender_username, $username, $credit_message_to_self);

_log('sent notification uid:' . $uid . ' parent_uid:' . $parent_uid . ' credit_lowest_limit:' . $credit_lowest_limit, 3, "credit_low_notif");
}


}
}

function credit_hook_playsmsd() {
if (!core_playsmsd_timer(300)) {
return;
}

_credit_low_notif();
}
8 changes: 4 additions & 4 deletions web/plugin/feature/simplebilling/fn.php
@@ -1,12 +1,12 @@
<?php
defined('_SECURE_') or die('Forbidden');

function simplebilling_hook_billing_post($smslog_id, $rate, $credit, $count, $charge) {
function simplebilling_hook_billing_post($smslog_id, $rate, $count, $charge) {
$ok = false;
_log("saving smslog_id:" . $smslog_id . " rate:" . $rate . " credit:" . $credit . " count:" . $count . " charge:" . $charge, 2, "simplebilling post");
$db_query = "INSERT INTO " . _DB_PREF_ . "_tblBilling (post_datetime,smslog_id,rate,credit,count,charge,status) VALUES ('" . core_get_datetime() . "','$smslog_id','$rate','$credit','$count','$charge','0')";
_log("saving smslog_id:" . $smslog_id . " rate:" . $rate . " count:" . $count . " charge:" . $charge, 2, "simplebilling_hook_billing_post");
$db_query = "INSERT INTO " . _DB_PREF_ . "_tblBilling (post_datetime,smslog_id,rate,count,charge,status) VALUES ('" . core_get_datetime() . "','$smslog_id','$rate','$count','$charge','0')";
if ($id = @dba_insert_id($db_query)) {
_log("saved smslog_id:" . $smslog_id . " id:" . $id, 2, "simplebilling post");
_log("saved smslog_id:" . $smslog_id . " id:" . $id, 2, "simplebilling_hook_billing_post");
$ok = true;
}
return $ok;
Expand Down
134 changes: 59 additions & 75 deletions web/plugin/feature/simplerate/fn.php
Expand Up @@ -49,6 +49,28 @@ function simplerate_getbyid($id) {
return $rate;
}

function simplerate_getadhoccredit($uid) {
$balance = 0;

if ($c_uid = (int) $uid) {
$db_query = "SELECT adhoc_credit FROM " . _DB_PREF_ . "_tblUser WHERE flag_deleted='0' AND uid='$c_uid'";
$db_result = dba_query($db_query);
$db_row = dba_fetch_array($db_result);
$balance = (float) $db_row['adhoc_credit'];
}

return $balance;
}

function simplerate_setadhoccredit($uid, $balance) {
$balance = (float) $balance;

if ($c_uid = (int) $uid) {
$db_query = "UPDATE " . _DB_PREF_ . "_tblUser SET c_timestamp='" . time() . "', adhoc_credit='$balance' WHERE uid='$c_uid' AND flag_deleted='0'";
dba_query($db_query);
}
}

// -----------------------------------------------------------------------------------------
function simplerate_hook_rate_getbyprefix($sms_to) {
global $core_config;
Expand Down Expand Up @@ -117,32 +139,43 @@ function simplerate_hook_rate_cansend($username, $sms_len, $unicode, $sms_to) {
$uid = user_username2uid($username);
list($count, $rate, $charge) = rate_getcharges($uid, $sms_len, $unicode, $sms_to);

// sender's
$adhoc_credit = simplerate_getadhoccredit($uid);
$adhoc_balance = $adhoc_credit - $charge;

// parent's when sender is a subuser
$parent_uid = user_getparentbyuid($uid);
if ($parent_uid) {
$username_parent = user_uid2username($parent_uid);
$credit_parent = rate_getusercredit($username_parent);
$balance_parent = $credit_parent - $charge;
} else {
// sender's
$credit = rate_getusercredit($username);
$balance = $credit - $charge;
if ($parent_uid = user_getparentbyuid($uid)) {
$adhoc_credit_parent = simplerate_getadhoccredit($parent_uid);
$adhoc_balance_parent = $adhoc_credit_parent - $charge;
}

if ($parent_uid) {
if (($balance_parent >= 0) && ($balance >= 0)) {
_log("allowed subuser uid:" . $uid . " parent_uid:" . $parent_uid . " sms_to:" . $sms_to . " credit:" . $credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " balance:" . $balance . " balance_parent:" . $balance_parent, 2, "simplerate_hook_rate_cansend");
if (($adhoc_balance_parent >= 0) && ($adhoc_balance >= 0)) {

// update adhoc_credit immediately, parent's too
simplerate_setadhoccredit($uid, $adhoc_balance);
simplerate_setadhoccredit($parent_uid, $adhoc_balance_parent);

_log("allowed subuser uid:" . $uid . " parent_uid:" . $parent_uid . " sms_to:" . $sms_to . " adhoc_credit:" . $adhoc_credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " adhoc_balance:" . $adhoc_balance . " adhoc_balance_parent:" . $adhoc_balance_parent, 2, "simplerate_hook_rate_cansend");

return TRUE;
} else {
_log("disallowed subuser uid:" . $uid . " parent_uid:" . $parent_uid . " sms_to:" . $sms_to . " credit:" . $credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " balance:" . $balance . " balance_parent:" . $balance_parent, 2, "simplerate_hook_rate_cansend");
_log("disallowed subuser uid:" . $uid . " parent_uid:" . $parent_uid . " sms_to:" . $sms_to . " adhoc_credit:" . $adhoc_credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " adhoc_balance:" . $adhoc_balance . " adhoc_balance_parent:" . $adhoc_balance_parent, 2, "simplerate_hook_rate_cansend");

return FALSE;
}
} else {
if ($balance >= 0) {
_log("allowed user uid:" . $uid . " sms_to:" . $sms_to . " credit:" . $credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " balance:" . $balance, 2, "simplerate_hook_rate_cansend");
if ($adhoc_balance >= 0) {

// update adhoc_credit immediately
simplerate_setadhoccredit($uid, $adhoc_balance);

_log("allowed user uid:" . $uid . " sms_to:" . $sms_to . " adhoc_credit:" . $adhoc_credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " adhoc_balance:" . $adhoc_balance, 2, "simplerate_hook_rate_cansend");

return TRUE;
} else {
_log("disallowed user uid:" . $uid . " sms_to:" . $sms_to . " credit:" . $credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " balance:" . $balance, 2, "simplerate_hook_rate_cansend");
_log("disallowed user uid:" . $uid . " sms_to:" . $sms_to . " adhoc_credit:" . $adhoc_credit . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " adhoc_balance:" . $adhoc_balance, 2, "simplerate_hook_rate_cansend");

return FALSE;
}
}
Expand All @@ -151,85 +184,36 @@ function simplerate_hook_rate_cansend($username, $sms_len, $unicode, $sms_to) {
function simplerate_hook_rate_deduct($smslog_id) {
global $core_config;

_log("enter smslog_id:" . $smslog_id, 2, "simplerate deduct");
$db_query = "SELECT p_dst,p_footer,p_msg,uid,unicode FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE smslog_id='$smslog_id'";
_log("enter smslog_id:" . $smslog_id, 2, "simplerate_hook_rate_deduct");
$db_query = "SELECT p_dst,p_footer,p_msg,uid,parent_uid,unicode FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE smslog_id='$smslog_id'";
$db_result = dba_query($db_query);
if ($db_row = dba_fetch_array($db_result)) {
$p_dst = $db_row['p_dst'];
$p_msg = $db_row['p_msg'];
$p_footer = $db_row['p_footer'];
$uid = $db_row['uid'];
$parent_uid = $db_row['parent_uid'];
$unicode = $db_row['unicode'];
if ($p_dst && $p_msg && $uid) {

// get charge
$p_msg_len = strlen($p_msg) + strlen($p_footer);
list($count, $rate, $charge) = rate_getcharges($uid, $p_msg_len, $unicode, $p_dst);

// sender's
$username = user_uid2username($uid);
$credit = rate_getusercredit($username);
$balance = $credit - $charge;

// parent's when sender is a subuser
$parent_uid = user_getparentbyuid($uid);
if ($parent_uid) {
$username_parent = user_uid2username($parent_uid);
$credit_parent = rate_getusercredit($username_parent);
$balance_parent = $credit_parent - $charge;
}

if (billing_post($smslog_id, $rate, $credit, $count, $charge)) {
_log("deduct successful uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct");

// if balance under credit lowest limit and never been notified then notify admins, parent_uid and uid


$credit_lowest_limit = (float) $core_config['main']['credit_lowest_limit'];
_log('credit_lowest_limit:' . $credit_lowest_limit . ' balance:' . $balance . ' charge:' . $charge, 3, 'simplerate deduct');

$reg = registry_search($uid, 'feature', 'credit', 'lowest_limit_notif');
$notified = ($reg['feature']['credit']['lowest_limit_notif'] ? TRUE : FALSE);

if ($charge && $balance && $credit_lowest_limit && ($balance <= $credit_lowest_limit) && !$notified) {

// set notified
registry_update($uid, 'feature', 'credit', array(
'lowest_limit_notif' => TRUE
));

// notif admins
$admins = user_getallwithstatus(2);
foreach ($admins as $admin) {
$credit_message_to_admins = sprintf(_('Username %s with account ID %d has reached lowest credit limit of %s'), $username, $uid, $credit_lowest_limit);
recvsms_inbox_add(core_get_datetime(), _SYSTEM_SENDER_ID_, $admin['username'], $credit_message_to_admins);
}

// notif parent_uid if exists
if ($parent_uid && $username_parent) {
$credit_message_to_parent = sprintf(_('Your subuser with username %s and account ID %d has reached lowest credit limit of %s'), $username, $uid, $credit_lowest_limit);
recvsms_inbox_add(core_get_datetime(), _SYSTEM_SENDER_ID_, $username_parent, $credit_message_to_parent);
}

// notif uid
$sender_username = ($username_parent ? $username_parent : _SYSTEM_SENDER_ID_);
$credit_message_to_self = sprintf(_('You have reached lowest credit limit of %s'), $credit_lowest_limit);
recvsms_inbox_add(core_get_datetime(), $sender_username, $username, $credit_message_to_self);

_log('sent notification credit_lowest_limit:' . $credit_lowest_limit, 3, 'simplerate deduct');
}

if (billing_post($smslog_id, $rate, $count, $charge)) {
_log("deduct successful uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate_hook_rate_deduct");

return TRUE;
} else {
_log("deduct failed uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct");
_log("deduct failed uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate_hook_rate_deduct");

return FALSE;
}
} else {
_log("rate deduct failed due to empty data uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct");
_log("rate deduct failed due to empty data uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate_hook_rate_deduct");
}
} else {
_log("rate deduct failed due to missing data uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct");
_log("rate deduct failed due to missing data uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate_hook_rate_deduct");
}

return FALSE;
Expand All @@ -238,7 +222,7 @@ function simplerate_hook_rate_deduct($smslog_id) {
function simplerate_hook_rate_refund($smslog_id) {
global $core_config;

_log("start smslog_id:" . $smslog_id, 2, "simplerate refund");
_log("start smslog_id:" . $smslog_id, 2, "simplerate_hook_rate_refund");
$db_query = "SELECT p_dst,p_msg,uid FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE p_status='2' AND smslog_id='$smslog_id'";
$db_result = dba_query($db_query);
if ($db_row = dba_fetch_array($db_result)) {
Expand All @@ -259,7 +243,7 @@ function simplerate_hook_rate_refund($smslog_id) {
}

function simplerate_hook_setsmsdeliverystatus($smslog_id, $uid, $p_status) {
//_log("start smslog_id:".$smslog_id, 2, "simplerate setsmsdeliverystatus");
//_log("start smslog_id:".$smslog_id, 2, "simplerate_hook_setsmsdeliverystatus");
if ($p_status == 2) {
// check in billing table smslog_id with status=0, status=1 is finalized, status=2 is rolled-back
$db_query = "SELECT id FROM " . _DB_PREF_ . "_tblBilling WHERE status='0' AND smslog_id='$smslog_id'";
Expand Down