<?php
class eventclass_stock_transfer_form2  extends TableEventsBase {
	
	function init() {
		$this->events = array(
	'OnPageLoad' => true,
	'BeforeAdd' => true,
	'BeforeProcessAdd' => true 
);
		$this->fieldValues = array(
	'filterLimit' => array(
		 
	),
	'mapIcon' => array(
		 
	),
	'viewCustom' => array(
		 
	),
	'lookupWhere' => array(
		 
	),
	'viewFileText' => array(
		 
	),
	'defaultValue' => array(
		'transfer_form_date' => array(
			'edit' => true 
		),
		'issued_date' => array(
			'edit' => true 
		),
		'received_date' => array(
			'edit' => true 
		),
		'witnessed_date' => array(
			'edit' => true 
		),
		'approved_date' => array(
			'edit' => true 
		) 
	),
	'autoUpdateValue' => array(
		 
	),
	'uploadFolder' => array(
		 
	),
	'viewPluginInit' => array(
		 
	),
	'editPluginInit' => array(
		 
	) 
);
			}
		function BeforeAdd( &$values, &$sqlValues, &$message, $inline, $pageObject ) {
		// ======================================================
// STF EXTERNAL
// BEFORE RECORD ADDED
// ======================================================


// ------------------------------------------------------
// TRANSFER FORM DATE
// ------------------------------------------------------

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

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


// ------------------------------------------------------
// ALLOCATE STF NUMBER
//
// transaction_numbering.next_no = next AVAILABLE number
//
// Example:
// next_no = 9
// generated = STF00009
// next_no becomes 10
// ------------------------------------------------------

$sqlAllocate = "
    UPDATE transaction_numbering
    SET next_no =
        LAST_INSERT_ID(next_no) + 1
    WHERE transaction_code = 'STF'
      AND is_active = 1
";

DB::Exec($sqlAllocate);


// ------------------------------------------------------
// Get allocated sequence
// ------------------------------------------------------

$rsSequence =
    DB::Query("
        SELECT
            LAST_INSERT_ID() AS allocated_no
    ");

$sequence =
    $rsSequence
    ? $rsSequence->fetchAssoc()
    : null;

$allocatedNo =
    $sequence
    ? intval($sequence["allocated_no"])
    : 0;


if ($allocatedNo <= 0) {

    $message =
        "Unable to generate STF number. " .
        "Please check transaction_numbering.";

    return false;
}


// ------------------------------------------------------
// Get STF numbering configuration
// ------------------------------------------------------

$rsConfig =
    DB::Query("
        SELECT
            prefix,
            digit_length
        FROM transaction_numbering
        WHERE transaction_code = 'STF'
          AND is_active = 1
        LIMIT 1
    ");

$config =
    $rsConfig
    ? $rsConfig->fetchAssoc()
    : null;


if (!$config) {

    $message =
        "STF numbering configuration was not found.";

    return false;
}


$prefix =
    trim(
        (string)$config["prefix"]
    );

$digits =
    intval(
        $config["digit_length"]
    );

if ($digits <= 0) {
    $digits = 5;
}


// ------------------------------------------------------
// FINAL STF NUMBER
// ------------------------------------------------------

$values["transfer_form_no"] =
    $prefix .
    str_pad(
        $allocatedNo,
        $digits,
        "0",
        STR_PAD_LEFT
    );


// ------------------------------------------------------
// INITIAL VALUES
// ------------------------------------------------------

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

$values["approval_status"] =
    "FOR_APPROVAL";

$values["digital_signature_status"] =
    "UNSIGNED";


return true;
		;
		return true;
	}

	function BeforeProcessAdd( $pageObject ) {
		/* =========================================================
   STF EXTERNAL
   ADD PAGE - BEFORE PROCESS

   Create real DRAFT master first,
   then immediately open it in Edit mode.

   This guarantees transfer_form_id exists before
   any detail rows are added.
   ========================================================= */


/* =========================================================
   1. GET STF NUMBERING
   ========================================================= */

DB::Exec("START TRANSACTION");


$rsNumber = DB::Query("

    SELECT
        prefix,
        digit_length,
        next_no

    FROM transaction_numbering

    WHERE transaction_code = 'STF'
      AND is_active = 1

    LIMIT 1

    FOR UPDATE

");


$numberRow = $rsNumber->fetchAssoc();


if (!$numberRow) {

    DB::Exec("ROLLBACK");

    die("STF transaction numbering is not configured.");
}


$prefix =
    trim($numberRow["prefix"]);


$digitLength =
    intval($numberRow["digit_length"]);


$sequenceNo =
    intval($numberRow["next_no"]);


if ($digitLength <= 0) {
    $digitLength = 5;
}


$transferFormNo =
    $prefix .
    str_pad(
        $sequenceNo,
        $digitLength,
        "0",
        STR_PAD_LEFT
    );


/* =========================================================
   2. ADVANCE NUMBER
   ========================================================= */

DB::Exec("

    UPDATE transaction_numbering

    SET next_no = next_no + 1

    WHERE transaction_code = 'STF'
      AND is_active = 1

");


DB::Exec("COMMIT");


/* =========================================================
   3. CREATE ACTUAL STF EXTERNAL DRAFT
   ========================================================= */

$draft = array();

$draft["transfer_form_no"] =
    $transferFormNo;

$draft["transfer_form_date"] =
    date("Y-m-d");

$draft["transfer_scope"] =
    "WAREHOUSE_TO_WAREHOUSE";

$draft["jurisdiction_type"] =
    "CROSS_JURISDICTION";

$draft["purpose"] =
    "WAREHOUSING";

$draft["visual_inspection_conducted"] =
    "NO";

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

$draft["approval_status"] =
    "FOR_APPROVAL";

$draft["digital_signature_status"] =
    "UNSIGNED";


DB::Insert(
    "stock_transfer_form",
    $draft
);


$insertError =
    DB::LastError();


if ($insertError != "") {

    die(
        "Unable to create STF External draft: "
        .
        $insertError
    );
}


/* =========================================================
   4. GET REAL MASTER ID
   ========================================================= */

$transferFormId =
    intval(
        DB::LastId()
    );


if ($transferFormId <= 0) {

    die(
        "Unable to determine the new STF External ID."
    );
}


/* =========================================================
   5. REDIRECT TO SAME RECORD IN EDIT MODE
   ========================================================= */

header(
    "Location: stock_transfer_form2_edit.php?editid1="
    .
    $transferFormId
);

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

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

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

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

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

}


?>