Exemplo n.º 1
0
        public async Task <PiDetailResult> Create(CreatePiDetailDto createPiDetailDto)
        {
            var piDetail = new PeroformaInvoiceDetail()
            {
                DepositDate        = createPiDetailDto.DepositDate,
                DepositPrice       = createPiDetailDto.DepositPrice,
                BrokerId           = createPiDetailDto.BrokerId,
                PeroformaInvoiceId = createPiDetailDto.PiId,
                IsSold             = false
            };
            await piDetailRepository.AddEntity(piDetail);

            await _brokerRepository.UpdateBrokerAmount(piDetail.BrokerId, piDetail.DepositPrice, true);

            await piDetailRepository.SaveChanges();

            return(PiDetailResult.Success);
        }
        private async Task <SalesResult> FillAutomaticCurrencySaleDetailExDecsAndCurrencySaleDetailPis(CreateSaleDto saleDto, CurrencySale currencySales)
        {
            #region Get List Of ExDeclaration (Is Not Sold Yet)

            if (saleDto.CurrencyType == CurrencyType.CarrencySales)
            {
                Boolean isExDecAutomatic;
                var     exDecList = new List <ExDecExport>();
                if (saleDto.ExDecExport.Count > 0)
                {
                    isExDecAutomatic = false;
                    exDecList        = saleDto.ExDecExport;
                }
                else
                {
                    isExDecAutomatic = true;
                    var lisexDecList = await _declarationRepository.GetExDecAccountBalanceByExDecId();

                    foreach (var item in lisexDecList)
                    {
                        exDecList.Add(new ExDecExport {
                            Id = item.Id, Price = item.Price, ExCode = item.ExchangeDeclarationCode
                        });
                    }
                }

                #region Insert Into CurrencySaleDetailExDec

                if (isExDecAutomatic)
                {
                    var saleexDecResult = await InserSaleCurrExDecAutomatic(exDecList, saleDto, currencySales);

                    if (saleexDecResult != SalesResult.Success)
                    {
                        return(saleexDecResult);
                    }
                }
                else
                {
                    var saleexDecResult = await InserSaleCurrExDecManual(exDecList, saleDto, currencySales);

                    if (saleexDecResult != SalesResult.Success)
                    {
                        return(saleexDecResult);
                    }
                }
                #endregion
            }

            #endregion

            #region Get List Of PiDetails (Is Not Sold Yet)

            var brokerId = saleDto.CurrencyType == CurrencyType.CurrencyTransferFromTheBroker ? saleDto.CustomerId : saleDto.BrokerId;

            var piDetails = await _piDetailRepository.GetAccountBalanceByDetailsByBrokerId(brokerId);

            #region Insert Into  CurrencySalePi

            var salePiDetailResult = await InserSaleCurrPiDetail(piDetails, saleDto, currencySales);

            if (salePiDetailResult != SalesResult.Success)
            {
                return(salePiDetailResult);
            }
            if (saleDto.CurrencyType == CurrencyType.CurrencyTransferFromTheBroker)
            {
                var updateStartBrokerAmount = await _brokerRepository.UpdateBrokerAmount(saleDto.CustomerId, saleDto.SalePrice + saleDto.TransferPrice, false);

                if (!updateStartBrokerAmount)
                {
                    return(SalesResult.CannotUpdateBrokerAmountBalance);
                }

                var updateBrokerAmount = await _brokerRepository.UpdateBrokerAmount(saleDto.BrokerId, saleDto.SalePrice, true);

                if (!updateBrokerAmount)
                {
                    return(SalesResult.CannotUpdateBrokerAmountBalance);
                }
            }
            else
            {
                var updateBrokerAmount = await _brokerRepository.UpdateBrokerAmount(saleDto.BrokerId, saleDto.SalePrice + saleDto.TransferPrice, false);

                if (!updateBrokerAmount)
                {
                    return(SalesResult.CannotUpdateBrokerAmountBalance);
                }
            }

            #endregion

            #endregion

            return(SalesResult.Success);
        }