Skip to content

Commit 785ee77

Browse files
committed
Added new features && code Improvements
1 parent 570787a commit 785ee77

File tree

18 files changed

+1220
-246
lines changed

18 files changed

+1220
-246
lines changed

Api/OrderListInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
interface OrderListInterface
1212
{
1313
/**
14-
* Get order list with grand total as configured threshold and within defined date range
14+
* Get order list with grand total above configured threshold and within a defined date range.
1515
*
16-
* @return \Magento\Sales\Api\Data\OrderInterface[]|array
16+
* @return \Magento\Sales\Model\ResourceModel\Order\Collection|null
1717
*/
18-
public function getOrders(): array;
18+
public function getOrders(): ?\Magento\Sales\Model\ResourceModel\Order\Collection;
1919
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* ManiyaTech
4+
*
5+
* @author Milan Maniya
6+
* @package ManiyaTech_OrderApi
7+
*/
8+
9+
namespace ManiyaTech\OrderApi\Block\Adminhtml\System\Config\Form\Field;
10+
11+
use Magento\Framework\View\Element\Html\Select;
12+
use Magento\Framework\Data\OptionSourceInterface;
13+
14+
class OrderAttributeCode extends Select
15+
{
16+
/**
17+
* Render the HTML for the select element.
18+
*
19+
* @return string
20+
*/
21+
protected function _toHtml(): string
22+
{
23+
if (!$this->getOptions()) {
24+
$this->setOptions($this->getOrderAttributes());
25+
}
26+
27+
$this->setName($this->getInputName());
28+
$this->setId($this->getInputId());
29+
$this->setClass('select admin__control-select');
30+
$this->setTitle(__('Select Order Attribute Code'));
31+
return parent::_toHtml();
32+
}
33+
34+
/**
35+
* Get list of available order attributes to be used in the select field.
36+
*
37+
* @return array<int, array{value: string, label: \Magento\Framework\Phrase}>
38+
*/
39+
protected function getOrderAttributes(): array
40+
{
41+
return [
42+
['value' => 'increment_id', 'label' => __('Increment ID')],
43+
['value' => 'status', 'label' => __('Order Status')],
44+
['value' => 'state', 'label' => __('Order State')],
45+
['value' => 'created_at', 'label' => __('Order Date')],
46+
['value' => 'updated_at', 'label' => __('Updated At')],
47+
['value' => 'customer_firstname', 'label' => __('Customer Name')],
48+
['value' => 'customer_email', 'label' => __('Customer Email')],
49+
['value' => 'customer_group_id', 'label' => __('Customer Group')],
50+
['value' => 'coupon_code', 'label' => __('Coupon Code')],
51+
['value' => 'shipping_method', 'label' => __('Shipping Method')],
52+
['value' => 'shipping_description', 'label' => __('Shipping Description')],
53+
['value' => 'shipping_amount', 'label' => __('Shipping Amount')],
54+
['value' => 'shipping_incl_tax', 'label' => __('Shipping Incl. Tax')],
55+
['value' => 'shipping_tax_amount', 'label' => __('Shipping Tax Amount')],
56+
['value' => 'subtotal', 'label' => __('Subtotal')],
57+
['value' => 'discount_amount', 'label' => __('Discount Amount')],
58+
['value' => 'grand_total', 'label' => __('Grand Total')],
59+
['value' => 'base_grand_total', 'label' => __('Base Grand Total')],
60+
['value' => 'tax_amount', 'label' => __('Tax Amount')],
61+
['value' => 'total_due', 'label' => __('Total Due')],
62+
['value' => 'total_item_count', 'label' => __('Total Item Count')],
63+
['value' => 'total_qty_ordered', 'label' => __('Total Qty Ordered')],
64+
['value' => 'billing_address_id', 'label' => __('Billing Address')],
65+
['value' => 'shipping_address_id', 'label' => __('Shipping Address')],
66+
];
67+
}
68+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* ManiyaTech
4+
*
5+
* @author Milan Maniya
6+
* @package ManiyaTech_OrderApi
7+
*/
8+
9+
namespace ManiyaTech\OrderApi\Block\Adminhtml\System\Config\Form\Field;
10+
11+
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
12+
use Magento\Framework\Data\OptionSourceInterface;
13+
use Magento\Framework\View\Element\Html\Select;
14+
use ManiyaTech\OrderApi\Block\Adminhtml\System\Config\Form\Field\OrderAttributeCode;
15+
16+
class OrderExportFields extends AbstractFieldArray
17+
{
18+
/**
19+
* @var \Magento\Framework\Data\Form\Element\Renderer\RendererInterface
20+
*/
21+
protected $renderer;
22+
23+
/**
24+
* Prepare to render dynamic rows
25+
*/
26+
protected function _prepareToRender()
27+
{
28+
$this->addColumn('order_title', [
29+
'label' => __('Label'),
30+
'class' => 'required-entry',
31+
]);
32+
33+
$this->addColumn('order_code', [
34+
'label' => __('Code'),
35+
'renderer' => $this->getCodeRenderer()
36+
]);
37+
38+
$this->_addAfter = false;
39+
$this->_addButtonLabel = __('Add Attribute');
40+
}
41+
42+
/**
43+
* Get order attribute code renderer for dynamic rows in system config.
44+
*/
45+
protected function getCodeRenderer()
46+
{
47+
if (!$this->renderer) {
48+
$this->renderer = $this->getLayout()->createBlock(
49+
OrderAttributeCode::class,
50+
'',
51+
['data' => ['is_render_to_js_template' => true]]
52+
);
53+
}
54+
return $this->renderer;
55+
}
56+
57+
/**
58+
* Prepare each row with selected option.
59+
*
60+
* @param \Magento\Framework\DataObject $row
61+
* @return void
62+
*/
63+
protected function _prepareArrayRow(\Magento\Framework\DataObject $row): void
64+
{
65+
$options = [];
66+
$code = $row->getData('order_code');
67+
if ($code !== null) {
68+
$options['option_' . $this->getCodeRenderer()->calcOptionHash($code)] = 'selected="selected"';
69+
}
70+
$row->setData('option_extra_attrs', $options);
71+
}
72+
}

Console/Command/OrderListCommand.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,70 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
return Command::SUCCESS;
9090
}
9191

92+
$selectedOrderFields = $this->orderList->getSelectedOrderFields();
93+
$defaultOrderFields = $this->orderList->getDefaultOrderFields();
94+
$finalFields = $selectedOrderFields ?: $defaultOrderFields;
95+
9296
foreach ($orders as $order) {
93-
$output->writeln(str_repeat('-', 50));
94-
$output->writeln("Order ID : {$order['increment_id']}");
95-
$output->writeln("Customer Name : " . ($order['billing_name'] ?? 'N/A'));
96-
$output->writeln("Total Amount : {$order['grand_total']}");
97-
$output->writeln("Status : " . ($order['order_status'] ?? 'N/A'));
98-
$output->writeln("Date : " . ($order['order_date'] ?? 'N/A'));
99-
$output->writeln("Payment Method : " . ($order['payment_title'] ?? $order['payment_method'] ?? 'N/A'));
100-
$output->writeln("Shipping Desc : " . ($order['shipping_description'] ?? 'N/A'));
101-
$output->writeln("Shipping Amt : " . ($order['shipping_amount'] ?? '0'));
102-
$output->writeln("Shipping Incl : " . ($order['shipping_incl_tax'] ?? '0'));
97+
$output->writeln(str_repeat('-', 100));
98+
foreach ($finalFields as $field) {
99+
$code = $field['order_code'];
100+
$value = $order->getData($code) ?? '';
101+
$lable = $field['order_title'];
102+
switch ($code) {
103+
case 'status':
104+
$value = ucfirst((string) $value);
105+
$output->writeln("$lable : {$value}");
106+
break;
107+
108+
case 'created_at':
109+
case 'updated_at':
110+
$value = $value ? date('d-m-Y', strtotime((string) $value)) : '';
111+
$output->writeln("$lable : {$value}");
112+
break;
113+
114+
case 'customer_firstname':
115+
$firstname = $order->getData('customer_firstname') ?? '';
116+
$lastname = $order->getData('customer_lastname') ?? '';
117+
$value = trim($firstname . ' ' . $lastname);
118+
$output->writeln("$lable : {$value}");
119+
break;
120+
121+
case 'customer_group_id':
122+
$groupId = $order->getData('customer_group_id');
123+
$value = $this->orderList->getCustomerGroupName((int) $groupId);
124+
$output->writeln("$lable : {$value}");
125+
break;
126+
127+
case 'shipping_amount':
128+
case 'shipping_incl_tax':
129+
case 'shipping_tax_amount':
130+
case 'subtotal':
131+
case 'discount_amount':
132+
case 'grand_total':
133+
case 'base_grand_total':
134+
case 'tax_amount':
135+
case 'total_due':
136+
$value = $this->orderList->getFormattedPrice((float) $value, $order->getOrderCurrencyCode());
137+
$output->writeln("$lable : {$value}");
138+
break;
139+
140+
case 'billing_address_id':
141+
$value = $this->orderList->formatAddress($order->getBillingAddress());
142+
$output->writeln("$lable : {$value}");
143+
break;
144+
145+
case 'shipping_address_id':
146+
$value = $this->orderList->formatAddress($order->getShippingAddress());
147+
$output->writeln("$lable : {$value}");
148+
break;
149+
150+
default:
151+
$value = (string) $value;
152+
$output->writeln("$lable : {$value}");
153+
break;
154+
}
155+
}
103156
}
104157

105158
return Command::SUCCESS;

0 commit comments

Comments
 (0)