package com.example.waoperations.Transference;

import java.sql.Date;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TransferenceDataBaseHelperZ {

    private static final int DATABASE_VERSION = 7;
    private static final String DATABASE_NAME = "WMSTransferZ.db";
    private static final String TABLE_NAME = "TransferZ_Table";
    private static final String TRANSFERENCEZ_ID = "_id";
    private static final String INVENTTRANSID =  "InventTransId";
    private static final String PURCHID =  "PurchId";
    private static final String SHIPPINGID =  "ShippingId";
    private static final String ITEMID = "Articulo";
    private static final String QTYTRANSFER = "CantidadRequerida";
    private static final String QTYCOUNTED= "CantidadContada";
    private static final String DATAAREAIDTO = "EmpresaA";
    private static final String INVENTLOCATIONIDTO= "BodegaHasta";
    private static final String PROCESSED = "Procesado";
    private static final String TRANSFERSTATUS = "Estado";
    private static final String QTYAVAILABLE = "CantidadDisponible";
    private static final String ITEMBARCODE = "barcode";
    private static final String QTYREMAIN = "Qtyremanente";
    private static final String WMSLOCATIONID = "posicion";
    private static final String PURCHUNIT = "UnidadConfig";


    private ReceivingOpenHelper openHelper;
    private SQLiteDatabase database;

    public TransferenceDataBaseHelperZ(Context context) {
        openHelper = new ReceivingOpenHelper(context);
        database = openHelper.getWritableDatabase();
    }

    public void saveTransferenceLineZ(String inventtransid, String purchid, String shippingid,  String itemid,  String qtytransfer, String qtycounted, String dataareaidto,String inventlocationidto, String processed, String estado, String qtyavailable,String barcode,String qtyremain,String wmslocationid,String purchunit)
    {
    	 ContentValues contentValues = new ContentValues();
    	 contentValues.put(INVENTTRANSID, inventtransid);
    	 contentValues.put(PURCHID, purchid);
    	 contentValues.put(SHIPPINGID, shippingid);
 	     contentValues.put(ITEMID , itemid);
 	     contentValues.put(QTYTRANSFER , qtytransfer);
 	     contentValues.put(QTYCOUNTED ,qtycounted);
         contentValues.put(DATAAREAIDTO, dataareaidto);
         contentValues.put(INVENTLOCATIONIDTO, inventlocationidto);
         contentValues.put(PROCESSED , processed);
         contentValues.put(TRANSFERSTATUS , estado);
         contentValues.put(QTYAVAILABLE, qtyavailable);
         contentValues.put(ITEMBARCODE, barcode);
         contentValues.put(QTYREMAIN, qtyremain);
         contentValues.put(WMSLOCATIONID, wmslocationid);
         contentValues.put(PURCHUNIT, purchunit);

          database.insert(TABLE_NAME, null, contentValues);
    }

    public void deleteAllData()
    {
    database.delete(TABLE_NAME, null, null);
    }

public Cursor getAllTransferenciasLines() {
    return database.rawQuery(
    "SELECT * FROM " + TABLE_NAME+"",  
    null);
} 
public Cursor getTransferenciasLines(String itemid) {
return database.rawQuery(
      "SELECT * FROM " + TABLE_NAME+" WHERE " +ITEMID+ " ='"
      +itemid+"'",
      null);
} 

public Cursor getTransferenciasLines2(String transferid) {
return database.rawQuery(
      "SELECT * FROM " + TABLE_NAME+" WHERE " +PURCHID+ " ='"
      +transferid+"'",
      null);
} 

public Cursor getTransferItem(String purchid,String str,String invid) {
   return database.rawQuery(
        "SELECT * FROM " + TABLE_NAME+" WHERE " +PURCHID+ " ='"
        +purchid+"' AND "+ITEMID+" ='"+str+"'  AND "+INVENTTRANSID+" ='"+invid+"'",
        null);
}  
public Cursor getTransferItemBarcode(String transferid,String barcode) {
   return database.rawQuery(
        "SELECT * FROM " + TABLE_NAME+" WHERE " +PURCHID+ " ='"
        +transferid+"' AND "+ITEMBARCODE+" ='"+barcode+"'",
        null);
   }
public Cursor getItemByID(String id) {
   String query= "SELECT * FROM "+TABLE_NAME+" where "+ TRANSFERENCEZ_ID+"='"+id+"'";
   return database.rawQuery(query,null);
   }

public Cursor getItemDetail(String itemid,String inventtrans,String itemId) {
	   String query= "SELECT * FROM "+TABLE_NAME+" where "+ ITEMID+"='"+itemid+"' AND "+ INVENTTRANSID+"='"+inventtrans+"'";
	   return database.rawQuery(query,null);
	   }

    public Cursor getTransferItemDetail(String purchid,String inventtransid,String itemId) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ PURCHID+"='"+purchid+"' AND "+ INVENTTRANSID+"='"+inventtransid+"' AND "+ITEMID+"='"+itemId+"'";
        return database.rawQuery(query,null);
    }

public Cursor getItemDetail2(String transferid,String itemId) {
	   String query= "SELECT * FROM "+TABLE_NAME+" where "+ PURCHID+"='"+transferid+"' AND "+ ITEMID+"='"+itemId+"'";
	   return database.rawQuery(query,null);
	   }

public void UpdateTransferItem(String qtyCounted,String newqtyAvailable, String newreservada, String id) {
   ContentValues contentValues = new ContentValues();
   contentValues.put(QTYCOUNTED, qtyCounted);
   contentValues.put(QTYREMAIN, newqtyAvailable);
   contentValues.put(QTYAVAILABLE, newreservada);
   contentValues.put(PROCESSED , "2");
   String strFilter = "_id=" + id;
   database.update(TABLE_NAME, contentValues, strFilter, null);    
}

    public void UpdateItem(String purchunit, String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PURCHUNIT, purchunit);
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }
    
    public void UpdateProcessed(String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PROCESSED , "1");
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }
    
    public void UpdateProcessed2(String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PROCESSED , "2");
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }
    
    public void UpdateWMSLocationID(String id, String wms) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(WMSLOCATIONID , wms);
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }

    private class ReceivingOpenHelper extends SQLiteOpenHelper {

        public ReceivingOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase database) {
                database.execSQL("CREATE TABLE " + TABLE_NAME + "("
                		  + TRANSFERENCEZ_ID + " INTEGER PRIMARY KEY, "
                		  + INVENTTRANSID+ " TEXT, "
                          + PURCHID+ " TEXT, "
                          + SHIPPINGID+ " TEXT, "
                          + ITEMID + " TEXT, "
                          + QTYTRANSFER+ " TEXT, "
                          + QTYCOUNTED+ " TEXT, "
                          + DATAAREAIDTO + " TEXT, "
                          + INVENTLOCATIONIDTO + " TEXT, "
                          + PROCESSED + " TEXT, "
                          + TRANSFERSTATUS + " TEXT, "
                          + QTYAVAILABLE+ " TEXT, "
                          + ITEMBARCODE + " TEXT, "
                          + QTYREMAIN + " TEXT, "
                		  + WMSLOCATIONID + " TEXT, "
                		  + PURCHUNIT +  " TEXT)");
        }
        
        @Override
        public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
            database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
            onCreate(database);
        }

    }

}
