/// <summary>
        /// This needs to be called after FillInReceiveDocInformation has been called.
        /// </summary>
        /// <param name="rec">ReceiveDoc should be passed to this function having been saved and with the id field different from null</param>
        /// <param name="dr">ReceiveDoc should be passed to this function having been saved and with the id field different from null</param>
        private void HandleReceiveDocShortage(DataRowView dr, ReceiveDoc rec, int receiveDocID = 0)
        {
            var recShortage = new ReceiveDocShortage();
            bool shortagetoBeAdded = false;

            if (receiveDocID == 0)
            {
                receiveDocID = Convert.ToInt32(rec.DefaultView.Table.Select(String.Format("GUID = '{0}'", dr["GUID"]))[0]["ID"]);
                shortagetoBeAdded = true;
            }

            //if (rec.GetColumn("ShortageReasonID") == System.DBNull.Value || Convert.ToInt32(rec.GetColumn("ShortageReasonID")) == 1) return;

            if ((srm && Convert.ToBoolean(dr["IsDamaged"])) || (Convert.ToDecimal(dr["InvoicedQty"]) >= Convert.ToDecimal(dr["Pack Qty"])))
            {
                var item = new Item();
                item.LoadByPrimaryKey(Convert.ToInt32(dr["id"]));

                if (dr["ShortageReasonID"] == DBNull.Value &&  rec.ExpDate <= DateTimeHelper.ServerDateTime)
                {
                    dr["ShortageReasonID"] = ShortageReasons.Constants.DAMAGED;
                }

                if (dr["ShortageReasonID"] != DBNull.Value && (!(Convert.ToDecimal(dr["Pack Qty"]) == 0 && Convert.ToInt32(dr["ShortageReasonID"]) == ShortageReasons.Constants.NOT_RECEIVED)))
                {
                    int shortageReasonID = Convert.ToInt32(dr["ShortageReasonID"]);

                    recShortage.AddNew();
                    recShortage.ShortageReasonID = shortageReasonID;
                    recShortage.ReceiveDocID = receiveDocID;
                    recShortage.NoOfPacks = Convert.ToDecimal(dr["Pack Qty"]);

                    if (shortagetoBeAdded)
                    {
                        var receiveDoc = new ReceiveDoc();
                        receiveDoc.LoadByPrimaryKey(receiveDocID);
                        if (receiveDoc.InvoicedNoOfPack!=0 && receiveDoc.Quantity >= receiveDoc.InvoicedNoOfPack)
                        {
                            receiveDoc.InvoicedNoOfPack = receiveDoc.Quantity + recShortage.NoOfPacks;
                            receiveDoc.Save();
                        }

                    }
                }
                else
                {
                    XtraMessageBox.Show(
                        "Please enter the reason for the discrepancy in invoiced vs. received qty.", "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            recShortage.Save();
        }
        /// <summary>
        /// This method handles Receive of an Item with full NotReceived Reason
        /// </summary>
        /// <param name="rec"></param>
        /// <param name="dr"></param>
        private void HandleFullNotReceivedAndMultipleBatchPalletlization(ReceiveDoc rec,DataRowView dr)
        {
            DataRow firstEntry = null;
            bool noSoundItems = false;
            // Here: We Can Use One of the Items on a receipt //
            if ((rec.Quantity == 0 && !rec.IsColumnNull("ShortageReasonID") &&
                 rec.ShortageReasonID == ShortageReasons.Constants.NOT_RECEIVED))
            {
                firstEntry =
                    rec.DefaultView.Table.Select(String.Format("[ID] > 0 AND [IsDamaged] = 0")).FirstOrDefault();
            }

            //~ This is a multiple batch case ~//
            if ((rec.Quantity == 0 && rec.IsColumnNull("ShortageReasonID")))
            {
                  var oneAmongMultipleBatchsAndDbSavedGuid =
                        _dtRecGrid.Select(string.Format("CopyGUID = '{0}' And IsCopied = 1 ", dr["CopyGUID"]));

                DataRow[] dbSavedcounts = oneAmongMultipleBatchsAndDbSavedGuid.Any()
                    ? rec.DefaultView.Table.Select(String.Format("GUID = '{0}'",
                        oneAmongMultipleBatchsAndDbSavedGuid[0]["GUID"]))
                    : oneAmongMultipleBatchsAndDbSavedGuid;

                firstEntry = dbSavedcounts.Any()
                    ? dbSavedcounts.FirstOrDefault()
                    : rec.DefaultView.Table.Select(String.Format("[ID] > 0 AND [IsDamaged] = 0")).FirstOrDefault();
            }
            // If we cant find any normal receive in this receipt let's just use one of receiveDocID's randomly: This is just to save a not received Entry with zero quantity! (an awful recent request!)~//
            if (firstEntry == null && ((rec.Quantity == 0 && !rec.IsColumnNull("ShortageReasonID") &&
                rec.ShortageReasonID == ShortageReasons.Constants.NOT_RECEIVED)))
            {
                firstEntry =
                    rec.DefaultView.Table.Select(String.Format("[ID] > 0")).FirstOrDefault();
                if (firstEntry != null)
                {
                    noSoundItems = true;

                }
            }
            // This is  a Zero Quantity ReceiveDoc //
            if (firstEntry != null)
            {
                rec.PhysicalStoreID = Convert.ToInt16(firstEntry["PhysicalStoreID"]);
                rec.InventoryPeriodID = Convert.ToInt16(firstEntry["InventoryPeriodID"]);
                rec.Save();

                // Lets Create a Zero ReceivePallet in the same palletlocation as one of the Items in the receipt //
                var oneOfrecievePallet = new BLL.ReceivePallet();
                oneOfrecievePallet.LoadByReceiveDocID(Convert.ToInt16(firstEntry["ID"]));

                var newReceivePallet = new BLL.ReceivePallet();
                newReceivePallet.AddNew();
                newReceivePallet.IsOriginalReceive = true;
                newReceivePallet.ReceiveID = rec.ID;
                newReceivePallet.Balance = newReceivePallet.ReceivedQuantity = newReceivePallet.ReservedStock = 0;
                newReceivePallet.PalletID = oneOfrecievePallet.PalletID;
                newReceivePallet.PalletLocationID = oneOfrecievePallet.PalletLocationID;
                newReceivePallet.BoxSize = oneOfrecievePallet.BoxSize;
                newReceivePallet.Save();
            }

            if (!rec.IsColumnNull("ShortageReasonID") && rec.ShortageReasonID == BLL.ShortageReasons.Constants.NOT_RECEIVED)
            {
                var recShortage = new ReceiveDocShortage();
                recShortage.AddNew();
                recShortage.ShortageReasonID = rec.ShortageReasonID;
                recShortage.ReceiveDocID = rec.ID;
                recShortage.NoOfPacks = Convert.ToDecimal(dr["InvoicedQty"]);
                recShortage.Save();
            }
        }
        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;
            }
        }