package com.example.waoperations.Aduanas;

//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 AduanasDatabaseHelper3 {

        private static final int DATABASE_VERSION = 6;
        private static final String DATABASE_NAME = "WMSAduanas1.db";
        private static final String TABLE_NAME = "Aduanas_Table";
        private static final String CUSTOMINVENTORYID = "_id";
        private static final String ITEMID = "Articulo";
        private static final String QTY = "Cantidad";
        private static final String INVENTLOCATIONID = "Bodega";
        private static final String WMSLOCATIONID = "Posicion";
        private static final String INVENTSERIALID = "Pais_id";
        private static final String CONFIGID = "Configuracion";
        private static final String YEARID = "Año";
        private static final String DATAAREAID = "Empresa";
        private static final String RECID = "Rec_id";
        private static final String DATAAREAID2 = "Empresa2";
        private static final String INTRACODE = "Intracode";
        private static final String PRIMARYVENDORID = "Contratista_id";
        private static final String DIMENSION = "Dimension";
        private static final String DIMENSION2 = "Dimension2";
        private static final String DIMENSION3 = "Dimension3";
        private static final String SCANNED = "scaneado";
        private static final String ITEMLINE = "Linea";
        private static final String ITEMBARCODE = "barcode";
        
        private ContactOpenHelper openHelper;
        private SQLiteDatabase database;

        public AduanasDatabaseHelper3(Context context) {
                openHelper = new ContactOpenHelper(context);
                database = openHelper.getWritableDatabase();
        }
        
        public void saveAduanasLine(String custominventoryid,String itemid,String qty,String inventlocationid,String wmslocationid,String inventserialid,String configid,String yearid,String dataareaid,String recid,String dataareaid2,String intracode,String primaryvendorid,String dimension, String dimension2, String dimension3) {
        	    ContentValues contentValues = new ContentValues();
       //         contentValues.put(CUSTOMINVENTORYID, custominventoryid);
                contentValues.put(ITEMID, itemid);
                contentValues.put(QTY, qty);
                contentValues.put(INVENTLOCATIONID, inventlocationid);
                contentValues.put(WMSLOCATIONID, wmslocationid);
                contentValues.put(INVENTSERIALID, inventserialid);
                contentValues.put(CONFIGID, configid);
                contentValues.put(YEARID, yearid);
                contentValues.put(DATAAREAID, dataareaid);
                contentValues.put(RECID, recid);
                contentValues.put(DATAAREAID2, dataareaid2);
                contentValues.put(INTRACODE, intracode);
                contentValues.put(PRIMARYVENDORID, primaryvendorid);
                contentValues.put(DIMENSION, dimension);
                contentValues.put(DIMENSION2, dimension2);
                contentValues.put(DIMENSION3, dimension3);
                database.insert(TABLE_NAME, null, contentValues);
        }
    
    public void deleteAllData()
        {
        database.delete(TABLE_NAME, null, null);
        }
    
    public Cursor getAllAduanasLines(String wmslocationid) {
                return database.rawQuery(
                             "SELECT * FROM " + TABLE_NAME+" WHERE " +WMSLOCATIONID+ " ='"
                                     +wmslocationid+"'",
                             null);
        } 
        public Cursor getAduanasLines(String wmslocationid) {
            return database.rawQuery(
                            "SELECT * FROM " + TABLE_NAME+" WHERE " +WMSLOCATIONID+ " ='"
                            +wmslocationid+"'",
                            null);
        }  

        
        public void UpdateScannedItem(String id, String iD2) {
    	    ContentValues contentValues = new ContentValues();
            
            contentValues.put(SCANNED , "1");
            String strFilter = "_id=" + id;
            database.update(TABLE_NAME, contentValues, strFilter, null);    
        }  
        public void UpdateScannedAmountItem(String qty,String id) {
    	    ContentValues contentValues = new ContentValues();
            contentValues.put(QTY , qty);
            String strFilter = "_id=" + id;
            database.update(TABLE_NAME, contentValues, strFilter, null);    
        }
        
        
        public Cursor getNonScannedItemsPerWMSLocation(String wmslocationid) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+SCANNED+"='"+0+"' and "+ WMSLOCATIONID+"='"+wmslocationid+"' LIMIT 1";
            return database.rawQuery(query,null);
            }
        
        /*public Cursor getCountItemBarcode(String wmslocationid,String barcode) {
            return database.rawQuery(
                 "SELECT * FROM " + TABLE_NAME+" WHERE " +WMSLOCATIONID+ " ='"
                 +wmslocationid+"' AND "+ITEMBARCODE+" ='"+barcode+"'",
                 null);
        }*/
        public Cursor getCCItem(String wmslocationid,String itemid) {
            return database.rawQuery(
                            "SELECT * FROM " + TABLE_NAME+" WHERE " +WMSLOCATIONID+ " ='"
                            +wmslocationid+"' AND "+ITEMID+" ='"+itemid+"'",
                            null);
        }  
        
        public Cursor getItemLocation(String wmslocationid,String itemid,String LOC) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ WMSLOCATIONID+"='"+wmslocationid+"' and "+ ITEMID+"='"+itemid+"'and "+ INVENTLOCATIONID+"='"+LOC+"' LIMIT 1";
            return database.rawQuery(query,null);
            }
   
        public Cursor getItemByID(String ID) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ CUSTOMINVENTORYID+"='"+ID+"'";
            return database.rawQuery(query,null);
            }
        
        public Cursor getItemDetail(String wmslocationid,String inventdim,String itemid) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ ITEMLINE+"='"+inventdim+"' AND "+ ITEMID+"='"+itemid+"' AND "+WMSLOCATIONID+" ='"+wmslocationid+"'";
            return database.rawQuery(query,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 + "("
                                        + CUSTOMINVENTORYID + " INTEGER PRIMARY KEY, "
                                        + ITEMID + " TEXT, "
                                        + QTY + " TEXT, "
                                        + INVENTLOCATIONID + " TEXT, "
                                        + WMSLOCATIONID + " TEXT, "
                                        + INVENTSERIALID + " TEXT, "
                                        + CONFIGID + " TEXT, "
                                        + YEARID + " TEXT, "
                                        + DATAAREAID + " TEXT, "
                                        + RECID + " TEXT, "
                                        + DATAAREAID2 + " TEXT, "
                                        + INTRACODE + " TEXT, "
                                        + PRIMARYVENDORID + " TEXT, "
                                        + DIMENSION + " TEXT, "
                                        + DIMENSION2 + " TEXT, "
                                        + DIMENSION3 + " TEXT)" );
                }
                
                @Override
                public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
                        onCreate(database);
                }

        }

}

