FinancialAccount.php 8.65 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34 35 36 37 38
 */

/**
 * This class generates form components for Financial Account
 */
class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {

Pradeep Nayak's avatar
Pradeep Nayak committed
39
  /**
40
   * Flag if its a AR account type.
Pradeep Nayak's avatar
Pradeep Nayak committed
41 42 43 44
   *
   * @var boolean
   */
  protected $_isARFlag = FALSE;
eileen's avatar
eileen committed
45

Pradeep Nayak's avatar
Pradeep Nayak committed
46
  /**
47
   * Set variables up before form is built.
Pradeep Nayak's avatar
Pradeep Nayak committed
48 49 50
   */
  public function preProcess() {
    parent::preProcess();
eileen's avatar
eileen committed
51

Pradeep Nayak's avatar
Pradeep Nayak committed
52
    if ($this->_id) {
53
      $params = [
Pradeep Nayak's avatar
Pradeep Nayak committed
54
        'id' => $this->_id,
55
      ];
Pradeep Nayak's avatar
Pradeep Nayak committed
56
      $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, CRM_Core_DAO::$_nullArray);
57 58
      $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
      if ($financialAccount->financial_account_type_id == $financialAccountTypeId
eileen's avatar
eileen committed
59
        && strtolower($financialAccount->account_type_code) == 'ar'
60
        && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, $financialAccountTypeId)
totten's avatar
totten committed
61
      ) {
Pradeep Nayak's avatar
Pradeep Nayak committed
62 63
        $this->_isARFlag = TRUE;
        if ($this->_action & CRM_Core_Action::DELETE) {
64
          $msg = ts("The selected financial account cannot be deleted because at least one Accounts Receivable type account is required (to ensure that accounting transactions are in balance).");
65
          CRM_Core_Error::statusBounce($msg);
Pradeep Nayak's avatar
Pradeep Nayak committed
66 67 68 69 70
        }
      }
    }
  }

totten's avatar
totten committed
71
  /**
72
   * Build the form object.
totten's avatar
totten committed
73
   */
totten's avatar
totten committed
74
  public function buildQuickForm() {
75
    parent::buildQuickForm();
76
    $this->setPageTitle(ts('Financial Account'));
totten's avatar
totten committed
77 78 79 80

    if ($this->_action & CRM_Core_Action::DELETE) {
      return;
    }
81

totten's avatar
totten committed
82 83
    $this->applyFilter('__ALL__', 'trim');
    $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount');
Pradeep Nayak's avatar
Pradeep Nayak committed
84
    $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
totten's avatar
totten committed
85
    $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
86
      'objectExists', ['CRM_Financial_DAO_FinancialAccount', $this->_id]);
87

totten's avatar
totten committed
88 89
    $this->add('text', 'description', ts('Description'), $attributes['description']);
    $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
Pradeep Nayak's avatar
Pradeep Nayak committed
90
    $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
91 92
    $this->addEntityRef('contact_id', ts('Owner'), [
      'api' => ['params' => ['contact_type' => 'Organization']],
totten's avatar
totten committed
93
      'create' => TRUE,
94
    ]);
totten's avatar
totten committed
95 96
    $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
    $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
Pradeep Nayak's avatar
Pradeep Nayak committed
97
    $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
totten's avatar
totten committed
98
    $this->add('checkbox', 'is_tax', ts('Is Tax?'));
99

Pradeep Nayak's avatar
Pradeep Nayak committed
100 101 102 103 104
    $element = $this->add('checkbox', 'is_default', ts('Default?'));
    // CRM-12470 freeze is default if is_default is set
    if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) {
      $element->freeze();
    }
105

106
    $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
totten's avatar
totten committed
107
    if (!empty($financialAccountType)) {
Pradeep Nayak's avatar
Pradeep Nayak committed
108
      $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
109
        ['' => '- select -'] + $financialAccountType, TRUE, ['class' => 'crm-select2 huge']);
