Merge pull request #4054 from eileenmcnaughton/CRM-15237
[civicrm-core.git] / CRM / Upgrade / Incremental / sql / 4.3.6.mysql.tpl
CommitLineData
e993b6e5 1{* file to handle db changes in 4.3.6 during upgrade *}
157b21d8
PN
2-- CRM-13060
3UPDATE civicrm_price_set_entity cpse
4LEFT JOIN civicrm_price_set cps ON cps.id = cpse.price_set_id
5LEFT JOIN civicrm_price_field cpf ON cps.id = cpf.price_set_id
6LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
7LEFT JOIN civicrm_event ce ON cpse.entity_id = ce.id AND cpse.entity_table = 'civicrm_event'
8LEFT JOIN civicrm_contribution_page ccg ON cpse.entity_id = ccg.id AND cpse.entity_table = 'civicrm_contribution_page'
9SET cpfv.financial_type_id = CASE
cc9b58f3 10 WHEN ce.id IS NOT NULL
157b21d8 11 THEN ce.financial_type_id
cc9b58f3
PN
12 WHEN ccg.id IS NOT NULL
13 THEN ccg.financial_type_id
157b21d8
PN
14END,
15cps.financial_type_id = CASE
16 WHEN ce.id IS NOT NULL
17 THEN ce.financial_type_id
18 WHEN ccg.id IS NOT NULL
19 THEN ccg.financial_type_id
20END
bf45dbe8
PN
21WHERE cps.is_quick_config = 1;
22
23-- CRM-12844
24-- DELETE bad data
25DELETE cli FROM `civicrm_contribution` cc
26LEFT JOIN civicrm_line_item cli ON cli.entity_id = cc.id
27LEFT JOIN civicrm_financial_item cfi ON cfi.entity_id = cli.id AND cfi.entity_table = 'civicrm_line_item'
28LEFT JOIN civicrm_price_field cpf ON cpf.id = cli.price_field_id
29LEFT JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id
30WHERE cc.contribution_recur_id IS NOT NULL
31AND cli.entity_table = 'civicrm_contribution' AND cfi.id IS NULL
32AND cps.is_quick_config = 1;
33
34-- Set from_financial_account_id to null
35UPDATE `civicrm_contribution` cc
36LEFT JOIN civicrm_entity_financial_trxn ceft ON ceft.entity_id = cc.id
37LEFT JOIN civicrm_financial_trxn cft ON cft.id = ceft.financial_trxn_id
38LEFT JOIN civicrm_entity_financial_trxn ceft1 ON ceft1.financial_trxn_id = ceft.financial_trxn_id
39LEFT JOIN civicrm_financial_item cfi ON cfi.id = ceft1.entity_id
40LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.payment_processor_id
41SET cft.from_financial_account_id = NULL
42WHERE ceft.entity_table = 'civicrm_contribution' AND cc.contribution_recur_id IS NOT NULL
43AND ceft1.entity_table = 'civicrm_financial_item' AND cft.id IS NOT NULL AND cft.payment_instrument_id = 1 AND cfi.entity_table = 'civicrm_line_item' AND cft.from_financial_account_id IS NOT NULL
44AND cefa.entity_table = 'civicrm_payment_processor' AND cefa.financial_account_id = cft.to_financial_account_id;
948e62a4 45
46-- CRM-13096
47DROP TABLE IF EXISTS civicrm_official_receipt;
3b67ab13
PN
48
49-- CRM-13231
50SELECT @option_group_id_arel := max(id) from civicrm_option_group where name = 'account_relationship';
51SELECT @option_group_id_fat := max(id) from civicrm_option_group where name = 'financial_account_type';
52SELECT @opexp := value FROM civicrm_option_value WHERE name = 'Expenses' and option_group_id = @option_group_id_fat;
53SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opexp;
87f4e761 54SELECT @domainContactId := contact_id from civicrm_domain where id = {$domainID};
3b67ab13
PN
55
56SELECT @option_value_rel_id_exp := value FROM civicrm_option_value WHERE option_group_id = @option_group_id_arel AND name = 'Expense Account is';
57
58INSERT IGNORE INTO civicrm_financial_account (id, name, contact_id, financial_account_type_id, description, account_type_code, accounting_code, is_active, is_default)
59VALUES (@financialAccountId, 'Banking Fees', @domainContactId, @opexp, 'Payment processor fees and manually recorded banking fees', 'EXP', '5200', 1, 1);
60
61SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opexp;
62
63INSERT INTO civicrm_entity_financial_account(entity_table, entity_id, account_relationship, financial_account_id)
64SELECT 'civicrm_financial_type', cft.id, @option_value_rel_id_exp, @financialAccountId
65FROM civicrm_financial_type cft
66LEFT JOIN civicrm_entity_financial_account ceft
67ON ceft.entity_id = cft.id AND ceft.account_relationship = @option_value_rel_id_exp AND ceft.entity_table = 'civicrm_financial_type'
68WHERE ceft.entity_id IS NULL;
69
70UPDATE civicrm_financial_trxn cft
71INNER JOIN civicrm_entity_financial_trxn ceft ON ceft.financial_trxn_id = cft .id
72INNER JOIN civicrm_entity_financial_trxn ceft1 ON ceft1.financial_trxn_id = cft .id
73INNER JOIN civicrm_financial_item cfi ON cfi.id = ceft1.entity_id
74INNER JOIN civicrm_contribution cc ON cc.id = ceft.entity_id
75INNER JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cc.financial_type_id
76SET cft.to_financial_account_id = cefa.financial_account_id
87f4e761
PN
77WHERE ceft.entity_table = 'civicrm_contribution' AND ceft1.entity_table = 'civicrm_financial_item' AND cfi.entity_table = 'civicrm_financial_trxn' AND cft.to_financial_account_id IS NULL AND cefa.entity_table = 'civicrm_financial_type' AND cefa.account_relationship = @option_value_rel_id_exp;
78
79-- Add COGS account relationship
80SELECT @option_value_rel_id_cg := value FROM civicrm_option_value WHERE option_group_id = @option_group_id_arel AND name = 'Cost of Sales Account is';
81SELECT @opCost := value FROM civicrm_option_value WHERE name = 'Cost of Sales' and option_group_id = @option_group_id_fat;
82SET @financialAccountId := '';
83SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opCost;
84
85-- CRM-13231
86INSERT IGNORE INTO civicrm_financial_account (id, name, contact_id, financial_account_type_id, description, account_type_code, accounting_code, is_active, is_default)
87VALUES (@financialAccountId, 'Premiums', @domainContactId, @opCost, 'Account to record cost of premiums provided to payors', 'COGS', '5100', 1, 1);
88
89SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opCost;
90
91INSERT INTO civicrm_entity_financial_account(entity_table, entity_id, account_relationship, financial_account_id)
92SELECT 'civicrm_financial_type', cft.id, @option_value_rel_id_cg, @financialAccountId
93FROM civicrm_financial_type cft
94LEFT JOIN civicrm_entity_financial_account ceft
95ON ceft.entity_id = cft.id AND ceft.account_relationship = @option_value_rel_id_cg AND ceft.entity_table = 'civicrm_financial_type'
96WHERE ceft.entity_id IS NULL;