[solved] Low Credit Notification Issue

I dont understand why the logic for _credit_low_notif() function changed:
From:
if (...&& !$notified) {

To:
if ((...&& $notified === TRUE) {

This makes the notification to be sent endless times since the $notified is set to true by the same function. My suggestion is that the last condition should have remained as if (...&& !$notified) {.
This variable is set to false when a user’s credit is added and thus allow for notification again.

Can we also add _credit_update($db_row['uid'], $db_row['status']); before sending out the notifications?

Yeah, let see, here it is suppose to be:

it needs to check $notified, if its FALSE ($notifed === FALSE or use !$notifed) then send notification, and also set lowest_limit_notif to TRUE. This will make the notification sent once, due to $notified now being TRUE.

After that, add an IF block below, if $balance > $credit_lowest_limit and $notified is TRUE (which mean sometime ago the user has been notified, but now the balance is more then the limit), then set lowest_limit_notif back to FALSE. This will make sure the notification will run again should the user hit lowest limit again.

like this, please help to verifiy and test:

       // 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 user never been notified and the balance is lower than the limit
        if (($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");
        }
         
        // if user has been notified before, and now the balance is enough
        if (($balance > $credit_lowest_limit) && $notified) {
            // set notified to false
            registry_update($uid, 'feature', 'credit', array(
                'lowest_limit_notif' => FALSE
            ));
        }

UPDATE:

Commited for next release (1.4.4):

anton

This is working properly.

On the last part:
// if user has been notified before, and now the balance is enough
if (($balance > $credit_lowest_limit) && $notified) {
Can we remove the last condition ...&& $notified and set $notified to false when the balance is enough only

If this working properly why need more change ?

anton

I just don’t see the need to send a notification to a user whose balance is enough but was not notified when the balance had run out in the past.

It should last part should remain as follows( without && $notified):
// if user has been notified before, and now the balance is enough
if (($balance > $credit_lowest_limit) ) {
// set notified to false
registry_update($uid, ‘feature’, ‘credit’, array(
‘lowest_limit_notif’ => FALSE
));
}

saves some resources calling registry update, why bother update to false if its already false

anton

Ok… I get the point now. Thank you

please confirm, if everything as expected now, I’ll push the changes for next release, thanks

anton

This is working as expected now. I appreciate a lot
Regards
Kenn

1 Like