Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
66 changes: 66 additions & 0 deletions Packetery/Checkout/Controller/Adminhtml/Packet/SaveDraft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Packetery\Checkout\Controller\Adminhtml\Packet;

use Laminas\Http\Request;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\NotFoundException;
use Packetery\Checkout\Model\ResourceModel\Packetdraft\CollectionFactory;

class SaveDraft extends Action implements HttpPostActionInterface
{
public const ADMIN_RESOURCE = 'Packetery_Checkout::packetery';

/**
* Save constructor.
*
* @param Context $context
* @param \Packetery\Checkout\Model\ResourceModel\PacketDraft\CollectionFactory $packetDraftCollectionFactory
*/
public function __construct(
Context $context,
private readonly CollectionFactory $packetDraftCollectionFactory
) {
parent::__construct($context);
}

/**
* @return Redirect
* @throws \Exception
*/
public function execute(): Redirect
{
/** @var Request $request */
$request = $this->getRequest();
if (!$request->isPost()) {
throw new NotFoundException(__('Page not found'));
}

$postData = $request->getPost('general');
$data = [
'order_id' => $postData['order_id'],
'value' => $postData['order_value'] === '' ? null : $postData['order_value'],
'cod' => $postData['cod_value'] === '' ? null : $postData['cod_value'],
'weight' => $postData['weight'] === '' ? null : $postData['weight'],
'length' => $postData['length'] === '' ? null : $postData['length'],
'height' => $postData['height'] === '' ? null : $postData['height'],
'width' => $postData['width'] === '' ? null : $postData['width'],
'adult_content' => $postData['adult_content'] === '' ? null : $postData['adult_content'],
'dispatch_at' => $postData['dispatch_at'] === '' ? null : $postData['dispatch_at'],
];

$this->packetDraftCollectionFactory->saveData($data);

$this->messageManager->addSuccessMessage(
__('Saved')
);

return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('sales/order/view/order_id/' . $postData['magento_order_id']);
}
}
88 changes: 80 additions & 8 deletions Packetery/Checkout/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,103 @@ public function getDefaultValues()
*/
public function getPointId(): int
{
return (int)$this->getData('point_id');
return $this->getData('point_id');
}

/**
* @return string
*/
public function getPointName(): string
{
return (string)$this->getData('point_name');
return $this->getData('point_name');
}

/**
* @return string
* @return bool
*/
public function getOrderNumber(): string
public function isAddressValidated(): bool
{
return $this->getData('order_number');
return $this->getData('address_validated');
}

/**
* @return bool
* @return int
*/
public function isAddressValidated(): bool
public function getId(): int
{
return (int)parent::getId();
}

/**
* @return float|null
*/
public function getCod(): ?float
{
return $this->getData('cod');
}

/**
* @return string|null
*/
public function getCurrency(): ?string
{
return $this->getData('currency');
}

/**
* @return float|null
*/
public function getValue(): ?float
{
return $this->getData('value');
}

/**
* @return float|null
*/
public function getWeight(): ?float
{
return $this->getData('weight');
}

/**
* @return bool|null
*/
public function hasAdultContent(): ?bool
{
return $this->getData('adult_content');
}

/**
* @return string|null
*/
public function getPlannedDispatch(): ?string
{
return $this->getData('delayed_delivery');
}

/**
* @return int|null
*/
public function getWidth(): ?int
{
return $this->getData('width');
}

/**
* @return int|null
*/
public function getHeight(): ?int
{
return $this->getData('height');
}

/**
* @return int|null
*/
public function getLength(): ?int
{
return $this->getData('address_validated') === '1';
return $this->getData('depth');
}

