Skip to content

Commit

Permalink
BTHAB-185: Update statuses on contribution deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
erawat committed Sep 22, 2023
1 parent 58c889c commit 9795425
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
79 changes: 79 additions & 0 deletions CRM/Civicase/Hook/Pre/DeleteSalesOrderContribution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use Civi\Api4\CaseSalesOrder;
use Civi\Api4\Contribution;

/**
* Handles Precessing Contribution deletion.
*/
class CRM_Civicase_Hook_Pre_DeleteSalesOrderContribution {

/**
* Updates CaseSaleOrder and Opportunity statuses and financial details.
*
* @param string $op
* The operation being performed.
* @param string $objectName
* Object name.
* @param mixed $objectId
* Object ID.
* @param array &$params
* Array of an entity.
*/
public function run($op, $objectName, $objectId, &$params) {
if (!$this->shouldRun($op, $objectName)) {
return;
}

$salesOrderId = $this->getQuotationId($objectId);
if (empty($salesOrderId)) {
return;
}

$caseSaleOrderContributionService = new CRM_Civicase_Service_CaseSalesOrderContributionCalculator($salesOrderId);
$invoicingStatusId = $caseSaleOrderContributionService->calculateInvoicingStatus();
$paymentStatusId = $caseSaleOrderContributionService->calculatePaymentStatus();

CaseSalesOrder::update()
->addWhere('id', '=', $salesOrderId)
->addValue('invoicing_status_id', $invoicingStatusId)
->addValue('payment_status_id', $paymentStatusId)
->execute();

$caseSalesOrder = CaseSalesOrder::get()
->addSelect('case_id')
->addWhere('id', '=', $salesOrderId)
->execute()
->first();

$caseSaleOrderContributionService = new \CRM_Civicase_Service_CaseSalesOrderOpportunityCalculator($caseSalesOrder['case_id']);
$caseSaleOrderContributionService->updateOpportunityFinancialDetails();
}

/**
* Determines if the hook should run or not.
*
* @param string $op
* The operation being performed.
* @param string $objectName
* Object name.
*
* @return bool
* returns a boolean to determine if hook will run or not.
*/
private function shouldRun($op, $objectName) {
return strtolower($objectName) == 'contribution' && $op == 'delete';
}

/**
* Gets quotation ID by contribution ID.
*/
private function getQuotationId($id) {
return Contribution::get()
->addSelect('Opportunity_Details.Quotation')
->addWhere('id', '=', $id)
->execute()
->first()['Opportunity_Details.Quotation'];
}

}
13 changes: 13 additions & 0 deletions civicase.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ function civicase_civicrm_post($op, $objectName, $objectId, &$objectRef) {
}
}

/**
* Implements hook_civicrm_pre().
*/
function civicase_civicrm_pre($op, $objectName, $id, &$params) {
$hooks = [
new CRM_Civicase_Hook_Pre_DeleteSalesOrderContribution(),
];

foreach ($hooks as $hook) {
$hook->run($op, $objectName, $id, $params);
}
}

/**
* Implements hook_civicrm_postProcess().
*/
Expand Down

0 comments on commit 9795425

Please sign in to comment.