<?php

 // compare tables
 
		$db = $GLOBALS['db'];
		
		$query=$db->query("describe ecmnewinvoiceoutitems");
		$fields=array();
		while($r = $db->fetchByAssoc($query)){
		   $fields[]=$r;
		}
		var_dump($fields);
		
		$query2=$db->query("describe ecminvoiceoutitems");
		$fields2=array();
		while($r = $db->fetchByAssoc($query2)){
		    $fields2[]=$r;
		}

        echo "<table><tr><td>Nazwa pola1</td><td>Nazwa pola2</td>";
		for($i=0;$i<count($fields2);$i++){
		    echo "<tr><td>".$fields[$i]['Field']."</td><td>".$fields2[$i]['Field']."</td></tr>";
		}
		echo "</table>";
		
		// dodawanie itemkow
		
		$query2=$db->query("select * from ecminvoiceoutitems order by date_entered desc");
		$fields2=array();
		global $app_list_strings;
		while($r = $db->fetchByAssoc($query2)){
		    if($r['ecminvoiceoutitem_id']!=''){
		        $qq=$db->query("select total,subtotal from ecminvoiceoutitems where id='".$r['ecminvoiceoutitem_id']."'");
		        $cc = $db->fetchByAssoc($qq);
		        $r['old_total_vat']=$cc['total']-$cc['subtotal'];
		        $r['total_corrected']=$r['total']-$cc['total'];
		        $r['subtotal_corrected']=$r['subtotal']-$cc['subtotal'];
		        $r['quantity_corrected']=$r['quantity']-$cc['quantity'];
		    }
		    $total_vat=$r['total']-$r['subtotal'];
		    
		  $db->query("INSERT INTO ecmnewinvoiceoutitems VALUES(
		            '".$r['id']."'
		              ,'".$r['date_entered']."'
		,'".$r['date_modified']."'
		,'".$r['modified_user_id']."'
		,'".$r['assigned_user_id']."'
		,'".$r['created_by']."'
		,'".$r['deleted']."'
		,'".$r['ecminvoiceout_id']."'
		,'".$r['ecmproduct_id']."'
		,'".$r['position']."'
		,'".$r['code']."'
		,'".$r['name']."'
		,'".$r['quantity']."'
		,'".$r['startprice']."'
		,'".$r['subprice']."'
		,'".$r['price']."'
		,'".$r['discount']."'
		,'".$r['subtotal']."'
		,'".$r['total']."'
		,'".$r['dd_unit_id']."'
				,'".$r['dd_unit_name']."'
				,'".$r['ecmvat_id']."'
				,'".$r['ecmvat_name']."'
		,'".$r['ecmvat_value']."'
		
		,'".$r['ecmproductcategory_id']."'
		,'".$r['recipient_code']."'
		,'".$r['ecminvoiceoutitem_id']."'
		,'".$r['old_ecminvoiceoutitem_id']."'
		,'".$r['old_subtotal']."'
		,'".$r['old_quantity']."'
		,'".$r['subtotal_corrected']."'
		,'".$r['quantity_corrected']."'
		,'".$r['old_total_vat']."'
		,'".$total_vat."'
		,'".$r['old_total']."'
		,'".$r['total_corrected']."'
		,'".$r['purchase_price']."'
		,'".$r['parent_doc_id']."'
		,'".$r['parent_doc_type']."'
		,'".$r['parent_doc_item_id']."')");
				
		}
		
        
        $db = $GLOBALS['db'];
        $query=$db->query("describe ecmnewinvoiceouts");
        $fields=array();
        while($r = $db->fetchByAssoc($query)){
            $fields[]=$r;
        }
       // var_dump($fields);
        
        $query2=$db->query("describe ecminvoiceouts");
        $fields2=array();
        while($r = $db->fetchByAssoc($query2)){
            $fields2[]=$r;
        }
        
        echo "<table><tr><td>Nazwa pola1</td><td>Nazwa pola2</td>";
        for($i=0;$i<count($fields2);$i++){
            echo "<tr><td>".$fields[$i]['Field']."</td><td>".$fields2[$i]['Field']."</td></tr>";
        }
        echo "</table>";
        
        $query2=$db->query("select * from ecminvoiceouts");
        $fields2=array();
        $po = new EcmPurchaseOrder();
        
        echo "<pre>";
        while($r = $db->fetchByAssoc($query2)){
            $po->retrieve($r['id']);
           // echo $r['id'];
           $position_list=$po->getPositionList(true);
          $vat=$po->calculate($position_list);
        	    $vats_summary='';
	    $total_netto=0;
	    $total_brutto=0;
	    $total_vat=0;
	if($r['type']=='normal'){
	    $res= $GLOBALS ['db']->query ("select ROUND(sum(subtotal),2) AS total_netto,ROUND(sum((subtotal*ecmvat_value)/100),2) AS total_vat,
ROUND(sum(((subtotal*ecmvat_value)/100)+subtotal),2) AS total_brutto,ecmvat_value
from ecminvoiceoutitems where ecminvoiceout_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_netto'].':'.$cc['total_vat'].':'.$cc['total_brutto'].',';
	    }
	} else {
	    $res= $GLOBALS ['db']->query ("select ROUND(sum(total_netto_corrected),2) AS total_netto,ROUND(sum((total_netto_corrected*ecmvat_value)/100),2) AS total_vat,
ROUND(sum(((total_netto_corrected*ecmvat_value)/100)+total_netto_corrected),2) AS total_brutto,ecmvat_value
from ecmnewinvoiceoutitems where ecmnewinvoiceout_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_netto'].':'.$cc['total_vat'].':'.$cc['total_brutto'].',';
	    }
	}
         // var_dump($vat);
          
            if($r['end_date']=='')$r['end_date']=null;
            
         $GLOBALS ['db']->query ("INSERT INTO ecmnewinvoiceouts VALUES(
		'".$r['id']."'
		,'".$r['name']."'
		,'".$r['description']."'
		,'".$r['date_entered']."'
		,'".$r['date_modified']."'
		,'".$r['modified_user_id']."'
		,'".$r['assigned_user_id']."'
		,'".$r['created_by']."'
		,'".$r['deleted']."'
		,'".$r['number']."'
		,'".$r['document_no']."'
		,'".$r['parent_type']."'
		,'".$r['parent_name']."'
		,'".$r['parent_id']."'
		,'".$r['type']."'
		,'".$r['status']."'
		,'".$r['register_date']."'
		,'".$r['parent_address_street']."'
		,'".$r['parent_address_city']."'
		,'".$r['parent_address_postalcode']."'
		,'".$r['parent_address_country']."'
		,'".$total_netto."'
		,'".$total_brutto."'
		,'".$r['discount']."'
        ,'".$vats_summary."'
        ,''
        ,'null'
                ,'".$r['ecmlanguage']."'
                ,''
                ,'".$r['footer_text']."'
                ,'".$r['contact_id']."'
                  ,'".$r['ecmpaymentcondition_id']."'
                  ,'".$r['ecmpaymentcondition_text']."'
           
		,''
		,'".$r['currency_id']."'
		,'".$r['ecminvoiceout_id']."'
		,'".$r['ecminvoiceout_name']."'
		,'".$r['sell_date']."'
		,'".$r['stock_id']."'
		,'".$r['stock_name']."'
		,'".$r['order_no']."'
		,'".$r['supplier_code']."'
		,'".$r['currency_value_nbp']."'
		
		,'".$r['pdf_type']."'
		,'".$r['parent_shipping_address_name']."'
		,'".$r['parent_shipping_address_street']."'
		,'".$r['parent_shipping_address_city']."'
		,'".$r['parent_shipping_address_postalcode']."'
		,'".$r['parent_shipping_address_country']."'
		,'".$r['paid_val']."'
		,'".$r['prepaid']."'
		,'".$r['prepaid_nr']."'
		,'".$r['payment_method_paid']."'
		,'".$r['payment_date']."'
		,'".$r['currency_value']."'
		,'".$r['payment_date_d']."'
		,'".$r['payment_method']."'
		,'".$r['canceled']."'
		,'".$total_vat."'
		,'".$r['wz_id']."'
		,'".$r['wz_name']."'
		,'".$r['so_id']."'
        ,''
        ,''
        ,''
        ,''
        ,'')");
         if($r['canceled']=='0'){
          $GLOBALS ['db']->query ("update ecmnewinvoiceouts set canceled=null where id='".$r['id']."'");
         }
        }
        
?>