<?php
error_reporting(LC_ALL);
ini_set('display_errors',1);
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, $db, $timedate;


require_once('modules/EcmPrepaymentInvoices/EcmPrepaymentInvoice.php');
//require_once('modules/EcmPrepaymentInvoices/Forms.php');
require_once('include/json_config.php');

$json_config = new json_config();  
$json = getJSONobj();

	$file = 'modules/EcmGroupSales/EcmGroupSale.php';
	if(file_exists($file)) {
		$cc = array();
		require_once($file);
		$cc = EcmGroupSale::loadSettings();	
	}

  $OPT = array();

  $OPT['row_item_height'] = $cc['row_item_height'];
  $OPT['row_item_height_selected'] = $cc['row_item_height_selected'];
  $OPT['rows_on_item_list'] = $cc['rows_on_item_list'];
  $OPT['position_table_height'] = $OPT['row_item_height']*$OPT['rows_on_item_list']+40+$OPT['rows_on_item_list']*4;
  $OPT['quick_product_item_adding'] = $cc['quick_product_item_adding'];

require_once('include/MVC/View/SugarView.php');  
require_once('modules/EcmPrepaymentInvoices/views/EditView/view.edit.php');

$focus = new EcmPrepaymentInvoice(); 

if(isset($_REQUEST['record'])) {	
	$focus->retrieve($_REQUEST['record']);
	echo $focus->ecmsale_id;
	
}

echo $focus->register_date;

$cur = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select value from currency_nbp_archive where date<'".$focus->register_date."' and currency_id='".$focus->currency_id."' order by date desc limit 0,1;"));

if ($cur['value'])
	$focus->currency_value_nbp = $cur['value'];
	
var_dump($cur);
		
		
$edit = new EcmPrepaymentInvoicesViewEdit();
$edit->ss = new Sugar_Smarty();
$edit->module = 'EcmPrepaymentInvoices';
$edit->bean = $focus;

if($_REQUEST['ecmsale_id']){
	$OPT['new_number'] = true;
	$arr=array("parent_contact_name","to_nip","currency_id","template_id","parent_id","parent_name","parent_type","parent_address_street","parent_address_postalcode","parent_address_city","parent_address_country","total");
	include_once("modules/EcmSales/EcmSale.php");
	$sale=new EcmSale();
	$sale->retrieve($_REQUEST['ecmsale_id']);
	foreach($arr as $a)$edit->bean->$a=$sale->$a;
	$edit->bean->descritpion="PRZEDPŁATA";
	$edit->bean->register_date=$timedate->to_display_date(date("Y-m-d"));
	$edit->bean->payment_date=$timedate->to_display_date(date("Y-m-d"));
	$edit->bean->currency_value = $sale->currency_value;
	$edit->bean->to_vatid=$sale->to_vatid;
	$focus->position_list = str_replace('&quot;','\"',$focus->getPositionList($_REQUEST['ecmsale_id']));
	$edit->ss->assign("POSITION_LIST",$focus->position_list);
	  
}

  $tt="";
  $w=$GLOBALS[db]->query("select name,id,deleted,date_modified from ecmdocumenttemplates where deleted='0' or id='".$focus->template_id."'");
  while($r=$GLOBALS[db]->fetchByAssoc($w)){
  	   if($r['deleted']==1 && $_REQUEST['record'] && $focus->template_id){
	        $tt.='<option value="'.$r['id'].'"';
			if($r['id']==$focus->template_id)$tt.=' selected';
			$tt.='>'.$r['name'].' till '.$r['date_modified'].'</option>';
	   }
	   elseif($r['deleted']==0){
	        $tt.='<option value="'.$r['id'].'"';
			if($r['id']==$focus->template_id)$tt.=' selected';
			$tt.='>'.$r['name'].'</option>';
	   }
  }

  $edit->ss->assign("DOCUMENT_TEMPLATES_OPTIONS", $tt);

  $tt="";
  $w=$GLOBALS[db]->query("select name,id,value from ecmvats where deleted='0' order by name");
  while($r=$GLOBALS[db]->fetchByAssoc($w)){
	        $tt.='<option value="'.$r['id'].'"';
			if($r['value']==23)$tt.=' selected';
			$tt.='>'.$r['name'].'</option>';   
  }
		$edit->ss->assign("ECMVATS_OPTIONS", $tt);
		$edit->ss->assign("ecmsale_id", $_REQUEST['ecmsale_id']);
		$edit->ss->assign("ecmsale_name", $sale->document_no);
	
	
	//$edit->tplFile = 'include/ECM/EcmViews/EditView/Tabs/EditView.tpl';
$edit->tplFile = 'include/EditView/EditView.tpl';



  $scriptOpt = '<script language="javascript">
  
                 var OPT = '.str_replace('&quot;','\"',$json->encode($OPT)).';

                 var MOD = '.str_replace('&quot;','\"',$json->encode($mod_strings)).';

                 var N;
				 

                </script>';
echo $scriptOpt;
$edit->preDisplay();        


echo $edit->display();
//echo '<div id="subpanels">';
//require_once('subpanels.php');
//echo '</div>';
?>