Document toolboxDocument toolbox

Equipment Details Sync

Sync Service GLA, Owner and Location to Equipment

Equipment Details Sync
<?php
/**
* Event: Custom PHP Event
* Frequency: Hourly
* Time:
* Listener Class: Core_Model_Event
* Listener Method: eventCustom
*/

PCR_Event::attachDb("equipment-bulk-update", ["Application_Model_Equipment_Equipment" => "eventBulkUpdate"]);

$records = $this->query(<<<SQL
        SELECT
          S.RECID AS SERVICES_RECID,
          E.RECID AS EQUIPMENT_RECID,
          S.OWNER_CONTACTS_RECID AS SERVICE_OWNER_CONTACTS_RECID,
          E.OWNER_CONTACTS_RECID AS EQUIPMENT_OWNER_CONTACTS_RECID,
          S.OWNER_DEPT_HIERARCHY_RECID AS SERVICE_OWNER_DEPT_HIERARCHY_RECID,
          E.OWNER_DEPT_HIERARCHY_RECID AS EQUIPMENT_OWNER_DEPT_HIERARCHY_RECID,
        CASE
          WHEN S.OWNER_CONTACTS_RECID IS NULL
            THEN SDH.BILLING_GROUPS_RECID
          WHEN SCON.BILLING_GROUPS_RECID IS NULL 
            THEN SCON_DEPT.BILLING_GROUPS_RECID
          ELSE SCON.BILLING_GROUPS_RECID
          END AS SERVICE_BILLING_GROUP,
        CASE
          WHEN E.OWNER_CONTACTS_RECID IS NULL 
            THEN EDH.BILLING_GROUPS_RECID
          WHEN ECON.BILLING_GROUPS_RECID IS NULL 
            THEN ECON_DEPT.BILLING_GROUPS_RECID
          ELSE ECON.BILLING_GROUPS_RECID
          END AS EQUIPMENT_BILLING_GROUP,
        SEG.GLA_RECID AS SERVICE_GLA,
        EEG.GLA_RECID AS EQUIPMENT_GLA,
        S.LOCATIONS_RECID AS SERVICE_LOCATION,
        E.LOCATIONS_RECID AS EQUIPMENT_LOCATION,
        S.MULTIPLE_LOCATIONS,
        COALESCE(EG.FORMAT,SG.FORMAT) AS GLA_FORMAT,
        E.BILLABLE,
        E.WARRANTY_END_DATE

        FROM SERVICES S
        JOIN SERVICES_EQUIPMENT SE ON S.RECID = SE.SERVICES_RECID
        JOIN EQUIPMENT E ON SE.EQUIPMENT_RECID = E.RECID
        LEFT JOIN DEPT_HIERARCHY EDH ON E.OWNER_DEPT_HIERARCHY_RECID = EDH.RECID
        LEFT JOIN DEPT_HIERARCHY SDH ON S.OWNER_DEPT_HIERARCHY_RECID = SDH.RECID
        LEFT JOIN CONTACTS ECON ON E.OWNER_CONTACTS_RECID = ECON.RECID
        LEFT JOIN DEPT_HIERARCHY ECON_DEPT ON ECON.DEPT_HIERARCHY_RECID = ECON_DEPT.RECID
        LEFT JOIN CONTACTS SCON ON S.OWNER_CONTACTS_RECID = SCON.RECID
        LEFT JOIN DEPT_HIERARCHY SCON_DEPT ON SCON.DEPT_HIERARCHY_RECID = SCON_DEPT.RECID
        LEFT JOIN SERVICES_EXPENSE_GLA SEG ON S.RECID = SEG.SERVICES_RECID
        LEFT JOIN EQUIPMENT_EXPENSE_GLA EEG ON E.RECID = EEG.EQUIPMENT_RECID
        LEFT JOIN LISTS SEGL ON SEG.EXPENSE_TYPE_LISTS_RECID = SEGL.RECID
        LEFT JOIN LISTS EEGL ON EEG.EXPENSE_TYPE_LISTS_RECID = EEGL.RECID
        LEFT JOIN GLA EG ON EEG.GLA_RECID = EG.RECID
        LEFT JOIN GLA SG ON SEG.GLA_RECID = SG.RECID

        WHERE
        -- GLAs are Default and 100% or are not set
        (
          ((SEGL.CODE = 'DEFAULT' AND SEG.PERCENT = 100) OR SEGL.CODE IS NULL)
          AND
          ((EEGL.CODE = 'DEFAULT' AND SEG.PERCENT = 100) OR EEGL.CODE IS NULL)
        )
        -- if the OWNER is different
        AND
        (
            (
              (S.OWNER_CONTACTS_RECID <> E.OWNER_CONTACTS_RECID AND S.OWNER_CONTACTS_RECID IS NOT NULL AND E.OWNER_CONTACTS_RECID IS NOT NULL)
              OR (S.OWNER_DEPT_HIERARCHY_RECID <> E.OWNER_DEPT_HIERARCHY_RECID AND S.OWNER_DEPT_HIERARCHY_RECID IS NOT NULL AND E.OWNER_DEPT_HIERARCHY_RECID IS NOT NULL)
              OR (S.OWNER_CONTACTS_RECID IS NULL AND E.OWNER_CONTACTS_RECID IS NOT NULL)
              OR (S.OWNER_CONTACTS_RECID IS NOT NULL AND E.OWNER_CONTACTS_RECID IS NULL)
              OR (S.OWNER_DEPT_HIERARCHY_RECID IS NULL AND E.OWNER_DEPT_HIERARCHY_RECID IS NOT NULL)
              OR (S.OWNER_DEPT_HIERARCHY_RECID IS NOT NULL AND E.OWNER_DEPT_HIERARCHY_RECID IS NULL)
            )
            -- if the location is different
            OR (NOT(S.LOCATIONS_RECID <=> E.LOCATIONS_RECID) AND S.MULTIPLE_LOCATIONS = 0)
            -- if the GLAs are different
            OR  NOT(SEG.GLA_RECID  <=>  EEG.GLA_RECID )
         );
SQL
);

