Esempio n. 1
0
        private string FormatPrototypesAsJson(Payouts payouts)
        {
            StringBuilder result = new StringBuilder(16384);

            DateTime today = DateTime.Today;
            bool     bitcoinHotWalletActive = (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot != null
                ? true
                : false);

            foreach (Payout payout in payouts)
            {
                if (bitcoinHotWalletActive && payout.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 0 && payout.Account.Length < 4)  // 4 because an empty account will be " / ", length 3
                {
                    // This is a person who will be paid in bitcoin per personal preferences, so don't show for manual payout

                    continue;
                }

                if (bitcoinHotWalletActive && payout.Account.StartsWith("bitcoin:"))
                {
                    // This is a payout registered to be paid in bitcoin, so don't show for manual payout

                    continue;
                }

                if (payout.Reference.Length < 2)
                {
                    continue; // must have at least a reference and a checksum - two digits minimum
                }

                if (!Formatting.CheckLuhnChecksum(payout.Reference))
                {
                    // invalid checksum

                    continue;
                }

                if (!Formatting.CheckLuhnChecksum(payout.Account))
                {
                    continue;
                }

                result.Append("{");
                result.AppendFormat(
                    "\"itemId\":\"{0}\"," +
                    "\"due\":\"{1}\"," +
                    "\"reference\":\"<span class='ocrFont'>{2}</span>\"," +
                    "\"amount\":\"<span class='ocrFont'>{3}</span>\"," +
                    "\"account\":\"<span class='ocrFont'>{4}</span>\"," +
                    "\"action\":\"" +
                    "<img class=\\\"IconApproval{5} LocalIconApproval LocalPrototype\\\" baseid=\\\"{0}\\\" height=\\\"18\\\" width=\\\"24\\\" />" +
                    "<img class=\\\"IconApproved{5} LocalIconApproved LocalPrototype\\\" baseid=\\\"{0}\\\" height=\\\"18\\\" width=\\\"24\\\" />" +
                    "<img class=\\\"IconDenial{5} LocalIconDenial LocalPrototype\\\" baseid=\\\"{0}\\\" height=\\\"18\\\" width=\\\"24\\\" />" +
                    "<img class=\\\"IconDenied{5} LocalIconDenied LocalPrototype\\\" baseid=\\\"{0}\\\" height=\\\"18\\\" width=\\\"24\\\" />" +
                    "<img class=\\\"IconUndo{5} LocalIconUndo LocalPrototype\\\" baseid=\\\"{0}\\\" height=\\\"18\\\" width=\\\"24\\\" />" +
                    "\"",
                    payout.ProtoIdentity,
                    (payout.ExpectedTransactionDate <= today
                        ? Global.Global_ASAP
                        : payout.ExpectedTransactionDate.ToShortDateString()),
                    GetReferenceOcr(payout),
                    GetAmountOcr(payout),
                    GetAccountOcr(payout),
                    payout.ProtoIdentity.Replace("|", ""));
                result.Append("},");
            }

            if (result.Length > 0)
            {
                result.Remove(result.Length - 1, 1);  // remove last comma, if there are any elements
            }

            return(result.ToString());
        }
