Added code to support custom field for Financial Type
authorPradeep Nayak <pradpnayak@gmail.com>
Mon, 23 Jul 2018 13:25:43 +0000 (14:25 +0100)
committerPradeep Nayak <pradpnayak@gmail.com>
Mon, 23 Jul 2018 13:25:43 +0000 (14:25 +0100)
CRM/Financial/DAO/FinancialType.php
CRM/Financial/Form/FinancialType.php
templates/CRM/Financial/Form/FinancialType.tpl
xml/schema/Financial/FinancialType.xml

index 70bd24234a2e4d100fed544754e00266eb0e1ab7..8f805cb8c3d62db4ddff77c3215e1a7d3b8d56ee 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Financial/FinancialType.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:237f3a0cb50c930dd33a9f957677245b)
+ * (GenCodeChecksum:6d85bc0675253407de19ac9226ba4478)
  */
 
 /**
@@ -114,6 +114,10 @@ class CRM_Financial_DAO_FinancialType extends CRM_Core_DAO {
           'entity' => 'FinancialType',
           'bao' => 'CRM_Financial_BAO_FinancialType',
           'localizable' => 0,
+          'html' => [
+            'type' => 'Text',
+            'label' => ts("Name"),
+          ],
           'pseudoconstant' => [
             'table' => 'civicrm_financial_type',
             'keyColumn' => 'id',
@@ -131,6 +135,10 @@ class CRM_Financial_DAO_FinancialType extends CRM_Core_DAO {
           'entity' => 'FinancialType',
           'bao' => 'CRM_Financial_BAO_FinancialType',
           'localizable' => 0,
+          'html' => [
+            'type' => 'TextArea',
+            'label' => ts("Description"),
+          ],
         ],
         'is_deductible' => [
           'name' => 'is_deductible',
@@ -142,6 +150,10 @@ class CRM_Financial_DAO_FinancialType extends CRM_Core_DAO {
           'entity' => 'FinancialType',
           'bao' => 'CRM_Financial_BAO_FinancialType',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+            'label' => ts("Tax-Deductible?"),
+          ],
         ],
         'is_reserved' => [
           'name' => 'is_reserved',
@@ -152,6 +164,10 @@ class CRM_Financial_DAO_FinancialType extends CRM_Core_DAO {
           'entity' => 'FinancialType',
           'bao' => 'CRM_Financial_BAO_FinancialType',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+            'label' => ts("Reserved?"),
+          ],
         ],
         'is_active' => [
           'name' => 'is_active',
@@ -162,6 +178,10 @@ class CRM_Financial_DAO_FinancialType extends CRM_Core_DAO {
           'entity' => 'FinancialType',
           'bao' => 'CRM_Financial_BAO_FinancialType',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+            'label' => ts("Enabled?"),
+          ],
         ],
       ];
       CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
index 10a30c7f2480a13f185b2298f1de9d0e06e43627..3600ef65473562eb458f52dfcb0444ea6e4770fd 100644 (file)
  */
 class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form {
 
+  use CRM_Core_Form_EntityFormTrait;
+
+  /**
+   * Fields for the entity to be assigned to the template.
+   *
+   * @var array
+   */
+  protected $entityFields = [];
+
+  /**
+   * Deletion message to be assigned to the form.
+   *
+   * @var string
+   */
+  protected $deleteMessage;
+
   /**
    * Set variables up before form is built.
    */
@@ -46,40 +62,67 @@ class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form {
     ) {
       CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
     }
+    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
     parent::preProcess();
+    $this->setPageTitle(ts('Financial Type'));
+    if ($this->_id) {
+      $this->_title = CRM_Core_PseudoConstant::getLabel(
+        'CRM_Financial_BAO_FinancialType',
+        'financial_type',
+        $this->_id
+      );
+      $this->assign('aid', $this->_id);
+    }
+  }
+
+  /**
+   * Set entity fields to be assigned to the form.
+   */
+  protected function setEntityFields() {
+    $this->entityFields = [
+      'name' => [
+        'name' => 'name',
+        'required' => TRUE,
+      ],
+      'description' => ['name' => 'description'],
+      'is_deductible' => [
+        'name' => 'is_deductible',
+        'description' => ts('Are contributions of this type tax-deductible?'),
+      ],
+      'is_reserved' => ['name' => 'is_reserved'],
+      'is_active' => ['name' => 'is_active'],
+    ];
+  }
+
+  /**
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'FinancialType';
+  }
+
+  /**
+   * Set the delete message.
+   *
+   * We do this from the constructor in order to do a translation.
+   */
+  public function setDeleteMessage() {
+    $this->deleteMessage = ts('WARNING: You cannot delete a financial type if it is currently used by any Contributions, Contribution Pages or Membership Types. Consider disabling this option instead.') . ts('Deleting a financial type cannot be undone.') . ts('Do you want to continue?');
   }
 
   /**
    * Build the form object.
    */
   public function buildQuickForm() {
-    parent::buildQuickForm();
-    $this->setPageTitle(ts('Financial Type'));
-
-    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
-    if ($this->_id) {
-      $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'name');
-    }
+    self::buildQuickEntityForm();
     if ($this->_action & CRM_Core_Action::DELETE) {
       return;
     }
