<?php
$EASY_AUTHENTICATE=true;
include("_config/addCfg.php");
include("_includes/addInc.php");

$orderFormHandler = \Modules\Orders\OrderForm\Model_Handler::getInstance();

// GET OWNER CORPORATE/CUSTOMER DETAILS ///////////////////////////////////////////////////////////
$corporateOwnerEnt = \Entity\Corporate::select($orderFormHandler->getIdCorporateOwner());
$customerOwnerEnt = \Entity\Customer::select($orderFormHandler->getIdCustomerOwner());


// credit limit status
$corporateTotalBalance = $corporateOwnerEnt->equivTotalBalance;
$creditLimitStatus = $corporateOwnerEnt->creditLimitStatus;
if ($creditLimitStatus=='notSet') {
	$displayCorporate="none";
}
elseif ($creditLimitStatus=='ok') {
	$displayCorporate="normal";
}
else {
	$displayCorporate=$creditLimitStatus;
}

// GET OWNER CORPORATE OFFICES LIST ///////////////////////////////////////////////////////////////
if ($orderFormHandler->getIdCorporateOwner()) {
	$ownerCorporateOfficesListSQL="SELECT id, name, mainOffice, costCenters FROM `$DBTCorporatesOffices` WHERE idCorporate='{$orderFormHandler->getIdCorporateOwner()}' AND deleted=0";
	$ownerCorporateOfficesList=$db->GetAll($ownerCorporateOfficesListSQL);
	if ($db->ErrorNo()) {
		writeLog("Could not get corporate owner offices.", 'events', 9);
	}
}
// search for the selected office in the list
$corporateOwnerOfficeCcs = [];
if (!$ownerCorporateOfficesList) {
	$R['idCorporateOwnerOffice']=0;
} else {
	$officeFound=false;
	foreach($ownerCorporateOfficesList as $off) {
		if ($off['id']==$R['idCorporateOwnerOffice']) {
			$officeFound=true;
			if ($off['costCenters']) {
				$corporateOwnerOfficeCcs = array_map('trim', explode("\n", $off['costCenters']));
			}
			break;
		}
	}
	// if office not found - empty the field
	if (!$officeFound) $R['idCorporateOwnerOffice']=0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TEMPLATE ASSIGNMENTS ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

$smarty->assign('corporateOwnerEnt', $corporateOwnerEnt);
$smarty->assign('idCorporateOwner', $orderFormHandler->getIdCorporateOwner());
$smarty->assign('corporateTotalBalance', $corporateTotalBalance);
$smarty->assign('defaultCurrency', $CFG['currencies']['defaultCurrency']);
$smarty->assign('displayCorporate', $displayCorporate);
$smarty->assign('ownerCorporateOfficesList', $ownerCorporateOfficesList);
$smarty->assign('idCorporateOwnerOffice', $R['idCorporateOwnerOffice']);
$smarty->assign('officeFound', $officeFound);

$smarty->assign('customerOwnerEnt', $customerOwnerEnt);
$smarty->assign('idCustomerOwner', $orderFormHandler->getIdCustomerOwner());
$smarty->assign('corporateOwnerOfficeCcs', $corporateOwnerOfficeCcs);
$smarty->assign('corporateOwnerOfficeCostCenter', $R['corporateOwnerOfficeCostCenter']);


// template assignments
$smarty->display("showOrderOwnerRemote.tpl");

?>