<?php declare(strict_types=1);
namespace OrderWatcher\Subscriber;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
class OrderSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $orderRepo;
public function __construct(
EntityRepositoryInterface $orderRepo
)
{
$this->orderRepo = $orderRepo;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
OrderEvents::ORDER_LOADED_EVENT => 'onOrderLoaded',
];
}
public function onOrderLoaded(EntityLoadedEvent $event)
{
// foreach ( $event->getEntities() as $e)
// {
// $e->addExtension('string', []);
// }
}
public function onOrderWritten(EntityWrittenEvent $event)
{
$ids = $event->getIds();
$orders = [];
foreach ($ids as $id)
{
$criteria = new Criteria([$id]);
$criteria->addAssociation('deliveries.shippingMethod')
->addAssociation('deliveries.shippingOrderAddress.country')
->addAssociation('transactions.paymentMethod')
->addAssociation('lineItems')
->addAssociation('currency')
->addAssociation('products')
->addAssociation('addresses');
$orders[] = $this->orderRepo->search($criteria, Context::createDefaultContext())->getEntities();
self::processOrders($orders);
}
}
/**
* @param $orders
*
* @return void
*/
public static function processOrders($orders)
{
$orders = json_decode(json_encode($orders));
foreach ($orders[0] as $order)
{
$csvHeader = "Artikelnummer;Menge;Kommentar;Name;Händlerpreis pro Artikel;Verkaufspreis pro Artikel;Verkaufspreis pro Artikel + MwSt.;Händlerpreis Gesamt;Verkaufspreis Gesamt;Verkaufspreis + MwSt. Gesamt;\n";
$items = $order->lineItems;
$csv = '';
$id = $order->orderNumber;
$lastName = $order->orderCustomer->lastName;
$time = date('Ymd', strtotime($order->orderDateTime));
foreach ($items as $item)
{
$tax = 1 + ((int) $item->price->calculatedTaxes[0]->taxRate / 100);
$productNumber = $item->payload->productNumber ?? '___keine___';
$quantity = $item->price->quantity;
$comment = '';
$name = str_replace(':', ',', $item->label);
$sellerPrice = 0;
$grossPrice = $item->price->unitPrice;
$netPrice = round($grossPrice / $tax, 2);
$sellerTotal = 0;
$grossTotal = round($item->price->totalPrice, 2);
$netTotal = $grossTotal / $tax;
$csv .= $productNumber . ';' . $quantity . ';' . $comment . ';' . $name . ';' . self::formatNumber($sellerPrice) . ';' . self::formatNumber($netPrice) . ';' . self::formatNumber($grossPrice) . ';' . self::formatNumber($sellerTotal) . ';' . self::formatNumber($netTotal) . ';' . self::formatNumber($grossTotal) . ";\n";
}
file_put_contents(getenv('PUBLICDIR').'intern/orders/' . $id.'_'. $time . '_' . $lastName . '.csv', $csvHeader . $csv);
}
}
public static function formatNumber($number): string
{
return str_replace('.', ',', (string) sprintf('%.2f', $number)) . ' €';
}
}