1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* @author Mediotype Developement <diveinto@mediotype.com>
* @copyright 2018 Mediotype. All rights reserved.
*/
namespace Vertex\Tax\Model\Plugin;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Message\MessageInterface;
use Magento\Quote\Api\Data\TotalSegmentExtensionFactory;
use Magento\Quote\Api\Data\TotalSegmentInterface;
use Magento\Quote\Model\Cart\TotalsConverter;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Vertex\Tax\Model\Calculator;
use Vertex\Tax\Model\Config;
/**
* Add extension attribute 'vertex-messages' containing all vertex message from calculation
*
* @see TotalsConverter
*/
class TotalsCalculationMessagePlugin
{
/** @var Config */
private $config;
/** @var LoggerInterface */
private $logger;
/** @var ManagerInterface */
private $messageManager;
/** @var StoreManagerInterface */
private $storeManager;
/** @var TotalSegmentExtensionFactory */
private $totalSegmentExtensionFactory;
/**
* @param TotalSegmentExtensionFactory $totalSegmentExtensionFactory
* @param ManagerInterface $messageManager
* @param Config $config
* @param StoreManagerInterface $storeManager
*/
public function __construct(
TotalSegmentExtensionFactory $totalSegmentExtensionFactory,
ManagerInterface $messageManager,
Config $config,
StoreManagerInterface $storeManager,
LoggerInterface $logger
) {
$this->totalSegmentExtensionFactory = $totalSegmentExtensionFactory;
$this->messageManager = $messageManager;
$this->config = $config;
$this->storeManager = $storeManager;
$this->logger = $logger;
}
/**
* Add any Vertex error messages to the tax totals data
*
* @see TotalsConverter::process()
* @param TotalsConverter $subject
* @param callable $super
* @param Total[] $addressTotals
* @return TotalSegmentInterface[]
*/
public function aroundProcess(
TotalsConverter $subject,
callable $super,
$addressTotals = []
) {
// Allows forward compatibility with argument additions
$arguments = func_get_args();
array_splice($arguments, 0, 2);
/** @var TotalSegmentInterface[] $totalSegment */
$totalSegments = call_user_func_array($super, $arguments);
$storeId = null;
try {
if ($currentStore = $this->storeManager->getStore()) {
$storeId = $currentStore->getId();
}
} catch (NoSuchEntityException $e) {
$this->logger->error($e->getMessage());
}
if (!$this->config->isVertexActive($storeId) || !$this->config->isTaxCalculationEnabled($storeId)) {
return $totalSegments;
}
if (!array_key_exists('tax', $addressTotals)) {
return $totalSegments;
}
$taxes = $addressTotals['tax']->getData();
if (!array_key_exists('full_info', $taxes)) {
return $totalSegments;
}
$messageCollection = $this->messageManager->getMessages(true, Calculator::MESSAGE_KEY);
if (!$messageCollection->getCount()) {
return $totalSegments;
}
$attributes = $totalSegments['tax']->getExtensionAttributes();
if ($attributes === null) {
$attributes = $this->totalSegmentExtensionFactory->create();
}
$attributes->setVertexTaxCalculationMessages(
array_map(
function (MessageInterface $message) {
return $message->getText();
},
$messageCollection->getItems()
)
);
$totalSegments['tax']->setExtensionAttributes($attributes);
return $totalSegments;
}
}