Skip to content

Commit

Permalink
BTHAB-186: Update case opportunity details
Browse files Browse the repository at this point in the history
Update case opportunity statuses, and total amounts when:
* Create or update sales order contribution
* Create or update sales order
* Create a payment
  • Loading branch information
erawat committed Sep 22, 2023
1 parent 9906d57 commit 58c889c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
21 changes: 20 additions & 1 deletion CRM/Civicase/Service/AbstractBaseSalesOrderCalculator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use Civi\Api4\OptionValue;

/**
* An Abstract class for SalesOrder and Opportunity statuses and amounts.
*/
class AbstractBaseSalesOrderCalculator {
abstract class CRM_Civicase_Service_AbstractBaseSalesOrderCalculator {

const
INVOICING_STATUS_NO_INVOICES = 'no_invoices',
Expand Down Expand Up @@ -72,4 +74,21 @@ protected function getValueFromOptionValues($needle, $options) {
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'];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* This class provides calculations for payment and invoices that
* attached to the sale order.
*/
class CRM_Civicase_Service_CaseSalesOrderContributionCalculator extends AbstractBaseSalesOrderCalculator {
class CRM_Civicase_Service_CaseSalesOrderContributionCalculator extends CRM_Civicase_Service_AbstractBaseSalesOrderCalculator {

/**
* Case Sales Order object.
Expand Down
16 changes: 8 additions & 8 deletions CRM/Civicase/Service/CaseSalesOrderOpportunityCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* A service class for calculate opportunity financial amounts and statuses.
*/
class CRM_Civicase_Service_CaseSalesOrderOpportunityCalculator extends AbstractBaseSalesOrderCalculator {
class CRM_Civicase_Service_CaseSalesOrderOpportunityCalculator extends CRM_Civicase_Service_AbstractBaseSalesOrderCalculator {
/**
* Case's total quoted amount.
*
Expand Down Expand Up @@ -83,14 +83,14 @@ public function calculateTotalQuotedAmount(): float {
*/
public function calculateInvoicingStatus() {
if (!($this->totalInvoicedAmount > 0)) {
return $this->getValueFromOptionValues(parent::INVOICING_STATUS_NO_INVOICES, $this->invoicingStatusOptionValues);
return $this->getLabelFromOptionValues(parent::INVOICING_STATUS_NO_INVOICES, $this->invoicingStatusOptionValues);
}

if ($this->totalInvoicedAmount < $this->totalQuotedAmount) {
return $this->getValueFromOptionValues(parent::INVOICING_STATUS_PARTIALLY_INVOICED, $this->invoicingStatusOptionValues);
return $this->getLabelFromOptionValues(parent::INVOICING_STATUS_PARTIALLY_INVOICED, $this->invoicingStatusOptionValues);
}

return $this->getValueFromOptionValues(parent::INVOICING_STATUS_FULLY_INVOICED, $this->invoicingStatusOptionValues);
return $this->getLabelFromOptionValues(parent::INVOICING_STATUS_FULLY_INVOICED, $this->invoicingStatusOptionValues);
}

/**
Expand All @@ -101,19 +101,19 @@ public function calculateInvoicingStatus() {
*/
public function calculatePaymentStatus() {
if (!($this->totalPaidAmount > 0)) {
return $this->getValueFromOptionValues(parent::PAYMENT_STATUS_NO_PAYMENTS, $this->paymentStatusOptionValues);
return $this->getLabelFromOptionValues(parent::PAYMENT_STATUS_NO_PAYMENTS, $this->paymentStatusOptionValues);
}

if ($this->totalPaidAmount < $this->totalInvoicedAmount) {
return $this->getValueFromOptionValues(parent::PAYMENT_STATUS_PARTIALLY_PAID, $this->paymentStatusOptionValues);
return $this->getLabelFromOptionValues(parent::PAYMENT_STATUS_PARTIALLY_PAID, $this->paymentStatusOptionValues);
}

if ($this->totalPaidAmount > $this->totalInvoicedAmount) {
return $this->getValueFromOptionValues(parent::PAYMENT_STATUS_OVERPAID, $this->paymentStatusOptionValues);
return $this->getLabelFromOptionValues(parent::PAYMENT_STATUS_OVERPAID, $this->paymentStatusOptionValues);

}

return $this->getValueFromOptionValues(parent::PAYMENT_STATUS_FULLY_PAID, $this->paymentStatusOptionValues);
return $this->getLabelFromOptionValues(parent::PAYMENT_STATUS_FULLY_PAID, $this->paymentStatusOptionValues);
}

/**
Expand Down

0 comments on commit 58c889c

Please sign in to comment.