Пример #1
0
        public Inv_StockSupplier SetToModelObjectStockSupplierReceive(InvLeatherIssueItem model)
        {
            Inv_StockSupplier Entity = new Inv_StockSupplier();

            Entity.SupplierID      = model.SupplierID;
            Entity.StoreID         = Convert.ToByte(model.StoreIdIssueTo);
            Entity.RefChallanID    = model.ChallanID;
            Entity.ItemTypeID      = model.ItemType;
            Entity.LeatherType     = model.LeatherType;
            Entity.LeatherStatusID = model.LeatherStatus;
            Entity.UnitID          = model.UnitID;
            Entity.OpeningQty      = 0;
            Entity.ReceiveQty      = model.IssueQty;
            Entity.IssueQty        = 0;
            Entity.ClosingQty      = model.IssueQty;
            Entity.UpdateReason    = "issue";
            return(Entity);
        }
        public Inv_StockSupplier SetToModelObjectStockSupplier(InvLeatherTransferReceiveItem model)
        {
            Inv_StockSupplier Entity = new Inv_StockSupplier();

            Entity.SupplierID      = model.SupplierID;
            Entity.StoreID         = model.StoreID;
            Entity.RefChallanID    = model.ChallanID;
            Entity.PurchaseID      = model.PurchaseID;
            Entity.ItemTypeID      = model.ItemType;
            Entity.LeatherType     = model.LeatherType;
            Entity.LeatherStatusID = model.LeatherStatus;
            Entity.UnitID          = model.UnitID;
            Entity.OpeningQty      = 0; // model.StockQty;
            Entity.ReceiveQty      = model.ReceiveQty;
            Entity.IssueQty        = 0; // model.IssueQty;
            Entity.ClosingQty      = Entity.OpeningQty + model.ReceiveQty - Entity.IssueQty;
            Entity.UpdateReason    = "store Transfer Receive";

            return(Entity);
        }
