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 #975 from compucorp/BTHAB-185-display-statuses
BTHAB-185: Implement invoicing and payment statuses for quotations
- Loading branch information
Showing
25 changed files
with
901 additions
and
650 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
use Civi\Api4\CaseSalesOrder; | ||
use Civi\Api4\Contribution; | ||
|
||
/** | ||
* Handles CaseSalesOrder payment processing. | ||
*/ | ||
class CRM_Civicase_Hook_Post_CaseSalesOrderPayment { | ||
|
||
/** | ||
* Updates CaseSaleOrder statuses when creating a payment transcation. | ||
* | ||
* @param string $op | ||
* The operation being performed. | ||
* @param string $objectName | ||
* Object name. | ||
* @param mixed $objectId | ||
* Object ID. | ||
* @param object $objectRef | ||
* Object reference. | ||
*/ | ||
public function run($op, $objectName, $objectId, &$objectRef) { | ||
if (!$this->shouldRun($op, $objectName)) { | ||
return; | ||
} | ||
|
||
$entityFinancialTrxn = civicrm_api3('EntityFinancialTrxn', 'get', [ | ||
'sequential' => 1, | ||
'entity_table' => 'civicrm_contribution', | ||
'financial_trxn_id' => $objectRef->financial_trxn_id, | ||
]); | ||
|
||
if (empty($entityFinancialTrxn['values'][0])) { | ||
return; | ||
} | ||
|
||
$contributionId = $entityFinancialTrxn['values'][0]['entity_id']; | ||
|
||
$salesOrderID = Contribution::get() | ||
->addSelect('Opportunity_Details.Quotation') | ||
->addWhere('id', '=', $contributionId) | ||
->execute() | ||
->first()['Opportunity_Details.Quotation']; | ||
|
||
if (empty($salesOrderID)) { | ||
return; | ||
} | ||
|
||
$transaction = CRM_Core_Transaction::create(); | ||
|
||
try { | ||
$caseSaleOrderContributionService = new CRM_Civicase_Service_CaseSaleOrderContribution($salesOrderID); | ||
$paymentStatusID = $caseSaleOrderContributionService->calculatePaymentStatus(); | ||
$invoicingStatusID = $caseSaleOrderContributionService->calculateInvoicingStatus(); | ||
|
||
CaseSalesOrder::update() | ||
->addWhere('id', '=', $salesOrderID) | ||
->addValue('invoicing_status_id', $invoicingStatusID) | ||
->addValue('payment_status_id', $paymentStatusID) | ||
->execute(); | ||
|
||
$transaction->commit(); | ||
} | ||
catch (\Throwable $th) { | ||
$transaction->rollback(); | ||
CRM_Core_Error::statusBounce(ts('Error updating sales order statues')); | ||
} | ||
} | ||
|
||
/** | ||
* 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 $objectName == 'EntityFinancialTrxn' && $op == 'create'; | ||
} | ||
|
||
} |
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
Oops, something went wrong.