ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Lts\Taxjar\Helper;
|
||||
|
||||
use JR\NetSuite\Lib\Restful\NetSuiteAPI;
|
||||
use JR\NetSuite\Logger\Logger as NetSuiteLogger;
|
||||
use Magento\Customer\Model\Data\Customer;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Quote\Api\Data\AddressInterface;
|
||||
|
||||
class ResaleCertificate extends AbstractHelper
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
protected \Magento\Checkout\Model\Session $checkoutSession,
|
||||
protected NetSuiteLogger $netSuiteLogger,
|
||||
protected NetSuiteAPI $netSuiteAPI
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function isResaleCertificate()
|
||||
{
|
||||
try {
|
||||
$quote = $this->checkoutSession->getQuote();
|
||||
$isResaleCertificate = $this->checkoutSession->getData('is_resale_certificate' . $quote->getId());
|
||||
$this->netSuiteLogger->critical(
|
||||
sprintf('resale certificate in checkout %d session: '.var_export($isResaleCertificate, true), $quote->getId())
|
||||
);
|
||||
if ($isResaleCertificate === null) {
|
||||
$isResaleCertificate = $this->getResaleCertificate($quote->getCustomer(), $quote->getShippingAddress());
|
||||
$this->netSuiteLogger->critical(
|
||||
'resale certificate from api: '.var_export($isResaleCertificate, true),
|
||||
);
|
||||
$this->checkoutSession->setData('is_resale_certificate' . $quote->getId(), $isResaleCertificate);
|
||||
}
|
||||
return $isResaleCertificate;
|
||||
} catch (\Exception $e) {
|
||||
$this->netSuiteLogger->critical(
|
||||
sprintf('get resale certificate exception: %s', $e->getMessage()),
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getResaleCertificate(Customer $dataCustomer, AddressInterface $address) {
|
||||
|
||||
$netsuiteId = $dataCustomer->getCustomAttribute('netsuite_id')?->getValue();
|
||||
// $this->netSuiteLogger->debug(
|
||||
// 'isResaleCertificate $netsuiteId: ',
|
||||
// [
|
||||
// '$netsuiteId' => $netsuiteId,
|
||||
// ]
|
||||
// );
|
||||
|
||||
$state = $address->getRegion();
|
||||
if (!$netsuiteId) {
|
||||
$this->netSuiteLogger->critical(
|
||||
sprintf('resale certificate request exception: customer %d netsuite_id not set', $dataCustomer->getId()),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$state) {
|
||||
$this->netSuiteLogger->critical(
|
||||
sprintf('resale certificate request exception: customer %d address %d state is not set ', $dataCustomer->getId(), $address->getId()),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
$query = sprintf(
|
||||
'custrecord_customer ANY_OF [%d] AND '.
|
||||
'custrecord_valid IS true AND custrecord_rcert_address_specific IS false AND custrecord_rc_state_full IS "%s"', $netsuiteId, $state
|
||||
);
|
||||
$service = 'record/v1/customrecord_resale_certificates';
|
||||
try {
|
||||
$response = $this->netSuiteAPI->get($service, ['q' => $query]);
|
||||
// $response = ['response'=>['count'=>1]];
|
||||
$this->netSuiteLogger->critical(
|
||||
'resale certificate request exception: ',
|
||||
[
|
||||
'$response' => $response,
|
||||
'$netsuiteId' => $netsuiteId,
|
||||
'$state' => $state,
|
||||
]
|
||||
);
|
||||
return isset($response['response']['count']) && $response['response']['count'] != 0;
|
||||
} catch (\Exception $e) {
|
||||
$this->netSuiteLogger->error(
|
||||
'resale certificate request exception: '.$e->getMessage());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user