<?php

 // compare tables
 echo "<pre>";
		$db = $GLOBALS['db'];
		global $app_list_strings;
		$query=$db->query("describe ecmstockdocoutitems");
		$fields=array();
		while($r = $db->fetchByAssoc($query)){
		   $fields[]=$r;
		}
		var_dump($fields);
		
		$query2=$db->query("describe ecmstockdocoutitems");
		$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($fields);$i++){
		    echo "<tr><td>".$fields[$i]['Field']."</td><td>".$fields2[$i]['Field']."</td></tr>";
		}
		echo "</table>";
		
		// dodawanie itemkow
		
	
		
		/*
        
        $db = $GLOBALS['db'];
        $query=$db->query("describe ecmstockdocouts");
        $fields=array();
        while($r = $db->fetchByAssoc($query)){
            $fields[]=$r;
        }
       // var_dump($fields);
        
        $query2=$db->query("describe ecmstockdocouts");
        $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>";
        */
        $l=$db->query("select * from ecmstockdocouts order by date_entered desc");
        $l2=$db->query("describe ecmstockdocouts");
        $pola2=array();
        $rekord;
        while($daj2=$db->fetchByAssoc($l2)){
            $pola2[]=$daj2['Field'];
        }
        
        while($daj=$db->fetchByAssoc($l)){ 
            $rekord=$daj['id'];
            $pola=array();
            $wartosci=array();
            foreach ($daj as $pole=>$wartosc){
                if($pole=='so_id'){
                    $pole='ecmsale_id';
                }
                if(in_array($pole,$pola2)==false)continue;
        
                $pola[]=$pole;
                $wartosci[]=$wartosc;
            }
            if($daj['so_id']!=''){
                $sh=$db->query("select 	shipping_address_name,
	shipping_address_street,
	shipping_address_city,
	shipping_address_postalcode,
	shipping_address_country,
    shipping_nip,
    delivery_date,
    document_no as ecmsale_document_no,                    
	shipping_iln from ecmsales where id='".$daj['so_id']."'");
                while($sh2=$db->fetchByAssoc($sh)){
                    foreach ($sh2 as $pole=>$wartosc){
        
                $pola[]=$pole;
                $wartosci[]=$wartosc;
            }
                }
            }
            
            $pola[]='parent_nip';
            $wartosci[]=$daj['to_vatid'];
            
            //itemki start
            
            $zap=$db->query("select * from ecmstockdocoutitems where ecmstockdocout_id='".$daj['id']."'");
            $l22=$db->query("describe ecmstockdocoutitems");
            $pos=0;
            $pola22=array();
            while($daj22=$db->fetchByAssoc($l22)){
                $pola22[]=$daj22['Field'];
            }
            
            while($item=$db->fetchByAssoc($zap)){
                $pola12=array();
                $wartosci12=array();
                $s2h=$db->query("select unit_id,ean,ean2 from ecmproducts where id='".$item['ecmproduct_id']."'");
                $s2h2=$db->fetchByAssoc($s2h);
                foreach ($item as $field=>$val){
                    if($field=='ecmstockdocout_id')$field='ecmstockdocout_id';
                    if($field=='selling_price'){
                        $field='price_sell';
                    }
                    if($field=='total'){
                        $field='total_sell';
                    }

                    if($field=='position'){
                        $val=$pos;
                    }
                    if($field=='recipient_code'){
                        $field='supplier_code';
                    }
                    if(in_array($field,$pola22)==false)continue;
                    if($field=='dd_unit_id'){
                        $val=$s2h2['unit_id'];
                    }
                    if($field=='dd_unit_name'){
                        $val=$app_list_strings['ecmproducts_unit_dom'][$s2h2['unit_id']];
                    }
                    $pola12[]=$field;
                    $wartosci12[]=$val;
            
                }
                $pola12[]='ean';
                $wartosci12[]=$s2h2['ean'];
                $pola12[]='total';
                $wartosci12[]=$item['price']*$item['quantity'];
                $pola12[]='ean2';
                $wartosci12[]=$s2h2['ean2'];
                $pos++;
                $db->query("INSERT INTO `ecmstockdocoutitems` (".implode(",",$pola12).") VALUES ('".implode("','",$wartosci12)."')");
                 
            }
            
            //itemki end
            
        $db->query("INSERT INTO `ecmstockdocouts` (".implode(",",$pola).") VALUES ('".implode("','",$wartosci)."')");
        $r = $db->fetchByAssoc($db->query("SELECT sum(total) as sum FROM ecmstockdocoutitems WHERE ecmstockdocout_id='".$rekord."' and deleted='0'"));
        $db->query("UPDATE ecmstockdocouts SET total='".$r['sum']."' WHERE id='".$rekord."'");
        $r = $db->fetchByAssoc($db->query("SELECT sum(total_sell) as sum FROM ecmstockdocoutitems WHERE ecmstockdocout_id='".$rekord."' and deleted='0'"));
        $db->query("UPDATE ecmstockdocouts SET total_sell='".$r['sum']."' WHERE id='".$rekord."'");
             
        }
        /*
        $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 ecmnewpurchaseorders 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']."')");
      
          
        }
        
?>