Ejemplo n.º 1
0
        public async Task <Account> BuyStock(Account account, string stock, int shares)
        {
            StockResult stockInfo = await _stockService.GetStock(stock);

            double stockPrice = stockInfo.Price;

            // 先判断余额是否够, 如果不够,直接抛出异常并立即结束方法
            double transactionPrice = stockPrice * shares;

            if (transactionPrice > account.Balance)
            {
                throw new InsufficientFundsException(account.Balance, transactionPrice);
            }

            AssetTransaction transaction = new AssetTransaction()
            {
                Account = account,
                Asset   = new Asset {
                    Symbol = stock, PricePerShare = stockPrice
                },
                DateProcessed = DateTime.Now,
                Shares        = shares,
                IsPurchase    = true,
            };

            account.AssetTransactions.Add(transaction);
            account.Balance -= transactionPrice;
            await _accountService.Update(account.Id, account);

            return(account);
        }
        public async Task <Account> BuyStock(Account buyer, string stock, int shares)
        {
            double stockPrice = await _stockPriceService.GetPrice(stock);

            double transactionAmount = stockPrice * shares;

            if (transactionAmount > buyer.Balance)
            {
                throw new InsufficientFundException(buyer.Balance, transactionAmount);
            }

            AssetTransaction assetTransaction = new AssetTransaction()
            {
                Account = buyer,
                Asset   = new Asset
                {
                    PricePerShare = stockPrice,
                    Symbol        = stock
                },
                DateProcessed = DateTime.Now,
                IsPurchase    = true,
                Shares        = shares
            };

            buyer.AssetTransactions.Add(assetTransaction);
            buyer.Balance -= transactionAmount;

            await _accountService.Update(buyer.Id, buyer);

            return(buyer);
        }
Ejemplo n.º 3
0
        public async Task <Account> StockPurchase(Account Buyer, string Symbol, int Qty)
        {
            double StockPrice = await _SPS.GetPrice(Symbol);

            double TranxCost = StockPrice * Qty;

            if ((TranxCost) > Buyer.Balance)
            {
                throw new InsufficientBalanceX(Buyer.Balance, TranxCost);
            }

            AssetTransaction Tranx = new AssetTransaction()
            {
                WhichAccount = Buyer,
                WhichStock   = new Stock()
                {
                    PricePerShare = StockPrice,
                    Symbol        = Symbol
                },
                DateProcessed = DateTime.Now,
                Shares        = Qty,
                IsPurchase    = true
            };

            Buyer.AssetTransactions.Add(Tranx);
            Buyer.Balance -= TranxCost;

            await _AccS.Update(Buyer.Id, Buyer);

            return(Buyer);
        }
Ejemplo n.º 4
0
        public ActionResult Index(Detail_Master doc)
        {
            DropdownMaster   dm  = new DropdownMaster();
            AssetTransaction asp = new AssetTransaction();
            string           str = doc.User_Name.ToString();

            string[] str1 = str.Split('|');

            doc.User_Name       = str1[0].ToString();
            doc.RoomNo          = str1[1].ToString();
            doc.Flag            = 1;
            db.Entry(doc).State = System.Data.Entity.EntityState.Modified;
            asp.D_ID            = doc.D_ID;
            asp.UserName        = str1[0].ToString();
            asp.Item_Id         = doc.item_ID;
            asp.Date_Modified   = DateTime.Now;
            asp.Roomno          = str1[1].ToString();;

            db.AssetTransactions.Add(asp);

            db.SaveChanges();


            return(RedirectToAction("Index", "Modifier"));
        }
