package com.example.waoperations.Transference;

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

public class TransferenceDataBaseHelper2 {
	
	    private static final int DATABASE_VERSION =3;
	    private static final String DATABASE_NAME = "WMSTransferenceNew.db";
        private static final String TABLE_NAME = "Transfer2_Table";
        private static final String ID = "_id";
	    private static final String INVENTTRANSID =  "InventTransId";
	    private static final String TRANSFERID =  "TransferId";
	    private static final String ITEMID = "Articulo";
	    private static final String QTYTRANSFER = "CantidadRequerida";
	    private static final String QTYCOUNTED= "CantidadContada";
	    private static final String INVENTLOCATIONIDFROM = "Desde";
	    private static final String INVENTLOCATIONIDTO= "Hasta";
	    private static final String PROCESSED = "Procesado";
	    private static final String TRANSFERSTATUS = "Estado";
	    private static final String DATAAREAID = "Empresa";
	    private static final String QTYAVAILABLE = "CantidadDisponible";
	    private static final String CREATEDDATE = "FechaCreacion";
	    private static final String ITEMBARCODE = "barcode";
	    private static final String QTYREMAIN = "Qtyremanente";
	    private static final String LINENUM = "NumeroLinea";
	    private static final String PEDIDO = "Pedido";
	    private static final String LINEAPEDIDO = "LineaPedido";
	    private static final String CONFIG = "Configuracion";
	    private static final String TOTALDISPONIBLE = "TotalDisponible";
	    private static final String ENVIARRESTO = "EnviarResto";

	    private ContactOpenHelper openHelper;
	    private SQLiteDatabase database;

	    public TransferenceDataBaseHelper2(Context context) {
            openHelper = new ContactOpenHelper(context);
            database = openHelper.getWritableDatabase();
    }
    
    public void saveTransferenceLine(String inventtransid, String transferid,  String itemid,  String qtytransfer, String qtycounted, String inventlocationidfrom,String inventlocationidto, String processed, String estado,String dataareaid, String qtyavailable,String createddate,String barcode,String qtyremain,String linenum,String pedido,String lineapedido,String config,String totaldisponible,String qtyremainship)
    {
    	 ContentValues contentValues = new ContentValues();
    	 contentValues.put(INVENTTRANSID, inventtransid);
    	 contentValues.put(TRANSFERID, transferid);
 	     contentValues.put(ITEMID , itemid);
 	     contentValues.put(QTYTRANSFER , qtytransfer);
 	     contentValues.put(QTYCOUNTED ,qtycounted);
         contentValues.put(INVENTLOCATIONIDFROM, inventlocationidfrom);
         contentValues.put(INVENTLOCATIONIDTO, inventlocationidto);
         contentValues.put(PROCESSED , processed);
         contentValues.put(TRANSFERSTATUS , estado);
         contentValues.put(DATAAREAID, dataareaid);
         contentValues.put(QTYAVAILABLE, qtyavailable);
         contentValues.put(CREATEDDATE, createddate);
         contentValues.put(ITEMBARCODE, barcode);
         contentValues.put(QTYREMAIN, qtyremain);
         contentValues.put(LINENUM, linenum);
         contentValues.put(PEDIDO, pedido);
         contentValues.put(LINEAPEDIDO, lineapedido);
         contentValues.put(CONFIG, config);
         contentValues.put(TOTALDISPONIBLE, totaldisponible);
         contentValues.put(ENVIARRESTO, qtyremainship);
         
          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 getTransferenciasLinesByPO(String transferid) {
return database.rawQuery(
      "SELECT * FROM " + TABLE_NAME+" WHERE " +TRANSFERID+ " ='"
      +transferid+"'",
      null);
} 

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

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

public Cursor getTransferItem(String transferid,String str,String invid) {
   return database.rawQuery(
        "SELECT * FROM " + TABLE_NAME+" WHERE " +TRANSFERID+ " ='"
        +transferid+"' AND "+ITEMID+" ='"+str+"'  AND "+INVENTTRANSID+" ='"+invid+"'",
        null);
}  
public Cursor getTransferItemBarcode(String transferid,String barcode) {
   return database.rawQuery(
        "SELECT * FROM " + TABLE_NAME+" WHERE " +TRANSFERID+ " ='"
        +transferid+"' AND "+ITEMBARCODE+" ='"+barcode+"'",
        null);
   }
public Cursor getItemByID(String id) {
   String query= "SELECT * FROM "+TABLE_NAME+" where "+ 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 transferid,String inventtransid,String itemId) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ TRANSFERID+"='"+transferid+"' 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 "+ TRANSFERID+"='"+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 newqtyavailable, String ped, String lineaped, String conf, String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(QTYTRANSFER, newqtyavailable);
        contentValues.put(QTYAVAILABLE, newqtyavailable);
         contentValues.put(TRANSFERSTATUS , "1");
         contentValues.put(PEDIDO , ped);
         contentValues.put(LINEAPEDIDO , lineaped);
         contentValues.put(CONFIG , conf);
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }
    
    public void UpdateItemPO( String ped, String lineaped, String id) {
        ContentValues contentValues = new ContentValues();
         contentValues.put(TRANSFERSTATUS , "1");
         contentValues.put(PEDIDO , ped);
         contentValues.put(LINEAPEDIDO , lineaped);
        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 UpdateTotalDisponible(String id, String totaldisp) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(TOTALDISPONIBLE , totaldisp);
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }
    
    public void UpdateQtyTransferRequired(String id, String qtytransfer) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(QTYTRANSFER , qtytransfer);
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }


    private class ContactOpenHelper extends SQLiteOpenHelper {

            public ContactOpenHelper(Context context) {
                    super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
            
            @Override
            public void onCreate(SQLiteDatabase database) {
                    database.execSQL("CREATE TABLE " + TABLE_NAME + "("
                    		  + ID + " INTEGER PRIMARY KEY, "
                    		  + INVENTTRANSID+ " TEXT, "
                              + TRANSFERID+ " TEXT, "
                              + ITEMID + " TEXT, "
                              + QTYTRANSFER+ " TEXT, "
                              + QTYCOUNTED+ " TEXT, "
                              + INVENTLOCATIONIDFROM + " TEXT, "
                              + INVENTLOCATIONIDTO + " TEXT, "
                              + PROCESSED + " TEXT, "
                              + TRANSFERSTATUS + " TEXT, "
                              + DATAAREAID + " TEXT, "
                              + QTYAVAILABLE+ " TEXT, "
                              + CREATEDDATE + " TEXT, "
                              + ITEMBARCODE + " TEXT, "
                              + QTYREMAIN + " TEXT, "
                    		  + LINENUM + " TEXT, "
                    		  + PEDIDO + " TEXT, "
                    		  + LINEAPEDIDO + " TEXT, "
                    		  + CONFIG + " TEXT, "
                    		  + TOTALDISPONIBLE +  " TEXT, "
                    		  + ENVIARRESTO +  " TEXT)");
            }
            
            @Override
            public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                    database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
                    onCreate(database);
            }
    }
}