Pradeep Nayak's avatar
Pradeep Nayak committed
110 111 112
      if ($this->_isARFlag) {
        $element->freeze();
        $elementAccounting->freeze();
Pradeep Nayak's avatar
Pradeep Nayak committed
113
        $elementActive->freeze();
Pradeep Nayak's avatar
Pradeep Nayak committed
114
      }
115 116 117
      elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) {
        $element->freeze();
      }
totten's avatar
totten committed
118
    }
119

totten's avatar
totten committed
120
    if ($this->_action == CRM_Core_Action::UPDATE &&
totten's avatar
totten committed
121 122
      CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')
    ) {
123
      $this->freeze(['name', 'description', 'is_active']);
totten's avatar
totten committed
124
    }
125
    $this->addFormRule(['CRM_Financial_Form_FinancialAccount', 'formRule'], $this);
totten's avatar
totten committed
126
  }
127

totten's avatar
totten committed
128
  /**
129
   * Global validation rules for the form.
totten's avatar
totten committed
130
   *
131 132
   * @param array $values
   *   posted values of the form
Eileen McNaughton's avatar
Eileen McNaughton committed
133 134 135
   * @param $files
   * @param $self
   *
136 137
   * @return array
   *   list of errors to be posted back to the form
totten's avatar
totten committed
138
   */
totten's avatar
totten committed
139
  public static function formRule($values, $files, $self) {
140
    $errorMsg = [];
141 142
    $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' "));
    if (isset($values['is_tax'])) {
totten's avatar
totten committed
143
      if ($values['financial_account_type_id'] != $financialAccountTypeId) {
144 145 146 147 148 149
        $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.');
      }
      if (CRM_Utils_Array::value('tax_rate', $values) == NULL) {
        $errorMsg['tax_rate'] = ts('Please enter value for tax rate');
      }
    }
150
    if ((CRM_Utils_Array::value('tax_rate', $values) != NULL)) {
151
      if ($values['tax_rate'] < 0 || $values['tax_rate'] >= 100) {
totten's avatar
totten committed
152 153 154
        $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
      }
    }
155 156 157
    if ($self->_action & CRM_Core_Action::UPDATE) {
      if (!(isset($values['is_tax']))) {
        $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
158
        $params = [
totten's avatar
totten committed
159
          'financial_account_id' => $self->_id,
160
          'account_relationship' => $relationshipId,
161
        ];
162
        $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
163 164 165 166 167
        if ($result) {
          $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
        }
      }
    }
168
    return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
totten's avatar
totten committed
169
  }
170

totten's avatar
totten committed
171
  /**
172
   * Set default values for the form.
173
   * the default values are retrieved from the database.
totten's avatar
totten committed
174
   */
175
  public function setDefaultValues() {
totten's avatar
totten committed
176 177 178 179 180 181
    $defaults = parent::setDefaultValues();
    if ($this->_action & CRM_Core_Action::ADD) {
      $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
    }
    return $defaults;
  }
182

totten's avatar
totten committed
183
  /**
184
   * Process the form submission.
totten's avatar
totten committed
185 186 187
   */
  public function postProcess() {
    if ($this->_action & CRM_Core_Action::DELETE) {
188 189 190 191 192 193
      if (CRM_Financial_BAO_FinancialAccount::del($this->_id)) {
        CRM_Core_Session::setStatus(ts('Selected Financial Account has been deleted.'));
      }
      else {
        CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
      }
totten's avatar
totten committed
194 195 196 197
    }
    else {
      // store the submitted values in an array
      $params = $this->exportValues();
198

totten's avatar
totten committed
199
      if ($this->_action & CRM_Core_Action::UPDATE) {
200
        $params['id'] = $this->_id;
totten's avatar
totten committed
201
      }
202 203 204 205 206 207 208 209
      foreach ([
        'is_active',
        'is_deductible',
        'is_tax',
        'is_default',
      ] as $field) {
        $params[$field] = CRM_Utils_Array::value($field, $params, FALSE);
      }
210
      $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
211
      CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', [1 => $financialAccount->name]), ts('Saved'), 'success');
totten's avatar
totten committed
212 213
    }
  }
214

totten's avatar
totten committed
215
}