Skip to content

Commit

Permalink
v3.1.7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaniabr committed Sep 18, 2023
1 parent 026f096 commit 4afd733
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public function beforeSetLayout(OrderView $subject)
}

$secret_key = $this->nfeData->manage_secret_key($order_id, "nfe_btn_emitir");
$url = $subject->getUrl('webmaniabrnfe/order/issuenfe');

$subject->addButton(
'order_custom_button',
[
'label' => __('Emitir NF-e'),
'class' => __('webmaniabr-emitir-nfe'),
'id' => 'order-view-webmaniabr-nfe',
'onclick' => 'window.open("' . $storeManager->getStore()->getBaseUrl() . 'webmaniabrnfe/index/nfeactions/?nfe_btn_emitir=' . $secret_key . '&order_id=' . $order_id . '")'
'onclick' => "setLocation('{$url}')"
]
);
}
Expand Down
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;
}
}
9 changes: 9 additions & 0 deletions 2.4/app/code/Webmaniabr/Nfe/Helper/NfeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,15 @@ public function get_address($cliente_details, $order) {
$customer['nome_completo'] = $cliente_details->getFirstname().' '.$cliente_details->getLastname();
}

// I.E.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerModel = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')->getById($order->getCustomerId());
$ieAttribute = $customerModel->getCustomAttribute('customer_ie');
if ($ieAttribute) {
$ieValue = $ieAttribute->getValue();
if (!empty($ieValue)) $customer['ie'] = $ieValue;
}

$cliente_details_address = $cliente_details->getStreet();

if (count($cliente_details_address) < 3 || is_null($cliente_details->getRegion())) return [];
Expand Down
138 changes: 138 additions & 0 deletions 2.4/app/code/Webmaniabr/Nfe/Setup/Patch/Data/CustomerIe.php
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();
}
}
2 changes: 1 addition & 1 deletion 2.4/app/code/Webmaniabr/Nfe/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"magento/framework": "100.0.*"
},
"type": "magento2-module",
"version": "3.1.7.3",
"version": "3.1.7.4",
"license": [
"GPLv2"
],
Expand Down
2 changes: 1 addition & 1 deletion 2.4/app/code/Webmaniabr/Nfe/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="webmaniabr" translate="label" sortOrder="10">
<label>WebmaniaBR NF-e - v3.1.7.3</label>
<label>WebmaniaBR NF-e - v3.1.7.4</label>
</tab>

<section id="webmaniabr_nfe_configs" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down

0 comments on commit 4afd733

Please sign in to comment.