Document toolboxDocument toolbox

(v1) .Move Available Services to Parent v2022.2

Move Available Services to their Parent Category

Move Available Services to Parent
<?php

// >>>> COPY START !!! <<<< //

/**
 * This looks for any Phone Services which, after aging are changed from 'Inactive' to 'Available', and which
 * are in any of the child Categories of 'Telephones & Voicemail'. It then creates a Bulk Update Event to
 * move those Services to the parent.
 *
 * Note: The child Catalog Type must be 'Phone'.
 *
 * Event:           Custom PHP Event
 * Frequency:       Daily
 * Time:
 * Listener Class:  Core_Model_Event
 * Listener Method: eventCustom
 */

PCR_Event::attachDb('services-bulk-update', ['Application_Model_Service_Service' => 'eventBulkUpdate']);

$parentCatalogRecidSql = <<<SQL
SELECT RECID
FROM   SERV_CATALOG
WHERE  SERVICE_NAME = 'Telephones & Voicemail'
SQL;

$listsRecidSql = <<<SQL
SELECT    LISTS.RECID
FROM      LISTS
LEFT JOIN LIST_TYPES
    ON LISTS.LIST_TYPES_RECID = LIST_TYPES.RECID
WHERE     LIST_TYPES.TYPE = 'SERVICE_TYPE'
AND       LISTS.CODE = 'PHONE';
SQL;

if (($parentCatalogRecid = $this->query($parentCatalogRecidSql)) &&
    ($listsRecid = $this->query($listsRecidSql))
) {
    $parentCatalogRecid = (int) $parentCatalogRecid[0]['RECID'];
    $listsRecid = (int) $listsRecid[0]['RECID'];

    $servicesToMoveSql = <<<SQL
SELECT SERVICES.RECID
FROM   SERVICES
LEFT JOIN LISTS
    ON SERVICES.SERVICE_STATUS_LISTS_RECID = LISTS.RECID
LEFT JOIN LIST_TYPES
    ON LISTS.LIST_TYPES_RECID = LIST_TYPES.RECID
WHERE  LISTS.CODE = 'AVAILABLE'
AND    LIST_TYPES.TYPE = 'SERVICE_STATUS'
AND    SERVICES.SERV_CATALOG_RECID IN (
    SELECT SERV_CATALOG_FULLPATH.SERV_CATALOG_RECID
    FROM   SERV_CATALOG_FULLPATH
    LEFT JOIN SERV_CATALOG
        ON SERV_CATALOG_FULLPATH.SERV_CATALOG_RECID = SERV_CATALOG.RECID
    LEFT JOIN LISTS
        ON SERV_CATALOG.SERVICE_TYPE_LISTS_RECID = LISTS.RECID
    LEFT JOIN LIST_TYPES
        ON LISTS.LIST_TYPES_RECID = LIST_TYPES.RECID
    WHERE  PATH_SERV_CATALOG_RECID = {$parentCatalogRecid}
    AND    SERV_CATALOG_RECID <> {$parentCatalogRecid}
    AND    LISTS.CODE = 'PHONE'
    AND    LIST_TYPES.TYPE = 'SERVICE_TYPE'
)
SQL;

    $userSql = <<<SQL
SELECT RECID, CONTACTS_RECID
FROM USERS
WHERE USERID = 'pcr'
SQL;

    if (($servicesToMove = $this->query($servicesToMoveSql)) &&
        ($user = $this->query($userSql))
    ) {
        $eventParams = [
            'service_type_enabled'    => 1,
            'location_enabled'        => 0,
            'status_enabled'          => 0,
            'service_host_enabled'    => 0,
            'sla_enabled'             => 0,
            'contact_owner_enabled'   => 0,
            'dept_hier_owner_enabled' => 0,
            'gla_enabled'             => 0,
            'pools_enabled'           => 0,
            'billable_enabled'        => 0,
            'directory_enabled'       => 0,
            'essential_enabled'       => 0,
            'report_911_enabled'      => 0,
            'isLocationRequired'      => 0,
            'isServiceHostRequired'   => 0,

            'contact'     => (int) $user[0]['CONTACTS_RECID'],
            'users_recid' => (int) $user[0]['RECID'],

            'service_type'                 => $parentCatalogRecid,
            'selectedServiceTypeListRecid' => $listsRecid,
            'comment'                      => 'Custom Event: Move all "Available" Services to parent Catalog.'
        ];

        foreach ($servicesToMove as $service) {
            $eventParams['selectedServiceRecids'][] = (int) $service['RECID'];
        }

        PCR_Event::trigger('services-bulk-update', $eventParams);
    }
}

// >>>> COPY STOP !!! <<<< //

?>

Help Desk Portal - Email: help@pcr.com - Phone: 616.259.9242