public ActionResult Save(EXPDeliveryChallan dataSet)
        {
            _userId = Convert.ToInt32(Session["UserID"]);

            objValMssg = objDalDeliveryChallan.Save(dataSet, _userId);
            return(Json(new { msg = objValMssg }));
        }
        public ValidationMsg Save(EXPDeliveryChallan model, int _userid)//, string pageUrl
        {
            try
            {
                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        //model.DeliverChallanNo = DalCommon.GetPreDefineNextCodeByUrl(pageUrl);//DalCommon.GetPreDefineValue("1", "00045");
                        if (model.DeliverChallanNo != null)
                        {
                            EXP_DeliveryChallan tblDeliveryChallan = SetTotblDeliveryChallanModelObject(model, _userid);//, _userid
                            _context.EXP_DeliveryChallan.Add(tblDeliveryChallan);
                            _context.SaveChanges();

                            if (model.expDeliveryChallanCIList.Count > 0)
                            {
                                foreach (var deliveryChallanCI in model.expDeliveryChallanCIList)
                                {
                                    deliveryChallanCI.DeliverChallanID = tblDeliveryChallan.DeliverChallanID;
                                    EXP_DeliveryChallanCI tblDeliveryChallanCI = SetToDeliveryChallanCIModelObject(deliveryChallanCI, _userid);
                                    _context.EXP_DeliveryChallanCI.Add(tblDeliveryChallanCI);
                                    _context.SaveChanges();
                                }
                            }
                            _context.SaveChanges();

                            tx.Complete();


                            _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);
        }
        public EXP_DeliveryChallan SetTotblDeliveryChallanModelObject(EXPDeliveryChallan model, int _userid)//int _userid
        {
            EXP_DeliveryChallan entity = new EXP_DeliveryChallan();

            entity.DeliverChallanID    = model.DeliverChallanID;
            entity.DeliverChallanNo    = model.DeliverChallanNo == null ? "" : model.DeliverChallanNo;
            entity.DeliverChallanRef   = model.DeliverChallanRef == null ? "" : model.DeliverChallanRef;
            entity.DeliveryChallanNote = model.DeliveryChallanNote == null ? "" : model.DeliveryChallanNote;
            entity.DeliverChallanDate  = model.DeliverChallanDate;
            entity.TruckNo             = model.TruckNo == null ? "" : model.TruckNo;
            entity.RecordStatus        = "NCF";
            entity.SetOn     = DateTime.Now;
            entity.SetBy     = _userid;
            entity.IPAddress = GetIPAddress.LocalIPAddress();

            return(entity);
        }
        public ValidationMsg Update(EXPDeliveryChallan model, int _userid)
        {
            _vmMsg = new ValidationMsg();
            try
            {
                #region Update

                using (var tx = new TransactionScope())
                {
                    using (_context)
                    {
                        #region Delivery Challan

                        EXP_DeliveryChallan deliveryChallan = SetTotblDeliveryChallanModelObject(model, _userid);
                        var OriginalEntity = _context.EXP_DeliveryChallan.First(m => m.DeliverChallanID == model.DeliverChallanID);

                        OriginalEntity.DeliverChallanNo    = deliveryChallan.DeliverChallanNo;
                        OriginalEntity.DeliverChallanDate  = deliveryChallan.DeliverChallanDate;
                        OriginalEntity.TruckNo             = deliveryChallan.TruckNo;
                        OriginalEntity.DeliveryChallanNote = deliveryChallan.DeliveryChallanNote;
                        OriginalEntity.ModifiedOn          = DateTime.Now;
                        OriginalEntity.ModifiedBy          = _userid;
                        _context.SaveChanges();
                        #endregion


                        #region Save NEW Data & Update Existing Crust Leather Transfer Data

                        if (model.expDeliveryChallanCIList != null)
                        {
                            foreach (EXPDeliveryChallanCI deliveryChallanCIList in model.expDeliveryChallanCIList)
                            {
                                deliveryChallanCIList.DeliverChallanID = model.DeliverChallanID;


                                if (deliveryChallanCIList.CIID != model.CIID)
                                {
                                    deliveryChallanCIList.DeliverChallanID = model.DeliverChallanID;
                                    EXP_DeliveryChallanCI tblDeliveryChallanCI = SetToDeliveryChallanCIModelObject(deliveryChallanCIList, _userid);
                                    _context.EXP_DeliveryChallanCI.Add(tblDeliveryChallanCI);
                                }
                                else
                                {
                                    EXP_DeliveryChallanCI deliveryChallanCIEntity = SetToDeliveryChallanCIModelObject(deliveryChallanCIList, _userid);
                                    var OriginalIssueItemEntity = _context.EXP_DeliveryChallanCI.First(m => m.DeliverChallanID == deliveryChallanCIList.DeliverChallanID);


                                    // OriginalDeliveryChallanCI.DeliverChallanID = deliveryChallanCIEntity.DeliverChallanID;
                                    //OriginalDeliveryChallanCI.CIID = deliveryChallanCIEntity.CIID;
                                    //OriginalDeliveryChallanCI.PLID = deliveryChallanCIEntity.PLID;
                                    OriginalIssueItemEntity.SetOn = deliveryChallanCIEntity.SetOn;
                                    OriginalIssueItemEntity.SetBy = deliveryChallanCIEntity.SetBy;


                                    OriginalIssueItemEntity.ModifiedBy = _userid;
                                    OriginalIssueItemEntity.ModifiedOn = DateTime.Now;
                                    OriginalIssueItemEntity.IPAddress  = GetIPAddress.LocalIPAddress();
                                }
                            }
                        }
                        #endregion

                        _context.SaveChanges();
                        tx.Complete();

                        //CLTransferNo = model.CLTransferNo;
                        _vmMsg.Type = Enums.MessageType.Update;
                        _vmMsg.Msg  = "Updated Successfully.";
                    }
                }

                #endregion
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("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)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }

                _vmMsg.Type = Enums.MessageType.Error;
                _vmMsg.Msg  = "Failed to Update.";
            }
            return(_vmMsg);
        }