/// <summary>
 /// Save or edit function , to check invalid entries
 /// </summary>
 public void SaveOrEdit()
 {
     try
     {
         PurchaseOrderBll BllPurchaseOrder = new PurchaseOrderBll();
         dgvPurchaseOrder.ClearSelection();
         int inRow = dgvPurchaseOrder.RowCount;
         if (txtOrderNo.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Enter voucher number");
             txtOrderNo.Focus();
         }
         else if (BllPurchaseOrder.PurchaseOrderNumberCheckExistence(txtOrderNo.Text.Trim(), 0, decPurchaseOrderTypeId) == true && btnSave.Text == "Save")
         {
             Messages.InformationMessage("Order number already exist");
             txtOrderNo.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 (txtDueDate.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Select due date");
             txtDueDate.Focus();
         }
         else if (txtDueDays.Text.Contains("-") && txtDueDays.Text != string.Empty)
         {
             Messages.InformationMessage("Due date should not be greater than order date");
             txtDueDate.Focus();
         }
         else if (inRow - 1 == 0)
         {
             Messages.InformationMessage("Can't save purchase order without atleast one product with complete details");
         }
         else
         {
             if (RemoveIncompleteRowsFromGrid())
             {
                 if (dgvPurchaseOrder.Rows[0].Cells["dgvtxtProductName"].Value == null && dgvPurchaseOrder.Rows[0].Cells["dgvtxtProductCode"].Value == null)
                 {
                     MessageBox.Show("Can't save purchase order without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     dgvPurchaseOrder.ClearSelection();
                     dgvPurchaseOrder.Focus();
                 }
                 else
                 {
                     if (btnSave.Text == "Save")
                     {
                         if (dgvPurchaseOrder.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);
                             dgvPurchaseOrder.ClearSelection();
                             dgvPurchaseOrder.Focus();
                         }
                         else
                         {
                             if (PublicVariables.isMessageAdd)
                             {
                                 if (Messages.SaveMessage())
                                 {
                                     SaveFunction();
                                 }
                             }
                             else
                             {
                                 SaveFunction();
                             }
                         }
                     }
                     if (btnSave.Text == "Update")
                     {
                         if (dgvPurchaseOrder.Rows[0].Cells["dgvtxtProductName"].Value == null)
                         {
                             MessageBox.Show("Can't Edit purchase order without atleast one product with complete details", "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             dgvPurchaseOrder.ClearSelection();
                             dgvPurchaseOrder.Focus();
                         }
                         else
                         {
                             if (PublicVariables.isMessageEdit)
                             {
                                 if (Messages.UpdateMessage())
                                 {
                                     EditFunction();
                                 }
                             }
                             else
                             {
                                 EditFunction();
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PO32:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }