<? 
$account=$_GET['account'];
$type=$_GET['type'];

if(!$_GET['date_from'])$date_from=date("Y-m-d");
else $date_from=$GLOBALS['timedate']->to_db_date($_GET['date_from']);
$exp=explode("-",$date_from);
$date_from=date("Y-m-d",mktime(0,0,0,$exp[1],$exp[2],$exp[0])+24*3600);
if(!$date_from)$date_from=date("Y-m-d");

if(!$_GET['date_to'])$date_to=date("Y-m-d");
else $date_to=$GLOBALS['timedate']->to_db_date($_GET['date_to']);
$exp=explode("-",$date_to);
$date_to=date("Y-m-d",mktime(0,0,0,$exp[1],$exp[2],$exp[0])+24*3600);
if(!$date_to)$date_to=date("Y-m-d");

?>
<table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="themes/Sugar/images/EcmProducts.gif" style="margin-top: 3px; margin-right: 3px;" alt="EcmProducts" width="16" border="0" height="16"></td><td><h2>Invoices: Daily sales </h2></td></tr></table><br />
<ul class="tablist" style="width:100%;">
<li>
	<a class="current" href="#">Basic Search</a>
</li>		
</ul>
<form action="index.php" method="get">
<input type="hidden" name="module" value="EcmReports" />
<input type="hidden" name="action" value="ListDailySales" />
<table style="border-top: 0px none; margin-bottom: 4px;width:100%" class="tabForm" border="0" cellpadding="0" cellspacing="0">
<tbody>
	<tr>			
		<td class="dataLabel" width="10%" nowrap="nowrap">
			Date from	</td>
		<td class="dataField" width="30%" nowrap="nowrap">
			<input autocomplete="off" name="date_from" id="date_from" value="<? echo $GLOBALS['timedate']->to_display_date($date_from);?>" title="" tabindex="" size="11" maxlength="10" type="text">
			<img src="themes/default/images/jscalendar.gif" alt="Enter Date" id="date_from_trigger" align="absmiddle" border="0">
			<script type="text/javascript">
			Calendar.setup ({
			inputField : "date_from",
			daFormat : "<? echo str_replace("d","%d",str_replace("m","%m",str_replace("Y","%Y",$GLOBALS['timedate']->get_date_format())));?>",
			button : "date_from_trigger",
			singleClick : true,
			dateStr : "",
			step : 1
			}
			);
			</script>
	  	</td>	
		<td class="dataLabel" width="10%" nowrap="nowrap">
			Date to	</td>
		<td class="dataField" width="30%" nowrap="nowrap">
			<input autocomplete="off" name="date_to" id="date_to" value="<? echo $GLOBALS['timedate']->to_display_date($date_to);?>" title="" tabindex="" size="11" maxlength="10" type="text">
			<img src="themes/default/images/jscalendar.gif" alt="Enter Date" id="date_to_trigger" align="absmiddle" border="0">
			<script type="text/javascript">
			Calendar.setup ({
			inputField : "date_to",
			daFormat : "<? echo str_replace("d","%d",str_replace("m","%m",str_replace("Y","%Y",$GLOBALS['timedate']->get_date_format())));?>",
			button : "date_to_trigger",
			singleClick : true,
			dateStr : "",
			step : 1
			}
			);
			</script>
	  	</td>	
		<td class="dataLabel" width="10%" nowrap="nowrap">
			Account	</td>
		<td class="dataField" width="30%" nowrap="nowrap">
			<select name="account">
			<option value="">select</option>
			<?php
			$w=$GLOBALS[db]->query("select id,name from accounts where deleted='0' order by name asc");
			while($r=$GLOBALS[db]->fetchByAssoc($w)){
				echo '<option value="'.$r['id'].'"';
				if($account==$r['id'])echo ' selected';
				echo '>'.$r['name'].'</option>';
			}
			?>
			</select>
	  	</td>
		<td class="dataLabel" width="10%" nowrap="nowrap">
			Document type	</td>
		<td class="dataField" width="30%" nowrap="nowrap">
			<select name="type">
			<option value="" <? if($type=="")echo 'selected';?>>normal and correct</option>
			<option value="normal" <? if($type=="normal")echo 'selected';?>>normal</option>
			<option value="correct" <? if($type=="correct")echo 'selected';?>>correct</option>
			</select>
	  	</td>
	 </tr>