-    $this->applyFilter('__ALL__', 'trim');
-    $this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'name'), TRUE);
-
-    $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'description'));
-
-    $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_deductible'));
-    $this->add('checkbox', 'is_active', ts('Enabled?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_active'));
-    $this->add('checkbox', 'is_reserved', ts('Reserved?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_reserved'));
-    if ($this->_action == CRM_Core_Action::UPDATE) {
-      $this->assign('aid', $this->_id);
-    }
-    if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved', 'vid')) {
-      $this->freeze(array('is_active'));
+    if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved')) {
+      $this->freeze(['is_active']);
     }
-
     $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists',
-      array('CRM_Financial_DAO_FinancialType', $this->_id)
+      ['CRM_Financial_DAO_FinancialType', $this->_id]
     );
   }
 
@@ -95,35 +138,46 @@ class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form {
       CRM_Core_Session::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success');
     }
     else {
-      $params = $ids = array();
       // store the submitted values in an array
       $params = $this->exportValues();
-
-      if ($this->_action & CRM_Core_Action::UPDATE) {
-        $ids['financialType'] = $this->_id;
+      if ($this->_id) {
+        $params['id'] = $this->_id;
       }
-
-      $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
+      foreach ([
+        'is_active',
+        'is_reserved',
+        'is_deductible',
+      ] as $field) {
+        $params[$field] = CRM_Utils_Array::value($field, $params, FALSE);
+      }
+      $financialType = civicrm_api3('FinancialType', 'create', $params);
       if ($this->_action & CRM_Core_Action::UPDATE) {
         $url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1&action=browse');
-        CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', array(1 => $financialType->name)), ts('Saved'), 'success');
+        CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', [1 => $params['name']]), ts('Saved'), 'success');
       }
       else {
-        $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType->id);
-        $statusArray = array(
-          1 => $financialType->name,
-          2 => $financialType->name,
-          3 => CRM_Utils_Array::value(0, $financialType->titles),
-          4 => CRM_Utils_Array::value(1, $financialType->titles),
-          5 => CRM_Utils_Array::value(2, $financialType->titles),
-        );
-        if (empty($financialType->titles)) {
-          $text = ts('Your Financial "%1" Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.', $statusArray);
+        $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType['id']);
+
+        $statusArray = [
+          1 => $params['name'],
+        ];
+        $financialAccounts = civicrm_api3('EntityFinancialAccount', 'get', [
+          'return' => ["financial_account_id.name"],
+          'entity_table' => "civicrm_financial_type",
+          'entity_id' => $financialType['id'],
+          'options' => ['sort' => "id"],
+          'account_relationship' => ['!=' => "Income Account is"],
+        ]);
+        if (!empty($financialAccounts['values'])) {
+          foreach ($financialAccounts['values'] as $financialAccount) {
+            $statusArray[] = $financialAccount['financial_account_id.name'];
+          }
+          $text = ts('Your Financial "%1" Type has been created, along with a corresponding income account "%1". That income account, along with standard financial accounts "%2", "%3" and "%4" have been linked to the financial type. You may edit or replace those relationships here.', $statusArray);
         }
         else {
-          $text = ts('Your Financial "%1" Type has been created, along with a corresponding income account "%2". That income account, along with standard financial accounts "%3", "%4" and "%5" have been linked to the financial type. You may edit or replace those relationships here.', $statusArray);
+          $text = ts('Your Financial "%1" Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.', $statusArray);
         }
-        CRM_Core_Session::setStatus($text, ts('Saved'), 'success', array('expires' => 0));
+        CRM_Core_Session::setStatus($text, ts('Saved'), 'success', ['expires' => 0]);
       }
 
       $session = CRM_Core_Session::singleton();
index c815ec8fbc64cd089f78c62df3b7cba637047040..675256334fd14be5d05eff3ac307491a72822a77 100644 (file)
  +--------------------------------------------------------------------+
 *}
 {* this template is used for adding/editing/deleting financial type  *}
