package com.example.waoperations.Shipping;

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

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "WMSShipTransfer3.db";
    private static final String TABLE_NAME = "ShippingTransferList_Table2";
    private static final String SHIPPING_ID = "_id";
    private static final String PONUMBER = "ponumber";
    private static final String PURCHID = "pedidocompra";
    private static final String ITEMID = "articulo";
    private static final String PURCHQTY = "cantidadseparada";
    private static final String PURCHNAME = "nombreproveedor";
    private static final String ORDERACCOUNT = "idproveedor";
    private static final String INVENTLOCATIONID = "bodegadestino";
    private static final String SALESID = "pedidoventa";
    private static final String DATAAREAID = "empresa";
    private static final String INVENTTRANSID = "inventtransid";
    private static final String INVENTDIMID = "inventdimid";


    private ReceivingOpenHelper openHelper;
    private SQLiteDatabase database;

    public ShippingTransferDatabaseHelper2(Context context) {
        openHelper = new ReceivingOpenHelper(context);
        database = openHelper.getWritableDatabase();
    }

    public void saveShippingTransferLine(String ponumber,String purchid,String itemid,String purchqty,String purchname,String orderaccount,String inventlocationid,String salesid,String dataareaid,String inventtransid,String inventdimid) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PONUMBER, ponumber);
        contentValues.put(PURCHID, purchid);
        contentValues.put(ITEMID,itemid);
        contentValues.put(PURCHQTY, purchqty);
        contentValues.put(PURCHNAME, purchname);
        contentValues.put(ORDERACCOUNT, orderaccount);
        contentValues.put(INVENTLOCATIONID, inventlocationid);
        contentValues.put(SALESID, salesid);
        contentValues.put(DATAAREAID, dataareaid);
        contentValues.put(INVENTTRANSID, inventtransid);
        contentValues.put(INVENTDIMID, inventdimid);

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

    public Cursor getAllShippingLines() {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+"",
                null);
    }
    public Cursor getShippingTransferAllLines() {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" ",
                null);
    }

    public Cursor getShippingTransferLines(String purchid) {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" WHERE " +PURCHID+ " ='"
                        +purchid+"'",
                null);
    }

    /* public Cursor getRemainingShippingLines(String ponumber) {
         return database.rawQuery(
               "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
               +ponumber+"' AND "+QTYRECEIVED+" ='0'",
               null);
     }*/
    public Cursor getShippingTransferItem(String ponumber,String itemid) {
        return database.rawQuery(

                "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                        +ponumber+"' AND "+ITEMID+" ='"+itemid+"'",
                null);
    }
    /*public Cursor getShippingItemBarcode(String ponumber,String barcode) {
        return database.rawQuery(
             "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
             +ponumber+"' AND "+ITEMBARCODE+" ='"+barcode+"'",
             null);
    } */
    public Cursor getItemByID(String ID) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ SHIPPING_ID+"='"+ID+"'";
        return database.rawQuery(query,null);
    }

    public Cursor getItemByIT(String ID) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ INVENTTRANSID+"='"+ID+"'";
        return database.rawQuery(query,null);
    }

       /* public Cursor getItemDetail(String ponumber,String inventrans,String sku,String orderid) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ PONUMBER+"='"+ponumber+"' AND "+ ITEMLINE+"='"+inventrans+"' AND "+ SKU+"='"+sku+"' AND "+ ORDERID+"='"+orderid+"'";
            return database.rawQuery(query,null);
            }*/

    public void UpdateShippingItem(String purchqty,String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PURCHQTY , purchqty);
        //contentValues.put(SCANNED , "1");
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, 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 + "("
                    + SHIPPING_ID + " INTEGER PRIMARY KEY, "
                    + PONUMBER + " TEXT, "
                    + PURCHID + " TEXT, "
                    + ITEMID + " TEXT, "
                    + PURCHQTY + " TEXT, "
                    + PURCHNAME + " TEXT, "
                    + ORDERACCOUNT + " TEXT, "
                    + INVENTLOCATIONID + " TEXT, "
                    + SALESID + " TEXT, "
                    + DATAAREAID + " TEXT, "
                    + INVENTTRANSID + " TEXT, "
                    + INVENTDIMID +" TEXT)");

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

    }

}