</tbody>
</table>

<input class="button" name="submit" value="Search" type="submit">
<input class="button" name="clear" value="Clear" type="button" onclick="location.href='index.php?module=EcmReports&action=ListDailySales';"><?
echo '&nbsp;<input type="button" class="button" name="CreateXLS" value="Create XLS" onclick="location.href=\'index.php?module=EcmInvoiceOutOlds&action=CreateXLS&date_from='.$date_from.'&date_to='.$date_to.'&account='.$account.'&type='.$type.'\';">';
?>
</form><br />
<?php

$tds1='<td class="listViewThS1">';

$trs='<tr>';
$tre='</tr>';

$tds='<td class="oddListRowS1">';
$tde='</td>';

$tbs='<table cellpadding="0" cellspacing="0" border="0" class="ListView" style="width:100%;">';
$tbe='</table>';

$t.=$tbs;
$t.=$trs;
	$t.=$tds1;
		$t.="Invoice No";
	$t.=$tde;
	$t.=$tds1;
		$t.="Type";
	$t.=$tde;
	$t.=$tds1;
		$t.="Account";
	$t.=$tde;
	$t.=$tds1;
		$t.="Register Date";
	$t.=$tde;
	$t.=$tds1;
		$t.="Total Brutto";
	$t.=$tde;
	$t.=$tds1;
		$t.="Total Netto";
	$t.=$tde;
	$t.=$tds1;
		$t.="Cost";
	$t.=$tde;
	$t.=$tds1;
		$t.="PLN Margin";
	$t.=$tde;
