コード例 #1
0
ファイル: DalSysBank.cs プロジェクト: EnggJibon/LatherERP
        public Sys_Bank SetToModelObject(SysBank model, int userid, string url)
        {
            //const string chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
            //var random = new Random();
            //var result = new string(
            //    Enumerable.Repeat(chars, 8)
            //              .Select(s => s[random.Next(s.Length)])
            //              .ToArray());
            Sys_Bank entity = new Sys_Bank();

            entity.BankID        = model.BankID;
            entity.BankCode      = DalCommon.GetPreDefineNextCodeByUrl(url);
            entity.BankName      = model.BankName;
            entity.BankCategory  = model.BankCategory;
            entity.BankType      = model.BankType;
            entity.BankBINNo     = model.BankBINNo;
            entity.BankSwiftCode = model.BankSwiftCode;
            entity.IsActive      = model.IsActive == "Active";
            entity.SetOn         = model.BankID == 0 ? DateTime.Now : _context.Sys_Bank.FirstOrDefault(ob => ob.BankID == model.BankID).SetOn;
            entity.SetBy         = model.BankID == 0 ? userid : _context.Sys_Bank.FirstOrDefault(ob => ob.BankID == model.BankID).SetBy;
            entity.ModifiedOn    = model.BankID == 0 ? (DateTime?)null : DateTime.Now;
            entity.ModifiedBy    = model.BankID == 0 ? (int?)null : userid;
            entity.IPAddress     = string.Empty;
            return(entity);
        }
コード例 #2
0
        public ValidationMsg Save(SysBank model, int _userid, string pageUrl)//, string pageUrl
        {
            vmMsg = new ValidationMsg();

            try
            {
                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        model.BankCode = DalCommon.GetPreDefineNextCodeByUrl(pageUrl);//DalCommon.GetPreDefineValue("1", "00045");
                        if (model.BankCode != null)
                        {
                            Sys_Bank tblBank = SetToBankModelObject(model, _userid);//, _userid
                            _context.Sys_Bank.Add(tblBank);
                            _context.SaveChanges();

                            if (model.Branches.Count > 0)
                            {
                                foreach (var branch in model.Branches)
                                {
                                    branch.BankID = tblBank.BankID;
                                    Sys_Branch tblBranch = SetToBranchModelObject(branch, _userid);
                                    _context.Sys_Branch.Add(tblBranch);
                                    _context.SaveChanges();
                                }
                            }
                            _context.SaveChanges();

                            tx.Complete();
                            //BankID = tblBank.BankID;

                            vmMsg.Type = Enums.MessageType.Success;
                            vmMsg.Msg  = "Saved Successfully.";
                        }
                    }
                }
            }


            catch (DbEntityValidationException e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                eve.Entry.Entity.GetType().Name,
                                                eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName,
                                                    ve.ErrorMessage));
                    }
                }
                throw new DbEntityValidationException(sb.ToString(), e);
            }
            return(vmMsg);
        }
