/**
 * Keep backup of a file in the specified path
 * @param String $file_path Full path to the original file
 * @param String $backup_path Backup location path (without filename/only directory)
 * @param Boolean $use_month Use month in backup path
 * @param Boolean $use_date Use date in backup path
 * @param String $new_filename Set manually a new filename for uploaded file
 * @param Boolean $overwrite_existing_file When set to false a timestamp suffix will be added to filename
 * @return Boolean
 */
function backupFile($file_path, $backup_path, $use_month = true, $use_date = false, $new_filename = '', $overwrite_existing_file = false)
{
    $backup_path = createBackupDirectory($backup_path, $use_month, $use_date);
    //prepare backup filename
    if ($new_filename == '') {
        $new_filename = basename($file_path);
    }
    $file_path_backup = $backup_path . $new_filename;
    //check if backup file to be created is already exist
    if (is_file($file_path_backup)) {
        //check if an overwrite mode is set
        if ($overwrite_existing_file) {
            //if yes, delete existing file
            unlink($file_path_backup);
        } else {
            //if no, add timestamp as suffix of new filename
            $timestamp = date('YmdHis');
            $filename_splits = explode('.', $new_filename);
            $filename_splits_total = count($filename_splits);
            if ($filename_splits_total == 1) {
                $new_filename .= '-' . $timestamp;
            } else {
                $filename_splits[$filename_splits_total - 2] .= '-' . $timestamp;
                $new_filename = implode('.', $filename_splits);
            }
            $file_path_backup = $backup_path . $new_filename;
        }
    }
    return copy($file_path, $file_path_backup);
}
         $xml_content .= writeXMLclose('product-line-item', 4);
     }
     $xml_content .= writeXMLclose('product-line-items', 3);
     $xml_content .= writeXMLclose('OrderState', 2);
     $xml_content .= writeXMLclose('OrderStates', 1);
     $xml_content .= writeXMLclose('ottopartner', 0);
     //CREATE XML ON SERVER
     $fn_counter = str_pad($ts_counter, 2, '0', STR_PAD_LEFT);
     $xml_filename = $sp_data[$jng_sp_id]['jng_id'] . '_orderstatus_' . $ts_period . $fn_counter . '.xml';
     $xmlfullpath = SP_OTTODE_UPLOAD_PATH_STATES . $xml_filename;
     $file = fopen($xmlfullpath, 'w');
     $success = fwrite($file, $xml_content) !== false;
     fclose($file);
     if ($success) {
         $class_jo->confirmOrderReturn($items_confirmed);
         $archive_path_raw = createBackupDirectory(SP_OTTODE_ARCHIVE_PATH . 'return/');
         $archive_fullpath = $archive_path_raw . $xml_filename;
         while (file_exists($archive_fullpath)) {
             $archive_fullpath .= '_copy';
         }
         copy($xmlfullpath, $archive_fullpath);
         $need_jms_upload = true;
     }
 }
 $logger->write("{$jng_sp_name}: {$orders_total} Orders confirmed");
 //CONFIRM DIRECTLY TO CUSTOMER
 $counter_success = 0;
 $counter_fail = 0;
 foreach ($orders as $counter => $o) {
     $order = new Order(Order::ORDER_TYPE_SALES_PARTNER, $o['jng_sp_orders_id']);
     $sent = $order->sendReturnNotification();
     }
     $xml_content .= writeXMLclose('product-line-items', 3);
     $xml_content .= writeXMLclose('OrderState', 2);
     $xml_content .= writeXMLclose('OrderStates', 1);
     $xml_content .= writeXMLclose('ottopartner', 0);
     //CREATE XML ON SERVER
     $fn_counter = str_pad($ts_counter, 2, '0', STR_PAD_LEFT);
     $xml_filename = $temp_sp['jng_id'] . '_orderstatus_' . $ts_period . $fn_counter . '.xml';
     $xmlfullpath = SP_OTTODE_UPLOAD_PATH_STATES . $xml_filename;
     $file = fopen($xmlfullpath, 'w');
     $success = fwrite($file, $xml_content) !== false;
     fclose($file);
     if ($success) {
         $orders_confirmed++;
         $class_jo->confirmOrders($o['jng_sp_orders_id'], $timestamp);
         $archive_path_raw = createBackupDirectory(SP_OTTODE_ARCHIVE_PATH . 'confirm/');
         $archive_fullpath = $archive_path_raw . $xml_filename;
         while (file_exists($archive_fullpath)) {
             $archive_fullpath .= '_copy';
         }
         copy($xmlfullpath, $archive_fullpath);
         $need_jms_upload = true;
     }
     $logger->write("SP-{$o['jng_sp_orders_id']} is confirmed");
 }
 if ($need_jms_upload) {
     $upload_ok = executeOTTOShellscript(SP_OTTODE_SHELLSCRIPT_UPLOAD_ORDERSTATUS);
 }
 if ($orders_confirmed > 0) {
     if ($upload_ok) {
         //$content .= '<h3 class="green">' . $jng_sp_name .
                     }
                     $col_counter++;
                 }
             }
         }
         if ($row_added) {
             $row_number++;
             $row_counter++;
         }
     }
     if ($row_counter >= $template_row_start) {
         //Remove red header rows
         $obj_excel_ws->removeRow($template_row_start - 1, 1);
     }
     $filename = 'ZaloraBMPImportFile-' . date('YmdHi') . '.xls';
     $filepath = createBackupDirectory(SP_ZALORABMP_UPLOAD_PATH);
     $datepath = str_replace(SP_ZALORABMP_UPLOAD_PATH, '', $filepath);
     $savepath = $datepath . $filename;
     $obj_excel_writer = new PHPExcel_Writer_Excel5($obj_excel);
     $obj_excel_writer->save($filepath . $filename);
     $upload_id = $class_ju->newUpload($jng_sp_id, 'CAT', $savepath, $session_userinfo['username']);
     foreach ($active_log as $status => $jccol) {
         foreach ($jccol as $jcid) {
             $class_ju->addUploadImage($upload_id, $jcid);
             $class_ju->addUploadProducts($upload_id, $jcid, $status);
         }
     }
     header("Location: print/print-file.php?type=zrbr-cat&fn={$savepath}");
     exit;
     //ZALORA BMP UPLOAD PROCESS END
 } else {