Пример #3
0
        public ValidationMsg LeatherIssueConfirmed(InvLeatherIssue model, int userid)
        {
            _vmMsg = new ValidationMsg();
            try
            {
                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        #region Update Record Status

                        var OriginalEntity = _context.Inv_LeatherIssue.First(m => m.IssueID == model.IssueID);

                        OriginalEntity.ApprovedBy     = userid;
                        OriginalEntity.ApproveComment = model.ApproveComment;
                        OriginalEntity.RecordStatus   = "CNF";

                        #endregion

                        #region Update Store

                        if (model.LeatherIssueItemList != null)
                        {
                            foreach (InvLeatherIssueItem objLeatherIssueItem in model.LeatherIssueItemList)
                            {
                                objLeatherIssueItem.StoreIdIssueFrom = model.IssueFrom;
                                objLeatherIssueItem.StoreIdIssueTo   = model.IssueTo;

                                #region Store Issue

                                #region Supplier_Stock_Update

                                var CheckSupplierStock = (from i in _context.Inv_StockSupplier
                                                          where i.SupplierID == objLeatherIssueItem.SupplierID &&
                                                          i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                          i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                          i.LeatherStatusID == objLeatherIssueItem.LeatherStatus &&
                                                          i.StoreID == objLeatherIssueItem.StoreIdIssueFrom &&
                                                          i.PurchaseID == objLeatherIssueItem.PurchaseID
                                                          select i).Any();

                                if (!CheckSupplierStock)
                                {
                                    Inv_StockSupplier tblStockSupplier = SetToModelObjectStockSupplier(objLeatherIssueItem);
                                    _context.Inv_StockSupplier.Add(tblStockSupplier);
                                    _context.SaveChanges();
                                }
                                else
                                {
                                    var LastSupplierStock = (from i in _context.Inv_StockSupplier
                                                             where i.SupplierID == objLeatherIssueItem.SupplierID &&
                                                             i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                             i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                             i.LeatherStatusID == objLeatherIssueItem.LeatherStatus &&
                                                             i.StoreID == objLeatherIssueItem.StoreIdIssueFrom &&
                                                             i.PurchaseID == objLeatherIssueItem.PurchaseID
                                                             orderby i.TransectionID descending
                                                             select i).FirstOrDefault();


                                    Inv_StockSupplier objStockSupplier = new Inv_StockSupplier();

                                    objStockSupplier.SupplierID      = objLeatherIssueItem.SupplierID;
                                    objStockSupplier.StoreID         = Convert.ToByte(objLeatherIssueItem.StoreIdIssueFrom);
                                    objStockSupplier.RefChallanID    = objLeatherIssueItem.ChallanID;
                                    objStockSupplier.PurchaseID      = objLeatherIssueItem.PurchaseID;
                                    objStockSupplier.ItemTypeID      = objLeatherIssueItem.ItemType;
                                    objStockSupplier.LeatherType     = objLeatherIssueItem.LeatherType;
                                    objStockSupplier.LeatherStatusID = objLeatherIssueItem.LeatherStatus;
                                    objStockSupplier.UnitID          = objLeatherIssueItem.UnitID;

                                    objStockSupplier.OpeningQty   = LastSupplierStock.ClosingQty;
                                    objStockSupplier.ReceiveQty   = 0;
                                    objStockSupplier.IssueQty     = objLeatherIssueItem.IssueQty;
                                    objStockSupplier.ClosingQty   = LastSupplierStock.ClosingQty - objLeatherIssueItem.IssueQty;
                                    objStockSupplier.UpdateReason = "issue";

                                    _context.Inv_StockSupplier.Add(objStockSupplier);
                                    _context.SaveChanges();
                                }

                                #endregion

                                #region Item_Stock_Update

                                var CheckItemStock = (from i in _context.Inv_StockItem
                                                      where i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                      i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                      i.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                      i.StoreID == objLeatherIssueItem.StoreIdIssueFrom
                                                      select i).Any();

                                if (!CheckItemStock)
                                {
                                    Inv_StockItem objStockItem = new Inv_StockItem();

                                    objStockItem.ItemTypeID    = objLeatherIssueItem.ItemType;
                                    objStockItem.LeatherType   = objLeatherIssueItem.LeatherType;
                                    objStockItem.LeatherStatus = objLeatherIssueItem.LeatherStatus;
                                    objStockItem.StoreID       = Convert.ToByte(objLeatherIssueItem.StoreIdIssueFrom);
                                    objStockItem.UnitID        = objLeatherIssueItem.UnitID;

                                    objStockItem.OpeningQty = objLeatherIssueItem.StockQty;
                                    objStockItem.IssueQty   = objLeatherIssueItem.IssueQty;
                                    objStockItem.ReceiveQty = 0;
                                    objStockItem.ClosingQty = objLeatherIssueItem.StockQty - objLeatherIssueItem.IssueQty;

                                    _context.Inv_StockItem.Add(objStockItem);
                                    _context.SaveChanges();
                                }
                                else
                                {
                                    var LastItemInfo = (from i in _context.Inv_StockItem
                                                        where i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                        i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                        i.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                        i.StoreID == objLeatherIssueItem.StoreIdIssueFrom
                                                        orderby i.TransectionID descending
                                                        select i).FirstOrDefault();


                                    Inv_StockItem objStockItem = new Inv_StockItem();


                                    objStockItem.ItemTypeID    = LastItemInfo.ItemTypeID;
                                    objStockItem.LeatherType   = LastItemInfo.LeatherType;
                                    objStockItem.LeatherStatus = LastItemInfo.LeatherStatus;
                                    objStockItem.StoreID       = LastItemInfo.StoreID;
                                    objStockItem.UnitID        = objLeatherIssueItem.UnitID;

                                    objStockItem.OpeningQty = LastItemInfo.ClosingQty;
                                    objStockItem.ReceiveQty = 0;
                                    objStockItem.IssueQty   = objLeatherIssueItem.IssueQty;
                                    objStockItem.ClosingQty = LastItemInfo.ClosingQty - objLeatherIssueItem.IssueQty;

                                    _context.Inv_StockItem.Add(objStockItem);
                                    _context.SaveChanges();
                                }

                                #endregion

                                #region Daily_Stock_Update

                                var currentDate = System.DateTime.Now.Date;

                                var CheckDate = (from ds in _context.Inv_StockDaily
                                                 where ds.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                 ds.StoreID == objLeatherIssueItem.StoreIdIssueFrom &&
                                                 ds.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                 ds.LeatherType == objLeatherIssueItem.LeatherType && ds.StockDate == currentDate
                                                 select ds).Any();

                                if (CheckDate)
                                {
                                    var CurrentItem = (from ds in _context.Inv_StockDaily
                                                       where ds.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                       ds.StoreID == objLeatherIssueItem.StoreIdIssueFrom &&
                                                       ds.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                       ds.LeatherType == objLeatherIssueItem.LeatherType && ds.StockDate == currentDate
                                                       select ds).FirstOrDefault();

                                    CurrentItem.DailyIssueQty = CurrentItem.DailyIssueQty + objLeatherIssueItem.IssueQty;
                                    CurrentItem.ClosingQty    = CurrentItem.ClosingQty - objLeatherIssueItem.IssueQty;
                                    _context.SaveChanges();
                                }
                                else
                                {
                                    var PreviousRecord = (from ds in _context.Inv_StockDaily
                                                          where ds.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                          ds.StoreID == objLeatherIssueItem.StoreIdIssueFrom &&
                                                          ds.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                          ds.LeatherType == objLeatherIssueItem.LeatherType
                                                          orderby ds.TransectionID descending
                                                          select ds).FirstOrDefault();

                                    Inv_StockDaily objStockDaily = new Inv_StockDaily();

                                    objStockDaily.ItemTypeID    = objLeatherIssueItem.ItemType;
                                    objStockDaily.StoreID       = Convert.ToByte(objLeatherIssueItem.StoreIdIssueFrom);
                                    objStockDaily.UnitID        = objLeatherIssueItem.UnitID;
                                    objStockDaily.LeatherStatus = objLeatherIssueItem.LeatherStatus;
                                    objStockDaily.LeatherType   = objLeatherIssueItem.LeatherType;

                                    objStockDaily.OpeningQty      = (PreviousRecord == null ? 0 : PreviousRecord.ClosingQty);
                                    objStockDaily.DailyReceiveQty = 0;
                                    objStockDaily.DailyIssueQty   = objLeatherIssueItem.IssueQty;
                                    objStockDaily.ClosingQty      = objStockDaily.OpeningQty - objLeatherIssueItem.IssueQty;

                                    objStockDaily.StockDate = currentDate;

                                    _context.Inv_StockDaily.Add(objStockDaily);
                                    _context.SaveChanges();
                                }

                                #endregion

                                #endregion

                                #region storeReceive

                                //if (model.IssueFor == "Production" || model.IssueFor == "Job Order")
                                if (model.IssueFor == "Job Order")
                                {
                                    #region Supplier_Stock_Update

                                    var CheckSupplierStockForRec = (from i in _context.Inv_StockSupplier
                                                                    where i.SupplierID == objLeatherIssueItem.SupplierID &&
                                                                    i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                                    i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                                    i.LeatherStatusID == objLeatherIssueItem.LeatherStatus &&
                                                                    i.StoreID == objLeatherIssueItem.StoreIdIssueTo &&
                                                                    i.PurchaseID == objLeatherIssueItem.PurchaseID
                                                                    select i).Any();

                                    if (!CheckSupplierStockForRec)
                                    {
                                        Inv_StockSupplier Entity = new Inv_StockSupplier();

                                        Entity.SupplierID      = objLeatherIssueItem.SupplierID;
                                        Entity.StoreID         = Convert.ToByte(objLeatherIssueItem.StoreIdIssueTo);
                                        Entity.RefChallanID    = objLeatherIssueItem.ChallanID;
                                        Entity.PurchaseID      = objLeatherIssueItem.PurchaseID;
                                        Entity.ItemTypeID      = objLeatherIssueItem.ItemType;
                                        Entity.LeatherType     = objLeatherIssueItem.LeatherType;
                                        Entity.LeatherStatusID = objLeatherIssueItem.LeatherStatus;
                                        Entity.UnitID          = objLeatherIssueItem.UnitID;

                                        Entity.OpeningQty = 0;
                                        Entity.ReceiveQty = objLeatherIssueItem.IssueQty;
                                        Entity.IssueQty   = 0;
                                        Entity.ClosingQty = objLeatherIssueItem.IssueQty;

                                        Entity.UpdateReason = "Job Order Receive";
                                        _context.Inv_StockSupplier.Add(Entity);
                                        _context.SaveChanges();
                                    }
                                    else
                                    {
                                        var LastSupplierStock = (from i in _context.Inv_StockSupplier
                                                                 where i.SupplierID == objLeatherIssueItem.SupplierID &&
                                                                 i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                                 i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                                 i.LeatherStatusID == objLeatherIssueItem.LeatherStatus &&
                                                                 i.StoreID == objLeatherIssueItem.StoreIdIssueTo &&
                                                                 i.PurchaseID == objLeatherIssueItem.PurchaseID
                                                                 orderby i.TransectionID descending
                                                                 select i).FirstOrDefault();


                                        Inv_StockSupplier objStockSupplier = new Inv_StockSupplier();

                                        objStockSupplier.SupplierID      = objLeatherIssueItem.SupplierID;
                                        objStockSupplier.StoreID         = Convert.ToByte(objLeatherIssueItem.StoreIdIssueTo);
                                        objStockSupplier.RefChallanID    = objLeatherIssueItem.ChallanID;
                                        objStockSupplier.PurchaseID      = objLeatherIssueItem.PurchaseID;
                                        objStockSupplier.ItemTypeID      = objLeatherIssueItem.ItemType;
                                        objStockSupplier.LeatherType     = objLeatherIssueItem.LeatherType;
                                        objStockSupplier.LeatherStatusID = objLeatherIssueItem.LeatherStatus;
                                        objStockSupplier.UnitID          = objLeatherIssueItem.UnitID;

                                        objStockSupplier.OpeningQty = LastSupplierStock.ClosingQty;
                                        objStockSupplier.ReceiveQty = objLeatherIssueItem.IssueQty;
                                        objStockSupplier.IssueQty   = 0;
                                        objStockSupplier.ClosingQty = LastSupplierStock.ClosingQty + objLeatherIssueItem.IssueQty;

                                        objStockSupplier.UpdateReason = "Job Order Receive";

                                        _context.Inv_StockSupplier.Add(objStockSupplier);
                                        _context.SaveChanges();
                                    }

                                    #endregion

                                    #region Item_Stock_Update

                                    var CheckItemStockForRec = (from i in _context.Inv_StockItem
                                                                where i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                                i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                                i.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                                i.StoreID == objLeatherIssueItem.StoreIdIssueTo
                                                                select i).Any();

                                    if (!CheckItemStockForRec)
                                    {
                                        Inv_StockItem objStockItem = new Inv_StockItem();

                                        objStockItem.ItemTypeID    = objLeatherIssueItem.ItemType;
                                        objStockItem.LeatherType   = objLeatherIssueItem.LeatherType;
                                        objStockItem.LeatherStatus = objLeatherIssueItem.LeatherStatus;
                                        objStockItem.StoreID       = Convert.ToByte(objLeatherIssueItem.StoreIdIssueTo);
                                        objStockItem.UnitID        = objLeatherIssueItem.UnitID;

                                        objStockItem.OpeningQty = 0;
                                        objStockItem.ReceiveQty = objLeatherIssueItem.IssueQty;
                                        objStockItem.IssueQty   = 0;
                                        objStockItem.ClosingQty = objLeatherIssueItem.IssueQty;

                                        _context.Inv_StockItem.Add(objStockItem);
                                        _context.SaveChanges();
                                    }
                                    else
                                    {
                                        var LastItemInfo = (from i in _context.Inv_StockItem
                                                            where i.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                            i.LeatherType == objLeatherIssueItem.LeatherType &&
                                                            i.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                            i.StoreID == objLeatherIssueItem.StoreIdIssueTo
                                                            orderby i.TransectionID descending
                                                            select i).FirstOrDefault();


                                        Inv_StockItem objStockItem = new Inv_StockItem();


                                        objStockItem.ItemTypeID    = LastItemInfo.ItemTypeID;
                                        objStockItem.LeatherType   = LastItemInfo.LeatherType;
                                        objStockItem.LeatherStatus = LastItemInfo.LeatherStatus;
                                        objStockItem.StoreID       = LastItemInfo.StoreID;
                                        objStockItem.UnitID        = objLeatherIssueItem.UnitID;

                                        objStockItem.OpeningQty = LastItemInfo.ClosingQty;
                                        objStockItem.ReceiveQty = objLeatherIssueItem.IssueQty;
                                        objStockItem.IssueQty   = 0;
                                        objStockItem.ClosingQty = LastItemInfo.ClosingQty + objLeatherIssueItem.IssueQty;

                                        _context.Inv_StockItem.Add(objStockItem);
                                        _context.SaveChanges();
                                    }

                                    #endregion

                                    #region Daily_Stock_Update

                                    //var currentDate = System.DateTime.Now.Date;

                                    var CheckDateForRec = (from ds in _context.Inv_StockDaily
                                                           where ds.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                           ds.StoreID == objLeatherIssueItem.StoreIdIssueTo &&
                                                           ds.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                           ds.LeatherType == objLeatherIssueItem.LeatherType && ds.StockDate == currentDate
                                                           select ds).Any();

                                    if (CheckDateForRec)
                                    {
                                        var CurrentItem = (from ds in _context.Inv_StockDaily
                                                           where ds.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                           ds.StoreID == objLeatherIssueItem.StoreIdIssueTo &&
                                                           ds.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                           ds.LeatherType == objLeatherIssueItem.LeatherType && ds.StockDate == currentDate
                                                           select ds).FirstOrDefault();

                                        CurrentItem.DailyReceiveQty = CurrentItem.DailyReceiveQty + objLeatherIssueItem.IssueQty;
                                        CurrentItem.ClosingQty      = CurrentItem.ClosingQty + objLeatherIssueItem.IssueQty;
                                        _context.SaveChanges();
                                    }
                                    else
                                    {
                                        var PreviousRecord = (from ds in _context.Inv_StockDaily
                                                              where ds.ItemTypeID == objLeatherIssueItem.ItemType &&
                                                              ds.StoreID == objLeatherIssueItem.StoreIdIssueTo &&
                                                              ds.LeatherStatus == objLeatherIssueItem.LeatherStatus &&
                                                              ds.LeatherType == objLeatherIssueItem.LeatherType
                                                              orderby ds.TransectionID descending
                                                              select ds).FirstOrDefault();

                                        Inv_StockDaily objStockDaily = new Inv_StockDaily();

                                        objStockDaily.ItemTypeID    = objLeatherIssueItem.ItemType;
                                        objStockDaily.StoreID       = Convert.ToByte(objLeatherIssueItem.StoreIdIssueTo);
                                        objStockDaily.UnitID        = objLeatherIssueItem.UnitID;
                                        objStockDaily.LeatherStatus = objLeatherIssueItem.LeatherStatus;
                                        objStockDaily.LeatherType   = objLeatherIssueItem.LeatherType;

                                        objStockDaily.OpeningQty      = (PreviousRecord == null ? 0 : PreviousRecord.ClosingQty);
                                        objStockDaily.DailyReceiveQty = objLeatherIssueItem.IssueQty;
                                        objStockDaily.DailyIssueQty   = 0;
                                        objStockDaily.ClosingQty      = objStockDaily.OpeningQty + objLeatherIssueItem.IssueQty;

                                        objStockDaily.StockDate = currentDate;

                                        _context.Inv_StockDaily.Add(objStockDaily);
                                        _context.SaveChanges();
                                    }

                                    #endregion
                                }

                                #endregion

                                #region Update Record Status

                                var OriginalIssueItemEntity = _context.Inv_LeatherIssueItem.First(m => m.ItemIssueID == objLeatherIssueItem.ItemIssueID);

                                OriginalIssueItemEntity.RecordStatus = "CNF";

                                #endregion
                            }
                        }

                        #endregion

                        _context.SaveChanges();

                        tx.Complete();
                        _vmMsg.Type = Enums.MessageType.Success;
                        _vmMsg.Msg  = "Confirmed Successfully.";
                    }
                }
            }
            catch
            {
                _vmMsg.Type = Enums.MessageType.Error;
                _vmMsg.Msg  = "Failed to Confirmed.";
            }
            return(_vmMsg);
        }
