<?php
/*****************************************************************************
 * The contents of this file are subject to the RECIPROCAL PUBLIC LICENSE
 * Version 1.1 ("License"); You may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/rpl.php. Software distributed under the
 * License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied.
 *
 * You may:
 * a) Use and distribute this code exactly as you received without payment or
 *    a royalty or other fee.
 * b) Create extensions for this code, provided that you make the extensions
 *    publicly available and document your modifications clearly.
 * c) Charge for a fee for warranty or support or for accepting liability
 *    obligations for your customers.
 *
 * You may NOT:
 * a) Charge for the use of the original code or extensions, including in
 *    electronic distribution models, such as ASP (Application Service
 *    Provider).
 * b) Charge for the original source code or your extensions other than a
 *    nominal fee to cover distribution costs where such distribution
 *    involves PHYSICAL media.
 * c) Modify or delete any pre-existing copyright notices, change notices,
 *    or License text in the Licensed Software
 * d) Assert any patent claims against the Licensor or Contributors, or
 *    which would in any way restrict the ability of any third party to use the
 *    Licensed Software.
 *
 * You must:
 * a) Document any modifications you make to this code including the nature of
 *    the change, the authors of the change, and the date of the change.
 * b) Make the source code for any extensions you deploy available via an
 *    Electronic Distribution Mechanism such as FTP or HTTP download.
 * c) Notify the licensor of the availability of source code to your extensions
 *    and include instructions on how to acquire the source code and updates.
 * d) Grant Licensor a world-wide, non-exclusive, royalty-free license to use,
 *    reproduce, perform, modify, sublicense, and distribute your extensions.
 *
 * The Original Code is: CommuniCore
 *                       Olavo Farias
 *                       2006-04-7 olavo.farias@gmail.com
 *
 * The Initial Developer of the Original Code is CommuniCore.
 * Portions created by CommuniCore are Copyright (C) 2005 CommuniCore Ltda
 * All Rights Reserved.
 ********************************************************************************/
include ("modules/EcmProducts/moduleMenu.php");

if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( 'Not A Valid Entry Point' );
$_SESSION ['pricebook_duplicate_id'] = $_REQUEST ['record'];
require_once ('XTemplate/xtpl.php');
require_once ('data/Tracker.php');
require_once ('modules/EcmPriceBooks/EcmPriceBook.php');
require_once ('modules/EcmPriceBooks/Forms.php');
require_once ('include/DetailView/DetailView.php');

global $mod_strings;
global $app_strings;
global $db;

$focus = new EcmPriceBook ();

$detailView = new DetailView ();
$offset = 0;

// ONLY LOAD A RECORD IF A RECORD ID IS GIVEN;
// A RECORD ID IS NOT GIVEN WHEN VIEWING IN LAYOUT EDITOR
if (isset ( $_REQUEST ['offset'] ) or isset ( $_REQUEST ['record'] )) {
	$result = $detailView->processSugarBean ( "ECMPRICEBOOKS", $focus, $offset );
	if ($result == null) {
		sugar_die ( $app_strings ['ERROR_NO_RECORD'] );
	}
	$focus = $result;
} else {
	header ( "Location: index.php?module={EcmPriceBooks}&action=index" );
}

if (isset ( $_REQUEST ['isDuplicate'] ) && $_REQUEST ['isDuplicate'] == 'true') {
	$focus->id = "";
}
echo "\n<p>\n";
echo get_module_title ( $mod_strings ['LBL_MODULE_ID'], $mod_strings ['LBL_MODULE_NAME'] . ": " . $focus->name, true );
echo "\n</p>\n";
global $theme;
$theme_path = "themes/" . $theme . "/";
$image_path = $theme_path . "images/";
require_once ($theme_path . 'layout_utils.php');

 if(!$_SESSION['pricebook_check'][$_REQUEST['pricebook_id']]['all']){ $result = $GLOBALS['db']->query("select id from ecmpricebooks_ecmproducts"); $checkc=''; while($ro=$GLOBALS['db']->fetchByAssoc($result)){ $_SESSION['pricebook_check'][$_REQUEST['pricebook_id']][$ro['id']]="true"; } $_SESSION['pricebook_check'][$_REQUEST['pricebook_id']]['all']="true"; }
 
