protected void btnAdjust_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            int selectedItemsCode = int.Parse(hfAdjustItemID.Value);
            int adjustmentQty = int.Parse(txtAdjustQty.Value);

            if (rdbRemoval.Checked)
                adjustmentQty = adjustmentQty * -1;

            DataModelEntities entities = new DataModelEntities();
            Item item = entities.Items.First(u => u.ID == selectedItemsCode);
            item.Balance_Quantity = item.Balance_Quantity != null ? item.Balance_Quantity + (adjustmentQty) : adjustmentQty;

            PriceManagerDAL.StockLedger dborder = new PriceManagerDAL.StockLedger();
            dborder.User_Code = item.UserCode;
            dborder.AccountID = null;
            dborder.Type = null;
            dborder.TransactionID = null;
            dborder.ItemID = null;
            dborder.CustomLabel = item.CustomLabel;
            dborder.Quantity = adjustmentQty;
            dborder.Narration = txtAdjustNarration.Value;
            dborder.Stock_Ledger_Type = (int)Common.StockLegerType.Addjustment;
            dborder.Created_Date = System.DateTime.Now;
            dborder.ID = item.ID;
            entities.AddToStockLedgers(dborder);
            entities.SaveChanges();

            BindItemss();
        }
    }
    private void AddNewItems()
    {
        Item item = new Item();
        item.CustomLabel = txtCustomLabel.Value;
        item.Description = txtDesc.Value;
        item.Length = txtLength.Value;
        item.Height = txtHeight.Value;
        item.Weight = txtWeight.Value;
        item.Width = txtWidth.Value;
        item.UserCode = UserKey;

        /*Updated by javed*/
        if (!string.IsNullOrEmpty(txtInitialQty.Value))
            item.Opening_Quantity = int.Parse(txtInitialQty.Value);

        item.Balance_Quantity = item.Opening_Quantity != null ? item.Opening_Quantity : 0;

        if (!string.IsNullOrEmpty(txtMinimumThreshold.Value))
            item.Minimum_Threshold = int.Parse(txtMinimumThreshold.Value);

        DataModelEntities entities = new DataModelEntities();
        entities.AddToItems(item);
        entities.SaveChanges();

        if (item.Opening_Quantity != null)
        {
            PriceManagerDAL.StockLedger dborder = new PriceManagerDAL.StockLedger();
            dborder.User_Code = item.UserCode;
            dborder.AccountID = null;
            dborder.Type = null;
            dborder.TransactionID = null;
            dborder.ItemID = null;
            dborder.CustomLabel = item.CustomLabel;
            dborder.Quantity = item.Opening_Quantity;
            dborder.Narration = "Item Added To Stock";
            dborder.Stock_Ledger_Type = (int)Common.StockLegerType.Opening;
            dborder.Created_Date = System.DateTime.Now;
            dborder.ID = item.ID;
            entities.AddToStockLedgers(dborder);
            entities.SaveChanges();
        }

        BindItemss();
    }
    public void SaveTransactions(List<ParcelItem> allItems, int UserCode)
    {
        DataModelEntities context = new DataModelEntities();
        PriceManagerDAL.User user = context.Users.FirstOrDefault(f => f.User_Code == UserCode);
        List<PriceManagerDAL.ParcelItem> eItems = context.ParcelItems.Where(p => p.User_Code == UserCode && p.Is_Active == true).ToList();
        Dictionary<int, int> itemsToUpdateQty = new Dictionary<int, int>();
        Dictionary<string, int> itemsToSendAlert = new Dictionary<string, int>();
        foreach (ParcelItem item in allItems)
        {
            try
            {
                // check if the item already does not exist in the database and insert it then.
                PriceManagerDAL.ParcelItem dbItem = eItems.FirstOrDefault(p => p.ItemID == item.ItemID && p.TransactionID == item.TransactionID);

                if (dbItem == null)
                {
                    dbItem = new PriceManagerDAL.ParcelItem();
                    dbItem.User_Code = UserCode;
                    dbItem.AccountID = int.Parse(item.AccountID);
                    dbItem.BuyerID = item.BuyerID;
                    dbItem.BuyerName = item.BuyerName;
                    dbItem.City = item.City;
                    dbItem.Country = item.Country;
                    dbItem.Created_Date = DateTime.Now;
                    dbItem.Currency = item.Currency;
                    dbItem.CustomLabel = item.CustomLabel;
                    dbItem.CustomLabelText = item.CustomLabelText;
                    dbItem.EmailAddress = item.EmailAddress;
                    dbItem.HasInsurance = item.HasInsurance;
                    dbItem.Insurance = Convert.ToDecimal(item.Insurance);
                    dbItem.Is_Active = true;
                    dbItem.ItemID = item.ItemID;
                    dbItem.ItemName = item.ItemName;
                    dbItem.Messages = item.Messages;
                    dbItem.Parcel_Status_Code = (int)Constant.ParcelStatusCode.Pending;
                    dbItem.Phone = item.Phone;
                    dbItem.PostalCode = item.PostalCode;
                    dbItem.PostCodeImageURL = item.PostCodeImageURL;
                    dbItem.Price = Convert.ToDecimal(item.Price);
                    dbItem.Quantity = item.Quantity;
                    dbItem.RecordNumber = item.RecordNumber;
                    dbItem.SaleRecordId = item.SaleRecordId;
                    dbItem.ShippingCost = Convert.ToDecimal(item.ShippingCost);
                    dbItem.ShippingMethod = item.ShippingMethod;
                    dbItem.State = item.State;
                    dbItem.Street = item.Street;
                    dbItem.Street2 = item.Street2;
                    dbItem.Street3 = item.Street3;
                    dbItem.TransactionID = item.TransactionID;
                    dbItem.Type = item.Type;
                    dbItem.AddressID = item.AddressID;
                    context.ParcelItems.AddObject(dbItem);

                }
                /*Create Order for inventory tracking*/
                PriceManagerDAL.Item product = context.Items.FirstOrDefault(f => f.CustomLabel == item.CustomLabel && f.UserCode == UserCode);
                if (product != null)
                {
                    /*First Check if item with the same ItemID and TransactionID does not exists in the order table*/
                    PriceManagerDAL.StockLedger checkorder = context.StockLedgers.FirstOrDefault(p => p.ItemID == item.ItemID && p.TransactionID == item.TransactionID && p.Type == item.Type && p.AccountID == item.AccountID);
                    if (checkorder == null)
                    {
                        PriceManagerDAL.StockLedger dborder = new PriceManagerDAL.StockLedger();
                        dborder.User_Code = UserCode;
                        dborder.AccountID = item.AccountID;
                        dborder.Type = item.Type;
                        dborder.TransactionID = item.TransactionID;
                        dborder.ItemID = item.ItemID;
                        dborder.CustomLabel = item.CustomLabel;
                        dborder.Quantity = item.Quantity * -1;
                        dborder.Narration = item.Type + " New Sale";
                        dborder.Stock_Ledger_Type = (int)Common.StockLegerType.NewSale;
                        dborder.Created_Date = System.DateTime.Now;
                        dborder.ID = product.ID;
                        context.AddToStockLedgers(dborder);

                        if (!itemsToUpdateQty.Any(a => a.Key == product.ID))
                            itemsToUpdateQty.Add(product.ID, item.Quantity);
                        else
                            itemsToUpdateQty[product.ID] = itemsToUpdateQty[product.ID] + item.Quantity;

                    }
                    else
                    {
                        /*Check if Qty is different from the existing order item*/
                        if (checkorder.Quantity != item.Quantity * -1)
                        {
                            if (!itemsToUpdateQty.Any(a => a.Key == product.ID))
                                itemsToUpdateQty.Add(product.ID, item.Quantity - (int)checkorder.Quantity);
                            else
                                itemsToUpdateQty[product.ID] = itemsToUpdateQty[product.ID] + (item.Quantity - (int)checkorder.Quantity);

                            checkorder.Quantity = item.Quantity;
                            checkorder.Modifed_Date = System.DateTime.Now;

                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Logging.WriteLog(LogType.Critical, ex.ToString());
            }
        }
        /*Update Items Balance Qty*/
        foreach (var item in itemsToUpdateQty)
        {
            PriceManagerDAL.Item updateItem = context.Items.FirstOrDefault(f => f.ID == item.Key);
            if (updateItem != null && updateItem.Balance_Quantity != null)
            {
                int previousBalance = updateItem.Balance_Quantity != null ? (int)updateItem.Balance_Quantity : 0;
                int currentBalance = previousBalance - item.Value;

                /*Add items if user min threshold alert is on and item is reaching to min threshold level*/
                if (user.Minimum_Threshold_Alert == true && updateItem.Minimum_Threshold != null && previousBalance > updateItem.Minimum_Threshold && currentBalance <= updateItem.Minimum_Threshold)
                    itemsToSendAlert.Add(updateItem.CustomLabel + " - " + updateItem.Description, currentBalance);

                /*Update item's balance quantity*/
                updateItem.Balance_Quantity = currentBalance;

            }
        }
        context.SaveChanges();

        /*Send Email Notification For Minimum Threshold*/
        if (itemsToSendAlert.Count > 0)
        {
            System.Threading.Thread t = new System.Threading.Thread(() => SendMinimumThresholdAlert(itemsToSendAlert, user));
            t.Start();
        }
    }