/// <summary>
 /// Save or edit function checking the invalid entries of Form
 /// </summary>
 public void SaveOrEdit()
 {
     decimal decEdit = 0;
     SalesInvoiceBll BllSalesInvoice = new SalesInvoiceBll();
     try
     {
         dgvSalesInvoice.ClearSelection();
         int inRow = dgvSalesInvoice.RowCount;
         if (txtInvoiceNo.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Enter voucher number");
             txtInvoiceNo.Focus();
         }
         else if (BllSalesInvoice.SalesInvoiceInvoiceNumberCheckExistence(txtInvoiceNo.Text.Trim(), 0, DecSalesInvoiceVoucherTypeId) == true && btnSave.Text == "Save")
         {
             Messages.InformationMessage("Invoice number already exist");
             txtInvoiceNo.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 (cmbSalesMode.SelectedText == "NA" && cmbSalesModeOrderNo.SelectedValue == null)
         {
             Messages.InformationMessage("Select a Order No");
             cmbSalesModeOrderNo.Focus();
         }
         else if (cmbSalesAccount.SelectedValue == null)
         {
             Messages.InformationMessage("Select SalesAccount");
             cmbSalesAccount.Focus();
         }
         else if (cmbSalesMan.SelectedValue == null)
         {
             Messages.InformationMessage("Select SalesMan");
             cmbSalesMan.Focus();
         }
         else if (txtDate.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Select Credit period");
             txtCreditPeriod.Focus();
         }
         else if (inRow - 1 == 0 || dgvSalesInvoice.Rows[0].Cells["dgvtxtSalesInvoiceProductId"].Value as string == string.Empty)
         {
             Messages.InformationMessage("Can't save Sales Invoice without atleast one product with complete details");
             dgvSalesInvoice.Focus();
         }
         else
         {
             if (RemoveIncompleteRowsFromGrid())
             {
                 if (dgvSalesInvoice.Rows[0].Cells["dgvtxtSalesInvoiceProductName"].Value == null && dgvSalesInvoice.Rows[0].Cells["dgvtxtSalesInvoiceQty"].Value == null)
                 {
                     MessageBox.Show("Can't save Sales Invoice without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     dgvSalesInvoice.ClearSelection();
                     dgvSalesInvoice.Focus();
                 }
                 else
                 {
                     if (btnSave.Text == "Save")
                     {
                         if (dgvSalesInvoice.Rows[0].Cells["dgvtxtSalesInvoiceProductName"].Value == null)
                         {
                             MessageBox.Show("Can't save Sales Invoice without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             dgvSalesInvoice.ClearSelection();
                             dgvSalesInvoice.Focus();
                         }
                         else
                         {
                             if (PublicVariables.isMessageAdd)
                             {
                                 if (Messages.SaveMessage())
                                 {
                                     RemoveIncompleteRowsFromAdditionalCostGrid();
                                     SaveFunction();
                                 }
                             }
                             else
                             {
                                 RemoveIncompleteRowsFromAdditionalCostGrid();
                                 SaveFunction();
                             }
                         }
                     }
                     if (btnSave.Text == "Update")
                     {
                         if (QuantityCheckWithReference() == 1 && PartyBalanceCheckWithReference() == 1)
                         {
                             if (dgvSalesInvoice.Rows[0].Cells["dgvtxtSalesInvoiceProductName"].Value == null)
                             {
                                 MessageBox.Show("Can't Edit Sales Invoice without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                 dgvSalesInvoice.ClearSelection();
                                 dgvSalesInvoice.Focus();
                             }
                             else if (decEdit == 1)
                             {
                                 MessageBox.Show("Can't Edit SalesInvoice with Invalid Quantity", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                 dgvSalesInvoice.ClearSelection();
                                 dgvSalesInvoice.Focus();
                             }
                             else
                             {
                                 if (PublicVariables.isMessageEdit)
                                 {
                                     if (Messages.UpdateMessage())
                                     {
                                         EditFunction();
                                     }
                                 }
                                 else
                                 {
                                     EditFunction();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SI: 69" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }