Skip to content

Commit

Permalink
WIP add returns qty to sku custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
supercid committed Dec 11, 2023
1 parent 1ef10ca commit 4f0d5d8
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion Model/Product/Sku/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
use Nosto\Tagging\Model\Service\Stock\StockService;
use Nosto\Types\Product\ProductInterface;

use Magento\Sales\Model\OrderRepository;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Sales\Model\Order\ItemRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;

Check warning on line 59 in Model/Product/Sku/Builder.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

There must be one blank line after the last USE statement; 2 found;


// @codingStandardsIgnoreLine

class Builder
Expand Down Expand Up @@ -84,6 +90,11 @@ class Builder
/** @var StockService */
private StockService $stockService;

private $orderRepository;
private $orderCollectionFactory;
private $orderItemRepository;
private $searchCriteriaBuilder;

/**
* Builder constructor.
* @param NostoDataHelper $nostoDataHelper
Expand All @@ -105,7 +116,11 @@ public function __construct(
AttributeServiceInterface $attributeService,
AvailabilityService $availabilityService,
ImageService $imageService,
StockService $stockService
StockService $stockService,
OrderRepository $orderRepository,
OrderCollectionFactory $orderCollectionFactory,
ItemRepository $orderItemRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->nostoDataHelper = $nostoDataHelper;
$this->nostoPriceHelper = $priceHelper;
Expand All @@ -116,6 +131,10 @@ public function __construct(
$this->availabilityService = $availabilityService;
$this->imageService = $imageService;
$this->stockService = $stockService;
$this->orderRepository = $orderRepository;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->orderItemRepository = $orderItemRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
Expand Down Expand Up @@ -174,6 +193,8 @@ public function build(
}
}
}
$nostoSku->addCustomField('returns', $this->getReturnRate($product->getId()));
$nostoSku->addCustomField('##id is:', $product->getId());
if ($this->nostoDataHelper->isInventoryTaggingEnabled($store)) {
$nostoSku->setInventoryLevel($this->stockService->getQuantity($product, $store));
}
Expand Down Expand Up @@ -203,4 +224,45 @@ private function buildSkuAvailability(Product $product, Store $store)

return ProductInterface::OUT_OF_STOCK;
}

public function getReturnRate($productId)
{
$orderCollection = $this->orderCollectionFactory->create();
$orderCollection->addFieldToFilter('status', ['in' => ['complete', 'closed']]);

$orderItems = [];
foreach ($orderCollection as $order) {
foreach ($order->getAllVisibleItems() as $item) {
$orderItems[] = $item->getProductId();
}
}

$totalOrders = count($orderItems);
$returnedOrders = $this->getReturnedOrders($productId);
return (string)$returnedOrders;
// if ($totalOrders > 0) {
// $returnRate = $returnedOrders / $totalOrders * 100;
// return $returnRate;
// }

// return 0;
}

private function getReturnedOrders($productId)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('product_id', $productId)
// ->addFilter('product_id', "1812")
// ->addFilter('qty_invoiced', 0, '>')
->create();

$returnedOrders = 0;
$orderItems = $this->orderItemRepository->getList($searchCriteria)->getItems();

Check failure on line 260 in Model/Product/Sku/Builder.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to method getItems on non-class type \Magento\Sales\Api\Data\OrderItemInterface[]

foreach ($orderItems as $item) {
$returnedOrders += $item->getQtyRefunded();
}

return $returnedOrders;
}
}

0 comments on commit 4f0d5d8

Please sign in to comment.