<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( 'Not A Valid Entry Point' );
	
	// require_once('modules/EcmGroupSales/HeaderMenu.php');

global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings;

require_once ('modules/EcmStockDocOuts/EcmStockDocOut.php');
require_once ('include/time.php');
require_once ('include/json_config.php');
echo '<script type="text/javascript"
src="include/ECM/SearchProductTable.js"></script>';
$json_config = new json_config ();

$focus = new EcmStockDocOut ();

if (isset ( $_REQUEST ['record'] )) {
	$focus->retrieve ( $_REQUEST ['record'] );
	$focus->format_all_fields ();
	$OPT ['status'] = $focus->status;
}

require_once ('include/MVC/View/SugarView.php');
require_once ('modules/EcmStockDocOuts/views/DetailView/view.detail.my.php');
$edit = new ViewDetailMy ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmStockDocOuts';

// add pdf buttons
//$btn = '<input title="Generuj PDF" class="button" onclick="if($(\'#div_pdf\').css(\'display\') == \'none\'){$(\'#div_pdf\').show(\'slow\'); } else { $(\'#div_pdf\').hide(\'slow\'); }" type="button" name="productcard" id="productcard" value="Generuj PDF">';
//$btn .= '<div id="div_pdf" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
//$btn .= 'Typ: <select name="pdf_type" id="pdf_type"><option value="1">Dokument</option><option value="0">Operacje magazynowe</option></select><br /><br />';
$btn .= '<input name="quote_pdf" id="quote_pdf" title="Show PDF" accessKey="" class="button" onclick="window.open(\'index.php?module=EcmStockDocOuts&action=createPDF&to_pdf=1&pdf_type=\'+$(\'#pdf_type\').val()+\'&record=' . $_REQUEST ['record'] . '\',\'_blank\');" type="button" value="Pokaż PDF"></div>';
$edit->ss->assign ( "CREATE_PDF", $btn );
$btn2 .= '<input name="quote_pdf" id="quote_pdf" title="Show PDF" accessKey="" class="button" onclick="window.open(\'index.php?module=EcmStockDocOuts&action=createPDF2&to_pdf=1&pdf_type=\'+$(\'#pdf_type\').val()+\'&record=' . $_REQUEST ['record'] . '\',\'_blank\');" type="button" value="Pokaż PDF dla klienta"></div>';
$edit->ss->assign ( "CREATE_PDF2", $btn2 );
$can_edit = false;
global $current_user;
$db = $GLOBALS['db'];
$inv_button=true;
$out = $db->query("SELECT * FROM ecminvoiceouts WHERE wz_id like '%".$focus->id."%'");
if($out->num_rows>0){
    $inv_button=false;
}
//get operations

$out = $db->fetchByAssoc($db->query("SELECT sum(price*quantity) as sum FROM ecmstockoperations WHERE type='1' AND parent_id='".$focus->id."'"));
$edit->ss->assign("OP_OUT", format_number($out['sum']));
$in = $db->fetchByAssoc($db->query("SELECT sum(price*quantity) as sum FROM ecmstockoperations WHERE type='0' AND parent_id='".$focus->id."'"));
$edit->ss->assign("OP_IN", format_number($in['sum']));
$in = $db->fetchByAssoc($db->query("SELECT sum(quantity) as sum FROM ecmstockdocoutitems WHERE ecmstockdocout_id='".$focus->id."'"));
$edit->ss->assign("quantity", $in['sum']);
$edit->ss->assign ( "CREATED_BY_NAME", $focus->created_by_name );
$edit->ss->assign ( "MODIFIED_BY_NAME", $focus->modified_by_name );
$edit->ss->assign ( "inv_button", $inv_button );

$a = new EcmSale();
$a->retrieve($focus->so_id);

$the_array ['ZS'] = "<a  target='_blank' href='index.php?module=EcmSales&action=DetailView&record=$a->id'>".$a->document_no."</a>";
$edit->ss->assign ( "zs_no", $the_array ['ZS'] );

$out = $focus->db->query("SELECT document_no,id FROM ecminvoiceouts WHERE wz_id like '%".$focus->id."%' and deleted=0 and canceled=0");
$dane=$focus->db->fetchByAssoc($out);
$the_array ['INVOICE_DOC'] = '<a href="index.php?module=EcmInvoiceOuts&action=DetailView&record=' . $dane['id'] . '">' . $dane['document_no'] . '</a>';

$edit->ss->assign ( "fk_no", $the_array ['INVOICE_DOC']  );

$a = new Account();
$a->retrieve($focus->parent_id);
$edit->ss->assign ( "EDI", $a->parent_id == '1249' ? true : false );

$edit->bean = $focus;
$edit->tplFile = 'include/ECM/EcmViews/DetailView/Tabs/DetailView.tpl';

$edit->preDisplay ();
echo $edit->display ();

require_once ('include/SubPanel/SubPanelTiles.php');
$subpanel = new SubPanelTiles ( $focus, 'EcmStockDocOuts' );
echo $subpanel->display ();
// loading view
echo '<link rel="stylesheet" type="text/css" href="modules/EcmStockDocOuts/javascript/helper.css" media="screen" /><div class="loading_panel"></div>';
?>