/// <summary>
 /// Function for saveoredit
 /// </summary>
 public void SaveOrEdit()
 {
     PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
     try
     {
         int inRowCount = dgvProductDetails.RowCount;
         dgvProductDetails.ClearSelection();
         if (txtVoucherNo.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Enter voucher number");
             txtVoucherNo.Focus();
         }
         else if (BllPurchaseInvoice.PurchaseInvoiceVoucherNoCheckExistance(txtVoucherNo.Text.Trim(), strVoucherNo, decPurchaseInvoiceVoucherTypeId, decPurchaseMasterId) == 1)
         {
             Messages.InformationMessage("Voucher number already exist");
             txtVoucherNo.Focus();
         }
         else if (txtVoucherDate.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Select a date in between financial year");
             txtVoucherDate.Focus();
         }
         else if (txtInvoiceDate.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Select a date in between financial year");
             txtInvoiceDate.Focus();
         }
         else if (cmbCashOrParty.SelectedValue == null)
         {
             Messages.InformationMessage("Select Cash/Party");
             cmbCashOrParty.Focus();
         }
         else if (cmbPurchaseAccount.SelectedValue == null)
         {
             Messages.InformationMessage("Select PurchaseAccount");
             cmbPurchaseAccount.Focus();
         }
         else if (cmbCurrency.SelectedValue == null)
         {
             Messages.InformationMessage("Select Currency");
             cmbCurrency.Focus();
         }
         else if (cmbCurrency.SelectedValue.ToString() == "0")
         {
             Messages.InformationMessage("Select Currency");
             cmbCurrency.Focus();
         }
         else if (cmbPurchaseMode.Text == "Against PurchaseOrder" && cmbOrderNo.Text == string.Empty)
         {
             Messages.InformationMessage("Select OrderNo");
             cmbOrderNo.Focus();
         }
         else if (cmbPurchaseMode.Text == "Against MaterialReceipt" && cmbOrderNo.Text == string.Empty)
         {
             Messages.InformationMessage("Select ReceiptNo");
             cmbOrderNo.Focus();
         }
         else
         {
             if (RemoveIncompleteRowsFromGrid())
             {
                 if (dgvProductDetails.Rows.Count != 0)
                 {
                     if (dgvProductDetails.Rows[0].Cells["dgvtxtProductName"].Value == null && dgvProductDetails.Rows[0].Cells["dgvtxtProductCode"].Value == null)
                     {
                         MessageBox.Show("Can't save purchase invoice without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         dgvProductDetails.ClearSelection();
                         dgvProductDetails.Focus();
                     }
                     else
                     {
                         if (btnSave.Text == "Save")
                         {
                             if (dgvProductDetails.Rows[0].Cells["dgvtxtProductName"].Value == null)
                             {
                                 MessageBox.Show("Can't save purchase order without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                 dgvProductDetails.ClearSelection();
                                 dgvProductDetails.Focus();
                             }
                             else
                             {
                                 if (PublicVariables.isMessageAdd)
                                 {
                                     if (Messages.SaveMessage())
                                     {
                                         Save();
                                     }
                                 }
                                 else
                                 {
                                     Save();
                                 }
                             }
                         }
                         if (btnSave.Text == "Update")
                         {
                             if (QuantityCheckWithReference() == 1 && PartyBalanceCheckWithReference() == 1)
                             {
                                 if (dgvProductDetails.Rows[0].Cells["dgvtxtProductName"].Value == null)
                                 {
                                     MessageBox.Show("Can't Edit purchase invoice without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                     dgvProductDetails.ClearSelection();
                                     dgvProductDetails.Focus();
                                 }
                                 else
                                 {
                                     if (PublicVariables.isMessageEdit)
                                     {
                                         if (Messages.UpdateMessage())
                                         {
                                             Edit();
                                         }
                                     }
                                     else
                                     {
                                         Edit();
                                     }
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     Messages.InformationMessage("Can't save purchase invoice without atleast one product with complete details");
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI37:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }