public ActionResult Create1Step(string Number, DateTime Date, DateTime?DateEnd, string Title, string Description, long ProjectId, long LeadId, Lead model) { // Ищем или создаем лида, к которому создается заказ Lead lead; if (LeadId != -1) { lead = DataContext.Leads.FirstOrDefault(l => l.Id == LeadId); if (lead == null) { ShowError("Такой лид не найден"); return(RedirectToAction("Create")); } } else { model.DateCreated = DateTime.Now; DataContext.Leads.InsertOnSubmit(model); lead = model; } // Создаем договор var agreement = new LeadAgreement() { Number = Number, Date = Date, EndDate = DateEnd, Title = Title, Description = Description, ProjectId = ProjectId, Lead = lead, User = CurrentUser, Status = (short)LeadAgreementStatus.Negotiation, DateCreated = DateTime.Now }; agreement.LeadAgreementChangements.Add(new LeadAgreementChangement() { User = CurrentUser, LeadAgreement = agreement, Comments = "Создание договора пользователем " + CurrentUser.GetFio(), DateCreated = DateTime.Now }); DataContext.LeadAgreements.InsertOnSubmit(agreement); DataContext.SubmitChanges(); ShowSuccess(string.Format("Договор №{0} для лида {1} успешно создан", Number, lead.ToString())); return(RedirectToAction("Info", new { id = agreement.Id })); }
public ActionResult UpdateAgreementInfo(LeadAgreement model, string comments) { var projects = CurrentUser.IsAdmin() ? DataContext.Projects.Select(p => p.Id) : CurrentUser.ProjectUsers.Select(p => p.ProjectId); var agreement = DataContext.LeadAgreements.FirstOrDefault(a => a.Id == model.Id && projects.Contains(a.ProjectId)); if (agreement == null) { ShowError("Такой договор не найден"); return(RedirectToAction("Index")); } // Валидируем номер договора - он должен быть уникальным var existed = agreement.Project.LeadAgreements.Any( a => a.Number.ToLower() == model.Number.ToLower() && a.Id != agreement.Id); if (existed) { ShowError("Договор с таким номером уже есть в системе"); return(RedirectToAction("Info", new { id = agreement.Id })); } // Изменение var changement = new LeadAgreementChangement() { Comments = comments, User = CurrentUser, LeadAgreement = agreement, DateCreated = DateTime.Now }; if (model.Number != agreement.Number) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = agreement.Number, NewValue = model.Number, PropertyName = "Номер" }); agreement.Number = model.Number; } if (model.Date != agreement.Date) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = agreement.Date.FormatDate(), NewValue = model.Date.FormatDate(), PropertyName = "Дата заключения" }); agreement.Date = model.Date; } if (model.EndDate != agreement.EndDate) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = agreement.EndDate.FormatDate(), NewValue = model.EndDate.FormatDate(), PropertyName = "Дата завершения действия" }); agreement.EndDate = model.EndDate; } if (model.EndDate != agreement.EndDate) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = agreement.EndDate.FormatDate(), NewValue = model.EndDate.FormatDate(), PropertyName = "Дата завершения действия" }); agreement.EndDate = model.EndDate; } if (model.Title != agreement.Title) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = agreement.Title, NewValue = model.Title, PropertyName = "Название" }); agreement.Title = model.Title; } if (model.Description != agreement.Description) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = agreement.Description, NewValue = model.Description, PropertyName = "Описание" }); agreement.Description = model.Description; } if (model.Status != agreement.Status) { changement.LeadAgreementChangementValues.Add(new LeadAgreementChangementValue() { LeadAgreementChangement = changement, OldValue = ((LeadAgreementStatus)agreement.Status).GetEnumMemberName(), NewValue = ((LeadAgreementStatus)model.Status).GetEnumMemberName(), PropertyName = "Статус" }); agreement.Status = model.Status; } if (changement.LeadAgreementChangementValues.Count > 0) { agreement.LeadAgreementChangements.Add(changement); agreement.DateModified = DateTime.Now; DataContext.SubmitChanges(); } ShowSuccess(string.Format("Договор №{0} был успешно сохранен", agreement.Number)); return(RedirectToAction("Info", new { id = agreement.Id })); }