<?php
class eventclass_external_workforce_requisition  extends TableEventsBase {
	
	function init() {
		$this->events = array(
	'OnPageLoad' => true,
	'BeforeShowAdd' => true,
	'BeforeAdd' => true 
);
		$this->fieldValues = array(
	'filterLimit' => array(
		 
	),
	'mapIcon' => array(
		 
	),
	'viewCustom' => array(
		'print' => array(
			'view' => true 
		) 
	),
	'lookupWhere' => array(
		 
	),
	'viewFileText' => array(
		 
	),
	'defaultValue' => array(
		'request_date' => array(
			'edit' => true 
		),
		'date_accomplished' => array(
			'edit' => true 
		),
		'requested_by_date_signed' => array(
			'edit' => true 
		),
		'approved_by_date_signed' => array(
			'edit' => true 
		),
		'received_by_date_signed' => array(
			'edit' => true 
		) 
	),
	'autoUpdateValue' => array(
		 
	),
	'uploadFolder' => array(
		 
	),
	'viewPluginInit' => array(
		 
	),
	'editPluginInit' => array(
		 
	) 
);
			}
		function BeforeShowAdd( &$xt, &$templatefile, $pageObject ) {
		// Place event code here.
// Use "Add Action" button to add code snippets.

// Place event code here.
// Use "Add Action" button to add code snippets.

// Place event code here.
// Use "Add Action" button to add code snippets.

// Place event code here.
// Use "Add Action" button to add code snippets.

$transactionCode = "EWRF";

$sql = "
    SELECT 
        prefix,
        digit_length,
        next_no
    FROM transaction_numbering
    WHERE transaction_code = '" . db_addslashes($transactionCode) . "'
      AND is_active = 1
    LIMIT 1
";

$rs = DB::Query($sql);

$previewReferenceNo = "";

if ($rs) {
    $data = $rs->fetchAssoc();

    if ($data) {
        $prefix = $data["prefix"];
        $digitLength = (int)$data["digit_length"];
        $nextNo = (int)$data["next_no"];

        $previewReferenceNo = $prefix . str_pad($nextNo, $digitLength, "0", STR_PAD_LEFT);
    }
}

// Send value to JavaScript
$pageObject->setProxyValue("preview_reference_number", $previewReferenceNo);
		;
		
	}

	function BeforeAdd( &$values, &$sqlValues, &$message, $inline, $pageObject ) {
		$transactionCode = "EWRF";

DB::Exec("START TRANSACTION");

try {

    $sql = "
        SELECT 
            id,
            prefix,
            digit_length,
            next_no
        FROM transaction_numbering
        WHERE transaction_code = '" . db_addslashes($transactionCode) . "'
          AND is_active = 1
        FOR UPDATE
    ";

    $rs = DB::Query($sql);

    if (!$rs) {
        DB::Exec("ROLLBACK");
        $message = "Unable to read transaction numbering setup.";
        return false;
    }

    $data = $rs->fetchAssoc();

    if (!$data) {
        DB::Exec("ROLLBACK");
        $message = "No active transaction numbering setup found for PR.";
        return false;
    }

    $id = (int)$data["id"];
    $prefix = $data["prefix"];
    $digitLength = (int)$data["digit_length"];
    $nextNo = (int)$data["next_no"];

    $referenceNo = $prefix . str_pad($nextNo, $digitLength, "0", STR_PAD_LEFT);

    DB::Exec("
        UPDATE transaction_numbering
        SET next_no = next_no + 1
        WHERE id = $id
    ");

    DB::Exec("COMMIT");

    $values["reference_number"] = $referenceNo;

    return true;

} catch (Exception $e) {

    DB::Exec("ROLLBACK");
    $message = "Unable to generate Reference Number.";
    return false;
}
		;
		return true;
	}
	public function default_request_date_efedit(  ) {
	$defaultValue = Now();
return $defaultValue;
}

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

public function default_requested_by_date_signed_efedit(  ) {
	$defaultValue = Now();
return $defaultValue;
}

public function default_approved_by_date_signed_efedit(  ) {
	$defaultValue = Now();
return $defaultValue;
}

public function default_received_by_date_signed_efedit(  ) {
	$defaultValue = Now();
return $defaultValue;
}

public function custom_print_vfview( $value, $data ) {
	if($data['status'] =="Approved"){

$value = "<a href='" . $_SESSION["path"] . "frameset?__report=EWRF_DSWD.rptdesig &txnid=".$data['id']."&ea=".$_SESSION["ea"]."&&__dpi=96&__format=pdf&__pageoverflow=2&__overwrite=false'>Print</a>";

}

else
{
$value ="";
};
return $value;
}	

}


?>