Skip to content

Commit

Permalink
Filters for dunning lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ImanuelBertrand committed Jul 23, 2024
1 parent cdcf660 commit 9c1893b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
74 changes: 71 additions & 3 deletions Ui/DataProvider/DunningListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ class DunningListing extends AbstractDataProvider
protected OrderAddressCollectionFactory $orderAddressCollectionFactory;
protected CollectionFactory $dunningCollectionFactory;
const JOIN_CONFIG = [
'invoice' => ['table_name' => 'sales_invoice', 'on_clause' => 'invoice.entity_id = main_table.invoice_id', 'needed_joins' => []],
'order' => ['table_name' => 'sales_order', 'on_clause' => 'order.entity_id = invoice.order_id', 'needed_joins' => ['invoice']],
'invoice' => [
'table_name' => 'sales_invoice',
'on_clause' => 'invoice.entity_id = main_table.invoice_id',
'needed_joins' => [],
],
'order' => [
'table_name' => 'sales_order',
'on_clause' => 'order.entity_id = invoice.order_id',
'needed_joins' => ['invoice'],
],
];
const JOINS_NEEDED = [
'email_address' => ['order'],
Expand Down Expand Up @@ -104,7 +112,7 @@ public function getData()
'invoice_date' => $invoice->getCreatedAt(),
'invoice_increment_id' => $this->display->getObjectLink($invoice),
'is_sent' => (int)!empty($item['sent_at']),
'name' => implode(', ', $names),
'name' => implode("<br>", $names),
]);
}

Expand All @@ -122,6 +130,65 @@ protected function setFilterIsSent(Filter $filter): void
->setValue(true);
}

protected function setFilterName(Filter $filter): void
{
// Normalize the filter value by replacing multiple consecutive spaces with a single space
$filterValue = trim(preg_replace('/\s\s+/u', ' ', $filter->getValue()));

// Fetch invoice IDs related to dunnings and corresponding invoices, orders, and billing addresses.
$allInvoiceIdsWithDunnings = $this->dunningCollectionFactory->create()->getColumnValues('invoice_id');
$allInvoicesWithDunnings = $this->invoiceCollectionFactory->create()
->addFieldToFilter('entity_id', ['in' => $allInvoiceIdsWithDunnings]);
$allOrderIdsWithDunnings = $allInvoicesWithDunnings->getColumnValues('order_id');
$allBillingAddressesWithDunnings = $allInvoicesWithDunnings->getColumnValues('billing_address_id');

// Use customer name to filter matching orders.
$matchingOrders = $this->orderCollectionFactory->create()
->addFieldToFilter('entity_id', ['in' => $allOrderIdsWithDunnings]);
$matchingOrders->getSelect()
->where(
'REGEXP_REPLACE(CONCAT(customer_firstname, " ", customer_lastname), "\\\\s\\\\s+", " ") LIKE ?',
$filterValue
);
$matchingOrderIds = $matchingOrders->getColumnValues('entity_id');
$this->logger->debug('Query: ' . $matchingOrders->getSelect()->__toString());
$this->logger->debug('Matching order IDs', ['ids' => $matchingOrderIds]);

// Similarly, filter billing addresses by name or company.
$matchingBillingAddresses = $this->orderAddressCollectionFactory->create()
->addFieldToFilter('entity_id', ['in' => $allBillingAddressesWithDunnings]);
$matchingBillingAddresses->getSelect()
->where(
'REGEXP_REPLACE(CONCAT(firstname, " ", lastname), "\\\\s\\\\s+", " ") LIKE ? ' .
'OR REGEXP_REPLACE(company, "\\\\s\\\\s+", " ") LIKE ?',
$filterValue
);
$this->logger->debug('Query: ' . $matchingBillingAddresses->getSelect()->__toString());
$this->logger->debug(
'Matching billing address IDs',
['ids' => $matchingBillingAddresses->getColumnValues('entity_id')]
);

// Deduplicate and combine matching order IDs from orders and billing addresses for efficient filtering.
$matchingOrderIds = array_unique(array_merge(
$matchingOrderIds,
$matchingBillingAddresses->getColumnValues('parent_id')
));
$matchingOrderIds = array_combine($matchingOrderIds, $matchingOrderIds);

// Directly filter invoices in memory using the reduced set of order IDs.
$matchingInvoices = array_filter(
$allInvoicesWithDunnings->getItems(),
fn($invoice) => isset($matchingOrderIds[$invoice->getOrderId()])
);
$matchingInvoiceIds = array_map(fn($invoice) => $invoice->getId(), $matchingInvoices);

// Set the refined invoice ID filter.°
$filter->setField('invoice_id')
->setConditionType('in')
->setValue(implode(',', $matchingInvoiceIds));
}

/**
* @param string $joinIdent
* @return void
Expand Down Expand Up @@ -160,6 +227,7 @@ public function addFilter(Filter $filter)
'invoice_date' => 'invoice.created_at',
'invoice_increment_id' => 'invoice.increment_id',
'is_sent' => [$this, 'setFilterIsSent'],
'name' => [$this, 'setFilterName'],
];

$processor = $processors[$filter->getField()] ?? null;
Expand Down
2 changes: 2 additions & 0 deletions view/adminhtml/ui_component/dunning_listing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@
<column name="name" sortOrder="25">
<settings>
<sortable>false</sortable>
<filter>text</filter>
<label translate="true">Name</label>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
</settings>
</column>
<column name="dunning_type" sortOrder="30" component="Magento_Ui/js/grid/columns/select">
Expand Down

0 comments on commit 9c1893b

Please sign in to comment.