using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Bt.ScanLib;
using Bt;
using CsvReadWrite;
using System.Data.SqlClient;

namespace HT_SKT_Stock
{
    public partial class FrmStoreOutbound : Form
    {
        public static FrmStoreOutbound _Form_StoreOutboundInstance;
        public String AppPath = "";
        public List<List<string>> tableList;
        List<DisplayTable> lstTableStore = new List<DisplayTable>();
        List<DisplayTable> lstTablePickup = new List<DisplayTable>();
        List<DisplayTable> lstTableStoreShow = new List<DisplayTable>();
        List<DisplayTable> lstTablePickupShow = new List<DisplayTable>();
        List<DisplayTable> lstTableStoreTemp = new List<DisplayTable>();
        List<DisplayTable> lstTablePickupTemp = new List<DisplayTable>();
        private BtScanLib btScanLib;
        private String sysKanbanCodeStore = "";
        private String sysKanbanCodePickup = "";
        private String sysKanbanPart = "";
        private String sysKanbanSerial = "";
        private String sysRow = "";
        private bool scanSotre = true;
        public Int32 RowStartStore = 0;
        public Int32 RowEndStore = 9;
        public Int32 RowStartPickup = 0;
        public Int32 RowEndPickup = 9;
        public bool blFG = true;

        public FrmStoreOutbound()
        {
            InitializeComponent();

            // Enable BTScanLib
            btScanLib = BtScanLib.getInstance();
            btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
            btScanLib.Debug = false;
            btScanLib.Enable();

            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnBack.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnDelete.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnAllDelete.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnConfirm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            dgStoreKanban.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            dgPickupKanban.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnBackPageStore.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnNextPageStore.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnBackPagePickup.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

            btnNextPagePickup.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStoreOutbound_KeyDown);

        }

        private void FrmStoreOutbound_Load(object sender, EventArgs e)
        {
            if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width == 240)
            {
                resolution.ScreenSize.VGAtoQVGA(this);
            }

            this.MaximizeBox = !this.MaximizeBox;
            this.MinimizeBox = !this.MinimizeBox;

            _Form_StoreOutboundInstance = this;


            String path = this.GetType().Assembly.GetModules()[0].FullyQualifiedName;
            Int32 en = path.LastIndexOf("\\");
            AppPath = path.Substring(0, en);

            lblUser.Text = "User : " + FrmLogin._Form_LoginInstance.userName;

            scanSotre = true;

            RowStartStore = 0;
            RowEndStore = 9;
            RowStartPickup = 0;
            RowEndPickup = 9;

            lblStaus1Store.Visible = false;
            lblStaus2Store.Visible = false;
            lblStaus3Store.Visible = false;
            lblStaus4Store.Visible = false;
            lblStaus5Store.Visible = false;
            lblStaus1Pickup.Visible = false;
            lblStaus2Pickup.Visible = false;
            lblStaus3Pickup.Visible = false;
            lblStaus4Pickup.Visible = false;
            lblStaus5Pickup.Visible = false;

            btnNextPageStore.Enabled = false;
            btnBackPageStore.Enabled = false;

