forked from civicrm/org.civicrm.civicase
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #977 from compucorp/BTHAB-186-diplay-opportunity-f…
…inancial-details BTHAB-186: Add case's opportunity details
- Loading branch information
Showing
11 changed files
with
720 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
use Civi\Api4\OptionValue; | ||
|
||
/** | ||
* An Abstract class for SalesOrder and Opportunity statuses and amounts. | ||
*/ | ||
abstract class CRM_Civicase_Service_AbstractBaseSalesOrderCalculator { | ||
|
||
const | ||
INVOICING_STATUS_NO_INVOICES = 'no_invoices', | ||
INVOICING_STATUS_PARTIALLY_INVOICED = 'partially_invoiced', | ||
INVOICING_STATUS_FULLY_INVOICED = 'fully_invoiced', | ||
PAYMENT_STATUS_NO_PAYMENTS = 'no_payments', | ||
PAYMENT_STATUS_PARTIALLY_PAID = 'partially_paid', | ||
PAYMENT_STATUS_OVERPAID = 'overpaid', | ||
PAYMENT_STATUS_FULLY_PAID = 'fully_paid'; | ||
|
||
/** | ||
* Case Sales Order payment status option values. | ||
* | ||
* @var array | ||
*/ | ||
protected array $paymentStatusOptionValues; | ||
/** | ||
* Case Sales Order Invoicing status option values. | ||
* | ||
* @var array | ||
*/ | ||
protected array $invoicingStatusOptionValues; | ||
|
||
/** | ||
* AbstractBaseSalesOrderCalculator constructor. | ||
*/ | ||
public function __construct() { | ||
$this->paymentStatusOptionValues = $this->getOptionValues('case_sales_order_payment_status'); | ||
$this->invoicingStatusOptionValues = $this->getOptionValues('case_sales_order_invoicing_status'); | ||
} | ||
|
||
/** | ||
* Gets option values by option group name. | ||
* | ||
* @param string $name | ||
* Option group name. | ||
* | ||
* @return array | ||
* Option values. | ||
* | ||
* @throws API_Exception | ||
* @throws \Civi\API\Exception\UnauthorizedException | ||
*/ | ||
protected function getOptionValues($name) { | ||
return OptionValue::get(FALSE) | ||
->addSelect('*') | ||
->addWhere('option_group_id:name', '=', $name) | ||
->execute() | ||
->getArrayCopy(); | ||
} | ||
|
||
/** | ||
* Gets status (option values' value) from the given options. | ||
* | ||
* @param string $needle | ||
* Search value. | ||
* @param array $options | ||
* Option value. | ||
* | ||
* @return string | ||
* Option values' value. | ||
*/ | ||
protected function getValueFromOptionValues($needle, $options) { | ||
$key = array_search($needle, array_column($options, 'name')); | ||
|
||
return $options[$key]['value']; | ||
} | ||
|
||
/** | ||
* Gets status (option values' label) from the given options. | ||
* | ||
* @param string $needle | ||
* Search value. | ||
* @param array $options | ||
* Option value. | ||
* | ||
* @return string | ||
* Option values' value. | ||
*/ | ||
protected function getLabelFromOptionValues($needle, $options) { | ||
$key = array_search($needle, array_column($options, 'name')); | ||
|
||
return $options[$key]['label']; | ||
} | ||
|
||
} |
Oops, something went wrong.