Esempio n. 2
0
        public static PaymentTransferInfo FromObject(IHasIdentity financialObject, Money amountToPay = null)
        {
            if (financialObject is ExpenseClaim)
            {
                ExpenseClaim claim = financialObject as ExpenseClaim;

                if (amountToPay == null)
                {
                    return(FromObject(claim.Claimer, new Money(claim.AmountCents, claim.Organization.Currency)));
                }
                else
                {
                    return(FromObject(claim.Claimer, amountToPay));
                }
            }

            if (financialObject is CashAdvance)
            {
                CashAdvance advance = financialObject as CashAdvance;

                if (amountToPay == null)
                {
                    return(FromObject(advance.Person, new Money(advance.AmountCents, advance.Organization.Currency)));
                }
                else
                {
                    return(FromObject(advance.Person, amountToPay));
                }
            }

            if (financialObject is Salary)
            {
                Salary salary = financialObject as Salary;
                return(FromObject(salary.PayrollItem.Person, new Money(salary.NetSalaryCents, salary.PayrollItem.Organization.Currency)));
            }

            if (financialObject is InboundInvoice)
            {
                // TODO: Create Supplier object, read from that instead

                InboundInvoice      invoice = financialObject as InboundInvoice;
                PaymentTransferInfo result  = new PaymentTransferInfo();

                // For now, do a switch on the pay-to-account string, before the payment targets are implemented
                // on invoices and expense claims:

                result.LocalizedPaymentInformation = new Dictionary <string, string>();

                if (invoice.PayToAccount.StartsWith("IBAN"))
                {
                    result.TargetType = PaymentTargetType.InternationalBankTransfer;

                    result.LocalizedPaymentInformation[
                        Logic_Financial_PaymentTransferInfo.PaymentTargetField_Iban] =
                        invoice.PayToAccount.Substring(5);

                    // this should have bic too but we don't have that information before payment targets
                    // are properly implemented
                }
                else if (invoice.PayToAccount.StartsWith("SEBG"))
                {
                    result.TargetType = PaymentTargetType.DomesticBankGiro; // for now
                    result.LocalizedPaymentInformation[
                        Logic_Financial_PaymentTransferInfo.PaymentTargetField_GiroNumber] =
                        invoice.PayToAccount.Substring(5);

                    // Check for SEBG OCR availability

                    if (invoice.InvoiceReference.Length > 2 && Formatting.CheckLuhnChecksum(invoice.InvoiceReference) &&
                        Formatting.CheckLuhnChecksum(invoice.PayToAccount))
                    {
                        result.OcrAvailable = true;
                        result.OcrData      = new string[3];
                        result.OcrData[0]   = invoice.InvoiceReference + " #";
                        result.OcrData[1]   = string.Format("{0} {1:00}   {2} >", // three spaces between the cents and the checksum
                                                            invoice.AmountCents / 100, invoice.AmountCents % 100,
                                                            Formatting.GetLuhnChecksum(invoice.AmountCents.ToString(CultureInfo.InvariantCulture)));
                        result.OcrData[2] = Formatting.CleanNumber(invoice.PayToAccount) + "#41#";
                    }
                }
                else
                {
                    // Assume domestic giro in lack of other information

                    result.TargetType = PaymentTargetType.DomesticBankGiro;
                    result.LocalizedPaymentInformation[
                        Logic_Financial_PaymentTransferInfo.PaymentTargetField_GiroNumber] =
                        invoice.PayToAccount; // the whole string, as opposed to case above
                }

                result.LocalizedPaymentMethodName =
                    Logic_Financial_PaymentTransferInfo.ResourceManager.GetString("PaymentTargetType_" +
                                                                                  result.TargetType.ToString());
                if (invoice.HasNativeCurrency)
                {
                    result.Currency       = invoice.NativeCurrencyAmount.Currency;
                    result.CurrencyAmount = result.Currency.Code + " " +
                                            (invoice.NativeCurrencyAmount.Cents / 100.0).ToString("N2");
                }
                else
                {
                    result.Currency       = invoice.Organization.Currency;
                    result.CurrencyAmount = result.Currency.Code + " " + (invoice.AmountCents / 100.0).ToString("N2");
                }

                result.Country = null; // TODO: Set to Supplier's PaymentTarget country

                result.Recipient = invoice.Supplier;

                return(result);
            }

            if (financialObject is Person)
            {
                Person person = financialObject as Person;

                // For now, assume the bank/clearing/account triplet
                // TODO: Implement payment targets on teh Person object

                PaymentTransferInfo result = new PaymentTransferInfo();
                result.TargetType = PaymentTargetType.DomesticBankTransfer;

                // We must know the amount at this point

                if (amountToPay == null)
                {
                    throw new ArgumentNullException("amountToPay", @"Cannot determine payment transfer information without knowing origin currency");
                }

                if (string.IsNullOrEmpty(person.BankName) || string.IsNullOrEmpty(person.BankClearing) ||
                    string.IsNullOrEmpty(person.BankAccount))
                {
                    throw new NotImplementedException("Cannot provide payment transfer information for Person #" + person.Identity.ToString());
                }

                result.TargetType = PaymentTargetType.DomesticBankTransfer;
                result.Country    = person.CountryId == 0? null: person.Country;
                result.Currency   = amountToPay.Currency; // TODO: The currency will need to come from the payment method instead

                result.CurrencyAmount = result.Currency.Code + " " + (amountToPay.ToCurrency(result.Currency).Cents / 100.0).ToString("N2");

                result.LocalizedPaymentMethodName =
                    Logic_Financial_PaymentTransferInfo.ResourceManager.GetString("PaymentTargetType_" +
                                                                                  result.TargetType.ToString());
                result.Recipient = person.Canonical;

                result.LocalizedPaymentInformation = new Dictionary <string, string>();
                result.LocalizedPaymentInformation[
                    Logic_Financial_PaymentTransferInfo.PaymentTargetField_ClearingCode] =
                    person.BankName + " " + person.BankClearing;
                result.LocalizedPaymentInformation[
                    Logic_Financial_PaymentTransferInfo.PaymentTargetField_AccountNumber] = person.BankAccount;

                return(result);
            }

            throw new NotImplementedException("Unknown payment information");
        }