$t.=$tre;
$i=1;
$wh[]="deleted='0'";
if($type)$wh[]="type='".$type."'";
if($account)$wh[]="parent_id='".$account."'";
if($date_from)$wh[]="register_date>='".$date_from."'";
if($date_to)$wh[]="register_date<='".$date_to."'";
$where=implode(" and ",$wh);
$z="select document_no,register_date,id,parent_id,parent_name,type,ecminvoiceoutold_id,currency_value from ecminvoiceoutolds where ".$where." order by type desc, register_date asc, name asc";
$w=$GLOBALS[db]->query($z);
echo mysql_error();
while($r=$GLOBALS[db]->fetchByAssoc($w)){
	$total_netto=0;
	$total_pur=0;
	$total_brutto=0;
	$total_margin=0;
	if(!$r['currency_value'])$currency_value=1;
	else $currency_value=$r['currency_value'];
	
	$ww=$GLOBALS[db]->query("select price,ecmvat_value,quantity,purchase_price,ecmproduct_id,ecminvoiceoutolditem_id from ecminvoiceoutolditems where ecminvoiceoutold_id='".$r['id']."' and deleted='0'");
	while($rr=$GLOBALS[db]->fetchByAssoc($ww)){
		if($r['type']!="correct"){
			$pprice=$rr['purchase_price'];
			$total_netto+=$currency_value*$rr['price']*$rr['quantity'];
			$total_pur+=$pprice*$rr['quantity'];
			$total_brutto+=$currency_value*$rr['price']*$rr['quantity']*(1+$rr['ecmvat_value']/100);
			$total_margin+=($currency_value*$rr['price']-$pprice)*$rr['quantity'];
		}
		else{
			$rrrr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select price,quantity,purchase_price from ecminvoiceoutolditems where id='".$rr['ecminvoiceoutolditem_id']."'"));
			$pprice=$rrrr['purchase_price'];
			$total_netto+=$currency_value*$rr['price']*$rr['quantity']-$currency_value*$rrrr['price']*$rrrr['quantity'];
			$total_pur+=$pprice*($rr['quantity']-$rrrr['quantity']);
			$total_brutto+=($currency_value*$rr['price']*$rr['quantity']-$currency_value*$rrrr['price']*$rrrr['quantity'])*(1+$rr['ecmvat_value']/100);
			$total_margin+=($currency_value*$rr['price']-$pprice)*$rr['quantity']-($currency_value*$rrrr['price']-$pprice)*$rrrr['quantity'];
		
		}
	}
	/*
	if($r['type']=="correct"){
		//echo $total_margin.'<br>';
		$ww=$GLOBALS[db]->query("select price,ecmvat_value,quantity,purchase_price,ecmproduct_id from ecminvoiceoutolditems where ecminvoiceoutold_id='".$r['ecminvoiceoutold_id']."' and deleted='0'");
		while($rr=$GLOBALS[db]->fetchByAssoc($ww)){
			if(mysql_num_rows($GLOBALS[db]->query("select id from ecmproducts where product_active='1' and id='".$rr['ecmproduct_id']."'"))==0)continue;
			
			$total_netto-=$rr['price']*$rr['quantity'];
			$total_pur-=$rr['purchase_price']*$rr['quantity'];
			//echo $total_margin.'<br>';
			$total_brutto-=$rr['price']*$rr['quantity']*(1+$rr['ecmvat_value']/100);
			$total_margin-=($rr['price']-$rr['purchase_price'])*$rr['quantity'];
		}
	}*/
	if($total_netto-$total_margin>0)$margin=100*$total_margin/($total_netto-$total_margin);
	else $margin=0;
	$t.=$trs;
		$t.=$tds;
			$t.='<a href="index.php?module=EcmInvoiceOutOlds&action=DetailView&record='.$r['id'].'">'.$r['document_no'].'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.=$r['type'];
		$t.=$tde;
		$t.=$tds;
			$t.='<a href="index.php?module=Accounts&action=DetailView&record='.$r['parent_id'].'">'.$r['parent_name'].'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.=$GLOBALS['timedate']->to_display_date($r['register_date']);
		$t.=$tde;
		$t.=$tds;
			$t.=number_format($total_brutto,2,",",".");
		$t.=$tde;
		$t.=$tds;
			$t.=number_format($total_netto,2,",",".");
		$t.=$tde;
		$t.=$tds;
			$t.=number_format(($total_netto-$total_margin),2,",",".");
		$t.=$tde;
		$t.=$tds;
			$t.=number_format($total_margin,2,",",".")." (".number_format($margin,2,",",".")."%)";
		$t.=$tde;
	$t.=$tre;
	
	$sum_total_netto+=$total_netto;
	$sum_total_pur+=$total_pur;
	$sum_total_brutto+=$total_brutto;
	$sum_total_margin+=$total_margin;
	
	$i++;
}
$t.=$trs;
	if($sum_total_netto-$sum_total_margin>0)$sum_margin=100*$sum_total_margin/($sum_total_netto-$sum_total_margin);
	else $sum_margin=0;
	$t.=$tds1;
		$t.="&nbsp;";
	$t.=$tde;
	$t.=$tds1;
		$t.="&nbsp;";
	$t.=$tde;
	$t.=$tds1;
		$t.="&nbsp;";
	$t.=$tde;
	$t.=$tds1;
		$t.="&nbsp;";
	$t.=$tde;
	$t.=$tds1;
		$t.=number_format($sum_total_brutto,2,",",".");
	$t.=$tde;
	$t.=$tds1;
		$t.=number_format($sum_total_netto,2,",",".");
	$t.=$tde;
	$t.=$tds1;
		$t.=number_format(($sum_total_netto-$sum_total_margin),2,",",".");
	$t.=$tde;
	$t.=$tds1;
		$t.=number_format($sum_total_margin,2,",",".")."(".number_format($sum_margin,2,",",".")."%)";
	$t.=$tde;
$t.=$tre;

$t.=$tbe;
echo $t;
?>