Skip to content

Commit

Permalink
BTHAB-386: Support creating contribution lineitems for specfic product
Browse files Browse the repository at this point in the history
  • Loading branch information
olayiwola-compucorp committed Jan 7, 2025
1 parent 0512976 commit e9c8a7f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CRM/Civicase/Form/CaseSalesOrderContributionCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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']);

Expand Down
9 changes: 8 additions & 1 deletion Civi/Api4/Action/CaseSalesOrder/ContributionCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class ContributionCreateAction extends AbstractAction {
*/
protected $percentValue;

/**
* The products contribution should apply to.
*
* @var array
*/
protected $products;

/**
* Contribution Date.
*
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
</div>
</div>

<div class="form-group" ng-show="$ctrl.data.toBeInvoiced === 'percent'">
<label class="col-sm-2 control-label">
Products
</label>
<div class="col-sm-5 civicase__ui-range">
<input class="form-control"
ng-model="$ctrl.data.products"
name="products"
placeholder="All Products"
crm-entityref="{
entity: 'Product',
select: { allowClear: true, 'minimumInputLength': 0, create: false, multiple: true }
}"
/>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label required-mark">
Financial Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
percentValue: null,
statusId: null,
financialTypeId: null,
products: null,
date: $.datepicker.formatDate('yy-mm-dd', new Date())
};
ctrl.salesOrderStatus = SalesOrderStatus.getAll();
Expand All @@ -53,6 +54,9 @@
const chunkedIds = _.chunk(ctrl.ids, BATCH_SIZE);
for (const salesOrderIds of chunkedIds) {
try {
if (ctrl.data.products.length > 0) {
ctrl.data.products = ctrl.data.products.split(',');
}
const result = await crmApi4('CaseSalesOrder', 'contributionCreateAction', { ...ctrl.data, salesOrderIds });
contributionCreated += result.created_contributions_count ?? 0;
} catch (error) {
Expand Down
15 changes: 15 additions & 0 deletions templates/CRM/Civicase/Form/CaseSalesOrderContributionCreate.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
</div>
</div>
<div class="form-group" id="product-select">
<div class="row">
<label class="col-sm-1 control-label">
{$form.products.label}
</label>
<div class="col-sm-5">
{$form.products.html}
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-sm-12 control-label">
Expand All @@ -45,18 +56,22 @@
CRM.$(function ($) {
CRM.$('input[name="percent_amount"]').hide();
CRM.$('#product-select').hide()
if ( CRM.$('input[name="to_be_invoiced"]').val() == 'percent') {
$('input[name="to_be_invoiced"]#invoice_percent').prop("checked", true);
CRM.$('input[name="percent_amount"]').show();
CRM.$('#product-select').show()
}
CRM.$('input[name="to_be_invoiced"]').on('input', (e) => {
if (e.target.value == 'percent') {
CRM.$('input[name="percent_amount"]').show();
CRM.$('#product-select').show();
return
}
CRM.$('input[name="percent_amount"]').hide();
CRM.$('#product-select').hide();
});
});
</script>
Expand Down

0 comments on commit e9c8a7f

Please sign in to comment.