Document toolboxDocument toolbox

(2022.1) GLA Rollover

This Custom Event relies on UDFs for 'End_Date' and "Rollover_GLA" to roll over a GLA for billing on a given date.

GLA Rollover
/**
 * Event: Custom PHP Event
 * Frequency: Daily
 * Time: 12:00am
 * Listener Class: Core_Model_Event
 * Listener Method: eventCustom
 */

$glas = $this->query(<<<SQL
    SELECT G.STATUS AS STATUS, V.GLA_FULL_CODES AS OLD_GLA, 
           G.RECID AS OLD_GLA_RECID, V1.GLA_FULL_CODES AS NEW_GLA, 
           G1.RECID AS NEW_GLA_RECID, U2.DATETIME_VALUE AS END_DATE
    FROM V_GLA_COMP_COMB_ADMIN V
    INNER JOIN GLA G ON V.RECID = G.RECID
    INNER JOIN USER_DEFINED_FIELDS_VALS U1 
        ON G.RECID = U1.TABLE_RECID AND U1.TABLE_NAME = 'GLA' AND U1.UDF_RECID = (
            SELECT RECID FROM USER_DEFINED_FIELDS WHERE LABEL = 'Rollover_GLA'
        )
    LEFT JOIN GLA G1 ON G1.RECID = U1.INTEGER_VALUE
    LEFT JOIN V_GLA_COMP_COMB_ADMIN V1 ON G1.RECID = V1.RECID
    INNER JOIN USER_DEFINED_FIELDS_VALS U2 
        ON G.RECID = U2.TABLE_RECID AND U2.TABLE_NAME = 'GLA' AND U2.UDF_RECID = (
            SELECT RECID FROM USER_DEFINED_FIELDS WHERE LABEL = 'End_Date'
        )
    WHERE
    U2.DATETIME_VALUE IS NOT NULL
    AND U2.DATETIME_VALUE <= NOW()
    AND G.STATUS = 1
    ;
SQL
);

$results = ["deactivateGla" => [], "activateGla" => [], "rolledGla" => []];
PCR_Event::attachDb("replace-gla", [
    "Application_Model_Gla_Gla" => "eventReplaceGla"
]);
foreach ($glas as $gla) {

    // deactivate anything with past end_date
    $results["deactivateGla"][] = $gla["OLD_GLA_RECID"];

    // only Roll to the new GLA if there is a new GLA
    if(!empty($gla["NEW_GLA_RECID"])) {
        $results["activateGla"][] = $gla["NEW_GLA_RECID"];
        // only rollover if the END_DATE was within the last 30 days
        if (time() - strtotime($gla["END_DATE"]) < (30 * 24 * 60 * 60)) {
            $results["rolledGla"][] = $gla["OLD_GLA_RECID"];
            PCR_Event::trigger("replace-gla", [
                "contact" => 101452,  // PCR Admin contact
                "users_recid" => 485, // pcradmin user
                "glaRecid" => [$gla["OLD_GLA_RECID"]],
                "replaceGlaRecid" => $gla["NEW_GLA_RECID"],
                "prorate" => true,
                "setInactiveGla" => true,
                "comment" => "Nightly GLA Rollover from {$gla['OLD_GLA']} to {$gla['NEW_GLA']} recid"
                             . " {$gla['OLD_GLA_RECID']} to {$gla['NEW_GLA_RECID']}",
            ]);
        }
    }
}

// if anything Rolled/Deactivated then we need to fire that event
if(!empty($results["activateGla"]) || !empty($results["rolledGla"]) || !empty($results["deactivateGla"])){
    $results["ACTIVATE_COUNT"] = count($results["activateGla"]);
    $results["DEACTIVATE_COUNT"] = count($results["deactivateGla"]);
    $results["ROLLED_COUNT"] = count($results["rolledGla"]);

    // set the gla-status event and attach for a notification too
    PCR_Event::attachDb("gla-status", [
        "Core_Model_Notification" => "eventSend",
        "Application_Model_Gla_Gla" => "eventGlaStatus"
    ]);
    PCR_Event::trigger("gla-status", $results);
} else {
    $results["ACTIVATE_COUNT"] = $results["DEACTIVATE_COUNT"] = $results["ROLLED_COUNT"] = 0;

    // set the gla-status event and attach for a notification to indicate no glas rolled over
    PCR_Event::attachDb(
        "gla-status",
        [
            "Core_Model_Notification"   => "eventSend",
            "Application_Model_Gla_Gla" => "eventGlaStatus"
        ]
    );
    PCR_Event::trigger("gla-status", $results); 
}