<?php

set_time_limit(0);

$bme_prod_flds = Array(
	"SUPPLIER_AID",
	"article_mode",
	"EAN",
	"DESCRIPTION_SHORT",
	"DESCRIPTION_LONG",
	"REMARKS",
	"MANUFACTURER_AID",
	"MANUFACTURER_NAME",
	"PRICE_AMOUNT",
	//"PRICE_AMOUNT2",
	//"PRICE_AMOUNT3",
	"TAX",
	"DELIVERY_TIME",
	"ORDER_UNIT",
	"CONTENT_UNIT",
	"NO_CU_PER_OU",
	"QUANTITY_MIN",
	"QUANTITY_INTERVAL",
	"KEYWORD",
	//"REMARKS",
	"product_group",
	"image1",
	"image_type1",
	"image_desc1",
	"image2",
	"image_type2",
	"image_desc2",
	"image3",
	"image_type3",
	"image_desc3",
	"image4",
	"image_type4",
	"image_desc4",
	"image5",
	"image_type5",
	"image_desc5",
	"image6",
	"image_type6",
	"image_desc6",
	"image7",
	"image_type7",
	"image_desc7",
	"image8",
	"image_type8",
	"image_desc8",
	"eclassversion",
	"eclass",
	"PRICE_QUANTITY",
	"price_type",
	"BUYER_AID",
	"SPECIAL_TREATMENT_CLASS",
	"buyer_aid_type",
	"ERP_GROUP_BUYER",
	"valid_start_date",
	"valid_end_date",
	"ARTICLE_ORDER",
	"ARTICLE_REFERENCE_SIMILA",
	"ARTICLE_REFERENCE_ACCESS",
	"ARTICLE_REFERENCE_OTHERS",
	"ARTICLE_REFERENCE_CONSISTSOF",
	"ARTICLE_REFERENCE_DIFFORDERUNIT",
	"ARTICLE_REFERENCE_FOLLOWUP",
	"ARTICLE_REFERENCE_MANDATORY",
	"ARTICLE_REFERENCE_SELECT",
	"ARTICLE_REFERENCE_SPAREPART",
	"UDX.NEWTRON.PARTNER.CATEGORY",
	"UDX.NEWTRON.VIEW.CATEGORY",
	"add1",
);
$bme_general_flds = array(
	"#LANGUAGE",
	"#CATALOG_ID",
	"#CATALOG_VERSION",
	"CATALOG_NAME",
	"GENERATION_DATE",
	"CURRENCY",
	"TERRITORIES",
	"PRICE_FLAGS",
	"MIME_ROOT",
	"PRICE_TYPE",
	"#AGREEMENT_ID",
	"AGREEMENT_START_DATE",
	"#AGREEMENT_END_DATE",
	"#TRANSACTION",
	"prev_version",
	"GROUP_SYSTEM_ID",
	"GROUP_SYSTEM_NAME",
	"encoding",
);

$bme_buyer_flds = array(
	"BUYER_ID",
	"BUYER_ID_TYPE",
	"#BUYER_NAME",
	"NAME",
	"NAME2",
	"NAME3",
	"CONTACT",
	"STREET",
	"ZIP",
	"BOXNO",
	"ZIPBOX",
	"CITY",
	"STATE",
	"COUNTRY",
	"PHONE",
	"FAX",
	"EMAIL",
	"URL",
	"ADDRESS_REMARKS",
);
$bme_supplier_flds = array(
	"SUPPLIER_ID",
	"SUPPLIER_ID_TYPE",
	"#SUPPLIER_NAME",
	"NAME",
	"NAME2",
	"NAME3",
	"CONTACT",
	"STREET",
	"ZIP",
	"BOXNO",
	"ZIPBOX",
	"CITY",
	"STATE",
	"COUNTRY",
	"PHONE",
	"FAX",
	"EMAIL",
	"URL",
	"ADDRESS_REMARKS",
	"LOGO",
);
$bme_address_flds = array(
	"#address_type",
	"NAME",
	"NAME2",
	"NAME3",
	"CONTACT",
	"STREET",
	"ZIP",
	"BOXNO",
	"ZIPBOX",
	"CITY",
	"STATE",
	"COUNTRY",
	"PHONE",
	"FAX",
	"EMAIL",
	"URL",
	"ADDRESS_REMARKS",
);
$bme_groupsys_flds = array(
	"#GROUP_ID",
	"#GROUP_NAME",
	"GROUP_DESCRIPTION",
	"#PARENT_ID",
	"CATALOG_STRUCTURE",
    "MIME_INFO",
    "MIME_PURPOSE",
	"GROUP_ORDER",
);

