Skip to content

Commit

Permalink
More changes to accomodate description of advantage
Browse files Browse the repository at this point in the history
  • Loading branch information
Edzelopez committed Mar 23, 2020
1 parent 91e70dc commit 36dc51c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 26 deletions.
34 changes: 25 additions & 9 deletions CRM/Cdntaxreceipts/DAO/CdnAdvantage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ class CRM_Cdntaxreceipts_DAO_CdnAdvantage extends CRM_Core_DAO {
public $id;

/**
* FK to Contact
* Contribution ID
*
* @var int
*/
public $contact_id;
public $contribution_id;

/**
* Advantage Description
*
* @var string
*/
public $advantage_description;

/**
* Class constructor.
Expand All @@ -59,7 +66,6 @@ public function __construct() {
public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'contact_id', 'civicrm_contact', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
Expand All @@ -84,16 +90,26 @@ public static function &fields() {
'bao' => 'CRM_Cdntaxreceipts_DAO_CdnAdvantage',
'localizable' => 0,
],
'contact_id' => [
'name' => 'contact_id',
'contribution_id' => [
'name' => 'contribution_id',
'type' => CRM_Utils_Type::T_INT,
'description' => ts('FK to Contact'),
'where' => 'cdntaxreceipts_advantage.contact_id',
'description' => ts('Contribution ID'),
'where' => 'cdntaxreceipts_advantage.contribution_id',
'table_name' => 'cdntaxreceipts_advantage',
'entity' => 'CdnAdvantage',
'bao' => 'CRM_Cdntaxreceipts_DAO_CdnAdvantage',
'localizable' => 0,
],
'advantage_description' => [
'name' => 'advantage_description',
'type' => CRM_Utils_Type::T_STRING,
'description' => ts('Description of Advantage'),
'where' => 'cdntaxreceipts_advantage.advantage_description',
'table_name' => 'cdntaxreceipts_advantage',
'entity' => 'CdnAdvantage',
'bao' => 'CRM_Cdntaxreceipts_DAO_CdnAdvantage',
'localizable' => 1,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down Expand Up @@ -139,7 +155,7 @@ public function getLog() {
* @return array
*/
public static function &import($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'ceipts_advantage', $prefix, []);
$r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'cdntaxreceipts_advantage', $prefix, []);
return $r;
}

Expand All @@ -151,7 +167,7 @@ public static function &import($prefix = FALSE) {
* @return array
*/
public static function &export($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'ceipts_advantage', $prefix, []);
$r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'cdntaxreceipts_advantage', $prefix, []);
return $r;
}

Expand Down
11 changes: 9 additions & 2 deletions CRM/Cdntaxreceipts/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ public function upgrade_1410() {
}

public function upgrade_1510() {
$this->ctx->log->info('Applying update 1510: Adding gift advantage description field');
CRM_Core_DAO::executeQuery('ALTER TABLE cdntaxreceipts_log_contributions ADD advantage_description varchar(255) NULL');
$this->ctx->log->info('Applying update 1510: Adding gift advantage description table');
$sql = "CREATE TABLE IF NOT EXISTS cdntaxreceipts_advantage (
id int(11) UNSIGNED NOT NULL,
contribution_id int(10) UNSIGNED NOT NULL,
advantage_description varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
INDEX contribution_id (contribution_id)
)";
CRM_Core_DAO::executeQuery($sql);
return TRUE;
}

Expand Down
12 changes: 12 additions & 0 deletions cdntaxreceipts.db.inc
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,15 @@ function cdntaxreceipts_log_next_id() {
$last_id = CRM_Core_DAO::singleValueQuery($sql);
return $last_id + 1;
}

