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 FrmKanbanTransfer : Form
    {
        public static FrmKanbanTransfer _Form_KanbanTransferInstance;
        public String AppPath = "";
        public List<List<string>> tableList;
        List<DisplayTable> lstTableTemplary = new List<DisplayTable>();
        List<DisplayTable> lstTableNormal = new List<DisplayTable>();
        List<DisplayTable> lstTableTemplaryShow = new List<DisplayTable>();
        List<DisplayTable> lstTableNormalShow = new List<DisplayTable>();
        List<DisplayTable> lstTableTemplaryTemp = new List<DisplayTable>();
        List<DisplayTable> lstTableNormalTemp = new List<DisplayTable>();
        private BtScanLib btScanLib;
        private String sysKanbanCodeTemplary = "";        
        private String sysKanbanCodeNormal = "";
        private String sysKanbanPart = "";
        private String sysKanbanSerial = "";
        private String sysRow = "";
        private bool scanTemplary = true;
        public Int32 RowStartTemplary = 0;
        public Int32 RowEndTemplary = 9;
        public Int32 RowStartNormal = 0;
        public Int32 RowEndNormal = 9;

        public FrmKanbanTransfer()
        {
            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.FrmKanbanTransfer_KeyDown);

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

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

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

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

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

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

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

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

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

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

        }

        private void FrmKanbanTransfer_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_KanbanTransferInstance = 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;

            RowStartTemplary = 0;
            RowEndTemplary = 9;
            RowStartNormal = 0;
            RowEndNormal = 9;

            lblStaus1Templary.Visible = false;
            lblStaus2Templary.Visible = false;
            lblStaus3Templary.Visible = false;
            lblStaus4Templary.Visible = false;
            lblStaus5Templary.Visible = false;
            lblStaus1Normal.Visible = false;
            lblStaus2Normal.Visible = false;
            lblStaus3Normal.Visible = false;
            lblStaus4Normal.Visible = false;
            lblStaus5Normal.Visible = false;

            btnNextPageTemplary.Enabled = false;
            btnBackPageTemplary.Enabled = false;

            btnNextPageNormal.Enabled = false;
            btnBackPageNormal.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 SetTableTemplary(Int32 processStart)
        {

            lblStaus1Templary.Visible = false;
            lblStaus2Templary.Visible = false;
            lblStaus3Templary.Visible = false;
            lblStaus4Templary.Visible = false;
            lblStaus5Templary.Visible = false;


            lstTableTemplaryShow.Clear();

            if (lstTableTemplary.Count > 5)
            {
                btnNextPageTemplary.Enabled = true;
                btnBackPageTemplary.Enabled = true;
            }
            else
            {
                btnNextPageTemplary.Enabled = false;
                btnBackPageTemplary.Enabled = false;
            }

            if (processStart == 0)
            {
                RowStartTemplary = 0;
                RowEndTemplary = 4;
            }

            if ((RowStartTemplary > lstTableTemplary.Count - 1) && (lstTableTemplary.Count > 0))
            {
                RowStartTemplary = RowStartTemplary - 5;
                RowEndTemplary = RowEndTemplary - 5;
            }

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

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

                RowStartTemplary = (iPage * 5) - 5;
                RowEndTemplary = RowStartTemplary + 4;

            }

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

                iRow = iRow + 1;
                if ((iRow >= RowStartTemplary + 1) && (iRow <= RowEndTemplary + 1))
                {
                    //iRow = iRow + 1;
                    lstTableTemplaryShow.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:
                            lblStaus1Templary.Text = strStatus;
                            if (lblStaus1Templary.Text == "OK")
                            {
                                lblStaus1Templary.BackColor = Color.Green;
                                lblStaus1Templary.ForeColor = Color.White;
                            }
                            else if (lblStaus1Templary.Text == "NG")
                            {
                                lblStaus1Templary.BackColor = Color.Red;
                                lblStaus1Templary.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus1Templary.Text = "-";
                                lblStaus1Templary.BackColor = Color.White;
                                lblStaus1Templary.ForeColor = Color.Black;
                            }
                            lblStaus1Templary.Visible = true;
                            break;

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

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

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

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


                        default:
                            break;
                    }

                }
            }

            var blist = new BindingList<DisplayTable>(lstTableTemplaryShow);
            dgTemplaryKanban.DataSource = blist;

            InitialDataGridTemplaryKanban();

            if (lstTableTemplaryShow.Count > 0)
            {
                dgTemplaryKanban.CurrentRowIndex = lstTableTemplaryShow.Count - 1;
            }

            dgTemplaryKanban.Focus();

        }

        private void SetTableNormal(Int32 processStart)
        {

            lblStaus1Normal.Visible = false;
            lblStaus2Normal.Visible = false;
            lblStaus3Normal.Visible = false;
            lblStaus4Normal.Visible = false;
            lblStaus5Normal.Visible = false;


            lstTableNormalShow.Clear();

            if (lstTableNormal.Count > 5)
            {
                btnNextPageNormal.Enabled = true;
                btnBackPageNormal.Enabled = true;
            }
            else
            {
                btnNextPageNormal.Enabled = false;
                btnBackPageNormal.Enabled = false;
            }

            if (processStart == 0)
            {
                RowStartNormal = 0;
                RowEndNormal = 4;
            }

            if ((RowStartNormal > lstTableNormal.Count - 1) && (lstTableNormal.Count > 0))
            {
                RowStartNormal = RowStartNormal - 5;
                RowEndNormal = RowEndNormal - 5;
            }

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

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

                RowStartNormal = (iPage * 5) - 5;
                RowEndNormal = RowStartNormal + 4;

            }

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

                iRow = iRow + 1;
                if ((iRow >= RowStartNormal + 1) && (iRow <= RowEndNormal + 1))
                {
                    //iRow = iRow + 1;
                    lstTableNormalShow.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:
                            lblStaus1Normal.Text = strStatus;
                            if (lblStaus1Normal.Text == "OK")
                            {
                                lblStaus1Normal.BackColor = Color.Green;
                                lblStaus1Normal.ForeColor = Color.White;
                            }
                            else if (lblStaus1Normal.Text == "NG")
                            {
                                lblStaus1Normal.BackColor = Color.Red;
                                lblStaus1Normal.ForeColor = Color.White;
                            }
                            else
                            {
                                lblStaus1Normal.Text = "-";
                                lblStaus1Normal.BackColor = Color.White;
                                lblStaus1Normal.ForeColor = Color.Black;
                            }
                            lblStaus1Normal.Visible = true;
                            break;

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

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

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

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


                        default:
                            break;
                    }

                }
            }

            var blist = new BindingList<DisplayTable>(lstTableNormalShow);
            dgNormalKanban.DataSource = blist;

            InitialDataGridNormalKanban();

            if (lstTableNormalShow.Count > 0)
            {
                dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
            }

            dgNormalKanban.Focus();

        }

        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 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;

            }


        }

        void btScanLib_OnScan(string result)
        {
            //if (scanSotre == true)
            if (lstTableTemplary.Count == lstTableNormal.Count)
            {

                //Scan Templary Kanban
                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";
                string tempKanban = result;

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

                    try
                    {

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

                            //lblQtyAll.Text = quantity;

                            if (kanbanClass != "2")
                            {
                                //sysRow = (Convert.ToInt32(sysRow) + 1).ToString();
                                strStatus = "NG";
                                lstTableTemplary.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    tempKanban = tempKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableTemplary(3);

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

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

                                return;
                            }

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


                            if (intCheck == 4)
                            {
                                //strStatus = "NG";
                                //lstTableTemplary.Add(new DisplayTable
                                //{
                                //    Partno = partNoScan,
                                //    DRow = sysRow,
                                //    serialNo = kanbanSerialNo,
                                //    qtySerial = quantity,
                                //    statusSerial = strStatus,
                                //    tempKanban = tempKanban,
                                //    kanbanCode = kanbanCode,
                                //    kanbanClass = kanbanClass,
                                //    remainderClass = remainderClass
                                //});

                                //SetTableTemplary(3);

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

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

                                //if (lstTableTemplary.Count > 0)
                                //{
                                //    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                //}

                                //dgTemplaryKanban.Focus();

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

                                //return;


                            }
                            else if (intCheck == 2)
                            {
                                strStatus = "NG";
                                lstTableTemplary.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    tempKanban = tempKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableTemplary(3);

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

                                return;
                            }


                            lblTypeTemplary.Text = checkTypeKanban(sysKanbanCodeTemplary);

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

                            SetTableTemplary(3);

                            //var blist = new BindingList<DisplayTable>(lstTableTemplary);
                            //dgTemplaryKanban.DataSource = blist;

                            //InitialDataGridTemplaryKanban();

                            ////scanSotre = false;

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

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

                                }

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

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

                                }

                            }

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

                                SetTableTemplary(3);

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

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

                                return;
                            }

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


                            if (intCheck == 4)
                            {
                                strStatus = "NG";
                                lstTableTemplary.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    tempKanban = tempKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableTemplary(3);

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

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

                                return;


                            }
                            else if (intCheck == 2)
                            {
                                strStatus = "NG";
                                lstTableTemplary.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    tempKanban = tempKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableTemplary(3);

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

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

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

                                if (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

                                return;
                            }

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

                                SetTableTemplary(3);

                                //var blist = new BindingList<DisplayTable>(lstTableTemplary);
                                //dgTemplaryKanban.DataSource = blist;

                                //InitialDataGridTemplaryKanban();

                                //******* 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 (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

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

                                SetTableTemplary(3);

                                //var blist = new BindingList<DisplayTable>(lstTableTemplary);
                                //dgTemplaryKanban.DataSource = blist;

                                //InitialDataGridTemplaryKanban();

                                //******* 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 (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

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

                                SetTableTemplary(3);

                                //var blist = new BindingList<DisplayTable>(lstTableTemplary);
                                //dgTemplaryKanban.DataSource = blist;

                                //InitialDataGridTemplaryKanban();

                                //******* 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 (lstTableTemplary.Count > 0)
                                {
                                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                }

                                dgTemplaryKanban.Focus();

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

                                SetTableTemplary(3);

                                //var blist = new BindingList<DisplayTable>(lstTableTemplary);
                                //dgTemplaryKanban.DataSource = blist;

                                //InitialDataGridTemplaryKanban();

                                ////scanSotre = false;

                                //if (lstTableTemplary.Count > 0)
                                //{
                                //    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                                //}

                                //dgTemplaryKanban.Focus();
                            }
                        }



                    }
                    catch (Exception)
                    {

                    }

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

                    if (lstTableTemplary.Count > 0)
                    {
                        dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                    }

                    dgTemplaryKanban.Focus();
                }
            }
            else
            {
                //Scan Pick-up Kanban
                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";
                string normalKanban = result;

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

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

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

                                SetTableNormal(3);


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

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

                                return;
                            }

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


                            if (intCheck == 1)
                            {
                                strStatus = "NG";
                                lstTableNormal.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    normalKanban = normalKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableNormal(3);

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

                                return;


                            }
                            else if (intCheck == 2)
                            {
                                strStatus = "NG";
                                lstTableNormal.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    normalKanban = normalKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableNormal(3);

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

                                return;
                            }

                            lblQtyAll.Text = quantity;
                            lblTypeNormal.Text = checkTypeKanban(sysKanbanCodeNormal);

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

                            SetTableNormal(3);

                            //var blist = new BindingList<DisplayTable>(lstTableNormal);
                            //dgNormalKanban.DataSource = blist;

                            //InitialDataGridNormalKanban();

                            ////scanSotre = true;

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

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

                                }

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

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

                                }

                            }

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

                                SetTableNormal(3);


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

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

                                return;
                            }


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


                            if (intCheck == 1)
                            {
                                strStatus = "NG";
                                lstTableNormal.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    normalKanban = normalKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableNormal(3);

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

                                return;


                            }
                            else if (intCheck == 2)
                            {
                                strStatus = "NG";
                                lstTableNormal.Add(new DisplayTable
                                {
                                    Partno = partNoScan,
                                    DRow = sysRow,
                                    serialNo = kanbanSerialNo,
                                    qtySerial = quantity,
                                    statusSerial = strStatus,
                                    normalKanban = normalKanban,
                                    kanbanCode = kanbanCode,
                                    kanbanClass = kanbanClass,
                                    remainderClass = remainderClass
                                });

                                SetTableNormal(3);

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

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

                                if (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

                                return;
                            }


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

                                SetTableNormal(3);

                                //var blist = new BindingList<DisplayTable>(lstTableNormal);
                                //dgNormalKanban.DataSource = blist;

                                //InitialDataGridNormalKanban();

                                //******* 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 (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

                                SetTableNormal(3);

                                //var blist = new BindingList<DisplayTable>(lstTableNormal);
                                //dgNormalKanban.DataSource = blist;

                                //InitialDataGridNormalKanban();

                                //******* 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 (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

                                SetTableNormal(3);

                                //var blist = new BindingList<DisplayTable>(lstTableNormal);
                                //dgNormalKanban.DataSource = blist;

                                //InitialDataGridNormalKanban();

                                //******* 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 (lstTableNormalShow.Count > 0)
                                {
                                    dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                                }

                                dgNormalKanban.Focus();

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

                                SetTableNormal(3);

                                //var blist = new BindingList<DisplayTable>(lstTableNormal);
                                //dgNormalKanban.DataSource = blist;

                                //InitialDataGridNormalKanban();

                                ////scanSotre = true;

                                //if (lstTableNormal.Count > 0)
                                //{
                                //    dgNormalKanban.CurrentRowIndex = lstTableNormal.Count - 1;
                                //}

                                //dgNormalKanban.Focus();
                            }
                        }



                    }
                    catch (Exception)
                    {

                    }

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

                    if (lstTableNormalShow.Count > 0)
                    {
                        dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                    }

                    dgNormalKanban.Focus();
                }




            }


        }


        private void InitialDataGridTemplaryKanban()
        {
            int grid_width = 445;
            // hidden column and row header
            dgTemplaryKanban.ColumnHeadersVisible = true;
            dgTemplaryKanban.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 = 54;
            tbc1.MappingName = "Partno";
            tbc1.HeaderText = "Part no";
            tableStyle.GridColumnStyles.Add(tbc1);

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

            // Column 3
            DataGridTextBoxColumn tbc3 = new DataGridTextBoxColumn();
            tbc3.Width = 43;
            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);

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


        }

        private void InitialDataGridNormalKanban()
        {
            int grid_width = 445;
            // hidden column and row header
            dgNormalKanban.ColumnHeadersVisible = true;
            dgNormalKanban.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 = 54;
            tbc1.MappingName = "Partno";
            tbc1.HeaderText = "Part no";
            tableStyle.GridColumnStyles.Add(tbc1);

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

            // Column 3
            DataGridTextBoxColumn tbc3 = new DataGridTextBoxColumn();
            tbc3.Width = 43;
            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);

            dgNormalKanban.TableStyles.Clear();
            dgNormalKanban.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 FrmKanbanTransfer_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 (lstTableTemplary.Count > lstTableNormal.Count)
            {
                int row = dgTemplaryKanban.CurrentCell.RowNumber;

                //lstTableTemplary.RemoveAt(row);

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

                lstTableTemplary.RemoveAt(Convert.ToInt32(dgTemplaryKanban[row, 0].ToString()) - 1);

                if (lstTableTemplary.Count == 0)
                {
                    lblTypeTemplary.Text = string.Empty;
                    sysKanbanCodeTemplary = "";
                    sysKanbanCodeNormal = "";
                    sysKanbanPart = "";
                    sysKanbanSerial = "";
                    sysRow = "";
                }

                //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;
                lstTableTemplaryTemp.Clear();
                foreach (var item in lstTableTemplary)
                {

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


                    iRow = iRow + 1;
                }
                lstTableTemplary.Clear();
                foreach (var item in lstTableTemplaryTemp)
                {

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

                }

                SetTableTemplary(3);

                //var blist = new BindingList<DisplayTable>(lstTableTemplary);
                //dgTemplaryKanban.DataSource = blist;

                //InitialDataGridTemplaryKanban();

                //if (lstTableTemplary.Count > 0)
                //{
                //    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                //}

                //dgTemplaryKanban.Focus();
            }
            else
            {
                if (lstTableNormal.Count == 0)
                {
                    return;
                }

                int row = dgNormalKanban.CurrentCell.RowNumber;

                //lstTableNormal.RemoveAt(row);

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

                lstTableNormal.RemoveAt(Convert.ToInt32(dgTemplaryKanban[row, 0].ToString()) - 1);


                int iRow = 1;
                lstTableNormalTemp.Clear();
                foreach (var item in lstTableNormal)
                {

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


                    iRow = iRow + 1;
                }
                lstTableNormal.Clear();
                foreach (var item in lstTableNormalTemp)
                {

                    lstTableNormal.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 lstTableNormal)
                {

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

                    }

                }

                if (lstTableNormal.Count == 0)
                {
                    lblTypeNormal.Text = string.Empty;
                    lblQtyAll.Text = string.Empty;
                }
                else
                {
                    lblQtyAll.Text = quantityAll;
                }

                SetTableNormal(3);

                //var blist = new BindingList<DisplayTable>(lstTableNormal);
                //dgNormalKanban.DataSource = blist;

                //InitialDataGridNormalKanban();

                //if (lstTableNormal.Count > 0)
                //{
                //    dgNormalKanban.CurrentRowIndex = lstTableNormal.Count - 1;
                //}

                //dgNormalKanban.Focus();
            }

        }

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

            //var blist = new BindingList<DisplayTable>(lstTableTemplary);
            //dgTemplaryKanban.DataSource = blist;

            //InitialDataGridTemplaryKanban();

            //dgTemplaryKanban.Focus();


            //lstTableNormal.Clear();

            //var blist1 = new BindingList<DisplayTable>(lstTableNormal);
            //dgNormalKanban.DataSource = blist1;

            //InitialDataGridNormalKanban();

            //dgNormalKanban.Focus();

            lstTableTemplary.Clear();

            SetTableTemplary(0);

            lstTableNormal.Clear();

            SetTableNormal(0);

            lblQtyAll.Text = string.Empty;
            lblTypeTemplary.Text = string.Empty;
            lblTypeNormal.Text = string.Empty;
            sysKanbanCodeTemplary = "";
            sysKanbanCodeNormal = "";
            sysKanbanPart = "";
            sysKanbanSerial = "";
            sysRow = "";
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (lstTableTemplary.Count == 0)
            {
                WarningErr1 errEmptyData = new WarningErr1("Empty Data ");
                errEmptyData.ShowDialog();

                if (lstTableTemplary.Count > 0)
                {
                    dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                }

                dgTemplaryKanban.Focus();

                return;
            }

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

                        if (lstTableTemplary.Count > 0)
                        {
                            dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                        }

                        dgTemplaryKanban.Focus();

                        return;
                    }
                }
            }

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

                        if (lstTableNormalShow.Count > 0)
                        {
                            dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                        }

                        dgNormalKanban.Focus();

                        return;
                    }
                }
            }

            Int32 indexNormal = 0;

            foreach (var item in lstTableTemplary)
            {

                if ((item.statusSerial == "OK") && (lstTableNormal[indexNormal].statusSerial == "OK"))
                {

                    Int32 intCheck11 = InsertKanbanTransfer(
                            item.tempKanban, item.Partno, item.serialNo, item.qtySerial,
                            item.statusSerial, item.kanbanCode, item.kanbanClass, item.remainderClass,
                            lstTableNormal[indexNormal].normalKanban,
                            lstTableNormal[indexNormal].Partno, lstTableNormal[indexNormal].serialNo,
                            lstTableNormal[indexNormal].qtySerial, lstTableNormal[indexNormal].statusSerial,
                            lstTableNormal[indexNormal].kanbanCode, lstTableNormal[indexNormal].kanbanClass,
                            lstTableNormal[indexNormal].remainderClass);

                    indexNormal = indexNormal + 1;

                    if (intCheck11 == 3)
                    {

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

                        if (lstTableTemplary.Count > 0)
                        {
                            dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                        }

                        dgTemplaryKanban.Focus();

                        return;
                    }

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

                    Int32 intCheck = UpdateStatusKanban(sysKanbanCodeTemplary, "2", item.Partno, item.serialNo, 0, item.remainderClass);

                    if (intCheck == 3)
                    {

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

                        if (lstTableTemplary.Count > 0)
                        {
                            dgTemplaryKanban.CurrentRowIndex = lstTableTemplary.Count - 1;
                        }

                        dgTemplaryKanban.Focus();

                        return;
                    }

                }

            }

            Int32 indexTemp = 0;

            foreach (var item in lstTableNormal)
            {

                if ((item.statusSerial == "OK") && (lstTableTemplary[indexTemp].statusSerial == "OK"))
                {

                    Int32 intCheck12 = UpdateStatusKanban(sysKanbanCodeTemplary, "1", item.Partno, item.serialNo, 1, item.remainderClass);

                    indexTemp = indexTemp + 1;

                    if (intCheck12 == 3)
                    {

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

                        if (lstTableNormalShow.Count > 0)
                        {
                            dgNormalKanban.CurrentRowIndex = lstTableNormalShow.Count - 1;
                        }

                        dgNormalKanban.Focus();

                        return;
                    }
                }

            }

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

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

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

        private Int32 InsertKanbanTransfer(string tempKanban, string tempPartNo, string tempSerial, string tempQuantity
            , string tempStatus, string tempKanbanCode, string tempKanbanClass, string tempRemainderClass
            , string normalKanban, string normalPartNo, string normalSerial, string normalQuantity
            , string normalStatus, string normalKanbanCode, string normalKanbanClass, string normalRemainderClass)
        {
            try
            {


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

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

                Decimal tempQuantityScan = Convert.ToDecimal(tempQuantity);
                Decimal normalQuantityScan = Convert.ToDecimal(normalQuantity);

                strSQL = "EXEC sp_insert_Kanban_Transfer @temp_kanban = '" + tempKanban
                    + "' , @temp_parts_no = '" + tempPartNo
                    + "' , @temp_serial_no = '" + tempSerial
                    + "' , @temp_qty = " + tempQuantityScan
                    + " , @temp_status = '" + tempStatus
                    + "' , @temp_kanbanCode = '" + tempKanbanCode
                    + "' , @temp_kanbanClass = '" + tempKanbanClass
                    + "' , @temp_remainderClass = '" + tempRemainderClass
                    + "' , @normal_kanban = '" + normalKanban
                    + "' , @normal_parts_no = '" + normalPartNo
                    + "' , @normal_serial_no = '" + normalSerial
                    + "' , @normal_qty = " + normalQuantityScan
                    + " , @normal_status = '" + normalStatus
                    + "' , @normal_kanbanCode = '" + normalKanbanCode
                    + "' , @normal_kanbanClass = '" + normalKanbanClass
                    + "' , @normal_remainderClass = '" + normalRemainderClass
                    + "' , @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 void btnBackPageTemplary_Click(object sender, EventArgs e)
        {
            RowStartTemplary = RowStartTemplary - 5;
            RowEndTemplary = RowEndTemplary - 5;

            if (RowStartTemplary < 0)
            {
                RowStartTemplary = 0;
                RowEndTemplary = 4;
            }

            SetTableTemplary(1);
        }

        private void btnNextPageTemplary_Click(object sender, EventArgs e)
        {
            RowStartTemplary = RowStartTemplary + 5;
            RowEndTemplary = RowEndTemplary + 5;

            SetTableTemplary(1);
        }

        private void btnNextPageNormal_Click(object sender, EventArgs e)
        {
            RowStartNormal = RowStartNormal + 5;
            RowEndNormal = RowEndNormal + 5;

            SetTableNormal(1);
        }

        private void btnBackPageNormal_Click(object sender, EventArgs e)
        {
            RowStartNormal = RowStartNormal - 5;
            RowEndNormal = RowEndNormal - 5;

            if (RowStartNormal < 0)
            {
                RowStartNormal = 0;
                RowEndNormal = 4;
            }

            SetTableNormal(1);
        }
    }
}