/// <summary>
 /// Function to call the savefunction or editfunction after checking invalid entries
 /// </summary>
 public void SaveOrEdit()
 {
     MaterialReceiptBll bllMaterialReceiptMaster = new MaterialReceiptBll();
     try
     {
         dgvProduct.ClearSelection();
         int inRow = dgvProduct.RowCount;
         String strInvoiceNo = txtReceiptNo.Text.Trim();
         if (txtReceiptNo.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Enter voucher number");
             txtReceiptNo.Focus();
         }
         else if (bllMaterialReceiptMaster.MaterialReceiptNumberCheckExistence(txtReceiptNo.Text.Trim(), decMaterialReceiptVoucherTypeId) == true && btnSave.Text == "Save")
         {
             Messages.InformationMessage("Receipt number already exist");
             txtReceiptNo.Focus();
         }
         else if (txtDate.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Select a date in between financial year");
             txtDate.Focus();
         }
         else if (cmbCashOrParty.SelectedValue == null)
         {
             Messages.InformationMessage("Select Cash/Party");
             cmbCashOrParty.Focus();
         }
         else if (cmbcurrency.SelectedValue == null)
         {
             Messages.InformationMessage("Select Currency");
             cmbcurrency.Focus();
         }
         else if (inRow - 1 == 0)
         {
             Messages.InformationMessage("Can't save material receipt without atleast one product with complete details");
         }
         else
         {
             if (RemoveIncompleteRowsFromGrid())
             {
                 if (dgvProduct.Rows[0].Cells["dgvtxtProductName"].Value == null && dgvProduct.Rows[0].Cells["dgvtxtProductCode"].Value == null)
                 {
                     MessageBox.Show("Can't save material receipt without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     dgvProduct.ClearSelection();
                     dgvProduct.Focus();
                 }
                 else
                 {
                     if (btnSave.Text == "Save")
                     {
                         if (dgvProduct.Rows[0].Cells["dgvtxtProductName"].Value == null)
                         {
                             MessageBox.Show("Can't save material receipt without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             dgvProduct.ClearSelection();
                             dgvProduct.Focus();
                         }
                         else
                         {
                             if (PublicVariables.isMessageAdd)
                             {
                                 if (Messages.SaveMessage())
                                 {
                                     SaveFunction();
                                 }
                             }
                             else
                             {
                                 SaveFunction();
                             }
                         }
                     }
                     if (btnSave.Text == "Update")
                     {
                         if (QuantityCheckWithReference() == 1)
                         {
                             if (dgvProduct.Rows[0].Cells["dgvtxtProductName"].Value == null)
                             {
                                 MessageBox.Show("Can't Edit MateraialReceipt without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                 dgvProduct.ClearSelection();
                                 dgvProduct.Focus();
                             }
                             else if (decEdit == 1)
                             {
                                 MessageBox.Show("Can't Edit MateraialReceipt with Invalid Quantity", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                 dgvProduct.ClearSelection();
                                 dgvProduct.Focus();
                             }
                             else
                             {
                                 if (PublicVariables.isMessageEdit)
                                 {
                                     if (Messages.UpdateMessage())
                                     {
                                         EditFunction();
                                     }
                                 }
                                 else
                                 {
                                     EditFunction();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("MR32:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }