<?php
function xml($product_id,$langg="pl",$import_srp_price=0){
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmproducts where id='".$product_id."'"));
$rrr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmproductcategories where id='".$r['product_category_id']."'"));
$rrrs=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmproductcategories where id='".$r['product_subcategory_id']."'"));
$ww=$GLOBALS['db']->query("select * from ecmproduct_language where language='".$langg."' and ecmproduct_id='".$product_id."'");
while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
	$ean=$rr['ean'];
	$lang[$rr['language']]['ean']=$rr['ean'];
	$lang[$rr['language']]['remarks']=$rr['remarks'];
	$lang[$rr['language']]['product_name']='<![CDATA['.$rr['short_description'].']]>';
	$lang[$rr['language']]['product_s_desc']='<![CDATA['.$rr['short_description'].']]>';
	$lang[$rr['language']]['product_desc']='<![CDATA['.$rr['long_description'].']]>';
	if($import_srp_price){
		$rr['price']=$r['srp_price'];
	}
	else{
		$rpr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id from ecmpricebooks where name like 'leobite".$langg."%'"));
		$rlang=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select price from ecmpricebooks_ecmproducts where ecmpricebook_id='".$rpr['id']."' and ecmproduct_id='".$product_id."'"));
		$rr['price']=$rlang['price'];
	}
	$lang[$rr['language']]['product_price']=$rr['price'];
	//$lang[$rr['language']]['category']=$rrr['name_'.$rr['language']];
}
if($r['product_picture']){
$images[$i]['id']=$product_id."_1";
$images[$i]['content']=base64_encode(file_get_contents("modules/EcmProducts/upload/images/big/".$r['product_picture']));
$exp=explode(".",$r['product_picture']);
$images[$i]['filename']=$r['product_picture'];
if($exp[count($exp)-1]=="jpeg")$exp[count($exp)-1]="jpg";
$images[$i]['extension']=$exp[count($exp)-1];
$i++;
}
$ww=$GLOBALS['db']->query("select * from notes where parent_type='EcmProducts' and deleted='0' and parent_id='".$product_id."'");
while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
	if(eregi(" kar ",$rr['name']))continue;
	if($rr['file_mime_type']=="image/jpeg" || $rr['file_mime_type']=="image/jpg" || $rr['file_mime_type']=="image/png" || $rr['file_mime_type']=="image/gif"){
		$images[$i]['id']=$rr['id'];
		$images[$i]['content']=base64_encode(file_get_contents("cache/upload/".$rr['id']));
		$images[$i]['filename']=$rr['filename'];
		$exp=explode("/",$rr['file_mime_type']);
		if($exp[1]=="jpeg")$exp[1]="jpg";
		$images[$i]['extension']=$exp[1];
		$i++;
		
	}
}
if($r['packing_front_picture']){
$images[$i]['id']=$product_id."_2";
$images[$i]['content']=base64_encode(file_get_contents("modules/EcmProducts/upload/images/big/".$r['packing_front_picture']));
$exp=explode(".",$r['packing_front_picture']);
$images[$i]['filename']=$r['packing_front_picture'];
if($exp[count($exp)-1]=="jpeg")$exp[count($exp)-1]="jpg";
$images[$i]['extension']=$exp[count($exp)-1];
}

$rmuid=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select first_name,last_name,user_name from users where id='".$r['modified_user_id']."'"));
$rbean=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select email_id from emails_beans where bean_id='".$r['modified_user_id']."' and bean_module='Users' and deleted='0'"));
$remail=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select email_address from email_addresses where id='".$rbean['email_id']."'"));
$modified=$rmuid['user_name'];
if($remail['email_address'])$modified=', '.$remail['email_address'];
$xml.='<?xml version="1.0" encoding="UTF-8"?>
<products>
 <product>
  <modified_user_info>'.$modified.'</modified_user_info>
  <vendor_id>'.$r['vendor_id'].'</vendor_id>
  <product_parent_id>0</product_parent_id>
  <product_sku>'.$r['code'].'</product_sku>
  <product_name><![CDATA['.$r['name'].']]></product_name>
  <product_price>'.$r['selling_price'].'</product_price>
  <product_desc><![CDATA['.$r['l_desc'].']]></product_desc>
  <product_s_desc>'.$ean.'</product_s_desc>
  <product_thumb_image>'.$product_id.'_1</product_thumb_image>
  <product_full_image>'.$product_id.'_1</product_full_image>
  <product_publish>N</product_publish>
  <product_weight>'.$r['product_brutto_weight'].'</product_weight>
  <product_weight_uom>kg</product_weight_uom>
  <product_length>0.0000</product_length>
  <product_width>0.0000</product_width>
  <product_height>0.0000</product_height>
  <product_lwh_uom></product_lwh_uom>
  <product_unit>'.$r['unit_id'].'</product_unit>
  <product_packaging></product_packaging>
  <product_url></product_url>
  <product_in_stock>'.$r['ems_qty_in_stock'].'</product_in_stock>
  <attribute></attribute>
  <product_custom_attribute></product_custom_attribute>
  <product_available_date></product_available_date>
  <product_availability></product_availability>
  <product_special>N</product_special>
  <child_options></child_options>
  <quantity_options></quantity_options>
  <product_discount_id>0</product_discount_id>
  <cdate>'.$r['date_entered'].'</cdate>
  <mdate>'.$r['date_modified'].'</mdate>
  <product_tax_id>23%</product_tax_id>
  <product_tax_value>23</product_tax_value>
  <included_product_id></included_product_id>
  <order_levels></order_levels>
  <product_category><![CDATA['.$r['product_category_name'].']]></product_category>
  <nowosci>0</nowosci>
  <polecamy>0</polecamy>
  <categories>
   <category>
   	 <id><![CDATA['.$r['product_category_id'].']]></id>
        <name><![CDATA['.$r['product_category_name'].']]></name>
	 <name_en><![CDATA['.$rrr['name_en'].']]></name_en>
	 <name_de><![CDATA['.$rrr['name_de'].']]></name_de>
   </category>
  </categories>';
if($r['product_subcategory_id']){
$xml.='<subcategories>
   <subcategory>
   	 <id><![CDATA['.$r['product_subcategory_id'].']]></id>
        <name><![CDATA['.$r['product_subcategory_name'].']]></name>
	 <name_en><![CDATA['.$rrrs['name_en'].']]></name_en>
	 <name_de><![CDATA['.$rrrs['name_de'].']]></name_de>
   </subcategory>
  </subcategories>';
}
$xml.='<images>';
  foreach($images as $img){
   $xml.='<image>
    <id>'.$img['id'].'</id>
    <data>'.$img['content'].'</data>
    <filename>'.$img['filename'].'</filename>
    <extension>'.$img['extension'].'</extension>
   </image>';
  }
  $xml.='</images>
  <language>';
  foreach($lang as $k=>$v){
  	$xml.='<'.$k.'>';
	foreach($v as $kk=>$vv){
		$xml.='<'.$kk.'>'.$vv.'</'.$kk.'>';
	}
	$xml.='</'.$k.'>';
  }
  $xml.='</language>
 </product>
</products>';
  return $xml;
}
?>