Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-8033 CiviAccounts Data Schema
  3. CRM-8425

Replace civicrm_contribution_type table with civicrm_financial_account table

    Details

    • Type: Sub-task
    • Status: Done/Fixed
    • Priority: Major
    • Resolution: Fixed/Completed
    • Affects Version/s: 4.0.0
    • Fix Version/s: 4.3.0
    • Component/s: Accounting Integration
    • Labels:
      None

      Description

      xml/schema/Contribute/ContributionType.xml has been dropped and replaced by xml/schema/Financial/FinancialAccount.xml. This issue involves making this change in all relevant places in the codebase and test suites. After this issue is completed, the system should function in all respects as at present from the perspective of a browser user.

      Tasks:

      1. Isolate the relevant changes in the v4.1.civiaccounts/xml/schema in a local dev version. Include all changes in tables that used to have foreign key references to civicrm_contribution_type.id and now refer to civicrm_financial_account.id as a foreign key.

      2. Get xml/GenCode.php to produce the schema and DAO code. (Ignore upgrade scripts at the moment.)

      3. Modify but do not eliminate api/v3/Contribution.php and api/v2/Contribution.php:

      3.1 In _civicrm_api3_contribute_format_params, do NOT replace existing references to contribution_type_id and contribution_type with references to financial_account_id and financial_account respectively.

      3.2. In civicrm_api3_contribution_create do NOT replace existing references to contribution_type_id, contribution_type with financial_account_id, financial_account.

      3.3.1 Around lines 67,142 change the key in $params from contribution_type_id to financial_account_id and at 142 return.contribution_type_id to return.financial_account_id (ensure contribution_type param is converted to contribution_type_id). After line 84, 186 change the key in $contribution from financial_account_id to contribution_type_id.

      3.3.2 At line 311, leave contribution_type_id unchanged, but on line 312 change contributionType to financialAccount.

      3.4. in api/v2/utils.v2.php _civicrm_contribute_formatted_param, leave case 'contribution_type' and $values['contribution_type_id'] but use CRM_Contribute_PseudoConstant::financialAccount instead of CRM_Contribute_PseudoConstant::contributionType.

      3.5 In ./api/v2/Contribution.php:

      3.5.1: line 280: do not change names of required parameters.

      3.5.2: lines 415,419, change contributionType to financialAccount, but leave contribution_type and contribution_type_id code in at this point.

      3.5.3: around lines 70, 118, 180 (if param present): change name of contribution_type_id key to financial_account_id and at 118 return.contribution_type_id to return.financial_account_id, and around lines 82, 129, 223 change financial_account_id back to contribution_type_id in $contribution.

      3.6.1 Rename ./tests/phpunit/api/v2/dataset/contribution_types.xml to ./tests/phpunit/api/v2/dataset/financial_accounts.xml, and ./tests/phpunit/api/v3/dataset/contribution_types.xml to ./tests/phpunit/api/v3/dataset/financial_accounts.xml.

      3.6.2 In the files in 3.6.1, add initialization of financial_account_type_id for Revenue account.

      4. Reimplement CRM/Contribute/BAO/ContributionType.php as CRM/Financial/BAO/FinancialAccount.php. Update all references. Handle new fields as indicated elsewhere in this issue.

      5. Replace references as indicated generally below, following more specifical instructions where present in the attached files.
      NB The attached files were updated July 4th to include only relevant references with line number and line text. Commands used to generate the files were similar to:
      find . -path '*/.svn' -prune -o -type f -print | xargs grep -H -n -e '\Wcontribution_type\W' | sort | uniq > contribution_type.txt

      5.1 contribution_type to financial_account in the attached contribution_type.txt file,
      5.1.1 Correction: Please do NOT make the changes suggested in the following lines in the file, (the first was an error, the others in order to provide better backwards compatibility in theming):

      ./CRM/Upgrade/Incremental/sql/3.4.beta1.mysql.tpl:19:UPDATE civicrm_uf_field SET field_name = 'contribution_type' WHERE field_name = 'contribution_type_id';

      ./templates/CRM/Contribute/Form/ContributionType.tpl:28:<div class="crm-block crm-form-block crm-contribution_type-form-block">
      ./templates/CRM/Contribute/Form/Selector.tpl:66: <td class="crm-contribution-type crm-contribution-type_{$row.contribution_type_id}">{$row.contribution_type}</td>
      ./templates/CRM/Contribute/Form/Task/Print.tpl:50: <td class="crm-contribution-type crm-contribution-{$row.contribution_type}">{$row.contribution_type}</td>
      ./templates/CRM/Pledge/Form/PledgeView.tpl:56: <tr class="crm-pledge-form-block-contribution_type"><td class="label">

      {ts}

      Contribution Type

      {/ts}

      </td><td>{$contribution_type} 

      5.2 contribution_type_id to financial_account_id in cti.txt,

      5.3 civicrm_contribution_type to civicrm_financial_account in cct.txt
      5.3.1: the schema xml files should NOT be changed

      5.4 ContributionType with FinancialAccount in ContributionType.txt.

      5.5 'contribution type' with 'financial account' in contributionSpaceType.txt.
      5.5.1 NB: Do NOT change the strings relevant to the UI in this file (ie the first section).

      NB: I have not examined the context of each reference carefully. If there are non-obvious fixes to the code, post them back here. The code around some will need to be changed to reflect field changes as in 8 below.

      6. Rename CRM/Contribute/Form/ContributionType.php as CRM/Financial/Form/FinancialAccount.php, templates/CRM/Contribute/Form/ContributionForm.tpl as templates/CRM/Financial/Form/FinancialAccount.tpl, and CRM/Contribute/BAO/ContributionType.php as CRM/Financial/BAO/FinancialAccount.php (names/references in these files to be changed as specified above).
      6.1 Rename ./tests/phpunit/CRM/Contribute/BAO/ContributionTypeTest.php to ./tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php, ./templates/CRM/Contribute/Page/ContributionType.tpl to ./templates/CRM/Financial/Page/FinancialAccount.tpl, ./CRM/Contribute/Page/ContributionType.php to ./CRM/Financial/Page/FinancialAccount.php

      7. Update contribution type field references as appropriate. All fields in 4.0 contribution_type remain with same names in 4.1 financial_account, so no deletions or updates to code are required from that. However, there are several new fields that will need support: contact_id (required, default to the , financial_account_type_id, is_header_account, is_default. Where code explicitly names all fields to be returned or to be provided to Smarty, for example, these fields should also be returned or provided to Smarty. For example:

      7.1. CRM/Financial/Form/FinancialAccount.php buildQuickForm should have form elements added for the new fields.
      7.1.1. The names of the fields to be added are: contact_id, parent_id, is_header_account, is_tax, tax_rate, is_default. Details are:

      • contact_id is a reference to a contact that is in charge of the account
      • parent_id is a reference to another account, and will normally be null
      • is_header_account means that there are children. If there are no children, ie no records with parent_id pointing to the current record, then it is still possible that is_header_account is true, just that they will be added later.
      • tax_rate is a maximum of 1. Probably best to display as a percentage 0 - 100, with up to 6 digits of accuracy after the decimal point.
      • There can only be 1 is_default true for any financial_account_type_id
        ie so each financial_account_type can have a default financial_account

      7.2. CRM/Financial/BAO/FinancialAccount.php add() should have references to appropriate fields added (appears to be just the boolean fields - not sure how name, description etc fields are stored on creation - new non-boolean fields may need to be supported here or elsewhere).

      8. In sql/civicrm_data.mysql, create an entry for financial_account_type in civicrm_option_group and for the the following financial account types in civicrm_option_value
      Label and Name the same, Description in parenthesis: Asset (Things you own), Liability (Things you owe, like a grant still to be disbursed), Revenue (Income from contributions and sales of tickets and memberships), Cost of Goods Sold (Costs incurred to get revenue, e.g. premiums for donations, dinner for a fundraising dinner ticket), Expenses (Things that are paid for that are consumable, e.g. grants disbursed)
      For all entries, values should be: is_optgroup=0, is_reserved=1, is_active=1, component_id=2, is_default=0 (except for Revenue, which has is_default=1).
      8.1. Change the INSERT INTO civicrm_financial_account statement to include a value for the financial_account_type_id based on the id for the Revenue financial account type inserted in 8.

      9. Modify the default value in xml/schema/Financial/FinancialAccount for financial_account_type_id so that it reflects the value for Revenue accounts.

      9.1 Modify the values in civicrm_data.mysql for the Donation record being inserted into civicrm_financial_account (previously civicrm_contribution_type) to make is_default true, with is_default false for the three other records.

      10. Create tests/phpunit/WebTest/Financial/FinancialAccountTest.php to ensure that Add, Update, and Delete work properly. Test all fields are properly stored and retrieved for add and update operations.

      11. Ensure the whole test suite of unit and selenium tests pass.

      12. After completing the above and debugging, create a new version of CRM/Contribute/BAO/Contribution.php with the same interface as the old one, ie support contribution_type/contribution_type_id as input and output, but converts calls into calls to the new CRM/Financial/BAO/FinancialAccount.php. Note: new fields need to be removed from results being returned from add, retrieve.

      13. Ask Deepak or others: Is there an agreed upon method for setting a default value for a field to a specific civicrm_option_value.id which is going to vary, but is the one associated with a particular group and value eg Revenue financial_account_type should be the default value for civicrm_finanacial_account.financial_account_type_id? Implement suggested method.

      Please ignore the ct.txt and CT.txt files attached to this issue. They have been replaced by other attachments, but I can't figure out how to delete them.

      Current questions for further clarification by Steering Group:
      Do we need to worry about code in .tools/drupal/modules/civicrm_giftaid_alpha or .tools/drupal/modules/civicrm_giftaid?
      Notes on excluded scope:
      Default Financial Account Types will exclude: Equity, Other Revenue and Expenses.

        Attachments

        1. cct.txt
          8 kB
          Joe Murray
        2. contribution_type.txt
          11 kB
          Joe Murray
        3. contributionSpaceType.txt
          10 kB
          Joe Murray
        4. ContributionType.txt
          2 kB
          Joe Murray
        5. ct.txt
          9 kB
          Joe Murray
        6. CT.txt
          9 kB
          Joe Murray
        7. cti.txt
          333 kB
          Joe Murray
        8. cti.txt
          8 kB
          Joe Murray

          Activity

            People

            • Assignee:
              pradeep.nayak Pradeep Nayak
              Reporter:
              joemurray Joe Murray
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 week, 4 days, 6 hours, 10 minutes
                1w 4d 6h 10m