diff --git a/batchexportperm.php b/batchexportperm.php index d35c492..2c17b78 100644 --- a/batchexportperm.php +++ b/batchexportperm.php @@ -214,6 +214,22 @@ function batchexportperm_civicrm_pre($op, $objectName, $id, &$params) { if ($op == 'edit' && $objectName == 'Contribution') { _batchexportperm_civicrm_check_permission($id); } + + if ($objectName == 'Batch' && $op == 'edit') { + if (!empty($params['status_id'])) { + $exportedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Exported'); + if ($exportedStatusId != $params['status_id']) { + return NULL; + } + $oldStatusId = civicrm_api3('Batch', 'getvalue', [ + 'return' => "status_id", + 'id' => $id, + ]); + if ($oldStatusId != $params['status_id']) { + CRM_Core_Smarty::singleton()->assign("batch_status_change_{$id}", TRUE); + } + } + } } /** @@ -279,3 +295,45 @@ function _batchexportperm_civicrm_allow_contribution_to_edit($contributionId) { } return FALSE; } + +/** + * Implements hook_civicrm_post(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post + */ +function batchexportperm_civicrm_post($op, $objectName, $objectId, &$objectRef) { + if ($objectName == 'Batch' && $op == 'edit') { + if (CRM_Core_Smarty::singleton()->get_template_vars("batch_status_change_{$objectId}")) { + _batchexportperm_civicrm_update_contribution_exporteddate($objectId); + CRM_Core_Smarty::singleton()->assign("batch_status_change_{$objectId}", FALSE); + } + } +} + +/** + * Set export date custom field for contribution when batch is exported. + * + * @param int $batchId + */ +function _batchexportperm_civicrm_update_contribution_exporteddate($batchId) { + $customFieldId = civicrm_api3('CustomField', 'getvalue', [ + 'return' => "id", + 'custom_group_id' => "batchexportperm_batch_details", + 'name' => "export_date", + ]); + $sql = "SELECT cc.id contribution_id + FROM civicrm_contribution cc + INNER JOIN civicrm_entity_financial_trxn ceft + ON ceft.entity_id = cc.id AND ceft.entity_table = 'civicrm_contribution' + INNER JOIN civicrm_entity_batch ceb + ON ceb.entity_id = ceft.financial_trxn_id + AND ceb.entity_table = 'civicrm_financial_trxn' AND ceb.batch_id = {$batchId} + GROUP BY cc.id"; + $dao = CRM_Core_DAO::executeQuery($sql); + while ($dao->fetch()) { + civicrm_api3('Contribution', 'create', [ + 'id' => $dao->contribution_id, + "custom_{$customFieldId}" => date('Y-m-d H:i:s'), + ]); + } +}