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

    private static final int DATABASE_VERSION =6;
    private static final String DATABASE_NAME = "WMSTransferencePO.db";
    private static final String TABLE_NAME = "TransferPO_Table";
    private static final String ID = "_id";
    private static final String PURCHID =  "PurchId";
    private static final String ITEMID =  "Articulo";
    private static final String PURCHQTY =  "CantidadBultos";
    private static final String QTYORDERED = "CantidadUnidades";
    private static final String PURCHUNIT = "TipoBulto";
    private static final String PURCHRECEIVEDNOW= "CantidadRecibidaAhora";
    private static final String PURCHSTATUS = "EstadoPO";
    private static final String ORDERACCOUNT= "IDProveedor";
    private static final String CREATEDDATE = "FechaCreacion";
    private static final String INVENTLOCATIONID = "Bodega";
    private static final String INVENTTRANSID = "Inventtransid";
    private static final String PROCESSED = "procesado";

    private ContactOpenHelper openHelper;
    private SQLiteDatabase database;

    public TransferenceDataBaseHelperPO(Context context) {
        openHelper = new ContactOpenHelper(context);
        database = openHelper.getWritableDatabase();
    }

    public void saveTransferencePOLine(String purchid, String itemid,String purchqty,String qtyordered,String purchunit,String purchreceivednow,String purchstatus,String orderaccount,String createddate,String inventlocationid, String inventtransid,String processed)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PURCHID, purchid);
        contentValues.put(ITEMID, itemid);
        contentValues.put(PURCHQTY, purchqty);
        contentValues.put(QTYORDERED , qtyordered);
        contentValues.put(PURCHUNIT , purchunit);
        contentValues.put(PURCHRECEIVEDNOW ,purchreceivednow);
        contentValues.put(PURCHSTATUS, purchstatus);
        contentValues.put(ORDERACCOUNT, orderaccount);
        contentValues.put(CREATEDDATE, createddate);
        contentValues.put(INVENTLOCATIONID, inventlocationid);
        contentValues.put(INVENTTRANSID, inventtransid);
        contentValues.put(PROCESSED, processed);

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

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

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

    public Cursor getTransferenciasPOLines2(String inventtransid) {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" WHERE " +INVENTTRANSID+ " ='"
                        +inventtransid+"'",
                null);
    }

    /*public Cursor getTransferItem(String transferid,String str) {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" WHERE " +TRANSFERID+ " ='"
                        +transferid+"' AND "+ITEMID+" ='"+str+"'",
                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 invendimid,String itemId) {
	   String query= "SELECT * FROM "+TABLE_NAME+" where "+ ITEMID+"='"+itemid+"' AND "+ INVENTDIMID+"='"+invendimid+"'";
	   return database.rawQuery(query,null);
	   }*/

    /*public Cursor getTransferItemDetail(String transferid,String itemId) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ TRANSFERID+"='"+transferid+"' 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 UpdatePurchLine(String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PROCESSED , "1");
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }
    
    public void UpdatePurchLine2(String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PROCESSED , "2");
        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, "
                    + PURCHID+ " TEXT, "
                    + ITEMID+ " TEXT, "
                    + PURCHQTY + " TEXT, "
                    + QTYORDERED+ " TEXT, "
                    + PURCHUNIT + " TEXT, "
                    + PURCHRECEIVEDNOW  + " TEXT, "
                    + PURCHSTATUS + " TEXT, "
                    + ORDERACCOUNT + " TEXT, "
                    + INVENTLOCATIONID + " TEXT, "
                    + CREATEDDATE + " TEXT, "
                    + INVENTTRANSID + " TEXT, "
                    + PROCESSED + " TEXT)");
        }

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







}
