public async Task <SalesResult> Create(CreateSaleDto createPiDto)
        {
            //var result = await ValidationBeforCreateCurrencySales(createPiDto);
            //if (result == SalesResult.Success)
            //{

            #region Create Currency Sale

            var currencySale = new CurrencySale()
            {
                SaleDate         = createPiDto.SaleDate,
                SalePrice        = createPiDto.SalePrice,
                SalePricePerUnit = createPiDto.SalePricePerUnit,
                BrokerId         = createPiDto.BrokerId,
                CustomerId       = createPiDto.CustomerId,
                TransferType     = (CurrencyTransferType)createPiDto.TransferType,
                TransferPrice    = createPiDto.TransferPrice,
                Description      = createPiDto.Description,
                CurrencyType     = (CurrencyType)createPiDto.CurrencyType
            };
            await _saleRepository.AddEntity(currencySale);

            #endregion

            #region Insert Data To CurrencySalesDetail By System

            var result = await FillAutomaticCurrencySaleDetailExDecsAndCurrencySaleDetailPis(createPiDto, currencySale);

            if (result != SalesResult.Success)
            {
                return(result);
            }



            #endregion
            //}


            await _saleRepository.SaveChanges();

            return(result);
        }
        private async Task <SalesResult> InserSaleCurrPiDetail(List <PeroformaInvoiceDetail> peroformaInvoiceDetails, CreateSaleDto saleDto, CurrencySale currencySales)
        {
            long totalInserted = 0;

            foreach (var piDetailDto in peroformaInvoiceDetails)
            {
                #region Validation - PiDetail Price Is Ok

                var usedPriceOfPiCode = await _salePiDetailRepository.GetSumPiCodeUsedById(piDetailDto.Id);

                var remaindPriceOfPiCode = piDetailDto.DepositPrice - usedPriceOfPiCode;
                var priceOfSales         = saleDto.SalePrice + saleDto.TransferPrice;
                //if (piDetailDto.DepositPrice < remaindPriceOfExdecCode)
                //{
                //    return SalesResult.PiAccountBalanceIsLowerThanPrice;
                //}

                long price;
                long profit;
                var  piBasePrice = piDetailDto.PeroformaInvoice.BasePrice;
                if (priceOfSales >= remaindPriceOfPiCode)
                {
                    price = remaindPriceOfPiCode;
                }
                else
                {
                    price = piDetailDto.DepositPrice;
                }

                if (price + totalInserted > priceOfSales)
                {
                    price  = priceOfSales - totalInserted;
                    profit = (saleDto.SalePricePerUnit - piBasePrice) * (price - saleDto.TransferPrice);
                }
                else
                {
                    profit = (saleDto.SalePricePerUnit - piBasePrice) * price;
                }



                #endregion

                #region Insert Data To CurrencySaleDetailPi

                var currencySaleDetailPi = new CurrencySaleDetailPi()
                {
                    CurrencySale             = currencySales,
                    Price                    = price,
                    ProfitLossAmount         = saleDto.CurrencyType == CurrencyType.CarrencySales ? profit : 0,
                    PeroformaInvoiceDetailId = piDetailDto.Id
                };
                await _salePiDetailRepository.AddEntity(currencySaleDetailPi);

                //await _salePiDetailRepository.SaveChanges();

                #endregion

                #region Update Sold In PiDetail

                if (price + usedPriceOfPiCode == piDetailDto.DepositPrice)
                {
                    var updateSold = await _piDetailRepository.SoldedPiDetail(piDetailDto.Id);

                    if (!updateSold)
                    {
                        return(SalesResult.CanNotUpdateSoldPiDetailInDataBase);
                    }
                }



                #endregion

                totalInserted += price;
                if (totalInserted == priceOfSales)
                {
                    return(SalesResult.Success);
                }
            }

            return(SalesResult.Success);
        }
        private async Task <SalesResult> InserSaleCurrExDecManual(List <ExDecExport> exDecList, CreateSaleDto saleDto, CurrencySale currencySales)
        {
            long totalInserted = 0;

            foreach (var exdecItem in exDecList)
            {
                #region Validation - ExDec Price Is Ok

                var exdecEntity = await _declarationRepository.GetEntityById(exdecItem.Id);

                var usedPriceOfExdecCode = await _saleExDecRepository.GetSumExCodeUsedById(exdecItem.Id);

                var remaindPriceOfExdecCode = exdecEntity.Price - usedPriceOfExdecCode;
                //if (exdecEntity.Price < exdec.Price)
                //{
                //    return SalesResult.ExDecAccountBalanceIsLowerThanPrice;
                //}

                //if (exdec.Price < remaindPriceOfExdecCode)
                //{
                //    return SalesResult.ExDecAccountBalanceIsLowerThanPrice;
                //}

                long price;

                if (exdecItem.Price >= remaindPriceOfExdecCode)
                {
                    price = remaindPriceOfExdecCode + (exdecItem.Price - remaindPriceOfExdecCode);
                }
                else
                {
                    price = exdecItem.Price;
                }

                //if (exdecItem.Price + totalInserted > saleDto.SalePrice)
                //{
                //    price = saleDto.SalePrice - totalInserted;
                //}
                #endregion

                #region Insert Into CurrencySaleDetailExDec

                var currencySaleDetailEx = new CurrencySaleDetailExDec
                {
                    CurrencySale    = currencySales,
                    Price           = price,
                    ExDeclarationId = exdecItem.Id
                };
                await _saleExDecRepository.AddEntity(currencySaleDetailEx);

                //await _saleExDecRepository.SaveChanges();

                #endregion



                #region Update Sold In ExDeclaration
                if (exdecItem.Price + usedPriceOfExdecCode >= exdecEntity.Price)
                {
                    var updateSoldExdec = await _declarationRepository.SoldedDeclaration(exdecEntity.Id);

                    if (!updateSoldExdec)
                    {
                        return(SalesResult.CanNotUpdateSoldExDecInDataBase);
                    }
                }

                #endregion

                totalInserted += exdecItem.Price;
                if (totalInserted == saleDto.SalePrice)
                {
                    return(SalesResult.Success);
                }
            }

            return(SalesResult.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);
        }
