Made tax processing currency-aware

This commit is contained in:
Rick Falkvinge
2015-02-08 10:47:33 +01:00
parent 5be93e252e
commit dbe7e89f70
4 changed files with 69 additions and 65 deletions
-2
View File
@@ -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 =
+24
View File
@@ -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 {
+36 -57
View File
@@ -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
+9 -6
View File
@@ -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)