            btnNextPagePickup.Enabled = false;
            btnBackPagePickup.Enabled = false;

        }

        private string checkTypeKanban(string kanbanCode)
        {
            switch (kanbanCode)
            {
                case "1":
                    return "Material";
                case "2":
                    return "Molding Shopping";
                case "3":
                    return "Molding FG Kanban";
                case "4":
                    return "Insert";
                case "5":
                    return "Process";
                case "6":
                    return "Supplier";
                case "7":
                    return "Shopping";
                case "8":
                    return "FG";
                case "9":
                    return "Lot card";
                case "10":
                    return "Customer";
                case "11":
                    return "Remainder";
                case "12":
                    return "Setup card";
                default:
                    return "";
            }
        }

        private void SetTableStore(Int32 processStart)
        {

            lblStaus1Store.Visible = false;
            lblStaus2Store.Visible = false;
            lblStaus3Store.Visible = false;
            lblStaus4Store.Visible = false;
            lblStaus5Store.Visible = false;


            lstTableStoreShow.Clear();

            if (lstTableStore.Count > 5)
            {
                btnNextPageStore.Enabled = true;
                btnBackPageStore.Enabled = true;
            }
            else
            {
                btnNextPageStore.Enabled = false;
                btnBackPageStore.Enabled = false;
            }

            if (processStart == 0)
            {
                RowStartStore = 0;
                RowEndStore = 4;
            }

            if ((RowStartStore > lstTableStore.Count - 1) && (lstTableStore.Count > 0))
            {
                RowStartStore = RowStartStore - 5;
                RowEndStore = RowEndStore - 5;
            }

            if (processStart == 3)
            {
                Int32 iPage = Convert.ToInt32(Math.Ceiling(lstTableStore.Count / 5));

                if ((lstTableStore.Count % 5) > 0)
                {
                    iPage = iPage + 1;
                }

                RowStartStore = (iPage * 5) - 5;
                RowEndStore = RowStartStore + 4;

            }

            Int32 iRow = 0;
            foreach (var item in lstTableStore)
            {
                //MessageBox.Show(iRow.ToString() + " | " + RowStart.ToString() + " | " + RowEnd.ToString());

                iRow = iRow + 1;
                if ((iRow >= RowStartStore + 1) && (iRow <= RowEndStore + 1))
                {
                    //iRow = iRow + 1;
                    lstTableStoreShow.Add(new DisplayTable
                    {
                        Partno = item.Partno,
                        DRow = iRow.ToString(),
                        serialNo = item.serialNo,
                        qtySerial = item.qtySerial,
                        statusSerial = item.statusSerial
                    });

                    Int32 rowCheck = iRow % 5;
                    String strStatus = item.statusSerial;

                    if (rowCheck == 0)
                    {
                        rowCheck = 5;
                    }

                    switch (rowCheck)
                    {
                        case 1:
                            lblStaus1Store.Text = strStatus;
                            if (lblStaus1Store.Text == "OK")
                            {
                                lblStaus1Store.BackColor = Color.Green;
                                lblStaus1Store.ForeColor = Color.White;
                            }
                            else if (lblStaus1Store.Text == "NG")
                            {
                                lblStaus1Store.BackColor = Color.Red;
                                lblStaus1Store.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus1Store.Text = "-";
                                lblStaus1Store.BackColor = Color.White;
                                lblStaus1Store.ForeColor = Color.Black;
                            }
                            lblStaus1Store.Visible = true;
                            break;

                        case 2:
                            lblStaus2Store.Text = strStatus;
                            if (lblStaus2Store.Text == "OK")
                            {
                                lblStaus2Store.BackColor = Color.Green;
                                lblStaus2Store.ForeColor = Color.White;
                            }
                            else if (lblStaus2Store.Text == "NG")
                            {
                                lblStaus2Store.BackColor = Color.Red;
                                lblStaus2Store.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus2Store.Text = "-";
                                lblStaus2Store.BackColor = Color.White;
                                lblStaus2Store.ForeColor = Color.Black;
                            }
                            lblStaus2Store.Visible = true;
                            break;

                        case 3:
                            lblStaus3Store.Text = strStatus;
                            if (lblStaus3Store.Text == "OK")
                            {
                                lblStaus3Store.BackColor = Color.Green;
                                lblStaus3Store.ForeColor = Color.White;
                            }
                            else if (lblStaus3Store.Text == "NG")
                            {
                                lblStaus3Store.BackColor = Color.Red;
                                lblStaus3Store.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus3Store.Text = "-";
                                lblStaus3Store.BackColor = Color.White;
                                lblStaus3Store.ForeColor = Color.Black;
                            }
                            lblStaus3Store.Visible = true;
                            break;

                        case 4:
                            lblStaus4Store.Text = strStatus;
                            if (lblStaus4Store.Text == "OK")
                            {
                                lblStaus4Store.BackColor = Color.Green;
                                lblStaus4Store.ForeColor = Color.White;
                            }
                            else if (lblStaus4Store.Text == "NG")
                            {
                                lblStaus4Store.BackColor = Color.Red;
                                lblStaus4Store.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus4Store.Text = "-";
                                lblStaus4Store.BackColor = Color.White;
                                lblStaus4Store.ForeColor = Color.Black;
                            }
                            lblStaus4Store.Visible = true;
                            break;

                        case 5:
                            lblStaus5Store.Text = strStatus;
                            if (lblStaus5Store.Text == "OK")
                            {
                                lblStaus5Store.BackColor = Color.Green;
                                lblStaus5Store.ForeColor = Color.White;
                            }
                            else if (lblStaus5Store.Text == "NG")
                            {
                                lblStaus5Store.BackColor = Color.Red;
                                lblStaus5Store.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus5Store.Text = "-";
                                lblStaus5Store.BackColor = Color.White;
                                lblStaus5Store.ForeColor = Color.Black;
                            }
                            lblStaus5Store.Visible = true;
                            break;


                        default:
                            break;
                    }

                }
            }

            var blist = new BindingList<DisplayTable>(lstTableStoreShow);
            dgStoreKanban.DataSource = blist;

            InitialDataGridStoreKanban();

            if (lstTableStoreShow.Count > 0)
            {
                dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
            }

            dgStoreKanban.Focus();

        }

        private void SetTablePickup(Int32 processStart)
        {

            lblStaus1Pickup.Visible = false;
            lblStaus2Pickup.Visible = false;
            lblStaus3Pickup.Visible = false;
            lblStaus4Pickup.Visible = false;
            lblStaus5Pickup.Visible = false;


            lstTablePickupShow.Clear();

            if (lstTablePickup.Count > 5)
            {
                btnNextPagePickup.Enabled = true;
                btnBackPagePickup.Enabled = true;
            }
            else
            {
                btnNextPagePickup.Enabled = false;
                btnBackPagePickup.Enabled = false;
            }

            if (processStart == 0)
            {
                RowStartPickup = 0;
                RowEndPickup = 4;
            }

            if ((RowStartPickup > lstTablePickup.Count - 1) && (lstTablePickup.Count > 0))
            {
                RowStartPickup = RowStartPickup - 5;
                RowEndPickup = RowEndPickup - 5;
            }

            if (processStart == 3)
            {
                Int32 iPage = Convert.ToInt32(Math.Ceiling(lstTablePickup.Count / 5));

                if ((lstTablePickup.Count % 5) > 0)
                {
                    iPage = iPage + 1;
                }

                RowStartPickup = (iPage * 5) - 5;
                RowEndPickup = RowStartPickup + 4;

            }

            Int32 iRow = 0;
            foreach (var item in lstTablePickup)
            {
                //MessageBox.Show(iRow.ToString() + " | " + RowStart.ToString() + " | " + RowEnd.ToString());

                iRow = iRow + 1;
                if ((iRow >= RowStartPickup + 1) && (iRow <= RowEndPickup + 1))
                {
                    //iRow = iRow + 1;
                    lstTablePickupShow.Add(new DisplayTable
                    {
                        Partno = item.Partno,
                        DRow = iRow.ToString(),
                        serialNo = item.serialNo,
                        qtySerial = item.qtySerial,
                        statusSerial = item.statusSerial
                    });

                    Int32 rowCheck = iRow % 5;
                    String strStatus = item.statusSerial;

                    if (rowCheck == 0)
                    {
                        rowCheck = 5;
                    }

                    switch (rowCheck)
                    {
                        case 1:
                            lblStaus1Pickup.Text = strStatus;
                            if (lblStaus1Pickup.Text == "OK")
                            {
                                lblStaus1Pickup.BackColor = Color.Green;
                                lblStaus1Pickup.ForeColor = Color.White;
                            }
                            else if (lblStaus1Pickup.Text == "NG")
                            {
                                lblStaus1Pickup.BackColor = Color.Red;
                                lblStaus1Pickup.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus1Pickup.Text = "-";
                                lblStaus1Pickup.BackColor = Color.White;
                                lblStaus1Pickup.ForeColor = Color.Black;
                            }
                            lblStaus1Pickup.Visible = true;
                            break;

                        case 2:
                            lblStaus2Pickup.Text = strStatus;
                            if (lblStaus2Pickup.Text == "OK")
                            {
                                lblStaus2Pickup.BackColor = Color.Green;
                                lblStaus2Pickup.ForeColor = Color.White;
                            }
                            else if (lblStaus2Pickup.Text == "NG")
                            {
                                lblStaus2Pickup.BackColor = Color.Red;
                                lblStaus2Pickup.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus2Pickup.Text = "-";
                                lblStaus2Pickup.BackColor = Color.White;
                                lblStaus2Pickup.ForeColor = Color.Black;
                            }
                            lblStaus2Pickup.Visible = true;
                            break;

                        case 3:
                            lblStaus3Pickup.Text = strStatus;
                            if (lblStaus3Pickup.Text == "OK")
                            {
                                lblStaus3Pickup.BackColor = Color.Green;
                                lblStaus3Pickup.ForeColor = Color.White;
                            }
                            else if (lblStaus3Pickup.Text == "NG")
                            {
                                lblStaus3Pickup.BackColor = Color.Red;
                                lblStaus3Pickup.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus3Pickup.Text = "-";
                                lblStaus3Pickup.BackColor = Color.White;
                                lblStaus3Pickup.ForeColor = Color.Black;
                            }
                            lblStaus3Pickup.Visible = true;
                            break;

                        case 4:
                            lblStaus4Pickup.Text = strStatus;
                            if (lblStaus4Pickup.Text == "OK")
                            {
                                lblStaus4Pickup.BackColor = Color.Green;
                                lblStaus4Pickup.ForeColor = Color.White;
                            }
                            else if (lblStaus4Pickup.Text == "NG")
                            {
                                lblStaus4Pickup.BackColor = Color.Red;
                                lblStaus4Pickup.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus4Pickup.Text = "-";
                                lblStaus4Pickup.BackColor = Color.White;
                                lblStaus4Pickup.ForeColor = Color.Black;
                            }
                            lblStaus4Pickup.Visible = true;
                            break;

                        case 5:
                            lblStaus5Pickup.Text = strStatus;
                            if (lblStaus5Pickup.Text == "OK")
                            {
                                lblStaus5Pickup.BackColor = Color.Green;
                                lblStaus5Pickup.ForeColor = Color.White;
                            }
                            else if (lblStaus5Pickup.Text == "NG")
                            {
                                lblStaus5Pickup.BackColor = Color.Red;
                                lblStaus5Pickup.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus5Pickup.Text = "-";
                                lblStaus5Pickup.BackColor = Color.White;
                                lblStaus5Pickup.ForeColor = Color.Black;
                            }
                            lblStaus5Pickup.Visible = true;
                            break;


                        default:
                            break;
                    }

                }
            }

            var blist = new BindingList<DisplayTable>(lstTablePickupShow);
            dgPickupKanban.DataSource = blist;

            InitialDataGridPickupKanban();

            if (lstTablePickupShow.Count > 0)
            {
                dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
            }

            dgPickupKanban.Focus();

        }

        private Int32 CheckInboundScan(string partNo, string kanbanSerialNo, Decimal qtyScan, string kanbanCode, string kanbanClass, string remainderClass)
        {
            try
            {


                tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 iRow = 0;


                strSQL = "EXEC sp_get_Inbound_part @partNo = '" + partNo
                    + "' , @serialNo = '" + kanbanSerialNo + "' , @qtyScan = "
                    + qtyScan + " , @kanbanCode = '" + kanbanCode
                    + "' , @kanbanClass = '" + kanbanClass + "' , @remainderClass = '" + remainderClass
                    + "'";


                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                iRow = dt.Rows.Count;

                dtAdapter = null;
                objConn.Close();
                objConn = null;


                if (iRow > 0)
                {

                    return 1;

                }
                else
                {
                    return 2;
                }




            }
            catch (Exception)
            {

                return 3;

            }


        }

        private Int32 CheckStatusScan(string kanbanCode, string kanbanClass, string partNo, string kanbanSerialNo, string remainderClass)
        {
            try
            {


                tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 qtyScan = 0;


                strSQL = "EXEC sp_get_kanban_status @kanbanCode = '" + kanbanCode
                    + "' , @kanbanClass = '" + kanbanClass + "' , @partNo = '" + partNo
                + "' , @serialNo = '" + kanbanSerialNo + "' , @remainderClass = '" + remainderClass
                + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                dtAdapter = null;
                objConn.Close();
                objConn = null;

                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["status"].ToString() == "1")
                    {
                        return 1;
                    }
                    else
                    {
                        return 4;
                    }


                }
                else
                {
                    return 2;
                }




            }
            catch (Exception)
            {

                return 3;

            }


        }

        private string CheckTypeKanbanScan(string kanbanCode)
        {
            try
            {


                tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;


                strSQL = "EXEC sp_get_kanban_code @kanbanCode = '" + kanbanCode
                    + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                dtAdapter = null;
                objConn.Close();
                objConn = null;

                if (dt.Rows.Count > 0)
                {

                    return dt.Rows[0]["kanban_name"].ToString();


                }
                else
                {
                    return "2";
                }




            }
            catch (Exception)
            {

                return "3";

            }


        }

        void btScanLib_OnScan(string result)
        {

            if ((lstTableStore.Count == lstTablePickup.Count) || (blFG == true))
            {

                //Scan Store Kanban
                string storeKanban = "";
                string[] kanban = result.Split('|');
                string kanbanCode = "";
                string kanbanClass = "";
                string remainderClass = "";
                string partNoScan = "";
                string quantity = "0";
                string kanbanSerialNo = "";
                Int32 intCheck1 = 0;
                string strStatus = "";
                bool blCheckDuplicate = false;
                string quantityAll = "0";
                Int32 intCheck = 0;
                string qtyCheck = "0";

                if (kanban.Length == 6)
                {
                    kanbanCode = kanban[0];
                    kanbanClass = kanban[1];
                    remainderClass = kanban[2];
                    partNoScan = kanban[3];
                    quantity = kanban[4];
                    kanbanSerialNo = kanban[5];
                    storeKanban = result;

                    if ((kanbanCode == "8") || (kanbanCode == "1") || (kanbanCode == "3"))
                    {
                        blFG = true;
                    }
                    else
                    {
                        blFG = false;
                    }

                    if (remainderClass == "2")
                    {
                        quantity = "";
                    }
                    FrmLogin.strQty = quantity;

                    //******* disable BTScanLib
                    btScanLib.OnScan -= btScanLib_OnScan;
                    btScanLib.Disable();

                    FrmInputQty input = new FrmInputQty(quantity);
                    input.ShowDialog();

                    // Enable BTScanLib
                    btScanLib = BtScanLib.getInstance();
                    btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                    btScanLib.Debug = false;
                    btScanLib.Enable();

                    quantity = FrmLogin.strQty;

                    try
                    {
                        //if (lblQtyAll.Text == "")
                        //{
                        //    qtyCheck = quantity;
                        //}
                        //else
                        //{
                        //    qtyCheck = (Convert.ToDecimal(lblQtyAll.Text) + Convert.ToDecimal(quantity)).ToString();
                        //}
                        intCheck = CheckInboundScan(partNoScan, kanbanSerialNo, Convert.ToDecimal(quantity), kanbanCode, kanbanClass, remainderClass);

                        //intCheck = CheckInboundScan(partNoScan, kanbanSerialNo, Convert.ToDecimal(qtyCheck));

                        if (intCheck == 2)
                        {
                            //******* disable BTScanLib
                            btScanLib.OnScan -= btScanLib_OnScan;
                            btScanLib.Disable();

                            //FrmWarningLeader err = new FrmWarningLeader("Inbound not found", "data", "");
                            FrmWarningLeader err = new FrmWarningLeader("", "", "Over Stock");
                            err.ShowDialog();

                            // Enable BTScanLib
                            btScanLib = BtScanLib.getInstance();
                            btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                            btScanLib.Debug = false;
                            btScanLib.Enable();

                            if (lstTableStoreShow.Count > 0)
                            {
                                dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                            }

                            dgStoreKanban.Focus();

                            return;
                        }
                        else if (intCheck == 3)
                        {
                            WarningErr1 err = new WarningErr1("Error Database Connection ");
                            err.ShowDialog();

                            if (lstTableStoreShow.Count > 0)
                            {
                                dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                            }

                            dgStoreKanban.Focus();

                            return;
                        }


                        if (lstTableStore.Count == 0)
                        {
                            strStatus = "OK";
                            sysKanbanCodeStore = kanbanCode;
                            sysKanbanPart = partNoScan;
                            sysKanbanSerial = kanbanSerialNo;
                            sysRow = "1";

                            //lblQtyAll.Text = quantity;

                            Int32 intCheck2 = CheckStatusScan(kanbanCode, kanbanClass, partNoScan, kanbanSerialNo, remainderClass);

                            if ((intCheck2 == 4) || (intCheck2 == 2))
                            {
                                strStatus = "NG";

                            }
                            else if (intCheck2 == 3)
                            {
                                WarningErr1 err = new WarningErr1("Error Database Connection ");
                                err.ShowDialog();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }

                            //if (blFG == true)
                            //{
                                lblQtyAll.Text = quantity;

                            //}

                            lstTableStore.Add(new DisplayTable
                            {
                                Partno = partNoScan,
                                DRow = sysRow,
                                serialNo = kanbanSerialNo,
                                qtySerial = quantity,
                                statusSerial = strStatus,
                                kanbanCode = kanbanCode,
                                kanbanClass = kanbanClass,
                                remainderClass = remainderClass,
                                storeKanban = storeKanban
                            });

                            SetTableStore(3);

                            if (intCheck2 == 4)
                            {

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Kanban status", "doesn't match", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }
                            else if (intCheck2 == 2)
                            {
                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Kanban Master", "not found", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }

                            string checkType = CheckTypeKanbanScan(sysKanbanCodeStore);

                            if (checkType == "2")
                            {
                                lblTypeStore.Text = checkTypeKanban(sysKanbanCodeStore);
                            }
                            else if (checkType == "3")
                            {
                                WarningErr1 err = new WarningErr1("Error Database Connection ");
                                err.ShowDialog();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }
                            else
                            {
                                lblTypeStore.Text = checkType;
                            }



                        }
                        else
                        {
                            quantityAll = "0";
                            foreach (var item in lstTableStore)
                            {

                                if (item.serialNo == kanbanSerialNo)
                                {
                                    blCheckDuplicate = true;

                                }

                                if (item.statusSerial == "OK")
                                {

                                    quantityAll = (Convert.ToDecimal(quantityAll) + Convert.ToDecimal(item.qtySerial)).ToString();

                                }

                            }


                            if (blCheckDuplicate == true)
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTableStore.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    storeKanban = storeKanban
                                });

                                SetTableStore(3);

                                //var blist = new BindingList<DisplayTable>(lstTableStore);
                                //dgStoreKanban.DataSource = blist;

                                //InitialDataGridStoreKanban();

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Duplicate scans of the", "same serial", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }
                            else if (partNoScan != sysKanbanPart)
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTableStore.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    storeKanban = storeKanban
                                });

                                SetTableStore(3);

                                //var blist = new BindingList<DisplayTable>(lstTableStore);
                                //dgStoreKanban.DataSource = blist;

                                //InitialDataGridStoreKanban();

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("", "", "Different part no.");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }
                            else if (kanbanCode != sysKanbanCodeStore)
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTableStore.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    storeKanban = storeKanban
                                });

                                SetTableStore(3);

                                //var blist = new BindingList<DisplayTable>(lstTableStore);
                                //dgStoreKanban.DataSource = blist;

                                //InitialDataGridStoreKanban();

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Different types of", "Kanban", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTableStoreShow.Count > 0)
                                {
                                    dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                }

                                dgStoreKanban.Focus();

                                return;
                            }
                            else
                            {
                                Int32 intCheck4 = CheckStatusScan(kanbanCode, kanbanClass, partNoScan, kanbanSerialNo, remainderClass);

                                if (intCheck4 == 4)
                                {
                                    sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                    strStatus = "NG";
                                    lstTableStore.Add(new DisplayTable
                                    {
                                        Partno = partNoScan,
                                        DRow = sysRow,
                                        serialNo = kanbanSerialNo,
                                        qtySerial = quantity,
                                        statusSerial = strStatus,
                                        kanbanCode = kanbanCode,
                                        kanbanClass = kanbanClass,
                                        remainderClass = remainderClass,
                                        storeKanban = storeKanban
                                    });

                                    SetTableStore(3);

                                    //******* disable BTScanLib
                                    btScanLib.OnScan -= btScanLib_OnScan;
                                    btScanLib.Disable();

                                    FrmWarningLeader err = new FrmWarningLeader("Kanban status", "doesn't match", "");
                                    err.ShowDialog();

                                    // Enable BTScanLib
                                    btScanLib = BtScanLib.getInstance();
                                    btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                    btScanLib.Debug = false;
                                    btScanLib.Enable();

                                    if (lstTableStoreShow.Count > 0)
                                    {
                                        dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                    }

                                    dgStoreKanban.Focus();

                                    return;
                                }
                                else if (intCheck4 == 2)
                                {
                                    sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                    strStatus = "NG";
                                    lstTableStore.Add(new DisplayTable
                                    {
                                        Partno = partNoScan,
                                        DRow = sysRow,
                                        serialNo = kanbanSerialNo,
                                        qtySerial = quantity,
                                        statusSerial = strStatus,
                                        kanbanCode = kanbanCode,
                                        kanbanClass = kanbanClass,
                                        remainderClass = remainderClass,
                                        storeKanban = storeKanban
                                    });

                                    SetTableStore(3);

                                    //******* disable BTScanLib
                                    btScanLib.OnScan -= btScanLib_OnScan;
                                    btScanLib.Disable();

                                    FrmWarningLeader err = new FrmWarningLeader("Kanban Master", "not found", "");
                                    err.ShowDialog();

                                    // Enable BTScanLib
                                    btScanLib = BtScanLib.getInstance();
                                    btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                    btScanLib.Debug = false;
                                    btScanLib.Enable();

                                    if (lstTableStoreShow.Count > 0)
                                    {
                                        dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                    }

                                    dgStoreKanban.Focus();

                                    return;
                                }

                                else if (intCheck4 == 3)
                                {
                                    WarningErr1 err = new WarningErr1("Error Database Connection ");
                                    err.ShowDialog();

                                    if (lstTableStoreShow.Count > 0)
                                    {
                                        dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                                    }

                                    dgStoreKanban.Focus();

                                    return;
                                }


                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "OK";

                                //if (blFG == true)
                                //{
                                    quantityAll = (Convert.ToInt32(quantityAll) + Convert.ToInt32(quantity)).ToString();
                                    lblQtyAll.Text = quantityAll;

                                //}

                                //quantityAll = (Convert.ToInt32(quantityAll) + Convert.ToInt32(quantity)).ToString();
                                //lblQtyAll.Text = quantityAll;
                                lstTableStore.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    storeKanban = storeKanban
                                });

                                SetTableStore(3);

                                //var blist = new BindingList<DisplayTable>(lstTableStore);
                                //dgStoreKanban.DataSource = blist;

                                //InitialDataGridStoreKanban();

                                //scanSotre = false;

                                //if (lstTableStore.Count > 0)
                                //{
                                //    dgStoreKanban.CurrentRowIndex = lstTableStore.Count - 1;
                                //}

                                //dgStoreKanban.Focus();
                            }
                        }



                    }
                    catch (Exception)
                    {

                    }

                }
                else
                {
                    WarningErr1 err = new WarningErr1("Kanban Format Incorrect");
                    err.ShowDialog();

                    if (lstTableStoreShow.Count > 0)
                    {
                        dgStoreKanban.CurrentRowIndex = lstTableStoreShow.Count - 1;
                    }

                    dgStoreKanban.Focus();
                }
            }
            else
            {
                //Scan Pick-up Kanban
                string pickupKanban = "";
                string[] kanban = result.Split('|');
                string kanbanCode = "";
                string kanbanClass = "";
                string remainderClass = "";
                string partNoScan = "";
                string quantity = "0";
                string kanbanSerialNo = "";
                Int32 intCheck1 = 0;
                string strStatus = "";
                bool blCheckDuplicate = false;
                string quantityAll = "0";

                if (kanban.Length == 6)
                {
                    kanbanCode = kanban[0];
                    kanbanClass = kanban[1];
                    remainderClass = kanban[2];
                    partNoScan = kanban[3];
                    quantity = kanban[4];
                    kanbanSerialNo = kanban[5];
                    pickupKanban = result;

                    if (remainderClass == "2")
                    {
                        quantity = "";
                    }
                    FrmLogin.strQty = quantity;

                    //******* disable BTScanLib
                    btScanLib.OnScan -= btScanLib_OnScan;
                    btScanLib.Disable();

                    FrmInputQty input = new FrmInputQty(quantity);
                    input.ShowDialog();

                    // Enable BTScanLib
                    btScanLib = BtScanLib.getInstance();
                    btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                    btScanLib.Debug = false;
                    btScanLib.Enable();

                    quantity = FrmLogin.strQty;

                    if (quantity == "")
                    {
                        quantity = "0";
                    }

                    try
                    {
                        if (lstTablePickup.Count == 0)
                        {
                            strStatus = "OK";
                            sysKanbanCodePickup = kanbanCode;
                            //sysKanbanPart = partNoScan;
                            sysKanbanSerial = kanbanSerialNo;
                            sysRow = "1";


                            Int32 intCheck = CheckStatusScan(kanbanCode, kanbanClass, partNoScan, kanbanSerialNo, remainderClass);


                            if ((intCheck == 1) || (intCheck == 2))
                            {
                                strStatus = "NG";

                            }
                            else if (intCheck == 3)
                            {
                                WarningErr1 err = new WarningErr1("Error Database Connection ");
                                err.ShowDialog();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }


                            lstTablePickup.Add(new DisplayTable
                            {
                                Partno = partNoScan,
                                DRow = sysRow,
                                serialNo = kanbanSerialNo,
                                qtySerial = quantity,
                                statusSerial = strStatus,
                                kanbanCode = kanbanCode,
                                kanbanClass = kanbanClass,
                                remainderClass = remainderClass,
                                pickupKanban = pickupKanban
                            });


                            SetTablePickup(3);

                            if (intCheck == 1)
                            {

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Kanban status", "doesn't match", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }
                            else if (intCheck == 2)
                            {
                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Kanban Master", "not found", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }


                            //lblQtyAll.Text = quantity;
                            //lblTypePickup.Text = checkTypeKanban(sysKanbanCodePickup);

                            string checkType = CheckTypeKanbanScan(sysKanbanCodePickup);

                            if (checkType == "2")
                            {
                                lblTypePickup.Text = checkTypeKanban(sysKanbanCodePickup);
                            }
                            else if (checkType == "3")
                            {
                                WarningErr1 err = new WarningErr1("Error Database Connection ");
                                err.ShowDialog();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }
                            else
                            {
                                lblTypePickup.Text = checkType;
                            }

                            //lstTablePickup.Add(new DisplayTable
                            //{
                            //    Partno = partNoScan,
                            //    DRow = sysRow,
                            //    serialNo = kanbanSerialNo,
                            //    qtySerial = quantity,
                            //    statusSerial = strStatus,
                            //    kanbanCode = kanbanCode,
                            //    kanbanClass = kanbanClass,
                            //    remainderClass = remainderClass
                            //});

                            //SetTablePickup(3);

                            //var blist = new BindingList<DisplayTable>(lstTablePickup);
                            //dgPickupKanban.DataSource = blist;

                            //InitialDataGridPickupKanban();

                            //scanSotre = true;

                            //dgPickupKanban.Focus();
                        }
                        else
                        {
                            quantityAll = "0";
                            foreach (var item in lstTablePickup)
                            {

                                if (item.serialNo == kanbanSerialNo)
                                {
                                    blCheckDuplicate = true;

                                }

                                if (item.statusSerial == "OK")
                                {

                                    quantityAll = (Convert.ToDecimal(quantityAll) + Convert.ToDecimal(item.qtySerial)).ToString();

                                }

                            }


                            if (blCheckDuplicate == true)
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTablePickup.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    pickupKanban = pickupKanban
                                });

                                SetTablePickup(3);

                                //var blist = new BindingList<DisplayTable>(lstTablePickup);
                                //dgPickupKanban.DataSource = blist;

                                //InitialDataGridPickupKanban();

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Duplicate scans of the", "same serial", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }
                            else if (partNoScan != sysKanbanPart)
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTablePickup.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    pickupKanban = pickupKanban
                                });

                                SetTablePickup(3);

                                //var blist = new BindingList<DisplayTable>(lstTablePickup);
                                //dgPickupKanban.DataSource = blist;

                                //InitialDataGridPickupKanban();

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("", "", "Different part no.");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                //if (lstTablePickup.Count > 0)
                                //{
                                //    dgPickupKanban.CurrentRowIndex = lstTablePickup.Count - 1;
                                //}

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }
                            else if (kanbanCode != sysKanbanCodePickup)
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTablePickup.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    pickupKanban = pickupKanban
                                });

                                SetTablePickup(3);

                                //var blist = new BindingList<DisplayTable>(lstTablePickup);
                                //dgPickupKanban.DataSource = blist;

                                //InitialDataGridPickupKanban();

                                //******* disable BTScanLib
                                btScanLib.OnScan -= btScanLib_OnScan;
                                btScanLib.Disable();

                                FrmWarningLeader err = new FrmWarningLeader("Different types of", "Kanban", "");
                                err.ShowDialog();

                                // Enable BTScanLib
                                btScanLib = BtScanLib.getInstance();
                                btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
                                btScanLib.Debug = false;
                                btScanLib.Enable();

                                //if (lstTablePickup.Count > 0)
                                //{
                                //    dgPickupKanban.CurrentRowIndex = lstTablePickup.Count - 1;
                                //}

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }
                            else
                            {
                                sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "OK";
                                quantityAll = (Convert.ToDecimal(quantityAll) + Convert.ToDecimal(quantity)).ToString();
                                //lblQtyAll.Text = quantityAll;
                                lstTablePickup.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass,
                                    pickupKanban = pickupKanban
                                });

                                SetTablePickup(3);

                                //var blist = new BindingList<DisplayTable>(lstTablePickup);
                                //dgPickupKanban.DataSource = blist;

                                //InitialDataGridPickupKanban();

                                //scanSotre = true;

                                //if (lstTablePickup.Count > 0)
                                //{
                                //    dgPickupKanban.CurrentRowIndex = lstTablePickup.Count - 1;
                                //}

                                //dgPickupKanban.Focus();
                            }
                        }



                    }
                    catch (Exception)
                    {

                    }

                }
                else
                {
                    WarningErr1 err = new WarningErr1("Kanban Format Incorrect");
                    err.ShowDialog();

                    if (lstTablePickupShow.Count > 0)
                    {
                        dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                    }

                    dgPickupKanban.Focus();
                }




            }


        }

        private void InitialDataGridStoreKanban()
        {
            int grid_width = 445;
            // hidden column and row header
            dgStoreKanban.ColumnHeadersVisible = true;
            dgStoreKanban.RowHeadersVisible = false;

            // set style
            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = (new BindingList<DisplayTable>()).GetType().Name;

            // Column 0
            DataGridTextBoxColumn tbc0 = new DataGridTextBoxColumn();
            tbc0.Width = 29;
            tbc0.MappingName = "DRow";
            tbc0.HeaderText = "#";
            tableStyle.GridColumnStyles.Add(tbc0);

            // Column 1
            DataGridTextBoxColumn tbc1 = new DataGridTextBoxColumn();
            tbc1.Width = 67;
            tbc1.MappingName = "Partno";
            tbc1.HeaderText = "Part no";
            tableStyle.GridColumnStyles.Add(tbc1);

            // Column 2
            DataGridTextBoxColumn tbc2 = new DataGridTextBoxColumn();
            tbc2.Width = 44;
            tbc2.MappingName = "serialNo";
            tbc2.HeaderText = "Serial";
            tableStyle.GridColumnStyles.Add(tbc2);

            // Column 3
            DataGridTextBoxColumn tbc3 = new DataGridTextBoxColumn();
            tbc3.Width = 40;
            tbc3.MappingName = "qtySerial";
            tbc3.HeaderText = "Qty";
            tableStyle.GridColumnStyles.Add(tbc3);

            // Column 4
            DataGridTextBoxColumn tbc4 = new DataGridTextBoxColumn();
            tbc4.Width = 37;
            tbc4.MappingName = "statusSerial";
            tbc4.HeaderText = "Status";
            tableStyle.GridColumnStyles.Add(tbc4);

            dgStoreKanban.TableStyles.Clear();
            dgStoreKanban.TableStyles.Add(tableStyle);


        }

        private void InitialDataGridPickupKanban()
        {
            int grid_width = 445;
            // hidden column and row header
            dgPickupKanban.ColumnHeadersVisible = true;
            dgPickupKanban.RowHeadersVisible = false;

            // set style
            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = (new BindingList<DisplayTable>()).GetType().Name;

            // Column 0
            DataGridTextBoxColumn tbc0 = new DataGridTextBoxColumn();
            tbc0.Width = 29;
            tbc0.MappingName = "DRow";
            tbc0.HeaderText = "#";
            tableStyle.GridColumnStyles.Add(tbc0);

            // Column 1
            DataGridTextBoxColumn tbc1 = new DataGridTextBoxColumn();
            tbc1.Width = 67;
            tbc1.MappingName = "Partno";
            tbc1.HeaderText = "Part no";
            tableStyle.GridColumnStyles.Add(tbc1);

            // Column 2
            DataGridTextBoxColumn tbc2 = new DataGridTextBoxColumn();
            tbc2.Width = 44;
            tbc2.MappingName = "serialNo";
            tbc2.HeaderText = "Serial";
            tableStyle.GridColumnStyles.Add(tbc2);

            // Column 3
            DataGridTextBoxColumn tbc3 = new DataGridTextBoxColumn();
            tbc3.Width = 40;
            tbc3.MappingName = "qtySerial";
            tbc3.HeaderText = "Qty";
            tableStyle.GridColumnStyles.Add(tbc3);

            // Column 4
            DataGridTextBoxColumn tbc4 = new DataGridTextBoxColumn();
            tbc4.Width = 37;
            tbc4.MappingName = "statusSerial";
            tbc4.HeaderText = "Status";
            tableStyle.GridColumnStyles.Add(tbc4);

            dgPickupKanban.TableStyles.Clear();
            dgPickupKanban.TableStyles.Add(tableStyle);


        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            //******* disable BTScanLib
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();

            FrmOutboundMenu._Form_OutboundMenuInstance.Show();
            //Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }

        private void FrmStoreOutbound_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode.ToString())
            {

                case "F1":
                    btnBack_Click(sender, e);
                    break;

                case "F2":
                    btnDelete_Click(sender, e);
                    break;

                case "F3":
                    btnAllDelete_Click(sender, e);
                    break;

                case "F4":
                    btnConfirm_Click(sender, e);
                    break;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstTableStore.Count > lstTablePickup.Count)
            {
                int row = dgStoreKanban.CurrentCell.RowNumber;

                //lstTableStore.RemoveAt(row);

                if (row == -1)
                {
                    row = lstTableStore.Count;
                }

                lstTableStore.RemoveAt(Convert.ToInt32(dgStoreKanban[row, 0].ToString()) - 1);



                if (lstTableStore.Count == 0)
                {
                    lblTypeStore.Text = string.Empty;
                }

                //String quantityAll = "0";
                //foreach (var item in lstTable)
                //{

                //    if (item.statusSerial == "OK")
                //    {
                //        quantityAll = (Convert.ToInt32(quantityAll) + Convert.ToInt32(item.qtySerial)).ToString();

                //    }

                //}

                //lblQtyAll.Text = quantityAll;

                int iRow = 1;
                lstTableStoreTemp.Clear();
                foreach (var item in lstTableStore)
                {

                    lstTableStoreTemp.Add(new DisplayTable
                    {
                        Partno = item.Partno,
                        DRow = iRow.ToString(),
                        serialNo = item.serialNo,
                        qtySerial = item.qtySerial,
                        statusSerial = item.statusSerial
                    });


                    iRow = iRow + 1;
                }
                lstTableStore.Clear();
                foreach (var item in lstTableStoreTemp)
                {

                    lstTableStore.Add(new DisplayTable
                    {
                        Partno = item.Partno,
                        DRow = item.DRow,
                        serialNo = item.serialNo,
                        qtySerial = item.qtySerial,
                        statusSerial = item.statusSerial
                    });

                }



                if (lstTableStore.Count == 0 && lstTablePickup.Count == 0)
                {
                    lstTableStore.Clear();

                    SetTableStore(0);

                    lstTablePickup.Clear();

                    SetTablePickup(0);

                    lblQtyAll.Text = string.Empty;
                    lblTypeStore.Text = string.Empty;
                    lblTypePickup.Text = string.Empty;
                    sysKanbanPart = "";
                    sysKanbanCodeStore = "";
                    sysKanbanCodePickup = "";
                    sysKanbanSerial = "";
                    sysRow = "";
                }
                else
                {
                    SetTableStore(3);
                }

                //var blist = new BindingList<DisplayTable>(lstTableStore);
                //dgStoreKanban.DataSource = blist;

                //InitialDataGridStoreKanban();

                //if (lstTableStore.Count > 0)
                //{
                //    dgStoreKanban.CurrentRowIndex = lstTableStore.Count - 1;
                //}

                //dgStoreKanban.Focus();
            }
            else
            {
                if (lstTablePickup.Count == 0)
                {
                    return;
                }

                int row = dgPickupKanban.CurrentCell.RowNumber;

                //lstTablePickup.RemoveAt(row);

                if (row == -1)
                {
                    row = lstTablePickup.Count;
                }

                lstTablePickup.RemoveAt(Convert.ToInt32(dgStoreKanban[row, 0].ToString()) - 1);


                int iRow = 1;
                lstTablePickupTemp.Clear();
                foreach (var item in lstTablePickup)
                {

                    lstTablePickupTemp.Add(new DisplayTable
                    {
                        Partno = item.Partno,
                        DRow = iRow.ToString(),
                        serialNo = item.serialNo,
                        qtySerial = item.qtySerial,
                        statusSerial = item.statusSerial
                    });


                    iRow = iRow + 1;
                }
                lstTablePickup.Clear();
                foreach (var item in lstTablePickupTemp)
                {

                    lstTablePickup.Add(new DisplayTable
                    {
                        Partno = item.Partno,
                        DRow = item.DRow,
                        serialNo = item.serialNo,
                        qtySerial = item.qtySerial,
                        statusSerial = item.statusSerial
                    });

                }


                String quantityAll = "0";
                foreach (var item in lstTablePickup)
                {

                    if (item.statusSerial == "OK")
                    {
                        quantityAll = (Convert.ToDecimal(quantityAll) + Convert.ToDecimal(item.qtySerial)).ToString();

                    }

                }

                if (lstTablePickup.Count == 0)
                {
                    lblTypePickup.Text = string.Empty;
                    //lblQtyAll.Text = string.Empty;
                }
                else
                {
                    //lblQtyAll.Text = quantityAll;
                }

                SetTablePickup(3);

                //var blist = new BindingList<DisplayTable>(lstTablePickup);
                //dgPickupKanban.DataSource = blist;

                //InitialDataGridPickupKanban();

                //dgPickupKanban.Focus();
            }


        }

        private void btnAllDelete_Click(object sender, EventArgs e)
        {
            //lstTableStore.Clear();

            //var blist = new BindingList<DisplayTable>(lstTableStore);
            //dgStoreKanban.DataSource = blist;

            //InitialDataGridStoreKanban();

            //dgStoreKanban.Focus();


            //lstTablePickup.Clear();

            //var blist1 = new BindingList<DisplayTable>(lstTablePickup);
            //dgPickupKanban.DataSource = blist1;

            //InitialDataGridPickupKanban();

            //dgPickupKanban.Focus();

            lstTableStore.Clear();

            SetTableStore(0);

            lstTablePickup.Clear();

            SetTablePickup(0);

            lblQtyAll.Text = string.Empty;
            lblTypeStore.Text = string.Empty;
            lblTypePickup.Text = string.Empty;
            sysKanbanPart = "";
            sysKanbanCodeStore = "";
            sysKanbanCodePickup = "";
            sysKanbanSerial = "";
            sysRow = "";

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //var itemStore in lstTableStore;

            if (lstTableStore.Count == 0)
            {
                WarningErr1 errEmptyData = new WarningErr1("Empty Data ");
                errEmptyData.ShowDialog();

                if (lstTablePickupShow.Count > 0)
                {
                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                }

                dgPickupKanban.Focus();

                return;
            }

            if (lstTableStore.Count > 0)
            {
                foreach (var item in lstTableStore)
                {
                    if (item.statusSerial == "NG")
                    {
                        WarningErr1 errNGData = new WarningErr1("NG Data ");
                        errNGData.ShowDialog();

                        if (lstTablePickupShow.Count > 0)
                        {
                            dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                        }

                        dgPickupKanban.Focus();

                        return;
                    }
                }
            }

            if (lstTablePickup.Count > 0)
            {
                foreach (var item in lstTablePickup)
                {
                    if (item.statusSerial == "NG")
                    {
                        WarningErr1 errNGData2 = new WarningErr1("NG Data ");
                        errNGData2.ShowDialog();

                        if (lstTablePickupShow.Count > 0)
                        {
                            dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                        }

                        dgPickupKanban.Focus();

                        return;
                    }
                }
            }


            Int32 indexStore = 0;

            if (lstTablePickup.Count > 0)
            {

                foreach (var item in lstTablePickup)
                {

                    if ((item.statusSerial == "OK") && (lstTableStore[indexStore].statusSerial == "OK"))
                    {


                        Int32 intCheck11 = InsertStoreOutbound(lstTableStore[indexStore].storeKanban,
                            lstTableStore[indexStore].Partno, lstTableStore[indexStore].serialNo,
                            lstTableStore[indexStore].qtySerial, lstTableStore[indexStore].statusSerial,
                            item.pickupKanban, item.Partno, item.serialNo, item.qtySerial,
                            item.statusSerial);

                        //indexStore = indexStore + 1;

                        if (intCheck11 == 3)
                        {

                            WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                            err1.ShowDialog();

                            if (lstTablePickupShow.Count > 0)
                            {
                                dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                            }

                            dgPickupKanban.Focus();

                            return;
                        }

                        //if (sysKanbanCodeStore != "8")
                        //{

                            Int32 intCheck1 = InsertOutbound(lstTableStore[indexStore].Partno, lstTableStore[indexStore].serialNo,
                            lstTableStore[indexStore].qtySerial, lstTableStore[indexStore].kanbanCode,
                            lstTableStore[indexStore].kanbanClass, lstTableStore[indexStore].remainderClass);

                            if (intCheck1 == 3)
                            {

                                WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                                err1.ShowDialog();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }

                        //}

                        //indexStore = indexStore + 1;

                        Int32 intCheck = UpdateStatusKanban(lstTableStore[indexStore].kanbanCode, 
                            lstTableStore[indexStore].kanbanClass, 
                            lstTableStore[indexStore].Partno, 
                            lstTableStore[indexStore].serialNo, 0, 
                            lstTableStore[indexStore].remainderClass);

                        if (intCheck == 3)
                        {

                            WarningErr1 err2 = new WarningErr1("Error Database Connection ");
                            err2.ShowDialog();

                            if (lstTablePickupShow.Count > 0)
                            {
                                dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                            }

                            dgPickupKanban.Focus();

                            return;
                        }

                        indexStore = indexStore + 1;
                    }

                }

            }
            else
            {
                foreach (var item in lstTableStore)
                {
                    if (item.statusSerial == "OK")
                    {
                        //if (sysKanbanCodeStore != "8")
                        //{
                            Int32 intCheck1 = InsertOutbound(item.Partno, item.serialNo, item.qtySerial, item.kanbanCode,
                                item.kanbanClass, item.remainderClass);

                            if (intCheck1 == 3)
                            {

                                WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                                err1.ShowDialog();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }


                            Int32 intCheck = UpdateStatusKanban(item.kanbanCode, item.kanbanClass, item.Partno, item.serialNo, 0, item.remainderClass);

                            if (intCheck == 3)
                            {

                                WarningErr1 err2 = new WarningErr1("Error Database Connection ");
                                err2.ShowDialog();

                                if (lstTablePickupShow.Count > 0)
                                {
                                    dgPickupKanban.CurrentRowIndex = lstTablePickupShow.Count - 1;
                                }

                                dgPickupKanban.Focus();

                                return;
                            }
                        //}
                    }
                }
            }

            WarningErr2 err = new WarningErr2("Data registration", "completed", "Green");
            err.ShowDialog();

            //******* disable BTScanLib
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();

            FrmOutboundMenu._Form_OutboundMenuInstance.Show();
            //Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }

        private Int32 UpdateStatusKanban(string kanbanCode, string kanbanClass, string partNo, string kanbanSerialNo, Int32 status, string remainderClass)
        {
            try
            {


                tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 qtyScan = 0;


                strSQL = "EXEC sp_update_status_kanban_HT @kanbanCode = '" + kanbanCode
                    + "' , @kanbanClass = '" + kanbanClass + "' , @partNo = '" + partNo
                    + "' , @serialNo = '" + kanbanSerialNo + "' , @userId = '" + FrmLogin._Form_LoginInstance.userName
                    + "' , @status = " + status + " , @remainderClass = '" + remainderClass
                    + "'";


                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                objCmd.ExecuteNonQuery();

                objConn.Close();
                objConn = null;


                return 1;


            }
            catch (Exception)
            {

                return 3;

            }


        }


        private Int32 InsertStoreOutbound(string storeKanban, string storePartNo, string storeSerial, string storeQuantity
            , string storeStatus, string pickupKanban, string pickupPartNo, string pickupSerial, string pickupQuantity
            , string pickupStatus)
        {
            try
            {


                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Decimal storeQuantityScan = Convert.ToDecimal(storeQuantity);
                Decimal pickupQuantityScan = Convert.ToDecimal(pickupQuantity);

                strSQL = "EXEC sp_insert_Store_Outbound @store_kanban = '" + storeKanban
                    + "' , @store_parts_no = '" + storePartNo
                    + "' , @store_serial_no = '" + storeSerial
                    + "' , @store_qty = " + storeQuantityScan
                    + " , @store_status = '" + storeStatus
                    + "' , @pickup_kanban = '" + pickupKanban
                    + "' , @pickup_parts_no = '" + pickupPartNo
                    + "' , @pickup_serial_no = '" + pickupSerial
                    + "' , @pickup_qty = " + pickupQuantityScan
                    + " , @pickup_status = '" + pickupStatus
                    + "' , @userId = '" + FrmLogin._Form_LoginInstance.userName                   
                    + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                objCmd.ExecuteNonQuery();

                objConn.Close();
                objConn = null;

                return 1;

            }
            catch (Exception)
            {
                return 3;
            }

        }


        private Int32 InsertOutbound(string partNo, string serial, string quantity, string kanbanCode, string kanbanClass,
            string remainderClass)
        {
            try
            {


                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Decimal qtyScan = Convert.ToDecimal(quantity);


                //strSQL = "EXEC sp_insert_Stock_Tracking_Outbound @partNo = '" + partNo
                //    + "' , @serial = '" + serial
                //    + "' , @qtySerial = " + quantity + " , @userId = '" + FrmLogin._Form_LoginInstance.userName
                //    + "'";

                strSQL = "EXEC sp_insert_Outbound @partNo = '" + partNo
                    + "' , @serial = '" + serial
                    + "' , @qtySerial = " + quantity + " , @userId = '" + FrmLogin._Form_LoginInstance.userName
                    + "' , @kanbanCode = '" + kanbanCode + "' , @kanbanClass = '" + kanbanClass
                    + "' , @remainderClass = '" + remainderClass
                    + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                objCmd.ExecuteNonQuery();

                objConn.Close();
                objConn = null;

                return 1;

            }
            catch (Exception)
            {
                return 3;
            }

        }



        private void btnNextPageStore_Click(object sender, EventArgs e)
        {
            RowStartStore = RowStartStore + 5;
            RowEndStore = RowEndStore + 5;

            SetTableStore(1);
        }

        private void btnBackPageStore_Click(object sender, EventArgs e)
        {
            RowStartStore = RowStartStore - 5;
            RowEndStore = RowEndStore - 5;

            if (RowStartStore < 0)
            {
                RowStartStore = 0;
                RowEndStore = 4;
            }

            SetTableStore(1);
        }

        private void btnNextPagePickup_Click(object sender, EventArgs e)
        {
            RowStartPickup = RowStartPickup + 5;
            RowEndPickup = RowEndPickup + 5;

            SetTablePickup(1);
        }

        private void btnBackPagePickup_Click(object sender, EventArgs e)
        {
            RowStartPickup = RowStartPickup - 5;
            RowEndPickup = RowEndPickup - 5;

            if (RowStartPickup < 0)
            {
                RowStartPickup = 0;
                RowEndPickup = 4;
            }

            SetTablePickup(1);
        }
    }
}