/
DataGridProducts.php
106 lines (87 loc) · 3.85 KB
/
DataGridProducts.php
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
<?php
namespace Backend\Modules\Commerce\Domain\Order;
use Backend\Core\Engine\DataGridDatabase;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Language\Language;
use Backend\Modules\Commerce\Domain\OrderProduct\OrderProduct;
use Backend\Modules\Commerce\Domain\Product\Product;
use Money\Currency;
use Money\Money;
use Tbbc\MoneyBundle\Formatter\MoneyFormatter;
class DataGridProducts extends DataGridDatabase
{
/**
* DataGrid constructor.
*
* @throws \Exception
*/
public function __construct(Order $order)
{
$query = '
SELECT
i.id,
i.title as product,
i.sku as article_number,
i.amount,
i.priceAmount AS price,
i.priceCurrencyCode,
i.totalAmount AS total,
i.totalCurrencyCode
FROM commerce_order_products i
WHERE i.orderId = ?
ORDER BY i.id ASC';
parent::__construct($query, [$order->getId()]);
// assign column functions
$this->setColumnHidden('id');
$this->setColumnFunction('htmlspecialchars', ['[product]'], 'product', false);
$this->setColumnFunction('htmlspecialchars', ['[sku]'], 'sku', false);
$this->setColumnFunction([self::class, 'getProductOptions'], ['[product]', '[id]'], 'product', true);
$this->setColumnFunction([self::class, 'getFormattedMoney'], ['[total]', '[totalCurrencyCode]'], 'total', true);
$this->setColumnFunction([self::class, 'getFormattedMoney'], ['[price]', '[priceCurrencyCode]'], 'price', true);
$this->setColumnsHidden(['totalCurrencyCode']);
$this->setColumnsHidden(['priceCurrencyCode']);
}
public static function getHtml(Order $order): string
{
return (new self($order))->getContent();
}
public static function getProductOptions($title, $id)
{
$orderProduct = self::getOrderProduct($id);
if (!$orderProduct) {
return $title;
}
$titleString = [];
// Check if the dimensions should be displayed
if ($orderProduct->getType() === Product::TYPE_DIMENSIONS) {
$titleString[] = '<h5 class="mt-0" style="margin-top:0;">' . $title . ' - ' . $orderProduct->getWidth() . 'mm x ' . $orderProduct->getHeight() . 'mm</h5>';
$titleString[] = '<i><strong>' . ucfirst(Language::lbl('ProductionDimensions')) . '</strong> ' . $orderProduct->getOrderWidth() . 'mm x ' . $orderProduct->getOrderHeight() . 'mm</i>';
} else {
$titleString[] = '<h5 class="mt-0" style="margin-top:0;">' . $title . '</h5>';
}
// Add extra product options
foreach ($orderProduct->getProductOptions() as $productOption) {
$productOptionTitle = '<strong>' . $productOption->getTitle() . '</strong> - ' . $productOption->getValue();
if ($productOption->getSku()) {
$productOptionTitle .= ' <strong>(' . $productOption->getSku() . ')</strong>';
}
$titleString[] = $productOptionTitle;
}
$notifications = [];
foreach ($orderProduct->getProductNotifications() as $notification) {
$notifications[] .= '<p class="m-0 text-warning" style="margin:0;">' . $notification->getMessage() . '</p>';
}
return implode('<br />', $titleString) . implode('', $notifications);
}
private static function getOrderProduct($id): ?OrderProduct
{
$repository = BackendModel::get('commerce.repository.order_product');
return $repository->findOneById($id);
}
public static function getFormattedMoney(int $amount, string $currencyCode): string
{
$money = new Money($amount, new Currency($currencyCode));
$moneyFormatter = new MoneyFormatter();
return $moneyFormatter->localizedFormatMoney($money);
}
}