<?php
mysql_connect('localhost','root','5z#JaL');
mysql_select_db('crm');
 // compare tables
 
		$db = $GLOBALS['db'];
		$query=$db->query("describe ecmsaleitems");
		$fields=array();
		while($r = $db->fetchByAssoc($query)){
		   $fields[]=$r;
		}
		var_dump($fields);
		
		$query2=$db->query("describe ecmsaleitems");
		$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 items.*,doc.currency_id,doc.currency_value from ecmsaleitems as items 
join ecmsales as doc on doc.id=items.ecmsale_id");
		$fields2=array();
		global $app_list_strings;
		while($r = $db->fetchByAssoc($query2)){
		    if($r['currency_id']!='PLN'){
		    	$r['price_brutto']=$r['price'];
		    	$r['total_brutto']=$r['total'];
		    } else {
		    	$r['price_brutto']=round($r['price']+($r['price']*($r['ecmvat_value']/100)),2);
		    	$r['total_brutto']=round($r['total']+($r['total']*($r['ecmvat_value']/100)),2);
		    }
		   $db->query("INSERT INTO ecmsaleitems VALUES(
		            '".$r['id']."'
		              ,'".$r['date_entered']."'
		,'".$r['date_modified']."'
		,'".$r['modified_user_id']."'
		,'".$r['assigned_user_id']."'
		,'".$r['created_by']."'
		,'".$r['deleted']."'
		,'".$r['ecmsale_id']."'
		,'".$r['ecmproduct_id']."'
		,'".$r['position']."'
		,'".$r['code']."'
		,'".$r['name']."'
		,'".$r['quantity']."'
		,'".$r['price']."'
		,'".$r['price']."'
		,'".$r['price_brutto']."'
		,'".$r['discount']."'
		,'".$r['total']."'
		,'".$r['total_brutto']."'
		,'".$r['dd_unit_id']."'
				,'".$r['dd_unit_name']."'
				,'".$r['ecmvat_id']."'
				,'".$r['ecmvat_name']."'
		    		,'".$r['ecmvat_value']."'
		    		,'".$r['ecmproductcategory_id']."'
		    		,'".$r['recipient_code']."')");
				
		}
		
        
        $db = $GLOBALS['db'];
        $query=$db->query("describe ecmsales");
        $fields=array();
        while($r = $db->fetchByAssoc($query)){
            $fields[]=$r;
        }
       // var_dump($fields);
        
        $query2=$db->query("describe ecmsales");
        $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 ecmsales");
        $fields2=array();
        $po = new EcmSale();
        
        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,value from ecmvats where id='".$k2."'"));
              		echo $k2;
                          $vats_summary.=$vv['name'].':'.$v2.',';
                      }
                  }
                  if($key=='total'){
                  
                      $r['total']=$v;
                    
                  }
                  if($key=='subtotal'){
                      $r['subtotal']=$v;
                  }
              }
              if($r['currency_id']!='PLN'){
              	$r['total']=$r['subtotal'];
              }
         // var_dump($vat);
            
            if($r['end_date']=='')$r['end_date']=null;
            
          $db->query("INSERT INTO ecmsales 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['validtill_date']."'

		,'".$r['parent_address_street']."'
		,'".$r['parent_address_city']."'
		,'".$r['parent_address_postalcode']."'
		,'".$r['parent_address_country']."'
		,'".$r['subtotal']."'
		,'".$r['total']."'
		,'".$r['discount']."'
		,'".$vats_summary."'
        ,'".$r['to_vatid']."'
        ,'".$r['to_vatid']."'
        ,'".$r['ecmlanguage']."'
        		,'".$r['to_is_vat_free']."'
        				,'".$r['footer_text']."'
                ,'".$r['contact_id']."'
                ,'".$r['ecmpaymentcondition_id']."'
                ,'".$r['ecmpaymentcondition_text']."'
                ,'".$r['ecmdeliverycondition_id']."'
                ,'".$r['ecmdeliverycondition_text']."'
                  ,'".$r['ecmpricebook_id']."'
                  ,'".$r['currency_id']."'
                  ,'".$r['parent_document_no']."'
                  ,'".$r['delivery_date']."'
                  ,''
                  ,'".$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']."'
                  ,'')");
      
          
        }
        
?>