$header_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_header.txt")));
$buyer_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_buyer.txt")));
$supplier_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_supplier.txt")));
$catalog_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_catalog.txt")));
$agreement_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_agreement.txt")));
$address_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_address.txt")));
$catgroupsys_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_catgroupsys.txt")));
$catstructure_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_catstructure.txt")));
$prod_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_product.txt")));
$mimeinfo_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_product_mimeinfo.txt")));
$mime_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_product_mime.txt")));
$art2group_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_article_group.txt")));
$features_tpl = str_replace("\r\n","\n",implode("",file("../templates/tpl_features.txt")));

if( file_exists( "./cb_validate.php" ) ) { require_once "./cb_validate.php"; }

$is_bmecat2005 = $argv[ 1 ] == "2005";

if( $is_bmecat2005 ) {
	$bme_file = "../export/export_bmecat_2005.xml";
} else {
	$bme_file = "../export/export_bmecat_1_2.xml";
}
$err_file = "../tmp/error.log";
$done_file = "../tmp/done.flag";

$fp = fopen($bme_file,"w");

include("functions.php");
include("makeHeader.php");

$general_info = parseGeneralInfo("../tmp/general_conv.txt");

$encoding = strtoupper($general_info["encoding"]);
if($encoding=="ISO-8859-1")
{
	$_utf8_encode = false;
}else
{
	$_utf8_encode = true;
}

fputs($fp,"<?xml version=\"1.0\" encoding=\"" . ($_utf8_encode?"UTF-8":"ISO-8859-1") . "\"?>\n");
if( $is_bmecat2005 ) {
$dt =<<< EOF
<!DOCTYPE BMECAT SYSTEM "bmecat_2005_tnc.dtd"
[
<!--<!ENTITY % USERDEFINES "(UDX.NEWTRON.PARTNER.CATEGORY*,UDX.NEWTRON.VIEW.CATEGORY*)">-->
<!ELEMENT USER_DEFINED_EXTENSIONS (UDX.NEWTRON.PARTNER.CATEGORY*,UDX.NEWTRON.VIEW.CATEGORY*)>
<!ELEMENT UDX.NEWTRON.PARTNER.CATEGORY (#PCDATA)>
<!ELEMENT UDX.NEWTRON.VIEW.CATEGORY (#PCDATA)>
]>

EOF;
} else {
$dt =<<< EOF
<!DOCTYPE BMECAT SYSTEM "bmecat_new_catalog_1_2.dtd"
[
<!ENTITY % USERDEFINES "(UDX.NEWTRON.PARTNER.CATEGORY*,UDX.NEWTRON.VIEW.CATEGORY*)">
<!ELEMENT UDX.NEWTRON.PARTNER.CATEGORY (#PCDATA)>
<!ELEMENT UDX.NEWTRON.VIEW.CATEGORY (#PCDATA)>
]>

EOF;
}

fputs($fp,$dt);
//fputs($fp,"<!DOCTYPE BMECAT SYSTEM \"bmecat_new_catalog_1_2.dtd\">\n");
//fputs($fp,"<BMECAT version=\"1.2\" xml:lang=\"de\" xmlns=\"http://www.bmecat.org/bmecat/1.2/bmecat_new_catalog\">\n");
if( $is_bmecat2005 ) {
	fputs($fp,"<BMECAT version=\"2005\" xmlns=\"http://www.bmecat.org/bmecat/2005\">\n" );
	//fputs($fp,"<BMECAT version=\"2005\">\n" );
} else {
	fputs($fp,"<BMECAT version=\"1.2\">\n");
}

@unlink($err_file);
@unlink($done_file);

$aErrors = Array();
$aAIDs = Array();

$buyer = parseBuyer("../tmp/buy_conv.txt");
$supplier = parseSupplier("../tmp/sup_conv.txt");
makeHeader();

if($general_info["TRANSACTION"]=="T_NEW_CATALOG")
	fputs($fp,"<T_NEW_CATALOG>\n");
else
	fputs($fp,"<" . $general_info["TRANSACTION"] . " prev_version=\"" . $general_info["prev_version"] . "\">\n");
include("makeCatalogGroupSys.php");
include("makeProduct.php");
fputs($fp,"</" . $general_info["TRANSACTION"] . ">\n");
fputs($fp,"</BMECAT>\n");

fclose($fp);

if(count($aErrors)>0)
{
	//unlink($bme_file);
	$fp_error = fopen($err_file,"w");
	foreach($aErrors as $error)
	{
		fputs($fp_error,$error . "\n");
		echo $error . "\n";
	}
	fflush($fp_error);
	fclose($fp_error);
}

$fp = fopen($done_file,"w");
fputs($fp,"ok\n");
fflush($fp);
fclose($fp);


?>
