Merge pull request #23959 from totten/master-casexml-acttype
[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 * @throws \CiviCRM_API3_Exception
29 */
30 public static function onToggle($oldValue, $newValue, $metadata) {
31 if ($oldValue == $newValue) {
32 return;
33 }
34 $existingUserViewOptions = civicrm_api3('Setting', 'get', ['return' => 'user_dashboard_options'])['values'][CRM_Core_Config::domainID()]['user_dashboard_options'];
35 $optionValues = civicrm_api3('Setting', 'getoptions', ['field' => 'user_dashboard_options'])['values'];
36 $invoiceKey = array_search('Invoices / Credit Notes', $optionValues);
37 $existingIndex = array_search($invoiceKey, $existingUserViewOptions);
38
39 if ($newValue && $existingIndex === FALSE) {
40 $existingUserViewOptions[] = $invoiceKey;
41 }
42 elseif (!$newValue && $existingIndex !== FALSE) {
43 unset($existingUserViewOptions[$existingIndex]);
44 }
45 civicrm_api3('Setting', 'create', ['user_dashboard_options' => $existingUserViewOptions]);
46 }
47
48 /**
49 * Function to call to determine if invoicing is enabled.
50 *
51 * Historically the invoicing was declared as a setting but actually
52 * set within contribution_invoice_settings (which stores multiple settings
53 * as an array in a non-standard way).
54 *
55 * We check both here. But will deprecate the latter in time.
56 */
57 public static function isInvoicingEnabled() {
58 return Civi::settings()->get('invoicing');
59 }
60
61 /**
62 * Function to get the tax term.
63 *
64 * The value is nested in the contribution_invoice_settings setting - which
65 * is unsupported. Here we have a wrapper function to make later cleanup easier.
66 */
67 public static function getTaxTerm() {
68 return Civi::settings()->get('tax_term');
69 }
70
71 }