<?php

 // compare tables
 
		$db = $GLOBALS['db'];
		$query=$db->query("describe ecmpurchaseorderitems");
		$fields=array();
		while($r = $db->fetchByAssoc($query)){
		   $fields[]=$r;
		}
		var_dump($fields);
		
		$query2=$db->query("describe ecmproducts_ecmpurchaseorders");
		$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 ecmproducts_ecmpurchaseorders order by date_entered desc");
		$fields2=array();
		global $app_list_strings;
		while($r = $db->fetchByAssoc($query2)){
		    
		    $db->query("INSERT INTO ecmpurchaseorderitems VALUES(
		            '".$r['id']."'
		              ,'".$r['date_entered']."'
		,'".$r['date_modified']."'
		,'".$r['modified_user_id']."'
		,'".$r['assigned_user_id']."'
		,'".$r['created_by']."'
		,'".$r['deleted']."'
		,'".$r['ecmpurchaseorder_id']."'
		,'".$r['ecmproduct_id']."'
		,'".$r['product_position']."'
		,'".$r['product_code']."'
		,'".$r['product_name']."'
		,'".$r['product_quantity']."'
		,'".$r['product_price']."'
		,'".$r['product_total']."'
		,'".$r['product_unit_id']."'
		,'".$r['product_unit_name']."'
		,'".$r['product_category_id']."'
		,'".$r['product_cbm']."'
		,'".$r['product_moq']."'
				,'".$r['product_vat_id']."'
				,'".$r['product_vat_name']."'
				,'".$r['product_vat_value']."')");
				
		}
		
        
        $db = $GLOBALS['db'];
        $query=$db->query("describe ecmpurchaseorders");
        $fields=array();
        while($r = $db->fetchByAssoc($query)){
            $fields[]=$r;
        }
       // var_dump($fields);
        
        $query2=$db->query("describe ecmpurchaseorders");
        $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 ecmpurchaseorders");
        $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='';
          
              foreach($vat as $key=>$v){
                  if($key=='vats'){
                      foreach($v as $k2=>$v2){
                          $vv=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select name from ecmvats where id='".$k2."'"));
                          
                          $vats_summary.=$vv['name'].':'.$v2.',';
                      }
                  }
                  if($key=='total'){
                      $r['total']=$v;
                  }
                  if($key=='subtotal'){
                      $r['subtotal']=$v;
                  }
              }
          
         // var_dump($vat);
            
            if($r['end_date']=='')$r['end_date']=null;
            
           $db->query("INSERT INTO ecmpurchaseorders VALUES(
		'".$r['id']."'
		,'".$r['name']."'
		,'".$r['description']."'
		,'".$r['date_entered']."'
		,'".$r['modified_user_id']."'
		,'".$r['date_modified']."'
		,'".$r['assigned_user_id']."'
		,'".$r['created_by']."'
		,'".$r['deleted']."'
		,'".$r['number']."'
		,'".$r['document_no']."'
		,'".$r['register_date']."'
		,'".$r['subtotal']."'
		,'".$r['ads_text']."'
		,'".$r['total']."'
		,'".$r['parent_name']."'
		,'".$r['parent_address_street']."'
		,'".$r['parent_address_city']."'
		,'".$r['parent_address_postalcode']."'
		,'".$r['parent_address_country']."'
		,'".$r['parent_name']."'
		,'".$r['parent_id']."'
		,'".$r['end_date']."'
		,'".$r['employers']."'
        ,'".$r['currency_id']."'
        ,'".$r['to_is_vat_free']."'
        ,'null'
                ,'".$r['contact_id']."'
                ,'".$r['delivery_date']."'
                ,'".$r['parent_contact_name']."'
                ,'".$r['parent_contact_title']."'
                ,'".$vats_summary."'
                  ,'".$r['status']."')");
      
          
        }
        
?>