package cpm.com.gskmtorange.upload;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

//import com.crashlytics.android.Crashlytics;

import androidx.appcompat.app.AppCompatActivity;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import cpm.com.gskmtorange.Database.GSKOrangeDB;
import cpm.com.gskmtorange.GetterSetter.AdditionalDialogGetterSetter;
import cpm.com.gskmtorange.GetterSetter.AddittionalGetterSetter;
import cpm.com.gskmtorange.GetterSetter.BrandAvabilityGetterSetter;
import cpm.com.gskmtorange.GetterSetter.CategoryPictureGetterSetter;
import cpm.com.gskmtorange.GetterSetter.CoachingVisitGetterSetter;
import cpm.com.gskmtorange.GetterSetter.CoverageBean;
import cpm.com.gskmtorange.GetterSetter.StoreBean;
import cpm.com.gskmtorange.R;
import cpm.com.gskmtorange.constant.CommonFunctions;
import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.xmlGetterSetter.AuditDataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.CompetitonPromoGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.CounterfeitProductGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.FailureGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.GapsChecklistGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.ImageGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.MSL_AvailabilityGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.MSL_AvailabilityStockFacingGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.MappingPharmaQuestionGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.NoCameraDataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.POGGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.PharmaMSLInsertedGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.PharmaSOSGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.PromoComplianceSkuwiseGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.Promo_Compliance_DataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.SkuGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.StockFacing_PlanogramTrackerDataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.Stock_FacingGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.Store_wise_camera_DataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.T2PGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.Visit_Type_Master_GetterSetter;
import cpm.com.gskmtorange.xmlHandlers.FailureXMLHandler;

