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

$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);
		;
		
	}
	public function default_date_efedit(  ) {
	$defaultValue = Now();
return $defaultValue;
}

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

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

}

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

}


?>