Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort Order "Comments History" tab comments correctly when same timestamp #34306

Open
wants to merge 51 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b7af8a5
Correctly label option for Protection Type
PromInc Jul 8, 2021
242c12f
Correctly label option for Protection Type
PromInc Jul 8, 2021
08d20be
Correctly label option for Protection Type
PromInc Jul 8, 2021
6c87486
Correctly label option for Protection Type
PromInc Jul 8, 2021
692f96d
Correctly label option for Protection Type
PromInc Jul 8, 2021
4febd7c
Correctly label option for Protection Type
PromInc Jul 8, 2021
41188db
Correctly label option for Protection Type
PromInc Jul 8, 2021
bf1c340
Correctly label option for Protection Type
PromInc Jul 8, 2021
ba24475
Sort by timestamp and id
PromInc Oct 11, 2021
836a325
Merge branch 'magento:2.4-develop' into 2.4-develop
PromInc Oct 11, 2021
eba21cf
Update ResetMethod.php
PromInc Oct 11, 2021
7947440
Update ResetMethod.php
PromInc Oct 11, 2021
36a2245
Update CollectionFactory.php
PromInc Oct 11, 2021
e46991f
Update ResetMethodTest.php
PromInc Oct 11, 2021
81e3cbc
Update ResetMethodTest.php
PromInc Oct 11, 2021
c1fb73c
Update CollectionFactoryTest.php
PromInc Oct 11, 2021
5174e50
Update FrequencyTest.php
PromInc Oct 11, 2021
c81212c
Update QuantityTest.php
PromInc Oct 11, 2021
75999e0
Update SecurityManagerTest.php
PromInc Oct 11, 2021
47e17a5
Update en_US.csv
PromInc Oct 11, 2021
1875102
Update ResetMethodTest.php
PromInc Oct 11, 2021
f597b7a
Force invoices to be after other records
PromInc Oct 11, 2021
6e79dc6
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Oct 12, 2021
a5b81c2
Add type for sorting
PromInc Nov 8, 2021
a23b463
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
70cf0a4
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
4109ff2
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
d4c0ba5
define arguments
PromInc Nov 9, 2021
1e8d5c6
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
e65dea4
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Nov 16, 2021
451831a
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Dec 8, 2021
5ce6523
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Dec 10, 2021
84cd2f4
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
engcom-Lima Dec 10, 2021
16ecc87
Merge branch '2.4-develop' of https://github.com/PromInc/magento2 int…
engcom-Lima Dec 10, 2021
30a9d66
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
engcom-Lima Dec 14, 2021
284dad8
[BUGFIX] - Fixed static test failures
engcom-Lima Dec 14, 2021
d4d00a3
Merge branch 'magento:2.4-develop' into 2.4-develop
PromInc Dec 22, 2021
f7a746f
Use configuration for mage-messages cookie
PromInc Dec 22, 2021
30dd1a4
Create Config.php
PromInc Dec 22, 2021
0a9223d
Pass the configured the cookie domain to Javascript
PromInc Dec 22, 2021
496302d
set the cookieDomain via the config value
PromInc Dec 22, 2021
fc46a6b
Delete Config.php
PromInc Dec 22, 2021
78c55d6
Revert "set the cookieDomain via the config value"
PromInc Dec 28, 2021
ceede4b
Revert "Pass the configured the cookie domain to Javascript"
PromInc Dec 28, 2021
2ade2a7
Revert "Use configuration for mage-messages cookie"
PromInc Dec 28, 2021
54a7be0
Update function name
PromInc Dec 28, 2021
170e2fb
Merge remote-tracking branch 'magento2/2.4-develop' into 2.4-develop
engcom-Charlie Feb 24, 2022
caf892c
Issue-34304 Fixed multiple tests failurs.
engcom-Charlie Mar 1, 2022
7130231
Merge remote-tracking branch 'magento2/2.4-develop' into 2.4-develop
engcom-Charlie Mar 1, 2022
76e55e0
Issue-34304 Fixed tests failurs.
engcom-Charlie Mar 2, 2022
259be51
Issue-34304 Fixed tests failurs.
engcom-Charlie Mar 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 106 additions & 23 deletions app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@
*/
class History extends \Magento\Backend\Block\Template implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
private const HISTORY_TYPE_INVOICE = 1;
private const HISTORY_TYPE_INVOICE_COMMENT = 2;
private const HISTORY_TYPE_ORDER = 3;
private const HISTORY_TYPE_CREDIT_MEMO = 4;
private const HISTORY_TYPE_CREDIT_MEMO_COMMENT = 5;
private const HISTORY_TYPE_SHIPMENT = 6;
private const HISTORY_TYPE_SHIPMENT_COMMENT = 7;
private const HISTORY_TYPE_TRACKING_NUM = 8;

/**
* Template
* History Template
*
* @var string
*/
protected $_template = 'Magento_Sales::order/view/tab/history.phtml';

/**
* Core registry
* Core registry variable
*
* @var \Magento\Framework\Registry
*/
Expand Down Expand Up @@ -75,74 +84,94 @@ public function getFullHistory()

