/// <summary>
 /// On cell end edit of dgvMultipleUnit
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvMultipleUnit_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         if (dgvMultipleUnit.Columns[e.ColumnIndex].Name == "dgvcmbmultipleunit")
         {
             UnitSP spUnit = new UnitSP();
             dgvMultipleUnit.Rows[e.RowIndex].Cells["dgvtxtunitname"].Value = spUnit.UnitName(Convert.ToDecimal(dgvMultipleUnit.Rows[e.RowIndex].Cells["dgvcmbmultipleunit"].Value));
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PMU:30" + ex.Message, "Open Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// On cell end edit of dgvMultipleUnit
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvMultipleUnit_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         if (dgvMultipleUnit.Columns[e.ColumnIndex].Name == "dgvcmbmultipleunit")
         {
             UnitSP spUnit = new UnitSP();
             dgvMultipleUnit.Rows[e.RowIndex].Cells["dgvtxtunitname"].Value = spUnit.UnitName(Convert.ToDecimal(dgvMultipleUnit.Rows[e.RowIndex].Cells["dgvcmbmultipleunit"].Value));
         }
     }
     catch (Exception ex)
     {
         formMDI.infoError.ErrorString = "PMU:30" + ex.Message;
     }
 }
        /// <summary>
        /// Work on cmbOpeningStock Leave event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbOpeningStock_Leave(object sender, EventArgs e)
        {
            try
            {
                if (cmbOpeningStock.SelectedIndex == 1)
                {
                    if (cmbUnit.SelectedIndex == -1)
                    {
                        Messages.InformationMessage("Select default unit");
                        cmbOpeningStock.SelectedIndex = 0;
                        cmbUnit.Focus();
                    }
                    if (!isstockPostingGridFil)//Checking during updation
                    {
                        if (isOpeningStockForUpdate)
                        {
                            OpeningStockGridFill();
                        }
                        if (isBatchForUpdate)
                        {
                            OpeningStockGridWithBathFill();
                        }
                    }
                    else
                    {
                        if (!isGridHasToClear)
                        {
                            dgvProductCreation.Rows.Clear();
                            isOpeningStockChanged = true;
                        }
                    }

                    if (cmbMultipleUnit.SelectedIndex == 1 && dtblForUnitIdInOpeNingStock != null && dtblForUnitIdInOpeNingStock.Rows.Count > 0)
                    {
                        UnitSP spUnit = new UnitSP();
                        DataTable dtbl = new DataTable();
                        decId = Convert.ToDecimal(cmbUnit.SelectedValue);
                        strUnitNameForGrid = spUnit.UnitName(decId);
                        DataRow dr = dtblForUnitIdInOpeNingStock.NewRow();
                        dr["dgvtxtUnitId"] = decId;
                        dr["unitName"] = strUnitNameForGrid;
                        dr["CnvertionRate"] = 0;
                        dr["quantities"] = 0;
                        dr["extra1"] = string.Empty;
                        dr["extra2"] = string.Empty;
                        dr["extraDate"] = DateTime.Now;
                        dtblForUnitIdInOpeNingStock.Rows.InsertAt(dr, (dtblForUnitIdInOpeNingStock.Rows.Count + 1));
                        dgvcmbUnit.DataSource = dtblForUnitIdInOpeNingStock;
                        dgvcmbUnit.DisplayMember = "unitName";
                        dgvcmbUnit.ValueMember = "dgvtxtunitId";
                        for (int i = 0; i < dgvProductCreation.Rows.Count; i++)
                        {
                            if (!dgvProductCreation.Rows[i].IsNewRow)
                            {
                                dgvProductCreation.Rows[i].Cells["dgvcmbUnit"].Value = dtblForUnitIdInOpeNingStock.Rows[0][1];
                            }
                        }
                    }
                    else if (decId != 0)
                    {
                        DefaultUnitFillingInProductGrid();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PC:115" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Work on cmbUnit Cell Leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbUnit_Leave(object sender, EventArgs e)
        {

            try
            {
                UnitSP spUnit = new UnitSP();
                DataTable dtbl = new DataTable();
                decId = Convert.ToDecimal(cmbUnit.SelectedValue);
                strUnitNameForGrid = spUnit.UnitName(decId);
                isSaveMulUnitCheck = false;
                if (dtblMulUnit != null)
                {
                    dtblMulUnit.Clear();
                }//if unit changed
                if (dtblForUnitIdInOpeNingStock != null)
                {
                    dtblForUnitIdInOpeNingStock.Clear();//if unit changed
                }
                if (dtblFromMulUnitForUpdate != null)
                {
                    dtblFromMulUnitForUpdate.Clear();//if unit changed
                }
                if (btnSave.Text == "Update")
                {
                    cmbMultipleUnit.SelectedIndex = 0;
                    isMulUnitFromRgister = false;
                    isCheck = true;
                }
                if (dgvProductCreation.Rows.Count != 0)
                {
                    for (int inI = 0; inI <= dgvProductCreation.Rows.Count - 1; inI++)
                    {
                        dgvProductCreation.Rows[inI].Cells["dgvtxtunit"].Value = strUnitNameForGrid;
                        DefaultUnitFillingInProductGrid();
                    }
                }
                else
                {
                    dgvProductCreation.Rows[0].Cells["dgvtxtunit"].Value = strUnitNameForGrid;
                    DefaultUnitFillingInProductGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PC:106" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// To woek on cmbMultipleUnit cell leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbMultipleUnit_Leave(object sender, EventArgs e)
        {
            try
            {
                //if (k.KeyCode != Keys.Escape)
                if (cmbMultipleUnit.SelectedIndex == 1)
                {
                    UnitSP spUnit = new UnitSP();
                    strProductName = txtName.Text.Trim();
                    decimal decUnitId = Convert.ToDecimal(cmbUnit.SelectedValue);
                    string strUnit = spUnit.UnitName(Convert.ToDecimal(cmbUnit.SelectedValue));
                    frmProductMultipleUnit frmMultipleUnitObj = new frmProductMultipleUnit();
                    frmMultipleUnitObj.MdiParent = formMDI.MDIObj;
                    frmProductMultipleUnit open = Application.OpenForms["frmProductMultipleUnit"] as frmProductMultipleUnit;
                    if (open == null)
                    {
                        if (isMulUnitFromRgister && btnSave.Text == "Update")
                        {
                            if (!isCheck)
                            {
                                MultipleUnitTableForUpdate();
                                frmMultipleUnitObj.CallFromProdutCreationForUpadte(this, strProductName, decProductIdForEdit, dtblMulUnitForEdit, strUnitNameForGrid, decUnitIdForUpdate);
                                isGridHasToClear = true;
                            }
                            else
                            {
                                if (strProductName != string.Empty)
                                {
                                    if (cmbUnit.SelectedIndex != -1)
                                    {
                                        if (isSaveMulUnitCheck && (decUnitId == decUnitIdSelectedWhenMulUntCalled))
                                        {
                                            frmMultipleUnitObj.CallFromProdutCreationAgain(this, strProductName, decUnitId, strUnit, dtblMulUnit);
                                            //txtPartNo.Focus();
                                        }
                                        else
                                        {
                                            frmMultipleUnitObj.CallFromProdutCreation(this, strProductName, decUnitId, strUnit);
                                            //txtPartNo.Focus();
                                        }
                                    }
                                    else
                                    {
                                        Messages.InformationMessage("Select default unit");
                                        cmbMultipleUnit.SelectedIndex = 0;
                                        cmbUnit.Focus();
                                    }
                                }
                                else
                                {
                                    Messages.InformationMessage("Enter product name");
                                    cmbMultipleUnit.SelectedIndex = 0;
                                    txtName.Focus();
                                }
                            }
                        }
                        else
                        {
                            if (strProductName != string.Empty)
                            {
                                if (cmbUnit.SelectedIndex != -1)
                                {
                                    if (isSaveMulUnitCheck && (decUnitId == decUnitIdSelectedWhenMulUntCalled))
                                    {
                                        frmMultipleUnitObj.CallFromProdutCreationAgain(this, strProductName, decUnitId, strUnit, dtblMulUnit);
                                        //txtPartNo.Focus();
                                    }
                                    else
                                    {
                                        frmMultipleUnitObj.CallFromProdutCreation(this, strProductName, decUnitId, strUnit);
                                        //txtPartNo.Focus();
                                    }
                                }
                                else
                                {
                                    Messages.InformationMessage("Select default unit");
                                    cmbMultipleUnit.SelectedIndex = 0;
                                    cmbUnit.Focus();
                                }
                            }
                            else
                            {
                                Messages.InformationMessage("Enter product name");
                                cmbMultipleUnit.SelectedIndex = 0;
                                txtName.Focus();
                            }
                        }
                    }
                    else if (open != null)
                    {
                        if (open.WindowState == FormWindowState.Minimized)
                        {
                            open.WindowState = FormWindowState.Normal;
                        }
                    }

                    if (dtblForUnitIdInOpeNingStock != null && dtblForUnitIdInOpeNingStock.Rows.Count > 0)
                    {
                        dgvcmbUnit.DataSource = dtblForUnitIdInOpeNingStock;
                        dgvcmbUnit.DisplayMember = "unitName";
                        dgvcmbUnit.ValueMember = "dgvtxtunitId";
                    }

                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("PC:103" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function to fill default Unit
        /// </summary>

        public void DefaultUnitFillingInProductGrid()
        {


            UnitSP spUnit = new UnitSP();
            string strName = spUnit.UnitName(decId);
            DataTable dtblUnit = new DataTable();

            dtblUnit.Columns.Add("UnitName", typeof(string));
            dtblUnit.Columns.Add("UnitId", typeof(decimal));
            if (btnSave.Text == "Update")
            {
                strName = spUnit.UnitName(Convert.ToDecimal(cmbUnit.SelectedValue));
                decId = Convert.ToDecimal(cmbUnit.SelectedValue);
            }
            DataRow dr = dtblUnit.NewRow();
            dr["UnitName"] = strName;
            dr["UnitId"] = decId;
            dtblUnit.Rows.Add(dr);
            DataRow drow = dtblUnit.NewRow();
            drow["UnitName"] = string.Empty;
            drow["UnitId"] = 0;
            dtblUnit.Rows.InsertAt(drow, 0);
            dgvcmbUnit.DataSource = dtblUnit;
            dgvcmbUnit.DisplayMember = "UnitName";
            dgvcmbUnit.ValueMember = "UnitId";
            for (int i = 0; i < dgvProductCreation.Rows.Count; i++)
            {
                if (!dgvProductCreation.Rows[i].IsNewRow)
                {
                    dgvProductCreation.Rows[i].Cells["dgvcmbUnit"].Value = decId;
                }
            }



        }
        /// <summary>
        /// Function for call from productRegister
        /// </summary>
        public void CallFromProductRegister(decimal decId, frmProductRegister frmProRegister)
        {
            try
            {
                base.Show();

                this.frmProductRegisterObj = frmProRegister;
                frmProductRegisterObj.Enabled = false;

                ProductSP spProduct = new ProductSP();
                ProductInfo infoProduct = new ProductInfo();
                StockPostingInfo infoStockPosting = new StockPostingInfo();
                BatchInfo infoBatch = new BatchInfo();
                StockPostingSP spStockposting = new StockPostingSP();
                BatchSP spBatch = new BatchSP();
                DataTable dtbl = new DataTable();
                UnitSP spUnit = new UnitSP();
                decProductIdForEdit = decId;
                infoProduct = spProduct.ProductView(decId);
                strUnitNameForGrid = spUnit.UnitName(infoProduct.UnitId);
                btnSave.Text = "Update";
                btnDelete.Enabled = true;
                //cmbUnit.Enabled = false;
                txtName.Text = infoProduct.ProductName;
                txtProductCode.Text = infoProduct.ProductCode;
                cmbGroup.SelectedValue = infoProduct.GroupId;
                cmbBrand.SelectedValue = infoProduct.BrandId;
                cmbUnit.SelectedValue = infoProduct.UnitId;
                if (spProduct.ProductReferenceCheck(decId))
                {
                    cmbUnit.Enabled = false;
                }

                decUnitIdForUpdate = infoProduct.UnitId;
                cmbSize.SelectedValue = infoProduct.SizeId;
                cmbModalNo.SelectedValue = infoProduct.ModelNoId;
                cmbTax.SelectedValue = infoProduct.TaxId;
                cmbTaxApplicableOn.SelectedItem = infoProduct.TaxapplicableOn;
                txtPurchaseRate.Text = infoProduct.PurchaseRate.ToString();
                txtSalesRate.Text = infoProduct.SalesRate.ToString();
                txtMrp.Text = infoProduct.Mrp.ToString();
                txtMinimumStock.Text = infoProduct.MinimumStock.ToString();
                txtMaximumStock.Text = infoProduct.MaximumStock.ToString();
                txtReorderLevel.Text = infoProduct.ReorderLevel.ToString();
                txtPartNo.Text = infoProduct.PartNo;
                cmbDefaultGodown.SelectedValue = infoProduct.GodownId;
                cmbDefaultRack.SelectedValue = infoProduct.RackId;

                if (infoProduct.IsBom)
                {
                    cmbBom.SelectedIndex = 1;
                    isBomFromRegister = true;
                }
                if (infoProduct.Ismultipleunit)
                {
                    cmbMultipleUnit.SelectedIndex = 1;
                    isMulUnitFromRgister = true;
                }


                if (infoProduct.Isopeningstock)
                {
                    isOpeningStockForUpdate = true;
                    OpeningStockGridFill();

                }

                if (infoProduct.IsallowBatch)
                {
                    OpeningStockGridWithBathFill();
                }
                else
                {
                    cmbAllowBatch.SelectedIndex = 0;
                    txtPartNo.Text = spBatch.PartNoReturn(decProductIdForEdit);
                }
                if (infoProduct.Ismultipleunit)
                {
                    cmbMultipleUnit.SelectedIndex = 1;
                }
                else
                {
                    cmbMultipleUnit.SelectedIndex = 0;
                }

                if (infoProduct.IsBom)
                {
                    cmbBom.SelectedIndex = 1;

                }
                else
                {
                    cmbBom.SelectedIndex = 0;
                }

                if (infoProduct.Isopeningstock)
                {
                    cmbOpeningStock.SelectedIndex = 1;
                }
                else
                {
                    cmbOpeningStock.SelectedIndex = 0;
                }


                if (infoProduct.IsActive)
                {
                    cbxActive.Checked = true;
                }
                else
                {
                    cbxActive.Checked = false;
                }
                if (infoProduct.IsshowRemember)
                {
                    cbxReminder.Checked = true;
                }
                else
                {
                    cbxReminder.Checked = false;
                }
                txtNarration.Text = infoProduct.Narration;


            }
            catch (Exception ex)
            {
                MessageBox.Show("PC:63" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// On cell end edit of dgvMultipleUnit
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvMultipleUnit_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         if (dgvMultipleUnit.Columns[e.ColumnIndex].Name == "dgvcmbmultipleunit")
         {
             UnitSP spUnit = new UnitSP();
             dgvMultipleUnit.Rows[e.RowIndex].Cells["dgvtxtunitname"].Value = spUnit.UnitName(Convert.ToDecimal(dgvMultipleUnit.Rows[e.RowIndex].Cells["dgvcmbmultipleunit"].Value));
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PMU:30" + ex.Message, "Open Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }