Merge pull request #4391 from seamuslee001/master
[civicrm-core.git] / CRM / Financial / Form / FinancialAccount.php
index a04b33276fdbd33cfd06eba40e5918cdcc4d2d42..938107994d43b6299eaf22c9d194525fc40e47f3 100644 (file)
@@ -68,12 +68,7 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
         $this->_isARFlag = TRUE;
         if ($this->_action & CRM_Core_Action::DELETE) {
           $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).");
-          if (CRM_Utils_Array::value('snippet', $_REQUEST) == 'json') {
-            CRM_Core_Page_AJAX::returnJsonResponse($msg);
-          }
-          CRM_Core_Session::setStatus($msg, '', 'error');
-          CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount',
-            "reset=1&action=browse"));
+          CRM_Core_Error::statusBounce($msg);
         }
       }
     }
@@ -87,6 +82,7 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
    */
   public function buildQuickForm( ) {
     parent::buildQuickForm( );
+    $this->setPageTitle(ts('Financial Account'));
 
     if ($this->_action & CRM_Core_Action::DELETE) {
       return;
@@ -116,7 +112,7 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
     $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
     if (!empty($financialAccountType)) {
       $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
-        array('' => '- select -') + $financialAccountType, TRUE);
+        array('' => '- select -') + $financialAccountType, TRUE, array('class' => 'crm-select2 huge'));
       if ($this->_isARFlag) {
         $element->freeze();
         $elementAccounting->freeze();
@@ -146,11 +142,33 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
    */
   static function formRule( $values, $files, $self ) {
     $errorMsg = array( );
-    if (!empty( $values['tax_rate'])) {
-      if ($values['tax_rate'] <= 0 || $values['tax_rate'] > 100) {
+    $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' "));
+    if (isset($values['is_tax'])) {
+      if ($values['financial_account_type_id'] !=  $financialAccountTypeId) {
+        $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');
+      }
+    }
+    if ((CRM_Utils_Array::value('tax_rate', $values) != NULL))  {
+      if ($values['tax_rate'] < 0 || $values['tax_rate'] >= 100) {
         $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
       }
     }
+    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' "));
+        $params = array(
+          'financial_account_id'  => $self->_id,
+          'account_relationship'=> $relationshipId
+        );
+        $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
+        if ($result) {
+          $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
+        }
+      }
+    }
     return CRM_Utils_Array::crmIsEmptyArray( $errorMsg ) ? true : $errorMsg;
   }
 
@@ -194,4 +212,5 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
       CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name)));
     }
   }
+
 }