Commit 6010c961 authored by Monish Deb's avatar Monish Deb

Separate Grant financial code to a separate extension

parent e4b76835
......@@ -458,29 +458,4 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
}
}
public static function deleteGrantFinancialEntries($grantID) {
$sql = "SELECT fi.id as fi_id, GROUP_CONCAT(DISTINCT ft.id) as ft_id, eb.batch_id
FROM civicrm_entity_financial_trxn eft
INNER JOIN civicrm_financial_trxn ft ON eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_grant' AND eft.entity_id = $grantID
LEFT JOIN civicrm_entity_financial_trxn eft1 ON eft1.financial_trxn_id = ft.id AND eft1.entity_table = 'civicrm_financial_item'
LEFT JOIN civicrm_financial_item fi ON eft1.entity_id = fi.id
LEFT JOIN civicrm_entity_batch eb ON eb.entity_table ='civicrm_financial_trxn' AND eb.entity_id = ft.id
LEFT JOIN civicrm_batch b ON b.id = eb.batch_id
GROUP BY eft.entity_id
";
$dao = CRM_Core_DAO::executeQuery($sql);
while($dao->fetch()) {
$ftIDs = explode(',', $dao->ft_id);
foreach ($ftIDs as $id) {
civicrm_api3('FinancialTrxn', 'delete', ['id' => $id]);
}
civicrm_api3('FinancialItem', 'delete', ['id' => $dao->fi_id]);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_payment WHERE financial_trxn_id IN ($dao->ft_id)");
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_entity_financial_trxn WHERE financial_trxn_id IN ($dao->ft_id)");
if ($dao->batch_id) {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_entity_batch WHERE entity_id IN ($dao->ft_id) AND entity_table = 'civicrm_financial_trxn' AND batch_id = $dao->batch_id ");
}
}
}
}
......@@ -383,14 +383,6 @@ WHERE civicrm_contact.id = $id ";
CRM_Activity_BAO_Activity::create($params);
}
public static function getAssetFinancialAccountID() {
$relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
return CRM_Core_DAO::singleValueQuery(
"SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1",
[1 => [$relationTypeId, 'Integer']]
);
}
public static function convertNumberToWords($num) {
return ucwords(Numbers_Words::toWords($num));
}
......
......@@ -156,34 +156,6 @@ class CRM_Grant_Form_Task_GrantPayment extends CRM_Core_Form {
}
}
public static function processPaymentDetails($params, $updateTrxn = TRUE) {
$trxnID = $params['financial_trxn_id'];
civicrm_api3('EntityBatch', 'create', [
'entity_table' => 'civicrm_financial_trxn',
'entity_id' => $trxnID,
'batch_id' => $params['batch_id'],
]);
if ($updateTrxn) {
civicrm_api3('FinancialTrxn', 'create', [
'id' => $trxnID,
'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'payment_instrument_id', 'Check'),
'check_number' => CRM_Utils_Array::value('check_number', $params),
'trxn_id' => CRM_Utils_Array::value('trxn_id', $params),
'trxn_date' => CRM_Utils_Array::value('trxn_date', $params, date('YmdHis')),
]);
}
$grantPaymentRecord = [
'financial_trxn_id' => $trxnID,
'payment_created_date' => date('Y-m-d'),
'payment_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Grant_DAO_GrantPayment', 'payment_status_id', 'Printed'),
'payment_reason' => CRM_Utils_Array::value('description', $params),
];
CRM_Grant_BAO_GrantPayment::add($grantPaymentRecord);
return $grantPaymentRecord;
}
public function postProcess() {
$values = $this->controller->exportValues($this->_name);
$approvedGrants = $this->get('approvedGrants');
......@@ -249,7 +221,7 @@ class CRM_Grant_Form_Task_GrantPayment extends CRM_Core_Form {
CRM_Grant_BAO_GrantProgram::sendMail($dao->contact_id, $mailParams, 'Paid', $grantID, 'Approved for Payment');
}
$grantPaymentRecord = self::processPaymentDetails([
$grantPaymentRecord = CRM_Grantfinancialsupport_Util::processPaymentDetails([
'trxn_id' => $values['trxn_id'],
'financial_trxn_id' => $trxnID,
'batch_id' => $values['contribution_batch_id'],
......
......@@ -269,20 +269,6 @@ function grantprograms_civicrm_preProcess($formName, &$form) {
}
}
function _setDefaultFinancialEntries($grantID) {
$sql = "SELECT ft.check_number, ft.trxn_date, ft.trxn_id, b.id as contribution_batch_id, fi.description
FROM civicrm_entity_financial_trxn eft
INNER JOIN civicrm_financial_trxn ft ON eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_grant' AND eft.entity_id = $grantID
LEFT JOIN civicrm_entity_financial_trxn eft1 ON eft1.financial_trxn_id = ft.id AND eft1.entity_table = 'civicrm_financial_item'
LEFT JOIN civicrm_financial_item fi ON eft1.entity_id = fi.id
LEFT JOIN civicrm_entity_batch eb ON eb.entity_table ='civicrm_financial_trxn' AND eb.entity_id = ft.id
LEFT JOIN civicrm_batch b ON b.id = eb.batch_id
ORDER BY eft.id DESC
LIMIT 1
";
return CRM_Utils_Array::value(0, CRM_Core_DAO::executeQuery($sql)->fetchAll(), []);
}
/*
* hook_civicrm_buildForm civicrm hook
*
......@@ -353,21 +339,6 @@ function grantprograms_civicrm_buildForm($formName, &$form) {
TRUE
);
//Financial Type RG-125
$financialType = CRM_Contribute_PseudoConstant::financialType();
foreach ($financialType as $id => $dontCare) {
if (!CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($id, 'Expense Account is')) {
unset($financialType[$id]);
}
}
if (count($financialType)) {
$form->assign('financialType', $financialType);
}
$form->add('select', 'financial_type_id',
ts('Financial Type'),
array('' => ts('- select -')) + $financialType,
FALSE
);
$showFields = FALSE;
if ( $form->getVar('_action') == CRM_Core_Action::UPDATE && $form->getVar('_id')) {
......@@ -379,9 +350,6 @@ function grantprograms_civicrm_buildForm($formName, &$form) {
array_search('Awaiting Information', $grantStatuses) => array('Eligible', '', ''),
array_search('Withdrawn', $grantStatuses),
);
if ($currentStatus == array_search('Paid', $grantStatuses)) {
$form->setDefaults(_setDefaultFinancialEntries($form->getVar('_id')));
}
$currentStatusWeight = $grantWeight[$currentStatus] + 1;
foreach ($grantStatuses as $statusId => $statusName) {
......@@ -670,17 +638,6 @@ function grantprograms_civicrm_validateForm($formName, &$fields, &$files, &$form
if (CRM_Utils_Array::value('amount_granted', $fields) && $fields['amount_granted'] > 0 && !CRM_Utils_Array::value('financial_type_id', $fields) && CRM_Utils_Array::value('money_transfer_date', $fields)) {
$errors['financial_type_id'] = ts('Financial Type is a required field if Amount is Granted');
}
if (!empty($fields['status_id'])) {
$grantStatuses = CRM_Core_OptionGroup::values('grant_status');
if (in_array($grantStatuses[$fields['status_id']], ['Paid', 'Withdrawn', 'Approved for Payment', 'Eligible']) && empty($fields['financial_type_id'])) {
$errors['financial_type_id'] = ts('Financial Type is a required field');
}
if ($grantStatuses[$fields['status_id']] == 'Paid') {
if (empty($fields['contribution_batch_id'])) {
$errors['contribution_batch_id'] = ts('Batch is a required field');
}
}
}
}
if ($formName == 'CRM_Grant_Form_Search') {
if (isset($fields['task']) && CRM_Utils_Array::value('task', $fields) == PAY_GRANTS || CRM_Utils_Array::value('task', $fields) == DELETE_GRANTS) {
......@@ -803,117 +760,6 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
}
}
}
// record financial record only on 'New Grant' form or on edit when grant status is changed
$recordFinancialRecords = ($op == 'create' || (!empty($previousGrant['status_id']) && $previousGrant['status_id'] != $objectRef->status_id));
if (!empty($objectRef->financial_type_id) && $recordFinancialRecords) {
$grantParams = (array) $objectRef;
_createFinancialEntries($previousGrant['status_id'], $grantParams, $params);
}
}
elseif ($objectName == 'Grant' && $op == 'delete') {
CRM_Grant_BAO_GrantPayment::deleteGrantFinancialEntries($objectId);
}
}
function _createFinancialEntries($previousStatusID, $grantParams, $params) {
$grantStatuses = CRM_Core_OptionGroup::values('grant_status');
$multiEntries = _processMultiFundEntries($_POST);
$amount = $grantParams['amount_total'];
$contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$financialItemStatus = CRM_Core_PseudoConstant::accountOptionValues('financial_item_status');
$currentStatusID = $grantParams['status_id'];
$createItem = TRUE;
$trxnParams = [];
$financialItemStatusID = array_search('Paid', $financialItemStatus);
if ($currentStatusID == array_search('Approved for Payment', $grantStatuses)) {
$trxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($grantParams['financial_type_id'], 'Accounts Receivable Account is');
$financialItemStatusID = array_search('Unpaid', $financialItemStatus);
$trxnParams['status_id'] = array_search('Pending', $contributionStatuses);
}
elseif ($currentStatusID == array_search('Paid', $grantStatuses)) {
$trxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($grantParams['financial_type_id'], 'Asset Account is') ?: CRM_Grant_BAO_GrantProgram::getAssetFinancialAccountID();
$trxnParams['status_id'] = array_search('Completed', $contributionStatuses);
$createItem = empty($previousStatusID);
}
elseif ($currentStatusID == array_search('Withdrawn', $grantStatuses)) {
$trxnParams['to_financial_account_id'] = CRM_Grant_BAO_GrantProgram::getAssetFinancialAccountID();
$trxnParams['from_financial_account_id'] = CRM_Core_DAO::singleValueQuery("
SELECT to_financial_account_id FROM civicrm_financial_trxn cft
INNER JOIN civicrm_entity_financial_trxn ecft ON ecft.financial_trxn_id = cft.id
WHERE ecft.entity_id = " . $grantParams['id'] . " and ecft.entity_table = 'civicrm_grant'
ORDER BY cft.id DESC LIMIT 1");
$trxnParams['status_id'] = array_search('Cancelled', $contributionStatuses);
$financialItemStatusID = array_search('Unpaid', $financialItemStatus);
$amount = -$amount;
}
//build financial transaction params
$trxnParams = array_merge($trxnParams, array(
'trxn_date' => date('YmdHis'),
'currency' => $grantParams['currency'],
'entity_table' => 'civicrm_grant',
'entity_id' => $grantParams['id'],
));
if (empty($multiEntries)) {
if ($previousStatusID == array_search('Approved for Payment', $grantStatuses) &&
$currentStatusID == array_search('Paid', $grantStatuses)
) {
$multiEntries[] = [
'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($grantParams['financial_type_id'], 'Accounts Receivable Account is'),
'total_amount' => $amount,
];
}
else {
$multiEntries[] = [
'from_financial_account_id' => CRM_Utils_Array::value('from_financial_account_id', $trxnParams),
'total_amount' => $amount,
];
}
}
$financialItemID = NULL;
foreach ($multiEntries as $key => $entry) {
$trxnParams = array_merge($trxnParams, $entry);
$trxnId = CRM_Core_BAO_FinancialTrxn::create($trxnParams);
if ($currentStatusID == array_search('Paid', $grantStatuses)) {
CRM_Grant_Form_Task_GrantPayment::processPaymentDetails([
'trxn_id' => $params['trxn_id'],
'financial_trxn_id' => $trxnId->id,
'batch_id' => $params['contribution_batch_id'],
'check_number' => $params['check_number'],
'description' => CRM_Utils_Array::value('description', $params),
]);
}
if ($createItem) {
$financialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($grantParams['financial_type_id'], 'Accounts Receivable Account is');
if ($financialItemID) {
civicrm_api3('EntityFinancialTrxn', 'create', [
'entity_table' => 'civicrm_financial_item',
'entity_id' => $financialItemID,
'financial_trxn_id' => $trxnId->id,
'amount' => $entry['total_amount'],
]);
}
else {
$itemParams = array(
'transaction_date' => date('YmdHis'),
'contact_id' => $grantParams['contact_id'],
'currency' => $grantParams['currency'],
'amount' => $entry['total_amount'],
'description' => CRM_Utils_Array::value('description', $params),
'status_id' => $financialItemStatusID,
'financial_account_id' => $financialAccountId,
'entity_table' => 'civicrm_grant',
'entity_id' => $grantParams['id'],
);
$trxnIds['id'] = $trxnId->id;
$financialItemID = CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds)->id;
}
}
}
}
......@@ -995,23 +841,6 @@ function grantprograms_civicrm_postProcess($formName, &$form) {
}
}
function _processMultiFundEntries($values) {
$multifundEntries = [];
if (empty($values['multifund_amount'])) {
return $multifundEntries;
}
for ($i = 0; $i < 2; $i++) {
if (!empty($values['financial_account'][$i])) {
$multifundEntries[$i] = [
'from_financial_account_id' => $values['financial_account'][$i],
'total_amount' => $values['multifund_amount'][$i],
];
}
}
return $multifundEntries;
}
/*
* hook_civicrm_searchTasks
*
......
......@@ -21,6 +21,9 @@ Grants which have been allocated and approved for payment can be selected and pa
<compatibility>
<ver>5.0</ver>
</compatibility>
<requires>
<ext>biz.jmaconsulting.grantfinancialsupport</ext>
</requires>
<comments>The development of this extension was subsidized by the Registered Nurses Association of Ontario (RNAO) and JMA Consulting. </comments>
<civix>
<namespace>CRM/Grantprograms</namespace>
......
......@@ -47,36 +47,7 @@
<td>{$form.prev_assessment.html}</td>
</tr>
{/if}
<tr class="crm-grant-form-block-financial_type">
<td class="label">{$form.financial_type_id.label}</td>
<td>
{if !$financialType}
{capture assign=ftUrl}{crmURL p='civicrm/admin/financial/financialType' q="reset=1"}{/capture}
{ts 1=$ftUrl}There is no Financial Type configured.<a href='%1'> Click here</a> if you want to configure financial type for your site.{/ts}
{else}
{$form.financial_type_id.html}
{/if}
</td>
</tr>
<tr class="crm-grant-form-block-batch_id">
<td class="label">{$form.batch_id.label}</td>
<td>{$form.batch_id.html}</td>
</tr>
</tbody></table>
<div class="crm-accordion-wrapper" id="payment-details">
<div class="crm-accordion-header">{ts}Payment Details{/ts}</div><!-- /.crm-accordion-header -->
<div class="crm-accordion-body" style="display: block;">
<table class="form-layout-compressed">
{foreach from=$paymentFields key=fieldName item=paymentField}
{assign var='name' value=$fieldName}
<tr class="crm-container {$name}-section">
<td class="label">{$form.$name.label}</td>
<td class="content">{$form.$name.html}</td>
</tr>
{/foreach}
</table>
</div><!-- /.crm-accordion-body -->
</div>
{/if}
{literal}
......@@ -87,36 +58,12 @@
$('.crm-grant-form-block-grant_program_id').insertAfter('.crm-grant-form-block-grant_type_id');
$('.crm-grant-form-block-assessment').insertAfter('.crm-grant-form-block-amount_requested');
$('.crm-grant-form-block-prev_assessment').insertAfter('.crm-grant-form-block-assessment');
$('.crm-grant-form-block-financial_type').insertAfter('.crm-grant-form-block-money_transfer_date');
$('#payment-details').insertBefore('#customData');
$('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible'));
$('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
{/literal}{if !$showFields}{literal}
$('.crm-grant-form-block-amount_granted').remove();
{/literal}{/if}{literal}
var statusChange = ($.inArray($("#status_id option:selected").text(), ['Paid', 'Approved for Payment', 'Withdrawn', 'Eligible']) > -1);
$('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible'));
$('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
$('.crm-grant-form-block-financial_type').toggle(statusChange);
if (!statusChange) {
$('#financial_type_id').val('');
}
$('#payment-details').toggle(($("#status_id option:selected").text() === 'Paid'));
$('#status_id').on('change', function() {
var statusChange = ($.inArray($("#status_id option:selected").text(), ['Paid', 'Approved for Payment', 'Withdrawn', 'Eligible']) > -1);
$('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible'));
$('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
$('.crm-grant-form-block-financial_type').toggle(statusChange);
if (!statusChange) {
$('#financial_type_id').val('');
}
$('#payment-details').toggle(($("#status_id option:selected").text() === 'Paid'));
if ($("#status_id option:selected").text() != 'Paid') {
$('#contribution_batch_id').val('');
}
});
});
</script>
{/literal}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment