private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var us = new User();
            var userID = MainWindow.LoggedinId;
            us.LoadByPrimaryKey(userID);
            var dr = (DataRowView) lstTree.GetDataRecordByNode(lstTree.FocusedNode);
            if (dr == null) return;

            if (us.UserName != "admin")
            {
                XtraMessageBox.Show("You don't have the privilege to update reference number!", "Caution");
                return;
            }
            var rec = new ReceiveDoc();
            if (XtraMessageBox.Show("Are You Sure, You want to delete this?", "Confirmation", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataTable dtbl = rec.GetTransactionByRefNo((string) dr["RefNo"]);
                foreach (DataRow dataRow in dtbl.Rows)
                {
                   // AddReceiveDocDeleted(dataRow);
                    dataRow.Delete();
                }
                rec.MarkAsDeleted();
                rec.Save();

                XtraMessageBox.Show("Item successfully deleted.", "Success");
            }
            else
            {
                return;
            }
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            var us = new User();
            var userID = MainWindow.LoggedinId;
            us.LoadByPrimaryKey(userID);

            var dr = gridView1.GetFocusedDataRow();

            if (dr == null) return;

            int tranId = Convert.ToInt32(dr["ID"]);
            var rec = new ReceiveDoc();
            rec.LoadByPrimaryKey(tranId);
            var iss = new IssueDoc();
            iss.GetIssueByBatchAndId(rec.ItemID, rec.BatchNo, rec.ID);
            _dtDate.CustomFormat = "MM/dd/yyyy";
            DateTime dtCurrent = ConvertDate.DateConverter(_dtDate.Text);
            if ((rec.Date.Year != dtCurrent.Year && rec.Date.Month < 11) || (iss.RowCount != 0))
            {
                XtraMessageBox.Show("Unable to Delete, This Transaction has been processed. Try Loss and Adjustment.",
                                    "Unable to Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
            else
            {

                if (
                    XtraMessageBox.Show(
                        "Are You Sure, You want to delete this Transaction? You will not be able to restore this data.",
                        "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //AddDeletedRecieveDoc(rec);
                    rec.MarkAsDeleted();
                    rec.Save();

                    _dtDate.Value = DateTime.Now;
                    _dtDate.CustomFormat = "MM/dd/yyyy";
                    dtCurrent = ConvertDate.DateConverter(_dtDate.Text);
                    int yr = ((dtCurrent.Month > 10) ? dtCurrent.Year : dtCurrent.Year - 1);
                    DateTime dt1 = new DateTime(yr, 11, 1);
                    DateTime dt2 = new DateTime(dtCurrent.Year, dtCurrent.Month, dtCurrent.Day);
                    DataTable dtRec = rec.GetTransactionByDateRange(Convert.ToInt32(cboStores.EditValue), dt1, dt2);
                    gridReceives.DataSource = dtRec;
                }
            }
        }
        private static void HandleReceiveDocDeleting(int receiveDocID, int userID, ReceiveDoc rd)
        {
            //Check if there is a Related Picklists
            var pld = new PickListDetail();
            pld.LoadByReceiveDocID(receiveDocID);
            pld.Rewind();
            if(pld.RowCount > 0)
            {
                var pl = new PickList();
                pl.LoadByPrimaryKey(pld.PickListID);

                var order = new Order();
                order.LoadByPrimaryKey(pl.OrderID);

                string printedIDs = "";
                var stvs = new BLL.Issue();
                stvs.Where.PickListID.Value = pl.ID;
                stvs.Query.Load();
                stvs.Rewind();
                while (!stvs.EOF)
                {
                    printedIDs += stvs.IDPrinted.ToString(CultureInfo.InvariantCulture) + " ,";
                    stvs.MoveNext();
                }
                printedIDs = printedIDs.Remove(printedIDs.Length - 1, 1);
                throw new Exception(
                    string.Format("Please cancel/void the following Stvs: Ref No = {0} With PrintedIDs : {1}",
                                  order.RefNo,printedIDs));
            }

            // Add new record on ReceiveDocDeleted
            var recDel = ReceiveDocDeleted.AddNewLog(rd, userID);

            // Delete related ReceivePallet
            var receivePallet = new BLL.ReceivePallet();
            receivePallet.LoadByReceiveDocID(receiveDocID);
            receivePallet.Rewind();
            while(!receivePallet.EOF)
            {
                receivePallet.MarkAsDeleted();
                receivePallet.MoveNext();
            }

            // Delete related ReceivePriceConfirmation
            var receivePriceConfirmation = new ReceivePriceConfirmation();
            receivePriceConfirmation.LoadByReceiveDocID(rd.ID);
            receivePriceConfirmation.MarkAsDeleted();

            // Delete related ReceiveDocShortage
            var rdShr = new ReceiveDocShortage();
            rdShr.Where.ReceiveDocID.Value = rd.ID;
            rdShr.Query.Load();
            rdShr.Rewind();
            while(!rdShr.EOF)
            {
                rdShr.MarkAsDeleted();
                rdShr.MoveNext();
            }

            // Delete related ReceiveDocConfirmation
            var rdConf = new ReceiveDocConfirmation();
            rdConf.Where.ReceiveDocID.Value = rd.ID;
            rdConf.Query.Load();
            rdConf.Rewind();
            while(!rdConf.EOF)
            {
                rdConf.MarkAsDeleted();
                rdConf.MoveNext();
            }

            rd.MarkAsDeleted();

            var transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();
            transaction.BeginTransaction();
            try
            {
                receivePallet.Save();
                receivePriceConfirmation.Save();
                rdShr.Save();
                rdConf.Save();
                recDel.Save();
                rd.Save();
                transaction.CommitTransaction();
            }
            catch (Exception exception)
            {
                transaction.RollbackTransaction();
                throw;
            }
        }