Files

230 lines
6.8 KiB
PHP
Executable File

<?php
namespace JR\NetSuite\Mapper;
use JR\NetSuite\Mapper\AbstractMapper;
use JR\NetSuite\Helper\Data as Helper;
use Magento\Catalog\Api\ProductRepositoryInterface;
use \Mirasvit\Rma\Api\Repository\RmaRepositoryInterface;
use \Mirasvit\Rma\Api\Service\Rma\RmaManagementInterface;
use \Mirasvit\Rma\Api\Service\Item\ItemListBuilderInterface;
use \Mirasvit\Rma\Api\Service\Item\ItemManagementInterface;
// Handles the specifics of mapping rma data.
class RmaMapper
{
public const STATUS_MAP = [
'pending-approval' => 'pending',
'Pending-receipt' => 'pending',
'partially-received' => 'pending',
'pending-refund/partially-received' => 'pending',
'pending-refund' => 'pending',
'refunded' => 'refunded',
'cancelled' => 'closed',
'closed' => 'closed',
'pendingApproval' => 'pending',
'pendingReceipt' => 'pending',
'partiallyReceived' => 'pending',
'pendingRefund/partiallyReceived' => 'pending',
'pendingRefund' => 'pending',
];
/**
* @var Helper
*/
protected $helper;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var RmaRepositoryInterface
*/
protected $rmaRepository;
/**
* @var RmaManagementInterface
*/
protected $rmaManagement;
/**
* @var ItemListBuilderInterface
*/
protected $itemListBuilder;
/**
* @var ItemManagementInterface
*/
protected $itemManagement;
/**
* @var \JR\NetSuite\Mapper\CustomerMapper
*/
private $customerMapper;
/**
* RmaMapper constructor.
*
* @param RmaRepositoryInterface $rmaRepository
* @return void
*/
public function __construct(
Helper $helper,
ProductRepositoryInterface $productRepository,
RmaRepositoryInterface $rmaRepository,
RmaManagementInterface $rmaManagement,
ItemListBuilderInterface $itemListBuilder,
ItemManagementInterface $itemManagement,
\JR\NetSuite\Mapper\CustomerMapper $customerMapper
) {
$this->helper = $helper;
$this->productRepository = $productRepository;
$this->rmaRepository = $rmaRepository;
$this->rmaManagement = $rmaManagement;
$this->itemListBuilder = $itemListBuilder;
$this->itemManagement = $itemManagement;
$this->customerMapper = $customerMapper;
}
/**
* Convert data from NetSuite format to Magento format.
*
* @param array $netSuiteData
* @return array
*/
public function convertToMagentoFormat($netSuiteData)
{
// convert netsuite data to magento data
$magentoData = [];
foreach ($netSuiteData as $data) {
$magentoData[] = [
'netsuite_id' => $data['internalid'],
'netsuite_tran_id' => $data['tranid'],
'recorded_at' => $data['datecreated'],
'netsuite_order_id' => $data['createdfrom'],
'netsuite_order_number' => $data['custbody_sales_order'],
'transaction_date' => $data['trandate'],
'status' => self::STATUS_MAP[$data['status']] ?? 'pending',
'customer' => $this->customerMapper->convertToMagentoFormat([$data['customer']])[0] ?? [],
'billaddress' => $data['billaddress'],
'billaddress1' => $data['billaddress1'],
'billaddress2' => $data['billaddress2'],
'billcity' => $data['billcity'],
'billcountry' => $data['billcountry'],
'billstate' => $data['billstate'],
'billzip' => $data['billzip'],
'billphone' => $data['billphone'],
'shipaddress' => $data['shipaddress'],
'shipaddress1' => $data['shipaddress1'],
'shipaddress2' => $data['shipaddress2'],
'shipcity' => $data['shipcity'],
'shipcountry' => $data['shipcountry'],
'shipstate' => $data['shipstate'],
'shipzip' => $data['shipzip'],
'shipphone' => $data['shipphone'],
'total' => $data['total'],
'taxtotal' => $data['taxtotal'],
'shippingcost' => $data['shippingcost'],
'discountamount' => $data['discountamount'],
'amount' => $data['amount'],
'location' => $data['location'],
'return_address' => $data['custbody_return_address_proxy'],
'notes' => $data['custbody_notes'],
'internal_notes' => $data['custbody_internal_notes'],
'items' => $this->convertItems($data),
];
}
return $magentoData;
}
/**
* Convert items from NetSuite format to Magento format.
*
* @param array $data
* @return array
*/
protected function convertItems($data)
{
$items = $data['items'];
$result = [];
foreach ($items as $item) {
$result[] = [
'sku' => $item['itemText'],
'netsuite_id' => $item['itemId'],
'name' => $item['itemText'],
'type' => $item['type'],
'description' => $item['description'],
'qty' => $item['quantity'],
'qty_billed' => $item['quantitybilled'],
'reason' => $item['reasonCode'],
'condition' => $item['conditions'],
'resolution' => $data['custbody_request_type'],
'rate' => $item['rate'],
'amount' => $item['amount'],
'discountamount' => $item['discountamount'],
];
}
return $result;
}
/**
* Convert data from Magento format to NetSuite format.
*
* @param string $rmaId
* @return array
*/
public function convertToNetSuiteFormat($rmaId)
{
$rma = $this->rmaRepository->get($rmaId);
$order = $this->rmaManagement->getOrder($rma);
$customer = $this->rmaManagement->getCustomer($rma);
$result = [
'orderId' => $order->getNetsuiteId(),
'createdAt' => $this->helper->convertLocalTimeToUtc($rma->getCreatedAt(), 'm/d/Y'),
'status' => $this->rmaManagement->getStatus($rma)->getCode(),
'customerId' => $customer->getNetsuiteId(),
'items' => $this->getRmaItems($rma),
];
return $result;
}
/**
* Get RMA items.
*
* @param RmaInterface $rma
* @return array
*/
protected function getRmaItems($rma)
{
$items = $this->itemListBuilder->getRmaItems($rma);
$result = [];
foreach ($items as $item) {
if (!$item->getQtyRequested()) {
continue;
}
$orderItem = $this->itemManagement->getOrderItem($item);
$product = $this->productRepository->getById($orderItem->getProductId());
$result[] = [
'id' => $product->getCustomAttribute('netsuite_id')?->getValue() ?? '',
'sku' => $product->getSku(),
'reason' => $this->itemManagement->getReasonName($item),
'condition' => $this->itemManagement->getConditionName($item),
'resolution' => $this->itemManagement->getResolutionName($item),
'rate' => $orderItem->getPrice(),
'qty' => $item->getQtyRequested(),
'amount' => $orderItem->getPrice() * $item->getQtyRequested(),
'tax_percent' => $orderItem->getTaxPercent(),
'tax_amount' => $orderItem->getTaxPercent() / 100 * $orderItem->getPrice() * $item->getQtyRequested(),
];
}
return $result;
}
}