$history = [];
foreach ($order->getAllStatusHistory() as $orderComment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$orderComment->getId(),
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved
$orderComment->getStatusLabel(),
$orderComment->getIsCustomerNotified(),
$orderComment->getIsCustomerNotified() ?? false,
$this->getOrderAdminDate($orderComment->getCreatedAt()),
$orderComment->getComment()
$orderComment->getComment() ?? '',
self::HISTORY_TYPE_ORDER
);
}

foreach ($order->getCreditmemosCollection() as $_memo) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_memo->getId(),
__('Credit memo #%1 created', $_memo->getIncrementId()),
$_memo->getEmailSent(),
$this->getOrderAdminDate($_memo->getCreatedAt())
$_memo->getEmailSent() ?? false,
$this->getOrderAdminDate($_memo->getCreatedAt()),
'',
self::HISTORY_TYPE_CREDIT_MEMO
);

foreach ($_memo->getCommentsCollection() as $_comment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_comment->getId(),
__('Credit memo #%1 comment added', $_memo->getIncrementId()),
$_comment->getIsCustomerNotified(),
$this->getOrderAdminDate($_comment->getCreatedAt()),
$_comment->getComment()
$_comment->getComment(),
self::HISTORY_TYPE_CREDIT_MEMO_COMMENT
);
}
}

foreach ($order->getShipmentsCollection() as $_shipment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_shipment->getId(),
__('Shipment #%1 created', $_shipment->getIncrementId()),
$_shipment->getEmailSent(),
$this->getOrderAdminDate($_shipment->getCreatedAt())
$_shipment->getEmailSent() ?? false,
$this->getOrderAdminDate($_shipment->getCreatedAt()),
'',
self::HISTORY_TYPE_SHIPMENT
);

foreach ($_shipment->getCommentsCollection() as $_comment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_comment->getId(),
__('Shipment #%1 comment added', $_shipment->getIncrementId()),
$_comment->getIsCustomerNotified(),
$this->getOrderAdminDate($_comment->getCreatedAt()),
$_comment->getComment()
$_comment->getComment(),
self::HISTORY_TYPE_SHIPMENT_COMMENT
);
}
}

foreach ($order->getInvoiceCollection() as $_invoice) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_invoice->getId(),
__('Invoice #%1 created', $_invoice->getIncrementId()),
$_invoice->getEmailSent(),
$this->getOrderAdminDate($_invoice->getCreatedAt())
$_invoice->getEmailSent() ?? false,
$this->getOrderAdminDate($_invoice->getCreatedAt()),
'',
self::HISTORY_TYPE_INVOICE
);

foreach ($_invoice->getCommentsCollection() as $_comment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_comment->getId(),
__('Invoice #%1 comment added', $_invoice->getIncrementId()),
$_comment->getIsCustomerNotified(),
$this->getOrderAdminDate($_comment->getCreatedAt()),
$_comment->getComment()
$_comment->getComment(),
self::HISTORY_TYPE_INVOICE_COMMENT
);
}
}

foreach ($order->getTracksCollection() as $_track) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->prepareHistoryItemWithId(
$_track->getId(),
__('Tracking number %1 for %2 assigned', $_track->getNumber(), $_track->getTitle()),
false,
$this->getOrderAdminDate($_track->getCreatedAt())
$this->getOrderAdminDate($_track->getCreatedAt()),
'',
self::HISTORY_TYPE_TRACKING_NUM
);
}

usort($history, [__CLASS__, 'sortHistoryByTimestamp']);
usort($history, [__CLASS__, 'sortHistory']);
return $history;
}

Expand Down Expand Up @@ -218,6 +247,35 @@ protected function _prepareHistoryItem($label, $notified, $created, $comment = '
return ['title' => $label, 'notified' => $notified, 'comment' => $comment, 'created_at' => $created];
}

/**
* Map history items as array with id and type
*
* @param int $id
* @param string $label
* @param bool $notified
* @param \DateTimeInterface $created
* @param string $comment
* @param int $type
* @return array
*/
private function prepareHistoryItemWithId(
int $id,
string $label,
bool $notified,
\DateTimeInterface $created,
string $comment = '',
int $type = 0
): array {
return [
'entity_id' => $id,
'title' => $label,
'notified' => $notified,
'comment' => $comment,
'created_at' => $created,
'type' => $type
];
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -298,6 +356,7 @@ public function isCustomerNotificationNotApplicable($historyItem)
* @param mixed $a
* @param mixed $b
* @return int
* phpcs:disable
*/
public static function sortHistoryByTimestamp($a, $b)
{
Expand All @@ -307,6 +366,30 @@ public static function sortHistoryByTimestamp($a, $b)
return $createdAtA->getTimestamp() <=> $createdAtB->getTimestamp();
}

/**
* Comparison For Sorting History By Timestamp and entity_id
*
* @param array $a
* @param array $b
* @return int
* @SuppressWarnings("unused")
* phpcs:enable
*/
private function sortHistory(array $a, array $b): int
{
$result = $this->sortHistoryByTimestamp($a, $b);
if (0 !== $result) {
return $result;
}

$result = $a['type'] <=> $b['type'];
if (0 !== $result) {
return $result;
}

return $a['entity_id'] <=> $b['entity_id'];
}

/**
* Get order admin date
*
Expand Down