$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");
?>
 | Eksport RW |
';
$trs='';
$tre='
';
$tds='';
$tde=' | ';
$tbs='';
$t.=$tbs;
$t.=$trs;
$t.=$tds1;
$t.="Nr faktury";
$t.=$tde;
$t.=$tds1;
$t.="Typ";
$t.=$tde;
$t.=$tds1;
$t.="Kontrahent";
$t.=$tde;
$t.=$tds1;
$t.="Data utworzenia";
$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.=$tre;
$i=1;
$wh[]="rw.deleted='0'";
if($type)$wh[]="type='".$type."'";
if($account_id)$wh[]="rw.parent_id='".$account_id."'";
if($account_name)$wh[]="rw.parent_name like '".$account_name."%'";
if($date_from)$wh[]="rw.register_date>='".$date_from."'";
if($date_to)$wh[]="rw.register_date<='".$date_to."'";
$where=implode(" and ",$wh);
$z="select rw.id,rw.document_no,rw.register_date,rw.name,rw.stock_id from ecmstockdocinsideouts as rw
where ".$where." order by rw.register_date asc";
echo $z;
$w=$db->query($z);
echo '
'.$w->num_rows.'
';
echo mysql_error();
$sum_margin = 0;
$count_normal = 0;
$microtime=str_replace(".","",str_replace(" ","",microtime()));
$name='rw_'.$microtime;
$fp = fopen('modules/EcmReports/TXT/'.$name.'.txt', 'w');
fwrite($fp, 'RW'."\n");
while($r=$db->fetchByAssoc($w)){
if(!$r['currency_value'])$currency_value=1;
else {
$currency_value=$r['currency_value'];
//echo $currency_value;
}
//$currency_value=1;
$nip=$db->query("select round(SUM(total),2) AS subtotal
from ecmstockdocinsideoutitems where ecmstockdocinsideout_id='".$r['id']."'");
$wal=$db->query("select name from currencies where id='".$r['currency_id']."'");
$nip=$db->fetchByAssoc($nip);
$waluta=$db->fetchByAssoc($wal);
if($db->getRowCount($wal)==0) $waluta['name']='PLN';
if($currency_value==1){
$vat=$r['total']-$r['subtotal'];
} else {$vat=0;}
if($r['stock_id']==''){
$stock='c7afd71a-4c3a-bde4-138d-4acaee1644e4';
} else {
$stock=$r['stock_id'];
} if($r['currency_value']=="")$r['currency_value']='0';
$total_pur=$r['purchase_price'];
if($total_pur=='')$total_pur='0';
fwrite($fp, $r['document_no'].' | '.$r['register_date'].' | '.$r['name'].' | '.$nip['subtotal'].' | '.$stock."\n");
$total_netto=$r['subtotal']*$currency_value;
$total_brutto=$r['total']*$currency_value;
$total_margin=0;
if ($r['document_no']=='001/E/2013') echo $currency_value.'
'.$r['subtotal'].'
'.$total_netto;
if($total_netto-$total_margin>0)$margin=100*$total_margin/($total_netto-$total_margin);
else $margin=0;
if ($r['type']=='normal') {
$total_margin = (($total_netto-$total_pur)/$total_netto)*100;
$count_normal++;
$sum_margin+=$total_margin;
}
if($r['discount']>0){
$vr=$total_brutto/$total_netto;
$total_brutto=$total_brutto-$r['discount'];
$total_netto=$total_brutto/$vr;
}
$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.=format_number($total_brutto);
$t.=$tde;
$t.=$tds;
$t.=format_number($total_netto);
$t.=$tde;
$t.=$tds;
$t.=format_number($total_pur);
$t.=$tde;
$t.=$tds;
$t.=format_number($total_margin)."%";
$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++;
}
fclose($fp);
$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.=format_number($sum_total_brutto);
$t.=$tde;
$t.=$tds1;
$t.=format_number($sum_total_netto);
$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;
if(isset($_REQUEST['txt'])){
// send the right headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('modules/EcmReports/TXT/'.$name.'.txt'));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize('modules/EcmReports/TXT/'.$name.'.txt'));
ob_clean();
flush();
readfile('modules/EcmReports/TXT/'.$name.'.txt');
exit;
// dump the picture and stop the script
}
?>