Ejemplo n.º 5
0
 public void AddUpload(LLUUID transactionID, AssetBase asset)
 {
     AssetTransaction upload = new AssetTransaction();
     lock (this.transactions)
     {
         upload.Asset = asset;
         upload.TransactionID = transactionID;
         this.transactions.Add(transactionID, upload);
     }
     if (upload.Asset.Data.Length > 2)
     {
         //is complete
         upload.UploadComplete = true;
         AssetUploadCompletePacket response = new AssetUploadCompletePacket();
         response.AssetBlock.Type = asset.Type;
         response.AssetBlock.Success = true;
         response.AssetBlock.UUID = transactionID.Combine(this.ourClient.SecureSessionID);
         this.ourClient.OutPacket(response);
         m_assetCache.AddAsset(asset);
     }
     else
     {
         upload.UploadComplete = false;
         upload.XferID = Util.GetNextXferID();
         RequestXferPacket xfer = new RequestXferPacket();
         xfer.XferID.ID = upload.XferID;
         xfer.XferID.VFileType = upload.Asset.Type;
         xfer.XferID.VFileID = transactionID.Combine(this.ourClient.SecureSessionID);
         xfer.XferID.FilePath = 0;
         xfer.XferID.Filename = new byte[0];
         this.ourClient.OutPacket(xfer);
     }
 }
Ejemplo n.º 6
0
        public async Task <Account> BuyStock(Account buyer, string symbol, int shares)
        {
            double stockPrice = await stockPriceService.GetPrice(symbol);

            double transactionPrice = stockPrice * shares;

            if (transactionPrice > buyer.Balance)
            {
                throw new InsufficientFundsException(buyer.Balance, transactionPrice);
            }

            AssetTransaction transaction = new AssetTransaction
            {
                Account = buyer,
                Asset   = new Asset
                {
                    PricePerShare = stockPrice,
                    Symbol        = symbol
                },
                DateProcessed = DateTime.Now,
                Shares        = shares,
                IsPurchase    = true
            };

            buyer.AssetTransactions.Add(transaction);
            buyer.Balance -= transactionPrice;

            await dataService.Update(buyer.Id, buyer);

            return(buyer);
        }
Ejemplo n.º 7
0
        public void AddUpload(LLUUID transactionID, AssetBase asset)
        {
            AssetTransaction upload = new AssetTransaction();

            lock (this.transactions)
            {
                upload.Asset         = asset;
                upload.TransactionID = transactionID;
                this.transactions.Add(transactionID, upload);
            }
            if (upload.Asset.Data.Length > 2)
            {
                //is complete
                upload.UploadComplete = true;
                AssetUploadCompletePacket response = new AssetUploadCompletePacket();
                response.AssetBlock.Type    = asset.Type;
                response.AssetBlock.Success = true;
                response.AssetBlock.UUID    = transactionID.Combine(this.ourClient.SecureSessionID);
                this.ourClient.OutPacket(response);
                m_assetCache.AddAsset(asset);
            }
            else
            {
                upload.UploadComplete = false;
                upload.XferID         = Util.GetNextXferID();
                RequestXferPacket xfer = new RequestXferPacket();
                xfer.XferID.ID        = upload.XferID;
                xfer.XferID.VFileType = upload.Asset.Type;
                xfer.XferID.VFileID   = transactionID.Combine(this.ourClient.SecureSessionID);
                xfer.XferID.FilePath  = 0;
                xfer.XferID.Filename  = new byte[0];
                this.ourClient.OutPacket(xfer);
            }
        }
Ejemplo n.º 8
0
        public void CreateBuyTransactions()
        {
            var portfolio = new Portfolio {
                Name = "Test Portfolio"
            };
            var account = new Account
            {
                Name        = "TD TFSA",
                Description = "TD TFSA Account CAD",
                Currency    = "CAD",
            };

            portfolio.Accounts.Add(account);
            this.model.Portfolios.Add(portfolio);
            this.model.SaveChanges();

            var           date     = DateTime.Now;
            const decimal Price    = (decimal)23.02;
            const decimal Quantity = (decimal)1000;
            const decimal Amount   = (Price * Quantity) + (decimal)9.99;

            var cashTransaction = new CashTransaction
            {
                TransactionDate = date,
                Description     = "Buying 1000 x $23.02 MSFT shares.",
                Comment         = "Great deal",
                Amount          = -Amount
            };

            var transaction = new AssetTransaction
            {
                TransactionDate = DateTime.Now,
                TickerSymbol    = "MSFT",
                Description     = "Microsoft Corporation",
                Price           = Price,
                Quantity        = Quantity,
                Commission      = (decimal)9.95,
                Amount          = Amount
            };

            account.CashTransactions.Add(cashTransaction);
            account.AssetTransactions.Add(transaction);

            this.model.SaveChanges();

            using (var vmodel = new IpmModel())
            {
                Assert.IsNotNull(vmodel);
                var pf = this.model.Portfolios.Find(portfolio.PortfolioId);
                Assert.IsNotNull(pf);
                var acc = pf.Accounts.FirstOrDefault(a => a.AccountId == account.AccountId);
                Assert.IsNotNull(acc);
                Assert.IsNotNull(acc.AssetTransactions.FirstOrDefault(t => t.TickerSymbol == "MSFT"));
            }
        }
