<?php

$db = $GLOBALS['db'];
$query2=$db->query("select * from ecmpurchaseorders");
$fields2=array();
echo $query2->num_rows.'tutaj';

 
echo "<pre>";
while($r = $db->fetchByAssoc($query2)){
    $vats_summary = '';
    $total_netto = 0;
    $total_brutto = 0;
    $total_vat = 0;

$res = $GLOBALS ['db']->query ( "select ROUND(sum(total_netto),2) AS total_netto,ROUND(sum((total_netto*ecmvat_value)/100),2) AS total_vat,
ROUND(sum(((total_netto*ecmvat_value)/100)+total_netto),2) AS total_brutto,ecmvat_value
from ecmpurchaseorderitems where ecmpurchaseorder_id='" . $r['id'] . "' group by ecmvat_id" );
while ( $cc = $GLOBALS ['db']->fetchByAssoc ( $res ) ) {
    $total_netto += $cc ['total_netto'];
    $total_brutto += $cc ['total_brutto'];
    $total_vat += $cc ['total_vat'];
    $vats_summary .= $cc ['ecmvat_value'] . '%:' . $cc ['total_vat'] . ',';
}
if($total_netto!=0){
$res = $GLOBALS ['db']->query ( "update ecmpurchaseorders set vats_summary='" . $vats_summary . "',total_netto='" . $total_netto . "',
	           total_brutto='" . $total_brutto . "' where id='" . $r['id'] . "'" );

}

}
/*
 * 
 */
 
?>