} else {
        //now we should give tradebyte server some time to process it
        if ($hour >= $config_upload_hour && $hour < $config_upload_wait_until) {
            $logger->write('Skip upload so tradebyte has enough time to' . ' process last full upload');
            $logger->close();
            die('Skipped!');
        }
    }
}
$reported_products = array();
//GET ALL PRODUCTS STOCK CONNECTED TO TRADEBYTE AND FLAG AS NEED STOCK UPDATE
$q = "SELECT ps.products_id, ps.articles_id" . ", ps.stock, ps.booking_active, p.products_model, p.active_status" . ", IFNULL(pa.active_status, p.active_status) AS article_status" . ", IF(ps.articles_id=0, pnc.products_length, pa.length) AS length" . ", jc.jng_sp_id, jc.active_status AS catalog_status" . ", jc.price, jc.price_old" . " FROM products_stock ps" . " INNER JOIN jng_sp_catalog jc" . " ON jc.jng_sp_id in ({$sp_use_tb_ids_filter})" . " AND jc.products_id=ps.products_id" . " INNER JOIN products p ON p.products_id=ps.products_id" . " LEFT JOIN products_non_configurator pnc" . " ON pnc.products_id=ps.products_id" . " LEFT JOIN products_articles pa" . " ON pa.products_articles_id=ps.articles_id" . " WHERE p.tb_status IN (1, 2, 3) AND ps.tb_need_update = 1" . " ORDER BY ps.products_id, ps.articles_id";
$dbq = tep_db_query($q);
if (tep_db_num_rows($dbq) > 0) {
    $xmlt = new xml_tools('1.0', 'UTF-8');
    $xml_articledata = $xmlt->drawTag($xmlt, 'ARTICLEDATA');
    $pri_data = array();
    while ($p = tep_db_fetch_array($dbq)) {
        $pid = $p['products_id'];
        $aid = $p['articles_id'];
        $a_nr = getANR($p['products_model'], $p['length']);
        //ACTIVE STATUS
        if ($p['active_status'] == '0' || $p['article_status'] == '0') {
            //Set inactive if either product / article is killed
            $active_status = '0';
        } else {
            //Otherwise use catalog active status
            $active_status = $p['catalog_status'];
        }
        if (!isset($pri_data[$a_nr])) {
            $pri_data[$a_nr] = array();
$logger = new logger('cron', 'depot-refill-local-xml-generator');
$class_do = new depot_orders();
$class_bins = new bins();
$today_datetime = date('Y-m-d H:i:s');
$today = date('Y-m-d', strtotime($today_datetime));
$country_name = getCountryName($server_local_code);
$logger->write("PREPARE XML DEPOT REFILL - START");
//GET TODAYS DEPOT REFILL CREATED
$q = "SELECT * FROM depot_orders" . " WHERE segments_id = {$segment_id_default} " . " AND trans_type = 'AR' AND trans_id = " . depot_orders::AUTO_REFILL_ID_DAILYCRON . " AND order_date >= '{$today} 00:00:00' AND order_date <= '{$today} 23:59:59'";
$r = tep_db_query($q);
if (tep_db_num_rows($r) > 0) {
    $segment = $class_bins->retrieveSegmentDetail($segment_id_default);
    //PREPARE FOR CREATE XML DEPOT REFILL LOCAL
    $xmlt = new xml_tools('1.0', 'UTF-8');
    //DEPOT_REFILL_LOCAL
    $depot_refill_local = $xmlt->drawTag($xmlt, 'DEPOT_REFILL_LOCAL');
    //LOCAL_INFO
    $local_info = $xmlt->drawTag($depot_refill_local, 'LOCAL_INFO');
    $xmlt->drawTag($local_info, 'SEGMENT_ID', $segment_id_default);
    $xmlt->drawTag($local_info, 'CREATED_DATE', date('Y-m-d H:i:s'));
    $group_name = "Manobo Local Orders ({$country_name})";
    $xmlt->drawTag($local_info, 'GROUP_NAME', $group_name);
    //REFILL_ORDERS
    $refill_orders = $xmlt->drawTag($depot_refill_local, 'REFILL_ORDERS');
    $counter = 0;
    while ($row = tep_db_fetch_array($r)) {
        $products_id = $row['products_id'];
        $obj_product = new Product($products_id);
        //Take only product which "category top" same with "category top" of segment used
        if ($obj_product->category_top_id == $segment['categories_top_id']) {
            $order = $xmlt->drawTag($refill_orders, 'ORDER');
}
$messages_list_added = false;
$items_confirmed = array();
$orders_canceled = array();
if (count($data) > 0) {
    //START PREPARING XML MESSAGE
    $xmlt = new xml_tools('1.0', 'UTF-8');
    foreach ($data as $jng_sp_id => $messages_types) {
        $jng_sp_name = $sp_detail[$jng_sp_id]['name'];
        $jng_sp_tb_channel = strtoupper($sp_detail[$jng_sp_id]['tb_channel']);
        foreach ($messages_types as $msg_type => $data_sp) {
            $orders = $data_sp['orders'];
            if (count($orders > 0)) {
                if (!$messages_list_added) {
                    //MESSAGES_LIST
                    $xml_messages_list = $xmlt->drawTag($xmlt, 'MESSAGES_LIST');
                    $messages_list_added = true;
                }
                //ORDERS
                foreach ($orders as $counter => $o) {
                    //ITEMS
                    $items = $data_sp['items'][$o['jng_sp_orders_id']];
                    foreach ($items as $i) {
                        $msg_data = array();
                        $msg_data['channel'] = $jng_sp_tb_channel;
                        $msg_data['sku'] = $i['article_number'];
                        $msg_data['tb_order_id'] = str_replace('TB', '', $o['shipment_id']);
                        $msg_data['tb_order_item_id'] = $i['article_reference_number'];
                        $msg_type_xml = $msg_type;
                        if ($msg_type == 'R') {
                            //Return
    }
}
$query .= " GROUP BY family_id";
$dbq = tep_db_query($query);
$messages = array();
$products_uploaded = array();
$images_uploaded = array();
$last_active_status_need_update = array();
$invalid_family_messages = array();
$total_family = 0;
$total_family_uploaded = 0;
//STARTING CAT PROCESS
if (tep_db_num_rows($dbq) > 0) {
    $xmlt = new xml_tools('1.0', 'UTF-8');
    //TBCATALOG
    $xml_tbcatalog = $xmlt->drawTag($xmlt, 'TBCATALOG', '', array('version' => '1.2', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'http://api.trade-server.net/' . 'schema/all_in_one/tb-cat_1_2_import.xsd'));
    //SUPPLIER
    $xml_supplier = $xmlt->drawTag($xml_tbcatalog, 'SUPPLIER');
    $xmlt->drawTag($xml_supplier, 'NAME', JNG_ADDRESS_NAME);
    $xmlt->drawTag($xml_supplier, 'CONTACT', EMAIL_NAME_PETERN);
    $xmlt->drawTag($xml_supplier, 'STREET', JNG_ADDRESS_STREET);
    $xmlt->drawTag($xml_supplier, 'ZIP', JNG_ADDRESS_ZIPCODE);
    $xmlt->drawTag($xml_supplier, 'CITY', JNG_ADDRESS_CITY);
    $xmlt->drawTag($xml_supplier, 'PHONE', JNG_PHONENO_INT);
    $xmlt->drawTag($xml_supplier, 'EMAIL', EMAIL_ADDERSS_TECHNICAL);
    //PRODUCTDATA
    $xml_productdata = $xmlt->drawTag($xml_tbcatalog, 'PRODUCTDATA');
    //LOOPING FOR FAMILY THAT NEED UPLOAD/REUPLOAD TO TB
    $tb_sp_ids = array();
    while ($p = tep_db_fetch_array($dbq)) {
        $total_family++;