<?php
class eventclass_purchaseorder1  extends TableEventsBase {
	
	function init() {
		$this->events = array(
	'BeforeShowAdd' => true,
	'OnPageLoad' => true 
);
		$this->fieldValues = array(
	'filterLimit' => array(
		 
	),
	'mapIcon' => array(
		 
	),
	'viewCustom' => array(
		 
	),
	'lookupWhere' => array(
		 
	),
	'viewFileText' => array(
		 
	),
	'defaultValue' => array(
		 
	),
	'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.

$transactionCode = "PO";

$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);
		;
		
	}
		

}


?>