Skip to content

Commit

Permalink
Merge pull request #148 from compucorp/PROD-156-refund-creditnote
Browse files Browse the repository at this point in the history
PROD-156: Create creditnote allocation for credit card refund
  • Loading branch information
olayiwola-compucorp authored May 16, 2024
2 parents 0e7564d + 67df918 commit 9043d3a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
88 changes: 88 additions & 0 deletions CRM/Financeextras/Form/Payment/Refund.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Civi\Api4\CreditNoteAllocation;
use Civi\Financeextras\Utils\FinancialAccountUtils;
use CRM_Finananceextras_ExtensionUtil as E;

/**
Expand Down Expand Up @@ -192,7 +194,13 @@ public function setFields() {
'attributes' => ['' => '- ' . ts('select reason') . ' -'] + $reasons,
'extra' => ['class' => 'huge crm-select2'],
'required' => TRUE,
],

'create_credit_note' => [
'type' => 'checkbox',
'label' => 'Also automatically create a credit note for this amount and apply to this invoice',
'extra' => ['checked' => TRUE],
'required' => FALSE,
],
];
}
Expand Down Expand Up @@ -296,6 +304,10 @@ public function postProcess($params = NULL) {
if ($this->refundStatus['refund_status'] == "Completed") {
$this->createFinancialTrxnRefund($vals);
}

if ($vals['create_credit_note'] ?? FALSE) {
$this->createCreditNote($vals);
}
}

/**
Expand Down Expand Up @@ -325,4 +337,80 @@ public function createFinancialTrxnRefund($vals) {
CRM_Core_Session::setStatus(ts('Refund has been requested successfully.'), ts('Success'), 'success');
}

/**
* Creates creditnote for refund
*
* @param array $vals
* Submitted refund vallues
*/
public function createCreditNote($vals) {
$contribution = \Civi\Api4\Contribution::get()
->addSelect('*', 'financeextras_contribution_owner.owner_organization')
->addWhere('id', '=', $this->contributionID)
->addChain('line_item', \Civi\Api4\LineItem::get()
->addWhere('contribution_id', '=', '$id')
)
->execute()
->first();

$status = \Civi\Api4\OptionValue::get()
->addSelect('value')
->addWhere('option_group_id:name', '=', 'financeextras_credit_note_status')
->addWhere('name', '=', 'open')
->setLimit(1)
->execute()
->first()['value'];

$creditNoteData = [
'contact_id' => $contribution['contact_id'],
'owner_organization' => $contribution['financeextras_contribution_owner.owner_organization'],
'currency' => $contribution['currency'],
'date' => date("Y-m-d"),
'status_id' => $status,
'items' => [],
];

$duePercent = $vals['amount'] / $contribution['total_amount'];
foreach ($contribution['line_item'] as $lineItem) {
$item = [
'financial_type_id' => $lineItem['financial_type_id'],
'description' => $lineItem['label'],
'quantity' => $lineItem['qty'] * $duePercent,
'unit_price' => $lineItem['unit_price'],
'line_total' => 0,
];
$taxAccount = FinancialAccountUtils::getFinancialTypeAccount($lineItem['financial_type_id'], 'Sales Tax Account is');
$item['tax_rate'] = \Civi\Api4\FinancialAccount::get(FALSE)
->addSelect('tax_rate')
->addWhere('id', '=', $taxAccount)
->execute()
->first()['tax_rate'] ?? 0;

$creditNoteData['items'][] = $item;
}

$creditNote = \Civi\Api4\CreditNote::save(FALSE)
->addRecord($creditNoteData)
->execute()
->first();

$allocationType = \Civi\Api4\OptionValue::get(FALSE)
->addSelect('value')
->addWhere('option_group_id:name', '=', 'financeextras_credit_note_allocation_type')
->addWhere('name', '=', 'invoice')
->execute()
->first()['value'];

CreditNoteAllocation::allocate(FALSE)
->setContributionId($contribution['id'])
->setCreditNoteId($creditNote['id'])
->setReference('CREDIT CARD REFUND')
->setTypeId($allocationType)
->setAmount($creditNote['total_credit'])
->setCurrency($contribution['currency'])
->execute();

return TRUE;
}

}
3 changes: 3 additions & 0 deletions templates/CRM/Financeextras/Form/Payment/Refund.hlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{htxt id="create_credit_note"}
{ts}By checking the box the system will automatically create a credit note equal to the amount of the refund (applied pro-rata against each of the line items on the contribution) and apply the credit to this contribution, thus reducing the amount due to you. If you do not check the box the contact will continue to owe the original amount. You can always create a credit note manually if you do not enable this option.{/ts}
{/htxt}
15 changes: 15 additions & 0 deletions templates/CRM/Financeextras/Form/Payment/Refund.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,23 @@
<td class="label">{$form.reason.label}</td>
<td>{$form.reason.html}</td>
</tr>

<tr class="crm-payment-refund-form-block-reason">
<td class="label">{$form.create_credit_note.html}</td>
<td>{$form.create_credit_note.label} {help id="create_credit_note" file="CRM/Financeextras/Form/Payment/Refund.hlp"}</td>
</tr>
</table>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
</div>

<script type="text/javascript">
{literal}
CRM.$(function($) {
$("a[target='crm-popup']").on('crmPopupFormSuccess', function (e) {
CRM.refreshParent(e);
});
});
{/literal}
</script>

0 comments on commit 9043d3a

Please sign in to comment.