<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace AccountServer {
	public class Investments : BankingAccounting {
		public Investments() {
			Menu = new MenuOption[] {
				new MenuOption("Listing", "/investments/default.html"),
				new MenuOption("Securities", "/investments/securities.html"),
				new MenuOption("New Account", "/investments/detail.html?id=0")
			};
		}

		/// <summary>
		/// List all security accounts, with their cash balance and current security value
		/// </summary>
		public object DefaultListing() {
			return Database.Query(@"SELECT Account.*, Amount AS CashBalance, Value
FROM Account
LEFT JOIN (SELECT AccountId, SUM(Amount) AS Amount FROM Journal GROUP BY AccountId) AS Balances ON Balances.AccountId = idAccount
JOIN AccountType ON idAccountType = AccountTypeId
LEFT JOIN (" + AccountValue(Utils.Today) + @") AS AccountValues ON AccountValues.ParentAccountId = Balances.AccountId
WHERE AccountTypeId = " + (int)AcctType.Investment + @"
GROUP BY idAccount ORDER BY AccountName");
		}

		/// <summary>
		/// Retrieve a security account for editing
		/// </summary>
		public void Detail(int id) {
			InvestmentDetail record = Database.QueryOne<InvestmentDetail>("Account.*, AcctType, SUM(Amount) AS CashBalance",
				"WHERE idAccount = " + id,
				"Account", "Journal");
			record.CurrentBalance = record.CashBalance - Database.QueryOne("SELECT SUM(Amount) AS Future FROM Journal JOIN Document ON idDocument = DocumentId WHERE AccountId = "
				+ id + " AND DocumentDate > " + Database.Quote(Utils.Today)).AsDecimal("Future");
			record.Value = Database.QueryOne("SELECT Value FROM (" + AccountValue(Utils.Today) + ") AS V WHERE ParentAccountid = " + id).AsDecimal("Value");
			if (record.Id == null) {
				record.AccountTypeId = (int)AcctType.Investment;
			} else {
				checkAcctType(record.AccountTypeId, AcctType.Investment);
				Title += " - " + record.AccountName;
			}
			Record = record;
		}

		/// <summary>
		/// List all transactions for account
		/// </summary>
		public IEnumerable<JObject> DetailListing(int id) {
			return detailsWithBalance(id).Reverse();
		}

		/// <summary>
		/// Update account info after editing
		/// </summary>
		public AjaxReturn DetailPost(Account json) {
			checkAcctType(json.AccountTypeId, AcctType.Investment);
			return PostRecord(json, true);
		}

		/// <summary>
		/// Portfolio header - same as Detail header
		/// </summary>
		public void Portfolio(int id) {
			Detail(id);
		}

		/// <summary>
		/// List all securities for account, with quantity and current value
		/// </summary>
		public IEnumerable<JObject> PortfolioListing(int id) {
			return (from p in Database.Query("SELECT SecurityName, AccountName, SV.* FROM ("
				+ SecurityValues(Utils.Today) + @") AS SV
JOIN Account ON idAccount = SV.AccountId
JOIN Security ON idSecurity = SecurityId
WHERE ParentAccountid = " + id)
						  let cb = SecurityCost(p.AsInt("AccountId"))
						  select new JObject(p).AddRange(
							  "CostBasis", cb,
							  "Change", cb == 0 ? 0 : 100 * (p.AsDecimal("Value") - cb) / cb
							  ));
		}

		/// <summary>
		/// Get a Buy or Sell document for editing
		/// </summary>
		public void Document(int id, DocType type) {
			Title = Title.Replace("Document", type.UnCamel());
			InvestmentDocument header = getDocument<InvestmentDocument>(id);
			if (header.idDocument == null) {
				header.DocumentTypeId = (int)type;
				header.DocType = type.UnCamel();
				header.DocumentDate = Utils.Today;
				header.DocumentName = "";
				if (GetParameters["acct"].IsInteger()) {<commit_msg>Fixed bug when using MySql to view securities portfolio (2 DataReaders going at once).<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace AccountServer {
	public class Investments : BankingAccounting {
		public Investments() {
			Menu = new MenuOption[] {
				new MenuOption("Listing", "/investments/default.html"),
				new MenuOption("Securities", "/investments/securities.html"),
				new MenuOption("New Account", "/investments/detail.html?id=0")
			};
		}

		/// <summary>
		/// List all security accounts, with their cash balance and current security value
		/// </summary>
		public object DefaultListing() {
			return Database.Query(@"SELECT Account.*, Amount AS CashBalance, Value
FROM Account
LEFT JOIN (SELECT AccountId, SUM(Amount) AS Amount FROM Journal GROUP BY AccountId) AS Balances ON Balances.AccountId = idAccount
JOIN AccountType ON idAccountType = AccountTypeId
LEFT JOIN (" + AccountValue(Utils.Today) + @") AS AccountValues ON AccountValues.ParentAccountId = Balances.AccountId
WHERE AccountTypeId = " + (int)AcctType.Investment + @"
GROUP BY idAccount ORDER BY AccountName");
		}

		/// <summary>
		/// Retrieve a security account for editing
		/// </summary>
		public void Detail(int id) {
			InvestmentDetail record = Database.QueryOne<InvestmentDetail>("Account.*, AcctType, SUM(Amount) AS CashBalance",
				"WHERE idAccount = " + id,
				"Account", "Journal");
			record.CurrentBalance = record.CashBalance - Database.QueryOne("SELECT SUM(Amount) AS Future FROM Journal JOIN Document ON idDocument = DocumentId WHERE AccountId = "
				+ id + " AND DocumentDate > " + Database.Quote(Utils.Today)).AsDecimal("Future");
			record.Value = Database.QueryOne("SELECT Value FROM (" + AccountValue(Utils.Today) + ") AS V WHERE ParentAccountid = " + id).AsDecimal("Value");
			if (record.Id == null) {
				record.AccountTypeId = (int)AcctType.Investment;
			} else {
				checkAcctType(record.AccountTypeId, AcctType.Investment);
				Title += " - " + record.AccountName;
			}
			Record = record;
		}

		/// <summary>
		/// List all transactions for account
		/// </summary>
		public IEnumerable<JObject> DetailListing(int id) {
			return detailsWithBalance(id).Reverse();
		}

		/// <summary>
		/// Update account info after editing
		/// </summary>
		public AjaxReturn DetailPost(Account json) {
			checkAcctType(json.AccountTypeId, AcctType.Investment);
			return PostRecord(json, true);
		}

		/// <summary>
		/// Portfolio header - same as Detail header
		/// </summary>
		public void Portfolio(int id) {
			Detail(id);
		}

		/// <summary>
		/// List all securities for account, with quantity and current value
		/// </summary>
		public IEnumerable<JObject> PortfolioListing(int id) {
			return (from p in Database.Query("SELECT SecurityName, AccountName, SV.* FROM ("
				+ SecurityValues(Utils.Today) + @") AS SV
JOIN Account ON idAccount = SV.AccountId
JOIN Security ON idSecurity = SecurityId
WHERE ParentAccountid = " + id).ToList()
						  let cb = SecurityCost(p.AsInt("AccountId"))
						  select new JObject(p).AddRange(
							  "CostBasis", cb,
							  "Change", cb == 0 ? 0 : 100 * (p.AsDecimal("Value") - cb) / cb
							  ));
		}

		/// <summary>
		/// Get a Buy or Sell document for editing
		/// </summary>
		public void Document(int id, DocType type) {
			Title = Title.Replace("Document", type.UnCamel());
			InvestmentDocument header = getDocument<InvestmentDocument>(id);
			if (header.idDocument == null) {
				header.DocumentTypeId = (int)type;
				header.DocType = type.UnCamel();
				header.DocumentDate = Utils.Today;
				header.DocumentName = "";
				if (GetParameters["acct"].IsInteger()) {
{'fraction_non_alphanumeric': 0.12979744136460555, 'fraction_numerical': 0.001732409381663113, 'mean_word_length': 3.6964956195244056, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '861816', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2189, 'n_words': 609}