Exemple #1
0
        private static string HandleValidationDevalidation(string identifier, ApprovalMode mode)
        {
            AuthenticationData authData = GetAuthenticationDataAndCulture();

            IValidatable validatableItem   = null;
            string       validatedTemplate = string.Empty;
            string       retractedTemplate = string.Empty;

            char   costType = identifier[0];
            int    itemId   = Int32.Parse(identifier.Substring(1));
            Int64  amountCents;
            string beneficiary = string.Empty;
            string result      = string.Empty;

            // A lot of this code was copied from attest/deattest, even though validation only concerns expense receipts

            switch (costType)
            {
            case 'E':     // Expense claim
                ExpenseClaim expense = ExpenseClaim.FromIdentity(itemId);
                if (expense.OrganizationId != authData.CurrentOrganization.Identity)
                {
                    throw new InvalidOperationException("Called to attest out-of-org line item");
                }
                if (
                    !authData.Authority.HasAccess(new Access(authData.CurrentOrganization,
                                                             AccessAspect.Financials, AccessType.Write)))
                {
                    throw new UnauthorizedAccessException();
                }

                validatableItem   = expense;
                validatedTemplate = Resources.Pages.Financial.ValidateReceipts_ReceiptsValidated;
                retractedTemplate = Resources.Pages.Financial.ValidateReceipts_ReceiptsDevalidated;
                amountCents       = expense.AmountCents;

                break;

            default:
                throw new InvalidOperationException("Unknown Cost Type in HandleValidationDevalidation: \"" +
                                                    identifier + "\"");
            }

            // Finally, attest or deattest

            if (mode == ApprovalMode.Approval)
            {
                validatableItem.Validate(authData.CurrentUser);
                result = string.Format(validatedTemplate, itemId, authData.CurrentOrganization.Currency.Code,
                                       amountCents / 100.0);
            }
            else if (mode == ApprovalMode.Retraction)
            {
                validatableItem.RetractValidation(authData.CurrentUser);
                result = string.Format(retractedTemplate, itemId, authData.CurrentOrganization.Currency.Code,
                                       amountCents / 100.0);
            }
            else
            {
                throw new InvalidOperationException("Unknown Approval Mode: " + mode);
            }

            return(result);
        }