Ejemplo n.º 9
0
        public async Task <string> GetAllAssets()
        {
            List <AssetBalance> list = new List <AssetBalance>();

            var assets = await client.Asset.ListAssetsAsync();

            foreach (var item in assets.Result)
            {
                AssetBalance balance = new AssetBalance();
                balance.AssetRef = item.AssetRef;
                balance.Name     = item.Name;
                balance.TxID     = item.GenesisTxId;
                balance.Details  = item.Details;

                client.Asset.Subscribe(item.Name, false);
                var transactions = await client.Asset.ListAssetTransactions(item.AssetRef, true);

                foreach (var transaction in transactions.Result)
                {
                    AssetTransaction at = new AssetTransaction();
                    at.Time      = transaction.Time;
                    at.Date      = BlockChainHelper.UnixTimeStampToDateTime(transaction.Time).ToString();
                    at.TxID      = transaction.TxId;
                    at.BlockHash = transaction.BlockHash;
                    if (transaction.Addresses.Count == 1)
                    {
                        KeyValuePair <string, decimal> address = transaction.Addresses.First();

                        at.AddressFrom = "";
                        at.AddressTo   = address.Key;
                        at.Qty         = address.Value;
                    }
                    else
                    {
                        IOrderedEnumerable <KeyValuePair <string, decimal> > oList = transaction.Addresses.OrderBy(e => e.Value);

                        KeyValuePair <string, decimal> addressFrom = oList.First();
                        KeyValuePair <string, decimal> addressTo   = oList.ElementAtOrDefault(1);

                        at.AddressFrom = addressFrom.Key;
                        at.AddressTo   = addressTo.Key;
                        at.Qty         = addressTo.Value;
                    }
                    balance.Transactions.Add(at);
                }

                list.Add(balance);
            }

            return(JsonConvert.SerializeObject(list, Formatting.Indented));
        }
        public static AssetTransaction ToEntity(this AssetTransactionModel model)
        {
            var entity = new AssetTransaction()
            {
                Id               = model.Id,
                AssetId          = model.AssetId,
                BusinessType     = (int)model.BusinessType,
                InUserId         = model.InUserId,
                InUserIsConfirm  = model.InUserIsConfirm,
                OutUserId        = model.OutUserId,
                OutUserIsConfirm = model.OutUserIsConfirm,
                Description      = model.Description
            };

            return(entity);
        }
