function constructMessage(&$xmlt, $msg_data, $type, $xml_messages_list)
{
    global $message_id_prefix, $message_id_counter;
    $message_id_counter++;
    $timestamp = time();
    $date_created = tradebyte::getDateFormat($timestamp);
    $xml_message = $xmlt->drawTag($xml_messages_list, 'MESSAGE');
    $xmlt->drawTag($xml_message, 'MESSAGE_ID', $message_id_prefix . $message_id_counter);
    $xmlt->drawTag($xml_message, 'MESSAGE_TYPE', tradebyte::getMessageType($type));
    $xmlt->drawTag($xml_message, 'TB_ORDER_ID', $msg_data['tb_order_id']);
    $xmlt->drawTag($xml_message, 'TB_ORDER_ITEM_ID', $msg_data['tb_order_item_id']);
    $xmlt->drawTag($xml_message, 'SKU', $msg_data['sku']);
    $xmlt->drawTag($xml_message, 'CHANNEL_SIGN', $msg_data['channel']);
    $xmlt->drawTag($xml_message, 'QUANTITY', intval($msg_data['qty']));
    //EXCLUDE CANCELATIONS FROM ADDING CARRIER/IDCODE TAG
    if ($type == 'S' || $type == 'R') {
        $xmlt->drawTag($xml_message, 'CARRIER_PARCEL_TYPE', 'DHL_STD_NATIONAL');
        if ($type == 'S') {
            $xmlt->drawTag($xml_message, 'IDCODE', $msg_data['idcode_ship']);
            $xmlt->drawTag($xml_message, 'IDCODE_RETURN_PROPOSAL', $msg_data['idcode_ret']);
        } elseif ($type == 'R') {
            $xmlt->drawTag($xml_message, 'IDCODE', $msg_data['idcode_ret']);
        }
    }
    //DATE_CREATED
    $xmlt->drawTag($xml_message, 'DATE_CREATED', $date_created);
}
function constructMessage(&$xmlt, $msg_data, $type, $xml_messages_list)
{
    global $message_id_prefix, $message_id_counter;
    $message_id_counter++;
    $timestamp = time();
    $date_created = tradebyte::getDateFormat($timestamp);
    $xml_message = $xmlt->drawTag($xml_messages_list, 'MESSAGE');
    $xmlt->drawTag($xml_message, 'MESSAGE_ID', $message_id_prefix . $message_id_counter);
    $xmlt->drawTag($xml_message, 'MESSAGE_TYPE', tradebyte::getMessageType($type));
    $xmlt->drawTag($xml_message, 'TB_ORDER_ID', $msg_data['tb_order_id']);
    $xmlt->drawTag($xml_message, 'CHANNEL_SIGN', $msg_data['channel']);
    $xmlt->drawTag($xml_message, 'QUANTITY', intval($msg_data['qty']));
    $xmlt->drawTag($xml_message, 'COMMENT', 'Payment state is "paid"');
    //DATE_CREATED
    $xmlt->drawTag($xml_message, 'DATE_CREATED', $date_created);
}
require_once '../functions-2.php';
require_once '../functions-xml.php';
require_once DIR_WS_FUNCTIONS . 'html_output.php';
tep_db_connect();
use_class('logger');
$logger = new logger('cron', 'tradebyte-orders-downloader');
use_class('Order');
use_class('tradebyte');
use_class('xml_tools');
use_class('products_ean');
use_class('jng_sp');
use_class('jng_sp_download');
use_class('jng_sp_orders');
use_class('jng_sp_customers');
use_class('logger');
$tb = new tradebyte();
$xmlt = new xml_tools();
$class_ean = new products_ean();
$class_sp = new jng_sp();
$class_jd = new jng_sp_download();
$class_jo = new jng_sp_orders();
$class_jcust = new jng_sp_customers();
$zade_prepayment_need_refreshed = false;
function logThis($logdata)
{
    global $logger;
    $logger->write($logdata);
    echo "{$logdata}<br />\n";
}
/////////////////////////////
// STEP 1: DOWNLOAD ORDERS //
 if (count($pri_data) > 0) {
     constructArticle($xmlt, $pri_data, $xml_articledata);
 }
 $reported_products = array_unique($reported_products);
 //CREATE XML FILE LOCALLY
 $timestamp = date('Ymd_His');
 //$xml_file = "TBCAT_$timestamp.TEST.xml";
 $xml_file = "TBCAT_{$timestamp}.xml";
 $xml_filename = TRADEBYTE_UPLOAD_CAT_PATH . $xml_file;
 $xmlt->setOutputVars($xml_filename);
 $xmlt->printOutput('F');
 //echo $xmlt->printOutput();
 $logger->write("PRI xml {$xml_file} created successfully");
 //SEND XML TO TRADEBYTE SERVER VIA FTP SSL
 use_class('tradebyte');
 $tb = new tradebyte();
 $tb->connect();
 $success_upload = false;
 if ($tb->send($xml_filename, TRADEBYTE_FTP_IN_PATH . $xml_file)) {
     rename($xml_filename, TRADEBYTE_ARCHIVE_PRI_PATH . $xml_file);
     $success_upload = true;
 }
 $tb->disconnect();
 if ($success_upload) {
     $new_status = 0;
     $class_pm->stockUpdateTBNeedUpdateStatus(WAREHOUSE_ID_PRODUCTS_HAMBURG, $reported_products, $new_status);
     $class_pm->stockResetTBNeedUpdateStatus();
     $logger->write("Successfully sent to TB");
 } else {
     $logger->write("Problem found when sending to TB");
 }
 //CREATE XML FILE LOCALLY
 $timestamp = date('Ymd_His');
 //$xml_file = "TBCAT_$timestamp.TEST.xml";
 $xml_file = "TBCAT_{$timestamp}.xml";
 $xml_filename = TRADEBYTE_UPLOAD_CAT_PATH . $xml_file;
 $xmlt->setOutputVars($xml_filename);
 $xmlt->printOutput('F');
 //$test_path = 'D:/Data Dewa/Data Kantor/Bonofactum/Tradebyte/';
 //$test_target = $test_path . 'tb-cat_1_2_import.xsd';
 //$xmlt->validate(
 //    $test_target,
 //    $xmlt->output_filename);
 //echo $xmlt->printOutput();
 //SEND XML TO TRADEBYTE SERVER VIA FTP SSL
 use_class('tradebyte');
 $tb = new tradebyte();
 $tb->connect();
 //$tb->connect('NONSSL');
 $target_filename = TRADEBYTE_FTP_IN_PATH . $xml_file;
 $success_upload = false;
 if ($tb->send($xml_filename, $target_filename)) {
     rename($xml_filename, TRADEBYTE_ARCHIVE_CAT_PATH . $xml_file);
     $success_upload = true;
 }
 $tb->disconnect();
 if ($success_upload) {
     //SET family_locked FOR ALREADY UPLOADED PRODUCTS TO TB
     $class_pm->updateFlagFamilyLocked($products_uploaded, 1);
     $class_pm->setTBStatus($products_uploaded, 1);
     if (count($images_uploaded) > 0) {
         $class_jc->updateImageUploadStatus($tb_sp_ids, $images_uploaded, '1');