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 ShippingOrderDatabaseHelper {
	
        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "WMSShipOrder.db";
        private static final String TABLE_NAME = "ShippingOrderList_Table";
        private static final String SHIPPING_ID = "_id";
        private static final String PONUMBER = "ponumber";
        private static final String DESTINATION = "destino";
        private static final String QTY = "cantidad";
        private static final String QTYRECEIVED = "cantidadenviada";
        private static final String WEIGHT = "peso";
        private static final String VOLUME = "volumen";
        private static final String ITEMLINE = "line";
        private static final String ORDERID = "orderid";
        private static final String SCANNED = "scanned";
        private ReceivingOpenHelper openHelper;
        private SQLiteDatabase database;

        public ShippingOrderDatabaseHelper(Context context) {
                openHelper = new ReceivingOpenHelper(context);
                database = openHelper.getWritableDatabase();
        }
        
        public void saveShippingLine(String ponumber,String sku,String qty,String qtyreceived,String type,String country,String weight,String volume,String barcode,String itemline,String orderid,String scanned) {
        	    ContentValues contentValues = new ContentValues();
                contentValues.put(PONUMBER, ponumber);
         
                contentValues.put(QTY, qty);
                contentValues.put(QTYRECEIVED, qtyreceived);
    
                contentValues.put(WEIGHT, weight);
                contentValues.put(VOLUME, volume);
        
                contentValues.put(ITEMLINE, itemline);
                contentValues.put(ORDERID, orderid);
                contentValues.put(SCANNED, scanned);
                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 getShippingLines(String ponumber) {
            return database.rawQuery(
                  "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                  +ponumber+"'",
                  null);
        } 
        
        public Cursor getRemainingShippingLines(String ponumber) {
            return database.rawQuery(
                  "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                  +ponumber+"' AND "+QTYRECEIVED+" ='0'",
                  null);
        }  
 
        public Cursor getItemByID(String ID) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ SHIPPING_ID+"='"+ID+"'";
            return database.rawQuery(query,null);
            }
        

        
        public void UpdateShippingItem(String qtyreceived,String id) {
    	    ContentValues contentValues = new ContentValues();
            contentValues.put(QTYRECEIVED , qtyreceived);
            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, "
                                        + DESTINATION + " TEXT, "
                                        + QTY + " TEXT, "
                                        + QTYRECEIVED + " TEXT, "
                                        + WEIGHT + " TEXT, "
                                        + VOLUME + " TEXT, "
                                        + ITEMLINE + " TEXT, "
                                        + ORDERID + " TEXT, "
                                        + SCANNED + " TEXT)");
                }
                @Override
                public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
                        onCreate(database);
                }

        }

}