foreach ($records as $rec) {
    $eventParams = [
        "contact" => 101452,  // PCR Admin contact
        "users_recid" => 485, // pcradmin user
        "selectedEquipmentRecids" => [$rec['EQUIPMENT_RECID']],
        "gla_enabled" => false,
        "condition_enabled" => false,
        "cabling_eqp_type_enabled" => false,
        "status_enabled" => false,
        "location_enabled" => false,
        "assoc_service_enabled" => false,
        "equipment_type_enabled" => false,
        "dhcp_enabled" => false,
        "lan_name_enabled" => false,
        "host_name_enabled" => false,
        "private_enabled" => false,
        "billable_enabled" => false,
        "billable" => $rec["BILLABLE"],
        "war_date_enabled" => false,
        "war_date" => $rec["WARRANTY_END_DATE"],
        "dept_hier_owner_enabled" => false,
        "contact_owner_enabled" => false,
        // other junk from the EQP Bulk UPdate
        "gla_format" => $rec["GLA_FORMAT"],
        "selectedEquipmentTypeListRecid" => null,
        "isLocationRequired" => null,
        "comment" => null,
    ];

    if (empty($rec['SERVICE_OWNER_CONTACTS_RECID'])
        && $rec['SERVICE_OWNER_DEPT_HIERARCHY_RECID'] != $rec['EQUIPMENT_OWNER_DEPT_HIERARCHY_RECID']
    ) {
        $eventParams['dept_hier_owner_enabled'] = true;
        $eventParams['dept_hier_owner'] = $rec['SERVICE_OWNER_DEPT_HIERARCHY_RECID'];
    } elseif(empty($rec['SERVICE_OWNER_DEPT_HIERARCHY_RECID'])
        && $rec['SERVICE_OWNER_CONTACTS_RECID'] != $rec['EQUIPMENT_OWNER_CONTACTS_RECID']
    ) {
        $eventParams['contact_owner_enabled'] = true;
        $eventParams['contact_owner'] = $rec['SERVICE_OWNER_CONTACTS_RECID'];
    } elseif(empty($rec['SERVICE_OWNER_DEPT_HIERARCHY_RECID'])
        && $rec['SERVICE_OWNER_CONTACTS_RECID'] == $rec['EQUIPMENT_OWNER_CONTACTS_RECID']
    ) {
        // Equipment has both Contact and Dept Owners -
        // @see https://jira.pcr.com/browse/PCR360-7612
        // updating the contact Owner even though it is already set will clear the Dept Owner
        $eventParams['contact_owner_enabled'] = true;
        $eventParams['contact_owner'] = $rec['SERVICE_OWNER_CONTACTS_RECID'];
    }

    if($rec['MULTIPLE_LOCATIONS'] == "0"
        && $rec['SERVICE_LOCATION'] != $rec['EQUIPMENT_LOCATION']
    ){
        $eventParams['location_enabled'] = true;
        $eventParams['location'] = $rec['SERVICE_LOCATION'];
    }

    if($rec['SERVICE_GLA'] != $rec['EQUIPMENT_GLA']){
        $eventParams['gla_enabled'] = true;
        $eventParams['gla'] = $rec['SERVICE_GLA'];
    }

    // Only trigger the bulk update if any of the options are enabled.  Otherwise nothing is changing, there is no
    // need to trigger a bulk update if nothing is changing.
    if($eventParams['dept_hier_owner_enabled']
        || $eventParams['location_enabled']
        || $eventParams['gla_enabled']
        || $eventParams['contact_owner_enabled']
    ){
        PCR_Event::trigger("equipment-bulk-update", $eventParams);
    }
}


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