Files
using.cash/Database/Database-Expenses.cs
T
2017-12-08 14:43:02 +01:00

475 lines
18 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Swarmops.Basic.Types;
using Swarmops.Basic.Types.Financial;
using Swarmops.Common.Enums;
namespace Swarmops.Database
{
public partial class SwarmDb
{
private string expenseClaimFieldSequence =
" ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + // 0-4
" Validated,Claimed,OrganizationId,GeographyId,BudgetId," + // 5-9
" ExpenseDate,Description,PreApprovedAmount,AmountCents,Repaid," + // 10-14
" KeepSeparate,VatCents,OrganizationSequenceId " + // 15-17
" FROM ExpenseClaims";
public BasicExpenseClaim GetExpenseClaim (int expenseClaimId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand (
"SELECT " + this.expenseClaimFieldSequence + " WHERE ExpenseClaimId=" + expenseClaimId,
connection);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return ReadExpenseFromDataReader (reader);
}
throw new ArgumentException ("Unknown ExpenseClaim Id");
}
}
}
public BasicExpenseClaim[] GetExpenseClaims (params object[] conditions)
{
List<BasicExpenseClaim> result = new List<BasicExpenseClaim>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand ("SELECT " + this.expenseClaimFieldSequence + ConstructWhereClause ("ExpenseClaims", conditions),
connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add (ReadExpenseFromDataReader (reader));
}
return result.ToArray();
}
}
}
public BasicExpenseClaim[] GetExpenseClaimsByClaimer (int claimingPersonId)
{
List<BasicExpenseClaim> result = new List<BasicExpenseClaim>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand ("SELECT " + this.expenseClaimFieldSequence + " WHERE ClaimingPersonId=" + claimingPersonId,
connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add (ReadExpenseFromDataReader (reader));
}
return result.ToArray();
}
}
}
public BasicExpenseClaim[] GetExpenseClaimsByClaimerAndOrganization (int claimingPersonId, int organizationId)
{
List<BasicExpenseClaim> result = new List<BasicExpenseClaim>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand ("SELECT " + this.expenseClaimFieldSequence + " WHERE ClaimingPersonId=" + claimingPersonId +
" AND OrganizationId=" + organizationId,
connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add (ReadExpenseFromDataReader (reader));
}
return result.ToArray();
}
}
}
public BasicExpenseClaim[] GetExpenseClaimsByBudgetAndYear (int budgetId, int budgetYear)
{
List<BasicExpenseClaim> result = new List<BasicExpenseClaim>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand ("SELECT " + this.expenseClaimFieldSequence + " WHERE BudgetId=" + budgetId + " AND BudgetYear=" + budgetYear,
connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add (ReadExpenseFromDataReader (reader));
}
return result.ToArray();
}
}
}
public BasicExpenseClaim[] GetExpenseClaimsByOrganization (int organizationId)
{
List<BasicExpenseClaim> result = new List<BasicExpenseClaim>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand ("SELECT " + this.expenseClaimFieldSequence + " WHERE OrganizationId=" + organizationId,
connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add (ReadExpenseFromDataReader (reader));
}
return result.ToArray();
}
}
}
private BasicExpenseClaim ReadExpenseFromDataReader (DbDataReader reader)
{
int expenseClaimId = reader.GetInt32 (0);
int claimingPersonId = reader.GetInt32 (1);
DateTime createdDateTime = reader.GetDateTime (2);
bool open = reader.GetBoolean (3);
bool attested = reader.GetBoolean (4);
bool documented = reader.GetBoolean (5);
bool claimed = reader.GetBoolean (6);
int organizationId = reader.GetInt32 (7);
int geographyId = reader.GetInt32 (8); // obsolete field
int budgetId = reader.GetInt32 (9);
DateTime expenseDate = reader.GetDateTime (10);
string description = reader.GetString (11);
double preApprovedAmount = reader.GetDouble (12);
Int64 amountCents = reader.GetInt64 (13);
bool repaid = reader.GetBoolean (14);
bool keepSeparate = reader.GetBoolean (15);
Int64 vatCents = reader.GetInt64(16);
int organizationSequenceId = reader.GetInt32(17);
return new BasicExpenseClaim (expenseClaimId, claimingPersonId, createdDateTime,
open, attested, documented, claimed, organizationId, organizationSequenceId, geographyId,
budgetId, expenseDate, description, preApprovedAmount,
amountCents, vatCents, repaid, keepSeparate);
}
public int CreateExpenseClaim (int claimingPersonId, int organizationId, int budgetId,
DateTime expenseDate, string description, Int64 amountCents)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("CreateExpenseClaim", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "claimingPersonId", claimingPersonId);
AddParameterWithName (command, "createdDateTime", DateTime.Now);
AddParameterWithName (command, "organizationId", organizationId);
AddParameterWithName (command, "budgetId", budgetId);
AddParameterWithName (command, "expenseDate", expenseDate);
AddParameterWithName (command, "description", description);
AddParameterWithName (command, "amountCents", amountCents);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimDescription(int expenseClaimId, string description)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetExpenseClaimDescription", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "expenseClaimId", expenseClaimId);
AddParameterWithName(command, "description", description);
return Convert.ToInt32(command.ExecuteScalar());
}
}
public void SetExpenseClaimVatCents(int expenseClaimId, Int64 vatCents)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetExpenseClaimVatCents", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "expenseClaimId", expenseClaimId);
AddParameterWithName(command, "vatCents", vatCents);
command.ExecuteNonQuery();
}
}
public int SetExpenseClaimSequence(int expenseClaimId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetExpenseClaimOrganizationSequenceId", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "expenseClaimId", expenseClaimId);
return Convert.ToInt32(command.ExecuteScalar());
}
}
public int SetExpenseClaimBudget (int expenseClaimId, int budgetId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimBudget", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "budgetId", budgetId);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimBudgetYear (int expenseClaimId, int budgetYear)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimBudgetYear", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "budgetYear", budgetYear);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimDate (int expenseClaimId, DateTime expenseDate)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimDate", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "expenseDate", expenseDate);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimAmount (int expenseClaimId, double amount)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimAmount", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "amount", amount);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimAmount (int expenseClaimId, Int64 amountCents)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimAmountPrecise", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "amountCents", amountCents);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimAttested (int expenseClaimId, bool attested)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimAttested", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "attested", attested);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimValidated (int expenseClaimId, bool validated)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimValidated", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "validated", validated);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimKeepSeparate (int expenseClaimId, bool keepSeparate)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimKeepSeparate", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "keepSeparate", keepSeparate);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimClaimed (int expenseClaimId, bool claimed)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimClaimed", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "claimed", claimed);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimOpen (int expenseClaimId, bool open)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimOpen", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "open", open);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int SetExpenseClaimRepaid (int expenseClaimId, bool repaid)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("SetExpenseClaimRepaid", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "expenseClaimId", expenseClaimId);
AddParameterWithName (command, "repaid", repaid);
return Convert.ToInt32 (command.ExecuteScalar());
}
}
public int CreateExpenseEvent (int expenseId, ExpenseEventType eventType, int personId)
{
throw new NotImplementedException ("Not migrated to version 4");
/*
// WARNING: NOT CONVERTED TO V4 YET
using (DbConnection connection = GetSqlServerDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("CreateExpenseEvent", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "expenseId", expenseId);
AddParameterWithName(command, "dateTime", DateTime.Now);
AddParameterWithName(command, "eventPersonId", personId);
AddParameterWithName(command, "eventType", eventType.ToString());
return Convert.ToInt32(command.ExecuteScalar());
}*/
}
}
}