コード例 #3
0
ファイル: DalSysSupplier.cs プロジェクト: EnggJibon/LatherERP
        public ValidationMsg Save(SysSupplier model, int userid)
        {
            _vmMsg = new ValidationMsg();
            try
            {
                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        //var exitSupplierCode = _context.Sys_Supplier.Where(m => m.SupplierCode == model.SupplierCode).ToList();
                        //if (exitSupplierCode.Count > 0)
                        //{
                        //    _vmMsg.Type = Enums.MessageType.Error;
                        //    _vmMsg.Msg = "Supplier Code Already Exit.";
                        //}
                        //else
                        //{
                        #region Supplier
                        model.SupplierCode = DalCommon.GetPreDefineNextCodeByUrl("Supplier/Supplier");//DalCommon.GetPreDefineValue("1", "00045");

                        model.SetBy = userid;
                        Sys_Supplier tblSysSupplier = SetToModelObject(model);
                        _context.Sys_Supplier.Add(tblSysSupplier);
                        _context.SaveChanges();

                        SupplierID = tblSysSupplier.SupplierID;

                        #endregion

                        #region Save Supplier Address

                        if (model.SupplierAddressList != null)
                        {
                            if (model.SupplierAddressList.Count > 1)
                            {
                                supplieraddress = 1;
                            }
                            else
                            {
                                foreach (SysSupplierAddress objSysSupplierAddress in model.SupplierAddressList)
                                {
                                    objSysSupplierAddress.SupplierID = SupplierID;
                                    objSysSupplierAddress.SetBy      = userid;
                                    objSysSupplierAddress.IsActive   = string.IsNullOrEmpty(objSysSupplierAddress.IsActive) ? "Active" : objSysSupplierAddress.IsActive;
                                    Sys_SupplierAddress tblSupplierAddress = SetToModelObject(objSysSupplierAddress);
                                    _context.Sys_SupplierAddress.Add(tblSupplierAddress);
                                }

                                #region Save Supplier Agent

                                if (model.SupplierAgentList != null)
                                {
                                    foreach (SysSupplierAgent objSupplierAgent in model.SupplierAgentList)
                                    {
                                        if (objSupplierAgent.SupplierID != 0)
                                        {
                                            if (objSupplierAgent.AgentType == "Local Agent")
                                            {
                                                ++localAgent;
                                            }
                                            else if (objSupplierAgent.AgentType == "Foreign Agent")
                                            {
                                                ++foreignAgent;
                                            }
                                            if (localAgent == 1 || foreignAgent == 1)
                                            {
                                                objSupplierAgent.SupplierAgentID = SupplierID;
                                                objSupplierAgent.SupplierID      = SupplierID;
                                                objSupplierAgent.SetBy           = userid;
                                                objSupplierAgent.IsActive        = string.IsNullOrEmpty(objSupplierAgent.IsActive) ? "Active" : objSupplierAgent.IsActive;
                                                Sys_SupplierAgent tblSysSupplierAgent = SetToModelObject(objSupplierAgent);
                                                _context.Sys_SupplierAgent.Add(tblSysSupplierAgent);
                                            }
                                        }
                                    }
                                }

                                #endregion

                                #region Transaction

                                if (supplieraddress == 1)
                                {
                                    _vmMsg.Type = Enums.MessageType.Error;
                                    _vmMsg.Msg  = "Please Enter only one Active Supplier Address.";
                                }

                                if (localAgent > 1 || foreignAgent > 1)
                                {
                                    _vmMsg.Type = Enums.MessageType.Error;
                                    _vmMsg.Msg  = "Please Enter Same Agent Type Only Once.";
                                }

                                if (supplieraddress == 0 && localAgent == 1 && foreignAgent == 1)
                                {
                                    _context.SaveChanges();
                                    tx.Complete();
                                    SupplierCode = model.SupplierCode;
                                    _vmMsg.Type  = Enums.MessageType.Success;
                                    _vmMsg.Msg   = "Saved Successfully.";
                                }
                                if (supplieraddress == 0 && localAgent == 0 && foreignAgent == 1)
                                {
                                    _context.SaveChanges();
                                    tx.Complete();
                                    SupplierCode = model.SupplierCode;
                                    _vmMsg.Type  = Enums.MessageType.Success;
                                    _vmMsg.Msg   = "Saved Successfully.";
                                }
                                if (supplieraddress == 0 && localAgent == 1 && foreignAgent == 0)
                                {
                                    _context.SaveChanges();
                                    tx.Complete();
                                    SupplierCode = model.SupplierCode;
                                    _vmMsg.Type  = Enums.MessageType.Success;
                                    _vmMsg.Msg   = "Saved Successfully.";
                                }
                                if (supplieraddress == 0 && localAgent == 0 && foreignAgent == 0)
                                {
                                    _context.SaveChanges();
                                    tx.Complete();
                                    SupplierCode = model.SupplierCode;
                                    _vmMsg.Type  = Enums.MessageType.Success;
                                    _vmMsg.Msg   = "Saved Successfully.";
                                }

                                #endregion
                            }
                        }
                        else
                        {
                            _vmMsg.Type = Enums.MessageType.Error;
                            _vmMsg.Msg  = "Please Enter Supplier Address.";
                        }

                        #endregion
                        //}
                    }
                }
            }
            catch
            {
                _vmMsg.Type = Enums.MessageType.Error;
                _vmMsg.Msg  = "Failed to save.";
            }
            return(_vmMsg);
        }
