Example #1
0
        public static ContractorVm GetContractorInfo(int id)
        {
            ContractorVm carrVmObj = new ContractorVm();

            if (id == 0)
            {
                ContactPersonVm contactVm = new ContactPersonVm();
                carrVmObj.ContactPersons.Add(contactVm);
            }
            else
            {
                EasyFreightEntities db        = new EasyFreightEntities();
                Contractor          carrDbObj = db.Contractors.Include("ContractorContacts")
                                                .Where(x => x.ContractorId == id).FirstOrDefault();

                Mapper.CreateMap <Contractor, ContractorVm>().IgnoreAllNonExisting();
                Mapper.Map(carrDbObj, carrVmObj);

                Mapper.CreateMap <ContractorContact, ContactPersonVm>().IgnoreAllNonExisting()
                .ForMember(x => x.FkValue, opts => opts.MapFrom(scr => scr.ContractorId));

                ContactPersonVm contactVm;
                foreach (var item in carrDbObj.ContractorContacts)
                {
                    contactVm = new ContactPersonVm();
                    Mapper.Map(item, contactVm);
                    carrVmObj.ContactPersons.Add(contactVm);
                }
            }
            return(carrVmObj);
        }
Example #2
0
        public ActionResult Add(int id = 0)
        {
            #region Check Rights
            bool hasRights;
            if (id == 0) //Check export rights
            {
                hasRights = AdminHelper.CheckUserAction(ScreenEnum.Contractor, ActionEnum.Add);
            }
            else
            {
                hasRights = AdminHelper.CheckUserAction(ScreenEnum.Contractor, ActionEnum.Edit);
            }

            if (!hasRights)
            {
                return(RedirectToAction("UnAuthorized", "Home", new { area = "" }));
            }

            #endregion

            ViewData["CityList"] = ListCommonHelper.GetCityGrouped();
            ContractorVm contractorObj = ContractorHelper.GetContractorInfo(id);
            return(View(contractorObj));
        }
Example #3
0
        public static string AddEditContractor(ContractorVm carrVm)
        {
            int    carrId  = carrVm.ContractorId;
            string isSaved = "true";
            EasyFreightEntities      db = new EasyFreightEntities();
            Contractor               carrDb;
            List <ContractorContact> dbContactList = new List <ContractorContact>();

            if (carrId == 0) //Add new case
            {
                carrDb = new Contractor();
            }
            else
            {
                carrDb = db.Contractors.Include("ContractorContacts").Where(x => x.ContractorId == carrId).FirstOrDefault();
                //delete any removed contact on the screen
                dbContactList = carrDb.ContractorContacts.ToList();
                try
                {
                    //Get contact Ids sent from the screen
                    List <int> contactVmIds = carrVm.ContactPersons.Select(x => x.ContactId).ToList();
                    var        contactDel   = dbContactList.Where(x => !contactVmIds.Contains(x.ContactId)).ToList();

                    foreach (var item in contactDel)
                    {
                        db.ContractorContacts.Remove(item);
                    }
                }
                catch { }
            }

            Mapper.CreateMap <ContractorVm, Contractor>().IgnoreAllNonExisting();
            Mapper.Map(carrVm, carrDb);



            if (carrId == 0)
            {
                Random rand = new Random();
                carrDb.ContractorCode = rand.Next(10000).ToString();
                db.Contractors.Add(carrDb);
            }

            Mapper.CreateMap <ContactPersonVm, ContractorContact>().IgnoreAllNonExisting()
            .ForMember(x => x.ContractorId, opts => opts.MapFrom(scr => scr.FkValue));

            ContractorContact carrContactDb;

            if (carrVm.ContactPersons != null && carrVm.ContactPersons.Count > 0)
            {
                foreach (var item in carrVm.ContactPersons.Where(x => !string.IsNullOrEmpty(x.ContactName)))
                {
                    if (item.ContactId == 0)
                    {
                        carrContactDb = new ContractorContact();
                    }
                    else
                    {
                        int contVmId = item.ContactId;
                        carrContactDb = dbContactList.Where(x => x.ContactId == contVmId).FirstOrDefault();
                    }

                    Mapper.Map(item, carrContactDb);

                    if (item.ContactId == 0)
                    {
                        carrDb.ContractorContacts.Add(carrContactDb);
                    }
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.Message;
            }

            return(isSaved);
        }
Example #4
0
        public ActionResult AddEditContractor(ContractorVm contractorVm)
        {
            string isSaved = ContractorHelper.AddEditContractor(contractorVm);

            return(Json(isSaved));
        }