Ejemplo n.º 11
0
        public AssetBase AddUploadToAssetCache(LLUUID transactionID)
        {
            AssetBase asset = null;

            if (this.transactions.ContainsKey(transactionID))
            {
                AssetTransaction trans = this.transactions[transactionID];
                if (trans.UploadComplete)
                {
                    m_assetCache.AddAsset(trans.Asset);
                    asset = trans.Asset;
                }
            }

            return(asset);
        }
        public void SetUp()
        {
            // setup fake model
            _dbAssetType = new AssetType {
                Id = 1, Name = "a", IsActive = true
            };
            _dbAsset = new Asset {
                Id = 2, AssetTypeId = _dbAssetType.Id, Name = "b", IsActive = true
            };
            _dbSettingType = new SettingType {
                Id = 3, Name = "c", IsActive = true
            };
            _dbAssetSetting = new AssetSetting {
                Id = 4, AssetId = _dbAsset.Id, SettingTypeId = _dbSettingType.Id, Value = "d", IsActive = true
            };
            _dbTransactionCategory = new TransactionCategory {
                Id = 5, Name = "e", IsActive = true
            };
            _dbTransactionDescription = new TransactionDescription {
                Id = 6, Name = "f", IsActive = true
            };
            _dbTransactionType = new TransactionType {
                Id = 7, Name = "g", IsActive = true
            };
            _dbAssetTransaction = new AssetTransaction
            {
                Id = 8,
                TransactionCategoryId    = _dbTransactionCategory.Id,
                TransactionDescriptionId = _dbTransactionDescription.Id,
                TransactionTypeId        = _dbTransactionType.Id,
                CheckNumber = "123",
                DueDate     = new DateTime(1234, 5, 6),
                ClearDate   = new DateTime(1234, 7, 8),
                Amount      = 123.45M,
                Note        = "abcdef",
                IsActive    = true
            };

            // setup DbContext
            Setup_FakeDbContext();

            // set up repository
            _repository = new AssetTransactionRepository(_fakeDbContext);
        }
Ejemplo n.º 13
0
        public void CreateTradeTransactions()
        {
            var portfolio = new Portfolio {
                Name = "Test Portfolio"
            };
            var account = new Account
            {
                Name        = "TD TFSA",
                Description = "TD TFSA Account CAD",
                Currency    = "CAD",
            };

            portfolio.Accounts.Add(account);
            this.model.Portfolios.Add(portfolio);
            this.model.SaveChanges();

            var transaction = new AssetTransaction
            {
                TransactionDate = DateTime.Now,
                TickerSymbol    = "MSFT",
                Description     = "Microsoft Corporation",
                Price           = (decimal)23.02,
                Quantity        = 1000,
                Commission      = (decimal)9.95,
                Amount          = (decimal)(23.02 * 1000)
            };

            account.AssetTransactions.Add(transaction);

            this.model.SaveChanges();

            using (new IpmModel())
            {
                Assert.IsNotNull(this.model);
                var pf = this.model.Portfolios.Find(portfolio.PortfolioId);
                Assert.IsNotNull(pf);
                var acc = pf.Accounts.FirstOrDefault(a => a.AccountId == account.AccountId);
                Assert.IsNotNull(acc);
                Assert.IsNotNull(acc.AssetTransactions.FirstOrDefault(t => t.TickerSymbol == "MSFT"));
            }
        }