コード例 #4
0
        public ActionResult Save(LoanReturnIssue dataSet)
        {
            decimal RemainingQty   = 0;
            decimal ReturnQuantity = 0;
            decimal TotalReturnQty = 0;

            try
            {
                if (dataSet.RequestID == 0)
                {
                    INV_TransRequest ob = new INV_TransRequest();
                    if (dataSet.RequestType == "RLRR")
                    {
                        ob.RequestNo = DalCommon.GetPreDefineNextCodeByUrl("LoanReturnRequest/LoanReturnIssue");
                    }
                    ob.RequestDate        = DalCommon.SetDate(dataSet.RequestDate);
                    ob.RequestType        = dataSet.RequestType;
                    ob.Remarks            = dataSet.Remarks;
                    ob.RequestFrom        = dataSet.RequestFrom.ToString();
                    ob.RequestTo          = dataSet.RequestTo;
                    ob.ToSource           = dataSet.ToSource;
                    ob.ExpectetReturnTime = Convert.ToByte(dataSet.ExpectetReturnTime);
                    ob.RecordStatus       = "NCF";
                    ob.SetOn = DateTime.Now;
                    if (dataSet.ReturnMethod == "Exchange Other Item")
                    {
                        ob.ReturnMethod = "EOI";
                    }
                    else if (dataSet.ReturnMethod == "Doller to Doller")
                    {
                        ob.ReturnMethod = "DTD";
                    }
                    else if (dataSet.ReturnMethod == "Exchange Same Item")
                    {
                        ob.ReturnMethod = "ESI";
                    }
                    else
                    {
                        ob.ReturnMethod = dataSet.ReturnMethod;
                    }

                    ob.SetBy      = Convert.ToInt32(Session["UserID"]);
                    ob.ModifiedBy = Convert.ToInt32(Session["UserID"]);
                    ob.ModifiedOn = DateTime.Now;
                    ob.IPAddress  = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRepository.Insert(ob);

                    INV_TransRequestRef obRef = new INV_TransRequestRef();
                    obRef.TransRequestRefNo = ob.RequestNo;
                    obRef.RequestID         = ob.RequestID;
                    obRef.RefRequestID      = Convert.ToInt64(dataSet.RefRequestID);
                    obRef.Remark            = dataSet.Remarks;
                    obRef.SetOn             = DateTime.Now;
                    obRef.SetBy             = Convert.ToInt32(Session["UserID"]);
                    obRef.ModifiedBy        = Convert.ToInt32(Session["UserID"]);
                    obRef.ModifiedOn        = DateTime.Now;
                    obRef.IPAddress         = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRefRepository.Insert(obRef);

                    if (dataSet.lstLoanReturnIssueItems != null)
                    {
                        foreach (var item in dataSet.lstLoanReturnIssueItems)
                        {
                            TotalReturnQty = (item.RemainingQty * item.ReturnRate);

                            RemainingQty   = item.RemainingQty == null ? 0 : Convert.ToDecimal(item.RemainingQty);
                            ReturnQuantity = item.ReturnQuantity == null ? 0 : Convert.ToDecimal(item.ReturnQuantity);
                            INV_TransRequestItem obReqItem = new INV_TransRequestItem();
                            obReqItem.RequestID         = ob.RequestID;
                            obReqItem.TransRequestRefID = obRef.TransRequestRefID;


                            obReqItem.RefItemID = Convert.ToInt32(item.ItemID);
                            if (item.ReturnMethodID == "ESI")
                            {
                                obReqItem.ItemID = Convert.ToInt32(item.ItemID);
                            }

                            obReqItem.ReferenceQty  = Convert.ToDecimal(item.ReferenceQty);
                            obReqItem.ReferenceUnit = Convert.ToByte(item.ReferenceUnit);
                            if (item.ReceiveCurrencyID != null)
                            {
                                obReqItem.ReferenceCurrency = Convert.ToByte(item.ReceiveCurrencyID);
                            }
                            obReqItem.ReferenceRate  = Convert.ToDecimal(item.ReceiveRate);
                            obReqItem.ReferenceValue = Convert.ToDecimal(item.ReceiveValue);
                            obReqItem.ReturnMethod   = item.ReturnMethodID == null ? "" : item.ReturnMethodID;
                            if (item.ReturnUnitID != null)
                            {
                                obReqItem.ReturnUnit = Convert.ToByte(item.ReturnUnitID);
                            }
                            if (item.ReturnItemID != null)
                            {
                                obReqItem.ItemID = Convert.ToInt32(item.ReturnItemID);
                            }
                            if (item.ReturnCurrencyID != null)
                            {
                                obReqItem.ReturnCurrency = Convert.ToByte(item.ReturnCurrencyID);
                            }
                            if (item.ReturnExchangeRate != null)
                            {
                                obReqItem.ExchangeRate = Convert.ToDecimal(item.ReturnExchangeRate);
                            }
                            if (item.ReturnRate != null)
                            {
                                obReqItem.ReturnRate = Convert.ToDecimal(item.ReturnRate);
                            }
                            obReqItem.RefItemDueQty = 0;                                      //Math.Round(item.RemainingQty - ((item.ReturnQuantity * item.RemainingQty) / (TotalReturnQty == 0 ? 1 : TotalReturnQty)),2);
                            obReqItem.ReturnValue   = Convert.ToDecimal(item.ReturnQuantity); //Math.Round(((item.ReturnQuantity * item.RemainingQty) / (TotalReturnQty == 0 ? 1 : TotalReturnQty)),2);
                            obReqItem.SetBy         = Convert.ToInt32(Session["UserID"]);
                            obReqItem.SetOn         = DateTime.Now;
                            obReqItem.ModifiedBy    = Convert.ToInt32(Session["UserID"]);
                            obReqItem.ModifiedOn    = DateTime.Now;
                            obReqItem.IPAddress     = GetIPAddress.LocalIPAddress();
                            repository.InvTransRequestItemRepository.Insert(obReqItem);
                        }
                    }
                    try
                    {
                        repository.Save();
                        _vmMsg.ReturnCode = ob.RequestNo;
                        _vmMsg.ReturnId   = ob.RequestID;
                        _vmMsg.Type       = Enums.MessageType.Success;
                        _vmMsg.Msg        = "Saved Successfully.";
                    }
                    catch (Exception ex)
                    {
                        _vmMsg.Type = Enums.MessageType.Error;
                        _vmMsg.Msg  = "Saved Faild.";
                    }
                }
                else
                {  // Update
                    INV_TransRequest ob = repository.InvTransRequestRepository.GetByID(dataSet.RequestID);
                    ob.RequestDate = DalCommon.SetDate(dataSet.RequestDate);
                    if (dataSet.ReturnMethod == "Exchange Other Item")
                    {
                        ob.ReturnMethod = "EOI";
                    }
                    else if (dataSet.ReturnMethod == "Doller to Doller")
                    {
                        ob.ReturnMethod = "DTD";
                    }
                    else if (dataSet.ReturnMethod == "Exchange Same Item")
                    {
                        ob.ReturnMethod = "ESI";
                    }
                    else
                    {
                        ob.ReturnMethod = dataSet.ReturnMethod;
                    }
                    ob.RequestType        = dataSet.RequestType;
                    ob.RequestFrom        = dataSet.RequestFrom.ToString();
                    ob.RequestTo          = dataSet.RequestTo;
                    ob.ToSource           = dataSet.ToSource;
                    ob.ExpectetReturnTime = Convert.ToByte(dataSet.ExpectetReturnTime);
                    ob.Remarks            = dataSet.Remarks;
                    ob.ModifiedOn         = DateTime.Now;
                    ob.ModifiedBy         = Convert.ToInt32(Session["UserID"]);
                    ob.IPAddress          = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRepository.Update(ob);

                    INV_TransRequestRef obRef = repository.InvTransRequestRefRepository.Get(filter: o => o.RequestID == dataSet.RequestID).FirstOrDefault();
                    obRef.Remark     = dataSet.Remarks;
                    obRef.ModifiedBy = Convert.ToInt32(Session["UserID"]);
                    obRef.ModifiedOn = DateTime.Now;
                    obRef.IPAddress  = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRefRepository.Update(obRef);
                    if (dataSet.lstLoanReturnIssueItems != null)
                    {
                        foreach (var item in dataSet.lstLoanReturnIssueItems)
                        {
                            TotalReturnQty = (item.RemainingQty * item.ReturnRate);
                            RemainingQty   = item.RemainingQty == null ? 0 : Convert.ToDecimal(item.RemainingQty);
                            ReturnQuantity = item.ReturnQuantity == null ? 0 : Convert.ToDecimal(item.ReturnQuantity);

                            INV_TransRequestItem obReqItem = repository.InvTransRequestItemRepository.GetByID(item.TransRequestItemID);

                            //obReqItem.TransRequestRefID = obRef.TransRequestRefID;
                            obReqItem.RefItemID     = Convert.ToInt32(item.ItemID);
                            obReqItem.ReferenceQty  = Convert.ToDecimal(item.ReferenceQty);
                            obReqItem.ReferenceUnit = Convert.ToByte(item.ReferenceUnit);
                            if (item.ReceiveCurrencyID != null)
                            {
                                obReqItem.ReferenceCurrency = Convert.ToByte(item.ReceiveCurrencyID);
                            }
                            if (item.ReturnMethodID == "ESI")
                            {
                                obReqItem.ItemID = Convert.ToInt32(item.ItemID);
                            }

                            obReqItem.ReferenceRate  = Convert.ToDecimal(item.ReceiveRate);
                            obReqItem.ReferenceValue = Convert.ToDecimal(item.ReceiveValue);
                            obReqItem.ReturnMethod   = item.ReturnMethodID == null ? "" : item.ReturnMethodID;
                            if (item.ReturnUnitID != null)
                            {
                                obReqItem.ReturnUnit = Convert.ToByte(item.ReturnUnitID);
                            }
                            if (item.ReturnItemID != null)
                            {
                                obReqItem.ItemID = Convert.ToInt32(item.ReturnItemID);
                            }
                            if (item.ReturnCurrencyID != null)
                            {
                                obReqItem.ReturnCurrency = Convert.ToByte(item.ReturnCurrencyID);
                            }
                            if (item.ReturnExchangeRate != null)
                            {
                                obReqItem.ExchangeRate = Convert.ToDecimal(item.ReturnExchangeRate);
                            }
                            if (item.ReturnRate != null)
                            {
                                obReqItem.ReturnRate = Convert.ToDecimal(item.ReturnRate);
                            }
                            obReqItem.RefItemDueQty = 0;                                      //Math.Round((item.RemainingQty + (obReqItem.ReturnValue == null?0 : Convert.ToDecimal(obReqItem.ReturnValue))) - ((item.ReturnQuantity * item.RemainingQty) / (TotalReturnQty == 0 ? 1 : TotalReturnQty)), 2);
                            obReqItem.ReturnValue   = Convert.ToDecimal(item.ReturnQuantity); //Math.Round(((item.ReturnQuantity * item.RemainingQty) / (TotalReturnQty == 0 ? 1 : TotalReturnQty)), 2);
                            obReqItem.SetBy         = Convert.ToInt32(Session["UserID"]);
                            obReqItem.SetOn         = DateTime.Now;
                            obReqItem.ModifiedBy    = Convert.ToInt32(Session["UserID"]);
                            obReqItem.ModifiedOn    = DateTime.Now;
                            obReqItem.IPAddress     = GetIPAddress.LocalIPAddress();
                            repository.InvTransRequestItemRepository.Update(obReqItem);
                        }
                    }

                    try
                    {
                        repository.Save();
                        _vmMsg.ReturnCode = ob.RequestNo;
                        _vmMsg.ReturnId   = dataSet.RequestID;
                        _vmMsg.Type       = Enums.MessageType.Success;
                        _vmMsg.Msg        = "Updated Successfully.";
                    }
                    catch (DbEntityValidationException e)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (var eve in e.EntityValidationErrors)
                        {
                            sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                        eve.Entry.Entity.GetType().Name,
                                                        eve.Entry.State));
                            foreach (var ve in eve.ValidationErrors)
                            {
                                sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                            ve.PropertyName,
                                                            ve.ErrorMessage));
                            }
                        }

                        _vmMsg.Type = Enums.MessageType.Error;
                        _vmMsg.Msg  = "Saved Faild.";
                        throw new DbEntityValidationException(sb.ToString(), e);
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                eve.Entry.Entity.GetType().Name,
                                                eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName,
                                                    ve.ErrorMessage));
                    }
                }

                _vmMsg.Type = Enums.MessageType.Error;
                _vmMsg.Msg  = "Saved Faild.";
                throw new DbEntityValidationException(sb.ToString(), e);
            }
            return(Json(new { msg = _vmMsg }));
        }
