<?php

if (!defined('sugarEntry') || !sugarEntry)
	die('Not A Valid Entry Point');

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

require_once ('modules/EcmSales/EcmSale.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 EcmSale ();

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/EcmSales/views/DetailView/view.detail.my.php');

//create position list table
$pl = $focus->getPositionList();

$edit = new ViewDetailMy ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmSales';
$fk = '<input title="{$APP.LBL_CREATE_INVOICE}" accessKey="{$APP.LBL_CREATE_INVOICE|" 
        class="button" type="submit" name="create_fk" value="{$APP.LBL_CREATE_INVOICE}" id="create_fk">';

//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="PDF">';
$btn .= '<div id="div_pdf" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
$btn .= 'Typ: <select name="pdf_images" id="pdf_type"><option value="1">Zamówienie sprzedaży</option>
        <option value="0">Proforma</option><option value="2">Oferta</option></select><br /><br />';
$btn .= 'Pokaż rabat: <select name="discount_show" id="discount_show"><option value="1">Tak</option>
        <option value="0">Nie</option></select><br /><br />';
$btn .= '<input name="quote_pdf" id="quote_pdf" title="Show PDF" accessKey="" class="button" onclick="window.open(\'index.php?module=EcmSales&action=createPDF&to_pdf=1&show_img=no&discount_show=\'+$(\'#discount_show\').val()+\'&pdf_type=\'+$(\'#pdf_type\').val()+\'&show_ean2=\'+$(\'#pdf_ean2\').val()+\'&show_recipient_code=\'+$(\'#pdf_recipient_code\').val()+\'&record=' . $_REQUEST['record'] . '\',\'_blank\');" type="button" value="Pokaż PDF"></div>';
$edit->ss->assign("CREATE_PDF", $btn);

$btn = '<input title="Generuj Katalog" class="button" onclick="if($(\'#div_cat\').css(\'display\') == \'none\'){$(\'#div_cat\').show(\'slow\'); } else { $(\'#div_cat\').hide(\'slow\'); }" type="button" name="productcat" id="productcat" value="Generuj Katalog">';
$btn .= '<div id="div_cat" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
$btn .= 'EAN: <select name="cat_ean" id="cat_ean"><option value="1">1</option><option value="2">2</option><option value="0">Brak</option></select><br /><br />';
$btn .= '<input name="cat_pdf" id="cat_pdf" title="Show Cat" accessKey="" class="button" onclick="window.open(\'index.php?module=EcmSales&action=createCatalogue&to_pdf=1&show_ean=\'+$(\'#cat_ean\').val()+\'&record=' . $_REQUEST['record'] . '\',\'_blank\');" type="button" value="Pokaż Katalog"></div>';
$edit->ss->assign("CATALOGUE", $btn);

$btn = '<input name="quote_xls" id="quote_xls" title="Show XLS" accessKey="" class="button" onclick="window.open(\'index.php?module=EcmSales&action=createXLS&to_pdf=1&show_ean=\'+$(\'#pdf_ean\').val()+\'&show_ean2=\'+$(\'#pdf_ean2\').val()+\'&show_recipient_code=\'+$(\'#pdf_recipient_code\').val()+\'&record=' . $_REQUEST['record'] . '\',\'_blank\');" type="button" value="Pokaż XLS"></div>';
$edit->ss->assign("CREATE_XLS", $btn);

if ($focus->check_mail_addresses() == 1) {
	//add pdf buttons
	$btn = '<input title="Wyślij dokument" class="button" onclick="if($(\'#div_mail\').css(\'display\') == \'none\'){$(\'#div_mail\').show(\'slow\'); } else { $(\'#div_mail\').hide(\'slow\'); }" type="button" name="productcard" id="productcard" value="Wyślij dokument">';
	$btn .= '<div id="div_mail" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
	$btn .= 'Typ: <select name="pdfs_type" id="pdfs_type"><option value="1">Zamówienie sprzedaży</option>
        <option value="0">Proforma</option><option value="2">Oferta</option></select><br /><br />';
	$btn.='<input name="send_mail" id="send_mail" class="button" type="button" value="' . $mod_strings['LBL_SEND_MAIL'] . '" onClick="SendMail(\'' . $focus->id . '\',$(\'#pdfs_type\').val(),\'' . $focus->parent_id . '\');"></div>';
	$edit->ss->assign("SEND_PDF", $btn);
	echo '<div id="sendreturn" style="display:none;"></div>';
}

$can_edit = true;
global $current_user;
if (($focus->created_by == $current_user->id || is_admin($current_user || $current_user->id=='6ff351c9-c340-7e81-4f29-59d5eb8c71c2')) && $focus->status != 's90')
	$can_edit = true;

$can_prepaid=false;
$q=$GLOBALS['db']->query("select sum(prepaid_amount) as total from ecmprepaymentinvoices where ecmsale_id='".$focus->id."'");
$res=$GLOBALS['db']->fetchByAssoc($q);
if($res['total']<$focus->total_brutto){
    $can_prepaid=true;
}
$create_code = '<input title="Utwórz" class="button" onclick="if($(\'#div_create\').css(\'display\') == \'none\'){$(\'#div_create\').show(\'slow\'); } else { $(\'#div_create\').hide(\'slow\'); }"
         type="button" name="productcat" id="productcat" value="Utwórz dokument">';
$create_code .= '<div id="div_create" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
$create_code .= 'Dokument: <select name="doc_type" id="doc_type">';
$create_code .= '<option value="EcmInvoiceOuts">Fakturę</option>';
$create_code .= '<option value="EcmReceipts">Paragon</option>';
if($can_prepaid==true){
    $create_code .= '<option value="EcmPrepaymentInvoices">Fakturę zaliczkowa</option>';
}
$create_code .= '<option value="EcmStockDocOuts">WZ</option>';
$create_code .= '</select><br /><br />';
$create_code .= '<input name="cat_pdf" id="cat_pdf" title="Show Cat" accessKey="" class="button" 
        onclick="createDoc(\''.$focus->id.'\')" type="button" value="Utwórz"></div>';

$edit->ss->assign("CREATE_DOCS",$create_code);
$edit->ss->assign("CAN_EDIT", $can_edit);
$edit->ss->assign("CAN_PREPAID", $can_prepaid);
$edit->ss->assign("CREATE_FK", $fk);
$edit->ss->assign("CREATED_BY_NAME", $focus->created_by_name);
$edit->ss->assign("MODIFIED_BY_NAME", $focus->modified_by_name);

$a = new EcmStockDocOut();
$a->retrieve_by_string_fields(['so_id'=>$focus->id]);

$the_array ['WZ'] = "<a  target='_blank' href='index.php?module=EcmStockDocOuts&action=DetailView&record=$a->id'>".$a->document_no."</a>";

$edit->ss->assign ( "wz_no", $the_array ['WZ']  );

$a = new EcmInvoiceOut();
$a->retrieve_by_string_fields(['so_id'=>$focus->id]);
$the_array ['FV'] = "<a  target='_blank'  href='index.php?module=EcmInvoiceOuts&action=DetailView&record=$a->id'>".$a->document_no."</a>";

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

$edit->ss->assign("can_copy_to_e5", isset($focus->edi_zs_id) && strlen($focus->edi_zs_id) > 0 ? false : true);

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

$edit->preDisplay();

//check number
$db = $GLOBALS['db'];
$res = $db->query("SELECT id FROM ecmsales WHERE document_no='$focus->document_no' and deleted='0'");
if ($res->num_rows > 1) {
	echo '<h1 style="color: red;">Błąd numeracji, skontaktuj się z administratorem! Nie drukuj/publikuj dokumentu!</h1>';
}

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