<?php
namespace Nextag\Checkout\Subscriber;
use Nextag\Checkout\Content\Checkout\CheckoutService;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
class OrderPlaced implements EventSubscriberInterface
{
private $requestStack;
private $session;
private $checkoutService;
public function __construct(RequestStack $requestStack,
CheckoutService $checkoutService,
\Nextag\Checkout\Helper\Log $log,
Session $session
)
{
$this->requestStack = $requestStack;
$this->checkoutService = $checkoutService;
$this->log = $log;
$this->session = $session;
}
public static function getSubscribedEvents()
{
return [
CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
];
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
{
try {
$request = $this->requestStack->getCurrentRequest();
$comment = $request->get('next_checkout_comment');
$deliverydate = $request->get('date');
$deliverytype = $request->get('next_checkout_deliverytype');
$deliverylocation = $request->get('next_checkout_deliverylocation');
$deliverylocationcomment = $request->get('next_checkout_deliverylocationcomment');
$giftmessagefrom = $request->get('next_checkout_giftmessagefrom');
$giftmessageto = $request->get('next_checkout_giftmessageto');
$giftmessage = $request->get('next_checkout_giftmessage');
$giftbox1qty = $request->get('next_checkout_giftbox1qty');
$giftbox2qty = $request->get('next_checkout_giftbox2qty');
$giftbox3qty = $request->get('next_checkout_giftbox3qty');
$giftbox4qty = $request->get('next_checkout_giftbox4qty');
$giftbox5qty = $request->get('next_checkout_giftbox5qty');
$giftbox6qty = $request->get('next_checkout_giftbox6qty');
$giftbox7qty = $request->get('next_checkout_giftbox7qty');
$giftbox8qty = $request->get('next_checkout_giftbox8qty');
$giftbox1comment = $request->get('next_checkout_giftbox1comment');
$giftbox2comment = $request->get('next_checkout_giftbox2comment');
$giftbox3comment = $request->get('next_checkout_giftbox3comment');
$giftbox4comment = $request->get('next_checkout_giftbox4comment');
$giftbox5comment = $request->get('next_checkout_giftbox5comment');
$giftbox6comment = $request->get('next_checkout_giftbox6comment');
$giftbox7comment = $request->get('next_checkout_giftbox7comment');
$giftbox8comment = $request->get('next_checkout_giftbox8comment');
$retouren = $request->get('next_checkout_retouren');
$voigtkundennummer = $request->get('next_checkout_voigtkundennummer');
//TODO erweitern
$language = $request->get('next_checkout_language');
$data = [];
if ($comment && gettype($comment) == 'string') {
$data["comment"] = $comment;
}
if ($deliverydate && gettype($deliverydate) == 'string') {
$data["deliverydate"] = $deliverydate;
}
if ($deliverytype && gettype($deliverytype) == 'string') {
$data["deliverytype"] = $deliverytype;
}
if ($deliverylocation && gettype($deliverylocation) == 'string') {
$data["deliverylocation"] = $deliverylocation;
}
if ($deliverylocationcomment && gettype($deliverylocationcomment) == 'string') {
$data["deliverylocationcomment"] = $deliverylocationcomment;
}
if ($giftmessagefrom && gettype($giftmessagefrom) == 'string') {
$data["giftmessagefrom"] = $giftmessagefrom;
}
if ($giftmessageto && gettype($giftmessageto) == 'string') {
$data["giftmessageto"] = $giftmessageto;
}
if ($giftmessage && gettype($giftmessage) == 'string') {
$data["giftmessage"] = $giftmessage;
}
if ($giftbox1qty && is_numeric($giftbox1qty)) {
$data["giftbox1qty"] =(int) $giftbox1qty;
}
if ($giftbox2qty && is_numeric($giftbox2qty)) {
$data["giftbox2qty"] = (int) $giftbox2qty;
}
if ($giftbox3qty && is_numeric($giftbox3qty)) {
$data["giftbox3qty"] = (int) $giftbox3qty;
}
if ($giftbox4qty && is_numeric($giftbox4qty)) {
$data["giftbox4qty"] = (int) $giftbox4qty;
}
if ($giftbox5qty && is_numeric($giftbox5qty)) {
$data["giftbox5qty"] = (int) $giftbox5qty;
}
if ($giftbox6qty && is_numeric($giftbox6qty)) {
$data["giftbox6qty"] = (int) $giftbox6qty;
}
if ($giftbox7qty && is_numeric($giftbox7qty)) {
$data["giftbox7qty"] = (int) $giftbox7qty;
}
if ($giftbox8qty && is_numeric($giftbox8qty)) {
$data["giftbox8qty"] = (int) $giftbox8qty;
}
if ($giftbox1comment && gettype($giftbox1comment) == 'string') {
$data["giftbox1comment"] = $giftbox1comment;
}
if ($giftbox2comment && gettype($giftbox2comment) == 'string') {
$data["giftbox2comment"] = $giftbox2comment;
}
if ($giftbox3comment && gettype($giftbox3comment) == 'string') {
$data["giftbox3comment"] = $giftbox3comment;
}
if ($giftbox4comment && gettype($giftbox4comment) == 'string') {
$data["giftbox4comment"] = $giftbox4comment;
}
if ($giftbox5comment && gettype($giftbox5comment) == 'string') {
$data["giftbox5comment"] = $giftbox5comment;
}
if ($giftbox6comment && gettype($giftbox6comment) == 'string') {
$data["giftbox6comment"] = $giftbox6comment;
}
if ($giftbox7comment && gettype($giftbox7comment) == 'string') {
$data["giftbox7comment"] = $giftbox7comment;
}
if ($giftbox8comment && gettype($giftbox8comment) == 'string') {
$data["giftbox7comment"] = $giftbox8comment;
}
if ($retouren && gettype($retouren) == 'string') {
$data["retouren"] = $retouren;
}
if ($voigtkundennummer && gettype($voigtkundennummer) == 'string') {
$data["voigtkundennummer"] = $voigtkundennummer;
}
if ($language && gettype($language) == 'string') {
$data["language"] = $language;
} else {
$language = "de";
}
if (!empty($data)) {
if (!isset($data["comment"]) || $data["comment"] == null) {
$data["comment"] = "";
}
if (!isset($data["retouren"]) || $data["retouren"] == null) {
$data["retouren"] = "";
}
if (!isset($data["giftmessage"]) || $data["giftmessage"] == null) {
$data["giftmessage"] = "";
}
if ($this->session->get("pos") == "SG") {
$data["orderedBy"] = "POS St. Gallen";
} elseif ($this->session->get("pos") == "ZH") {
$data["orderedBy"] = "POS St. Zürich";
} else {
$data["orderedBy"] = "Kunde";
}
$this->checkoutService->save($data, $event);
}
} catch (\Exception $exception) {
$this->log->error($exception);
}
}
}