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 Save(Lead model)
        {
            var lead = DataContext.Leads.FirstOrDefault(l => l.Id == model.Id);
            if (lead == null)
            {
                ShowError("Такой лид не найден");
                return RedirectToAction("Index");
            }

            // Основные данные
            lead.FirstName = model.FirstName;
            lead.SurName = model.SurName;
            lead.LastName = model.LastName;
            lead.Phone = model.Phone;
            lead.Email = model.Email;

            // Паспортные
            if (lead.LeadPassportInfos == null)
            {
                lead.LeadPassportInfos = new LeadPassportInfo()
                {
                    Lead = lead
                };
            }
            lead.LeadPassportInfos.Number = model.LeadPassportInfos.Number;
            lead.LeadPassportInfos.Series = model.LeadPassportInfos.Series;
            lead.LeadPassportInfos.IssuedBy = model.LeadPassportInfos.IssuedBy;
            lead.LeadPassportInfos.IssueDate = model.LeadPassportInfos.IssueDate;

            // Юридические
            if (lead.LeadLegalInfos == null)
            {
                lead.LeadLegalInfos = new LeadLegalInfo()
                {
                    Lead = lead
                };
            }
            lead.LeadLegalInfos.LegalType = model.LeadLegalInfos.LegalType;
            lead.LeadLegalInfos.CompanyName = model.LeadLegalInfos.CompanyName;
            lead.LeadLegalInfos.DirectorFIO = model.LeadLegalInfos.DirectorFIO;
            lead.LeadLegalInfos.INN = model.LeadLegalInfos.INN;
            lead.LeadLegalInfos.OGRN = model.LeadLegalInfos.OGRN;
            lead.LeadLegalInfos.KPP = model.LeadLegalInfos.KPP;
            lead.LeadLegalInfos.OKPO = model.LeadLegalInfos.OKPO;
            lead.LeadLegalInfos.LegalAddress = model.LeadLegalInfos.LegalAddress;
            lead.LeadLegalInfos.FacticalAddress = model.LeadLegalInfos.FacticalAddress;

            // Рассчетные данные
            if (lead.LeadAccountInfos == null)
            {
                lead.LeadAccountInfos = new LeadAccountInfo()
                {
                    Lead = lead
                };
            }
            lead.LeadAccountInfos.Number = model.LeadAccountInfos.Number;
            lead.LeadAccountInfos.BIK = model.LeadAccountInfos.BIK;
            lead.LeadAccountInfos.BankName = model.LeadAccountInfos.BankName;
            lead.LeadAccountInfos.KNumber = model.LeadAccountInfos.KNumber;

            // TODO: добавить историю редактирования лидов

            DataContext.SubmitChanges();

            ShowSuccess("Данные были успешно сохранены");

            return RedirectToAction("Edit", new {id = lead.Id});
        }
        public ActionResult Create1Step(long LeadId, long ProjectId, 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 order = new LeadOrder()
            {
                DateCreated = DateTime.Now,
                User = CurrentUser,
                DeliveryType = (short)DeliveryTypes.Self,
                PaymentType = (short)PaymentTypes.BankPayment,
                Status = (short)LeadOrderStatus.Initial,
                ProjectId = ProjectId
            };

            // Создаем первоначальные данные по истории заявки
            order.LeadOrderChangements.Add(new LeadOrderChangement()
            {
                AuthorId = CurrentUser.Id,
                LeadOrder = order,
                DateCreated = DateTime.Now,
                NewStatus = (short)LeadOrderStatus.Initial,
                Comments = "Создание заказа пользователем " + CurrentUser.GetFio()
            });

            lead.LeadOrders.Add(order);
            DataContext.SubmitChanges();

            ShowSuccess(string.Format("Заказ №{0} успешно создан для лида {1}", order.Id, lead.ToString()));

            return RedirectToAction("EditOrderItems", new { id = order.Id });
        }