diff --git a/CRM/Civicase/Form/CaseSalesOrderContributionCreate.php b/CRM/Civicase/Form/CaseSalesOrderContributionCreate.php index 4b8a435d7..86f39aed9 100644 --- a/CRM/Civicase/Form/CaseSalesOrderContributionCreate.php +++ b/CRM/Civicase/Form/CaseSalesOrderContributionCreate.php @@ -46,6 +46,13 @@ public function buildQuickForm() { 'min' => 1, ], FALSE); + $this->addEntityRef('products', ts('All Products'), [ + 'entity' => 'Product', + 'placeholder' => 'All Products', + 'class' => 'form-control', + 'select' => ['minimumInputLength' => 0], + ]); + if ($this->hasRemainingBalance()) { $this->addElement('radio', 'to_be_invoiced', '', ts('Remaining Balance'), self::INVOICE_REMAIN, @@ -228,6 +235,7 @@ public function createContribution(array $values) { 'to_be_invoiced' => $values['to_be_invoiced'], 'percent_value' => $values['to_be_invoiced'] == self::INVOICE_PERCENT ? floatval($values['percent_value']) : 0, + 'products' => $values['products'], ]; $url = CRM_Utils_System::url('civicrm/contribute/add', $query); diff --git a/CRM/Civicase/Hook/BuildForm/AddSalesOrderLineItemsToContribution.php b/CRM/Civicase/Hook/BuildForm/AddSalesOrderLineItemsToContribution.php index 0ac2e3964..7aa138b81 100644 --- a/CRM/Civicase/Hook/BuildForm/AddSalesOrderLineItemsToContribution.php +++ b/CRM/Civicase/Hook/BuildForm/AddSalesOrderLineItemsToContribution.php @@ -24,11 +24,13 @@ public function run(CRM_Core_Form &$form, $formName) { $status = CRM_Utils_Request::retrieve('sales_order_status_id', 'Integer'); $toBeInvoiced = CRM_Utils_Request::retrieve('to_be_invoiced', 'String'); $percentValue = CRM_Utils_Request::retrieve('percent_value', 'Float'); + $products = CRM_Utils_Request::retrieve('products', 'String') ?? ""; + $products = explode(",", $products) ?? []; if (!$this->shouldRun($form, $formName, $salesOrderId)) { return; } - $lineItemGenerator = new salesOrderlineItemGenerator($salesOrderId, $toBeInvoiced, $percentValue); + $lineItemGenerator = new salesOrderlineItemGenerator($salesOrderId, $toBeInvoiced, $percentValue, $products); $lineItems = $lineItemGenerator->generateLineItems(); $priceField = $this->getDefaultPriceSetFields(); \Civi::cache('short')->set('sales_order_line_items', $lineItems); diff --git a/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php b/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php index 70248d17c..05f2a180a 100644 --- a/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php +++ b/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php @@ -23,8 +23,14 @@ class CRM_Civicase_Service_CaseSalesOrderLineItemsGenerator { /** * Constructs CaseSalesOrderLineItemsGenerator service. */ - public function __construct(private int $salesOrderId, private string $type, private ?string $percentValue) { + public function __construct( + private int $salesOrderId, + private string $type, + private ?string $percentValue, + private ?array $products, + ) { $this->setSalesOrder(); + $this->products = array_filter($this->products); } /** @@ -73,6 +79,11 @@ private function getLineItemForSalesOrder() { $item['quantity'] = ($this->type === self::INVOICE_PERCENT) ? ($this->percentValue / 100) * $item['quantity'] : $item['quantity']; + + if (!empty($this->products) && $this->type === self::INVOICE_PERCENT && !in_array($item['product_id'], $this->products)) { + continue; + } + $item['total'] = $item['quantity'] * floatval($item['unit_price']); $item['tax'] = empty($item['tax_rate']) ? 0 : $this->percent($item['tax_rate'], $item['total']); diff --git a/Civi/Api4/Action/CaseSalesOrder/ContributionCreateAction.php b/Civi/Api4/Action/CaseSalesOrder/ContributionCreateAction.php index 00a9ae638..944fd88a9 100644 --- a/Civi/Api4/Action/CaseSalesOrder/ContributionCreateAction.php +++ b/Civi/Api4/Action/CaseSalesOrder/ContributionCreateAction.php @@ -49,6 +49,13 @@ class ContributionCreateAction extends AbstractAction { */ protected $percentValue; + /** + * The products contribution should apply to. + * + * @var array + */ + protected $products; + /** * Contribution Date. * @@ -106,7 +113,7 @@ private function createContribution() { * Array of price fields. */ private function createContributionWithLineItems(int $salesOrderId, array $priceField): array { - $salesOrderContribution = new salesOrderlineItemGenerator($salesOrderId, $this->toBeInvoiced, $this->percentValue ?? 0); + $salesOrderContribution = new salesOrderlineItemGenerator($salesOrderId, $this->toBeInvoiced, $this->percentValue ?? 0, $this->products ?? []); $lineItems = $salesOrderContribution->generateLineItems(); $taxAmount = $lineTotal = 0; diff --git a/ang/civicase-features/quotations/directives/quotations-contribution-bulk.directive.html b/ang/civicase-features/quotations/directives/quotations-contribution-bulk.directive.html index 8cdb5982e..1c20d7914 100644 --- a/ang/civicase-features/quotations/directives/quotations-contribution-bulk.directive.html +++ b/ang/civicase-features/quotations/directives/quotations-contribution-bulk.directive.html @@ -30,6 +30,23 @@ +