Example #1
0
        public static ResyncResults ExecuteResync(string guid)
        {
            AuthenticationData authenticationData = GetAuthenticationDataAndCulture();

            if (
                !authenticationData.Authority.HasAccess(new Access(authenticationData.CurrentOrganization,
                                                                   AccessAspect.Bookkeeping, AccessType.Write)))
            {
                throw new UnauthorizedAccessException();
            }

            ResyncResults results = new ResyncResults();

            ExternalBankMismatchingDateTime[] mismatchArray =
                (ExternalBankMismatchingDateTime[])
                HttpContext.Current.Session["LedgersResync" + guid + "MismatchArray"];

            FinancialAccount account =
                (FinancialAccount)HttpContext.Current.Session["LedgersResync" + guid + "Account"];

            long             autoDepositDonationCents = 1000 * 100;
            FinancialAccount autoDonationAccount      = account.Organization.FinancialAccounts.IncomeDonations;

            if (authenticationData.CurrentOrganization.Identity != account.OrganizationId)
            {
                throw new InvalidOperationException("Mismatching org");
            }

            foreach (ExternalBankMismatchingDateTime mismatchDateTime in mismatchArray)
            {
                foreach (
                    ExternalBankMismatchingRecordDescription mismatchingRecord in mismatchDateTime.MismatchingRecords)
                {
                    for (int index = 0; index < mismatchingRecord.MasterCents.Length; index++)
                    {
                        results.RecordsTotal++;
                        long cents = mismatchingRecord.MasterCents[index];

                        bool unhandled = false;
                        bool handlable = true;

                        FinancialTransaction tx = mismatchingRecord.Transactions[index];

                        if (tx != null && tx.Dependency != null)
                        {
                            unhandled = true;

                            IHasIdentity dependency = tx.Dependency;

                            if (dependency is PaymentGroup &&
                                mismatchingRecord.ResyncActions[index] ==
                                ExternalBankMismatchResyncAction.RewriteSwarmops)
                            {
                                if (cents == (dependency as PaymentGroup).SumCents)
                                {
                                    // Amount checks out with dependency; rewrite requested; this is handlable on auto

                                    Dictionary <int, long> newTx = new Dictionary <int, long>();
                                    newTx[account.Identity] = cents;
                                    newTx[account.Organization.FinancialAccounts.AssetsOutboundInvoices.Identity] =
                                        -cents;
                                    tx.RecalculateTransaction(newTx, authenticationData.CurrentUser);
                                    unhandled = false;
                                }
                            }


                            handlable = false; // need to fix this
                        }


                        if (handlable)
                        {
                            switch (mismatchingRecord.ResyncActions[index])
                            {
                            case ExternalBankMismatchResyncAction.DeleteSwarmops:
                                if (tx == null)
                                {
                                    throw new InvalidOperationException(
                                              "Can't have Delete op on a null transaction");
                                }

                                tx.Description = tx.Description + " (killed/zeroed in resync)";
                                tx.RecalculateTransaction(new Dictionary <int, long>(),
                                                          authenticationData.CurrentUser); // zeroes out
                                break;

                            case ExternalBankMismatchResyncAction.RewriteSwarmops:
                                if (tx == null)
                                {
                                    throw new InvalidOperationException(
                                              "Can't have Rewrite op on a null transaction");
                                }

                                Dictionary <int, long> newTx = new Dictionary <int, long>();
                                newTx[account.Identity] = cents;
                                if (cents > 0 && cents < autoDepositDonationCents)
                                {
                                    newTx[autoDonationAccount.Identity] = -cents;     // negative; P&L account
                                }
                                tx.RecalculateTransaction(newTx, authenticationData.CurrentUser);
                                break;

                            case ExternalBankMismatchResyncAction.CreateSwarmops:
                                if (tx != null)
                                {
                                    throw new InvalidOperationException("Transaction seems to already exist");
                                }

                                tx = FinancialTransaction.Create(account.OwnerPersonId, mismatchDateTime.DateTime,
                                                                 mismatchingRecord.Description);
                                tx.AddRow(account, cents, authenticationData.CurrentUser);

                                if (cents > 0 && cents < autoDepositDonationCents)
                                {
                                    tx.AddRow(autoDonationAccount, -cents, authenticationData.CurrentUser);
                                }
                                break;

                            case ExternalBankMismatchResyncAction.NoAction:
                                // no action
                                break;

                            default:
                                // not handled
                                unhandled = true;
                                break;
                            }
                        }

                        if (unhandled)
                        {
                            results.RecordsFail++;
                        }
                        else
                        {
                            results.RecordsSuccess++;
                        }
                    }
                }
            }

            return(results);
        }
        public static ResyncResults ExecuteResync (string guid)
        {
            AuthenticationData authenticationData = GetAuthenticationDataAndCulture();

            if (!authenticationData.CurrentUser.HasAccess(new Access(authenticationData.CurrentOrganization, AccessAspect.Bookkeeping, AccessType.Write)))
            {
                throw new UnauthorizedAccessException();
            }

            ResyncResults results = new ResyncResults();

            ExternalBankMismatchingDateTime[] mismatchArray =
                (ExternalBankMismatchingDateTime[]) HttpContext.Current.Session["LedgersResync" + guid + "MismatchArray"];

            FinancialAccount account = 
                (FinancialAccount) HttpContext.Current.Session["LedgersResync" + guid + "Account"];

            long autoDepositDonationCents = 1000 * 100;
            FinancialAccount autoDonationAccount = account.Organization.FinancialAccounts.IncomeDonations;

            if (authenticationData.CurrentOrganization.Identity != account.OrganizationId)
            {
                throw new InvalidOperationException("Mismatching org");
            }

            foreach (ExternalBankMismatchingDateTime mismatchDateTime in mismatchArray)
            {
                foreach (ExternalBankMismatchingRecordDescription mismatchingRecord in mismatchDateTime.MismatchingRecords)
                {
                    for (int index = 0; index < mismatchingRecord.MasterCents.Length; index++)
                    {
                        results.RecordsTotal++;
                        long cents = mismatchingRecord.MasterCents[index];

                        bool unhandled = false;
                        bool handlable = true;

                        FinancialTransaction tx = mismatchingRecord.Transactions[index];

                        if (tx != null && tx.Dependency != null)
                        {
                            unhandled = true;

                            IHasIdentity dependency = tx.Dependency as IHasIdentity;

                            if (dependency is PaymentGroup && mismatchingRecord.ResyncActions[index] == ExternalBankMismatchResyncAction.RewriteSwarmops)
                            {
                                if (cents == (dependency as PaymentGroup).SumCents)
                                {
                                    // Amount checks out with dependency; rewrite requested; this is handlable on auto

                                    Dictionary<int, long> newTx = new Dictionary<int, long>();
                                    newTx[account.Identity] = cents;
                                    newTx[account.Organization.FinancialAccounts.AssetsOutboundInvoices.Identity] =
                                        -cents;
                                    tx.RecalculateTransaction(newTx, authenticationData.CurrentUser);
                                    unhandled = false;
                                }
                            }


                            handlable = false; // need to fix this
                        }


                        if (handlable) switch (mismatchingRecord.ResyncActions[index])
                        {
                            case ExternalBankMismatchResyncAction.DeleteSwarmops:
                                if (tx == null)
                                {
                                    throw new InvalidOperationException("Can't have Delete op on a null transaction");
                                }

                                tx.Description = tx.Description + " (killed/zeroed in resync)";
                                tx.RecalculateTransaction(new Dictionary<int, long>(), authenticationData.CurrentUser);  // zeroes out
                                break;
                            case ExternalBankMismatchResyncAction.RewriteSwarmops:
                                if (tx == null)
                                {
                                    throw new InvalidOperationException("Can't have Rewrite op on a null transaction");
                                }

                                Dictionary<int, long> newTx = new Dictionary<int, long>();
                                newTx[account.Identity] = cents;
                                if (cents > 0 && cents < autoDepositDonationCents)
                                {
                                    newTx[autoDonationAccount.Identity] = -cents; // negative; P&L account
                                }
                                tx.RecalculateTransaction(newTx, authenticationData.CurrentUser);
                                break;
                            case ExternalBankMismatchResyncAction.CreateSwarmops:
                                if (tx != null)
                                {
                                    throw new InvalidOperationException("Transaction seems to already exist");
                                }

                                tx = FinancialTransaction.Create(account.OwnerPersonId, mismatchDateTime.DateTime,
                                                                 mismatchingRecord.Description);
                                tx.AddRow(account, cents, authenticationData.CurrentUser);

                                if (cents > 0 && cents < autoDepositDonationCents)
                                {
                                    tx.AddRow(autoDonationAccount, -cents, authenticationData.CurrentUser);
                                }
                                break;
                            case ExternalBankMismatchResyncAction.NoAction:
                                // no action
                                break;
                            default:
                                // not handled
                                unhandled = true;
                                break;
                        }

                        if (unhandled)
                        {
                            results.RecordsFail++;
                        }
                        else
                        {
                            results.RecordsSuccess++;
                        }
                    }
                }
            }

            return results;
        }