-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
026f096
commit 4afd733
Showing
6 changed files
with
239 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
2.4/app/code/Webmaniabr/Nfe/Controller/Adminhtml/Order/Issuenfe.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
namespace Webmaniabr\Nfe\Controller\Adminhtml\Order; | ||
|
||
use Webmaniabr\Nfe\Helper\NfeData; | ||
use Magento\Framework\App\CsrfAwareActionInterface; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\App\Request\InvalidRequestException; | ||
|
||
class Issuenfe extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface | ||
{ | ||
protected $_pageFactory; | ||
protected $request; | ||
protected $nfeData; | ||
|
||
public function __construct( | ||
\Magento\Framework\App\Action\Context $context, | ||
NfeData $nfeData | ||
){ | ||
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | ||
$this->nfeData = $nfeData; | ||
parent::__construct($context); | ||
} | ||
|
||
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException | ||
{ | ||
return null; | ||
} | ||
|
||
public function validateForCsrf(RequestInterface $request): ?bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function execute() { | ||
// Get order id | ||
$order_id = $this->getRequest()->getParam('order_id'); | ||
if (!$order_id) return; | ||
|
||
// Get the status order by id | ||
$result = $this->nfeData->get_status_order_by_id($order_id); | ||
|
||
// Obtain if are allowed to request duplicate NF-e | ||
$nfe_duplicada = $this->nfeData->get_nfe_duplicada(); | ||
|
||
// Create response | ||
$resultRedirect = $this->resultRedirectFactory->create(); | ||
$resultRedirect->setPath('sales/order/view', ['order_id' => $order_id]); | ||
|
||
// Check if the actual order already has a NF-e with Status "Emitida" | ||
// and the option nfe_duplicada is allowed | ||
if( ($result == "Emitida" || $result == "aprovado") && !$nfe_duplicada ) { | ||
$this->messageManager->addNoticeMessage("Já existe uma nota fiscal emitida para o pedido #{$order_id}."); | ||
return $resultRedirect; | ||
} | ||
|
||
// Prepare the data for API | ||
$data = $this->nfeData->get_the_order_data_by_id($order_id); | ||
|
||
// Call function to connect to API | ||
$response = $this->nfeData->emitir_nfe($order_id, $data); | ||
|
||
// If there's an error | ||
if (is_array($response)) { | ||
if (isset($response["return"])) { | ||
$this->messageManager->addErrorMessage($response['message']); | ||
return $resultRedirect; | ||
} | ||
} | ||
|
||
// Store the response of the WebmaniaBR REST API | ||
$uuid = $response->uuid; | ||
$chave_acesso = $response->chave; | ||
$n_recibo = ( isset($response->n_recibo) ? $response->n_recibo : "" ); | ||
$n_nfe = $response->nfe; | ||
$n_serie = $response->serie; | ||
$url_xml = $response->xml; | ||
$url_danfe = $response->danfe; | ||
$status = $response->status; | ||
$date = date('Y-m-d H:i:s'); | ||
$username = "Emitir NF-e - Order View"; | ||
$this->nfeData->register_comment( $order_id, $username, $url_danfe ); | ||
$this->nfeData->add_status_nfe( $order_id, $uuid, $chave_acesso, $n_recibo, $n_nfe, $n_serie, $url_xml, $url_danfe, $status, $date ); | ||
|
||
// Add success message | ||
$this->messageManager->addSuccessMessage("Nota Fiscal do pedido #{$order_id} gerada com sucesso."); | ||
return $resultRedirect; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
2.4/app/code/Webmaniabr/Nfe/Setup/Patch/Data/CustomerIe.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Webmaniabr\Nfe\Setup\Patch\Data; | ||
|
||
use Exception; | ||
use Psr\Log\LoggerInterface; | ||
use Magento\Customer\Api\CustomerMetadataInterface; | ||
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResource; | ||
use Magento\Customer\Setup\CustomerSetup; | ||
use Magento\Customer\Setup\CustomerSetupFactory; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\Patch\DataPatchInterface; | ||
|
||
/** | ||
* Creates a customer attribute for managing a customer's external system ID | ||
*/ | ||
class CustomerIe implements DataPatchInterface | ||
{ | ||
/** | ||
* @var ModuleDataSetupInterface | ||
*/ | ||
private $moduleDataSetup; | ||
|
||
/** | ||
* @var CustomerSetup | ||
*/ | ||
private $customerSetup; | ||
|
||
/** | ||
* @var AttributeResource | ||
*/ | ||
private $attributeResource; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param ModuleDataSetupInterface $moduleDataSetup | ||
* @param CustomerSetupFactory $customerSetupFactory | ||
* @param AttributeResource $attributeResource | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
ModuleDataSetupInterface $moduleDataSetup, | ||
CustomerSetupFactory $customerSetupFactory, | ||
AttributeResource $attributeResource, | ||
LoggerInterface $logger | ||
) { | ||
$this->moduleDataSetup = $moduleDataSetup; | ||
$this->customerSetup = $customerSetupFactory->create(['setup' => $moduleDataSetup]); | ||
$this->attributeResource = $attributeResource; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Get array of patches that have to be executed prior to this. | ||
* | ||
* Example of implementation: | ||
* | ||
* [ | ||
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class, | ||
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class | ||
* ] | ||
* | ||
* @return string[] | ||
*/ | ||
public static function getDependencies(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Get aliases (previous names) for the patch. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getAliases(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Run code inside patch | ||
*/ | ||
public function apply() | ||
{ | ||
// Start setup | ||
$this->moduleDataSetup->getConnection()->startSetup(); | ||
|
||
try { | ||
// Add customer attribute with settings | ||
$this->customerSetup->addAttribute( | ||
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, | ||
'customer_ie', | ||
[ | ||
'label' => 'Inscrição Estadual (I.E.)', | ||
'required' => 0, | ||
'position' => 100, | ||
'system' => 0, | ||
'user_defined' => 1, | ||
'is_used_in_grid' => 0, | ||
'is_visible_in_grid' => 0, | ||
'is_filterable_in_grid' => 0, | ||
'is_searchable_in_grid' => 0, | ||
] | ||
); | ||
|
||
// Add attribute to default attribute set and group | ||
$this->customerSetup->addAttributeToSet( | ||
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, | ||
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, | ||
null, | ||
'customer_ie' | ||
); | ||
|
||
// Get the newly created attribute's model | ||
$attribute = $this->customerSetup->getEavConfig() | ||
->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer_ie'); | ||
|
||
// Make attribute visible in customer forms | ||
$attribute->setData('used_in_forms', [ | ||
'adminhtml_customer' | ||
]); | ||
|
||
// Save attribute using its resource model | ||
$this->attributeResource->save($attribute); | ||
} catch (Exception $e) { | ||
$this->logger->err($e->getMessage()); | ||
} | ||
|
||
// End setup | ||
$this->moduleDataSetup->getConnection()->endSetup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters