public CreditRequestResult Add(DomainCreditRequest creditRequest, byte[] militaryId, byte[] incomeCertificate, string email, string baseUrl, string baseLocalhostUrl, ModelStateDictionary modelState)
        {
            creditRequest.Credit = Mapper.Map<DomainCredit>(_iUnitOfWork.CreditRepository.Get(creditRequest.CreditId));
            _iValidationService.ValidateSum(creditRequest.Sum, creditRequest.Credit.MinSum, creditRequest.Credit.MaxSum, modelState, false);
            _iValidationService.ValidateMonthCount(creditRequest.MonthCount, creditRequest.Credit.MinMonthPeriod, creditRequest.Credit.MaxMonthPeriod, modelState);
            if (!modelState.IsValid)
            {
                return new CreditRequestResult
                {
                    ModelState = modelState
                };
            }

            var customer = creditRequest.Customer;//_iUnitOfWork.CustomerRepository.GetCustomerByUserId(userId);
            var customerDb = _iUnitOfWork.CustomerRepository.GetAll()
                .FirstOrDefault(c => c.IdentificationNumber == creditRequest.Customer.IdentificationNumber);

            if (customerDb == null)
            {
                var id = _iCustomerService.Register(customer, email);
                creditRequest.CustomerId = id;
            }
            else
            {
                creditRequest.CustomerId = customerDb.Id;
            }

            if (militaryId != null)
            {
                var militaryPath = _iImageService.SaveImageFromByteArray(militaryId, baseUrl, creditRequest.CustomerId,
                    ImageType.MilitaryId, baseLocalhostUrl);
                creditRequest.MilitaryIdPath = militaryPath;
            }
            var incomeSertificatePath = _iImageService.SaveImageFromByteArray(incomeCertificate, baseUrl, creditRequest.CustomerId, ImageType.IncomeCertificate, baseLocalhostUrl);
            creditRequest.IncomeCertificatePath = incomeSertificatePath;

            var creditRequestDal = Mapper.Map<CreditRequest>(creditRequest);
            creditRequestDal.Credit = null;
            creditRequestDal.Customer = null;
            _iUnitOfWork.CreditRequestRepository.Add(creditRequestDal);
            _iUnitOfWork.SaveChanges();

            creditRequest.Id = creditRequestDal.Id;
            new CreditRequestDocService().FillConcreteContract(creditRequest);
            return new CreditRequestResult()
            {
                ModelState = null,
                DocPath = GetContract(creditRequestDal.Id, baseLocalhostUrl)
            };
        }
        public void FillConcreteContract(DomainCreditRequest creditRequest)
        {
            if (!File.Exists(_templateDocPath))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(_templateDocPath));
                GenerateContractTemplate();
            }

            var byteArray = File.ReadAllBytes(_templateDocPath);

            using (var stream = new MemoryStream())
            {
                stream.Write(byteArray, 0, byteArray.Length);
                using (var wordDoc = WordprocessingDocument.Open(stream, true))
                {
                    var body = wordDoc.MainDocumentPart.Document.Body;
                    var runs = body.Descendants<Run>().ToList();
                    var i = 0;
                    var dateNow = GlobalValues.BankDateTime;

                    //day & month
                    FindAndReplace(ref i, runs, DaymonthPlace, dateNow.ToString("dd MM"));//customerDeposit.StartDate.ToString("M"));
                    //year
                    FindAndReplace(ref i, runs, YearPlace, " " + dateNow.ToString("yyyy"));
                    //sum
                    FindAndReplace(ref i, runs, SumPlace, creditRequest.Sum.ToString());
                    //FIO
                    FindAndReplace(ref i, runs, LastNamePlace, creditRequest.Customer.Lastname);
                    FindAndReplace(ref i, runs, FirstNamePlace, creditRequest.Customer.Firstname);
                    FindAndReplace(ref i, runs, PatronymicPlace, creditRequest.Customer.Patronymic ?? "");
                    //birthday
                    FindAndReplace(ref i, runs, BirthDayPlace, creditRequest.Customer.DateOfBirth.ToString("dd.MM.yyyy") ?? "");
                    //documentNumber
                    FindAndReplace(ref i, runs, DocumentNumberPlace, creditRequest.Customer.DocumentNumber);
                    //address
                    FindAndReplace(ref i, runs, CityPlace, creditRequest.Customer.Address.City);
                    FindAndReplace(ref i, runs, StreetPlace, creditRequest.Customer.Address.Street);
                    FindAndReplace(ref i, runs, StreetNumberPlace, creditRequest.Customer.Address.House);
                    FindAndReplace(ref i, runs, FlatPlace, creditRequest.Customer.Address.Flat != null ? creditRequest.Customer.Address.Flat.ToString() : "");

                   //credit name
                    FindAndReplace(ref i, runs, CreditNamePlace, creditRequest.Credit.Name);
                    //sum
                    FindAndReplace(ref i, runs, SumPlace, creditRequest.Sum.ToString());
                    //monthCount
                    FindAndReplace(ref i, runs, MonthCountPlace, creditRequest.MonthCount.ToString());

                    //customerPlace
                    FindAndReplace(ref i, runs, FioPlace,
                        string.Format("{0} {1}{2}", creditRequest.Customer.Lastname,
                            creditRequest.Customer.Firstname,
                            creditRequest.Customer.Patronymic != null
                                ? " " + creditRequest.Customer.Patronymic
                                : ""), UnderlineValues.None);

                    //day & month
                    FindAndReplace(ref i, runs, DaymonthPlace, dateNow.ToString("dd MM"));//customerDeposit.StartDate.ToString("M"));
                    //year
                    FindAndReplace(ref i, runs, YearPlace, " " + dateNow.ToString("yyyy"));

                    wordDoc.MainDocumentPart.Document.Save();
                    //                                            wordDoc.MainDocumentPart.Document.Save(stream);
                    File.WriteAllBytes(
                        string.Format("{0}{1}.docx", _creditContractsDocPath, creditRequest.Id),
                        stream.ToArray());
                }
            }
        }