From d5e6980873d420f97fb532a58eb0aaa982f66666 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 12 Oct 2015 19:49:56 +0200 Subject: [PATCH] =?UTF-8?q?Hot=20payouts=20should=20be=20functional=20-=20?= =?UTF-8?q?live=20run=20kommer,=20klart=20bak=C3=A5t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Enums/FinancialDependencyType.cs | 5 + Logic/Financial/Payout.cs | 125 +++++++++++++++++++++++- Logic/Financial/Payouts.cs | 33 ++++--- 3 files changed, 149 insertions(+), 14 deletions(-) diff --git a/Common/Enums/FinancialDependencyType.cs b/Common/Enums/FinancialDependencyType.cs index 22352754..39124793 100644 --- a/Common/Enums/FinancialDependencyType.cs +++ b/Common/Enums/FinancialDependencyType.cs @@ -27,6 +27,11 @@ namespace Swarmops.Common.Enums /// Salary, + /// + /// Salary partially paid to the tax agency because people dressed in uniforms will beat you otherwise + /// + SalaryTax, + /// /// An outbound invoice, requesting money from another organization /// diff --git a/Logic/Financial/Payout.cs b/Logic/Financial/Payout.cs index b0beb3a4..530096e8 100644 --- a/Logic/Financial/Payout.cs +++ b/Logic/Financial/Payout.cs @@ -81,7 +81,7 @@ namespace Swarmops.Logic.Financial break; case FinancialDependencyType.Salary: Salary salary = Salary.FromIdentity (dependency.ForeignId); - if (salary.NetSalaryCents == AmountCents) // HACK: Assumes that tax total is not identical + if (salary.NetSalaryCents == AmountCents) // HACK/LEGACY: Assumes that tax total is not identical { this.DependentSalariesNet.Add (salary); } @@ -91,6 +91,11 @@ namespace Swarmops.Logic.Financial } break; + case FinancialDependencyType.SalaryTax: + Salary salaryTax = Salary.FromIdentity (dependency.ForeignId); + this.DependentSalariesTax.Add (salaryTax); + break; + case FinancialDependencyType.CashAdvance: this.DependentCashAdvancesPayout.Add (CashAdvance.FromIdentity (dependency.ForeignId)); break; @@ -294,6 +299,120 @@ namespace Swarmops.Logic.Financial get { return FinancialTransaction.FromDependency (this); } } + public static Payout CreateBitcoinPayoutFromPrototype (Organization organization, Payout prototype, string transactionHash) + { + string[] components = prototype.ProtoIdentity.Split('|'); + int payoutId = 0; + + // This function is made for complex bitcoin payouts and will typically take many different types of payouts to many people at once. + + if (components.Length == 0) + { + // nothing to construct. Exception or return null? + + return null; + } + + payoutId = SwarmDb.GetDatabaseForWriting().CreatePayout(organization.Identity, "Bitcoin network", "Multiple", + transactionHash, prototype.AmountCents, DateTime.UtcNow, 0); + + foreach (string component in components) + { + int foreignId = Int32.Parse(component.Substring(1)); + + switch (component[0]) + { + case 'A': + // Cash advance + CashAdvance advance = CashAdvance.FromIdentity (foreignId); + advance.PaidOut = true; + + SwarmopsLogEntry.Create (null, + new PayoutCreatedLogEntry (null, advance.Person, organization, + organization.Currency, advance.AmountCents/100.0, + "Cash Advance Paid Out"), + advance.Person, advance); + + OutboundComm.CreateNotificationOfFinancialValidation (advance.Budget, advance.Person, + advance.AmountCents/100.0, advance.Description, NotificationResource.CashAdvance_PaidOut); + SwarmDb.GetDatabaseForWriting() + .CreatePayoutDependency (payoutId, FinancialDependencyType.CashAdvance, + foreignId); + break; + + case 'a': + // This is a negative record - payback of cash advance + CashAdvance advancePayback = CashAdvance.FromIdentity (foreignId); + advancePayback.Open = false; + + SwarmDb.GetDatabaseForWriting() + .CreatePayoutDependency(payoutId, FinancialDependencyType.CashAdvancePayback, + foreignId); + + break; + + case 'C': + // Expense claim + ExpenseClaim claim = ExpenseClaim.FromIdentity (foreignId); + claim.Repaid = true; + claim.Close(); + + OutboundComm.CreateNotificationOfFinancialValidation (claim.Budget, claim.Claimer, + claim.AmountCents/100.0, claim.Description, NotificationResource.ExpenseClaim_PaidOut); + SwarmDb.GetDatabaseForWriting() + .CreatePayoutDependency(payoutId, FinancialDependencyType.ExpenseClaim, + foreignId); + + break; + + case 'I': + // Invoice + InboundInvoice invoice = InboundInvoice.FromIdentity (foreignId); + DateTime expectedPayment = invoice.DueDate; + + if (expectedPayment < DateTime.Today) + { + expectedPayment = DateTime.Today; + } + + SwarmDb.GetDatabaseForWriting() + .CreatePayoutDependency (payoutId, FinancialDependencyType.InboundInvoice, + invoice.Identity); + + // TODO: NOTIFY PAID? + + invoice.Open = false; + break; + + case 'S': + // Salary net + + Salary salaryNet = Salary.FromIdentity (foreignId); + SwarmDb.GetDatabaseForWriting().CreatePayoutDependency (payoutId, FinancialDependencyType.Salary, + salaryNet.Identity); + salaryNet.NetPaid = true; + break; + + case 'T': + // Tax payout, typically for multiple salaries + + Salary salaryTax = Salary.FromIdentity (foreignId); + SwarmDb.GetDatabaseForWriting().CreatePayoutDependency (payoutId, FinancialDependencyType.SalaryTax, + salaryTax.Identity); + salaryTax.TaxPaid = true; + + break; + default: + throw new NotImplementedException(); + + } + } + + // Return the new object by reloading it from database + + return Payout.FromIdentity (payoutId); + } + public static Payout CreateFromProtoIdentity (Person creator, string protoIdentity) { string[] components = protoIdentity.Split ('|'); @@ -335,7 +454,7 @@ namespace Swarmops.Logic.Financial amountCents += advance.AmountCents; advance.PaidOut = true; - //advance.Open = false; // isn't this closed only when settling the debt? Serious bug here? + // advance.Open remains true until the advance is repaid SwarmopsLogEntry.Create (creator, new PayoutCreatedLogEntry (creator, advance.Person, organization, @@ -546,7 +665,7 @@ namespace Swarmops.Logic.Financial foreach (int salaryId in identityList) { - SwarmDb.GetDatabaseForWriting().CreatePayoutDependency (payoutId, FinancialDependencyType.Salary, + SwarmDb.GetDatabaseForWriting().CreatePayoutDependency (payoutId, FinancialDependencyType.SalaryTax, salaryId); } } diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 96bce6a6..dc51752d 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -595,6 +595,15 @@ namespace Swarmops.Logic.Financial } catch (NotEnoughFundsException) { + // If we're at the whole hour, send a notification to people responsible for refilling the hotwallet + + if (utcNow.Minute != 0) + { + continue; // we're not at the whole hour, so continue with next org instead + } + + // Send urgent notification to top up the damn wallet so we can make payments + NotificationStrings primaryStrings = new NotificationStrings(); primaryStrings[NotificationString.CurrencyCode] = organization.Currency.Code; primaryStrings[NotificationString.OrganizationName] = organization.Name; @@ -636,7 +645,7 @@ namespace Swarmops.Logic.Financial Int64 satoshisUsed = 0; Dictionary> notificationSpecLookup = new Dictionary>(); Dictionary> notificationAmountLookup = new Dictionary>(); - Payout masterPayout = Payout.Empty; + Payout masterPayoutPrototype = Payout.Empty; foreach (Payout payout in bitcoinPayouts) { @@ -653,7 +662,7 @@ namespace Swarmops.Logic.Financial new Satoshis(satoshiPayoutLookup[payout.ProtoIdentity])); satoshisUsed += satoshiPayoutLookup[payout.ProtoIdentity]; - payout.MigrateDependenciesTo (masterPayout); + payout.MigrateDependenciesTo (masterPayoutPrototype); } // Set change address to wallet slush @@ -683,17 +692,15 @@ namespace Swarmops.Logic.Financial // Broadcast transaction - // TODO + BitcoinUtility.BroadcastTransaction (txReady); // Delete inputs, adjust balance for addresses - // TODO - // inputs.AsUnspents.DeleteAll(); + inputs.AsUnspents.DeleteAll(); - // Register new balance of change address, should have increased by satoshisInput-satoshisUsed + // Register new balance of change address, should have increased by (satoshisInput-satoshisUsed) - // TODO - // BitcoinUtility.CheckSomething (HotAddress.OrganizationWallet(organization)... + BitcoinUtility.TestUnspents (HotBitcoinAddress.OrganizationWalletZero (organization).Address); // Send notifications @@ -725,11 +732,15 @@ namespace Swarmops.Logic.Financial // Create the master payout from its prototype - // TODO + Payout masterPayout = Payout.CreateBitcoinPayoutFromPrototype (organization, masterPayoutPrototype, txReady.GetHash().ToString()); - // Ledger entries + // Finally, create ledger entries - // TODO + FinancialTransaction ledgerTransaction = FinancialTransaction.Create (organization, utcNow, + "Bitcoin automated payout"); + ledgerTransaction.AddRow (organization.FinancialAccounts.AssetsBitcoinHot, -masterPayoutPrototype.AmountCents, null); + + masterPayout.BindToTransactionAndClose (ledgerTransaction, null); } } }