/// <summary> /// Saves the transaction (purchase or sale) /// </summary> /// <param name="type">The transaction type : s(ales), c(harge), t(akeoff), p(urchase)</param> /// <param name="stock">the stock data</param> public void saveTransaction(TransactionItem.TransactionTypeEnum type, StockListitem stock) { BankAccountModel bankModel = new BankAccountModel(); var bank = bankModel.getBank(); var ttype = (type == TransactionItem.TransactionTypeEnum.Purchase) ? 'p' : 's'; var totalSum = (decimal)stock.PricePurchase * (decimal)stock.Quantity; if (ConnectionState == true && bank != null) { if (totalSum <= bank.AccountBalance) { decimal change = 0; switch (type) { case TransactionItem.TransactionTypeEnum.Sales: change += totalSum; break; case TransactionItem.TransactionTypeEnum.Purchase: change -= totalSum; break; } var bankQuery = (from bk in db.BankAccount where bk.BankAccountID == bank.BankAccountID select bk).First(); bankQuery.AccountBalance += change; db.SubmitChanges(); db.Transactions.InsertOnSubmit(new Transactions { Description = stock.Quantity + " - " + stock.name, BankAccountID = bank.BankAccountID, Created = DateTime.Now, Amount = totalSum, TransactionType = ttype, }); db.SubmitChanges(); } } }
/// <summary> /// Sales the stock and saves the transaction /// </summary> public void removeStock(StockListitem stock, decimal quantity) { if (ConnectionState == true) { var query = (from ds in db.StocksMap where ds.StocksMapID == stock.StocksMapID select ds).FirstOrDefault(); if (query != null) { if (quantity < stock.Quantity) { query.Quantity -= (int)quantity; } else if (quantity == stock.Quantity) { db.UserLines.DeleteAllOnSubmit(query.UserLines); db.StocksMap.DeleteOnSubmit(query); } db.SubmitChanges(); BankAccountModel bankModel = new BankAccountModel(); var bank = bankModel.getBank(); var valueBought = quantity * query.PurchasePrice; var valueSold = quantity * Convert.ToDecimal(stock.PriceCurrent); db.Transactions.InsertOnSubmit(new Transactions { Description = quantity + " - " + stock.name, BankAccountID = bank.BankAccountID, Created = DateTime.Now, Amount = valueSold, TransactionType = 's', }); var capitalGainTax = SettingsModel.getInstance().getCapitalGainTax(); if (capitalGainTax > 0) { decimal valueTax = 0; var taxType = ""; char taxTransType = 'x'; if (valueSold > valueBought) // Profit { valueTax = Convert.ToDecimal(valueSold - valueBought) * (capitalGainTax / 100); taxType = "Kapitalerstragssteuer nach Verkauf - " + stock.name; } else if (valueSold < valueBought) // Loss { valueTax = Convert.ToDecimal(valueBought - valueSold) * (capitalGainTax / 100); taxType = "Steuergutschrift durch Verkauf - " + stock.name; taxTransType = 'y'; } db.Transactions.InsertOnSubmit(new Transactions { Description = taxType, BankAccountID = bank.BankAccountID, Created = DateTime.Now, Amount = valueTax, TransactionType = taxTransType, }); } db.SubmitChanges(); } } }