$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");
?>
 | Invoices: Daily sales |
';
$trs='';
$tre='
';
$tds='';
$tde=' | ';
$tbs='';
$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,ecmreceipt_id,currency_value from ecmreceipts 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,ecmreceiptitem_id from ecmreceiptitems where ecmreceipt_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 ecmreceiptitems where id='".$rr['ecmreceiptitem_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.'
';
$ww=$GLOBALS[db]->query("select price,ecmvat_value,quantity,purchase_price,ecmproduct_id from ecmreceiptitems where ecmreceipt_id='".$r['ecmreceipt_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.'
';
$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.=''.$r['document_no'].'';
$t.=$tde;
$t.=$tds;
$t.=$r['type'];
$t.=$tde;
$t.=$tds;
$t.=''.$r['parent_name'].'';
$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.=" ";
$t.=$tde;
$t.=$tds1;
$t.=" ";
$t.=$tde;
$t.=$tds1;
$t.=" ";
$t.=$tde;
$t.=$tds1;
$t.=" ";
$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;
?>