package com.example.waoperations.Inventory;

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 InventoryDatabaseHelper {
	
        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "WMSInventory.db";
        private static final String TABLE_NAME = "InventoryLocation_Table";
        private static final String INVENTORY_ID = "_id";
        private static final String LOCATION = "ubicacion";
        private static final String SKU = "articulo";
        private static final String DESCRIPTION = "descripcion";
        private static final String QTY = "cantidad";
        private static final String TYPE = "tipo";
        private static final String COUNTRY = "pais";
        private static final String WEIGHT = "peso";
        private static final String VOLUME = "volumen";
        private ReceivingOpenHelper openHelper;
        private SQLiteDatabase database;

        public InventoryDatabaseHelper(Context context) {
                openHelper = new ReceivingOpenHelper(context);
                database = openHelper.getWritableDatabase();
        }
        
        public void saveInventoryLine(String location,String sku,String descripcion,String qty,String type,String country,String weight,String volume) {
        	    ContentValues contentValues = new ContentValues();
                contentValues.put(LOCATION, location);
                contentValues.put(SKU, sku);
                contentValues.put(QTY, qty);
                contentValues.put(TYPE, type);
                contentValues.put(DESCRIPTION, descripcion);
                contentValues.put(COUNTRY, country);
                contentValues.put(WEIGHT, weight);
                contentValues.put(VOLUME, volume);
                database.insert(TABLE_NAME, null, contentValues);
        }
        public void deleteAllData()
        {
                 database.delete(TABLE_NAME, null, null);
        }
        public Cursor getAllInventoryLines() {
                return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+"", 
                null);
        } 
        public Cursor getInventoryLines(String location) {
            return database.rawQuery(
                  "SELECT * FROM " + TABLE_NAME+" WHERE " +LOCATION+ " ='"
                  +location+"'",
                  null);
        }  

        public Cursor getInventoryItemLines(String sku) {
            return database.rawQuery(
                 "SELECT * FROM " + TABLE_NAME+" WHERE " +SKU+" ='"+sku+"'",
                 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 + "("
                                        + INVENTORY_ID + " INTEGER PRIMARY KEY, "
                                        + LOCATION + " TEXT, "
                                        + SKU + " TEXT, "
                                        + DESCRIPTION + " TEXT, "
                                        + QTY + " TEXT, "
                                        + TYPE + " TEXT, "
                                        + COUNTRY + " TEXT, "
                                        + WEIGHT + " TEXT, "
                                        + VOLUME + " TEXT)");
                }
                @Override
                public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
                        onCreate(database);
                }

        }

}

