Skip to content

Commit

Permalink
Merge pull request #63 from Trunkrs/develop
Browse files Browse the repository at this point in the history
Release: Feature - Add Merged PDFs (Trunkrs label and Packing slips)
  • Loading branch information
fmsthird authored Aug 31, 2023
2 parents 811c661 + 64392d6 commit 635d170
Show file tree
Hide file tree
Showing 24 changed files with 2,432 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*
.DS_Store
**/node_modules
vendor/
composer.lock
75 changes: 75 additions & 0 deletions Controller/Adminhtml/LabelAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Trunkrs\Carrier\Controller\Adminhtml;

use Magento\Framework\Exception\NotFoundException;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use setasign\Fpdi\PdfParser\PdfParserException;
use Trunkrs\Carrier\Service\Shipment\Labelling\GetLabels;
use Trunkrs\Carrier\Service\Shipment\Packingslip\GetPackingslip;
use Trunkrs\Carrier\Controller\Adminhtml\PdfDownload as GetPdf;

abstract class LabelAbstract extends Action
{
/**
* @var GetLabels
*/
protected $getLabels;

/**
* @var GetPdf
*/
protected $getPdf;

/**
* @var array
*/
protected $labels = [];

/**
* @var GetPackingslip
*/
private $getPackingSlip;

/**
* @param Context $context
* @param GetLabels $getLabels
* @param GetPdf $getPdf
* @param GetPackingslip $getPackingSlip
*/
public function __construct(
Context $context,
GetLabels $getLabels,
GetPdf $getPdf,
GetPackingslip $getPackingSlip,
) {
parent::__construct($context);

$this->getLabels = $getLabels;
$this->getPdf = $getPdf;
$this->getPackingSlip = $getPackingSlip;
}

/**
* @param $shipmentId
* @return void
* @throws NotFoundException|PdfParserException|\Zend_Pdf_Exception
*/
protected function setPackingslip($shipmentId)
{
$packingslip = $this->getPackingSlip->get($shipmentId);
if (is_array($packingslip)) {
$this->messageManager->addSuccessMessage(
__('Something went wrong.')
);
return;
}

if (strlen($packingslip) === 0) {
return;
}

$this->labels[] = $packingslip;
}
}
201 changes: 201 additions & 0 deletions Controller/Adminhtml/Order/PrintLabelAndPackingSlips.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

namespace Trunkrs\Carrier\Controller\Adminhtml\Order;

use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NotFoundException;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Shipping\Model\Shipping\LabelGenerator;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\ShipmentRepositoryInterface;
use Psr\Log\LoggerInterface;
use setasign\Fpdi\PdfParser\PdfParserException;
use Trunkrs\Carrier\Controller\Adminhtml\LabelAbstract;
use Trunkrs\Carrier\Controller\Adminhtml\PdfDownload as GetPdf;
use Trunkrs\Carrier\Service\Shipment\Labelling\GetLabels;
use Trunkrs\Carrier\Service\Shipment\Packingslip\GetPackingslip;

