From dbe7e89f70ce41a74380cecea8ca6759e14bff68 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 8 Feb 2015 10:47:33 +0100 Subject: [PATCH] Made tax processing currency-aware --- Database/Database-TaxLevels.cs | 2 - Logic/Financial/Money.cs | 24 +++++++++ Logic/Financial/Salary.cs | 93 +++++++++++++--------------------- Logic/Financial/TaxLevels.cs | 15 +++--- 4 files changed, 69 insertions(+), 65 deletions(-) diff --git a/Database/Database-TaxLevels.cs b/Database/Database-TaxLevels.cs index 58a34ca5..d68a8d62 100644 --- a/Database/Database-TaxLevels.cs +++ b/Database/Database-TaxLevels.cs @@ -14,8 +14,6 @@ namespace Swarmops.Database { int thisYear = DateTime.Today.Year; // TODO: Fiscal years may be different in some weird countries - // TODO: Adapt currency if the operating currency is different from the tax currency? Or should that be in the logic layer? - connection.Open(); DbCommand command = diff --git a/Logic/Financial/Money.cs b/Logic/Financial/Money.cs index 548715ac..b8d90903 100644 --- a/Logic/Financial/Money.cs +++ b/Logic/Financial/Money.cs @@ -6,14 +6,38 @@ using System.Threading.Tasks; namespace Swarmops.Logic.Financial { + [Serializable] public class Money { + public Money() + { + // default ctor for serializability + } + + public Money (Int64 cents, Currency currency) + : this (cents, currency, DateTime.UtcNow) + { + } + + public Money (Int64 cents, Currency currency, DateTime valuationDateTime) + { + this.Cents = cents; + this.Currency = currency; + this.ValuationDateTime = valuationDateTime; + } + public Currency Currency { get; set; } public Int64 Cents { get; set; } public DateTime ValuationDateTime { get; set; } public Money ToCurrency (Currency newCurrency) { + if (newCurrency.Identity == this.Currency.Identity) + { + // no conversion + return this; + } + double conversionRate = this.Currency.GetConversionRate (newCurrency, this.ValuationDateTime); return new Money { diff --git a/Logic/Financial/Salary.cs b/Logic/Financial/Salary.cs index 7f3d943b..7f9ef757 100644 --- a/Logic/Financial/Salary.cs +++ b/Logic/Financial/Salary.cs @@ -27,40 +27,9 @@ namespace Swarmops.Logic.Financial } - public decimal BaseSalaryDecimal - { - get { return base.BaseSalaryCents/100.0m; } - } - - - public decimal SubtractiveTaxDecimal - { - get { return base.SubtractiveTaxCents/100.0m; } - } - - public decimal AdditiveTaxDecimal - { - get { return base.AdditiveTaxCents/100.0m; } - } - - - public decimal NetSalaryDecimal - { - get { return base.NetSalaryCents/100.0m; } - } - - public new Int64 NetSalaryCents { - get { return base.NetSalaryCents; } // TODO: CONVERT TO MAKE CENTS-BASED CALCULATIONS THE MASTER - set - { - if (value != base.NetSalaryCents) - { - SwarmDb.GetDatabaseForWriting().SetSalaryNetSalary (Identity, value); - base.NetSalaryCents = value; - } - } + get { return base.NetSalaryCents; } } @@ -121,7 +90,7 @@ namespace Swarmops.Logic.Financial set { SwarmDb.GetDatabaseForWriting().SetSalaryTaxPaid (Identity, value); - base.NetPaid = value; + base.TaxPaid = value; base.Open = !(base.NetPaid && base.TaxPaid); } } @@ -209,16 +178,21 @@ namespace Swarmops.Logic.Financial // calculate tax - double subtractiveTax = TaxLevels.GetTax (payrollItem.Country, payrollItem.SubtractiveTaxLevelId, - payCents/100.0); - - if (subtractiveTax < 1.0) + Money grossInOrgCurrency = new Money { - // this is a percentage and not an absolute number + Cents = payCents, + Currency = payrollItem.Organization.Currency, + ValuationDateTime = DateTime.UtcNow + }; - subtractiveTax = payCents*subtractiveTax; - } - Int64 subtractiveTaxCents = (Int64) (subtractiveTax*100); + Money grossInTaxCurrency = grossInOrgCurrency.ToCurrency (payrollItem.Country.Currency); + + Money subtractiveTax = TaxLevels.GetTax (payrollItem.Country, payrollItem.SubtractiveTaxLevelId, + grossInTaxCurrency); + + Money subtractiveTaxInOrgCurrency = subtractiveTax.ToCurrency (payrollItem.Organization.Currency); + + Int64 subtractiveTaxCents = (Int64) (subtractiveTaxInOrgCurrency.Cents); Int64 additiveTaxCents = (Int64) (payCents*payrollItem.AdditiveTaxLevel); @@ -234,6 +208,25 @@ namespace Swarmops.Logic.Financial } } + // If net is negative, create rollover adjustment + + PayrollAdjustment rolloverAdjustment = null; + + if (payCents < 0) + { + rolloverAdjustment = PayrollAdjustment.Create(payrollItem, PayrollAdjustmentType.NetAdjustment, + -payCents, + "Deficit rolls over to next salary"); + + PayrollAdjustment.Create(payrollItem, PayrollAdjustmentType.NetAdjustment, + payCents, "Deficit rolled over from " + + payoutDate.ToString("yyyy-MM-dd")); + + // keep second rollover open, so the deficit from this salary is carried to the next + + payCents = 0; + } + // Create salary, close adjustments Salary salary = Create (payrollItem, payoutDate, payCents, subtractiveTaxCents, additiveTaxCents); @@ -245,23 +238,9 @@ namespace Swarmops.Logic.Financial adjustment.Close (salary); } - // If net is negative, create rollover adjustment - - if (payCents < 0) + if (rolloverAdjustment != null) { - PayrollAdjustment rollover1 = PayrollAdjustment.Create (payrollItem, PayrollAdjustmentType.NetAdjustment, - -payCents, - "Deficit rolls over to next salary"); - - rollover1.Close (salary); - - PayrollAdjustment rollover2 = PayrollAdjustment.Create (payrollItem, PayrollAdjustmentType.NetAdjustment, - payCents, "Deficit rolled over from " + - payoutDate.ToString ("yyyy-MM-dd")); - - // keep rollover2 open, so the deficit from this salary is carried to the next - - salary.NetSalaryCents = 0; + rolloverAdjustment.Close (salary); } // Add the financial transaction diff --git a/Logic/Financial/TaxLevels.cs b/Logic/Financial/TaxLevels.cs index 03979b85..a8dde8b0 100644 --- a/Logic/Financial/TaxLevels.cs +++ b/Logic/Financial/TaxLevels.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using Swarmops.Database; using Swarmops.Logic.Structure; @@ -6,26 +7,28 @@ namespace Swarmops.Logic.Financial { public class TaxLevels { - public static double GetTax (Country country, int taxLevelIdentifier, double grossSalary) + public static Money GetTax (Country country, int taxLevelIdentifier, Money grossSalary) { - if (grossSalary < 1.0) // The lowest tax bracket is 1 currency unit + if (grossSalary.Cents < 0) // The lowest tax bracket is 1 currency unit { - return 0.0; // no tax + return new Money(0, country.Currency); // no tax } + Int64 grossCentsInCountryCurrency = grossSalary.ToCurrency (country.Currency).Cents; + double taxLevel = SwarmDb.GetDatabaseForReading().GetSalaryTaxLevel (country.Identity, taxLevelIdentifier, - (int) Math.Floor (grossSalary)); + (int) Math.Floor (grossCentsInCountryCurrency / 100.0)); if (taxLevel < 1.0) { // Percentage - return Math.Floor (grossSalary*taxLevel); + return new Money ((Int64) Math.Floor (grossCentsInCountryCurrency*taxLevel), country.Currency); } // otherwise, absolute number - return taxLevel; + return new Money((Int64) (taxLevel * 100.0), country.Currency); } public static void ImportTaxLevels (Country country, int year, string data)