magento2-docker/app/code/IpSupply/QuoteIn/Controller/Request/Create.php

117 lines
3.8 KiB
PHP
Executable File

<?php
namespace IpSupply\QuoteIn\Controller\Request;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;
use IpSupply\QuoteIn\Api\QuoteInApi;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use IpSupply\QuoteIn\Helper\LogHelper;
use GuzzleHttp\Client;
class Create extends \Magento\Framework\App\Action\Action {
protected $pageFactory;
protected $_messageManager;
protected $resultFactory;
protected $inlineTranslation;
protected $escaper;
protected $transportBuilder;
protected $logger;
protected $quoteInApi;
protected $logHelper;
const LOG_FILE = "ipsupply_quote_in.log";
public function __construct(
Context $context,
PageFactory $pageFactory,
\Magento\Framework\Message\ManagerInterface $messageManager,
StateInterface $inlineTranslation,
Escaper $escaper,
TransportBuilder $transportBuilder,
QuoteInApi $quoteInApi,
LogHelper $logHelper,
\Magento\Framework\App\Helper\Context $helperContext
)
{
$this->pageFactory = $pageFactory;
$this->messageManager = $messageManager;
$this->resultFactory = $context->getResultFactory();
$this->inlineTranslation = $inlineTranslation;
$this->escaper = $escaper;
$this->transportBuilder = $transportBuilder;
$this->logger = $helperContext->getLogger();
$this->quoteInApi = $quoteInApi;
$this->logHelper = $logHelper;
parent::__construct($context);
}
private function preparePostData($data){
if (!array_key_exists('items', $data)) {
return null;
}
for ($x = 0; $x < count($data["items"]); $x++) {
$data["items"][$x]["productId"] = "test";
if (!array_key_exists('quote_ins', $data["items"][$x])) {
$data["items"][$x]["quote_ins"] = array();
}
$quote_ins_custom = array();
foreach ($data["items"][$x]["quote_ins"] as $quote_in) {
$quote_in_custom = array();
foreach ($quote_in as $key => $value) {
$quote_in_custom[$key] = $value;
}
array_push($quote_ins_custom, $quote_in_custom) ;
}
$data["items"][$x]["quote_ins"] = $quote_ins_custom;
}
return $data;
}
private function getCustomer(){
$om = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $om->get('Magento\Customer\Model\Session');
$customer = $customerSession->getCustomer();
return $customer;
}
public function execute()
{
$customer = $this->getCustomer();
$data = $this->getRequest()->getPostValue();
if (!empty($data)) {
$this->logHelper->setName(self::LOG_FILE);
$this->quoteInApi->setLogHelper($this->logHelper);
$post_data_to_erp = array(
"urlAPI" => "/api/magento/quotein-save",
"data" => $data["data"]
);
$response = $this->quoteInApi->login();
if ($response) {
//$this->logHelper->write($this->quoteInApi->getToken());
$rep = $this->quoteInApi->saveQuoteInToERP($post_data_to_erp);
if ($rep) {
$this->messageManager->addSuccess(__("Thank you. We'll get back to you very soon."));
} else {
$this->messageManager->addError(__("Error, Please try again"));
}
} else {
$this->messageManager->addError(__("Error, Please try again"));
}
}
return $this->pageFactory->create();
}
}