Exemplo n.º 1
0
        public bool ExchangeForBranch(int sellingCoinId, int purchasingCoinId, decimal firstAmount)
        {
            try
            {
                int treasuryId       = _appSession.GetCurrentTreasuryId();
                var secondCoinAmount = CalcForFirstCoin(sellingCoinId, purchasingCoinId, firstAmount);

                var mainCoin            = _unitOfWork.GenericRepository <BranchCash>().FindBy(c => c.IsMainCoin == true).FirstOrDefault();
                var firstCoinExchange   = _unitOfWork.GenericRepository <BranchCash>().FindBy(c => c.CoinId == sellingCoinId).FirstOrDefault();
                var secoundCoinExchange = _unitOfWork.GenericRepository <BranchCash>().FindBy(c => c.CoinId == purchasingCoinId).FirstOrDefault();


                _unitOfWork.CreateTransaction();


                var exchange = new Exchange()
                {
                    BranchId           = BranchHelper.Id,
                    FirstCoinId        = sellingCoinId,
                    SecoundCoinId      = purchasingCoinId,
                    AmountOfFirstCoin  = firstAmount,
                    AmoutOfSecoundCoin = secondCoinAmount,
                    MainCoinId         = mainCoin.CoinId,
                    FirstCoinExchangePriceWithMainCoin     = firstCoinExchange.ExchangePrice,
                    FirstCoinSellingPriceWithMainCoin      = firstCoinExchange.SellingPrice,
                    FirstCoinPurchasingPriceWithMainCoin   = firstCoinExchange.PurchasingPrice,
                    SecoundCoinExchangePriceWithMainCoin   = secoundCoinExchange.ExchangePrice,
                    SecoundCoinSellingPricWithMainCoin     = secoundCoinExchange.SellingPrice,
                    SecoundCoinPurchasingPriceWithMainCoin = secoundCoinExchange.PurchasingPrice
                };
                _unitOfWork.GenericRepository <Exchange>().Insert(exchange);


                var branchCaches          = _unitOfWork.GenericRepository <BranchCash>();
                var branchCashSellingCoin = branchCaches.FindBy(c => c.CoinId == sellingCoinId).FirstOrDefault();
                branchCashSellingCoin.Total -= firstAmount;
                _unitOfWork.GenericRepository <BranchCash>().Update(branchCashSellingCoin);


                var branchCashpurhesCoin = branchCaches.FindBy(c => c.CoinId == purchasingCoinId).FirstOrDefault();
                branchCashpurhesCoin.Total += secondCoinAmount;
                _unitOfWork.GenericRepository <BranchCash>().Update(branchCashpurhesCoin);



                var moneyAction = new MoneyAction()
                {
                    ExchangeId = exchange.Id
                };
                _unitOfWork.GenericRepository <MoneyAction>().Insert(moneyAction);
                var branchCashFlowForFirstCoin = new BranchCashFlow()
                {
                    BranchId    = BranchHelper.Id,
                    MoenyAction = moneyAction,
                    CoinId      = sellingCoinId,
                    Total       = branchCashSellingCoin.Total,
                    Amount      = -firstAmount,
                    TreasuryId  = treasuryId,
                };
                _unitOfWork.GenericRepository <BranchCashFlow>().Insert(branchCashFlowForFirstCoin);
                var sellingCoinTreasuryCash = _unitOfWork.GenericRepository <TreasuryCash>().FindBy(C => C.TreasuryId == treasuryId && C.CoinId == sellingCoinId).FirstOrDefault();
                sellingCoinTreasuryCash.Total -= firstAmount;
                _unitOfWork.GenericRepository <TreasuryCash>().Update(sellingCoinTreasuryCash);
                TreasuryMoneyAction sellingCoinTreasuryMoeyAction = new TreasuryMoneyAction()
                {
                    Amount         = -firstAmount,
                    CoinId         = sellingCoinId,
                    TreasuryId     = treasuryId,
                    Total          = sellingCoinTreasuryCash.Total,
                    BranchCashFlow = branchCashFlowForFirstCoin,
                };
                _unitOfWork.GenericRepository <TreasuryMoneyAction>().Insert(sellingCoinTreasuryMoeyAction);
                var mainTreasuryId = _appSession.GetMainTreasury();
                if (mainTreasuryId != treasuryId)
                {
                    var mainTruseryMoneyAction = new TreasuryMoneyAction()
                    {
                        TreasuryId     = mainTreasuryId,
                        Amount         = -firstAmount,
                        CoinId         = sellingCoinId,
                        BranchCashFlow = branchCashFlowForFirstCoin,
                        CreatedBy      = _appSession.GetUserName()
                    };

                    _unitOfWork.GenericRepository <TreasuryMoneyAction>().Insert(mainTruseryMoneyAction);
                }


                var branChCashFlowForSecoundCoin = new BranchCashFlow()
                {
                    BranchId     = BranchHelper.Id,
                    CoinId       = purchasingCoinId,
                    MonyActionId = moneyAction.Id,
                    Total        = branchCashpurhesCoin.Total,
                    Amount       = secondCoinAmount,
                    TreasuryId   = treasuryId,
                };
                _unitOfWork.GenericRepository <BranchCashFlow>().Insert(branChCashFlowForSecoundCoin);

                var secoundCoinTreasuryCash = _unitOfWork.GenericRepository <TreasuryCash>().FindBy(C => C.TreasuryId == treasuryId && C.CoinId == sellingCoinId).FirstOrDefault();
                secoundCoinTreasuryCash.Total += secondCoinAmount;
                _unitOfWork.GenericRepository <TreasuryCash>().Update(secoundCoinTreasuryCash);
                TreasuryMoneyAction secoundCoinTreasuryMoeyAction = new TreasuryMoneyAction()
                {
                    Amount         = +secondCoinAmount,
                    CoinId         = purchasingCoinId,
                    TreasuryId     = treasuryId,
                    Total          = secoundCoinTreasuryCash.Total,
                    BranchCashFlow = branChCashFlowForSecoundCoin,
                };
                _unitOfWork.GenericRepository <TreasuryMoneyAction>().Insert(secoundCoinTreasuryMoeyAction);

                if (mainTreasuryId != treasuryId)
                {
                    var mainTruseryMoneyActionSecoundCoin = new TreasuryMoneyAction()
                    {
                        TreasuryId     = mainTreasuryId,
                        Amount         = +secondCoinAmount,
                        BranchCashFlow = branChCashFlowForSecoundCoin,
                        CoinId         = purchasingCoinId,
                        CreatedBy      = _appSession.GetUserName()
                    };

                    _unitOfWork.GenericRepository <TreasuryMoneyAction>().Insert(mainTruseryMoneyActionSecoundCoin);
                }
                _unitOfWork.Save();
                _unitOfWork.Commit();
                return(true);
            }
            catch (Exception ex)
            {
                _unitOfWork.Rollback();
                return(false);
            }
        }
        public TransactionDto DileverTransaction(int transactionId)
        {
            TransactionDto transactionDto = null;

            try
            {
                _unitOfWork.CreateTransaction();
                var transaction    = _unitOfWork.GenericRepository <Transaction>().GetById(transactionId);
                int treasuryId     = _appSession.GetCurrentTreasuryId();
                var mainTreasuryId = _appSession.GetMainTreasury();
                if (transaction != null)
                {
                    transaction.Deliverd   = true;
                    transaction.ModifiedBy = _appSession.GetUserName();
                    _unitOfWork.GenericRepository <Transaction>().Update(transaction);

                    var branchCash = _unitOfWork.GenericRepository <BranchCash>().FindBy(x => x.CoinId == transaction.CoinId).FirstOrDefault();
                    if (branchCash != null)
                    {
                        branchCash.Total -= transaction.Amount;
                        _unitOfWork.GenericRepository <BranchCash>().Update(branchCash);
                    }

                    var treasuryCash = _unitOfWork.GenericRepository <TreasuryCash>()
                                       .FindBy(x => x.CoinId == transaction.CoinId && x.TreasuryId == treasuryId).FirstOrDefault();
                    if (treasuryCash != null)
                    {
                        treasuryCash.Total -= transaction.Amount;
                        _unitOfWork.GenericRepository <TreasuryCash>().Update(treasuryCash);
                    }

                    int moneyActinId = transaction.MoneyActionId();

                    var branchCashFLow = new BranchCashFlow()
                    {
                        Total        = branchCash.Total,
                        Amount       = -transaction.Amount,
                        MonyActionId = moneyActinId,
                        BranchId     = BranchHelper.Id,
                        CoinId       = transaction.CoinId,
                        TreasuryId   = _appSession.GetCurrentTreasuryId(),
                        CreatedBy    = _appSession.GetUserName()
                    };
                    _unitOfWork.GenericRepository <BranchCashFlow>().Insert(branchCashFLow);

                    var treuseryMoenyAction = new TreasuryMoneyAction()
                    {
                        Amount         = -transaction.Amount,
                        Total          = treasuryCash.Total,
                        CoinId         = transaction.CoinId,
                        TreasuryId     = _appSession.GetCurrentTreasuryId(),
                        BranchCashFlow = branchCashFLow,
                        CreatedBy      = _appSession.GetUserName()
                    };
                    _unitOfWork.GenericRepository <TreasuryMoneyAction>().Insert(treuseryMoenyAction);

                    if (mainTreasuryId != treasuryId)
                    {
                        var mainTruseryMoneyAction = new TreasuryMoneyAction()
                        {
                            Amount         = -transaction.Amount,
                            TreasuryId     = mainTreasuryId,
                            CoinId         = transaction.CoinId,
                            BranchCashFlow = branchCashFLow
                        };

                        _unitOfWork.GenericRepository <TreasuryMoneyAction>().Insert(mainTruseryMoneyAction);
                    }
                    _unitOfWork.Save();
                    _unitOfWork.Commit();

                    transactionDto = Mapper.Map <Transaction, TransactionDto>(transaction);
                }
            }
            catch (Exception ex)
            {
                Tracing.SaveException(ex);
                _unitOfWork.Rollback();
            }
            return(transactionDto);
        }