class PrintLabelAndPackingSlips extends LabelAbstract
{
const TRUNKRS_SHIPPING_CODE = 'trunkrsShipping_trunkrsShipping';
const TRUNKRS_LABEL_IN_PACKINGSLIPS = 'trunkrs_packingslips';

/**
* @var array
*/
protected $orderIds = [];
/**
* @var OrderCollectionFactory
*/
protected $orderCollectionFactory;
/**
* @var LabelGenerator
*/
protected $labelGenerator;
/**
* @var FileFactory
*/
protected $fileFactory;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* @var Filter
*/
private $filter;
/**
* @var ShipmentRepositoryInterface
*/
private $shipmentRepository;

/**
* @param Context $context
* @param GetLabels $getLabels
* @param GetPdf $getPdf
* @param GetPackingslip $getPackingSlip
* @param CollectionFactory $collectionFactory
* @param OrderCollectionFactory $orderCollectionFactory
* @param FileFactory $fileFactory
* @param LabelGenerator $labelGenerator
* @param ShipmentRepositoryInterface $shipmentRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param LoggerInterface $logger
* @param Filter $filter
*/
public function __construct(
Context $context,
GetLabels $getLabels,
GetPdf $getPdf,
GetPackingslip $getPackingSlip,
CollectionFactory $collectionFactory,
OrderCollectionFactory $orderCollectionFactory,
FileFactory $fileFactory,
LabelGenerator $labelGenerator,
ShipmentRepositoryInterface $shipmentRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
LoggerInterface $logger,
Filter $filter,
) {
$this->collectionFactory = $collectionFactory;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->fileFactory = $fileFactory;
$this->labelGenerator = $labelGenerator;
$this->shipmentRepository = $shipmentRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->logger = $logger;
$this->filter = $filter;
parent::__construct($context, $getLabels, $getPdf, $getPackingSlip);
}

/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|null
* @throws NotFoundException|PdfParserException|\Zend_Pdf_Exception
*/
public function execute()
{
$collection = $this->orderCollectionFactory->create();

try {
$collection = $this->filter->getCollection($collection);
} catch (LocalizedException $exception) {
$this->messageManager->addWarningMessage($exception->getMessage());
return null;
}

foreach ($collection as $order) {
$trunkrsShipment = $order->getShippingMethod() === $this::TRUNKRS_SHIPPING_CODE;
if($trunkrsShipment) {
$this->orderIds[] = $order->getId();
$this->handleShipmentDataFromOrder($order);
}
}

if (empty($this->orderIds)) {
$this->messageManager->addErrorMessage(
__('No pdf generated. Trunkrs shipment not found.')
);
return $this->_redirect($this->_redirect->getRefererUrl());
}

return $this->getPdf->get($this->labels, self::TRUNKRS_LABEL_IN_PACKINGSLIPS);
}

/**
* @param $order
* @return void
* @throws NotFoundException|PdfParserException|\Zend_Pdf_Exception
*/
private function handleShipmentDataFromOrder($order)
{
$shipments = $this->getShipmentDataByOrderId($order->getId());

if (!$shipments) {
return;
}

$this->loadLabels($shipments);
}

/**
* Shipment by Order id
*
* @param int $orderId
* @return ShipmentInterface[]|null
*/
public function getShipmentDataByOrderId(int $orderId)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('order_id', $orderId)->create();
try {
$shipments = $this->shipmentRepository->getList($searchCriteria);
$shipmentRecords = $shipments->getItems();
} catch (\Exception $exception) {
$this->logger->critical($exception->getMessage());
$shipmentRecords = null;
}
return $shipmentRecords;
}

/**
* Handle loading shipments
* @param $shipments
* @return void
* @throws NotFoundException|PdfParserException|\Zend_Pdf_Exception
*/
private function loadLabels($shipments)
{
if (!is_array($shipments)) {
$this->loadLabel($shipments);
return;
}

foreach ($shipments as $shipment) {
$this->loadLabel($shipment);
}

}

/**
* @param $shipment
* @return void
* @throws NotFoundException|\Zend_Pdf_Exception|PdfParserException
*/
private function loadLabel($shipment)
{
$this->setPackingslip($shipment->getId());
}
}
58 changes: 58 additions & 0 deletions Controller/Adminhtml/PdfDownload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Trunkrs\Carrier\Controller\Adminhtml;

use Magento\Framework\App\ResponseInterface;
use Trunkrs\Carrier\Service\Framework\FileFactory;
use Trunkrs\Carrier\Service\Shipment\Packingslip\Generate as PackingslipGenerate;

class PdfDownload
{
/**
* @var FileFactory
*/
private $fileFactory;

/**
* @var PackingslipGenerate
*/
private $packingslipGenerator;

/**
* PDF download constructor
* @param FileFactory $fileFactory
* @param PackingslipGenerate $packingslipGenerator
*/
public function __construct(
FileFactory $fileFactory,
PackingslipGenerate $packingslipGenerator,
)
{
$this->fileFactory = $fileFactory;
$this->packingslipGenerator = $packingslipGenerator;
}

/**
* @param $labels
* @param string $filename
* @return ResponseInterface
*/
public function get($labels, $filename = 'ShippingLabels')
{
$pdfLabel = $this->generateLabel($labels);

return $this->fileFactory->create(
$filename . '.pdf',
$pdfLabel
);
}

/**
* @param $labels
* @return string
*/
private function generateLabel($labels)
{
return $this->packingslipGenerator->run($labels);
}
}
Loading

0 comments on commit 635d170

Please sign in to comment.