ItemProcessor.php 2.67 KB
Newer Older
Ketan's avatar
Ketan committed
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
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\Api\Data\InvoiceRequestBuilder;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaBuilderFactory;

/**
 * Contains logic common to processing items on Invoices and Creditmemos
 */
class ItemProcessor
{
    /** @var SearchCriteriaBuilderFactory */
    private $criteriaBuilderFactory;

    /** @var ProductRepositoryInterface */
    private $productRepository;

    /**
     * @param SearchCriteriaBuilderFactory $criteriaBuilderFactory
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        SearchCriteriaBuilderFactory $criteriaBuilderFactory,
        ProductRepositoryInterface $productRepository
    ) {
        $this->criteriaBuilderFactory = $criteriaBuilderFactory;
        $this->productRepository = $productRepository;
    }

    /**
     * Fetch all utilized products from the database by SKU
     *
     * This was previously done by ID, but we had an issue with configurable products
     * in that the child did not have the total but the parent did, and the parent
     * was attached to the parent product.  This isn't bueno, since we need
     * to use the child's tax class for records.
     *
     * So.. that didn't work.  Instead, we're using SKU.  In the vast majority
     * of scenarios the SKU should not change on the product.
     *
     * The correct way to "fix" this would be to attach the necessary product
     * data to the order, and subsequently the invoice at the time of creation,
     * however we'd still have a problem if that data were missing
     * (b/c Vertex was disabled or older versions or any number of scenarios)
     *
     * @param string[] $productSku
     * @return ProductInterface[] Indexed by sku
     */
    public function getProductsIndexedBySku(array $productSku)
    {
        /** @var SearchCriteriaBuilder $criteriaBuilder */
        $criteriaBuilder = $this->criteriaBuilderFactory->create();
        $criteriaBuilder->addFilter(ProductInterface::SKU, $productSku, 'in');
        $criteria = $criteriaBuilder->create();

        $items = $this->productRepository->getList($criteria)->getItems();

        /** @var ProductInterface[] $products */
        return array_reduce(
            $items,
            function (array $carry, ProductInterface $product) {
                $carry[$product->getSku()] = $product;
                return $carry;
            },
            []
        );
    }
}