コード例 #5
0
        public ActionResult Save(TransRequest dataSet)
        {
            try
            {
                //######################################
                // Session["UserID"] = 1;
                if (dataSet.RequestID == 0)
                {
                    INV_TransRequest ob = new INV_TransRequest();
                    if (dataSet.RequestType == "LNIR")
                    {
                        ob.RequestNo  = DalCommon.GetPreDefineNextCodeByUrl("LoanRequest/LoanIssueRequest");
                        ob.FromSource = dataSet.ToSource;
                        //ob.ToSource = "STR";
                    }
                    else if (dataSet.RequestType == "LNRR")
                    {
                        ob.RequestNo = DalCommon.GetPreDefineNextCodeByUrl("LoanRequest/Index");
                        //ob.FromSource = "STR";
                        ob.ToSource = dataSet.ToSource;
                    }
                    else
                    {
                    }

                    ob.RequestDate        = DalCommon.SetDate(dataSet.RequestDate);
                    ob.RequestType        = dataSet.RequestType;
                    ob.RequestFrom        = dataSet.RequestFrom.ToString();
                    ob.RequestTo          = dataSet.RequestTo.ToString();
                    ob.ReturnMethod       = dataSet.ReturnMethod;
                    ob.ExpectetReturnTime = Convert.ToByte(dataSet.ExpectetReturnTime);
                    ob.RecordStatus       = "NCF";
                    ob.SetOn      = DateTime.Now;
                    ob.SetBy      = Convert.ToInt32(Session["UserID"]);
                    ob.ModifiedBy = Convert.ToInt32(Session["UserID"]);
                    ob.ModifiedOn = DateTime.Now;

                    ob.IPAddress = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRepository.Insert(ob);

                    INV_TransRequestRef obRef = new INV_TransRequestRef();
                    obRef.TransRequestRefNo = ob.RequestNo;
                    obRef.RequestID         = ob.RequestID;
                    //obRef.RefRequestID = ob.RequestID;
                    obRef.ReturnMethod = dataSet.ReturnMethod;
                    obRef.SetOn        = DateTime.Now;
                    obRef.SetBy        = Convert.ToInt32(Session["UserID"]);
                    obRef.ModifiedBy   = Convert.ToInt32(Session["UserID"]);
                    obRef.ModifiedOn   = DateTime.Now;
                    obRef.IPAddress    = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRefRepository.Insert(obRef);

                    if (dataSet.ChemicalSelectedList != null)
                    {
                        foreach (var item in dataSet.ChemicalSelectedList)
                        {
                            INV_TransRequestItem obReqItem = new INV_TransRequestItem();
                            obReqItem.RequestID         = ob.RequestID;
                            obReqItem.ReturnMethod      = dataSet.ReturnMethod;
                            obReqItem.TransRequestRefID = obRef.TransRequestRefID;
                            if (item.ItemID > 0)
                            {
                                obReqItem.ItemID = Convert.ToInt32(item.ItemID);
                            }
                            if (Convert.ToByte(item.PackSize) > 0)
                            {
                                obReqItem.PackSize = Convert.ToByte(item.PackSize);
                            }

                            obReqItem.PackQty = Convert.ToInt32(item.PackQty);
                            if (Convert.ToByte(item.SizeUnit) > 0)
                            {
                                obReqItem.SizeUnit = Convert.ToByte(item.SizeUnit);
                            }
                            if (obReqItem.PackQty != null && item.PackSize != null)
                            {
                                obReqItem.TransQty = obReqItem.PackQty * ((Convert.ToByte(repository.SysSizeRepository.GetByID(Convert.ToInt32(item.PackSize)).SizeName)));
                            }
                            if (Convert.ToByte(item.ReferenceUnit) > 0)
                            {
                                obReqItem.TransUnit = Convert.ToByte(item.ReferenceUnit);
                            }

                            if (item.RefSupplierID > 0)
                            {
                                obReqItem.RefSupplierID = Convert.ToInt32(item.RefSupplierID);
                            }

                            obReqItem.SetBy      = Convert.ToInt32(Session["UserID"]);
                            obReqItem.SetOn      = DateTime.Now;
                            obReqItem.ModifiedBy = Convert.ToInt32(Session["UserID"]);
                            obReqItem.ModifiedOn = DateTime.Now;
                            obReqItem.IPAddress  = GetIPAddress.LocalIPAddress();
                            repository.InvTransRequestItemRepository.Insert(obReqItem);
                        }
                    }
                    try
                    {
                        repository.Save();
                        _vmMsg.ReturnCode = ob.RequestNo;
                        _vmMsg.ReturnId   = repository.InvTransRequestRepository.Get().Last().RequestID;
                        _vmMsg.Type       = Enums.MessageType.Success;
                        _vmMsg.Msg        = "Saved Successfully.";
                    }
                    catch (Exception ex)
                    {
                        _vmMsg.Type = Enums.MessageType.Error;
                        _vmMsg.Msg  = "Saved Faild.";
                    }
                }
                else    // Update
                {
                    INV_TransRequest ob = repository.InvTransRequestRepository.GetByID(dataSet.RequestID);
                    ob.RequestDate        = DalCommon.SetDate(dataSet.RequestDate);
                    ob.RequestFrom        = dataSet.RequestFrom.ToString();
                    ob.RequestTo          = dataSet.RequestTo.ToString();
                    ob.ReturnMethod       = dataSet.ReturnMethod;
                    ob.ExpectetReturnTime = Convert.ToByte(dataSet.ExpectetReturnTime);
                    ob.ModifiedOn         = DateTime.Now;
                    ob.ModifiedBy         = Convert.ToInt32(Session["UserID"]);
                    ob.FromSource         = "STR";
                    if (ob.RequestType == "LNIR")
                    {
                        ob.FromSource = dataSet.ToSource;
                    }
                    else if (ob.RequestType == "LNRR")
                    {
                        ob.ToSource = dataSet.ToSource;
                    }
                    else
                    {
                    }
                    //ob.ToSource = dataSet.ToSource;
                    ob.IPAddress = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRepository.Update(ob);

                    INV_TransRequestRef obRef = repository.InvTransRequestRefRepository.Get(filter: o => o.RequestID == dataSet.RequestID).FirstOrDefault();
                    obRef.ReturnMethod = dataSet.ReturnMethod;
                    obRef.ModifiedBy   = Convert.ToInt32(Session["UserID"]);
                    obRef.ModifiedOn   = DateTime.Now;
                    obRef.IPAddress    = GetIPAddress.LocalIPAddress();
                    repository.InvTransRequestRefRepository.Update(obRef);
                    if (dataSet.ChemicalSelectedList != null)
                    {
                        foreach (var item in dataSet.ChemicalSelectedList)
                        {
                            if (item.TransRequestItemID == 0)
                            {
                                INV_TransRequestItem obReqItem = new INV_TransRequestItem();
                                obReqItem.RequestID         = ob.RequestID;
                                obReqItem.TransRequestRefID = obRef.TransRequestRefID;
                                obReqItem.ReturnMethod      = dataSet.ReturnMethod;
                                if (Convert.ToInt32(item.ItemID) > 0)
                                {
                                    obReqItem.ItemID = Convert.ToInt32(item.ItemID);
                                }
                                if (Convert.ToByte(item.PackSize) > 0)
                                {
                                    obReqItem.PackSize = Convert.ToByte(item.PackSize);
                                }
                                else
                                {
                                    obReqItem.PackSize = 0;
                                }
                                obReqItem.PackQty = Convert.ToInt32(item.PackQty);
                                if (Convert.ToByte(item.SizeUnit) > 0)
                                {
                                    obReqItem.SizeUnit = Convert.ToByte(item.SizeUnit);
                                }

                                if (obReqItem.PackQty > 0 && item.PackSize != null)
                                {
                                    obReqItem.TransQty = obReqItem.PackQty * ((Convert.ToByte(repository.SysSizeRepository.GetByID(Convert.ToInt32(item.PackSize)).SizeName)));//Convert.ToDecimal(item.ReferenceQty);
                                }
                                else
                                {
                                    obReqItem.TransQty = Convert.ToDecimal(item.ReferenceQty);
                                }

                                if (Convert.ToByte(item.ReferenceUnit) > 0)
                                {
                                    obReqItem.TransUnit = Convert.ToByte(item.ReferenceUnit);
                                }
                                if (Convert.ToInt32(item.RefSupplierID) > 0)
                                {
                                    obReqItem.RefSupplierID = Convert.ToInt32(item.RefSupplierID);
                                }
                                obReqItem.SetBy      = Convert.ToInt32(Session["UserID"]);
                                obReqItem.SetOn      = DateTime.Now;
                                obReqItem.ModifiedBy = Convert.ToInt32(Session["UserID"]);
                                obReqItem.ModifiedOn = DateTime.Now;
                                obReqItem.IPAddress  = GetIPAddress.LocalIPAddress();
                                repository.InvTransRequestItemRepository.Insert(obReqItem);
                            }
                            else
                            {
                                INV_TransRequestItem obReqItem = repository.InvTransRequestItemRepository.GetByID(item.TransRequestItemID);
                                if (Convert.ToInt32(item.ItemID) > 0)
                                {
                                    obReqItem.ItemID = Convert.ToInt32(item.ItemID);
                                }
                                if (Convert.ToByte(item.PackSize) > 0)
                                {
                                    obReqItem.PackSize = Convert.ToByte(item.PackSize);
                                }
                                else
                                {
                                    obReqItem.PackSize = 0;
                                }
                                obReqItem.PackQty      = Convert.ToInt32(item.PackQty);
                                obReqItem.ReturnMethod = dataSet.ReturnMethod;

                                if (Convert.ToByte(item.SizeUnit) > 0)
                                {
                                    obReqItem.SizeUnit = Convert.ToByte(item.SizeUnit);
                                }
                                if (obReqItem.PackQty > 0 && item.PackSize != null)
                                {
                                    obReqItem.TransQty = obReqItem.PackQty * (Convert.ToByte(repository.SysSizeRepository.GetByID(Convert.ToInt32(item.PackSize)).SizeName));    //Convert.ToDecimal(item.ReferenceQty);
                                }
                                else
                                {
                                    obReqItem.TransQty = Convert.ToDecimal(item.ReferenceQty);
                                }
                                if (Convert.ToByte(item.ReferenceUnit) > 0)
                                {
                                    obReqItem.TransUnit = Convert.ToByte(item.ReferenceUnit);
                                }

                                if (Convert.ToInt32(item.RefSupplierID) > 0)
                                {
                                    obReqItem.RefSupplierID = Convert.ToInt32(item.RefSupplierID);
                                }
                                obReqItem.ModifiedBy = Convert.ToInt32(Session["UserID"]);
                                obReqItem.ModifiedOn = DateTime.Now;
                                obReqItem.IPAddress  = GetIPAddress.LocalIPAddress();
                                repository.InvTransRequestItemRepository.Update(obReqItem);
                            }
                        }
                    }

                    try
                    {
                        repository.Save();
                        _vmMsg.ReturnCode = ob.RequestNo;
                        _vmMsg.ReturnId   = dataSet.RequestID;
                        _vmMsg.Type       = Enums.MessageType.Success;
                        _vmMsg.Msg        = "Updated Successfully.";
                    }
                    catch (Exception x)
                    {
                        _vmMsg.Type = Enums.MessageType.Error;
                        _vmMsg.Msg  = "Saved Faild.";
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                eve.Entry.Entity.GetType().Name,
                                                eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName,
                                                    ve.ErrorMessage));
                    }
                }
                throw new DbEntityValidationException(sb.ToString(), e);
            }
            return(Json(new { msg = _vmMsg }));
        }