Merge pull request #15901 from eileenmcnaughton/matt
[civicrm-core.git] / CRM / Invoicing / Utils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Invoicing_Utils {
18
19 /**
20 * Function to call when invoicing is toggled on or off.
21 *
22 * We add or remove invoicing from the user dashboard here.
23 *
24 * @param bool $oldValue
25 * @param bool $newValue
26 * @param array $metadata
27 */
28 public static function onToggle($oldValue, $newValue, $metadata) {
29 if ($oldValue == $newValue) {
30 return;
31 }
32 $existingUserViewOptions = civicrm_api3('Setting', 'get', ['return' => 'user_dashboard_options'])['values'][CRM_Core_Config::domainID()]['user_dashboard_options'];
33 $optionValues = civicrm_api3('Setting', 'getoptions', ['field' => 'user_dashboard_options'])['values'];
34 $invoiceKey = array_search('Invoices / Credit Notes', $optionValues);
35 $existingIndex = in_array($invoiceKey, $existingUserViewOptions);
36
37 if ($newValue && $existingIndex === FALSE) {
38 $existingUserViewOptions[] = $invoiceKey;
39 }
40 elseif (!$newValue && $existingIndex !== FALSE) {
41 unset($existingUserViewOptions[$existingIndex]);
42 }
43 civicrm_api3('Setting', 'create', ['user_dashboard_options' => $existingUserViewOptions]);
44 }
45
46 /**
47 * Function to call to determine if invoicing is enabled.
48 *
49 * Historically the invoicing was declared as a setting but actually
50 * set within contribution_invoice_settings (which stores multiple settings
51 * as an array in a non-standard way).
52 *
53 * We check both here. But will deprecate the latter in time.
54 */
55 public static function isInvoicingEnabled() {
56 if (Civi::settings()->get('invoicing')) {
57 return TRUE;
58 }
59 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
60 return CRM_Utils_Array::value('invoicing', $invoiceSettings);
61 }
62
63 /**
64 * Function to call to determine default invoice page.
65 *
66 * Historically the invoicing was declared as a setting but actually
67 * set within contribution_invoice_settings (which stores multiple settings
68 * as an array in a non-standard way).
69 *
70 * We check both here. But will deprecate the latter in time.
71 */
72 public static function getDefaultPaymentPage() {
73 $value = Civi::settings()->get('default_invoice_page');
74 if (is_numeric($value)) {
75 return $value;
76 }
77 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
78 return CRM_Utils_Array::value('default_invoice_page', $invoiceSettings);
79 }
80
81 /**
82 * Function to get the tax term.
83 *
84 * The value is nested in the contribution_invoice_settings setting - which
85 * is unsupported. Here we have a wrapper function to make later cleanup easier.
86 */
87 public static function getTaxTerm() {
88 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
89 return CRM_Utils_Array::value('tax_term', $invoiceSettings);
90 }
91
92 }