package com.example.waoperations.Replenishment;

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 ReplenishmentDatabaseHelper {

        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "WMSReplenishment.db";
        private static final String TABLE_NAME = "ReplenishList_Table";
        private static final String PICK_ID = "_id";
        private static final String PONUMBER = "ponumber";
        private static final String LOCATIONSTART = "origen";
        private static final String SKU = "articulo";
        private static final String LOCATIONEND = "destino";
        private static final String QTY = "cantidad";
        private static final String TYPE = "tipo";
        private static final String AMOUNTPICKED = "cantidadsacada";
        private static final String SCANNED = "scaneado";
        private static final String ITEMBARCODE = "barcode";
        private static final String COUNTRY = "country";
        private static final String ACTIVATIONDATE = "activationdate";
        private static final String ROUTEID = "routeID";
        private static final String INVENTDIMID = "inventdimID";

        
        private ContactOpenHelper openHelper;
        private SQLiteDatabase database;

        public ReplenishmentDatabaseHelper(Context context) {
                openHelper = new ContactOpenHelper(context);
                database = openHelper.getWritableDatabase();
        }
        
        public void saveReplenishmentLine(String ponumber,String locstart,String sku,String locend,String qty,String type,String amountpicked,String scanned,String barcode,String country,String activationdate,String routeid,String inventdimid) {
        	    ContentValues contentValues = new ContentValues();
                contentValues.put(PONUMBER, ponumber);
                contentValues.put(LOCATIONSTART, locstart);
                contentValues.put(SKU, sku);
                contentValues.put(LOCATIONEND, locend);
                contentValues.put(QTY, qty);
                contentValues.put(TYPE, type);
                contentValues.put(AMOUNTPICKED, amountpicked);
                contentValues.put(SCANNED, scanned);
                contentValues.put(ITEMBARCODE, barcode);
                contentValues.put(COUNTRY, country);
                contentValues.put(ACTIVATIONDATE, activationdate);
                contentValues.put(ROUTEID, routeid);
                contentValues.put(INVENTDIMID, inventdimid);
                database.insert(TABLE_NAME, null, contentValues);
                
        }
        public void deleteAllData()
        {
                 database.delete(TABLE_NAME, null, null);
        }
     
        public Cursor getAllPickLines(String ponumber) {
                return database.rawQuery(
                                "SELECT * FROM " + TABLE_NAME+"", 
                                null);
        } 
   
        public Cursor getPickingLines(String ponumber) {
            return database.rawQuery(
                            "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                            	     +ponumber+"'",
                            null);
        }  
   
        public void UpdateScannedItem(String amountpicked,String id) {
    	    ContentValues contentValues = new ContentValues();
            contentValues.put(AMOUNTPICKED , amountpicked);
            contentValues.put(SCANNED , "1");
            String strFilter = "_id=" + id;
            database.update(TABLE_NAME, contentValues, strFilter, null);    
        }  
   
        public Cursor getCountItemBarcode(String ponumber,String barcode) {
            return database.rawQuery(
                 "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                 +ponumber+"'"
                 +" AND "+ITEMBARCODE+" ='"+barcode+"'",
                 null);
        }
        
        public Cursor getPOItemLocation(String po,String idim) {
          //  String query= "SELECT * FROM "+TABLE_NAME+" where "+ PONUMBER+"='"+PO+"' and "+ SKU+"='"+PSKU+"'and "+ LOCATION+"='"+LOC+"' LIMIT 1";
        	String query= "SELECT * FROM "+TABLE_NAME+" where "+ INVENTDIMID+"='"+idim+"'and "+ PONUMBER+"='"+po+"' LIMIT 1";
            return database.rawQuery(query,null);
            }
   
        public Cursor getItemByID(String ID) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ PICK_ID+"='"+ID+"'";
            return database.rawQuery(query,null);
            }
     
        
        public Cursor getNonScannedItemsPerPO(String PO) {
             String query= "SELECT * FROM "+TABLE_NAME+" where "+SCANNED+"='"+0+"' and "+ PONUMBER+"='"+PO+"' LIMIT 1";
        	//  String query= "SELECT * FROM "+TABLE_NAME+" where "+SCANNED+"='"+0+"' LIMIT 1";
            return database.rawQuery(query,null);
            }
        
        public Cursor getPickingItemBarcode(String PO,String barcode) {
            return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                +PO+"' AND "+ITEMBARCODE+" ='"+barcode+"'",
         //       "SELECT * FROM " + TABLE_NAME+" WHERE "+
        //        ITEMBARCODE+" ='"+barcode+"'",
                 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 + "("
                                        + PICK_ID + " INTEGER PRIMARY KEY, "
                                        + PONUMBER + " TEXT, "
                                        + LOCATIONSTART + " TEXT, "
                                        + SKU + " TEXT, "
                                        + LOCATIONEND + " TEXT, "
                                        + QTY + " TEXT, "
                                        + TYPE + " TEXT, "
                                        + AMOUNTPICKED + " TEXT, "
                                        + SCANNED + " TEXT, "
                                        + ITEMBARCODE + " TEXT, "
                                        + COUNTRY + " TEXT, "
                                        + ACTIVATIONDATE + " TEXT, "
                                        + ROUTEID + " TEXT, "
                                        + INVENTDIMID + " TEXT)");

                }
                @Override
                public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
                        onCreate(database);
                }

        }

}