/**
Expand Down
24 changes: 24 additions & 0 deletions Packetery/Checkout/Model/Packetdraft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Packetery\Checkout\Model;

class Packetdraft extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
public const CACHE_TAG = 'packetery_checkout_packetdraft';

protected $_cacheTag = self::CACHE_TAG;

protected $_eventPrefix = 'packetery_checkout_packetdraft';

protected function _construct()
{
$this->_init(\Packetery\Checkout\Model\ResourceModel\Packetdraft::class);
}

public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
}
19 changes: 19 additions & 0 deletions Packetery/Checkout/Model/ResourceModel/Packetdraft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Packetery\Checkout\Model\ResourceModel;

class Packetdraft extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context
) {
parent::__construct($context);
}

protected function _construct()
{
$this->_init('packetery_packet_draft', 'id');
}
}
32 changes: 32 additions & 0 deletions Packetery/Checkout/Model/ResourceModel/Packetdraft/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Packetery\Checkout\Model\ResourceModel\Packetdraft;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected $_idFieldName = 'id';

protected $_eventPrefix = 'packetery_checkout_packetdraft_collection';

protected $_eventObject = 'packetdraft_collection';

/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Packetery\Checkout\Model\Packetdraft', 'Packetery\Checkout\Model\ResourceModel\Packetdraft');
}

/**
* @return \Packetery\Checkout\Model\Packetdraft[]
*/
public function getItems(): array
{
return parent::getItems();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Packetery\Checkout\Model\ResourceModel\Packetdraft;

use Magento\Framework\ObjectManagerInterface;

class CollectionFactory
{
/**
* Instance name to create
*
* @var string
*/
protected string $instanceName;

/**
* Factory constructor
*
* @param ObjectManagerInterface $objectManager
* @param string $instanceName
*/
public function __construct(
protected ObjectManagerInterface $objectManager,
string $instanceName = Collection::class
) {
$this->instanceName = $instanceName;
}

/**
* Create class instance with specified parameters
*
* @param array $data Class constructor arguments to override auto-wiring or specify non-service arguments.
* @return Collection
*/
public function create(array $data = []): Collection
{
/** @var Collection $collection */
$collection = $this->objectManager->create($this->instanceName, $data);

return $collection;
}

/**
* Creates a Collection specifically for inserting new entries into the DB
*
* @param array $data Class constructor arguments to override auto-wiring or specify non-service arguments.
* @return Collection
*/
public function createForDbInsert(array $data = []): Collection
{
$collection = $this->create($data);
$collection->getSelect()->where('0');

return $collection;
}

/**
* Saves Packet Draft data to DB
*
* @throws \Exception
* @param array $data
*/
public function saveData(array $data): void
{
$collection = $this->createForDbInsert();
$packetDraft = $collection->getNewEmptyItem();
$packetDraft->setData($data);
$collection->addItem($packetDraft);

$collection->save();
}
}
53 changes: 53 additions & 0 deletions Packetery/Checkout/Ui/Packetdraft/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Packetery\Checkout\Ui\Packetdraft;

use Magento\Ui\DataProvider\AbstractDataProvider;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Packetery\Checkout\Model\Order;

class DataProvider extends AbstractDataProvider
{
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $orderCollectionFactory,
private readonly \Packetery\Checkout\Model\ResourceModel\Order\CollectionFactory $orderFactory,
array $meta = [],
array $data = []
) {
$this->collection = $orderCollectionFactory->create();
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

/**
* @return array
*/
public function getData(): array
{
$result = [];
foreach ($this->collection->getItems() as $item) {
$orderNumber = $item->getDataByKey('increment_id');
/** @var Order $order */
$order = $this->orderFactory->create()->getItemByColumnValue('order_number', $orderNumber);

$result[$item->getId()]['general'] = [
'magento_order_id' => $item->getDataByKey('entity_id'),
'order_id' => $order->getId(),
'order_value' => $order->getValue(),
'cod_value' => $order->getCod(),
'weight' => $order->getWeight(),
'length' => $order->getLength(),
'height' => $order->getHeight(),
'width' => $order->getWidth(),
'adult_content' => $order->hasAdultContent() ?? false,
'dispatch_at' => $order->getPlannedDispatch(),
];
}

return $result;
}
}
Loading