Changing financial type on a contribution records incorrect financial items
To replicate on master:
- Ensure sales tax is off (this is the default).
- Create a contribution however you like. Note the contact ID.
- Run
SELECT * FROM civicrm_financial_item WHERE contact_id = <contact_id>
. - Note the new financial item created as a result of your contribution. Something like example 1 below.
- Edit the contribution, change the financial type, and save.
- Run the SQL above again. You'll see something like example 2 below.
Problems
- The third entry shouldn't exist. It's attempting to record sales tax on an item that has none.
- The fourth entry has a description of "Sales Tax". It should be inheriting its description as "Contribution Amount".
The issue is this line in CRM_Contribute_BAO_Contribution::updateFinancialAccounts(). If the financial type is being changed, a sales tax entry will ALWAYS be created. I'm not sure why this is; I assume it has to do with ensuring tax is charged when switching from a non-taxable to a taxable financial type. However, it's a mistake to always generate sales tax entries, even when sales tax is completely disabled.
Example 1:
mysql> select * from civicrm_financial_item WHERE contact_id = 202 and id < 105;
+-----+---------------------+---------------------+------------+---------------------+--------+----------+----------------------+-----------+-------------------+-----------+
| id | created_date | transaction_date | contact_id | description | amount | currency | financial_account_id | status_id | entity_table | entity_id |
+-----+---------------------+---------------------+------------+---------------------+--------+----------+----------------------+-----------+-------------------+-----------+
| 104 | 2019-05-22 15:20:23 | 2019-05-22 15:20:00 | 202 | Contribution Amount | 22.00 | USD | 3 | 1 | civicrm_line_item | 104 |
+-----+---------------------+---------------------+------------+---------------------+--------+----------+----------------------+-----------+-------------------+-----------+
Example 2:
mysql> select * from civicrm_financial_item WHERE contact_id = 202;
+-----+---------------------+---------------------+------------+---------------------+--------+----------+----------------------+-----------+-------------------+-----------+
| id | created_date | transaction_date | contact_id | description | amount | currency | financial_account_id | status_id | entity_table | entity_id |
+-----+---------------------+---------------------+------------+---------------------+--------+----------+----------------------+-----------+-------------------+-----------+
| 104 | 2019-05-22 15:20:23 | 2019-05-22 15:20:00 | 202 | Contribution Amount | 22.00 | USD | 3 | 1 | civicrm_line_item | 104 |
| 105 | 2019-05-22 15:20:34 | 2019-05-22 15:20:00 | 202 | Contribution Amount | -22.00 | USD | 3 | 1 | civicrm_line_item | 104 |
| 106 | 2019-05-22 15:20:34 | 2019-05-22 15:20:00 | 202 | Sales Tax | 0.00 | USD | NULL | 1 | civicrm_line_item | 104 |
| 107 | 2019-05-22 15:20:34 | 2019-05-22 15:20:00 | 202 | Sales Tax | 22.00 | USD | 1 | 1 | civicrm_line_item | 104 |
+-----+---------------------+---------------------+------------+---------------------+--------+----------+----------------------+-----------+-------------------+-----------+