$_SESSION['pricebook_check'][$_REQUEST['record']]['all']="true";
$GLOBALS ['log']->info ( "EcmPriceBooks detail view" );
$focus->format_all_fields ();
$xtpl = new XTemplate ( 'modules/EcmPriceBooks/DetailView.html' );
$xtpl->assign ( "MOD", $mod_strings );
$xtpl->assign ( "APP", $app_strings );
$xtpl->assign ( "THEME", $theme );
$xtpl->assign ( "GRIDLINE", $gridline );
$xtpl->assign ( "IMAGE_PATH", $image_path );
$xtpl->assign ( "PRINT_URL", "index.php?" . $GLOBALS ['request_string'] );
$xtpl->assign ( "ID", $focus->id );
$xtpl->assign ( "ASSIGNED_TO", $focus->assigned_user_name );
$xtpl->assign ( "NAME", $focus->name );
$xtpl->assign ( "DATE_ENTERED", $focus->date_entered );
$xtpl->assign ( "DATE_MODIFIED", $focus->date_modified );
include ("modules/EcmDocs/Pagination.php");
$xtpl->assign ( "PAGINATION1", getPagination ( "ecmpricebooks", "EcmPriceBooks", $_REQUEST ['record'] ) );

if ($focus->active == '1')
	$xtpl->assign ( 'ACTIVE', "Active" );
elseif ($focus->active == '2')
	$xtpl->assign ( 'ACTIVE', "Inactive" );
if ($focus->archived == '1')
	$xtpl->assign ( 'ARCHIVED', "checked" );
$xtpl->assign ( "DESCRIPTION", nl2br ( url2html ( $focus->description ) ) );
$r = $db->fetchByAssoc ( $db->query ( "select name from ecmproductexchangerates where id='" . $focus->exchange_rate_id . "'" ) );
$focus->exchange_rate_name = $r ['name'];

$desc = '';
$desc .= '<input title="Product Card" class="button" onclick="if(document.getElementById(\'div_desc2\').style.display==\'none\')document.getElementById(\'div_desc2\').style.display=\'block\';else document.getElementById(\'div_desc2\').style.display=\'none\';" type="button" name="productcard" id="productcard" value="' . $mod_strings ['LBL_PRODUCT_CARD'] . '">';
$desc .= '<div id="div_desc2" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
$desc .= 'Generuj jako JPG: <select name="new_image" id="new_image"><option value="1">Yes</option><option value="0" selected>No</option></select><br /><br />';
$desc .= $mod_strings ['LBL_LIST_PRICE_NAME_A'] . ': <select name="new_show_price" id="new_show_price"><option value="1">Yes</option><option value="0" selected>No</option></select><br /><br />';
$desc .= $mod_strings ['LBL_LIST_PRICE_NAME'] . ': <select name="new_price" id="new_price"><option value="pricebook_price">Pricebook price</option><option value="srp_price">SRP Price</option></select><br /><br />';
$desc .= $mod_strings ['LBL_LANGS'] . ': <select name="new_language" id="new_language"><option value="pl">pl</option><option value="en">en</option></select><br /><br />';
$desc .= 'EAN: <select name="new_ean" id="new_ean"><option value="1">1</option><option value="2">2</option></select><br /><br />';
$desc.='<input type="button" class="button" name="generate" id="generate" value="' . $mod_strings ['LBL_PRODUCT_CARD'] . '" onclick="window.open(\'index.php?module=EcmProducts&action=generateProductCardsFromPricebook&record='.$_REQUEST['record'].'&to_pdf=1&price=\'+document.getElementById(\'new_price\').value+\'&create_img=\'+document.getElementById(\'new_image\').value+\'&show_price=\'+document.getElementById(\'new_show_price\').value+\'&language=\'+document.getElementById(\'new_language\').value+\'&ean=\'+document.getElementById(\'new_ean\').value);"></div>';


$xtpl->assign ( "CATALOGUE", $desc );