public class PreviousDataUploadActivity extends AppCompatActivity {
    private Context context;
    GSKOrangeDB db;
    ArrayList<CoverageBean> coverageList;
    String date, userId, app_version;
    StoreBean storeData;
    String datacheck = "";
    String[] words;
    String validity;
    int mid;
    String errormsg = "", Path;
    Data data;
    ArrayList<MSL_AvailabilityGetterSetter> msl_availabilityList;
    ArrayList<Stock_FacingGetterSetter> stock_facingHeaderList, stock_facingChildList;
    ArrayList<Promo_Compliance_DataGetterSetter> promotionSkuList, additionalPromotionList;
    ArrayList<T2PGetterSetter> t2PGetterSetters;
    ArrayList<AddittionalGetterSetter> additionalVisibilityList;
    ArrayList<AdditionalDialogGetterSetter> additionalVisibilitySkuList;
    ArrayList<MSL_AvailabilityStockFacingGetterSetter> msl_availabilityStockFacingList;
    private Dialog dialog;
    private ProgressBar pb;
    private TextView percentage, message;
    private FailureGetterSetter failureGetterSetter = null;
    private SharedPreferences preferences;
    private int factor, k = 0;
    ArrayList<CategoryPictureGetterSetter> adddata = new ArrayList<CategoryPictureGetterSetter>();
    ArrayList<CategoryPictureGetterSetter> listdat = new ArrayList<CategoryPictureGetterSetter>();
    ArrayList<CoachingVisitGetterSetter> coachingVisitData;
    Object result = "";
    ArrayList<StockFacing_PlanogramTrackerDataGetterSetter> stockFacingPlanogramDataList;
    ArrayList<Store_wise_camera_DataGetterSetter> storeWiseCameraDataGetterSetters;
    ArrayList<PromoComplianceSkuwiseGetterSetter> promoComplianceSkuwiseGetterSetters, promoComplianceSkuwiseExistsGetterSetters;
    ArrayList<CounterfeitProductGetterSetter> counterfeitProductGetterSetters, counterfeitProductExistsGetterSetters;
    ArrayList<CompetitonPromoGetterSetter> competitonGetterSetters, competitonExistsGetterSetters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        date = preferences.getString(CommonString.KEY_DATE, null);
        userId = preferences.getString(CommonString.KEY_USERNAME, null);
        app_version = preferences.getString(CommonString.KEY_VERSION, null);
        db = new GSKOrangeDB(this);
        db.open();
        context = this;
        Path = CommonString.getImagesFolder(context);
        CommonFunctions.updateLangResources(context, preferences.getString(CommonString.KEY_LANGUAGE, ""));
        //start upload
        new UploadTask(this).execute();
    }

    public String UploadImage(String path, String folder_name) throws Exception {
        errormsg = "";
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(Path + path, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 1639;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFile(Path + path, o2);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        byte[] ba = bao.toByteArray();
        String ba1 = Base64.encodeBytes(ba);

        SoapObject request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_IMAGE);

        String[] split = path.split("/");
        String path1 = split[split.length - 1];

        request.addProperty("img", ba1);
        request.addProperty("name", path1);
        request.addProperty("FolderName", folder_name);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(CommonString.URL);
        androidHttpTransport.call(CommonString.SOAP_ACTION_UPLOAD_IMAGE, envelope);

        Object result = envelope.getResponse();

        if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
            if (result.toString().equalsIgnoreCase(CommonString.KEY_FALSE)) {
                return CommonString.KEY_FALSE;
            }

            SAXParserFactory saxPF = SAXParserFactory.newInstance();
            SAXParser saxP = saxPF.newSAXParser();
            XMLReader xmlR = saxP.getXMLReader();

            // for failure
            FailureXMLHandler failureXMLHandler = new FailureXMLHandler();
            xmlR.setContentHandler(failureXMLHandler);

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(result.toString()));
            xmlR.parse(is);

            failureGetterSetter = failureXMLHandler.getFailureGetterSetter();

            if (failureGetterSetter.getStatus().equalsIgnoreCase(CommonString.KEY_FAILURE)) {
                errormsg = failureGetterSetter.getErrorMsg();
                return CommonString.KEY_FAILURE;
            }
        } else {
            new File(Path + path).delete();
        }

        return result.toString();
    }

    class Data {
        int value;
        String name;
    }

    private class UploadTask extends AsyncTask<Void, Data, String> {
        private Context context;

        UploadTask(Context context) {
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Uploading Data");
            dialog.setCancelable(false);
            dialog.show();

            pb = (ProgressBar) dialog.findViewById(R.id.progressBar1);
            percentage = (TextView) dialog.findViewById(R.id.percentage);
            message = (TextView) dialog.findViewById(R.id.message);
        }

        @Override
        protected void onProgressUpdate(Data... values) {
            // TODO Auto-generated method stub

            pb.setProgress(values[0].value);
            percentage.setText(values[0].value + "%");
            message.setText(values[0].name);
        }

        @Override
        protected String doInBackground(Void... params) {
            try {
                data = new Data();

                ArrayList<CoverageBean> adhoc_coverageList = db.getPreviousCoverageData(date, CommonString.FROM_DEVIATION);

                if (adhoc_coverageList.size() > 0) {

                    for (int i = 0; i < adhoc_coverageList.size(); i++) {

                        String onXML = "[ADHOC_JCP]"
                                + "[USER_ID]" + userId + "[/USER_ID]"
                                + "[VISIT_DATE]" + adhoc_coverageList.get(i).getVisitDate() + "[/VISIT_DATE]"
                                + "[STORE_CD]" + adhoc_coverageList.get(i).getStoreId() + "[/STORE_CD]"
                                + "[/ADHOC_JCP]";

                        final String adhoc_xml = "[DATA]" + onXML + "[/DATA]";

                        SoapObject request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                        request.addProperty("XMLDATA", adhoc_xml);
                        request.addProperty("KEYS", "ADHOC_JOURNEY_PLAN");
                        request.addProperty("USERNAME", userId);
                        request.addProperty("MID", 0);

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);

                        HttpTransportSE androidHttpTransport = new HttpTransportSE(CommonString.URL);
                        androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                        result = envelope.getResponse();
                        if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                            return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                        }
                    }
                }


                coverageList = db.getPreviousCoverageData(date, null);

                if (coverageList.size() > 0) {
                    if (coverageList.size() == 1) {
                        factor = 50;
                    } else {
                        factor = 100 / (coverageList.size());
                    }
                }

                for (int i = 0; i < coverageList.size(); i++) {

                    storeData = db.getSpecificStoreData(coverageList.get(i).getVisitDate(), coverageList.get(i).getStoreId(), coverageList.get(i).getFlag_from());
                    if (storeData.getSTORE_ID() != null) {

                        if (storeData.getCHECKOUT_STATUS().equals(CommonString.KEY_Y) ||
                                storeData.getCHECKOUT_STATUS().equals(CommonString.KEY_L) ||
                                !storeData.getUPLOAD_STATUS().equals(CommonString.KEY_U) ||
                                !storeData.getUPLOAD_STATUS().equals(CommonString.KEY_UN)) {

                            String camera_allow = storeData.getCAMERA_ALLOW();

                            String adhoc;
                            if (coverageList.get(i).getFlag_from() != null && coverageList.get(i).getFlag_from().equals(CommonString.FROM_DEVIATION)) {
                                adhoc = "1";
                            } else {
                                adhoc = "0";
                            }

                            String coverage_service, status_service;

                            if (coverageList.get(i).getFlag_from().equals(CommonString.FROM_ADDITIONAL)) {
                                status_service = CommonString.METHOD_UPLOAD_COVERAGE_STATUS_ADDITIONAL;
                                coverage_service = CommonString.METHOD_UPLOAD_COVERAGE_NEW_ADDITIONAL;
                            } else {
                                status_service = CommonString.METHOD_UPLOAD_COVERAGE_STATUS;
                                coverage_service = CommonString.METHOD_UPLOAD_COVERAGE_NEW;
                            }

                            String sub_reason_id = coverageList.get(i).getSub_reasonId();
                            if (sub_reason_id != null && sub_reason_id.equals("")) {
                                sub_reason_id = "0";
                            }

                            String onXML = "[DATA]"
                                    + "[USER_DATA]"
                                    + "[STORE_CD]" + coverageList.get(i).getStoreId() + "[/STORE_CD]"
                                    + "[VISIT_DATE]" + coverageList.get(i).getVisitDate() + "[/VISIT_DATE]"
                                    + "[LATITUDE]" + coverageList.get(i).getLatitude() + "[/LATITUDE]"
                                    + "[APP_VERSION]" + app_version + "[/APP_VERSION]"
                                    + "[LONGITUDE]" + coverageList.get(i).getLongitude() + "[/LONGITUDE]"
                                    + "[IN_TIME]" + coverageList.get(i).getInTime() + "[/IN_TIME]"
                                    + "[OUT_TIME]" + coverageList.get(i).getOutTime() + "[/OUT_TIME]"
                                    + "[UPLOAD_STATUS]" + "N" + "[/UPLOAD_STATUS]"
                                    + "[USER_ID]" + userId + "[/USER_ID]"
                                    + "[IMAGE_URL]" + coverageList.get(i).getImage() + "[/IMAGE_URL]"
                                    + "[REASON_ID]" + coverageList.get(i).getReasonid() + "[/REASON_ID]"
                                    + "[REASON_REMARK]" + coverageList.get(i).getRemark() + "[/REASON_REMARK]"
                                    + "[CAMERA_ALLOWED]" + camera_allow + "[/CAMERA_ALLOWED]"
                                    + "[CHECKOUT_IMAGE]" + coverageList.get(i).getCheckOut_Image() + "[/CHECKOUT_IMAGE]"
                                    + "[ADHOC]" + adhoc + "[/ADHOC]"
                                    + "[SUBREASONID]" + sub_reason_id + "[/SUBREASONID]"
                                    + "[/USER_DATA]"
                                    + "[/DATA]";

                            SoapObject request = new SoapObject(CommonString.NAMESPACE, coverage_service);
                            request.addProperty("onXML", onXML);

                            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                            envelope.dotNet = true;
                            envelope.setOutputSoapObject(request);

                            HttpTransportSE androidHttpTransport = new HttpTransportSE(CommonString.URL);
                            androidHttpTransport.call(CommonString.SOAP_ACTION + coverage_service, envelope);

                            result = envelope.getResponse();

                            datacheck = result.toString();
                            words = datacheck.split("\\;");
                            validity = (words[0]);

                            if (validity.equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                db.updateCoverageStatus(coverageList.get(i).getStoreId(), CommonString.KEY_P);

                                db.updateStoreStatusOnLeave(coverageList.get(i).getStoreId(), date, CommonString.KEY_P, coverageList.get(i).getFlag_from());
                            } else {
                                continue;
                                //return CommonString.METHOD_UPLOAD_COVERAGE;
                            }
                            mid = Integer.parseInt((words[1]));

                            if (coverageList.get(i).getFlag_from().equals(CommonString.FROM_ADDITIONAL)) {

                                //MSL_Availability_StockFacing
                                String mslAvailability_stockFacing_xml = "";
                                onXML = "";
                                db.open();
                                msl_availabilityStockFacingList = db.getStockUploadServerData(coverageList.get(i).getStoreId());

                                if (msl_availabilityStockFacingList.size() > 0) {
                                    for (int j = 0; j < msl_availabilityStockFacingList.size(); j++) {
                                        if (!msl_availabilityStockFacingList.get(j).getSku_id().equals("0")) {

                                            String stock;
                                            if (!msl_availabilityStockFacingList.get(j).getStock().equals("")) {
                                                stock = msl_availabilityStockFacingList.get(j).getStock();
                                            } else {
                                                stock = "0";
                                            }
                                            onXML = "[ST]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USERID]" + userId + "[/USERID]"
                                                    + "[SKUID]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getSku_id()) + "[/SKUID]"
                                                    + "[STK]" + Integer.parseInt(stock) + "[/STK]"
                                                    + "[MBQ]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getMbq()) + "[/MBQ]"
                                                    + "[MSL]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getMust_have()) + "[/MSL]" +

                                                    "[/ST]";

                                            mslAvailability_stockFacing_xml = mslAvailability_stockFacing_xml + onXML;
                                        }
                                    }

                                    final String sos_xml = "[DATA]" + mslAvailability_stockFacing_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "STOCK_ADDITIONAL_STORE");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 15;
                                data.name = getString(R.string.availability_data_uploading);
                                publishProgress(data);
                            } else if (coverageList.get(i).getFlag_from().equals(CommonString.FROM_PHARMA)) {

                                //Pharmacy store data

                                //Store detail
                                String store_detail_xml = "";
                                onXML = "";
                                db.open();
                                ImageGetterSetter imageGetterSetter = db.getStoreDetailPharmaImageInsertedData(coverageList.get(i).getStoreId());
                                String image_xml = "";

                                image_xml = "[IMG_L]"
                                        + "[MID]" + mid + "[/MID]"
                                        + "[USERID]" + userId + "[/USERID]"
                                        + "[IMG1]" + imageGetterSetter.getImage1() + "[/IMG1]"
                                        + "[IMG2]" + imageGetterSetter.getImage2() + "[/IMG2]"
                                        + "[IMG3]" + imageGetterSetter.getImage3() + "[/IMG3]"
                                        + "[IMG4]" + imageGetterSetter.getImage4() + "[/IMG4]" +
                                        "[/IMG_L]";
                                ArrayList<MappingPharmaQuestionGetterSetter> pharmaStoreDetail = db.getStoreDetailPharmaAnswerInsertedData(coverageList.get(i).getStoreId(), null);

                                if (pharmaStoreDetail.size() > 0) {
                                    for (int j = 0; j < pharmaStoreDetail.size(); j++) {

                                        onXML = "[ST]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[QID]" + Integer.parseInt(pharmaStoreDetail.get(j).getQUESTION_ID().get(0)) + "[/QID]"
                                                + "[BID]" + Integer.parseInt(pharmaStoreDetail.get(j).getBRAND_ID().get(0)) + "[/BID]"
                                                + "[SCID]" + Integer.parseInt(pharmaStoreDetail.get(j).getSUB_CATEGORY_ID().get(0)) + "[/SCID]"
                                                + "[PRESENT]" + pharmaStoreDetail.get(j).getIsChecked() + "[/PRESENT]"
                                                + "[IMG]" + pharmaStoreDetail.get(j).getImgPath() + "[/IMG]" +

                                                "[/ST]";

                                        store_detail_xml = store_detail_xml + onXML;
                                    }

                                    final String sos_xml = "[DATA]" + image_xml + store_detail_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "PHARMA_STORE_DETAIL");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }

                                data.value = 15;
                                data.name = getString(R.string.availability_data_uploading);
                                publishProgress(data);

                                String pharma_sos_xml = "";
                                onXML = "";

                                ArrayList<PharmaSOSGetterSetter> pharmaSOSList = db.getStoreDetailPharmaForSOSData(coverageList.get(i).getStoreId(), "1");

                                if (pharmaSOSList.size() > 0) {
                                    for (int j = 0; j < pharmaSOSList.size(); j++) {

                                        onXML = "[ST]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[QID]" + Integer.parseInt(pharmaSOSList.get(j).getQUESTION_ID()) + "[/QID]"
                                                + "[BID]" + Integer.parseInt(pharmaSOSList.get(j).getBRAND_ID()) + "[/BID]"
                                                + "[SCID]" + Integer.parseInt(pharmaSOSList.get(j).getSUB_CATEGORY_ID()) + "[/SCID]"
                                                + "[G_FAC]" + pharmaSOSList.get(j).getGskFacing() + "[/G_FAC]"
                                                + "[C_FAC]" + pharmaSOSList.get(j).getCompetitorFacing() + "[/C_FAC]" +
                                                "[/ST]";

                                        pharma_sos_xml = pharma_sos_xml + onXML;
                                    }

                                    final String sos_xml = "[DATA]" + pharma_sos_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "PHARMA_SOS");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }

                                data.value = 15;
                                data.name = getString(R.string.availability_data_uploading);
                                publishProgress(data);

                                String pharma_msl_xml = "";
                                onXML = "";

                                ArrayList<PharmaMSLInsertedGetterSetter> pharmaMSLList = db.getPharmaMSLInsertedData(coverageList.get(i).getStoreId());

                                if (pharmaMSLList.size() > 0) {
                                    for (int j = 0; j < pharmaMSLList.size(); j++) {

                                        onXML = "[ST]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[BID]" + Integer.parseInt(pharmaMSLList.get(j).getBrandId()) + "[/BID]"
                                                + "[SKID]" + Integer.parseInt(pharmaMSLList.get(j).getSkuId()) + "[/SKID]"
                                                + "[PRESENT]" + pharmaMSLList.get(j).getPresent() + "[/PRESENT]" +
                                                "[/ST]";

                                        pharma_msl_xml = pharma_msl_xml + onXML;
                                    }

                                    final String sos_xml = "[DATA]" + pharma_msl_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "PHARMA_MSL");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }

                                data.value = 15;
                                data.name = getString(R.string.availability_data_uploading);
                                publishProgress(data);

                            } else {
                                //MSL_Availability_StockFacing
                                String mslAvailability_stockFacing_xml = "";
                                onXML = "";
                                db.open();
                                msl_availabilityStockFacingList = db.getMSL_Availability_StockFacing_UploadServerDataNew(coverageList.get(i)
                                                .getStoreId(), coverageList.get(i).getVisitDate(),
                                        coverageList.get(i).getKeyAccountId(),
                                        coverageList.get(i).getStoreTypeId(),
                                        coverageList.get(i).getClassId(),
                                        coverageList.get(i).getMappingStk());
                                if (msl_availabilityStockFacingList.size() > 0) {
                                    for (int j = 0; j < msl_availabilityStockFacingList.size(); j++) {
                                        if (!msl_availabilityStockFacingList.get(j).getSku_id().equals("0")) {
                                            String stock, pricing;
                                            if (!msl_availabilityStockFacingList.get(j).getStock().equals("")) {
                                                stock = msl_availabilityStockFacingList.get(j).getStock();
                                            } else {
                                                stock = "0";
                                            }
                                            if (!msl_availabilityStockFacingList.get(j).getPricingStock().equals("")) {
                                                pricing = msl_availabilityStockFacingList.get(j).getPricingStock();
                                            } else {
                                                pricing = "-1";
                                            }
                                            onXML = "[ST]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    //+ "[USERID]" + userId + "[/USERID]"
                                                    + "[SID]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getSku_id()) + "[/SID]"
                                                    + "[MBQ]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getMbq()) + "[/MBQ]"
                                                    + "[AVL]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getToggleValue()) + "[/AVL]"
                                                    + "[FC]" + Double.parseDouble(msl_availabilityStockFacingList.get(j).getFacing()) + "[/FC]"
                                                    + "[SK]" + Integer.parseInt(stock) + "[/SK]"
                                                    + "[PRICING]" + Double.parseDouble(pricing) + "[/PRICING]"
                                                    // + "[PRICING]" + Integer.parseInt(pricing) + "[/PRICING]"
                                                    + "[ML]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getMust_have()) + "[/ML]" +
                                                    "[/ST]";

                                            mslAvailability_stockFacing_xml = mslAvailability_stockFacing_xml + onXML;
                                        }
                                    }

                                    final String sos_xml = "[DATA]" + mslAvailability_stockFacing_xml + "[/DATA]";
                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "STOCK_AVAILABILITY_WITH_PRICING");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);
                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);
                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);
                                    result = envelope.getResponse();
                                }
                                data.value = 15;
                                data.name = getString(R.string.availability_data_uploading);
                                publishProgress(data);

                                String msl_availabilityStockEntryXml = "";
                                onXML = "";
                                db.open();
                                msl_availabilityStockFacingList = db.getStock_Entry_UploadServerDataNew(coverageList.get(i)
                                                .getStoreId(), coverageList.get(i).getVisitDate(),
                                        coverageList.get(i).getKeyAccountId(),
                                        coverageList.get(i).getStoreTypeId(),
                                        coverageList.get(i).getClassId(),
                                        coverageList.get(i).getMappingStk());

                                if (msl_availabilityStockFacingList.size() > 0) {
                                    for (int j = 0; j < msl_availabilityStockFacingList.size(); j++) {
                                        if (!msl_availabilityStockFacingList.get(j).getSku_id().equals("0")) {
                                            String posm_stock, warehouse_stock;
                                            if (!msl_availabilityStockFacingList.get(j).getPosmStock().equals("")) {
                                                posm_stock = msl_availabilityStockFacingList.get(j).getPosmStock();
                                            } else {
                                                posm_stock = "0";
                                            }
                                            if (!msl_availabilityStockFacingList.get(j).getWarehouseStock().equals("")) {
                                                warehouse_stock = msl_availabilityStockFacingList.get(j).getWarehouseStock();
                                            } else {
                                                warehouse_stock = "0";
                                            }
                                            onXML = "[ST]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USERID]" + userId + "[/USERID]"
                                                    + "[SID]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getSku_id()) + "[/SID]"
                                                    + "[POSM_STOCK]" + Integer.parseInt(posm_stock) + "[/POSM_STOCK]"
                                                    + "[WAREHOUSE_STOCK]" + Integer.parseInt(warehouse_stock) + "[/WAREHOUSE_STOCK]"
                                                    + "[MBQ]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getMbq()) + "[/MBQ]"
                                                    + "[ML]" + Integer.parseInt(msl_availabilityStockFacingList.get(j).getMust_have()) + "[/ML]" +
                                                    "[/ST]";

                                            msl_availabilityStockEntryXml = msl_availabilityStockEntryXml + onXML;
                                        }
                                    }
                                    final String sos_xml = "[DATA]" + msl_availabilityStockEntryXml + "[/DATA]";
                                    Log.d("STOCK_ENTRY_DATA", sos_xml);
                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "STOCK_ENTRY");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 16;
                                data.name = getString(R.string.stock_entry_data_uploading);
                                publishProgress(data);


                                //Promo Compliance - Promotion Data
                                String promoition_data_xml = "";
                                onXML = "";
                                db.open();
                                promotionSkuList = db.getPromoComplianceSkuServerUploadData(coverageList.get(i).getStoreId());

                                if (promotionSkuList.size() > 0) {
                                    for (int i1 = 0; i1 < promotionSkuList.size(); i1++) {
                                        if (!promotionSkuList.get(i1).getCategory_id().equals("0")) {

                                            onXML = "[PROMOTION_DATA_NEW]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USER_ID]" + userId + "[/USER_ID]"
                                                    + "[CATEGORY_ID]" + Integer.parseInt(promotionSkuList.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                    + "[PROMO_ID]" + Integer.parseInt(promotionSkuList.get(i1).getPromo_id()) + "[/PROMO_ID]"
                                                    //+ "[SKU]" + promotionSkuList.get(i1).getSku() + "[/SKU]"
                                                    + "[SKU_ID]" + Integer.parseInt(promotionSkuList.get(i1).getSku_id()) + "[/SKU_ID]"
                                                    + "[IN_STOCK]" + Integer.parseInt(promotionSkuList.get(i1).getIn_stock()) + "[/IN_STOCK]"
                                                    + "[PROMO_ANNOUNCER]" + Integer.parseInt(promotionSkuList.get(i1).getPromo_announcer()) + "[/PROMO_ANNOUNCER]"
                                                    + "[RUNNING_POS]" + Integer.parseInt(promotionSkuList.get(i1).getRunning_pos()) + "[/RUNNING_POS]"
                                                    + "[PROMO_IMAGE]" + promotionSkuList.get(i1).getImage_promotion() + "[/PROMO_IMAGE]"
                                                    + "[/PROMOTION_DATA_NEW]";

                                            promoition_data_xml = promoition_data_xml + onXML;
                                        }
                                    }

                                    final String sos_xml = "[DATA]" + promoition_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "PROMOTION_DATA_NEW");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 30;
                                data.name = getString(R.string.promo_data_uploading);
                                publishProgress(data);

                                //Promo Compliance - Country Id - 7 (SA) Promotion Data
                                String promotion_sku_wise_data_xml = "", add_promotion_data_xml = "", exists_xml = "", add_xml = "";

                                db.open();
                                promoComplianceSkuwiseExistsGetterSetters = db.getPromoComplianceSkuwiseExistsAfterData(coverageList.get(i).getStoreId(), null);
                                if (promoComplianceSkuwiseExistsGetterSetters.size() > 0) {

                                    for (int l = 0; l < promoComplianceSkuwiseExistsGetterSetters.size(); l++) {

                                        promoComplianceSkuwiseGetterSetters = db.getPromoComplianceSkuwiseAfterData(promoComplianceSkuwiseExistsGetterSetters.get(l).get_id(), null);

                                        for (int i1 = 0; i1 < promoComplianceSkuwiseGetterSetters.size(); i1++) {
                                            if (!promoComplianceSkuwiseGetterSetters.get(i1).getCategory_id().equals("0")) {

                                                String discount_val = promoComplianceSkuwiseGetterSetters.get(i1).getDiscount_value();
                                                if (discount_val.equals("")) {
                                                    discount_val = "0";
                                                }

                                                add_xml = "[PROMOTION_DATA]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USER_ID]" + userId + "[/USER_ID]"
                                                        + "[CATEGORY_ID]" + Integer.parseInt(promoComplianceSkuwiseGetterSetters.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                        + "[SUB_CATEGORY_ID]" + Integer.parseInt(promoComplianceSkuwiseGetterSetters.get(i1).getSub_category_id()) + "[/SUB_CATEGORY_ID]"
                                                        + "[SKU_ID]" + Integer.parseInt(promoComplianceSkuwiseGetterSetters.get(i1).getSku_id()) + "[/SKU_ID]"
                                                        + "[BRAND_ID]" + Integer.parseInt(promoComplianceSkuwiseGetterSetters.get(i1).getBrand_id()) + "[/BRAND_ID]"
                                                        + "[DISCOUNT_VALUE]" + discount_val + "[/DISCOUNT_VALUE]"
                                                        + "[PRICE]" + promoComplianceSkuwiseGetterSetters.get(i1).getPrice() + "[/PRICE]"
                                                        + "[PROMO_IMAGE]" + promoComplianceSkuwiseGetterSetters.get(i1).getImg() + "[/PROMO_IMAGE]"
                                                        + "[PRICE_FLASHER_AVAILABLE]" + promoComplianceSkuwiseGetterSetters.get(i1).getPrice_flasher() + "[/PRICE_FLASHER_AVAILABLE]"
                                                        + "[/PROMOTION_DATA]";

                                                add_promotion_data_xml = add_promotion_data_xml + add_xml;
                                            }
                                        }

                                        int exists = 0;
                                        if (promoComplianceSkuwiseExistsGetterSetters.get(l).isExists()) {
                                            exists = 1;
                                        }

                                        exists_xml = "[ADDITIONAL_PROMOTION_SKUWISE_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[CATEGORY_ID]" + Integer.parseInt(promoComplianceSkuwiseExistsGetterSetters.get(l).getCategory_id()) + "[/CATEGORY_ID]"
                                                + "[IS_EXISTS]" + exists + "[/IS_EXISTS]"
                                                + "[ADDITIONAL_PROMOTION_DATA]" + add_promotion_data_xml + "[/ADDITIONAL_PROMOTION_DATA]"
                                                + "[/ADDITIONAL_PROMOTION_SKUWISE_DATA]";

                                        promotion_sku_wise_data_xml = promotion_sku_wise_data_xml + exists_xml;

                                    }

                                    final String sos_xml = "[DATA]" + promotion_sku_wise_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "ADDITIONAL_PROMOTION_SKUWISE_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }


                                data.value = 30;
                                data.name = getString(R.string.promo_data_uploading);
                                publishProgress(data);


                                //Promo Compliance -Additional Promotion Data
                                String additional_promoition_data_xml = "";
                                onXML = "";
                                db.open();
                                additionalPromotionList = db.getAdditionalPromotionServerUploadData(coverageList.get(i).getStoreId());

                                if (additionalPromotionList.size() > 0) {
                                    for (int i1 = 0; i1 < additionalPromotionList.size(); i1++) {
                                        if (!additionalPromotionList.get(i1).getCategory_id().equals("0")) {

                                            onXML = "[ADDITIONAL_PROMOTION_DATA_NEW]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USER_ID]" + userId + "[/USER_ID]"
                                                    + "[CATEGORY_ID]" + Integer.parseInt(additionalPromotionList.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                    + "[PROMO_ID]" + Integer.parseInt(additionalPromotionList.get(i1).getPromo_id()) + "[/PROMO_ID]"
                                                    + "[SKU_ID]" + Integer.parseInt(additionalPromotionList.get(i1).getSku_id()) + "[/SKU_ID]"
                                                    //+ "[SKU]" + additionalPromotionList.get(i1).getSku() + "[/SKU]"
                                                    + "[IN_STOCK]" + Integer.parseInt(additionalPromotionList.get(i1).getIn_stock()) + "[/IN_STOCK]"
                                                    + "[PROMO_ANNOUNCER]" + Integer.parseInt(additionalPromotionList.get(i1).getPromo_announcer()) + "[/PROMO_ANNOUNCER]"
                                                    + "[RUNNING_POS]" + Integer.parseInt(additionalPromotionList.get(i1).getRunning_pos()) + "[/RUNNING_POS]"
                                                    + "[ADD_PROMO_IMAGE]" + additionalPromotionList.get(i1).getImage_promotion() + "[/ADD_PROMO_IMAGE]"
                                                    + "[/ADDITIONAL_PROMOTION_DATA_NEW]";

                                            additional_promoition_data_xml = additional_promoition_data_xml + onXML;
                                        }
                                    }

                                    final String sos_xml = "[DATA]" + additional_promoition_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "ADDITIONAL_PROMOTION_DATA_NEW");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 35;
                                data.name = getString(R.string.additional_data_uploading);
                                publishProgress(data);


                                //Additional Visibility  Data
                                String additional_visibility_data_xml = "";
                                String additional_visibility_dialog_xml = "";
                                onXML = "";
                                String onXMLdIALOG = "";
                                String imageV1, imageV2, imageV3;

                                db.open();
                                additionalVisibilityList = db.getAdditionalStockUpload(coverageList.get(i).getStoreId());

                                if (additionalVisibilityList.size() > 0) {
                                    for (int J = 0; J < additionalVisibilityList.size(); J++) {

                                        if (additionalVisibilityList.get(J).getImage() == null) {
                                            imageV1 = "";
                                        } else {
                                            imageV1 = additionalVisibilityList.get(J).getImage();
                                        }

                                        if (additionalVisibilityList.get(J).getImage2() == null) {
                                            imageV2 = "";
                                        } else {
                                            imageV2 = additionalVisibilityList.get(J).getImage2();
                                        }

                                        if (additionalVisibilityList.get(J).getImage3() == null) {
                                            imageV3 = "";
                                        } else {
                                            imageV3 = additionalVisibilityList.get(J).getImage3();
                                        }

                                        String KeyID = additionalVisibilityList.get(J).getKey_id();

                                        additionalVisibilitySkuList = db.getDialogStockUpload(KeyID);
                                        //additionalVisibilitySkuList = additionalVisibilityList.get(J).getSkuDialogList();

                                        if (additionalVisibilitySkuList.size() > 0) {
                                            for (int k = 0; k < additionalVisibilitySkuList.size(); k++) {

                                                onXMLdIALOG = "[VISIBILITY_DAILOG]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USER_ID]" + userId + "[/USER_ID]"
                                                        + "[KEY_ID]" + additionalVisibilitySkuList.get(k).getCOMMON_ID() + "[/KEY_ID]"
                                                        + "[CATEGORY_ID]" + additionalVisibilitySkuList.get(k).getCategoryId() + "[/CATEGORY_ID]"
                                                        + "[SKU_ID]" + additionalVisibilitySkuList.get(k).getSku_id() + "[/SKU_ID]"
                                                        + "[QUANTITY]" + additionalVisibilitySkuList.get(k).getQuantity() + "[/QUANTITY]"
                                                        + "[/VISIBILITY_DAILOG]";

                                                additional_visibility_dialog_xml = additional_visibility_dialog_xml + onXMLdIALOG;
                                            }
                                        }

                                        onXML = "[ADDITIONAL_VISIBILITY_NEW]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[KEY_ID]" + additionalVisibilityList.get(J).getKey_id() + "[/KEY_ID]"
                                                + "[CATEGORY_ID]" + additionalVisibilityList.get(J).getCategoryId() + "[/CATEGORY_ID]"
                                                + "[ADDITIONAL_DISPLAY]" + additionalVisibilityList.get(J).getBtn_toogle() + "[/ADDITIONAL_DISPLAY]"
                                                //+ "[BRAND_ID]"+ additionalVisibilityList.get(J).getBrand_id()+ "[/BRAND_ID]"
                                                + "[IMAGE_URL]" + imageV1 /*additionalVisibilityList.get(J).getImage()*/ + "[/IMAGE_URL]"
                                                + "[IMAGE_URL1]" + imageV2 /*additionalVisibilityList.get(J).getImage2()*/ + "[/IMAGE_URL1]"
                                                + "[IMAGE_URL2]" + imageV3 /*additionalVisibilityList.get(J).getImage3()*/ + "[/IMAGE_URL2]"
                                                + "[DISPLAY_ID]" + additionalVisibilityList.get(J).getSku_id() + "[/DISPLAY_ID]"
                                                + "[SKU_LIST]" + additional_visibility_dialog_xml + "[/SKU_LIST]"
                                                + "[/ADDITIONAL_VISIBILITY_NEW]";
                                        //Here getSku_id gives display_id

                                        additional_visibility_data_xml = additional_visibility_data_xml + onXML;
                                        KeyID = "";
                                        additionalVisibilitySkuList.clear();

                                        additional_visibility_dialog_xml = "";
                                    }

                                    final String sos_xml = "[DATA]" + additional_visibility_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "ADDITIONAL_VISIBILITY_NEW");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 45;
                                data.name = getString(R.string.additional_data_uploading);
                                publishProgress(data);


                                //T2p Upload Data
                                String t2p_data_xml = "";
                                onXML = "";
                                db.open();
                                t2PGetterSetters = db.getT2pComplianceData(coverageList.get(i).getStoreId(), null);

                                if (t2PGetterSetters.size() > 0) {
                                    for (int i1 = 0; i1 < t2PGetterSetters.size(); i1++) {

                                        ArrayList<GapsChecklistGetterSetter> gapsList = db.getGapsData(t2PGetterSetters.get(i1).getKey_id());
                                        ArrayList<SkuGetterSetter> skuList = db.getT2PSKUData(t2PGetterSetters.get(i1).getKey_id());
                                        ArrayList<BrandAvabilityGetterSetter> brandList = db.getT2BrandData(t2PGetterSetters.get(i1).getKey_id());
                                        String gaps_xml = "";
                                        String gaps_child;

                                        for (int l = 0; l < gapsList.size(); l++) {
                                            String present = "";
                                            if (gapsList.get(l).isPresent()) {
                                                present = "1";
                                            } else {
                                                present = "0";
                                            }
                                            gaps_child = "[GAPS]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USER_ID]" + userId + "[/USER_ID]"
                                                    + "[CHECK_LIST_ID]" + gapsList.get(l).getChecklist_id() + "[/CHECK_LIST_ID]"
                                                    + "[DISPLAY_ID]" + gapsList.get(l).getDisplay_id() + "[/DISPLAY_ID]"
                                                    + "[PRESENT]" + present + "[/PRESENT]"
                                                    + "[COMMON_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getKey_id()) + "[/COMMON_ID]"
                                                    + "[/GAPS]";
                                            gaps_xml = gaps_xml + gaps_child;
                                        }

                                        String sku_xml = "";
                                        String sku_child;

                                        for (int k = 0; k < skuList.size(); k++) {
                                            sku_child = "[SKU]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USER_ID]" + userId + "[/USER_ID]"
                                                    + "[SKU_ID]" + skuList.get(k).getSKU_ID() + "[/SKU_ID]"
                                                    + "[BRAND_ID]" + skuList.get(k).getBRAND_ID() + "[/BRAND_ID]"
                                                    + "[STOCK]" + skuList.get(k).getSTOCK() + "[/STOCK]"
                                                    + "[COMMON_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getKey_id()) + "[/COMMON_ID]"
                                                    + "[/SKU]";
                                            sku_xml = sku_xml + sku_child;
                                        }


                                        String brandxml = "";
                                        String brandchild;

                                        if (brandList.size() > 0) {

                                            for (int M = 0; M < brandList.size(); M++) {
                                                brandchild = "[BRAND]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USER_ID]" + userId + "[/USER_ID]"
                                                        + "[BRAND_ID]" + brandList.get(M).getBRAND_ID() + "[/BRAND_ID]"
                                                        // + "[BRAND]" + brandList.get(M).getBRAND() + "[/BRAND]"
                                                        + "[COMMON_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getKey_id()) + "[/COMMON_ID]"
                                                        + "[/BRAND]";
                                                brandxml = brandxml + brandchild;
                                            }
                                        }

                                   /* String present = "";
                                    if (t2PGetterSetters.get(i1).isPresent()) {
                                        present = "1";
                                    } else {
                                        present = "0";
                                    }*/
                                        String qty = t2PGetterSetters.get(i1).getQty();
                                        if (qty.equals("")) {
                                            qty = "0";
                                        }

                                        onXML = "[T2P_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[CATEGORY_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                + "[BRAND_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getBrand_id()) + "[/BRAND_ID]"
                                                + "[DISPLAY_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getDisplay_id()) + "[/DISPLAY_ID]"
                                                + "[COMMON_ID]" + Integer.parseInt(t2PGetterSetters.get(i1).getKey_id()) + "[/COMMON_ID]"
                                                + "[IMAGE]" + t2PGetterSetters.get(i1).getImage() + "[/IMAGE]"
                                                + "[IMAGE1]" + t2PGetterSetters.get(i1).getImage1() + "[/IMAGE1]"
                                                + "[IMAGE2]" + t2PGetterSetters.get(i1).getImage2() + "[/IMAGE2]"
                                                + "[PRESENT]" + t2PGetterSetters.get(i1).getPresent() + "[/PRESENT]"

                                                //Quantity visibility added for UAE 04.07.2019
                                                + "[Quantity]" + qty + "[/Quantity]"

                                                //Non t2p reason id added - 21-6-2018
                                                + "[TREASON_ID]" + t2PGetterSetters.get(i1).getNon_t2p_reason_id() + "[/TREASON_ID]"

                                                + "[GAPS_DATA]" + gaps_xml + "[/GAPS_DATA]"
                                                + "[SKU_DATA]" + sku_xml + "[/SKU_DATA]"
                                                + "[CATEGORY_FIXTURE]" + t2PGetterSetters.get(i1).getCategory_fixture() + "[/CATEGORY_FIXTURE]"
                                                + "[BRAND_DATA]" + brandxml + "[/BRAND_DATA]"
                                                + "[/T2P_DATA]";

                                        t2p_data_xml = t2p_data_xml + onXML;
                                    }

                                    final String t2p_final_xml = "[DATA]" + t2p_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", t2p_final_xml);
                                    request.addProperty("KEYS", "T2P_DATA_FIXTURE_NEW");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 50;
                                data.name = getString(R.string.t2p_data_uploading);
                                publishProgress(data);


                                //Stock Facing Planogram Tracker
                                String stock_facing_planogram_xml = "";
                                onXML = "";
                                db.open();
                                stockFacingPlanogramDataList = db.getStockAndFacingPlanogramServerUploadData(coverageList.get(i).getStoreId());

                                if (stockFacingPlanogramDataList.size() > 0) {

                                    for (int i1 = 0; i1 < stockFacingPlanogramDataList.size(); i1++) {
                                        onXML = "[STOCK_FACING_PLANOGRAM_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[SKU_ID]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getSku_id()) + "[/SKU_ID]"
                                                + "[CATEGORY_ID]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                //+ "[company_id]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getCompany_id()) + "[/company_id]"
                                                //+ "[sub_category_id]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getSub_category_id()) + "[/sub_category_id]"
                                                //+ "[BRAND_ID]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getBrand_id()) + "[/BRAND_ID]"
                                                + "[SHELF_ID]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getSp_addShelf_id()) + "[/SHELF_ID]"
                                                + "[SHELF_POSITION]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getSp_shelfPosition()) + "[/SHELF_POSITION]"
                                                //+ "[CHECKBOX]" + Integer.parseInt(stockFacingPlanogramDataList.get(i1).getCheckbox_sku()) + "[/CHECKBOX]"
                                                + "[/STOCK_FACING_PLANOGRAM_DATA]";

                                        stock_facing_planogram_xml = stock_facing_planogram_xml + onXML;
                                    }

                                    final String sos_xml = "[DATA]" + stock_facing_planogram_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "STOCK_FACING_PLANOGRAM_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }
                                data.value = 55;
                                data.name = getString(R.string.stock_planogram_data_uploading);
                                publishProgress(data);


                                //Category Picture  Data
                                String Category_xml = "";
                                onXML = "";
                                String onCategoryList = "";
                                String CategoryLISTDATA = "";
                                String image1, image2, image3, image4;

                                db.open();
                                adddata = db.getCategoryPictureUpload(coverageList.get(i).getStoreId());

                                if (adddata.size() > 0) {
                                    for (int J = 0; J < adddata.size(); J++) {
                                        if (adddata.get(J).getCategoryImage1() == null) {
                                            image1 = "";
                                        } else {
                                            image1 = adddata.get(J).getCategoryImage1();
                                        }

                                        if (adddata.get(J).getCategoryImage2() == null) {
                                            image2 = "";
                                        } else {
                                            image2 = adddata.get(J).getCategoryImage2();
                                        }

                                        if (adddata.get(J).getCategoryImage3() == null) {
                                            image3 = "";
                                        } else {
                                            image3 = adddata.get(J).getCategoryImage3();
                                        }

                                        if (adddata.get(J).getCategoryImage4() == null) {
                                            image4 = "";
                                        } else {
                                            image4 = adddata.get(J).getCategoryImage4();
                                        }

                                        String KeyID = adddata.get(J).getKEY_ID();

                                        listdat = db.getCategoryPictureListUploaded(KeyID);

                                        if (listdat.size() > 0) {
                                            for (int k = 0; k < listdat.size(); k++) {
                                                onCategoryList = "[SUB_CATEGORY_LIST]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USER_ID]" + userId + "[/USER_ID]"
                                                        + "[KEY_ID]" + listdat.get(k).getCOMMON_ID() + "[/KEY_ID]"
                                                        + "[SUB_CATEGORY_ID]" + listdat.get(k).getSUB_CATEGORY_ID() + "[/SUB_CATEGORY_ID]"
                                                        + "[SUB_CategoryImage1]" + listdat.get(k).getSubCategoryCamera1() + "[/SUB_CategoryImage1]"
                                                        + "[SUB_CategoryImage2]" + listdat.get(k).getSubCategoryCamera2() + "[/SUB_CategoryImage2]"
                                                        + "[/SUB_CATEGORY_LIST]";

                                                CategoryLISTDATA = CategoryLISTDATA + onCategoryList;
                                            }
                                        }

                                        onXML = "[CATEGORY_LIST]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[KEY_ID]" + adddata.get(J).getKEY_ID() + "[/KEY_ID]"
                                                + "[CATEGORY_ID]" + adddata.get(J).getCategoryId() + "[/CATEGORY_ID]"
                                                + "[CAMERA_ALLOW]" + adddata.get(J).getCamera_allow() + "[/CAMERA_ALLOW]"
                                                + "[IMAGE_URL_1]" + image1 /*adddata.get(J).getCategoryImage1()*/ + "[/IMAGE_URL_1]"
                                                + "[IMAGE_URL_2]" + image2 /*adddata.get(J).getCategoryImage2()*/ + "[/IMAGE_URL_2]"
                                                + "[IMAGE_URL_3]" + image3 /*adddata.get(J).getCategoryImage3()*/ + "[/IMAGE_URL_3]"
                                                + "[IMAGE_URL_4]" + image4 /*adddata.get(J).getCategoryImage4()*/ + "[/IMAGE_URL_4]"
                                                + "[SUB_LIST]" + CategoryLISTDATA + "[/SUB_LIST]"
                                                + "[/CATEGORY_LIST]";

                                        Category_xml = Category_xml + onXML;
                                        CategoryLISTDATA = "";
                                    }

                                    final String sos_xml = "[DATA]" + Category_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "CATEGORY_PICTURE");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                } else {

                                    db.open();
                                    ArrayList<NoCameraDataGetterSetter> nocamera_data_list = db.getNoCameraCategoryStoreWiseDataInserted(coverageList.get(i).getStoreId());

                                    if (nocamera_data_list.size() > 0) {

                                        String onXml_nodata = "", No_camera_data = "";
                                        for (int n = 0; n < nocamera_data_list.size(); n++) {

                                            int vertical;
                                            if (nocamera_data_list.get(n).isVertical()) {
                                                vertical = 1;
                                            } else {
                                                vertical = 0;
                                            }

                                            onXml_nodata = "[NO_CAMERA_POG_DATA]"
                                                    + "[MID]" + mid + "[/MID]"
                                                    + "[USER_ID]" + userId + "[/USER_ID]"
                                                    + "[CATEGORY_ID]" + nocamera_data_list.get(n).getCATEGORY_ID() + "[/CATEGORY_ID]"
                                                    + "[SUB_CATEGORY_ID]" + nocamera_data_list.get(n).getSUB_CATEGORY_ID() + "[/SUB_CATEGORY_ID]"
                                                    + "[SKU_GROUP_ID]" + nocamera_data_list.get(n).getSKUGROUP_ID() + "[/SKU_GROUP_ID]"
                                                    + "[ROW_NO]" + nocamera_data_list.get(n).getRow_no() + "[/ROW_NO]"
                                                    + "[COLUMN_NO]" + nocamera_data_list.get(n).getColumn_no() + "[/COLUMN_NO]"
                                                    + "[FACING]" + nocamera_data_list.get(n).getFacing() + "[/FACING]"
                                                    + "[IS_VERTICAL]" + vertical + "[/IS_VERTICAL]"
                                                    + "[/NO_CAMERA_POG_DATA]";

                                            No_camera_data = No_camera_data + onXml_nodata;
                                        }

                                        final String sos_xml = "[DATA]" + No_camera_data + "[/DATA]";

                                        request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                        request.addProperty("XMLDATA", sos_xml);
                                        request.addProperty("KEYS", "NO_CAMERA_POG");
                                        request.addProperty("USERNAME", userId);
                                        request.addProperty("MID", mid);

                                        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                        envelope.dotNet = true;
                                        envelope.setOutputSoapObject(request);

                                        androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                        androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                        result = envelope.getResponse();
                                    }

                                }
                                data.value = 60;
                                data.name = getString(R.string.additional_data_uploading);
                                publishProgress(data);

                                //Audit data
                                db.open();
                                ArrayList<AuditDataGetterSetter> audit_data = db.getAuditDataForUpload(coverageList.get(i).getStoreId());

                                if (audit_data.size() > 0) {

                                    String onXml_audit_data = "", Audit_data = "";
                                    for (int n = 0; n < audit_data.size(); n++) {

                                        onXml_audit_data = "[AUDIT_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[CATEGORY_ID]" + audit_data.get(n).getCATEGORY_ID() + "[/CATEGORY_ID]"
                                                + "[QUESTION_ID]" + audit_data.get(n).getQUESTION_ID() + "[/QUESTION_ID]"
                                                + "[ANSWER_ID]" + audit_data.get(n).getANSWER_ID() + "[/ANSWER_ID]"
                                                + "[ANSWER_IMAGE]" + audit_data.get(n).getCAM_IMAGE() + "[/ANSWER_IMAGE]"
                                                + "[ANSWER_IMAGE2]" + audit_data.get(n).getCAM_IMAGE2() + "[/ANSWER_IMAGE2]"
                                                + "[ANSWER]" + audit_data.get(n).getANSWER() + "[/ANSWER]"
                                                + "[REMARK]" + audit_data.get(n).getTXT_REMARK() + "[/REMARK]"
                                                + "[/AUDIT_DATA]";

                                        Audit_data = Audit_data + onXml_audit_data;
                                    }

                                    final String sos_xml = "[DATA]" + Audit_data + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "AUDIT_DATA_NEW_REMARK");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();
                                }

                                data.value = 62;
                                data.name = getString(R.string.audit);
                                publishProgress(data);

                                //POG data
                                db.open();
                                ArrayList<POGGetterSetter> pog_data = db.getAfterSavePOGForUploadData(coverageList.get(i).getStoreId());

                                if (pog_data.size() > 0) {

                                    String onXml_pog_data = "", POG_data = "";
                                    for (int n = 0; n < pog_data.size(); n++) {

                                        onXml_pog_data = "[POG_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[CATEGORY_ID]" + pog_data.get(n).getCATEGORY_ID() + "[/CATEGORY_ID]"
                                                + "[SUB_CATEGORY_ID]" + pog_data.get(n).getSUB_CATEGORY_ID() + "[/SUB_CATEGORY_ID]"
                                                + "[QUESTION_ID]" + pog_data.get(n).getQUESTION_ID() + "[/QUESTION_ID]"
                                                + "[ANSWER_ID]" + pog_data.get(n).getANSWER_ID() + "[/ANSWER_ID]"
                                                + "[/POG_DATA]";

                                        POG_data = POG_data + onXml_pog_data;
                                    }

                                    final String sos_xml = "[DATA]" + POG_data + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "POG_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();
                                }

                                data.value = 63;
                                data.name = getString(R.string.pog);
                                publishProgress(data);

                                //Coaching visit data
                                db.open();
                                coachingVisitData = db.getCoachingVisitData(coverageList.get(i).getStoreId());

                                if (coachingVisitData.size() > 0) {

                                    String onXml_coaching_data = "", coaching_xml = "";
                                    for (int j = 0; j < coachingVisitData.size(); j++) {

                                        String exist;

                                        if (coachingVisitData.get(j).isExists()) {
                                            exist = "1";
                                        } else {
                                            exist = "0";
                                        }

                                        coaching_xml = "[COACHING_VISIT_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[EMP_ID]" + coachingVisitData.get(j).getEmp_id() + "[/EMP_ID]"
                                                + "[IMAGE]" + coachingVisitData.get(j).getImg_path() + "[/IMAGE]"
                                                + "[IS_EXIST]" + exist + "[/IS_EXIST]"
                                                + "[/COACHING_VISIT_DATA]";
                                        onXml_coaching_data = onXml_coaching_data + coaching_xml;
                                    }

                                    final String sos_xml = "[DATA]" + onXml_coaching_data + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "COACHING_VISIT_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                    data.value = 64;
                                    data.name = getString(R.string.coaching_visit);
                                    publishProgress(data);
                                }

                                //Coaching check visit data
                                db.open();
                                coachingVisitData = db.getCoachingVisitStoreData(coverageList.get(i).getStoreId());

                                if (coachingVisitData.size() > 0) {

                                    String onXml_coaching_data = "", coaching_xml = "";
                                    for (int j = 0; j < coachingVisitData.size(); j++) {

                                        String exist;

                                        if (coachingVisitData.get(j).isExists()) {
                                            exist = "1";
                                        } else {
                                            exist = "0";
                                        }

                                        coaching_xml = "[CHECK_VISIT_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[EMP_ID]" + coachingVisitData.get(j).getEmp_id() + "[/EMP_ID]"
                                                + "[IMAGE]" + coachingVisitData.get(j).getImg_path() + "[/IMAGE]"
                                                + "[IS_EXIST]" + exist + "[/IS_EXIST]"
                                                + "[/CHECK_VISIT_DATA]";
                                        onXml_coaching_data = onXml_coaching_data + coaching_xml;
                                    }

                                    final String sos_xml = "[DATA]" + onXml_coaching_data + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "CHECK_VISIT_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                    data.value = 65;
                                    data.name = getString(R.string.coaching_visit);
                                    publishProgress(data);
                                }

                                //Counterfeit Product - Country Id - 6 (UAE)
                                String counterfeit_product_data_xml = "", add_product_data_xml = "", product_exists_xml = "", product_add_xml = "";

                                db.open();
                                counterfeitProductExistsGetterSetters = db.getCounterfeitProductExistsAfterData(coverageList.get(i).getStoreId(), null);
                                if (counterfeitProductExistsGetterSetters.size() > 0) {

                                    for (int l = 0; l < counterfeitProductExistsGetterSetters.size(); l++) {

                                        add_product_data_xml = "";

                                        counterfeitProductGetterSetters = db.getCounterfeitProductAfterData(counterfeitProductExistsGetterSetters.get(l).get_id(), null);

                                        for (int i1 = 0; i1 < counterfeitProductGetterSetters.size(); i1++) {
                                            if (!counterfeitProductGetterSetters.get(i1).getCategory_id().equals("0")) {

                                                product_add_xml = "[PRODUCT_DATA]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USER_ID]" + userId + "[/USER_ID]"
                                                        + "[CATEGORY_ID]" + Integer.parseInt(counterfeitProductGetterSetters.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                        + "[SKU_NAME]" + counterfeitProductGetterSetters.get(i1).getSkuName() + "[/SKU_NAME]"
                                                        + "[BRAND_ID]" + counterfeitProductGetterSetters.get(i1).getBrandId() + "[/BRAND_ID]"
                                                        + "[STOCK]" + counterfeitProductGetterSetters.get(i1).getStock() + "[/STOCK]"
                                                        + "[IMAGE]" + counterfeitProductGetterSetters.get(i1).getImgStr() + "[/IMAGE]"
                                                        + "[IMAGE1]" + counterfeitProductGetterSetters.get(i1).getImgStr1() + "[/IMAGE1]"
                                                        + "[IMAGE2]" + counterfeitProductGetterSetters.get(i1).getImgStr2() + "[/IMAGE2]"
                                                        + "[/PRODUCT_DATA]";

                                                add_product_data_xml = add_product_data_xml + product_add_xml;
                                            }
                                        }

                                        int exists = 0;
                                        if (counterfeitProductExistsGetterSetters.get(l).isExists()) {
                                            exists = 1;
                                        }

                                        product_exists_xml = "[COUNTERFEIT_PRODUCT_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[CATEGORY_ID]" + Integer.parseInt(counterfeitProductExistsGetterSetters.get(l).getCategory_id()) + "[/CATEGORY_ID]"
                                                + "[IS_EXISTS]" + exists + "[/IS_EXISTS]"
                                                + "[COUNTERFEIT_DATA]" + add_product_data_xml + "[/COUNTERFEIT_DATA]"
                                                + "[/COUNTERFEIT_PRODUCT_DATA]";

                                        counterfeit_product_data_xml = counterfeit_product_data_xml + product_exists_xml;

                                    }

                                    final String sos_xml = "[DATA]" + counterfeit_product_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "COUNTERFEIT_PRODUCT_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }

                                data.value = 65;
                                data.name = getString(R.string.title_activity_counterfeit_products);
                                publishProgress(data);

                                //usk upload
                                //Competitomn promo - Country Id - 6 (egipt)
                                String competiton_data_xml = "", add_competiton_data_xml = "", competiton_exists_xml = "", competiton_add_xml = "";

                                db.open();
                                competitonExistsGetterSetters = db.getCompetitionPromoExistsAfterData(coverageList.get(i).getStoreId(), null);
                                if (competitonExistsGetterSetters.size() > 0) {

                                    for (int l = 0; l < competitonExistsGetterSetters.size(); l++) {

                                        add_competiton_data_xml = "";

                                        competitonGetterSetters = db.getCometionPromoAfterData(competitonExistsGetterSetters.get(l).get_id(), null);

                                        for (int i1 = 0; i1 < competitonGetterSetters.size(); i1++) {
                                            if (!competitonGetterSetters.get(i1).getCategory_id().equals("0")) {

                                                competiton_add_xml = "[COMPETITION_DATA]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USER_ID]" + userId + "[/USER_ID]"
                                                        + "[CATEGORY_ID]" + Integer.parseInt(competitonGetterSetters.get(i1).getCategory_id()) + "[/CATEGORY_ID]"
                                                        + "[COMMENT]" + competitonGetterSetters.get(i1).getSkuName() + "[/COMMENT]"
                                                        + "[BRAND_ID]" + competitonGetterSetters.get(i1).getBrandId() + "[/BRAND_ID]"
                                                        + "[IMAGE1]" + competitonGetterSetters.get(i1).getImgStr() + "[/IMAGE1]"
                                                        + "[PROMO_TYPE]" + competitonGetterSetters.get(i1).getPromotion_id() + "[/PROMO_TYPE]"
                                                        + "[/COMPETITION_DATA]";

                                                add_competiton_data_xml = add_competiton_data_xml + competiton_add_xml;
                                            }
                                        }

                                        int exists = 0;
                                        if (competitonExistsGetterSetters.get(l).isExists()) {
                                            exists = 1;
                                        }

                                        competiton_exists_xml = "[COMPETITION_PROMOTION_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USER_ID]" + userId + "[/USER_ID]"
                                                + "[CATEGORY_ID]" + Integer.parseInt(competitonExistsGetterSetters.get(l).getCategory_id()) + "[/CATEGORY_ID]"
                                                + "[IS_EXISTS]" + exists + "[/IS_EXISTS]"
                                                + "[COMPETITION_DATA]" + add_competiton_data_xml + "[/COMPETITION_DATA]"
                                                + "[/COMPETITION_PROMOTION_DATA]";

                                        competiton_data_xml = competiton_data_xml + competiton_exists_xml;
                                    }

                                    final String sos_xml = "[DATA]" + competiton_data_xml + "[/DATA]";

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "COMPETITION_PROMOTION_DATA");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();
                                }

                                data.value = 68;
                                data.name = getString(R.string.title_activity_competition_promo);
                                publishProgress(data);


                                String order_taking_xml = "";
                                String _onxml = "";
                                String categoryxml = "";
                                db.open();
                                ArrayList<MSL_AvailabilityStockFacingGetterSetter> ordertakingdata = db.getOrder_TAKING_UploadServerData(coverageList.get(i).getStoreId());
                                db.open();
                                ArrayList<MSL_AvailabilityStockFacingGetterSetter> getOrder_TAKING_CategoryData = db.getOrder_TAKING_CategoryData(coverageList.get(i).getStoreId());

                                if (ordertakingdata.size() > 0) {
                                    for (int k = 0; k < getOrder_TAKING_CategoryData.size(); k++) {
                                        _onxml = "[CATEGORY_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[PRESENT]" + getOrder_TAKING_CategoryData.get(k).getIsExist() + "[/PRESENT]"
                                                + "[CATEGORY_ID]" + getOrder_TAKING_CategoryData.get(k).getCategory_id() + "[/CATEGORY_ID]"
                                                + "[/CATEGORY_DATA]";
                                        categoryxml = categoryxml + _onxml;
                                    }

                                    String cat_xml = categoryxml;
                                    for (int j = 0; j < ordertakingdata.size(); j++) {
                                        if (ordertakingdata.get(j).getIsExist() == 1) {
                                            if (!ordertakingdata.get(j).getFacing().equals("0") &&
                                                    !ordertakingdata.get(j).getFacing().equals("")) {
                                                onXML = "[ORDER_TAKING]"
                                                        + "[MID]" + mid + "[/MID]"
                                                        + "[USERID]" + userId + "[/USERID]"
                                                        + "[CATEGORY_ID]" + ordertakingdata.get(j).getCategory_id() + "[/CATEGORY_ID]"
                                                        + "[SYSTEM_PO_NUM]" + ordertakingdata.get(j).getSys_PO_No() + "[/SYSTEM_PO_NUM]"
                                                        + "[STORE_PO_NUM]" + ordertakingdata.get(j).getStore_PO_No() + "[/STORE_PO_NUM]"
                                                        + "[PO_DATE]" + ordertakingdata.get(j).getPO_Date() + "[/PO_DATE]"
                                                        + "[BRAND_ID]" + ordertakingdata.get(j).getBrand_id() + "[/BRAND_ID]"
                                                        + "[SKU_ID]" + ordertakingdata.get(j).getSku_id() + "[/SKU_ID]"
                                                        + "[ORDER_QTY]" + ordertakingdata.get(j).getFacing() + "[/ORDER_QTY]"
                                                        + "[PO_IMAGE]" + ordertakingdata.get(j).getPO_Img() + "[/PO_IMAGE]" +
                                                        "[/ORDER_TAKING]";
                                                order_taking_xml = order_taking_xml + onXML;
                                            }
                                        }
                                    }
                                    String data_xml = cat_xml + order_taking_xml;

                                    final String sos_xml = "[DATA]" + data_xml + "[/DATA]";
                                    Log.e("order_xml_data", sos_xml);

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "ORDER_TAKING_NEW_XML");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }

                                data.value = 69;
                                data.name = getString(R.string.order_taking_data_uploading);
                                publishProgress(data);


                                String order_received_xml = "";
                                onXML = "";

                                ArrayList<MSL_AvailabilityStockFacingGetterSetter> orderreceiveddata = db.getOrder_RECEIVED_UploadServerData(coverageList.get(i).getStoreId());

                                if (orderreceiveddata.size() > 0) {
                                    for (int j = 0; j < orderreceiveddata.size(); j++) {
                                        onXML = "[ORDER_CONFIRM]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[CATEGORY_ID]" + orderreceiveddata.get(j).getCategory_id() + "[/CATEGORY_ID]"
                                                + "[SYSTEM_PO_NUM]" + orderreceiveddata.get(j).getSys_PO_No() + "[/SYSTEM_PO_NUM]"
                                                + "[STORE_PO_NUM]" + orderreceiveddata.get(j).getStore_PO_No() + "[/STORE_PO_NUM]"
                                                + "[PO_DATE]" + orderreceiveddata.get(j).getPO_Date() + "[/PO_DATE]"
                                                + "[BRAND_ID]" + orderreceiveddata.get(j).getBrand_id() + "[/BRAND_ID]"
                                                + "[SKU_ID]" + orderreceiveddata.get(j).getSku_id() + "[/SKU_ID]"
                                                + "[PO_QTY]" + orderreceiveddata.get(j).getOrderqty() + "[/PO_QTY]"
                                                + "[ACTUAL_QTY]" + orderreceiveddata.get(j).getFacing() + "[/ACTUAL_QTY]" +
                                                "[/ORDER_CONFIRM]";

                                        order_received_xml = order_received_xml + onXML;
                                    }

                                    final String sos_xml = "[DATA]" + order_received_xml + "[/DATA]";
                                    Log.e("order_xml_data", sos_xml);

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "ORDER_RECEIVED_STATUS_XML");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                /*if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                    return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                }*/
                                }

                                data.value = 70;
                                data.name = getString(R.string.order_received_data_uploading);
                                publishProgress(data);


                                ////IRPD-Upload Data
                                String irPD_xmlData = "";
                                onXML = "";
                                ArrayList<CategoryPictureGetterSetter> irdata = db.getIrPDforUpload(coverageList.get(i).getStoreId(), coverageList.get(i).getVisitDate());
                                if (irdata != null && !irdata.isEmpty()) {
                                    for (int j = 0; j < irdata.size(); j++) {
                                        onXML = "[IR_DATA]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[CATEGORY_ID]" + irdata.get(j).getCategoryId() + "[/CATEGORY_ID]"
                                                + "[SUB_CATEGORY_ID]" + irdata.get(j).getSUB_CATEGORY_ID() + "[/SUB_CATEGORY_ID]"
                                                + "[SUB_CATEGORY_CODE]" + irdata.get(j).getSubCatCode() + "[/SUB_CATEGORY_CODE]"
                                                + "[IR_IMAGES]" + irdata.get(j).getSubCategoryCamera2() + "[/IR_IMAGES]"
                                                + "[/IR_DATA]";

                                        irPD_xmlData = irPD_xmlData + onXML;
                                    }

                                    final String irpd_xml = "[DATA]" + irPD_xmlData + "[/DATA]";
                                    Log.e("irp_xml_data", irpd_xml);
                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", irpd_xml);
                                    request.addProperty("KEYS", "IR_DATA_XML");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);
                                    result = envelope.getResponse();

                                }

                                data.value = 71;
                                data.name = getString(R.string.ir_data_upload);
                                publishProgress(data);


                                ////IRPDVisibility-Upload Data
                                String irPD_visi_xmlData = "";
                                onXML = "";
                                ArrayList<CategoryPictureGetterSetter> ir_visi_data = db.getIrPDVisibilityforUpload(coverageList.get(i).getStoreId(), coverageList.get(i).getVisitDate());
                                if (ir_visi_data != null && !ir_visi_data.isEmpty()) {
                                    for (int j = 0; j < ir_visi_data.size(); j++) {
                                        onXML = "[IR_VISIBILITY_DATA]" + "[MID]" + mid + "[/MID]" + "[USERID]" + userId + "[/USERID]" + "[CATEGORY_ID]" + ir_visi_data.get(j).getCategoryId() + "[/CATEGORY_ID]" + "[SUB_CATEGORY_ID]" + ir_visi_data.get(j).getSUB_CATEGORY_ID() + "[/SUB_CATEGORY_ID]" + "[SUB_CATEGORY_CODE]" + ir_visi_data.get(j).getSubCatCode() + "[/SUB_CATEGORY_CODE]" + "[IR_VISIBILITY_IMAGES]" + ir_visi_data.get(j).getSubCategoryCamera2() + "[/IR_VISIBILITY_IMAGES]" + "[/IR_VISIBILITY_DATA]";
                                        irPD_visi_xmlData = irPD_visi_xmlData + onXML;
                                    }
                                    final String irpd_xml = "[DATA]" + irPD_visi_xmlData + "[/DATA]";
                                    Log.e("irpd_visi_xml_data", irPD_visi_xmlData);
                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);
                                    request.addProperty("XMLDATA", irpd_xml);
                                    request.addProperty("KEYS", "IRPD_VISIBILITY_DATA_XML");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);
                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);
                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);
                                    result = envelope.getResponse();
                                }
                                data.value = 72;
                                data.name = getString(R.string.ir_visibility_data_upload);
                                publishProgress(data);

                                String visit_xml = "";
                                onXML = "";
                                ArrayList<Visit_Type_Master_GetterSetter> visittypelist = db.getUploadedVisitType(coverageList.get(i).getStoreId());

                                if (visittypelist.size() > 0) {
                                    for (int j = 0; j < visittypelist.size(); j++) {
                                        onXML = "[VISIT_TYPE_STATUS]"
                                                + "[MID]" + mid + "[/MID]"
                                                + "[USERID]" + userId + "[/USERID]"
                                                + "[STORE_ID]" + coverageList.get(i).getStoreId() + "[/STORE_ID]"
                                                + "[VISIT_TYPE_ID]" + visittypelist.get(j).getVISIT_TYPE_ID().get(0) + "[/VISIT_TYPE_ID]"
                                                + "[/VISIT_TYPE_STATUS]";

                                        visit_xml = visit_xml + onXML;
                                    }
                                    final String sos_xml = "[DATA]" + visit_xml + "[/DATA]";
                                    Log.e("visit_xml_data", sos_xml);

                                    request = new SoapObject(CommonString.NAMESPACE, CommonString.METHOD_UPLOAD_STOCK_XML_DATA);

                                    request.addProperty("XMLDATA", sos_xml);
                                    request.addProperty("KEYS", "VISIT_TYPE_XML");
                                    request.addProperty("USERNAME", userId);
                                    request.addProperty("MID", mid);

                                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                    envelope.dotNet = true;
                                    envelope.setOutputSoapObject(request);

                                    androidHttpTransport = new HttpTransportSE(CommonString.URL);
                                    androidHttpTransport.call(CommonString.SOAP_ACTION + CommonString.METHOD_UPLOAD_STOCK_XML_DATA, envelope);

                                    result = envelope.getResponse();

                                    if (!result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                        return CommonString.METHOD_UPLOAD_STOCK_XML_DATA;
                                    }
                                }
                                data.value = 72;
                                data.name = getString(R.string.visit_type_data_uploading);
                                publishProgress(data);
                            }


                            // SET COVERAGE STATUS
                            String final_xml = "";
                            onXML = "";
                            onXML = "[COVERAGE_STATUS]"
                                    + "[STORE_ID]" + coverageList.get(i).getStoreId() + "[/STORE_ID]"
                                    + "[VISIT_DATE]" + coverageList.get(i).getVisitDate() + "[/VISIT_DATE]"
                                    + "[USER_ID]" + coverageList.get(i).getUserId() + "[/USER_ID]"
                                    + "[STATUS]" + CommonString.KEY_D + "[/STATUS]"
                                    + "[/COVERAGE_STATUS]";

                            final_xml = final_xml + onXML;

                            final String sos_xml = "[DATA]" + final_xml + "[/DATA]";

                            request = new SoapObject(CommonString.NAMESPACE, status_service);
                            request.addProperty("onXML", sos_xml);

                            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                            envelope.dotNet = true;
                            envelope.setOutputSoapObject(request);

                            androidHttpTransport = new HttpTransportSE(CommonString.URL);
                            androidHttpTransport.call(CommonString.SOAP_ACTION + status_service, envelope);

                            result = envelope.getResponse();

                            //to set previous date for further use in UploadImageWithRetrofit
                            date = coverageList.get(i).getVisitDate();

                            //If Result is success then status of that store is update
                            if (result.toString().equalsIgnoreCase(CommonString.KEY_SUCCESS)) {
                                k = k + factor;
                                data.value = k;
                                data.name = "Uploading";
                                publishProgress(data);

                                db.open();
                                //db.updateCoverageStatus(coverageList.get(i).getStoreId(), CommonString.KEY_D);
                                db.updateStoreStatusOnLeave(coverageList.get(i).getStoreId(), coverageList.get(i)
                                        .getVisitDate(), CommonString.KEY_D, coverageList.get(i).getFlag_from());

                                //db.deleteTableWithStoreID(coverageList.get(i).getStoreId());
                            }

                            data.value = 100;
                            publishProgress(data);
                        }
                    }
                }
                return CommonString.KEY_SUCCESS;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                //Crashlytics.log(7, CommonString.MESSAGE_EXCEPTION, e.toString());
                // Crashlytics.logException(e.getCause());
                // Crashlytics.logException(new Exception(e.getCause()));
                e.printStackTrace();
            } catch (Exception e) {
                // Crashlytics.log(7, CommonString.MESSAGE_EXCEPTION, e.toString());
                //Crashlytics.logException(e.getCause());
                //Crashlytics.logException(new Exception(e.getCause()));
                e.printStackTrace();
            }

            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            /*dialog.dismiss();
            db.deleteAllTables();
            if (result.equals("")) {
                showAlert(getString(R.string.menu_upload_data));
            }*/

            dialog.dismiss();

            if (result.contains(CommonString.KEY_SUCCESS)) {
                //db.deleteAllTables();
                File f = new File(CommonString.getImagesFolder(context));
                File file[] = f.listFiles();
                UploadImageWithRetrofit.uploadedFiles = 0;
                UploadImageWithRetrofit.totalFiles = file.length;
                UploadImageWithRetrofit uploadImg = new UploadImageWithRetrofit(date, userId, PreviousDataUploadActivity.this);
                uploadImg.UploadImageRecursiveNew(PreviousDataUploadActivity.this);
            } else {
                showAlert(getString(R.string.error) + result.toString());
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        CommonFunctions.updateLangResources(context, preferences.getString(CommonString.KEY_LANGUAGE, ""));
    }


    public void showAlert(String str) {

        AlertDialog.Builder builder = new AlertDialog.Builder(PreviousDataUploadActivity.this);
        builder.setTitle("Parinaam");
        builder.setMessage(str).setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                       /* Intent i = new Intent(activity, StorelistActivity.class);
                        activity.startActivity(i);
                        activity.finish();*/
                        finish();

                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}
