225 lines
8.0 KiB
PHP
Executable File
225 lines
8.0 KiB
PHP
Executable File
<?php
|
|
namespace IpSupply\Prology\Api\Controller;
|
|
|
|
use Magento\Framework\App\Helper\Context;
|
|
use IpSupply\Prology\Api\Interface\ErpInterface;
|
|
|
|
class ErpApi extends \Magento\Framework\App\Helper\AbstractHelper implements ErpInterface
|
|
{
|
|
const AUT_KEY = "$2y$10$5nnpVLdhnU1cfWQqn3n/xu4ZA6L.65GZWLVYLSBX.vSOiA/yu6I7K";
|
|
|
|
protected $request;
|
|
protected $productHelper;
|
|
protected $categoryHelper;
|
|
|
|
public function __construct(
|
|
Context $context,
|
|
\Magento\Framework\Webapi\Rest\Request $request,
|
|
\IpSupply\Prology\Helper\ProductHelper $productHelper,
|
|
\IpSupply\Prology\Helper\CategoryHelper $categoryHelper
|
|
) {
|
|
$this->productHelper = $productHelper;
|
|
$this->categoryHelper = $categoryHelper;
|
|
$this->request = $request;
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function checkAut() {
|
|
$aut = "";
|
|
if (isset($_SERVER["HTTP_AUT"])) {
|
|
$aut = $_SERVER["HTTP_AUT"];
|
|
} else if (isset($_SERVER["REDIRECT_HTTP_AUT"])) {
|
|
$aut = $_SERVER["REDIRECT_HTTP_AUT"];
|
|
}
|
|
if (self::AUT_KEY != $aut) {
|
|
header("HTTP/1.1 401 Unauthorized");
|
|
die;
|
|
}
|
|
}
|
|
|
|
public function getMessage($status, $message, $ProductId, $sku, $productUrl) {
|
|
return array(
|
|
"status" => $status,
|
|
"message" => $message,
|
|
"ProductId" => $ProductId,
|
|
"sku" => $sku,
|
|
"productUrl" => $productUrl
|
|
);
|
|
}
|
|
|
|
/**
|
|
* POST
|
|
* @return string
|
|
*/
|
|
public function createProductFromErp(){
|
|
|
|
$this->checkAut();
|
|
$result = [];
|
|
$items = (array) json_decode(file_get_contents('php://input'), TRUE);
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$logHelper = $objectManager->create('\IpSupply\Prology\Helper\LogHelper');
|
|
$logHelper->setDefaultName();
|
|
$logHelper->write(time());
|
|
$logHelper->write(json_encode($items));
|
|
|
|
foreach ($items as $item) {
|
|
try {
|
|
$product = $this->productHelper->createProductFromErp($item);
|
|
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
|
|
$baseUrl = $storeManager->getStore()->getBaseUrl();
|
|
|
|
$productUrl = $baseUrl."product/".$product->getUrlKey();
|
|
if ($product != null) {
|
|
$result[] = $this->getMessage(true, "", $product->getId(), $item["sku"], $productUrl);
|
|
} else {
|
|
$result[] = $this->getMessage(false, "", null, $item["sku"], null);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$logHelper->write("______error_______");
|
|
$logHelper->write(json_encode(json_encode($item)));
|
|
$result[] = $this->getMessage(false, $e->getMessage(), null, $item["sku"], null);
|
|
}catch (\Exception $e) {
|
|
$logHelper->write("______error_______");
|
|
$logHelper->write(json_encode(json_encode($item)));
|
|
$result[] = $this->getMessage(false, $e->getMessage(), null, $item["sku"], null);
|
|
}
|
|
|
|
}
|
|
echo json_encode($result);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* POST
|
|
* @return string
|
|
*/
|
|
public function deleteProductFromErp(){
|
|
|
|
$this->checkAut();
|
|
$items = (array) json_decode(file_get_contents('php://input'), TRUE);
|
|
if (!array_key_exists('sku', $items)) {
|
|
echo json_encode($this->getMessage(false, "sku not exist", null, null, null));
|
|
exit;
|
|
}
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$configurableSku = $items["sku"];
|
|
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
|
|
try {
|
|
$configurableProduct = $productRepository->get($configurableSku);
|
|
|
|
// Delete configurable product
|
|
if ($configurableProduct->getId()) {
|
|
|
|
// Delete child products
|
|
if ($configurableProduct->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
|
|
$childProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
|
|
foreach ($childProducts as $childProduct) {
|
|
$productRepository->delete($childProduct);
|
|
}
|
|
}
|
|
|
|
$productRepository->delete($configurableProduct);
|
|
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
|
|
$baseUrl = $storeManager->getStore()->getBaseUrl();
|
|
|
|
$productUrl = $baseUrl."product/".$configurableProduct->getUrlKey();
|
|
echo json_encode($this->getMessage(true, "", $configurableProduct->getId(), $items["sku"], $productUrl));
|
|
exit;
|
|
}
|
|
} catch (\Exception $e) {
|
|
echo json_encode($this->getMessage(false, $e->getMessage(), null, $items["sku"], null));
|
|
exit;
|
|
}
|
|
echo json_encode($this->getMessage(false, "product not exist", null, $items["sku"], null));
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* get
|
|
* @return string
|
|
*/
|
|
public function getAllCategories() {
|
|
//$this->checkAut();
|
|
echo json_encode($this->categoryHelper->getAllCategories());
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* POST
|
|
* @return string
|
|
*/
|
|
public function updatePriceAndQty(){
|
|
$this->checkAut();
|
|
$data = (array) json_decode(file_get_contents('php://input'), TRUE);
|
|
$product = $this->productHelper->updatePriceAndQty($data);
|
|
|
|
$productParent = $this->productHelper->getProductBySku($data["sku"]);
|
|
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
|
|
$baseUrl = $storeManager->getStore()->getBaseUrl();
|
|
|
|
$productUrl = "";
|
|
if ($productParent) {
|
|
$productUrl = $baseUrl."product/".$productParent->getUrlKey();
|
|
}
|
|
|
|
if ($product != null) {
|
|
echo json_encode($this->getMessage(true, "", $product->getId(), $data["sku"], $productUrl));
|
|
exit;
|
|
}
|
|
header("HTTP/1.1 404 Product Not Found");
|
|
echo json_encode($this->getMessage(false, "Product Unavailable", null, $data["sku"], null));
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* GET
|
|
* @return array
|
|
*/
|
|
public function getAllBrands() {
|
|
$response = array();
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$product = $objectManager->create('\Magento\Catalog\Model\Product');
|
|
$product_resource = $product->getResource();
|
|
$types_attribute = $product_resource->getAttribute('brands');
|
|
if ($types_attribute != false && $types_attribute->usesSource()) {
|
|
$options = $types_attribute->getSource()->getAllOptions();
|
|
foreach ($options as $option) {
|
|
$label = trim(strtoupper($option["label"]));
|
|
if (!empty($label)) {
|
|
$response[] = $label;
|
|
}
|
|
}
|
|
}
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* GET
|
|
* @return array
|
|
*/
|
|
public function getAllTypes() {
|
|
$response = array();
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$product = $objectManager->create('\Magento\Catalog\Model\Product');
|
|
$product_resource = $product->getResource();
|
|
$types_attribute = $product_resource->getAttribute('types');
|
|
if ($types_attribute != false && $types_attribute->usesSource()) {
|
|
$options = $types_attribute->getSource()->getAllOptions();
|
|
foreach ($options as $option) {
|
|
$label = trim(strtoupper($option["label"]));
|
|
if (!empty($label)) {
|
|
$response[] = $label;
|
|
}
|
|
}
|
|
}
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
}
|