public ActionResult Convert(long id, ViewModels.Leads.LeadConvertViewModel viewModel) { Common.Models.Account.Users currentUser; Common.Models.Opportunities.Opportunity model; using (Data.Transaction trans = Data.Transaction.Create(true)) { try { currentUser = Data.Account.Users.Get(trans, User.Identity.Name); Common.Models.Leads.Lead oldLead = Data.Leads.Lead.Get(trans, id); model = Mapper.Map <Common.Models.Opportunities.Opportunity>(viewModel.Opportunity); model.Lead = Mapper.Map <Common.Models.Leads.Lead>(viewModel.Opportunity.Lead); //model.Lead.Source = Mapper.Map<Common.Models.Leads.LeadSource>(viewModel.Lead.Source); model.Lead.Fee = Mapper.Map <Common.Models.Leads.LeadFee>(viewModel.Opportunity.Lead.Fee); if (oldLead.Fee != null) { model.Lead.Fee = Data.Leads.LeadFee.Edit(trans, model.Lead.Fee, currentUser); } else { model.Lead.Fee = Data.Leads.LeadFee.Create(trans, model.Lead.Fee, currentUser); } //model.Lead.Source = Data.Leads.LeadSource.Edit(trans, model.Lead.Source, currentUser); model.Lead = Data.Leads.Lead.Edit(trans, model.Lead, currentUser); // Check the probability, is it percent or decimal? if (model.Probability.HasValue && model.Probability.Value > 1) { model.Probability = model.Probability.Value / 100; } model = Data.Opportunities.Opportunity.Create(trans, model, currentUser); // Contacts involved if (viewModel.Contact1 != null & viewModel.Contact1.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact1) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact2 != null & viewModel.Contact2.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact2) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact3 != null & viewModel.Contact3.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact3) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact4 != null & viewModel.Contact4.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact4) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact5 != null & viewModel.Contact5.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact5) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact6 != null & viewModel.Contact6.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact6) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact7 != null & viewModel.Contact7.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact7) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact8 != null & viewModel.Contact8.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact8) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact9 != null & viewModel.Contact9.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact9) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } if (viewModel.Contact10 != null & viewModel.Contact10.Id.HasValue) { Common.Models.Opportunities.OpportunityContact oc = new Common.Models.Opportunities.OpportunityContact() { Opportunity = new Common.Models.Opportunities.Opportunity() { Id = model.Id.Value }, Contact = Mapper.Map <Common.Models.Contacts.Contact>(viewModel.Contact10) }; Data.Opportunities.OpportunityContact.Create(trans, oc, currentUser); } trans.Commit(); return(RedirectToAction("Details", "Opportunities", new { Id = model.Id })); } catch (Exception ex) { trans.Rollback(); return(Edit(id)); } } }
public ActionResult Convert(long id) { List <ViewModels.Leads.LeadStatusViewModel> leadStatusList = new List <ViewModels.Leads.LeadStatusViewModel>(); List <ViewModels.Leads.LeadSourceViewModel> leadSourceList = new List <ViewModels.Leads.LeadSourceViewModel>(); List <ViewModels.Opportunities.OpportunityStageViewModel> opportunityStageList = new List <ViewModels.Opportunities.OpportunityStageViewModel>(); ViewModels.Leads.LeadConvertViewModel viewModel = new ViewModels.Leads.LeadConvertViewModel(); viewModel.Opportunity = new ViewModels.Opportunities.OpportunityViewModel(); using (IDbConnection conn = Data.Database.Instance.GetConnection()) { Common.Models.Leads.Lead lead = Mapper.Map <Common.Models.Leads.Lead>(Data.Leads.Lead.Get(id, conn, false)); // Has this already been made an opportunity? If so, redirect Common.Models.Opportunities.Opportunity op = Data.Opportunities.Opportunity.GetForLead(lead.Id.Value); if (op != null) { return(new RedirectResult("/Opportunities/Details/" + op.Id.Value.ToString())); } lead.Contact = Mapper.Map <Common.Models.Contacts.Contact>(Data.Contacts.Contact.Get(lead.Contact.Id.Value)); if (lead.Fee != null) { lead.Fee = Mapper.Map <Common.Models.Leads.LeadFee>(Data.Leads.LeadFee.Get(lead.Fee.Id.Value, conn, false)); if (lead.Fee.To != null) { lead.Fee.To = Mapper.Map <Common.Models.Contacts.Contact>(Data.Contacts.Contact.Get(lead.Fee.To.Id.Value)); } } Data.Leads.LeadStatus.List(conn, false).ForEach(x => { leadStatusList.Add(Mapper.Map <ViewModels.Leads.LeadStatusViewModel>(x)); }); Data.Leads.LeadSource.List(conn, false).ForEach(x => { leadSourceList.Add(Mapper.Map <ViewModels.Leads.LeadSourceViewModel>(x)); }); Data.Opportunities.OpportunityStage.List(conn, false).ForEach(x => { opportunityStageList.Add(Mapper.Map <ViewModels.Opportunities.OpportunityStageViewModel>(x)); }); viewModel.Opportunity.Lead = Mapper.Map <ViewModels.Leads.LeadViewModel>(lead); viewModel.Opportunity.Lead.Contact = Mapper.Map <ViewModels.Contacts.ContactViewModel>(lead.Contact); viewModel.Opportunity.Account = viewModel.Opportunity.Lead.Contact; if (lead.Fee != null) { viewModel.Opportunity.Lead.Fee = Mapper.Map <ViewModels.Leads.LeadFeeViewModel>(lead.Fee); if (lead.Fee.To != null) { viewModel.Opportunity.Lead.Fee.To = Mapper.Map <ViewModels.Contacts.ContactViewModel>(lead.Fee.To); } else { viewModel.Opportunity.Lead.Fee.To = new ViewModels.Contacts.ContactViewModel(); } } else { viewModel.Opportunity.Lead.Fee = new ViewModels.Leads.LeadFeeViewModel() { To = new ViewModels.Contacts.ContactViewModel() }; } } ViewBag.LeadStatusList = leadStatusList; ViewBag.LeadSourceList = leadSourceList; ViewBag.StageList = opportunityStageList; return(View(viewModel)); }