<?php
class eventclass_requisition_issuance_slip2  extends TableEventsBase {
	
	function init() {
		$this->events = array(
	'BeforeAdd' => true 
);
		$this->fieldValues = array(
	'filterLimit' => array(
		 
	),
	'mapIcon' => array(
		 
	),
	'viewCustom' => array(
		 
	),
	'lookupWhere' => array(
		 
	),
	'viewFileText' => array(
		 
	),
	'defaultValue' => array(
		'date_accomplished' => array(
			'edit' => true 
		) 
	),
	'autoUpdateValue' => array(
		 
	),
	'uploadFolder' => array(
		 
	),
	'viewPluginInit' => array(
		 
	),
	'editPluginInit' => array(
		 
	) 
);
			}
		function BeforeAdd( &$values, &$sqlValues, &$message, $inline, $pageObject ) {
		// ======================================================
// RIS-L
// BEFORE RECORD ADDED
// ======================================================

$values["ris_type"] = "WAREHOUSE";

// ------------------------------------------------------
// DATE ACCOMPLISHED
// ------------------------------------------------------

if (empty($values["date_accomplished"])) {

    $values["date_accomplished"] =
        date("Y-m-d");
}


// ------------------------------------------------------
// GENERATE RIS-L NUMBER
// FORMAT: RISL YYYY-MM-###
// ------------------------------------------------------

if (
    !isset($values["ris_no"])
    ||
    trim((string)$values["ris_no"]) === ""
) {

    $yearMonth =
        date(
            "Y-m",
            strtotime(
                $values["date_accomplished"]
            )
        );

    $prefix =
    "RISW " .
    $yearMonth .
    "-";

    $safePrefix =
        db_addslashes(
            $prefix
        );

    $sqlNumber = "
        SELECT
            MAX(
                CAST(
                    RIGHT(
                        ris_no,
                        3
                    ) AS UNSIGNED
                )
            ) AS last_no
        FROM requisition_issuance_slip
        WHERE ris_type = 'WAREHOUSE'
          AND ris_no LIKE '" .
            $safePrefix .
            "%'
    ";

    $rsNumber =
        DB::Query(
            $sqlNumber
        );

    $numberRow =
        $rsNumber
        ? $rsNumber->fetchAssoc()
        : null;

    $nextNo = 1;

    if (
        $numberRow
        &&
        isset($numberRow["last_no"])
        &&
        intval($numberRow["last_no"]) > 0
    ) {

        $nextNo =
            intval(
                $numberRow["last_no"]
            ) + 1;
    }

    $values["ris_no"] =
        $prefix .
        str_pad(
            $nextNo,
            3,
            "0",
            STR_PAD_LEFT
        );
}


// ------------------------------------------------------
// LOGGED-IN USER
// ------------------------------------------------------

$username =
    Security::getUserName();

if ($username != "") {

    $safeUsername =
        db_addslashes(
            $username
        );

    $sqlUser = "
        SELECT
            ID,
            fullname,
            COALESCE(
                NULLIF(warehouse_id, 0),
                NULLIF(warehouse, 0)
            ) AS warehouse_id
        FROM users
        WHERE username = '" .
            $safeUsername . "'
        LIMIT 1
    ";

    $rsUser =
        DB::Query(
            $sqlUser
        );

    $user =
        $rsUser
        ? $rsUser->fetchAssoc()
        : null;

    if ($user) {

        $values["requested_by"] =
            intval(
                $user["ID"]
            );

        $values["created_by"] =
            intval(
                $user["ID"]
            );

        $values["requested_by_name"] =
            trim(
                (string)$user["fullname"]
            );

        $values["requested_at"] =
            date(
                "Y-m-d H:i:s"
            );


        // ----------------------------------------------
        // DEFAULT REQUESTING WAREHOUSE
        // ----------------------------------------------

        if (
            empty(
                $values[
                    "requesting_warehouse_id"
                ]
            )
            &&
            intval(
                $user["warehouse_id"]
            ) > 0
        ) {

            $values[
                "requesting_warehouse_id"
            ] =
                intval(
                    $user["warehouse_id"]
                );
        }
    }
}


// ------------------------------------------------------
// INITIAL STATUS
// ------------------------------------------------------

$values["status"] =
    "DRAFT";

return true;
		;
		return true;
	}
	public function default_date_accomplished_efedit(  ) {
	$defaultValue = now();
return $defaultValue;
}	

}


?>