Jump to content

Automatic assign to group of clients based on active services


hostingarg

Recommended Posts

Hi.

I need a module or hook that daily reviews the number of active hosting services (not domains) that each client has and, depending on the number of active services that the client has, assign it to one group or another. Example: if a client has up to 10 active services designate it to group 1, if a client has up to 20 active services designate it to group 2, etc. With daily update through cron.

Does anyone know a module or hook that performs this task?

Thanks a lot.

Link to comment
Share on other sites

12 minutes ago, hostingarg said:

Does anyone know a module or hook that performs this task?

Kian has a hook that works in a similar way with domains, e.g count how many domains and assign client to a group... you would have to adjust the db query, e.g change references of tbldomains to tblhosting; define the hosting statuses to count etc...

https://github.com/Katamaze/WHMCS-Free-Scripts/blob/master/hooks/AssignClientToGroupBasedOnRegisteredDomains.php

Link to comment
Share on other sites

Thank you very much @brian! as always for your help. Here modify the hook to adapt it to tblhosting. Do you think it's okay?

Thank you very much and sorry for the inconvenience.

 

<?php

/**
 * Assign Client to Group based on registered domains
 *
 * @package     WHMCS
 * @copyright   Katamaze
 * @link        https://katamaze.com
 * @author      Davide Mantenuto <info@katamaze.com>
 */

use WHMCS\Database\Capsule;

add_hook('DailyCronJob', 1, function($vars)
{
    // Define group/product pairs. Instructions provided below
    // https://github.com/Katamaze/WHMCS-Free-Action-Hooks#client-to-group-based-on-registered-domains
    $groups['4'] = '10';
    $groups['5'] = '25';
    $groups['6'] = '100';

    $activeCustomers = true;
    $placeholderGroup = false;

    if (!$groups): return; endif;
    $defaultGroup = ($placeholderGroup ? $placeholderGroup : '0');

    $filterStatus = ($activeCustomers ? ' AND t2.status = "Active"' : false);
    $filterGroup = ($placeholderGroup ? ' AND (t2.groupid = "' . $placeholderGroup . '" OR t2.groupid IN (\'' . implode('\', \'', array_keys($groups)) . '\'))' : false);

    foreach (Capsule::select(Capsule::raw('SELECT t1.userid, COUNT(t1.id) as total, t2.groupid FROM tblhosting AS t1 LEFT JOIN tblclients AS t2 ON t1.userid = t2.id WHERE t1.status IN ("Active") ' . $filterStatus . ' GROUP BY t1.userid')) as $v)
    {
        $current[$v->userid] = $v;
        $users[$v->userid] = $defaultGroup;

        foreach ($groups as $gid => $total)
        {
            if ($v->total >= $total)
            {
                $users[$v->userid] = $gid;
            }
        }
    }

    foreach (Capsule::table('tblclientgroups')->select('id', 'groupname')->get() as $v)
    {
        $groupLabels[$v->id] = $v->groupname;
    }

    foreach ($users as $userID => $groupID)
    {
        if ($current[$userID]->groupid != $groupID)
        {
            logActivity('Client Group Modified - User ID: ' . $userID . ' now has ' . $current[$userID]->total . ' domain(s) - Moved from #' . $current[$userID]->groupid . ' ' . $groupLabels[$current[$userID]->groupid] . ' to #' . $groupID . ' ' . $groupLabels[$groupID]);
        }

        Capsule::table('tblclients')->where('id', $userID)->update(['groupid' => $groupID]);
    }
});

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated