From e64a077a105da681d619c18c11aa79791b65895e Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 8 Dec 2012 12:35:35 +0000 Subject: [PATCH] Testing P&L statement on live system --- Basic/Basic.csproj | 1 + Basic/Enums/ObjectOptionalDataType.cs | 10 +- Basic/Types/Financial/BasicCashAdvance.cs | 60 ++ Database/Database-CashAdvances.cs | 523 ++++++++++++++++++ Database/Database.csproj | 1 + Logic/Financial/CashAdvance.cs | 65 +++ Logic/Logic.csproj | 1 + Logic/Structure/Organization.cs | 36 +- Site5/App_GlobalResources/Menu5.resx | 4 +- Site5/App_GlobalResources/Menu5.sv-SE.resx | 2 +- Site5/App_GlobalResources/Pages.Finance.resx | 52 ++ Site5/App_GlobalResources/Pages.Ledgers.resx | 6 + Site5/Master-v5.master | 4 +- Site5/Master-v5.master.cs | 15 + .../Pages/v5/Finance/RequestCashAdvance.aspx | 41 +- .../v5/Finance/RequestCashAdvance.aspx.cs | 77 +++ Site5/Pages/v5/Init/Default.aspx.cs | 2 +- .../Pages/v5/Ledgers/Json-ProfitLossData.aspx | 1 + .../v5/Ledgers/Json-ProfitLossData.aspx.cs | 285 ++++++++++ .../Pages/v5/Ledgers/ProfitLossStatement.aspx | 35 ++ .../v5/Ledgers/ProfitLossStatement.aspx.cs | 16 + Site5/Style/style-v5.css | 6 +- 22 files changed, 1217 insertions(+), 26 deletions(-) create mode 100644 Basic/Types/Financial/BasicCashAdvance.cs create mode 100644 Database/Database-CashAdvances.cs create mode 100644 Logic/Financial/CashAdvance.cs create mode 100644 Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx create mode 100644 Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx.cs create mode 100644 Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx create mode 100644 Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx.cs diff --git a/Basic/Basic.csproj b/Basic/Basic.csproj index f40a6512..b6253e5e 100644 --- a/Basic/Basic.csproj +++ b/Basic/Basic.csproj @@ -57,6 +57,7 @@ + diff --git a/Basic/Enums/ObjectOptionalDataType.cs b/Basic/Enums/ObjectOptionalDataType.cs index 32d33320..98c2df46 100644 --- a/Basic/Enums/ObjectOptionalDataType.cs +++ b/Basic/Enums/ObjectOptionalDataType.cs @@ -188,7 +188,15 @@ namespace Activizr.Basic.Enums /// /// If "1", this financial account may be charged with invoices /// - FinancialAccountEnabledForInvoicing + FinancialAccountEnabledForInvoicing, + /// + /// Organization's operations currency, stored as currency code + /// + OrgCurrency, + /// + /// First bookkeeping year of org in Activizr (can set inbound balances for this year only) + /// + OrgFirstBookkeepingYear } diff --git a/Basic/Types/Financial/BasicCashAdvance.cs b/Basic/Types/Financial/BasicCashAdvance.cs new file mode 100644 index 00000000..5960d83f --- /dev/null +++ b/Basic/Types/Financial/BasicCashAdvance.cs @@ -0,0 +1,60 @@ +using System; +using Activizr.Basic.Interfaces; + +namespace Activizr.Basic.Types.Financial +{ + [Serializable] + public class BasicCashAdvance: IHasIdentity + { + public BasicCashAdvance(int cashAdvanceId, int organizationId, int personId, DateTime createdDateTime, int createdByPersonId, + int financialAccountId, Int64 amountCents, string description, bool open, bool attested, + bool paidOut, int attestedByPersonId, DateTime attestedDateTime) + { + this.CashAdvanceId = cashAdvanceId; + this.OrganizationId = organizationId; + this.PersonId = personId; + this.CreatedDateTime = createdDateTime; + this.CreatedByPersonId = createdByPersonId; + this.FinancialAccountId = financialAccountId; + this.AmountCents = amountCents; + this.Description = description; + this.Open = open; + this.Attested = attested; + this.PaidOut = paidOut; + this.AttestedByPersonId = attestedByPersonId; + this.AttestedDateTime = attestedDateTime; + } + + public BasicCashAdvance (BasicCashAdvance original): + this(original.CashAdvanceId, original.OrganizationId, original.PersonId, original.CreatedDateTime, original.CreatedByPersonId, + original.FinancialAccountId, original.AmountCents,original.Description, original.Open, original.Attested, + original.PaidOut, original.AttestedByPersonId, original.AttestedDateTime) + { + // copy ctor - no action other than copying original object + } + + public BasicCashAdvance() + { + // Never call this ctor directly. Produces an intentional compile-time error. Exists for serialization only. + } + + + public int CashAdvanceId { get; private set; } + public int OrganizationId { get; private set; } + public int PersonId { get; protected set; } + public DateTime CreatedDateTime { get; protected set; } + public int CreatedByPersonId { get; protected set; } + public int FinancialAccountId { get; protected set; } + public Int64 AmountCents { get; protected set; } + public string Description { get; protected set; } + public bool Open { get; protected set; } + public bool Attested { get; protected set; } + public bool PaidOut { get; protected set; } + public int AttestedByPersonId { get; protected set; } + public DateTime AttestedDateTime { get; protected set; } + + // IHasIdentity interface + + public int Identity { get { return this.CashAdvanceId; } } + } +} diff --git a/Database/Database-CashAdvances.cs b/Database/Database-CashAdvances.cs new file mode 100644 index 00000000..7d211721 --- /dev/null +++ b/Database/Database-CashAdvances.cs @@ -0,0 +1,523 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Text; +using Activizr.Basic.Enums; +using Activizr.Basic.Types; +using Activizr.Basic.Types.Financial; + +namespace Activizr.Database +{ + public partial class PirateDb + { + + #region Database field reading + + private const string cashAdvanceFieldSequence = + " CashAdvanceId,OrganizationId,PersonId,CreatedDateTime,CreatedByPersonId," + // 0-4 + "FinancialAccountId,AmountCents,Description,Open,Attested," + // 5-9 + "PaidOut,AttestedByPersonId,AttestedDateTime " + // 10-12 + "FROM CashAdvances "; + + private static BasicCashAdvance ReadCashAdvanceFromDataReader(IDataRecord reader) + { + int cashAdvanceId = reader.GetInt32(0); + int organizationId = reader.GetInt32(1); + int personId = reader.GetInt32(2); + DateTime createdDateTime = reader.GetDateTime(3); + int createdByPersonId = reader.GetInt32(4); + + int financialAccountId = reader.GetInt32(5); + Int64 amountCents = reader.GetInt64(6); + string description = reader.GetString(7); + bool open = reader.GetBoolean(8); + bool attested = reader.GetBoolean(9); + + bool paidOut = reader.GetBoolean(10); + int attestedByPersonId = reader.GetInt32(11); + DateTime attestedDateTime = reader.GetDateTime(12); + + return new BasicCashAdvance(cashAdvanceId, organizationId, personId, createdDateTime, createdByPersonId, + financialAccountId, amountCents, description, open, attested, + paidOut, attestedByPersonId, attestedDateTime); + } + + + #endregion + + + #region Database record reading -- SELECT clauses + + + public BasicCashAdvance GetCashAdvance (int cashAdvanceId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT " + cashAdvanceFieldSequence + + " WHERE CashAdvanceId=" + cashAdvanceId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadCashAdvanceFromDataReader(reader); + } + + throw new ArgumentException("No such CashAdvanceId:" + cashAdvanceId.ToString()); + } + } + + } + + + public BasicCashAdvance[] GetCashAdvances(params object[] conditions) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT " + cashAdvanceFieldSequence + ConstructWhereClause("CashAdvances", conditions), connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadCashAdvanceFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + #endregion + + + #region Creation and manipulation -- stored procedures + + + public int CreateCashAdvance(int personId, int createdByPersonId, int organizationId, int financialAccountId, + Int64 amountCents, string description) + { + if (description.Length > 128) + { + description = description.Substring(0, 128); // varchar(128) field + } + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreateCashAdvance", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "personId", personId); + AddParameterWithName(command, "createdByPersonId", createdByPersonId); + AddParameterWithName(command, "createdDateTime", DateTime.Now); + AddParameterWithName(command, "organizationId", organizationId); + AddParameterWithName(command, "amountCents", amountCents); + AddParameterWithName(command, "financialAccountId", financialAccountId); + AddParameterWithName(command, "description", description); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + // TODO: SetCashAdvanceAttested + + + public void SetCashAdvanceOpen(int cashAdvanceId, bool open) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetCashAdvanceOpen", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "cashAdvanceId", cashAdvanceId); + AddParameterWithName(command, "open", open); + + command.ExecuteNonQuery(); + } + } + + + public void SetCashAdvancePaidOut(int cashAdvanceId, bool paidOut) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetCashAdvancePaidOut", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "cashAdvanceId", cashAdvanceId); + AddParameterWithName(command, "paidOut", paidOut); + + command.ExecuteNonQuery(); + } + } + + + #endregion + + + #region Dead template code + + /* + public BasicExpenseClaim GetExpenseClaim(int expenseClaimId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE ExpenseClaimId=" + expenseClaimId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadExpenseFromDataReader(reader); + } + + throw new ArgumentException("Unknown ExpenseClaim Id"); + } + } + } + + public BasicExpenseClaim[] GetExpenseClaimsByClaimer(int claimingPersonId) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE ClaimingPersonId=" + claimingPersonId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadExpenseFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + public BasicExpenseClaim[] GetExpenseClaimsByBudgetAndYear(int budgetId, int budgetYear) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE BudgetId=" + budgetId.ToString() + " AND BudgetYear=" + budgetYear.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadExpenseFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + public BasicExpenseClaim[] GetExpenseClaimsByOrganization(int organizationId) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE OrganizationId=" + organizationId.ToString(), + 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); + int budgetYear = reader.GetInt32(10); + DateTime expenseDate = reader.GetDateTime(11); + string description = reader.GetString(12); + double preApprovedAmount = reader.GetDouble(13); + double amount = reader.GetDouble(14); + bool repaid = reader.GetBoolean(15); + + return new BasicExpenseClaim(expenseClaimId, claimingPersonId, createdDateTime, + open, attested, documented, claimed, organizationId, geographyId, + budgetId, budgetYear, expenseDate, description, preApprovedAmount, + amount, repaid); + } + + + public int CreateExpenseClaim(int claimingPersonId, int organizationId, int budgetId, int budgetYear, + DateTime expenseDate, string description, double amount) + { + 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, "budgetYear", budgetYear); + AddParameterWithName(command, "expenseDate", expenseDate); + AddParameterWithName(command, "description", description); + AddParameterWithName(command, "amount", amount); + + 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 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 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 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) + { + // 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()); + } + } + + + */ + + #endregion + + + } +} \ No newline at end of file diff --git a/Database/Database.csproj b/Database/Database.csproj index e887a1e1..02c2b821 100644 --- a/Database/Database.csproj +++ b/Database/Database.csproj @@ -56,6 +56,7 @@ + diff --git a/Logic/Financial/CashAdvance.cs b/Logic/Financial/CashAdvance.cs new file mode 100644 index 00000000..1f1177ce --- /dev/null +++ b/Logic/Financial/CashAdvance.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Activizr.Basic.Types.Financial; +using Activizr.Database; +using Activizr.Logic.Pirates; +using Activizr.Logic.Structure; + +namespace Activizr.Logic.Financial +{ + [Serializable] + public class CashAdvance: BasicCashAdvance, IAttestable + { + #region Construction and creation + + private CashAdvance(BasicCashAdvance basic): base(basic) + { + + } + + public static CashAdvance FromBasic (BasicCashAdvance basic) + { + return new CashAdvance(basic); + } + + public static CashAdvance FromIdentity (int cashAdvanceId) + { + return FromBasic(PirateDb.GetDatabaseForReading().GetCashAdvance(cashAdvanceId)); + } + + public static CashAdvance FromIdentityAggressive (int cashAdvanceId) + { + return FromBasic(PirateDb.GetDatabaseForWriting().GetCashAdvance(cashAdvanceId)); + } + + public static CashAdvance Create(Organization organization, Person forPerson, Person createdByPerson, Int64 amountCents, FinancialAccount budget, string description) + { + return + FromIdentityAggressive(PirateDb.GetDatabaseForWriting().CreateCashAdvance(forPerson.Identity, + createdByPerson.Identity, + organization.Identity, + budget.Identity, amountCents, + description)); + } + + #endregion + + + + #region Implementation of IAttestable + + public void Attest(Person attester) + { + throw new NotImplementedException(); + } + + public void Deattest(Person deattester) + { + throw new NotImplementedException(); + } + + #endregion + } +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 2f419d14..be55412e 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -64,6 +64,7 @@ + diff --git a/Logic/Structure/Organization.cs b/Logic/Structure/Organization.cs index 56995ec6..dff16a60 100644 --- a/Logic/Structure/Organization.cs +++ b/Logic/Structure/Organization.cs @@ -151,7 +151,37 @@ namespace Activizr.Logic.Structure get { return OptionalData.GetOptionalDataBool(ObjectOptionalDataType.OrgEconomyEnabled); } } - public void EnableEconomy() + public Currency Currency + { + get + { + if (!IsEconomyEnabled) + { + throw new InvalidOperationException("Cannot request currency; organization is not economy-enabled"); + } + + string currencyCode = OptionalData.GetOptionalDataString(ObjectOptionalDataType.OrgCurrency); + + if (string.IsNullOrEmpty(currencyCode)) + { + if (this.Identity == 1 && (this.Name.StartsWith("Piratpartiet") || this.Name.StartsWith("Sandbox"))) + { + // This is a one-off to fix the v4 installation. Currency is SEK. + OptionalData.SetOptionalDataString(ObjectOptionalDataType.OrgCurrency, "SEK"); + currencyCode = "SEK"; + } + if (this.Identity == 2 && (this.Name.StartsWith("European"))) + { + OptionalData.SetOptionalDataString(ObjectOptionalDataType.OrgCurrency, "SEK"); + currencyCode = "SEK"; + } + } + + return Currency.FromCode(currencyCode); + } + } + + public void EnableEconomy(Currency currency) { if (IsEconomyEnabled) { @@ -224,6 +254,10 @@ namespace Activizr.Logic.Structure FinancialAccountType.Cost, 0); conferenceBase.IsConferenceParent = true; + // Set the currency + + OptionalData.SetOptionalDataString(ObjectOptionalDataType.OrgCurrency, currency.Code); + // Finally, flag the org as enabled this.OptionalData.SetOptionalDataBool(ObjectOptionalDataType.OrgEconomyEnabled, true); diff --git a/Site5/App_GlobalResources/Menu5.resx b/Site5/App_GlobalResources/Menu5.resx index a78e3c79..c9522983 100644 --- a/Site5/App_GlobalResources/Menu5.resx +++ b/Site5/App_GlobalResources/Menu5.resx @@ -261,8 +261,8 @@ Payroll - - Result Sheet + + Profit & Loss Statement Set Budget Amounts diff --git a/Site5/App_GlobalResources/Menu5.sv-SE.resx b/Site5/App_GlobalResources/Menu5.sv-SE.resx index 9ebe523e..6dc11c6b 100644 --- a/Site5/App_GlobalResources/Menu5.sv-SE.resx +++ b/Site5/App_GlobalResources/Menu5.sv-SE.resx @@ -255,7 +255,7 @@ Lönelista - + Resulträkning diff --git a/Site5/App_GlobalResources/Pages.Finance.resx b/Site5/App_GlobalResources/Pages.Finance.resx index 09450fa1..bc40fa04 100644 --- a/Site5/App_GlobalResources/Pages.Finance.resx +++ b/Site5/App_GlobalResources/Pages.Finance.resx @@ -117,10 +117,62 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Advance amount (in {0}) + {0} is the currency. + + + Account Number + + + Clearing Number + + + Bank Name + + + Budget + + + Your Bank Details + If you are about to make a purchase on behalf of the organization, but need the money in advance rather than claiming them back afterwards, you can request a cash advance for a specific purpose. Request Cash Advance + + Purpose + + + A cash advance has been requested for <strong>{0}</strong> for the purpose of {1}. The requested amount is {2} {3:N2}. Note that this is a <strong>loan</strong> from the organization until receipts have been presented. + {0} is the person receiving the advance, {1} is the entered description. {2} is the currency (in caps) and {3:N2} is the amount, with two decimals, displayed in culture. + + + This needs to be attested by the budget owner and will then proceed to payout. + + + As this is your own budget, this has been automatically attested and has proceeded to payout. + This is displayed if either the person creating the cash advance OR the person receiving it is the budget owner. + + + Please enter the amount of money (in {0}) that you'd like to advance. + {0} is the currency. + + + Please enter your bank account number. + + + Please enter your bank's clearing number. + + + Please enter your bank's name. + + + Please select a budget to charge. + + + Please state the purpose of the cash advance. + \ No newline at end of file diff --git a/Site5/App_GlobalResources/Pages.Ledgers.resx b/Site5/App_GlobalResources/Pages.Ledgers.resx index b6506b91..2cda3d36 100644 --- a/Site5/App_GlobalResources/Pages.Ledgers.resx +++ b/Site5/App_GlobalResources/Pages.Ledgers.resx @@ -117,6 +117,12 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + The P&L is an overview of income and expenditures, and are grouped by budgets or cost/income type. The sum of all income and expenditure is the yearly result. You may also want to look at the <a href="BalanceSheet.aspx">balance sheet</a> which is the overview of assets and debts. + + + Profit & Loss Statement + Budget, Owner Displayed above editables diff --git a/Site5/Master-v5.master b/Site5/Master-v5.master index 53634d6f..fbefdce0 100644 --- a/Site5/Master-v5.master +++ b/Site5/Master-v5.master @@ -66,6 +66,8 @@ $("a[rel*=leanModal]").leanModal(); + + }); @@ -178,7 +180,7 @@ - + diff --git a/Site5/Master-v5.master.cs b/Site5/Master-v5.master.cs index e42f7a06..765f90f8 100644 --- a/Site5/Master-v5.master.cs +++ b/Site5/Master-v5.master.cs @@ -96,6 +96,21 @@ namespace Activizr SetupDropboxes(); this.ButtonSwitchOrganizations.Attributes.Add("onclick", "$('#" + this.TextSwitchToOrganizationId.ClientID + "').val($('#" + this.DropSwitchOrganizations.ClientID + "').val()); $('#" + this.ButtonActuatorSwitchOrganizations.ClientID + "').click();"); + + // Check for message to display + + HttpCookie dashMessage = Request.Cookies["DashboardMessage"]; + + if (dashMessage != null && dashMessage.Value.Length > 0) + { + this.LiteralDocumentReadyHook.Text = string.Format("alertify.alert(unescape('{0}'.replace(/\\+/g, '%20')));", dashMessage.Value); + Response.SetCookie(new HttpCookie("DashboardMessage", string.Empty)); + Response.Cookies ["DashboardMessage"].Expires = DateTime.Now.AddYears(-10); + } + else + { + this.LiteralDocumentReadyHook.Text = string.Empty; + } } private void SetupDropboxes() diff --git a/Site5/Pages/v5/Finance/RequestCashAdvance.aspx b/Site5/Pages/v5/Finance/RequestCashAdvance.aspx index 881922bc..da130709 100644 --- a/Site5/Pages/v5/Finance/RequestCashAdvance.aspx +++ b/Site5/Pages/v5/Finance/RequestCashAdvance.aspx @@ -2,7 +2,10 @@ - + + @@ -69,16 +78,16 @@  
 
 
- +
diff --git a/Site5/Pages/v5/Finance/RequestCashAdvance.aspx.cs b/Site5/Pages/v5/Finance/RequestCashAdvance.aspx.cs index 8dbbaac7..092ea4d5 100644 --- a/Site5/Pages/v5/Finance/RequestCashAdvance.aspx.cs +++ b/Site5/Pages/v5/Finance/RequestCashAdvance.aspx.cs @@ -24,6 +24,83 @@ public partial class Pages_v5_Finance_RequestCashAdvance : PageV5Base this.TextAccount.Text = this.CurrentUser.BankAccount; this.TextAmount.Text = 0.ToString("N2"); this.TextAmount.Focus(); + + Localize(); } } + + + private void Localize() + { + this.LabelAmount.Text = string.Format(Resources.Pages.Finance.RequestCashAdvance_Amount, + CurrentOrganization.Currency.Code); + this.LabelPurpose.Text = Resources.Pages.Finance.RequestCashAdvance_Purpose; + this.LabelBudget.Text = Resources.Pages.Finance.RequestCashAdvance_Budget; + this.LabelHeaderBankDetails.Text = Resources.Pages.Finance.RequestCashAdvance_HeaderBankDetails; + this.LabelBankName.Text = Resources.Pages.Finance.RequestCashAdvance_BankName; + this.LabelBankClearing.Text = Resources.Pages.Finance.RequestCashAdvance_BankClearing; + this.LabelBankAccount.Text = Resources.Pages.Finance.RequestCashAdvance_BankAccount; + + this.LiteralErrorAmount.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Amount; + this.LiteralErrorPurpose.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Purpose; + this.LiteralErrorBudget.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_Budget; + this.LiteralErrorBankName.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankName; + this.LiteralErrorBankClearing.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankClearing; + this.LiteralErrorBankAccount.Text = Resources.Pages.Finance.RequestCashAdvance_ValidationError_BankAccount; + } + + + protected void ButtonRequest_Click(object sender, EventArgs e) + { + // The data has been validated client-side already. We'll throw unfriendly exceptions if invalid data is passed here. + + double amount = Double.Parse(this.TextAmount.Text, NumberStyles.Number); // parses in current culture - intentional + Int64 amountCents = (Int64) amount*100; + + string description = this.TextPurpose.Text; + + FinancialAccount budget = FinancialAccount.FromIdentity(Int32.Parse(this.Request.Form["DropBudgets"])); + + // sanity check + + if (budget.Organization.Identity != CurrentOrganization.Identity) + { + throw new InvalidOperationException("Budget-organization mismatch; won't file cash advance"); + } + + // Store bank details for current user + + CurrentUser.BankName = this.TextBank.Text; + CurrentUser.BankClearing = this.TextClearing.Text; + CurrentUser.BankAccount = this.TextAccount.Text; + + // Create cash advance + + CashAdvance cashAdvance = CashAdvance.Create(CurrentOrganization, CurrentUser, CurrentUser, amountCents, budget, + description); + + // Create success message + + string successMessage = string.Format(Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartOne, + HttpUtility.HtmlEncode(CurrentUser.Name), + HttpUtility.HtmlEncode(description), CurrentOrganization.Currency.Code, + (double) (amountCents/100.0)); + + if (budget.OwnerPersonId != CurrentUser.Identity) + { + successMessage += "

" + Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartTwo + "
"; + } + else + { + successMessage += "

" + Resources.Pages.Finance.RequestCashAdvance_SuccessMessagePartTwoOwnBudget + + "
"; + // TODO: Auto-attest + } + + Response.AppendCookie(new HttpCookie("DashboardMessage", HttpUtility.UrlEncode(successMessage))); + + // Redirect to dashboard + + Response.Redirect("/", true); + } } \ No newline at end of file diff --git a/Site5/Pages/v5/Init/Default.aspx.cs b/Site5/Pages/v5/Init/Default.aspx.cs index cf65ffbc..9d328cb9 100644 --- a/Site5/Pages/v5/Init/Default.aspx.cs +++ b/Site5/Pages/v5/Init/Default.aspx.cs @@ -527,7 +527,7 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page Activizr.Logic.Structure.Organization.Create(0, "Sandbox", "Sandbox", "Sandbox", "activizr.com", "Act", rootGeographyId, true, - true, 0).EnableEconomy(); + true, 0).EnableEconomy(Activizr.Logic.Financial.Currency.FromCode("EUR")); _initProgress = 100; _initMessage = "Complete."; diff --git a/Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx b/Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx new file mode 100644 index 00000000..eaed9493 --- /dev/null +++ b/Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-ProfitLossData.aspx.cs" Inherits="Pages_v5_Ledgers_Json_ProfitLossData" %> diff --git a/Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx.cs b/Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx.cs new file mode 100644 index 00000000..71f02c26 --- /dev/null +++ b/Site5/Pages/v5/Ledgers/Json-ProfitLossData.aspx.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using Activizr.Basic.Enums; +using Activizr.Logic.Financial; +using Activizr.Logic.Pirates; +using Activizr.Logic.Security; +using Activizr.Logic.Structure; + +public partial class Pages_v5_Ledgers_Json_ProfitLossData : System.Web.UI.Page +{ + protected void Page_Load(object sender, EventArgs e) + { + // Current authentication + + string identity = HttpContext.Current.User.Identity.Name; + string[] identityTokens = identity.Split(','); + + string userIdentityString = identityTokens[0]; + string organizationIdentityString = identityTokens[1]; + + int currentUserId = Convert.ToInt32(userIdentityString); + int currentOrganizationId = Convert.ToInt32(organizationIdentityString); + + Person currentUser = Person.FromIdentity(currentUserId); + Authority authority = currentUser.GetAuthority(); + Organization currentOrganization = Organization.FromIdentity(currentOrganizationId); + + // Get accounts + + FinancialAccounts accounts = FinancialAccounts.ForOrganization(currentOrganization, FinancialAccountType.Result); + + // Build tree (there should be a template for this) + + Dictionary> treeMap = new Dictionary>(); + + foreach (FinancialAccount account in accounts) + { + if (!treeMap.ContainsKey(account.ParentIdentity)) + { + treeMap[account.ParentIdentity] = new List(); + } + + treeMap[account.ParentIdentity].Add(account); + } + + + FinancialAccounts orderedList = new FinancialAccounts(); // This list is guaranteed to have parents before children + + PopulateOrderedList(treeMap, orderedList, 0); // recursively add nodes parents-first + PopulateLookups(orderedList); // populate the lookup tables for results per account + + Response.ContentType = "application/json"; + + int renderRootNodeId = 0; + + if (treeMap[0].Count == 1) + { + // assume there's a master root like "Costs"; bypass it + + renderRootNodeId = treeMap[0][0].Identity; + } + + Response.Output.WriteLine(RecurseTreeMap(treeMap, renderRootNodeId)); + + Response.End(); + } + + + private string RecurseTreeMap (Dictionary> treeMap, int renderNodeId) + { + List elements = new List(); + + foreach (FinancialAccount account in treeMap[renderNodeId]) + { + string element = string.Format("\"id\":{0},\"name\":\"{1}\"", account.Identity, + account.Name.Replace("\"", "'")); + + element += string.Format(",\"lastYear\":\"{0:N2} ({1:N2})\"", singleLookups[0][account.Identity], + treeLookups[0][account.Identity]); + + element += string.Format(",\"ytd\":\"{0:N2} ({1:N2})\"", singleLookups[5][account.Identity], + treeLookups[5][account.Identity]); + + if (treeMap.ContainsKey(account.Identity)) + { + element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, account.Identity); + } + + elements.Add("{" + element + "}"); + } + + return "[" + String.Join(",", elements.ToArray()) + "]"; + } + + + private void PopulateOrderedList (Dictionary> treeMap, FinancialAccounts orderedList, int renderNodeId) + { + foreach (FinancialAccount account in treeMap[renderNodeId]) + { + orderedList.Add(account); + + if (treeMap.ContainsKey(account.Identity)) + { + PopulateOrderedList(treeMap, orderedList, account.Identity); // recursive call + } + } + } + + + private int _year = 2012; + private Dictionary[] singleLookups; + private Dictionary[] treeLookups; + + + private void PopulateLookups(FinancialAccounts accounts) + { + singleLookups = new Dictionary[6]; + treeLookups = new Dictionary[6]; + + for (int index = 0; index < 6; index++) + { + treeLookups[index] = new Dictionary(); + singleLookups[index] = new Dictionary(); + } + + DateTime[] quarterBoundaries = + { + new DateTime(_year, 1, 1), new DateTime(_year, 3, 1), new DateTime(_year, 6, 1), + new DateTime(_year, 9, 1), new DateTime(_year + 1, 1, 1) + }; + + // 1) Actually, the accounts are already sorted. Or are supposed to be, anyway, + // since FinancialAccounts.ForOrganization gets the _tree_ rather than the flat list. + + // 2) Add all values to the accounts. + + foreach (FinancialAccount account in accounts) + { + // Find this year's inbound + + singleLookups[0][account.Identity] = account.GetDeltaCents(new DateTime(_year - 1, 1, 1), new DateTime(_year, 1, 1)); + + // Find quarter diffs + + for (int quarter = 0; quarter < 4; quarter++) + { + singleLookups[quarter + 1][account.Identity] = account.GetDeltaCents(quarterBoundaries[quarter], + quarterBoundaries[quarter + 1]); + } + + // Find outbound + + singleLookups[5][account.Identity] = account.GetDeltaCents(new DateTime(_year, 1, 1), new DateTime(_year + 1, 1, 1)); + + // copy to treeLookups + + for (int index = 0; index < 6; index++) + { + treeLookups[index][account.Identity] = singleLookups[index][account.Identity]; + } + } + + // 3) Add all children's values to parents + + for (int index = 0; index < 6; index++) + { + AddChildrenValuesToParents(treeLookups[index], accounts); + } + + // Done. + } + + + private void AddChildrenValuesToParents(Dictionary lookup, FinancialAccounts accounts) + { + // Iterate backwards and add any value to its parent's value, as they are sorted in tree order. + + for (int index = accounts.Count - 1; index >= 0; index--) + { + int parentFinancialAccountId = accounts[index].ParentFinancialAccountId; + int accountId = accounts[index].Identity; + + if (parentFinancialAccountId != 0) + { + lookup[parentFinancialAccountId] += lookup[accountId]; + } + } + } + + + + /* + protected void GridBudgetAccounts_ItemCreated(object sender, GridItemEventArgs e) + { + // CreateExpandCollapseButton(e.Item, "Name"); + + if (e.Item is GridHeaderItem && e.Item.OwnerTableView != this.GridBudgetAccounts.MasterTableView) + { + e.Item.Style["display"] = "none"; + } + + if (e.Item is GridNestedViewItem) + { + e.Item.Cells[0].Visible = false; + } + + if (e.Item is GridDataItem) + { + int year = DateTime.Today.Year; + + FinancialAccount account = (FinancialAccount) e.Item.DataItem; + + if (account == null) + { + return; + } + + HyperLink editLink = (HyperLink) e.Item.FindControl("ManageLink"); + editLink.Attributes["href"] = "#"; + editLink.Attributes["onclick"] = String.Format("return ShowAccountForm('{0}','{1}');", + account.FinancialAccountId, e.Item.ItemIndex); + + PopulateLine(e.Item); + } + } + + protected void PopulateLine(GridItem item) + { + FinancialAccount account = (FinancialAccount) item.DataItem; + + if (account == null) + { + return; + } + + Label labelAccountName = (Label) item.FindControl("LabelAccountName"); + Literal literalLastYear = (Literal) item.FindControl("LiteralLastYear"); + Literal literalQ1 = (Literal) item.FindControl("LiteralQuarter1"); + Literal literalQ2 = (Literal) item.FindControl("LiteralQuarter2"); + Literal literalQ3 = (Literal) item.FindControl("LiteralQuarter3"); + Literal literalQ4 = (Literal) item.FindControl("LiteralQuarter4"); + Literal literalYtd = (Literal) item.FindControl("LiteralThisYear"); + + PopulateDualFigure(literalLastYear, "LastYear", 0, account.Identity); + PopulateDualFigure(literalQ1, "Q1", 1, account.Identity); + PopulateDualFigure(literalQ2, "Q2", 2, account.Identity); + PopulateDualFigure(literalQ3, "Q3", 3, account.Identity); + PopulateDualFigure(literalQ4, "Q4", 4, account.Identity); + PopulateDualFigure(literalYtd, "Ytd", 5, account.Identity); + labelAccountName.Text = account.Name; + } + */ + private void PopulateDualFigure(Literal literal, string spanPrefix, int category, int accountId) + { + Int64 treeValue = treeLookups[category][accountId]; + Int64 singleValue = singleLookups[category][accountId]; + + string span = + string.Format( + "{2:N0}{4}{3:N0}", + accountId, spanPrefix, singleValue, treeValue, + singleValue != treeValue ? "Σ " : string.Empty); + literal.Text = span; + } + + private void PopulateFigureCollapsed(Label label, int category, int accountId) + { + string prefix = string.Empty; + + Int64 treeValue = treeLookups[category][accountId]; + Int64 singleValue = singleLookups[category][accountId]; + + if (treeValue != singleValue) + { + prefix = "Σ "; + } + + label.Text = prefix + treeValue.ToString("N0"); + } + +} \ No newline at end of file diff --git a/Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx b/Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx new file mode 100644 index 00000000..8c724457 --- /dev/null +++ b/Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx @@ -0,0 +1,35 @@ +<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="ProfitLossStatement.aspx.cs" Inherits="Pages_v5_Ledgers_ProfitLossStatement" %> + + + + + + + + + +
+

P&L for Piratpartiet For Year

+ + + + + + + + + + + + +
AccountLast YearQ1Q2Q3Q4YTD
+
+ +
+ + + diff --git a/Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx.cs b/Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx.cs new file mode 100644 index 00000000..3d089484 --- /dev/null +++ b/Site5/Pages/v5/Ledgers/ProfitLossStatement.aspx.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +public partial class Pages_v5_Ledgers_ProfitLossStatement : PageV5Base +{ + protected void Page_Load(object sender, EventArgs e) + { + this.PageIcon = "iconshock-abacus"; + this.PageTitle = Resources.Pages.Ledgers.ProfitLossStatement_PageTitle; + this.InfoBoxLiteral = Resources.Pages.Ledgers.ProfitLossStatement_Info; + } +} \ No newline at end of file diff --git a/Site5/Style/style-v5.css b/Site5/Style/style-v5.css index 8281b08e..cf09aec6 100644 --- a/Site5/Style/style-v5.css +++ b/Site5/Style/style-v5.css @@ -511,7 +511,7 @@ span.loginalphabetasign margin-top: 5px; margin-bottom: 5px; font-size: 24px; - width: 335px; + width: 330px; -webkit-box-shadow: 0 1px 3px 1px #1C397E; -moz-box-shadow: 0 1px 3px 1px #1C397E; @@ -521,8 +521,8 @@ span.loginalphabetasign .entryFields input, .entryFields textarea, .entryFields select { - border-radius: 15px; - padding: 0px 10px 0px 15px; + border-radius: 10px; + padding: 0px 10px 0px 10px; font-size: 24px; }