Пример #4
0
        public bool ConfirmPurchase(string purchaseNumber, string confirmComment)
        {
            try
            {
                using (TransactionScope Transaction = new TransactionScope())
                {
                    using (_context)
                    {
                        var purchaseInfo = (from p in _context.Prq_Purchase.AsEnumerable()
                                            where (p.PurchaseID).ToString() == purchaseNumber
                                            select p).FirstOrDefault();

                        var challanList = (from c in _context.Prq_PurchaseChallan.AsEnumerable()
                                           where (c.PurchaseID).ToString() == purchaseNumber
                                           select c).ToList();

                        foreach (var item in challanList)
                        {
                            var challanItem = (from i in _context.Prq_PurchaseChallanItem.AsEnumerable()
                                               where i.ChallanID == item.ChallanID
                                               select i).ToList();

                            foreach (var leatherItem in challanItem)
                            {
                                var leatherTypeID = DalCommon.GetLeatherTypeCode("Raw Hide");
                                leatherTypeID = leatherTypeID == 0 ? Convert.ToByte(1) : leatherTypeID;
                                var leatherStatusID = DalCommon.GetLeatherStatusCode("Raw Hide");
                                leatherStatusID = leatherStatusID == 0 ? Convert.ToByte(1) : leatherStatusID;

                                var currentDate = DateTime.Now.Date;

                                if (leatherItem.RecordStatus != "CNF" && leatherItem.RecordStatus != "Confirmed")
                                {
                                    #region Daily_Stock_Update

                                    var CheckDate = (from ds in _context.Inv_StockDaily.AsEnumerable()
                                                     where ds.ItemTypeID == leatherItem.ItemTypeID && ds.StoreID == item.ReceiveStore &&
                                                     ds.LeatherStatus == leatherStatusID && ds.LeatherType == leatherTypeID && ds.StockDate == currentDate
                                                     select ds).Any();

                                    if (CheckDate)
                                    {
                                        var CurrentItem = (from ds in _context.Inv_StockDaily.AsEnumerable()
                                                           where ds.ItemTypeID == leatherItem.ItemTypeID && ds.StoreID == item.ReceiveStore &&
                                                           ds.LeatherStatus == leatherStatusID && ds.LeatherType == leatherTypeID && ds.StockDate == currentDate
                                                           select ds).FirstOrDefault();

                                        CurrentItem.DailyReceiveQty = CurrentItem.DailyReceiveQty + leatherItem.ReceiveQty;
                                        CurrentItem.ClosingQty      = CurrentItem.ClosingQty + leatherItem.ReceiveQty;
                                        _context.SaveChanges();
                                    }
                                    else
                                    {
                                        var PreviousRecord = (from ds in _context.Inv_StockDaily.AsEnumerable()
                                                              where ds.ItemTypeID == leatherItem.ItemTypeID && ds.StoreID == item.ReceiveStore &&
                                                              ds.LeatherStatus == leatherStatusID && ds.LeatherType == leatherTypeID
                                                              orderby ds.TransectionID descending
                                                              select ds).FirstOrDefault();

                                        Inv_StockDaily objStockDaily = new Inv_StockDaily();

                                        objStockDaily.ItemTypeID    = leatherItem.ItemTypeID;
                                        objStockDaily.StoreID       = item.ReceiveStore;
                                        objStockDaily.UnitID        = leatherItem.UnitID;
                                        objStockDaily.LeatherStatus = leatherStatusID;
                                        objStockDaily.LeatherType   = leatherTypeID;

                                        objStockDaily.OpeningQty      = (PreviousRecord == null ? 0 : PreviousRecord.ClosingQty);
                                        objStockDaily.DailyReceiveQty = leatherItem.ReceiveQty;
                                        objStockDaily.DailyIssueQty   = 0;
                                        objStockDaily.ClosingQty      = objStockDaily.OpeningQty + leatherItem.ReceiveQty;

                                        objStockDaily.StockDate = currentDate;

                                        _context.Inv_StockDaily.Add(objStockDaily);
                                        _context.SaveChanges();
                                    }

                                    #endregion

                                    #region Supplier_Stock_Update

                                    var CheckSupplierStock = (from i in _context.Inv_StockSupplier.AsEnumerable()
                                                              where i.SupplierID == purchaseInfo.SupplierID && i.ItemTypeID == leatherItem.ItemTypeID &&
                                                              i.LeatherType == leatherTypeID && i.LeatherStatusID == leatherStatusID && i.StoreID == item.ReceiveStore &&
                                                              i.PurchaseID.ToString() == purchaseInfo.PurchaseID.ToString()
                                                              select i).Any();

                                    if (!CheckSupplierStock)
                                    {
                                        Inv_StockSupplier objStockSupplier = new Inv_StockSupplier();
                                        objStockSupplier.SupplierID      = purchaseInfo.SupplierID;
                                        objStockSupplier.StoreID         = item.ReceiveStore;
                                        objStockSupplier.PurchaseID      = purchaseInfo.PurchaseID;
                                        objStockSupplier.RefChallanID    = item.ChallanID;
                                        objStockSupplier.ItemTypeID      = leatherItem.ItemTypeID;
                                        objStockSupplier.LeatherType     = leatherTypeID;
                                        objStockSupplier.LeatherStatusID = leatherStatusID;
                                        objStockSupplier.UnitID          = leatherItem.UnitID;
                                        objStockSupplier.OpeningQty      = 0;
                                        objStockSupplier.ReceiveQty      = leatherItem.ReceiveQty;
                                        objStockSupplier.IssueQty        = 0;
                                        objStockSupplier.ClosingQty      = leatherItem.ReceiveQty;
                                        objStockSupplier.UpdateReason    = "Receive";


                                        _context.Inv_StockSupplier.Add(objStockSupplier);
                                        _context.SaveChanges();
                                    }
                                    else
                                    {
                                        var LastSupplierStock = (from i in _context.Inv_StockSupplier.AsEnumerable()
                                                                 where i.SupplierID == purchaseInfo.SupplierID && i.ItemTypeID == leatherItem.ItemTypeID && i.LeatherType == leatherTypeID && i.LeatherStatusID == leatherStatusID && i.StoreID == item.ReceiveStore && i.PurchaseID.ToString() == purchaseInfo.PurchaseID.ToString()
                                                                 orderby i.TransectionID descending
                                                                 select i).FirstOrDefault();

                                        Inv_StockSupplier objStockSupplier = new Inv_StockSupplier();
                                        objStockSupplier.SupplierID      = purchaseInfo.SupplierID;
                                        objStockSupplier.StoreID         = item.ReceiveStore;
                                        objStockSupplier.PurchaseID      = purchaseInfo.PurchaseID;
                                        objStockSupplier.RefChallanID    = item.ChallanID;
                                        objStockSupplier.ItemTypeID      = leatherItem.ItemTypeID;
                                        objStockSupplier.LeatherType     = leatherTypeID;
                                        objStockSupplier.LeatherStatusID = leatherStatusID;
                                        objStockSupplier.UnitID          = leatherItem.UnitID;

                                        objStockSupplier.OpeningQty   = LastSupplierStock.ClosingQty;
                                        objStockSupplier.ReceiveQty   = leatherItem.ReceiveQty;
                                        objStockSupplier.IssueQty     = 0;
                                        objStockSupplier.ClosingQty   = LastSupplierStock.ClosingQty + leatherItem.ReceiveQty;
                                        objStockSupplier.UpdateReason = "Receive";

                                        _context.Inv_StockSupplier.Add(objStockSupplier);
                                        _context.SaveChanges();
                                    }
                                    #endregion

                                    #region Item_Stock_Update

                                    var CheckItemStock = (from i in _context.Inv_StockItem.AsEnumerable()
                                                          where i.ItemTypeID == leatherItem.ItemTypeID && i.LeatherType == leatherTypeID && i.LeatherStatus == leatherStatusID && i.StoreID == item.ReceiveStore
                                                          select i).Any();


                                    if (!CheckItemStock)
                                    {
                                        Inv_StockItem objStockItem = new Inv_StockItem();

                                        objStockItem.ItemTypeID    = leatherItem.ItemTypeID;
                                        objStockItem.LeatherType   = leatherTypeID;
                                        objStockItem.LeatherStatus = leatherStatusID;
                                        objStockItem.StoreID       = item.ReceiveStore;
                                        objStockItem.UnitID        = leatherItem.UnitID;
                                        objStockItem.OpeningQty    = 0;
                                        objStockItem.ReceiveQty    = leatherItem.ReceiveQty;
                                        objStockItem.IssueQty      = 0;
                                        objStockItem.ClosingQty    = leatherItem.ReceiveQty;

                                        _context.Inv_StockItem.Add(objStockItem);
                                        _context.SaveChanges();
                                    }
                                    else
                                    {
                                        var LastItemInfo = (from i in _context.Inv_StockItem
                                                            where i.ItemTypeID == leatherItem.ItemTypeID && i.LeatherType == leatherTypeID && i.LeatherStatus == leatherStatusID && i.StoreID == item.ReceiveStore
                                                            orderby i.TransectionID descending
                                                            select i).FirstOrDefault();

                                        //var LastItemInfo = (from i in context.Inv_StockItem
                                        //                    where i.TransectionID == LastItemTransactionID
                                        //                    select i).FirstOrDefault();

                                        Inv_StockItem objStockItem = new Inv_StockItem();

                                        objStockItem.OpeningQty    = LastItemInfo.ClosingQty;
                                        objStockItem.IssueQty      = 0;
                                        objStockItem.ReceiveQty    = leatherItem.ReceiveQty;
                                        objStockItem.ClosingQty    = LastItemInfo.ClosingQty + leatherItem.ReceiveQty;
                                        objStockItem.ItemTypeID    = LastItemInfo.ItemTypeID;
                                        objStockItem.LeatherType   = LastItemInfo.LeatherType;
                                        objStockItem.LeatherStatus = LastItemInfo.LeatherStatus;
                                        objStockItem.StoreID       = LastItemInfo.StoreID;
                                        objStockItem.UnitID        = leatherItem.UnitID;

                                        _context.Inv_StockItem.Add(objStockItem);
                                        _context.SaveChanges();
                                    }

                                    #endregion

                                    leatherItem.RecordStatus = "CNF";
                                }
                            }

                            item.RecordStatus = "CNF";
                        }

                        purchaseInfo.RecordStatus = "CNF";

                        _context.SaveChanges();
                    }
                    Transaction.Complete();
                }

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        public bool ApproveAdjustRequest(string requestID, string approveComment, int userID)
        {
            try
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    var StockStatus    = 1; //To check Stock Availability
                    var currentRequest = (from s in _context.Inv_LeatherStockAdjustRequest.AsEnumerable()
                                          where s.RequestID == Convert.ToInt32(requestID)
                                          select s).FirstOrDefault();
                    var ItemList = (from i in _context.Inv_LeatherStockAdjustItem.AsEnumerable()
                                    where i.RequestID == currentRequest.RequestID
                                    select i).ToList();
                    foreach (var item in ItemList)
                    {
                        #region Item Stock Update
                        var LastItem = (from i in _context.Inv_StockItem.AsEnumerable()
                                        where i.ItemTypeID == item.ItemTypeID && i.StoreID == currentRequest.StoreID && i.LeatherType == item.LeatherType && i.LeatherStatus == item.LeatherStatus
                                        orderby i.TransectionID descending
                                        select i).FirstOrDefault();

                        if (LastItem.ClosingQty >= item.ItemQty)
                        {
                            var objItemStock = new Inv_StockItem();

                            objItemStock.StoreID       = LastItem.StoreID;
                            objItemStock.LeatherType   = LastItem.LeatherType;
                            objItemStock.LeatherStatus = LastItem.LeatherStatus;
                            objItemStock.OpeningQty    = LastItem.ClosingQty;
                            objItemStock.ReceiveQty    = 0;
                            objItemStock.IssueQty      = item.ItemQty;
                            objItemStock.ClosingQty    = LastItem.ClosingQty - item.ItemQty;
                            objItemStock.ItemTypeID    = LastItem.ItemTypeID;
                            objItemStock.UnitID        = LastItem.UnitID;


                            _context.Inv_StockItem.Add(objItemStock);
                            _context.SaveChanges();
                        }
                        else
                        {
                            StockStatus = 0;
                        }



                        #endregion
                        #region Supplier Stock Update

                        var LastSupplierItem = (from s in _context.Inv_StockSupplier.AsEnumerable()
                                                where s.ItemTypeID == item.ItemTypeID && s.StoreID == currentRequest.StoreID && s.LeatherType == item.LeatherType && s.LeatherStatusID == item.LeatherStatus &&
                                                s.SupplierID == item.SupplierID && s.PurchaseID == item.PurchaseID
                                                orderby s.TransectionID descending
                                                select s).FirstOrDefault();

                        if (LastSupplierItem.ClosingQty >= item.ItemQty)
                        {
                            var objStockSupplier = new Inv_StockSupplier();
                            objStockSupplier.PurchaseID      = LastSupplierItem.PurchaseID;
                            objStockSupplier.SupplierID      = LastSupplierItem.SupplierID;
                            objStockSupplier.ItemTypeID      = LastSupplierItem.ItemTypeID;
                            objStockSupplier.LeatherType     = LastSupplierItem.LeatherType;
                            objStockSupplier.LeatherStatusID = LastSupplierItem.LeatherStatusID;
                            objStockSupplier.StoreID         = LastSupplierItem.StoreID;
                            objStockSupplier.OpeningQty      = LastSupplierItem.ClosingQty;
                            objStockSupplier.ReceiveQty      = 0;
                            objStockSupplier.IssueQty        = item.ItemQty;
                            objStockSupplier.ClosingQty      = LastSupplierItem.ClosingQty - item.ItemQty;
                            objStockSupplier.UnitID          = LastSupplierItem.UnitID;
                            objStockSupplier.UpdateReason    = "Adjustment";

                            _context.Inv_StockSupplier.Add(objStockSupplier);
                            _context.SaveChanges();
                        }
                        else
                        {
                            StockStatus = 0;
                        }


                        #endregion
                        #region Daily Stock Update
                        var CheckCurrentDateItem = (from d in _context.Inv_StockDaily.AsEnumerable()
                                                    where d.ItemTypeID == item.ItemTypeID && d.StoreID == currentRequest.StoreID && d.LeatherType == item.LeatherType && d.LeatherStatus == item.LeatherStatus && d.StockDate == DateTime.Now.Date
                                                    select d).Any();


                        if (CheckCurrentDateItem)
                        {
                            var CurrentDateItem = (from d in _context.Inv_StockDaily.AsEnumerable()
                                                   where d.ItemTypeID == item.ItemTypeID && d.StoreID == currentRequest.StoreID && d.LeatherType == item.LeatherType && d.LeatherStatus == item.LeatherStatus && d.StockDate == DateTime.Now.Date
                                                   select d).FirstOrDefault();

                            if (CurrentDateItem.ClosingQty >= item.ItemQty)
                            {
                                CurrentDateItem.DailyIssueQty = item.ItemQty;
                                CurrentDateItem.ClosingQty    = CurrentDateItem.ClosingQty - item.ItemQty;
                                _context.SaveChanges();
                            }
                            else
                            {
                                StockStatus = 0;
                            }
                        }
                        else
                        {
                            var CurrentDateItem = (from d in _context.Inv_StockDaily.AsEnumerable()
                                                   where d.ItemTypeID == item.ItemTypeID && d.StoreID == currentRequest.StoreID && d.LeatherType == item.LeatherType && d.LeatherStatus == item.LeatherStatus
                                                   orderby d.StockDate descending
                                                   select d).FirstOrDefault();
                            if (CurrentDateItem.ClosingQty >= item.ItemQty)
                            {
                                var objDailyStock = new Inv_StockDaily();

                                objDailyStock.StockDate       = DateTime.Now.Date;
                                objDailyStock.ItemTypeID      = CurrentDateItem.ItemTypeID;
                                objDailyStock.StoreID         = CurrentDateItem.StoreID;
                                objDailyStock.LeatherType     = CurrentDateItem.LeatherType;
                                objDailyStock.LeatherStatus   = CurrentDateItem.LeatherStatus;
                                objDailyStock.UnitID          = CurrentDateItem.UnitID;
                                objDailyStock.OpeningQty      = CurrentDateItem.ClosingQty;
                                objDailyStock.DailyIssueQty   = item.ItemQty;
                                objDailyStock.DailyReceiveQty = 0;
                                objDailyStock.ClosingQty      = CurrentDateItem.ClosingQty - item.ItemQty;

                                _context.Inv_StockDaily.Add(objDailyStock);
                                _context.SaveChanges();
                            }
                            else
                            {
                                StockStatus = 0;
                            }
                        }
                        #endregion
                    }

                    if (StockStatus == 1)
                    {
                        currentRequest.RecordStatus   = "APV";
                        currentRequest.ApproveComment = approveComment;
                        currentRequest.ApprovedBy     = userID;
                        currentRequest.ApproveDate    = DateTime.Now;
                        _context.SaveChanges();
                        transaction.Complete();
                        return(true);
                    }
                    else
                    {
                        transaction.Dispose();
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                return(false);
            }
        }