示例#1
0
        public CashOrder Card([FromBody] int id)
        {
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }

            var cashOrder = _repository.Get(id);

            if (cashOrder == null)
            {
                throw new InvalidOperationException();
            }

            return(cashOrder);
        }
示例#2
0
        public Selling Sell([FromBody] Selling selling)
        {
            using (var transaction = _repository.BeginTransaction())
            {
                if (selling.Id == 0)
                {
                    throw new PawnshopApplicationException("Сохраните реализацию для регистрации продажи");
                }

                if (selling.CashOrderId.HasValue)
                {
                    throw new PawnshopApplicationException("Продажа уже зарегистрирована");
                }

                selling.Status = SellingStatus.Sold;

                if (selling.ContractId.HasValue && selling.ContractPositionId.HasValue)
                {
                    var contract = _contractRepository.Get(selling.ContractId.Value);
                    var position = contract.Positions.SingleOrDefault(p => p.Id == selling.ContractPositionId.Value);
                    if (position != null)
                    {
                        position.Status = ContractPositionStatus.Disposed;
                    }
                    if (contract.Positions.All(p => p.Status == ContractPositionStatus.Disposed))
                    {
                        contract.Status = ContractStatus.Disposed;
                    }
                    _contractRepository.Update(contract);
                }

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

                var settings = _branchContext.Configuration.CashOrderSettings
                               .Get(selling.CollateralType).DisposeSettings;

                if (!selling.SellingDate.HasValue)
                {
                    throw new PawnshopApplicationException("Поле дата продажи обязательно для заполнения");
                }

                if (!selling.SellingCost.HasValue)
                {
                    throw new PawnshopApplicationException("Поле стоимость продажи обязательно для заполнения");
                }

                if (!settings.DebitId.HasValue)
                {
                    throw new PawnshopApplicationException("В настройках организации заполните счет дебет для реализации");
                }

                if (!settings.CreditId.HasValue)
                {
                    throw new PawnshopApplicationException("В настройках организации заполните счет кредит для реализации");
                }

                var cashOrder = new CashOrder
                {
                    OrderType   = OrderType.CashIn,
                    OrderNumber = _counterRepository.Next(
                        OrderType.CashIn, selling.SellingDate.Value.Year,
                        _branchContext.Branch.Id,
                        _branchContext.Configuration.CashOrderSettings.CashInNumberCode),
                    OrderDate       = selling.SellingDate.Value,
                    OrderCost       = selling.SellingCost.Value,
                    DebitAccountId  = settings.DebitId.Value,
                    CreditAccountId = settings.CreditId.Value,
                    UserId          = _sessionContext.UserId,
                    Reason          = $"Продажа изделия {selling.Note}",
                    RegDate         = DateTime.Now,
                    OwnerId         = _branchContext.Branch.Id,
                    BranchId        = _branchContext.Branch.Id,
                    AuthorId        = _sessionContext.UserId
                };

                _cashOrderRepository.Insert(cashOrder);
                selling.CashOrderId = cashOrder.Id;
                selling.CashOrder   = _cashOrderRepository.Get(cashOrder.Id);

                _repository.Update(selling);

                transaction.Commit();
            }

            return(selling);
        }