}
        $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');
            }
            $class_jc->updatePriceUploadStatus($tb_sp_ids, $products_uploaded, '1');
            //Set all catalog for each jng_sp_id (related to the upload)
            //last_active_status = 1/0 to trigger the active age counting.
            if (count($last_active_status_need_update) > 0) {
                $all_cats = implode(',', $last_active_status_need_update);
                $filter_las = "jng_sp_catalog_id IN ({$all_cats})";
                $class_jc->updateLastActiveStatus(null, $filter_las);
            }
            $messages['s'] = "<h3>{$total_family_uploaded} of {$total_family}" . " Family are uploaded to TB</h3>" . "<h3>{$total_products_uploaded} products are uploaded to TB</h3>";
        } else {
            $messages['e'] = '<h3>Failed uploading xml cat to TB Server</h3><br/>';
            unlink($xml_filename);
        }
    } else {
        $messages['n'] = 'No products need to upload/reupload to Tradebyte';
        unset($xmlt);
    }
}
if (count($invalid_family_messages) > 0) {
    foreach ($invalid_family_messages as $family_id => $ifm) {
        if (is_null($family_id) || $family_id == '') {
            $messages['e'] .= '<h2>Some products have no Family:</h2>' . '<div>' . $ifm . '</div>';
     if (isset($temp_images)) {
         foreach ($temp_images as $ti) {
             if (file_exists($ti)) {
                 unlink($ti);
             }
         }
     }
     exit;
 }
 //XML CLOSER
 $xml_content .= writeXMLclose('NVAG_CATALOG', 0);
 //FINISH XML FILE CREATION
 $success = fwrite($file, $xml_content) !== false;
 fclose($file);
 if ($success) {
     $class_jc->updateLastActiveStatus($jng_sp_id);
     $class_jc->updateLastReportedStock($jng_sp_id);
     //BACKUP XML FILE
     $shell = array();
     $shell[] = 'cd ' . SP_NECKERMANNAT_UPLOAD_PATH;
     $shell[] = 'zip ' . SP_NECKERMANNAT_PATH . 'cat-backup/' . $xml_filename . '.zip ' . $xml_filename;
     $output = shell_exec(implode("\n", $shell));
     $file = fopen(SP_NECKERMANNAT_UPLOAD_PATH . $xml_filename_marker, 'w');
     fclose($file);
     $upload_id = $class_ju->newUpload($jng_sp_id, 'CAT', $xml_filename, $session_userinfo['username']);
     $content = '<h3 class="green">CAT XML File is uploaded successfully</h3>';
     if (count($upload_image_log) > 0 && $create_image_zip) {
         $zipper->create_archive();
         $success = rename($zip_file, $zip_final);
         if ($success) {
             $file = fopen($zip_marker, 'w');