<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
	
global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings, $current_user, $app_list_strings;
	
require_once('modules/EcmTransactions/EcmTransaction.php');
require_once('modules/EcmTransactions/Forms.php');
require_once ('include/time.php');
require_once('include/json_config.php');

$json_config = new json_config();
	//ACLAction::addActions('EcmTransactions', 'EcmTransactions'); 
$focus = new EcmTransaction();   

if(isset($_REQUEST['record']))$focus->retrieve($_REQUEST['record']);
echo '<script type="text/javascript" src="modules/EcmTransactions/js/DetailView.js"></script>';
require_once('include/MVC/View/SugarView.php');  

if(file_exists('modules/EcmTransactions/view/DetailView/view.detail.my.php')) {
	require_once('modules/EcmTransactions/view/DetailView/view.detail.my.php');
	$detail = new ViewDetailMy();
		$detail->ss = new Sugar_Smarty();
	$detail->module = 'EcmTransactions';
}
else{
	require_once('include/MVC/View/views/view.detail.php');
	$detail = new ViewDetail();  
	$detail->ss = new Sugar_Smarty();
	$detail->module = 'EcmTransactions';
	
}

global $app_list_strings;

$db = $GLOBALS['db'];
if($focus->record_type!=''){
$mod=substr($focus->record_type, 0, -1);
$doc = new $mod();
$doc->retrieve($focus->record_id);
if($mod=='Document'){
	$detail->ss->assign('ASSIGNED_DOCUMENT', '<a target="new" href="index.php?module='.$focus->record_type.'&action=DetailView&record='.$focus->record_id.'">'.$doc->document_name.'</a>');
	
} else {
	$detail->ss->assign('ASSIGNED_DOCUMENT', '<a target="new" href="index.php?module='.$focus->record_type.'&action=DetailView&record='.$focus->record_id.'">'.$doc->document_no.'</a>');
	
}
}

$can_edit = true;
//get transactions
	$res = $db->query("
select t.id, t.name, rel.value from ecmtransactions_rel as rel
inner join ecmtransactions as t
on t.id = rel.ecmtransaction_a_id
where rel.ecmtransaction_b_id='$focus->id'");
	$trans = '<table style="width: 40%">';	
	while ($row = $db->fetchByAssoc($res)) {
		$can_edit = false;
		$row['value'] = format_number($row['value']);
		$trans.='<tr style="width: 100%">';
			$trans.='<td><a target="new" href="index.php?module=EcmTransactions&action=DetailView&record='.$row['id'].'">'.$row['name'].'</a></td>';
			$trans.='<td style="text-align: right">'.$row['value'].'</td>';
		$trans.='</tr>';
	}
	$res = $db->query("
select t.id, t.name, rel.value from ecmtransactions_rel as rel
inner join ecmtransactions as t
on t.id = rel.ecmtransaction_b_id
where rel.ecmtransaction_a_id='$focus->id'");	
	while ($row = $db->fetchByAssoc($res)) {
		$can_edit = false;
		$row['value'] = format_number($row['value']);
		$trans.='<tr style="width: 100%">';
		$trans.='<td><a target="new" href="index.php?module=EcmTransactions&action=DetailView&record='.$row['id'].'">'.$row['name'].'</a></td>';
		$trans.='<td style="text-align: right">'.$row['value'].'</td>';
		$trans.='</tr>';
	}
	$trans.='</table>';

$detail->ss->assign('RELATED_TRANSACTIONS', $trans);
$detail->ss->assign('CAN_EDIT', $can_edit);
$detail->ss->assign("MOD",$mod_strings);

$detail->bean = $focus;
$detail->preDisplay();        
unset($detail->defs['templateMeta']['form']['buttons'][2]);
echo $detail->display();
echo '<div id="subpanels_div">';

require_once('subpanels.php');

echo '</div>';
?>