$xtpl->assign ( "EXCHANGE_RATE_NAME", $focus->exchange_rate_name );
$xtpl->assign ( "EXCHANGE_RATE_ID", $focus->exchange_rate_id );
$xtpl->assign ( "ACCOUNT_NAME", $focus->account_name );
$xtpl->assign ( "ACCOUNT_ID", $focus->account_id );
$xtpl->assign ( "LEAD_NAME", $focus->lead_name );
$xtpl->assign ( "LEAD_ID", $focus->lead_id );
if ($focus->name == "leobitepl" || $focus->name == "www.e5" || $focus->name == "getmir") {
	// $xtpl->assign("NAME_style", 'style="display:none;"');
	if ($focus->name == "leobitepl") {
		$addrl = "leobite.pl";
		$shop = "leobite";
	} elseif ($focus->name == "getmir") {
		$addrl = "getmir.com";
		$shop = "Getmir shop";
	} else {
		$addrl = "e5.pl";
		$shop = "e5 shop PL";
	}
	$xtpl->assign ( "LEOBITE", '<input title="Update prices on leobite" 
		        class="button" 
		        onclick="window.open(\'http://www.' . $addrl . '/administrator/components/com_virtuemart/more7_refreshStock.php?m7_debug=0&m7_test=1\');" 
		        type="button" 
		        name="upol" 
		        value=" Update prices on ' . $shop . ' ">' );
}
if ($focus->name == "www.e5") {
	$addrl = "shop-en.e5.pl";
	$shop = "e5 shop EN";
	$xtpl->assign ( "LEOBITEEN", '<input title="Update prices on leobite" 
		        class="button" 
		        onclick="window.open(\'http://' . $addrl . '/administrator/components/com_virtuemart/more7_refreshStock.php?m7_debug=0&m7_test=1\');" 
		        type="button" 
		        name="upol" 
		        value=" Update prices on ' . $shop . ' ">' );
}
/*
$xtpl->assign ( "SHOWPDF", '<input title="Pokaż PDF" 
		        class="button" 
		        onclick="location.href=\'index.php?module=EcmPriceBooks&action=CreatePDF&record=' . $focus->id . '&to_pdf=1&disposition=D&images=\'+document.getElementById(\'images\').checked;" 
		        type="button" 
		        name="showpdf" 
		        value="Pokaż PDF">' ); */
$xtpl->assign ( "SORT_ORDER", '<input title="Sortuj według" 
		        class="button" 
		        onclick="location.href=\'index.php?module=EcmPriceBooks&action=categoryOrder&record=' . $focus->id . '\';" 
		        type="button" 
		        name="sort_order" 
		        value="Sortuj według">' );
require_once ('modules/Currencies/Currency.php');
$currency = new Currency ();
if (isset ( $focus->exchange_rate_id ) && ! empty ( $focus->exchange_rate_id )) {
	$currency->retrieve ( $focus->exchange_rate_id );
	if ($currency->deleted != 1) {
		$xtpl->assign ( "CURRENCY", $currency->iso4217 . ' ' . $currency->symbol );
	} else
		$xtpl->assign ( "CURRENCY", $currency->getDefaultISO4217 () . ' ' . $currency->getDefaultCurrencySymbol () );
} else {
	
	$xtpl->assign ( "CURRENCY", $currency->getDefaultISO4217 () . ' ' . $currency->getDefaultCurrencySymbol () );
}

if ($_SESSION ['pricebook_images'] == "true")
	$xtpl->assign ( "IMAGES_CHECKED", "checked" );

global $current_user;

if (is_admin ( $current_user ) && $_REQUEST ['module'] != 'DynamicLayout' && ! empty ( $_SESSION ['editinplace'] )) {
	$xtpl->assign ( "ADMIN_EDIT", "<a href='index.php?action=index&module=DynamicLayout&from_action=" . $_REQUEST ['action'] . "&from_module=" . $_REQUEST ['module'] . "&record=" . $_REQUEST ['record'] . "'>" . get_image ( $image_path . "EditLayout", "border='0' alt='Edit Layout' align='bottom'" ) . "</a>" );
}

$result = $db->query ( "select ecmproduct_id,price from ecmpricebooks_ecmproducts where ecmpricebook_id='" . $_REQUEST ['record'] . "' and deleted='0'" );

$products = "";
while ( ($row = $db->fetchByAssoc ( $result )) != null ) {
	$row1 = $db->fetchByAssoc ( $db->query ( "select name from ecmproducts where id='" . $row ['ecmproduct_id'] . "' and deleted='0'" ) );
	$products .= $row1 ['name'] . ", price: " . $row ['price'] . "<br>";
}
$xtpl->assign ( "PRODUCTS", $products );

$result = $db->query ( "select id,name from ecmpricebooks_customview where name!='' order by name asc" );
if (! $_COOKIE ['customview_id'] || $_COOKIE ['customview_id'] == "select" || $_COOKIE ['customview_id'] == "Select") {
	$rrr = $GLOBALS ['db']->fetchByAssoc ( $GLOBALS ['db']->query ( "select id,name from ecmpricebooks_customview where name!='' order by name asc" ) );
	setcookie ( "customview_id", $rrr ['id'], time () + 3600 * 24 * 60 );
	$_COOKIE ['customview_id'] = $rrr ['id'];
}
$products = "";
$cv_select = '<select name="customview" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmPriceBooks&action=ListViewProductsAjax&customview_id=\'+this.value+\'&pricebook_id=' . $_REQUEST ['record'] . '&sorder=asc\',\'products\');" id="customview"><option>Select</option>';
while ( ($row = $db->fetchByAssoc ( $result )) != null ) {
	$cv_select .= '<option value="' . $row ['id'] . '"';
	if ($_COOKIE ['customview_id'] == $row ['id'])
		$cv_select .= ' selected';
	$cv_select .= '>' . $row ['name'] . "</option>";
}
$cv_select .= '</select>&nbsp;<a style="cursor:pointer;" onclick="location.href=\'index.php?module=EcmPriceBooks&action=EditCustomView&record=\'+document.getElementById(\'customview\').value;">' . $mod_strings ['LBL_CV_EDIT'] . '</a>&nbsp;|&nbsp;<a style="cursor:pointer;" onclick="if(confirm(\'' . $mod_strings ['LBL_CV_QUESTION'] . '\'))location.href=\'index.php?module=EcmPriceBooks&action=DeleteCustomView&record=\'+document.getElementById(\'customview\').value;">' . $mod_strings ['LBL_CV_DELETE'] . '</a>&nbsp;|&nbsp;<a style="cursor:pointer;" onclick="location.href=\'index.php?module=EcmPriceBooks&action=EditCustomView\';">' . $mod_strings ['LBL_CV_CREATE'] . '</a><script>mintajaxget(\'index.php?to_pdf=1&module=EcmPriceBooks&action=ListViewProductsAjax&customview_id=' . $_COOKIE ['customview_id'] . '&pricebook_id=' . $_REQUEST ['record'] . '&sorder=asc\',\'products\');</script>';
$xtpl->assign ( "CUSTOMVIEW_SELECT", $cv_select );

$xtpl->assign ( "CURRENCY_VALUE", $focus->currency_value );
$xtpl->assign ( "CREATED_BY", $focus->created_by_name );
$xtpl->assign ( "MODIFIED_BY", $focus->modified_by_name );
$desc='';
$desc.='<input title="'.$mod_strings ['LBL_SHOW_PDF'] .'" class="button" onclick="if(document.getElementById(\'div_desc\').style.display==\'none\')document.getElementById(\'div_desc\').style.display=\'block\';else document.getElementById(\'div_desc\').style.display=\'none\';" type="button" name="productcard" id="productcard" value="Generuj PDF">';
$desc .= '<div id="div_desc" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
$lol="'index.php?to_pdf=1&module=EcmPriceBooks&action=selectImages','images'";
$desc .=$mod_strings ['LBL_LANGS'] .' :<select tabindex="108" title="" id="langs" name="langs">
<option selected="selected" value="pl_pl" label="Polski">PL</option>
<option value="en_us" label="Angielski">US</option>
</select><br>';
$desc .= 'Pokaż zdjęcia: <input type="checkbox" value="1" id="images" name="images" onclick="mintajaxget('.$lol.');"/><br /><br />';
$desc .= '<input title="Pokaż PDF" 
		        class="button" 
		        onclick="location.href=\'index.php?module=EcmPriceBooks&action=CreatePDF&record=' . $focus->id . '&to_pdf=1&disposition=D&images=\'+document.getElementById(\'images\').checked+\'&langs=\'+document.getElementById(\'langs\').value;" 
		        type="button" 
		        name="showpdf" 
		        value="'.$mod_strings ['LBL_SHOW_PDF'] .'"></div>';


   $xtpl->assign ("PDFGEN",$desc);
  $desc='';
$desc.='<input title="'.$mod_strings ['LBL_SHOW_XLS2'] .'" class="button" onclick="if(document.getElementById(\'div_desc3\').style.display==\'none\')document.getElementById(\'div_desc3\').style.display=\'block\';else document.getElementById(\'div_desc3\').style.display=\'none\';" type="button" name="productcard" id="productcard" value="Generuj XLS">';
$desc .= '<div id="div_desc3" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
//$lol="'index.php?to_pdf=1&module=EcmPriceBooks&action=selectImages','images'";
$desc .=$mod_strings ['LBL_LANGS'] .' :<select tabindex="108" title="" id="langs2" name="langs2">
<option selected="selected" value="pl_pl" label="Polski">PL</option>
<option value="en_us" label="Angielski">US</option>
</select><br>';
//$desc .= 'Pokaż zdjęcia: <input type="checkbox" value="1" id="images" name="images" onclick="mintajaxget('.$lol.');"/><br /><br />';
   $desc.='<input value="'.$mod_strings ['LBL_SHOW_XLS'] .'" 
		        class="button" 
		        onclick="location.href=\'index.php?module=EcmPriceBooks&amp;action=CreateXLS&amp;to_pdf=1&amp;disposition=D&langs2=\'+document.getElementById(\'langs2\').value;" type="button" >';
 $xtpl->assign("GENXLS",$desc);
// $xtpl->assign("CATALOGUE",'<input type="button" class="button" value="Create Catalogue PL" onclick="location.href=\'index.php?module=EcmPriceBooks&action=createCatalogue&language=pl&record='.$focus->id.'&to_pdf=1\';">&nbsp;<input type="button" class="button" value="Create Catalogue EN" onclick="location.href=\'index.php?module=EcmPriceBooks&action=createCatalogue&language=en&record='.$focus->id.'&to_pdf=1\';">');

$detailView->processListNavigation ( $xtpl, "ECMPRICEBOOKS", $offset, $focus->is_AuditEnabled () );

// ADDING CUSTOM FIELDS:
require_once ('modules/DynamicFields/templates/Files/DetailView.php');

if (! empty ( $focus->id )) {
	$merge_button = <<<EOQ
<input title="{$app_strings['LBL_DUP_MERGE']}" accessKey="M" class="button" onclick="this.form.return_module.value='EcmPriceBooks'; this.form.return_action.value='DetailView';this.form.return_id.value='{$focus->id}'; this.form.action.value='Step1'; this.form.module.value='MergeRecords';" type="submit" name="Merge" value=" {$app_strings['LBL_DUP_MERGE']} "/>
EOQ;
	$xtpl->assign ( "FIND_DUPES_MERGE_BUTTON", $merge_button );
}
if ($_REQUEST ['tab'])
	$xtpl->assign ( "LOAD_TAB", "<script>showBlock('" . $_REQUEST ['tab'] . "');</script>" );
$xtpl->parse ( "main" );
$xtpl->out ( "main" );

$sub_xtpl = $xtpl;
$old_contents = ob_get_contents ();
ob_end_clean ();
ob_start ();
echo $old_contents;

require_once ('modules/SavedSearch/SavedSearch.php');
$savedSearch = new SavedSearch ();
$json = getJSONobj ();
$savedSearchSelects = $json->encode ( array (
		$GLOBALS ['app_strings'] ['LBL_SAVED_SEARCH_SHORTCUT'] . '<br>' . $savedSearch->getSelect ( 'EcmPriceBooks' ) 
) );
$str = "<script>
   YAHOO.util.Event.addListener(window, 'load', SUGAR.util.fillShortcuts, $savedSearchSelects);
   </script>";
echo $str;
setcookie ( "pricebook_id", $_REQUEST ['record'], time () + 60 * 24 * 60 * 3600 );
$_SESSION ['pricebook_id'] = $_REQUEST ['record'];
?>
