private void txtScan_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Media.SoundPlayer barcodesoundplayergood = new System.Media.SoundPlayer();
            barcodesoundplayergood.SoundLocation = Application.StartupPath + "\\good.wav";
            System.Media.SoundPlayer barcodesoundplayerbad = new System.Media.SoundPlayer();
            barcodesoundplayerbad.SoundLocation = Application.StartupPath + "\\bad.wav";
            bool bBarcodeLength;
            txtScan.Focus();
            dgItemScan.ClearSelection();
            bool bBarcodeFound = false;

            if (e.KeyChar == (char)13)
            {
                //scan PO
                if (txtScan.Text.Substring(0, 2) == "AA")
                {

                    string sBarcodePO = txtScan.Text.Replace("AA", "");
                    txtPONumber.Text = sBarcodePO;
                    dgItemScan.DefaultCellStyle.SelectionBackColor = Color.Transparent;
                    dgItemScan.DefaultCellStyle.SelectionForeColor = Color.Transparent;
                    dgItemScan.ClearSelection();
                    dgItemScan.Rows.Clear();
                    barcodesoundplayergood.Play();
                    lblBarcodeNotFound.Visible = false;
                    lblPONotFound.Visible = false;
                    lblAllStockScanned.Visible = false;
                    lblPreScan.Visible = false;
                    lblScanned.Visible = true;

                    using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sPastelConnStr))
                    {
                        oConn.Open();
                        string sSql = "select h.ItemCode,h.Description PODescription,Inventory.Description GeneralDescription,Qty,";
                        sSql += "convert((select \"ExternalDoc\" from notes n where n.Code = h.itemcode and n.Subject ='Weight'),SQL_VARCHAR) Weight,";
                        sSql += "Convert((select \"ExternalDoc\" from notes where Code = h.itemcode and subject ='LinkedItem'),SQL_Varchar) LinkedItem from HistoryLines h ";
                        sSql += "left join Inventory on Inventory.ItemCode = Convert((select \"ExternalDoc\" from notes where Code = h.itemcode and subject ='LinkedItem'),SQL_Varchar)";
                        sSql += "where DocumentNumber = '" + sBarcodePO + "'";

                        PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                        while (rdReader.Read())
                        {
                            int n = dgItemScan.Rows.Add();
                            dgItemScan.Rows[n].Cells["clItemCode"].Value = rdReader["ItemCode"].ToString();
                            dgItemScan.Rows[n].Cells["clItemDescription"].Value = rdReader["PODescription"].ToString();
                            dgItemScan.Rows[n].Cells["clQuantity"].Value = rdReader["Qty"].ToString();
                            dgItemScan.Rows[n].Cells["clWeight"].Value = rdReader["Weight"].ToString();
                            if (dgItemScan.Rows[n].Cells["clWeight"].Value.ToString() == "")// as general item en nie specific nie
                            {
                                dgItemScan.Rows[n].Cells["clWeight"].Value = 1;
                            }
                            dgItemScan.Rows[n].Cells["clLinkedItem"].Value = rdReader["LinkedItem"].ToString();
                            if (dgItemScan.Rows[n].Cells["clLinkedItem"].Value.ToString() == "")
                            {
                                dgItemScan.Rows[n].Cells["clLinkedItem"].Value = dgItemScan.Rows[n].Cells["clItemCode"].Value;
                            }
                            dgItemScan.Rows[n].Cells["clGeneralItemDescription"].Value = rdReader["GeneralDescription"].ToString();
                            if (dgItemScan.Rows[n].Cells["clGeneralItemDescription"].Value.ToString() == "")
                            {
                                dgItemScan.Rows[n].Cells["clGeneralItemDescription"].Value = dgItemScan.Rows[n].Cells["clItemDescription"].Value;
                            }
                            dgItemScan.Rows[n].Cells["clScannedRawItems"].Value = "0";
                            bBarcodeFound = true;
                        }
                        lblPONotFound.Visible = false;
                        rdReader.Close();
                        oConn.Dispose();

                    }

                    if (!bBarcodeFound)
                    {
                        //barcodesoundplayer.SoundLocation = @"c:\bad.wav";
                        //barcodesoundplayer.Play();
                        barcodesoundplayerbad.Play();
                        //MessageBox.Show("Purchase Order Not Found", "Purchase Order Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        lblPONotFound.Visible = true;
                        txtScan.Enabled = true;
                        txtScan.Text = "";

                    }
                    else
                    {

                        //update quantitylefttoscan
                        bool bQL = false;
                        using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                        {
                            oConn.Open();
                            string sSql = "";
                            foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                            {
                                bQL = false;
                                sSql = "Select QuantityScanned, TotalBatchQuantity FROM SOLMSM ";
                                sSql += "inner join SOLMSMBM on BatchReference = BarcodeID ";
                                sSql += "where PONumber = '" + txtScan.Text.Replace("AA", "") + "'";
                                sSql += "and SOLMSMBM.POItemCode = '" + dgRow.Cells["clItemCode"].Value.ToString().Trim() + "'";
                                PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();

                                int iQScanned = 0;
                                while (rdReader.Read())
                                {

                                    iQScanned = iQScanned + Convert.ToInt16(rdReader["QuantityScanned"]);
                                    try
                                    {
                                        dgRow.Cells["clQuantityLeft"].Value = Convert.ToInt16(rdReader["TotalBatchQuantity"]) - (iQScanned / Convert.ToInt16(dgRow.Cells["clWeight"].Value));
                                    }
                                    catch
                                    {
                                        dgRow.Cells["clQuantityLeft"].Value = Convert.ToInt16(rdReader["TotalBatchQuantity"]) - (iQScanned / 1);
                                    }
                                    //dgRow.Cells["clQuantityLeft"].Value = Convert.ToInt16(rdReader["TotalBatchQuantity"]) - (Convert.ToInt16(rdReader["QuantityScanned"]) / Convert.ToInt16(dgRow.Cells["clWeight"].Value));
                                    bQL = true;
                                    dgRow.DefaultCellStyle.BackColor = Color.Red;
                                }

                                dgRow.Cells["clRealtimeScan"].Value = Convert.ToDouble(dgRow.Cells["clQuantity"].Value) - Convert.ToDouble(dgRow.Cells["clQuantityLeft"].Value) + " of " + dgRow.Cells["clQuantity"].Value.ToString().Trim();

                                rdReader.Close();
                                if (!bQL)
                                {
                                    dgRow.Cells["clQuantityLeft"].Value = dgRow.Cells["clQuantity"].Value.ToString();
                                    dgRow.DefaultCellStyle.BackColor = Color.Red;
                                    dgRow.Cells["clRealtimeScan"].Value = "0 of " + dgRow.Cells["clQuantity"].Value.ToString();

                                }

                                if (dgRow.Cells["clQuantityLeft"].Value.ToString() == "0")
                                {
                                    dgRow.Cells["clScannedRawItems"].ReadOnly = true;
                                    dgRow.DefaultCellStyle.BackColor = Color.Lime;
                                    dgRow.Cells["clRealtimeScan"].Value = dgRow.Cells["clQuantity"].Value.ToString() + " of " + dgRow.Cells["clQuantity"].Value.ToString();
                                }
                                dgRow.Cells["clRealtimeScan"].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                         //       dgRow.Cells["clRealtimeScan"].Style.BackColor = Color.Black;
                            }

                            oConn.Dispose();
                        }

                    }

                    txtScan.Text = "";
                }
                //UNDO
                else if (txtScan.Text == "UNDO")
                {
                    foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                    {

                        try
                        {
                            if (dgRow.Cells["clRawItemBarCode"].Value.ToString() == txtLastScan.Text && dgRow.Cells["clQuantityLeft"].Value.ToString() != "0")
                            {
                                lblBarcodeNotFound.Visible = false;
                                lblPONotFound.Visible = false;
                                lblAllStockScanned.Visible = false;
                                lblScanned.Visible = true;
                                barcodesoundplayergood.Play();
                                dgRow.Cells["clScannedRawItems"].Value = (Convert.ToDouble(dgRow.Cells["clScannedRawItems"].Value) - 1).ToString();
                                dgRow.Cells["clQuantityLeft"].Value = (Convert.ToDouble(dgRow.Cells["clQuantityLeft"].Value) + 1).ToString();
                                if (dgRow.Cells["clQuantityLeft"].Value == dgRow.Cells["clQuantity"].Value)
                                {
                                    dgRow.Cells["clRealtimeScan"].Value = "0" + " of " + dgRow.Cells["clQuantity"].Value.ToString().Trim();
                                }
                                else
                                {
                                    dgRow.Cells["clRealtimeScan"].Value = Convert.ToDouble(dgRow.Cells["clQuantity"].Value) - Convert.ToDouble(dgRow.Cells["clQuantityLeft"].Value) + " of " + dgRow.Cells["clQuantity"].Value.ToString().Trim();
                                }

                                decimal iSQ = Convert.ToDecimal(dgRow.Cells["clScannedRawItems"].Value);
                                decimal iQ = Convert.ToDecimal(dgRow.Cells["clQuantity"].Value);

                                if (iSQ > iQ)
                                {
                                    dgRow.Cells["clScannedRawItems"].Value = dgRow.Cells["clQuantity"].Value.ToString();
                                }
                                if (dgRow.Cells["clQuantityLeft"].Value.ToString() == "0")
                                {
                                    dgRow.Cells["clQuantityLeft"].Value = "0";
                                    dgRow.Cells["clScannedRawItems"].Value = dgRow.Cells["clQuantity"].Value.ToString();
                                    dgRow.DefaultCellStyle.BackColor = Color.Lime;
                                }

                                if (Convert.ToInt16(dgRow.Cells["clScannedRawItems"].Value.ToString()) <= 0)
                                {
                                    dgRow.Cells["clScannedRawItems"].Value = "0";

                                    bool bQL = false;
                                    using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                                    {
                                        oConn.Open();
                                        string sSql = "";
                                        sSql = "Select QuantityScanned, TotalBatchQuantity FROM SOLMSM ";
                                        sSql += "inner join SOLMSMBM on BatchReference = BarcodeID ";
                                        sSql += "where PONumber = '" + txtPONumber.Text + "'";
                                        sSql += "and SOLMSMBM.POItemCode = '" + dgRow.Cells["clItemCode"].Value.ToString().Trim() + "'";

                                        PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                                        int iQScanned = 0;
                                        while (rdReader.Read())
                                        {
                                            iQScanned = iQScanned + Convert.ToInt16(rdReader["QuantityScanned"]);
                                            dgRow.Cells["clQuantityLeft"].Value = Convert.ToInt16(rdReader["TotalBatchQuantity"]) - (iQScanned / Convert.ToInt16(dgRow.Cells["clWeight"].Value));

                                            //dgRow.Cells["clQuantityLeft"].Value = Convert.ToInt16(rdReader["TotalBatchQuantity"]) - (Convert.ToInt16(rdReader["QuantityScanned"]) / Convert.ToInt16(dgRow.Cells["clWeight"].Value));
                                            bQL = true;
                                            dgRow.DefaultCellStyle.BackColor = Color.Red;
                                        }
                                        dgRow.Cells["clRealtimeScan"].Value = "0" + " of " + dgRow.Cells["clQuantity"].Value.ToString().Trim();
                                        rdReader.Close();
                                        if (!bQL)
                                        {
                                            dgRow.Cells["clQuantityLeft"].Value = dgRow.Cells["clQuantity"].Value.ToString();

                                            dgRow.DefaultCellStyle.BackColor = Color.Red;

                                        }

                                        if (dgRow.Cells["clQuantityLeft"].Value.ToString() == "0")
                                        {
                                            dgRow.Cells["clScannedRawItems"].ReadOnly = true;
                                            dgRow.DefaultCellStyle.BackColor = Color.Lime;
                                        }
                                        oConn.Dispose();
                                    }
                                }
                            }
                            if (dgRow.Cells["clRawItemBarCode"].Value.ToString() == txtLastScan.Text && dgRow.Cells["clQuantityLeft"].Value.ToString() == "0")
                            {
                                lblBarcodeNotFound.Visible = false;
                                barcodesoundplayergood.Play();
                                dgRow.Cells["clScannedRawItems"].Value = (Convert.ToDouble(dgRow.Cells["clScannedRawItems"].Value) - 1).ToString();
                                dgRow.Cells["clRealtimeScan"].Value = dgRow.Cells["clScannedRawItems"].Value + " of " + dgRow.Cells["clQuantity"].Value;
                                dgRow.Cells["clQuantityLeft"].Value = (Convert.ToDouble(dgRow.Cells["clQuantityLeft"].Value) + 1).ToString();
                                dgRow.DefaultCellStyle.BackColor = Color.Red;
                            }
                        }
                        catch
                        {
                            barcodesoundplayerbad.Play();
                            //MessageBox.Show("Barcode Error", "Store Management Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            lblBarcodeNotFound.Visible = true;
                            txtScan.Text = "";
                            break;
                        }
                        this.ActiveControl = txtScan;

                    }
                    txtScan.Text = "";
                }
                else if (txtScan.Text == "CLEAR")
                {
                    lblScanned.Visible = true;
                    lblBarcodeNotFound.Visible = false;
                    lblPONotFound.Visible = false;
                    lblAllStockScanned.Visible = false;

                    dgItemScan.Rows.Clear();
                    txtPONumber.Text = "";
                    txtScan.Text = "";
                    txtScan.Enabled = true;
                    this.ActiveControl = txtScan;
                    barcodesoundplayergood.Play();

                }
                    //PrintBarcode
                else if(txtScan.Text.Replace(" ","") == "PRINTB")
                {
                    try
                    {
                        Forms.Manufacturing.BarcodePrinter frmBarcodePrinter = new Solsage_Process_Management_System.Forms.Manufacturing.BarcodePrinter();
                        frmBarcodePrinter.txtPrintDocumentNumber.Text = txtScan.Text;

                        barcodesoundplayergood.Play();
                        if (lblRecieveStock.Text == "Recieve Raw Stock")
                        {

                            using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                            {
                                oConn.Open();
                                string sSql = "";

                                foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                                {

                                    sSql = "select top  1 BarcodeID from SOLMSMBM where PONumber = '" + txtPONumber.Text.Trim() + "' and rtrim(POItemCode) Like '" + dgRow.Cells["clItemCode"].Value.ToString().Trim() + "'";

                                    object oRet = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteScalar();
                                    if (oRet == null)
                                    {

                                        sSql = "Insert into SOLMSMBM ";
                                        sSql += "(PONumber,POItemCode) ";
                                        sSql += "Values ('" + txtPONumber.Text.Trim() + "','" + dgRow.Cells["clItemCode"].Value.ToString().Trim() + "'";
                                        sSql += ")";
                                        int iReturn = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteNonQuery();
                                        sSql = "select Max(BarcodeID) BarcodeID from SOLMSMBM ";
                                        sRawItemBarcode = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteScalar().ToString().PadLeft(7, '0');

                                    }
                                    else
                                    {
                                        sRawItemBarcode = oRet.ToString().PadLeft(7, '0');
                                    }

                                    int n = frmBarcodePrinter.dgPrintBarcodeList.Rows.Add();
                                    frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clItemCode"].Value = dgItemScan.Rows[n].Cells["clItemCode"].Value.ToString();
                                    frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clItemDescription"].Value = dgItemScan.Rows[n].Cells["clItemDescription"].Value.ToString();
                                    frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clQuantity"].Value = dgItemScan.Rows[n].Cells["clQuantityLeft"].Value.ToString();
                                    frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clPrintQuantity"].Value = frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clQuantity"].Value.ToString();
                                    frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clRawItemBarcode"].Value = sRawItemBarcode;
                                    dgRow.Cells["clRawItemBarcode"].Value = sRawItemBarcode;

                                    if (frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clQuantity"].Value.ToString() == "0")
                                    {
                                        //frmBarcodePrinter.dgPrintBarcodeList.Rows[n].DefaultCellStyle.ForeColor = Color.Black;
                                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].DefaultCellStyle.BackColor = Color.Lime;
                                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clPrintQuantity"].ReadOnly = true;
                                    }

                                }
                                oConn.Dispose();

                            }

                            frmBarcodePrinter.frmStoreManagementScanning = this;
                            frmBarcodePrinter.ShowDialog();

                        }
                        txtScan.Text = "";
                        lblBarcodeNotFound.Visible = false;
                        txtScan.Enabled = true;
                        this.ActiveControl = txtScan;

                    }
                    catch
                    {
                        lblBarcodeNotFound.Visible = true;
                    }
                    txtScan.Text = "";
                }
                //scan raw stock
                else
                {
                  try
                  {
                    bBarcodeLength = true;
                    if (sScanningDocumentName == "Recieve Raw Stock")
                    {

                        string sBarcode = "0";
                        try
                        {
                            sBarcode = txtScan.Text.Replace(" ","").Substring(0, 7);
                            txtLastScan.Text = txtScan.Text.Substring(0, 7);
                            bBarcodeLength = true;
                        }

                        catch
                        {

                            bBarcodeLength = false;
                        }

                        if (bBarcodeLength)
                        {

                            foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                            {

                                if (dgRow.Cells["clRawItemBarCode"].Value.ToString() == sBarcode && dgRow.Cells["clQuantityLeft"].Value.ToString() != "0")
                                {
                                    lblAllStockScanned.Visible = false;
                                    lblBarcodeNotFound.Visible = false;
                                    lblScanned.Visible = true;
                                    barcodesoundplayergood.Play();
                                    dgRow.Cells["clScannedRawItems"].Value = (Convert.ToDouble(dgRow.Cells["clScannedRawItems"].Value) + 1).ToString();
                                    dgRow.Cells["clQuantityLeft"].Value = (Convert.ToDouble(dgRow.Cells["clQuantityLeft"].Value) - 1).ToString();
                                    dgRow.Cells["clRealtimeScan"].Value = Convert.ToDouble(dgRow.Cells["clQuantity"].Value) - Convert.ToDouble(dgRow.Cells["clQuantityLeft"].Value) + " of " + dgRow.Cells["clQuantity"].Value.ToString().Trim();
                                    decimal iSQ = Convert.ToDecimal(dgRow.Cells["clScannedRawItems"].Value);
                                    dgRow.DefaultCellStyle.BackColor = Color.Yellow;
                                    decimal iQ = Convert.ToDecimal(dgRow.Cells["clQuantity"].Value);
                                    if (iSQ > iQ)
                                    {
                                        dgRow.Cells["clScannedRawItems"].Value = dgRow.Cells["clQuantity"].Value.ToString();
                                    }
                                    if (dgRow.Cells["clQuantityLeft"].Value.ToString() == "0")
                                    {
                                        dgRow.Cells["clQuantityLeft"].Value = "0";
                                        //dgRow.Cells["clScannedRawItems"].Value = dgRow.Cells["clQuantity"].Value.ToString();
                                        dgRow.DefaultCellStyle.BackColor = Color.Lime;
                                    }
                                    bBarcodeFound = true;
                                    txtScan.Text = "";
                                }

                                if (dgRow.Cells["clRawItemBarCode"].Value.ToString() == sBarcode && dgRow.Cells["clQuantityLeft"].Value.ToString() == "0")
                                {
                                    //MessageBox.Show("All Stock Scanned", "Store Management Scanning ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    lblBarcodeNotFound.Visible = false;
                                    lblScanned.Visible = false;
                                    lblPONotFound.Visible = false;
                                    lblAllStockScanned.Visible = true;
                                    txtScan.Text = "";
                                    bBarcodeFound = true;

                                }

                            }
                            if (!bBarcodeFound)
                            {
                                //System.Media.SoundPlayer barcodesoundplayer = new System.Media.SoundPlayer();
                                //barcodesoundplayer.SoundLocation = @"c:\bad.wav";
                                //barcodesoundplayer.Play();
                                barcodesoundplayerbad.Play();
                                //MessageBox.Show("Barcode Not Found", "Store Management Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                lblBarcodeNotFound.Visible = true;
                                lblScanned.Visible = false;
                                lblPONotFound.Visible = false;
                                lblAllStockScanned.Visible = false;
                                txtScan.Text = "";
                            }

                        }
                        else
                        {
                            barcodesoundplayerbad.Play();
                            //MessageBox.Show("Barcode Not Found", "Store Management Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            lblBarcodeNotFound.Visible = true;
                            lblScanned.Visible = false;
                            lblPONotFound.Visible = false;
                            lblAllStockScanned.Visible = false;
                            txtScan.Text = "";

                        }
                    }
                  }
                  catch
                  {
                      barcodesoundplayerbad.Play();
                      //MessageBox.Show("Barcode Not Found", "Store Management Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      lblBarcodeNotFound.Visible = true;
                      lblScanned.Visible = false;
                      lblPONotFound.Visible = false;
                      lblAllStockScanned.Visible = false;
                      txtScan.Text = "";
                  }

                    //scan raw batch
                  if (sScanningDocumentName == "Recieve Raw Batch")
                  {
                      cmdSubmit.Enabled = true;
                      using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                      {
                            string sBarcode = txtScan.Text;
                            oConn.Open();

                            try
                            {
                                string sBarcodeTest = txtScan.Text.Replace("BB", "");
                                sBarcodeTest = txtScan.Text.Substring(0, 7);
                                bBarcodeLength = true;
                            }

                            catch
                            {

                                bBarcodeLength = false;
                            }

                            if (bBarcodeLength)
                            {
                                if (sBarcode.Substring(0, 2) == "BB")
                                {

                                    dgItemScan.Rows.Clear();
                                    lblBatchName.Visible = true;
                                    lblBN.Visible = true;
                                    dgItemScan.Columns["clQuantity"].Visible = true;
                                    dgItemScan.Columns["clQuantity"].Width = 130;

                                    //using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))

                                    //scan saved unfinished batch
                                    try
                                    {
                                        String sSql = "select BarcodeID,RawBatchName,FkItemCode,ItemDescription,Quantity,FkBatchId From SOLMWBM";
                                        sSql += " inner join SOLMRBI on FkRawBatchID = FkBatchID";
                                        sSql += " inner join SOLMRBS on RawBatchID = FkBatchID";
                                        sSql += " where BarcodeID = '" + Convert.ToInt16(txtScan.Text.Remove(0, 2)) + "'";

                                        PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                                        while (rdReader.Read())
                                        {
                                            lblBatchName.Text = rdReader["RawBatchName"].ToString();
                                            int n = dgItemScan.Rows.Add();
                                            dgItemScan.Rows[n].Cells["clItemCode"].Value = rdReader["FkItemCode"].ToString();
                                            dgItemScan.Rows[n].Cells["clItemDescription"].Value = rdReader["ItemDescription"].ToString();
                                            dgItemScan.Rows[n].Cells["clQuantity"].Value = rdReader["Quantity"].ToString();
                                            dgItemScan.Rows[n].Cells["clQuantityLeft"].Value = rdReader["Quantity"].ToString();
                                            dgItemScan.Rows[n].Cells["clScannedRawItems"].Value = "0";
                                            dgItemScan.Rows[n].Cells["clRecieveBatchBarcodeId"].Value = rdReader["BarcodeID"].ToString();
                                            txtRawBatchID.Text = rdReader["FkBatchId"].ToString();
                                            txtRecieveBatchBarcodeId.Text = rdReader["BarcodeID"].ToString();
                                            dgItemScan.Rows[n].DefaultCellStyle.BackColor = Color.Red;
                                        }

                                        sSql = "select * from SOLMRBRI where FkBarcodeId = '" + Convert.ToInt16(txtScan.Text.Remove(0, 2)) + "'";

                                        PsqlDataReader rdReader2 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                                        while (rdReader2.Read())
                                        {
                                            string sSql1 = "Select Top 1 GeneralItemCode,BatchReference, POItemCode, POItemDescription From SOLMSM where BatchReference = '" + rdReader2["RBRItemReference"].ToString() + "'";
                                            using (PsqlConnection oConn1 = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                                            {
                                                PsqlDataReader rdReader1 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql1, oConn1).ExecuteReader();
                                                while (rdReader1.Read())
                                                {
                                                    foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                                                    {
                                                        if (dgRow.Cells["clItemCode"].Value.ToString() == rdReader1["GeneralItemCode"].ToString())
                                                        {
                                                            int iRowIndex = dgRow.Index + 1;
                                                            dgItemScan.Rows[iRowIndex - 1].DefaultCellStyle.BackColor = Color.Lime;
                                                            dgItemScan.Rows.Insert(iRowIndex);
                                                            dgItemScan.Rows[iRowIndex].DefaultCellStyle.BackColor = Color.Orange;
                                                            dgItemScan.Rows[iRowIndex].DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 8);
                                                            dgItemScan.Rows[iRowIndex].Cells["clItemCode"].Value = rdReader1["POItemCode"].ToString();
                                                            dgItemScan.Rows[iRowIndex].Cells["clItemDescription"].Value = rdReader1["POItemDescription"].ToString();
                                                            dgItemScan.Rows[iRowIndex].Cells["clRawItemBarcode"].Value = rdReader1["BatchReference"].ToString().PadLeft(7, '0');
                                                            bBarcodeFound = true;
                                                            txtScan.Text = "";
                                                        }
                                                    }
                                                }
                                                oConn1.Dispose();
                                            }
                                        }

                                        rdReader.Close();
                                        oConn.Dispose();

                                        dgItemScan.DefaultCellStyle.SelectionBackColor = Color.Transparent;
                                        dgItemScan.DefaultCellStyle.SelectionForeColor = Color.Transparent;
                                        dgItemScan.ClearSelection();
                                        this.ActiveControl = txtScan;
                                    }

                                    //scan new batch
                                    catch
                                    {
                                        String sSql = "select BarcodeID,RawBatchName,FkItemCode,ItemDescription,Quantity,FkBatchId From SOLMWBM inner join SOLMRBI on FkRawBatchID = FkBatchId inner join SOLMRBS on RawBatchID = FkBatchId where BarcodeID = '" + Convert.ToInt16(txtScan.Text.Remove(0, 2)) + "'";

                                        PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                                        while (rdReader.Read())
                                        {
                                            lblBatchName.Text = rdReader["RawBatchName"].ToString();
                                            int n = dgItemScan.Rows.Add();
                                            dgItemScan.Rows[n].Cells["clItemCode"].Value = rdReader["FkItemCode"].ToString();
                                            dgItemScan.Rows[n].Cells["clItemDescription"].Value = rdReader["ItemDescription"].ToString();
                                            dgItemScan.Rows[n].Cells["clQuantity"].Value = rdReader["Quantity"].ToString();
                                            dgItemScan.Rows[n].Cells["clQuantityLeft"].Value = rdReader["Quantity"].ToString();
                                            dgItemScan.Rows[n].Cells["clScannedRawItems"].Value = "0";
                                            dgItemScan.Rows[n].Cells["clRecieveBatchBarcodeId"].Value = rdReader["BarcodeID"].ToString();
                                            txtRawBatchID.Text = rdReader["FkBatchId"].ToString();
                                            txtRecieveBatchBarcodeId.Text = rdReader["BarcodeID"].ToString();
                                            dgItemScan.Rows[n].DefaultCellStyle.BackColor = Color.Red;
                                        }

                                        rdReader.Close();
                                        oConn.Dispose();

                                        dgItemScan.DefaultCellStyle.SelectionBackColor = Color.Transparent;
                                        dgItemScan.DefaultCellStyle.SelectionForeColor = Color.Transparent;
                                        dgItemScan.ClearSelection();
                                        this.ActiveControl = txtScan;
                                    }

                                }

                                else
                                {
                                    string sSql1 = "Select Top 1 GeneralItemCode,BatchReference, POItemCode, POItemDescription From SOLMSM where BatchReference = '" + Convert.ToInt16(sBarcode) + "'";

                                    PsqlDataReader rdReader1 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql1, oConn).ExecuteReader();
                                    while (rdReader1.Read())
                                    {
                                        barcodesoundplayergood.Play();
                                        foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                                        {
                                            if (dgRow.Cells["clItemCode"].Value.ToString() == rdReader1["GeneralItemCode"].ToString())
                                            {
                                                int iRowIndex = dgRow.Index + 1;
                                                dgItemScan.Rows[iRowIndex - 1].DefaultCellStyle.BackColor = Color.Lime;
                                                dgItemScan.Rows.Insert(iRowIndex);
                                                dgItemScan.Rows[iRowIndex].DefaultCellStyle.BackColor = Color.Orange;
                                                dgItemScan.Rows[iRowIndex].DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 8);
                                                dgItemScan.Rows[iRowIndex].Cells["clItemCode"].Value = rdReader1["POItemCode"].ToString();
                                                dgItemScan.Rows[iRowIndex].Cells["clItemDescription"].Value = rdReader1["POItemDescription"].ToString();
                                                dgItemScan.Rows[iRowIndex].Cells["clRawItemBarcode"].Value = rdReader1["BatchReference"].ToString().PadLeft(7, '0');
                                                bBarcodeFound = true;
                                                txtScan.Text = "";
                                                break;
                                            }
                                        }
                                    }

                                    if (!bBarcodeFound)
                                    {
                                        barcodesoundplayerbad.Play();
                                        MessageBox.Show("Barcode Not Found", "Store Management Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        txtScan.Text = "";
                                    }
                                    rdReader1.Close();
                                    oConn.Dispose();
                                }
                            }
                            else
                            {
                                barcodesoundplayerbad.Play();
                                MessageBox.Show("Barcode Not Found", "Store Management Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                txtScan.Text = "";
                            }
                        }
                        this.ActiveControl = txtScan;

                  }

                    bool bAllScanned = false;
                    foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                    {
                        if (dgRow.DefaultCellStyle.BackColor == Color.Red)
                        {
                            bAllScanned = false;
                            break;
                        }
                        else
                        {
                            bAllScanned = true;
                        }
                    }
                    if (bAllScanned)
                    {
                        cmdSubmit.Enabled = true;
                    }

                }
                dgItemScan.ClearSelection();
                txtScan.Text = "";
                this.ActiveControl = txtScan;
            }
        }
        private void cmdPrintBarcode_Click(object sender, EventArgs e)
        {
            txtScan.Enabled = true;
            Forms.Manufacturing.BarcodePrinter frmBarcodePrinter = new Solsage_Process_Management_System.Forms.Manufacturing.BarcodePrinter();
            frmBarcodePrinter.txtPrintDocumentNumber.Text = txtScan.Text;

            if (lblRecieveStock.Text == "Recieve Raw Stock")
            {
                using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                {
                    oConn.Open();
                    string sSql = "";

                    foreach (DataGridViewRow dgRow in dgItemScan.Rows)
                    {

                        sSql = "select top  1 BarcodeID from SOLMSMBM where PONumber = '" + txtPONumber.Text.Trim() + "' and rtrim(POItemCode) Like '" + dgRow.Cells["clItemCode"].Value.ToString().Trim() + "'";

                        object oRet = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteScalar();
                        if (oRet == null)
                        {

                            sSql = "Insert into SOLMSMBM ";
                            sSql += "(PONumber,POItemCode) ";
                            sSql += "Values ('" + txtScan.Text.Replace("AA","").Trim() + "','" + dgRow.Cells["clItemCode"].Value.ToString().Trim() + "'";
                            sSql += ")";
                            int iReturn = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteNonQuery();
                            sSql = "select Max(BarcodeID) BarcodeID from SOLMSMBM ";
                            sRawItemBarcode = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteScalar().ToString().PadLeft(7, '0');

                        }
                        else
                        {
                            sRawItemBarcode = oRet.ToString().PadLeft(7, '0');
                        }

                        int n = frmBarcodePrinter.dgPrintBarcodeList.Rows.Add();
                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clItemCode"].Value = dgItemScan.Rows[n].Cells["clItemCode"].Value.ToString();
                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clItemDescription"].Value = dgItemScan.Rows[n].Cells["clItemDescription"].Value.ToString();
                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clQuantity"].Value = dgItemScan.Rows[n].Cells["clQuantityLeft"].Value.ToString();
                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clPrintQuantity"].Value = frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clQuantity"].Value.ToString();
                        frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clRawItemBarcode"].Value = sRawItemBarcode;
                        dgRow.Cells["clRawItemBarcode"].Value = sRawItemBarcode;

                        if (frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clQuantity"].Value.ToString() == "0")
                        {
                            //frmBarcodePrinter.dgPrintBarcodeList.Rows[n].DefaultCellStyle.ForeColor = Color.Black;
                            frmBarcodePrinter.dgPrintBarcodeList.Rows[n].DefaultCellStyle.BackColor = Color.Lime;
                            frmBarcodePrinter.dgPrintBarcodeList.Rows[n].Cells["clPrintQuantity"].ReadOnly = true;
                        }

                    }
                    oConn.Dispose();

                }

                frmBarcodePrinter.frmStoreManagementScanning = this;
                frmBarcodePrinter.ShowDialog();

            }
            txtScan.Text = "";
            txtScan.Enabled = true;
            this.ActiveControl = txtScan;
        }