WHMCS guide

What to check before you merge two WHMCS clients.

Merge Clients Accounts moves invoices, services, transactions and tickets into one account, and there is no undo. On our own Brazilian install one duplicate group had all of its credit on one account and a different person logging in to another. Five minutes with the queries below tells you which way round to merge.

DMS.S2 — Before you start

Before you run anything.

Read only

Every query below is a SELECT. It changes nothing. Run it in phpMyAdmin or the mysql client, on a copy of the database if you have one.

Two IDs, one direction

Queries 1 and 2 start with SET: put the ID of the account you want to keep and the one you want to remove, as shown on each client's Summary tab.

Back up first

The merge cannot be undone. Take a database backup before you click Submit, even when the queries show nothing worrying.

DMS.S3 — The queries

Four ways to find them.

What each account holds

Status, currency, credit, active services and domains, unpaid invoices, saved cards or bank accounts and open tickets, side by side. Keep the account that holds most of it.

SET @keep = 101, @remove = 102;
SELECT c.id, c.status, cur.code AS currency, c.credit, c.datecreated,
       (SELECT COUNT(*) FROM tblhosting h WHERE h.userid = c.id
          AND h.domainstatus IN ('Active', 'Suspended')) AS services,
       (SELECT COUNT(*) FROM tbldomains d WHERE d.userid = c.id
          AND d.status IN ('Active', 'Pending', 'Pending Transfer')) AS domains,
       (SELECT COUNT(*) FROM tblinvoices i WHERE i.userid = c.id AND i.status = 'Unpaid') AS unpaid_invoices,
       (SELECT COALESCE(SUM(i.total), 0) FROM tblinvoices i WHERE i.userid = c.id
          AND i.status = 'Unpaid') AS unpaid_total,
       (SELECT COUNT(*) FROM tblpaymethods p WHERE p.userid = c.id AND p.deleted_at IS NULL) AS saved_pay_methods,
       (SELECT COUNT(*) FROM tbltickets t WHERE t.userid = c.id AND t.status <> 'Closed') AS open_tickets
FROM tblclients c
JOIN tblcurrencies cur ON cur.id = c.currency
WHERE c.id IN (@keep, @remove);

Who logs in to each account

Since WHMCS 8 a login (user) is separate from the client account, and one user can reach several accounts. Owner 1 is the account owner. Check that the person who logs in to the account you remove should see the one you keep.

SET @keep = 101, @remove = 102;
SELECT uc.client_id, u.email, uc.owner, uc.last_login
FROM tblusers_clients uc
JOIN tblusers u ON u.id = uc.auth_user_id
WHERE uc.client_id IN (@keep, @remove)
ORDER BY uc.client_id, uc.owner DESC;

Duplicates in different currencies

Stop here before merging. WHMCS tells you not to change the currency of a client that already has invoices or transactions, and a merge puts both currencies on one account. Same mailbox, ignoring +tags and Gmail dots.

SELECT m.mailbox, GROUP_CONCAT(CONCAT('#', c.id, ' ', cur.code) ORDER BY c.id SEPARATOR ', ') AS accounts
FROM (SELECT id,
             CASE WHEN SUBSTRING_INDEX(LOWER(TRIM(email)), '@', -1) IN ('gmail.com', 'googlemail.com')
                  THEN CONCAT(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(LOWER(TRIM(email)), '@', 1), '+', 1), '.', ''), '@gmail.com')
                  ELSE CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX(LOWER(TRIM(email)), '@', 1), '+', 1), '@',
                              SUBSTRING_INDEX(LOWER(TRIM(email)), '@', -1)) END AS mailbox
      FROM tblclients) m
JOIN tblclients c ON c.id = m.id
JOIN tblcurrencies cur ON cur.id = c.currency
GROUP BY m.mailbox
HAVING COUNT(*) > 1 AND COUNT(DISTINCT c.currency) > 1;

Duplicates where money is waiting

Groups in which at least one account has credit or an unpaid invoice, with what each one holds. These are the merges to do by hand, with the client's invoices open in another tab.

SELECT m.mailbox,
       GROUP_CONCAT(CONCAT('#', c.id, ' ', c.status,
         ' services:', (SELECT COUNT(*) FROM tblhosting h WHERE h.userid = c.id
                          AND h.domainstatus IN ('Active', 'Suspended')),
         ' credit:', c.credit,
         ' unpaid:', (SELECT COUNT(*) FROM tblinvoices i WHERE i.userid = c.id AND i.status = 'Unpaid'),
         ' cards:', (SELECT COUNT(*) FROM tblpaymethods p WHERE p.userid = c.id AND p.deleted_at IS NULL))
         ORDER BY c.id SEPARATOR ' | ') AS accounts
FROM (SELECT id,
             CASE WHEN SUBSTRING_INDEX(LOWER(TRIM(email)), '@', -1) IN ('gmail.com', 'googlemail.com')
                  THEN CONCAT(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(LOWER(TRIM(email)), '@', 1), '+', 1), '.', ''), '@gmail.com')
                  ELSE CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX(LOWER(TRIM(email)), '@', 1), '+', 1), '@',
                              SUBSTRING_INDEX(LOWER(TRIM(email)), '@', -1)) END AS mailbox
      FROM tblclients) m
JOIN tblclients c ON c.id = m.id
GROUP BY m.mailbox
HAVING COUNT(*) > 1
   AND SUM(c.credit > 0)
     + SUM(EXISTS (SELECT 1 FROM tblinvoices i WHERE i.userid = c.id AND i.status = 'Unpaid')) > 0;

DMS.S4 — The addon

Or see both accounts side by side inside the WHMCS admin.

Brasa Duplicate Clients finds the groups, shows each account's status, creation date, active services and domains, open invoices and credit, suggests which one to keep, gives the Merge into #ID to type and warns when the currencies differ. Query 2 and saved cards are not in the addon. It never merges anything.

Brasa Duplicate Clients screen in the WHMCS admin, with sample data

Brasa Duplicate Clients

$39 per year

Finds the accounts that look like the same person or company, so you can merge the right ones.

  • Same mailbox, ignoring capital letters, +tags and Gmail dots
  • Same tax ID, same name and phone, same name and postcode
  • Each group shows services, open invoices and credit, and which account to merge into
  • One mailbox behind unrelated last names is listed last, as possible fake signups
First run in 16 seconds, on sample data: the summary, then each group of accounts with the one to keep. Brasa Duplicate Clients on the WHMCS Marketplace
Buy for $39

DMS.S5 — Questions

Merging without surprises.

What does Merge Clients Accounts move?

Everything related to the two accounts, such as invoices, services, transactions and support tickets, goes to the account you choose to keep, as the WHMCS documentation describes. It is found on the client's Summary tab, under Other Actions.

Is moving one service the same as merging?

No. Transfer Ownership on a service moves the service only: its invoices stay with the old account, and the hosting account on your server is not touched. Use it when only one service belongs to the other client, and merge when the whole account is the same client.

The two accounts use different currencies. What now?

Do not merge. WHMCS's own guide, Change a Client's Currency, says to create a new client in the right currency and move invoices, services and transactions to it, never to switch the currency on an account with history. Settle which currency the client pays in first.