Ejemplo n.º 14
0
 public void CreateInventoryItem(CreateInventoryItemPacket packet)
 {
     if (this.transactions.ContainsKey(packet.InventoryBlock.TransactionID))
     {
         AssetTransaction trans = this.transactions[packet.InventoryBlock.TransactionID];
         trans.Asset.Description = Util.FieldToString(packet.InventoryBlock.Description);
         trans.Asset.Name        = Util.FieldToString(packet.InventoryBlock.Name);
         trans.Asset.Type        = packet.InventoryBlock.Type;
         trans.Asset.InvType     = packet.InventoryBlock.InvType;
         if (trans.UploadComplete)
         {
             //already complete so we can add it to the inventory
             //m_assetCache.AddAsset(trans.Asset);
             m_inventoryCache.AddNewInventoryItem(this.ourClient, packet.InventoryBlock.FolderID, trans.Asset);
         }
         else
         {
             trans.AddToInventory = true;
             trans.InventFolder   = packet.InventoryBlock.FolderID;
         }
     }
 }
        // private


        private void Setup_FakeDb()
        {
            _dbAssetType = new AssetType {
                Id = 1, Name = "a", IsActive = true
            };
            _dbAsset = new Asset {
                Id = 2, AssetTypeId = _dbAssetType.Id, Name = "b", IsActive = true
            };
            _dbSettingType = new SettingType {
                Id = 3, Name = "c", IsActive = true
            };
            _dbAssetSetting = new AssetSetting {
                Id = 4, AssetId = _dbAsset.Id, SettingTypeId = _dbSettingType.Id, Value = "d", IsActive = true
            };
            _dbTransactionCategory = new TransactionCategory {
                Id = 5, Name = "e", IsActive = true
            };
            _dbTransactionDescription = new TransactionDescription {
                Id = 6, Name = "f", IsActive = true
            };
            _dbTransactionType = new TransactionType {
                Id = 7, Name = "g", IsActive = true
            };
            _dbAssetTransaction = new AssetTransaction
            {
                Id = 8,
                TransactionCategoryId    = _dbTransactionCategory.Id,
                TransactionDescriptionId = _dbTransactionDescription.Id,
                TransactionTypeId        = _dbTransactionType.Id,
                CheckNumber = "123",
                DueDate     = new DateTime(1234, 5, 6),
                ClearDate   = new DateTime(1234, 7, 8),
                Amount      = 123.45M,
                Note        = "abcdef",
                IsActive    = true
            };
        }
        public static AssetTransactionModel ToModel(this AssetTransaction entity)
        {
            var model = new AssetTransactionModel()
            {
                Id               = entity.Id,
                AssetId          = entity.AssetId,
                InUserId         = entity.InUserId,
                InUserIsConfirm  = entity.InUserIsConfirm,
                OutUserId        = entity.OutUserId,
                OutUserIsConfirm = entity.OutUserIsConfirm,
                Description      = entity.Description
            };

            if (entity.BusinessType.HasValue)
            {
                model.BusinessType = (BusinessType)entity.BusinessType.Value;
            }
            if (entity.Status.HasValue)
            {
                model.Status = (AssetTransactionStatus)entity.Status.Value;
            }

            return(model);
        }
Ejemplo n.º 17
0
        public void SetUp()
        {
            // setup db models
            _dbAssetType = new AssetType {
                Id = 1, Name = "a", IsActive = true
            };
            _dbAsset = new Asset
            {
                Id          = 2,
                AssetTypeId = _dbAssetType.Id,
                AssetType   = _dbAssetType, // setup include
                Name        = "b",
                IsActive    = true
            };
            _dbSettingType = new SettingType {
                Id = 3, Name = "c", IsActive = true
            };
            _dbAssetSetting = new AssetSetting
            {
                Id            = 4,
                AssetId       = _dbAsset.Id,
                Asset         = _dbAsset,       // setup include
                SettingTypeId = _dbSettingType.Id,
                SettingType   = _dbSettingType, // setup include
                Value         = "d",
                IsActive      = true
            };
            _dbTransactionType = new TransactionType {
                Id = 5, Name = "e", IsActive = true
            };
            _dbTransactionCategory = new TransactionCategory {
                Id = 6, Name = "f", IsActive = true
            };
            _dbTransactionDescription = new TransactionDescription {
                Id = 7, Name = "g", IsActive = true
            };
            _dbAssetTransaction = new AssetTransaction
            {
                Id                       = 8,
                AssetId                  = _dbAsset.Id,
                Asset                    = _dbAsset,                  // setup include
                TransactionTypeId        = _dbTransactionType.Id,
                TransactionType          = _dbTransactionType,        // setup include
                TransactionCategoryId    = _dbTransactionCategory.Id,
                TransactionCategory      = _dbTransactionCategory,    // setup include
                TransactionDescriptionId = _dbTransactionDescription.Id,
                TransactionDescription   = _dbTransactionDescription, // setup include
                DueDate                  = new DateTime(1234, 5, 6),
                ClearDate                = new DateTime(1234, 7, 8),
                Amount                   = 1.23M,
                Note                     = "abc",
                IsActive                 = true
            };

            // setup DbContext
            Setup_FakeDbContext();

            // setup uow
            _fakeUnitOfWork = new UnitOfWork(_fakeDbContext);

            // setup service
            _service = new AccountTransactionService(_fakeUnitOfWork);
        }