$db = $GLOBALS['db'];
$account=$_GET['account'];
$account_id=$_GET['account_id'];
$account_name=$_GET['account_name'];
$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");
?>
 | Sprzedaż |
';
$trs='';
$tre='
';
$tds='';
$tde=' | ';
$tbs='';
$i=1;
$wh[]="deleted='0'";
$wh[]="canceled='0'";
if($type)$wh[]="type='".$type."'";
if($account_id)$wh[]="parent_id='".$account_id."'";
if($account_name)$wh[]="parent_name like '".$account_name."%'";
if($date_from)$wh[]="register_date>='".$date_from."'";
if($date_to)$wh[]="register_date<='".$date_to."'";
$where=implode(" and ",$wh);
$z="select purchase_price,total, subtotal,document_no,register_date,id,parent_id,parent_name,type,ecminvoiceout_id,currency_value,discount from ecminvoiceouts where ".$where." order by type desc, register_date asc, name asc";
$w=$db->query($z);
$z2="select purchase_price,total, subtotal,document_no,register_date,id,parent_id,parent_name,type,ecminvoiceout_id,currency_value,discount from ecminvoiceouts where ".$where." group by parent_id";
//echo $z2;
$w2=$db->query($z2);
$i=0;
$ar=array();
while($r2=$db->fetchByAssoc($w2)){
$ar[$i]['parent_id']=$r2['parent_id'];
$ar[$i]['parent_name']=$r2['parent_name'];
$ar[$i]['total']=0;
$ar[$i]['subtotal']=0;
$w3=$db->query("select ac.assigned_user_id,us.first_name,us.last_name,us.id from accounts as ac
inner join users as us on us.id=ac.assigned_user_id where ac.id='".$r2['parent_id']."'");
$us=$db->fetchByAssoc($w3);
$ar[$i]['user']=$us['first_name'].' '.$us['last_name'];
$ar[$i]['userid']=$us['id'];
$i++;
}
//echo '
'.$w->num_rows.'
';
echo mysql_error();
$sum_margin = 0;
$count_normal = 0;
while($r=$db->fetchByAssoc($w)){
if(!$r['currency_value'])$currency_value=1;
else {
$currency_value=$r['currency_value'];
//echo $currency_value;
}
//$currency_value=1;
$total_netto=$r['subtotal']*$currency_value;
$total_pur=$r['purchase_price'];
$total_brutto=$r['total']*$currency_value;
$total_margin=0;
for($ac=0;$ac'.$r['subtotal'].'
'.$total_netto;
if($total_netto-$total_margin>0)$margin=100*$total_margin/($total_netto-$total_margin);
else $margin=0;
if($r['discount']>0){
$vr=$total_brutto/$total_netto;
$total_brutto=$total_brutto-$r['discount'];
$total_netto=$total_brutto/$vr;
}
$i++;
}
$pokaz=0;
$t.=$tbs;
$t.=$trs;
$t.=$tds1;
$t.="Kontrahent";
$t.=$tde;
$t.=$tds1;
$t.="Razem Brutto";
$t.=$tde;
$t.=$tds1;
$t.="Razem Netto";
$t.=$tde;
$t.=$tds1;
$t.="Koszt";
$t.=$tde;
$t.=$tds1;
$t.="PLN Margin";
$t.=$tde;
$t.=$tds1;
$t.="Użytkownik";
$t.=$tde;
$t.=$tre;
for($ac=0;$ac'.$ar[$ac]['parent_name'].'';
$t.=$tde;
$t.=$tds;
$t.=format_number($ar[$ac]['total']);
$t.=$tde;
$t.=$tds;
$t.=format_number($ar[$ac]['subtotal']);
$t.=$tde;
$t.=$tds;
$t.=format_number($total_pur);
$t.=$tde;
$t.=$tds;
$t.=format_number($total_margin)."%";
$t.=$tde;
$t.=$tds;
$t.=''.$ar[$ac]['user'].'';
$t.=$tde;
$t.=$tre;
}
$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.=format_number($pokaz);
$t.=$tde;
$t.=$tds1;
$t.=format_number($pokaz2);
$t.=$tde;
$t.=$tds1;
$t.=format_number($sum_total_pur);
$t.=$tde;
$t.=$tds1;
$t.=format_number($sum_margin/$count_normal)."%";
$t.=$tde;
$t.=$tre;
$t.=$tbe;
echo $t;
?>