Exemple #1
0
        public Contract Save([FromBody] Contract contract)
        {
            using (var transaction = _repository.BeginTransaction())
            {
                if (contract.ContractData.Client.DocumentDateExpire < DateTime.Now.Date)
                {
                    throw new PawnshopApplicationException("Срок действия вашего документа истёк");
                }
                if (contract.Id == 0)
                {
                    if (contract.ContractData != null && contract.ContractData.Client != null)
                    {
                        ModelState.Clear();
                        TryValidateModel(contract.ContractData.Client);
                        ModelState.Validate();

                        if (contract.ContractData.Client.Id == 0)
                        {
                            try
                            {
                                var person = new Person(contract.ContractData.Client);
                                _personRepository.Insert(person);
                                contract.ContractData.Client.Id = person.Id;
                            }
                            catch (SqlException e)
                            {
                                if (e.Number == 2627)
                                {
                                    throw new PawnshopApplicationException("Поле ИИН/БИН должно быть уникальным");
                                }
                                throw new PawnshopApplicationException(e.Message);
                            }
                            catch (Exception e)
                            {
                                throw new PawnshopApplicationException(e.Message);
                            }
                        }
                        else
                        {
                            var person = new Person(contract.ContractData.Client);
                            _personRepository.Update(person);
                        }

                        contract.ClientId = contract.ContractData.Client.Id;
                    }
                }

                if (contract.Id == 0)
                {
                    contract.OwnerId  = _branchContext.Branch.Id;
                    contract.BranchId = _branchContext.Branch.Id;
                    contract.AuthorId = _sessionContext.UserId;
                }

                ModelState.Clear();
                TryValidateModel(contract);
                ModelState.Validate();

                if (contract.CollateralType == CollateralType.Car || contract.CollateralType == CollateralType.Machinery)
                {
                    foreach (var position in contract.Positions)
                    {
                        var exist = _repository.Find(new ContractQueryModel
                        {
                            PositionId     = position.PositionId,
                            CollateralType = contract.CollateralType,
                            Status         = ContractStatus.Signed
                        });

                        if (exist != null)
                        {
                            throw new PawnshopApplicationException("По выбранной позиции существует открытый договор");
                        }
                    }
                }

                if (contract.Id > 0)
                {
                    _repository.Update(contract);
                }
                else
                {
                    contract.ContractNumber = _counterRepository.Next(
                        contract.ContractDate.Year, _branchContext.Branch.Id,
                        _branchContext.Configuration.ContractSettings.NumberCode);
                    _repository.Insert(contract);
                }

                transaction.Commit();
            }

            return(contract);
        }