function cdntaxreceipts_advantage($id, $desc = NULL, &$defaults = [], $view = FALSE) {
$advantage = new CRM_Cdntaxreceipts_DAO_CdnAdvantage;
$advantage->contribution_id = $id;
$advantage->find(TRUE);
if ($view) {
$defaults['advantage_description'] = $advantage->advantage_description ?: NULL;
return;
}
$advantage->advantage_description = $desc;
$advantage->save();
}
10 changes: 8 additions & 2 deletions cdntaxreceipts.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function cdntaxreceipts_civicrm_buildForm( $formName, &$form ) {
if (is_a($form, 'CRM_Contribute_Form_Contribution') && in_array($form->_action, [CRM_Core_Action::ADD, CRM_Core_Action::UPDATE])) {
$form->add('text', 'non_deductible_amount', ts('Advantage Amount'), NULL);
$form->add('textarea', 'advantage_description', ts('Advantage Description'), ['cols' => '60', 'rows' => '4']);
if ($form->_action & CRM_Core_Action::UPDATE) {
cdntaxreceipts_advantage($form->_id, NULL, $defaults, TRUE);
$form->setDefaults($defaults);
}

CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Cdntaxreceipts/Form/AddAdvantage.tpl',
Expand Down Expand Up @@ -80,9 +84,11 @@ function cdntaxreceipts_civicrm_validateForm($formName, &$fields, &$files, &$for

function cdntaxreceipts_civicrm_postProcess( $formName, &$form ) {

// Handle saving of advantage amount
// Handle saving of advantage description
if (is_a($form, 'CRM_Contribute_Form_Contribution')) {

if (CRM_Utils_Array::value('advantage_description', $form->_params) && !empty($form->_id)) {
cdntaxreceipts_advantage($form->_id, $form->_params['advantage_description']);
}
}

// first check whether I really need to process this form
Expand Down
8 changes: 8 additions & 0 deletions sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ CREATE TABLE cdntaxreceipts_log_contributions (
INDEX contribution_id (contribution_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contributions for each tax reciept issuing.';

CREATE TABLE `cdntaxreceipts_advantage` (
`id` int(11) UNSIGNED NOT NULL,
`contribution_id` int(10) UNSIGNED NOT NULL,
`advantage_description` varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
INDEX contribution_id (contribution_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1 change: 1 addition & 0 deletions sql/uninstall.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

DROP TABLE IF EXISTS cdntaxreceipts_log_contributions;
DROP TABLE IF EXISTS cdntaxreceipts_log;
DROP TABLE IF EXISTS cdntaxreceipts_advantage;

-- Delete message template and respective option group and option values
DELETE civicrm_option_value.*, civicrm_option_group.*, civicrm_msg_template.*
Expand Down
27 changes: 14 additions & 13 deletions xml/schema/CRM/Cdntaxreceipts/CdnAdvantage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@
<base>CRM/Cdntaxreceipts</base>
<class>CdnAdvantage</class>
<name>cdntaxreceipts_advantage</name>
<comment>FIXME</comment>
<add>4.4</add>
<comment>Holds description for advantage for each contribution</comment>
<add>5.23</add>
<log>true</log>

<field>
<name>id</name>
<type>int unsigned</type>
<required>true</required>
<comment>Unique CdnAdvantage ID</comment>
<add>4.4</add>
<add>5.23</add>
</field>
<primaryKey>
<name>id</name>
<autoincrement>true</autoincrement>
</primaryKey>

<field>
<name>contact_id</name>
<name>contribution_id</name>
<type>int unsigned</type>
<comment>FK to Contact</comment>
<add>4.4</add>
<comment>FK to Contribution</comment>
<add>5.23</add>
</field>

<field>
<name>advantage_description</name>
<type>varchar</type>
<length>255</length>
<comment>Description of Advantage</comment>
<add>5.23</add>
</field>
<foreignKey>
<name>contact_id</name>
<table>civicrm_contact</table>
<key>id</key>
<add>4.4</add>
<onDelete>CASCADE</onDelete>
</foreignKey>

</table>

0 comments on commit 36dc51c

Please sign in to comment.