예제 #5
0
        public async Task <CurrencySaleDto> GetViewDataFromCurrency(ICurrencySaleRepository saleRepository, ICurrencySalePiDetailRepository salePiDetailRepository, CurrencySale currencySale)
        {
            //var currencySaleItem = (CurrencyType)_currencySale.CurrencyType == CurrencyType.CarrencySales ? await _saleRepository.GetCurrencyByIdIncludesCustomerAndBroker(_currencySale.Id) : await _saleRepository.GetCurrencyByIdIncludesBroker(_currencySale.Id);
            var currencySaleItem = await saleRepository.GetCurrencyByIdIncludesCustomerAndBroker(currencySale.Id);

            var sumProfit = (CurrencyType)currencySale.CurrencyType == CurrencyType.CarrencySales ? await salePiDetailRepository.GetSumProfitLost(currencySale.Id) :0;

            var filterDto = new CurrencySaleDto()
            {
                Id               = currencySale.Id,
                BrokerName       = currencySaleItem.Broker.Name + " (" + currencySaleItem.Broker.Title + ") ",
                CurrSaleDate     = currencySaleItem.SaleDate,
                CustomerName     = currencySaleItem.Customer.Name,
                Price            = currencySale.SalePrice,
                ProfitLossAmount = sumProfit,
                SalePricePerUnit = currencySale.SalePricePerUnit,
                TransferPrice    = currencySale.TransferPrice,
                TransferType     = (CurrencyTransferType)currencySale.TransferType,
                CurrencyType     = (CurrencyType)currencySale.CurrencyType
            };

            return(filterDto);
        }
        public async Task <CurrencySaleDto> GetViewDataFromCurrency(ICurrencySaleRepository saleRepository,
                                                                    ICurrencySalePiDetailRepository salePiDetailRepository, CurrencySale currencySale)
        {
            var filterDto = new CurrencySaleDto();

            var currencySaleItem = await saleRepository.GetCurrencyByIdIncludesBroker(currencySale.Id);

            var sumProfit = await salePiDetailRepository.GetSumProfitLost(currencySale.Id);

            var miscellaneousCustomer = await _customerRepository.GetEntityById(currencySale.CustomerId);

            filterDto = new CurrencySaleDto()
            {
                Id               = currencySale.Id,
                BrokerName       = currencySaleItem.Broker.Name + " (" + currencySaleItem.Broker.Title + ") ",
                CurrSaleDate     = currencySaleItem.SaleDate,
                CustomerName     = miscellaneousCustomer.Name,
                Price            = currencySale.SalePrice,
                ProfitLossAmount = sumProfit,
                SalePricePerUnit = currencySale.SalePricePerUnit,
                TransferPrice    = currencySale.TransferPrice,
                TransferType     = (CurrencyTransferType)currencySale.TransferType,
                CurrencyType     = (CurrencyType)currencySale.CurrencyType
            };
            return(filterDto);
        }
예제 #7
0
 public async Task <CurrencySaleDto> SetCurrency(ISterategyPatternCurrencyType currencyType, ICurrencySaleRepository saleRepository, ICurrencySalePiDetailRepository salePiDetailRepository, CurrencySale currencySale)
 {
     _currencyType = currencyType;
     return(await _currencyType.GetViewDataFromCurrency(saleRepository, salePiDetailRepository, currencySale));
 }