public InvLeatherIssueItem SetToBussinessObject(Inv_LeatherIssueItem Entity)
        {
            InvLeatherIssueItem Model = new InvLeatherIssueItem();

            Model.ItemIssueID       = Entity.ItemIssueID;
            Model.SupplierID        = Entity.SupplierID;
            Model.SupplierName      = _context.Sys_Supplier.Where(m => m.SupplierID == Entity.SupplierID).FirstOrDefault().SupplierName;
            Model.ChallanID         = Entity.ChallanID;
            Model.ChallanNo         = Entity.ChallanID == null ? "" : _context.Prq_PurchaseChallan.Where(m => m.ChallanID == Entity.ChallanID).FirstOrDefault().ChallanNo;
            Model.PurchaseID        = Entity.PurchaseID;
            Model.PurchaseNo        = _context.Prq_Purchase.Where(m => m.PurchaseID == Entity.PurchaseID).FirstOrDefault().PurchaseNo;
            Model.ItemType          = Entity.ItemType;
            Model.ItemTypeName      = _context.Sys_ItemType.Where(m => m.ItemTypeID == Entity.ItemType).FirstOrDefault().ItemTypeName;
            Model.LeatherType       = Entity.LeatherType;
            Model.LeatherTypeName   = _context.Sys_LeatherType.Where(m => m.LeatherTypeID == Entity.LeatherType).FirstOrDefault().LeatherTypeName;
            Model.LeatherStatus     = Entity.LeatherStatus;
            Model.LeatherStatusName = _context.Sys_LeatherStatus.Where(m => m.LeatherStatusID == Entity.LeatherStatus).FirstOrDefault().LeatherStatusName;
            Model.IssueQty          = Entity.IssueQty;
            Model.IssueSide         = Entity.IssueSide;

            Model.ReceiveQty  = Entity.IssueQty;
            Model.ReceiveSide = Entity.IssueSide;

            Model.UnitID   = Entity.UnitID;
            Model.UnitName = _context.Sys_Unit.Where(m => m.UnitID == Entity.UnitID).FirstOrDefault().UnitName;
            Model.Remarks  = Entity.Remarks;

            return(Model);
        }
        public InvLeatherIssueItem SetToBussinessObject(Inv_LeatherIssueItem Entity, byte storeid)
        {
            InvLeatherIssueItem Model = new InvLeatherIssueItem();

            Model.ItemIssueID       = Entity.ItemIssueID;
            Model.SupplierID        = Entity.SupplierID;
            Model.SupplierName      = Entity.SupplierID == null ? "" : _context.Sys_Supplier.Where(m => m.SupplierID == Entity.SupplierID).SingleOrDefault().SupplierName;
            Model.ChallanID         = Entity.ChallanID;
            Model.ChallanNo         = Entity.ChallanID == null ? "" : _context.Prq_PurchaseChallan.Where(m => m.ChallanID == Entity.ChallanID).FirstOrDefault().ChallanNo;
            Model.PurchaseID        = Entity.PurchaseID;
            Model.PurchaseNo        = Entity.PurchaseID == null ? "" : _context.Prq_Purchase.Where(m => m.PurchaseID == Entity.PurchaseID).FirstOrDefault().PurchaseNo;
            Model.ItemType          = Entity.ItemType;
            Model.ItemTypeName      = Entity.ItemType == null ? "" : _context.Sys_ItemType.Where(m => m.ItemTypeID == Entity.ItemType).FirstOrDefault().ItemTypeName;
            Model.LeatherType       = Entity.LeatherType;
            Model.LeatherTypeName   = Entity.LeatherType == null ? "" : _context.Sys_LeatherType.Where(m => m.LeatherTypeID == Entity.LeatherType).FirstOrDefault().LeatherTypeName;
            Model.LeatherStatus     = Entity.LeatherStatus;
            Model.LeatherStatusName = Entity.LeatherStatus == null ? "" : _context.Sys_LeatherStatus.Where(m => m.LeatherStatusID == Entity.LeatherStatus).FirstOrDefault().LeatherStatusName;
            Model.IssueQty          = Entity.IssueQty;
            if (_context.Inv_StockSupplier.Where(m => m.SupplierID == Entity.SupplierID && m.StoreID == storeid && m.ItemTypeID == Entity.ItemType && m.LeatherType == Entity.LeatherType && m.LeatherStatusID == Entity.LeatherStatus && m.PurchaseID == Entity.PurchaseID).OrderByDescending(m => m.TransectionID).FirstOrDefault() != null)
            {
                Model.StockQty = _context.Inv_StockSupplier.Where(m => m.SupplierID == Entity.SupplierID && m.StoreID == storeid && m.ItemTypeID == Entity.ItemType && m.LeatherType == Entity.LeatherType && m.LeatherStatusID == Entity.LeatherStatus && m.PurchaseID == Entity.PurchaseID).OrderByDescending(m => m.TransectionID).FirstOrDefault().ClosingQty;
            }
            Model.UnitID    = Entity.UnitID;
            Model.UnitName  = _context.Sys_Unit.Where(m => m.UnitID == Entity.UnitID).FirstOrDefault().UnitName;
            Model.IssueSide = Entity.IssueSide;
            Model.Remarks   = Entity.Remarks;

            return(Model);
        }
        public ValidationMsg Save(InvLeatherIssue model, int userid)
        {
            _vmMsg = new ValidationMsg();
            try
            {
                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        model.SetBy = userid;
                        Inv_LeatherIssue tblLeatherIssue = SetToModelObject(model);
                        _context.Inv_LeatherIssue.Add(tblLeatherIssue);
                        _context.SaveChanges();

                        #region Save Detail Records

                        if (model.LeatherIssueItemList != null)
                        {
                            foreach (InvLeatherIssueItem objInvLeatherIssueItem in model.LeatherIssueItemList)
                            {
                                objInvLeatherIssueItem.SetBy   = userid;
                                objInvLeatherIssueItem.IssueID = tblLeatherIssue.IssueID;
                                Inv_LeatherIssueItem tblPurchaseYearPeriod =
                                    SetToModelObject(objInvLeatherIssueItem);
                                _context.Inv_LeatherIssueItem.Add(tblPurchaseYearPeriod);
                            }
                        }
                        _context.SaveChanges();

                        #endregion

                        tx.Complete();
                        IssueID     = tblLeatherIssue.IssueID;
                        _vmMsg.Type = Enums.MessageType.Success;
                        _vmMsg.Msg  = "Saved Successfully.";
                    }
                }
            }
            catch
            {
                _vmMsg.Type = Enums.MessageType.Error;
                _vmMsg.Msg  = "Failed to save.";
            }
            return(_vmMsg);
        }
        public Inv_LeatherIssueItem SetToModelObject(InvLeatherIssueItem model)
        {
            Inv_LeatherIssueItem Entity = new Inv_LeatherIssueItem();

            Entity.ItemIssueID   = model.ItemIssueID;
            Entity.IssueID       = model.IssueID;// Convert.ToInt16(_context.Inv_LeatherIssue.DefaultIfEmpty().Max(m => m.IssueID == null ? 0 : m.IssueID));
            Entity.SupplierID    = model.SupplierID;
            Entity.ChallanID     = model.ChallanID;
            Entity.PurchaseID    = model.PurchaseID;//_context.Prq_PurchaseChallan.Where(m => m.ChallanID == model.ChallanID).FirstOrDefault().PurchaseID;
            Entity.ItemType      = model.ItemType;
            Entity.LeatherType   = model.LeatherType;
            Entity.LeatherStatus = model.LeatherStatus;
            Entity.IssueQty      = model.IssueQty;
            Entity.UnitID        = model.UnitID;
            Entity.IssueSide     = model.IssueSide;
            Entity.Remarks       = model.Remarks;
            Entity.RecordStatus  = "NCF";//model.RecordStatus;
            Entity.SetOn         = DateTime.Now;
            Entity.SetBy         = model.SetBy;
            Entity.IPAddress     = string.Empty;
            return(Entity);
        }
        public ValidationMsg Update(InvLeatherIssue model, int userid)
        {
            _vmMsg = new ValidationMsg();
            try
            {
                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        Inv_LeatherIssue CurrentEntity = SetToModelObject(model);
                        var OriginalEntity             = _context.Inv_LeatherIssue.First(m => m.IssueID == model.IssueID);

                        OriginalEntity.IssueDate    = CurrentEntity.IssueDate;// Convert.ToDateTime(CurrentEntity.IssueDate).Date;
                        OriginalEntity.IssueFor     = CurrentEntity.IssueFor;
                        OriginalEntity.IssueRef     = CurrentEntity.IssueRef;
                        OriginalEntity.IssueFrom    = CurrentEntity.IssueFrom;
                        OriginalEntity.IssueTo      = CurrentEntity.IssueTo;
                        OriginalEntity.JobOrderNo   = CurrentEntity.JobOrderNo;
                        OriginalEntity.PurchaseYear = CurrentEntity.PurchaseYear;
                        OriginalEntity.SetBy        = userid;
                        OriginalEntity.SetOn        = DateTime.Now;

                        #region Save Detail Records

                        if (model.LeatherIssueItemList != null)
                        {
                            foreach (InvLeatherIssueItem objInvLeatherIssueItem in model.LeatherIssueItemList)
                            {
                                if (objInvLeatherIssueItem.ItemIssueID == 0)
                                {
                                    objInvLeatherIssueItem.IssueID = model.IssueID;
                                    Inv_LeatherIssueItem tblPurchaseYearPeriod =
                                        SetToModelObject(objInvLeatherIssueItem);
                                    _context.Inv_LeatherIssueItem.Add(tblPurchaseYearPeriod);
                                }
                                else
                                {
                                    Inv_LeatherIssueItem CurEntity = SetToModelObject(objInvLeatherIssueItem);
                                    var OrgEntity = _context.Inv_LeatherIssueItem.First(m => m.ItemIssueID == objInvLeatherIssueItem.ItemIssueID);

                                    OrgEntity.SupplierID    = CurEntity.SupplierID;
                                    OrgEntity.ChallanID     = CurEntity.ChallanID;
                                    OrgEntity.ItemType      = CurEntity.ItemType;
                                    OrgEntity.LeatherType   = CurEntity.LeatherType;
                                    OrgEntity.LeatherStatus = CurEntity.LeatherStatus;
                                    OrgEntity.IssueQty      = CurEntity.IssueQty;
                                    OrgEntity.UnitID        = CurEntity.UnitID;
                                    OrgEntity.IssueSide     = CurEntity.IssueSide;
                                    OrgEntity.Remarks       = CurEntity.Remarks;
                                    OrgEntity.SetBy         = userid;
                                    OrgEntity.SetOn         = DateTime.Now;
                                }
                            }
                        }
                        _context.SaveChanges();

                        #endregion

                        tx.Complete();
                        IssueID     = model.IssueID;
                        _vmMsg.Type = Enums.MessageType.Update;
                        _vmMsg.Msg  = "Updated Successfully.";
                    }
                }
            }
            catch
            {
                _vmMsg.Type = Enums.MessageType.Error;
                _vmMsg.Msg  = "Failed to Update.";
            }
            return(_vmMsg);
        }