//GET PAGE
$page = 1;
if (isset($_GET['page'])) {
    $page = $_GET['page'];
}
//SUBMIT ACTION
if (isset($_POST['me_action'])) {
    if ($_POST['me_action'] == 'SETSPUPLOADSTATUS') {
        if ($_POST['submit'] == 'Reupload') {
            $upload_sp_id = $_POST['upload_sp_id'];
            header("Location: ?open=sp-catalog-generator&jng_sp_id={$upload_sp_id}&reupload=true");
            exit;
        } else {
            $upload_id = tep_db_prepare_input($_POST['upload_id']);
            $status = $_POST['submit'] == 'Success' ? 'S' : 'F';
            $class_ju->updateStatus($upload_id, $status, $session_userinfo['username']);
            header("Location: ?open=sp-catalog");
            exit;
        }
    } elseif ($_POST['me_action'] == 'COUNTCATSTATUS') {
        $category_top_id = $_POST['top_cat_id'];
        $sp = new SalesPartner($jng_sp_id);
        $status_arr = array('all', 'new', 'update', 'deactivate');
        $total_catalog_status = $sp->getCatalogTotalCount($status_arr, $category_top_id);
        $catalog_status_combo_box = '<select id="status-catalog" style="width:180px;"><option value="0">Choose Catalog Status..</option>' . loadComboListFromArray($total_catalog_status, null, null, false) . '</select>';
        echo $catalog_status_combo_box;
        exit;
    }
}
//START TEMPLATE
//CHECK IF UPLOAD IS ALREADY MADE
     $conn_id = ftp_connect($ftp_server);
     $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
     if ($conn_id && $login_result) {
         $contents = ftp_nlist($conn_id, $ftp_file_path);
         if (is_array($contents) && count($contents) > 0) {
             if (in_array($xml_filename_encrypted, $contents)) {
                 ftp_delete($conn_id, $xml_filename_encrypted);
             }
         }
         $success_upload = ftp_put($conn_id, $ftp_file_path . $xml_filename_encrypted, $encrypted_file, FTP_BINARY);
         if ($success_upload) {
             $upload_id = $class_ju->newUpload($jng_sp_id, 'ORDRSP', $xml_filename_encrypted, $session_userinfo['username']);
             $uploaded_size = ftp_size($conn_id, $ftp_file_path . $xml_filename_encrypted);
             $local_size = filesize($encrypted_file);
             if ($uploaded_size == $local_size) {
                 $class_ju->updateStatus($upload_id, 'S', 'auto-check');
                 $class_jo->confirmOrders($orders_confirmed, $export_date);
                 $content .= '<h3 class="green">' . $jng_sp_name . ': ORDRSP XML File is uploaded successfully</h3>';
             } else {
                 $content .= '<h3 class="red">' . $jng_sp_name . ': ORDRSP XML File is not uploaded successfully, please try again.</h3>';
             }
         } else {
             $content .= '<h3 class="red">' . $jng_sp_name . ': Upload file failed</h3>';
         }
         ftp_close($conn_id);
     } else {
         $content .= '<h3 class="red">' . $jng_sp_name . ': Failed connecting to FTP server, please try again later.</h3>';
     }
 } else {
     $content .= '<h3 class="red">' . $jng_sp_name . ': Failed encrypting file, please contact administrator</h3>';
 }
$dbq = tep_db_query("SELECT * FROM jng_sp_upload" . " WHERE jng_sp_id = {$jng_sp_id} AND (type LIKE '%CAT%')" . " AND upload_status='0'");
$prev_uploads = tep_db_num_rows($dbq);
if ($prev_uploads > 1) {
    $upoad_ids = array();
    while ($r = tep_db_fetch_array($dbq)) {
        $upoad_ids[] = $r['jng_sp_upload_id'];
    }
    $upoad_ids_text = implode(', ', $upc);
    logProcess("UNCONFIRMED PREV. CAT UPLOAD ({$prev_uploads}) " . $upoad_ids_text, '- ', true);
    logProcess("Please confirm last upload status manually in SP Catalog Upload", '- ', true);
} else {
    $status = $class_ju->retrieveNotSetStatus($jng_sp_id, 'CAT');
    $last_upload_id = isset($status['jng_sp_upload_id']) ? $status['jng_sp_upload_id'] : 0;
    if ($last_upload_id > 0) {
        //SET UPLOAD STATUS TO SUCCESS 'S' ALSO SET IMAGE UPLOADED TO '1'
        $class_ju->updateStatus($last_upload_id, 'S', 'auto');
    }
    //Use manual filter sof special cases
    //$products = $class_jc->retrieveCatalogForUpload(
    //    $jng_sp_id,
    //    false,
    //    'jc.products_id IN (15288,18029,18041,43783,43793,45176)');
    $products = $class_jc->retrieveCatalogForUpload($jng_sp_id);
    //23.06.2015 by sahat
    //because there are lotes of products deactivated by OTTO
    //we stop sending deactivation from our sides in catalog file
    //we send simply just the out of stock status to OTTO
    $logger->write("Skipping killed/deactivated products:");
    $products_deactivated_count = 0;
    foreach ($products as $key => $p) {
        if ($p['active_status'] == '0' && $p['last_active_status'] == '1') {