-<div class="crm-block crm-form-block crm-financial_type-form-block">
-   {if $action eq 8}
-      <div class="messages status">
-          <div class="icon inform-icon"></div>
-          {ts}WARNING: You cannot delete a financial type if it is currently used by any Contributions, Contribution Pages or Membership Types. Consider disabling this option instead.{/ts} {ts}Deleting a financial type cannot be undone.{/ts} {ts}Do you want to continue?{/ts}
-      </div>
-   {else}
-     <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
-     <table class="form-layout-compressed">
-      <tr class="crm-contribution-form-block-name">
-     <td class="label">{$form.name.label}</td>
-    <td class="html-adjust">{$form.name.html}</td>
-       </tr>
-       <tr class="crm-contribution-form-block-description">
-        <td class="label">{$form.description.label}</td>
-    <td class="html-adjust">{$form.description.html}</td>
-       </tr>
-
-       <tr class="crm-contribution-form-block-is_deductible">
-        <td class="label">{$form.is_deductible.label}</td>
-    <td class="html-adjust">{$form.is_deductible.html}<br />
-        <span class="description">{ts}Are contributions of this type tax-deductible?{/ts}</span>
-    </td>
-       </tr>
-       <tr class="crm-contribution-form-block-is_active">
-        <td class="label">{$form.is_active.label}</td>
-    <td class="html-adjust">{$form.is_active.html}</td>
-       </tr>
-      <tr class="crm-contribution-form-block-is_reserved">
-        <td class="label">{$form.is_reserved.label}</td>
-    <td class="html-adjust">{$form.is_reserved.html}</td>
-       </tr>
-
-      </table>
-   {/if}
-   <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="botttom"}</div>
-   {if $action eq 2 or $action eq 4 } {* Update or View*}
-    <div class="crm-submit-buttons">
-       <a href="{crmURL p='civicrm/admin/financial/financialType/accounts' q="action=browse&reset=1&aid=$aid"}" class="button"><span>{ts}View or Edit Financial Accounts{/ts}</a></span>
-    </div>
-   {/if}
-</div>
-
+{include file="CRM/Core/Form/EntityForm.tpl"}
+{if $action eq 2 or $action eq 4 } {* Update or View*}
+  <div class="crm-submit-buttons">
+    <a href="{crmURL p='civicrm/admin/financial/financialType/accounts' q="action=browse&reset=1&aid=$aid"}" class="button"><span>{ts}View or Edit Financial Accounts{/ts}</a></span>
+  </div>
+{/if}
index d82956aa51ccd03549378598136030993908cd7b..8e93b6c82ec3816d8de0c33f615835a3f043cc69 100644 (file)
     <headerPattern>/(finan(cial)?)?type/i</headerPattern>
     <dataPattern>/donation|member|campaign/i</dataPattern>
     <comment>Financial Type Name.</comment>
+    <html>
+      <type>Text</type>
+      <label>Name</label>
+    </html>
     <add>1.3</add>
     <pseudoconstant>
       <table>civicrm_financial_type</table>
   <field>
     <name>description</name>
     <type>varchar</type>
+    <html>
+      <type>TextArea</type>
+      <rows>6</rows>
+      <cols>50</cols>
+      <label>Description</label>
+    </html>
     <length>255</length>
     <comment>Financial Type Description.</comment>
     <add>1.3</add>
     <default>1</default>
     <comment>Is this financial type tax-deductible? If true, contributions of this type may be fully OR partially deductible - non-deductible amount is stored in the Contribution record.</comment>
     <add>1.3</add>
+    <html>
+      <type>CheckBox</type>
+      <label>Tax-Deductible?</label>
+    </html>
   </field>
   <field>
     <name>is_reserved</name>
     <length>4</length>
     <comment>Is this a predefined system object?</comment>
     <add>1.3</add>
+    <html>
+      <type>CheckBox</type>
+      <label>Reserved?</label>
+    </html>
   </field>
   <field>
     <name>is_active</name>
     <length>4</length>
     <comment>Is this property active?</comment>
     <add>1.3</add>
+    <html>
+      <type>CheckBox</type>
+      <label>Enabled?</label>
+    </html>
   </field>
   <index>
     <name>UI_id</name>