From f8df2e94c036d16221438422c570ad3ff3910e51 Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Wed, 21 Nov 2018 11:18:47 -0300 Subject: [PATCH] Merged recent changes, 1.2.8, into master --- README.md | 12 +- README.txt | 14 +- composer.json | 3 +- includes/admin/class-admin.php | 46 ++-- includes/admin/class-api.php | 252 +++++++++++------- includes/admin/class-settings.php | 18 +- includes/admin/class-webhook.php | 25 +- .../emails/class-nfe-email-receipt-issued.php | 14 +- includes/frontend/class-frontend.php | 17 +- includes/nfe-functions.php | 40 ++- package.json | 8 +- woo-nfe.php | 35 +-- 12 files changed, 289 insertions(+), 195 deletions(-) diff --git a/README.md b/README.md index 1d5b719..7919147 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Ou você pode colocar este plugin no diretório wp-content/plugins e ativá-lo. ## Changelog ## -### 1.0 +### 1.0.0 * Initial release ### 1.0.1 @@ -50,3 +50,13 @@ Ou você pode colocar este plugin no diretório wp-content/plugins e ativá-lo. ### 1.2.7 * Fixing how we verify the type of customer to output its information on the NFe receipt. + +### 1.2.8 +* Improved code documentation, PHPDoc. +* Started to use `[]` instead of `array()`. +* Started to use the new logger implementation, `wc_get_logger()`. +* Updated WordPress tested header to 3.5.1. +* Removed Extra Checkout plugin dependency. +* Removed Composer support for the client-php. +* Removed checks when on automatic issuing, as it was avoiding important log information to be saved. +* Added better labeling for the NFe.io `flowStatus`. diff --git a/README.txt b/README.txt index 35f786f..9e3d862 100644 --- a/README.txt +++ b/README.txt @@ -3,9 +3,9 @@ Contributors: nfe, espellcaste Tags: woocommerce, shop, receipt, nfe, nota fiscal, nota, receita, sefaz, nfse, emitir nfse, emitir nfe Requires at least: 4.7 Tested up to: 4.9.4 -Stable tag: 1.2.7 +Stable tag: 1.2.8 Requires PHP: 5.5 -WC tested up to: 3.4.3 +WC tested up to: 3.5.1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -66,3 +66,13 @@ or = 1.2.7 = * Fixing how we verify the type of customer to output its information on the NFe receipt. + += 1.2.8 = +* Improved code documentation, PHPDoc. +* Started to use `[]` instead of `array()`. +* Started to use the new logger implementation, `wc_get_logger()`. +* Updated WordPress tested header to 3.5.1. +* Removed Extra Checkout plugin dependency. +* Removed Composer support for the client-php. +* Removed checks when on automatic issuing, as it was avoiding important log information to be saved. +* Added better labeling for the NFe.io `flowStatus`. diff --git a/composer.json b/composer.json index 3d3de7c..2f439cb 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,6 @@ "composer/installers": "~1.0" }, "require-dev": { - "phpunit/phpunit": ">=3.7@stable", - "nfe/nfe": ">=2.0" + "phpunit/phpunit": ">=3.7@stable" } } diff --git a/includes/admin/class-admin.php b/includes/admin/class-admin.php index 542ac13..1f9a452 100755 --- a/includes/admin/class-admin.php +++ b/includes/admin/class-admin.php @@ -61,15 +61,18 @@ public function __construct() { * Issue a NFe receipt when WooCommerce does its thing. * * @param int $order_id Order ID. + * * @return void */ public function issue_trigger( $order_id ) { + + // Bail early. if ( nfe_get_field( 'issue_when' ) === 'manual' ) { return; } // Check if order exists first. - $order = nfe_wc_get_order( $order_id ); + $order = nfe_wc_get_order( $order_id ); $order_id = $order->get_id(); // Bail for no order. @@ -77,19 +80,9 @@ public function issue_trigger( $order_id ) { return; } - // Bail for zeroed order. - if ( '0.00' === $order->get_total() ) { - return; - } - - // Checking if the address is required and if the order address was filled. - if ( ! nfe_order_address_filled( $order_id ) ) { - return; - } - - // We just can issue the invoice if the status is equal to the configured one. + // We just can issue the invoice automatically if the status is equal to the configured one. if ( $order->has_status( nfe_get_field( 'issue_when_status' ) ) ) { - NFe_Woo()->issue_invoice( array( $order_id ) ); + NFe_Woo()->issue_invoice( [ $order_id ] ); } } @@ -179,9 +172,9 @@ public function nfe_status_widget_order_rows() { */ public function product_data_tab( $product_data_tabs ) { $product_data_tabs['nfe-product-info-tab'] = array( - 'label' => esc_html__( 'WooCommerce NFe', 'woo-nfe' ), - 'target' => 'nfe_product_info_data', - 'class' => array( 'hide_if_variable' ), + 'label' => esc_html__( 'WooCommerce NFe', 'woo-nfe' ), + 'target' => 'nfe_product_info_data', + 'class' => array( 'hide_if_variable' ), ); return $product_data_tabs; } @@ -307,7 +300,7 @@ public function save_variations_fields( $post_id ) { * Adds the Download and Issue actions to the actions list in the order edit page. * * @param array $actions Order actions array to display. - * @return array List of actions. + * @return array|void List of actions. */ public function download_and_issue_actions( $actions ) { global $theorder; @@ -390,9 +383,9 @@ public function order_status_column_header( $columns ) { public function order_status_column_content( $column ) { // Get information. - $order = nfe_wc_get_order( get_the_ID() ); - $order_id = $order->get_id(); - $nfe = get_post_meta( $order_id, 'nfe_issued', true ); + $order = nfe_wc_get_order( get_the_ID() ); + $order_id = $order->get_id(); + $nfe = get_post_meta( $order_id, 'nfe_issued', true ); // Bail early. if ( 'nfe_receipts' !== $column ) { @@ -490,7 +483,7 @@ public function display_order_data_preview_in_admin( $order ) {

- +
@@ -542,7 +535,7 @@ public function nfe_admin_order_preview_details( $fields, $order ) { } $fields['nfe'] = [ - 'status' => ! empty( $nfe['status'] ) ? $nfe['status'] : '', + 'status' => ! empty( $nfe['status'] ) ? nfe_status_label( $nfe['status'] ) : '', 'number' => ! empty( $nfe['number'] ) ? $nfe['number'] : '', 'check_code' => ! empty( $nfe['checkCode'] ) ? $nfe['checkCode'] : '', 'issued' => ! empty( $nfe['issuedOn'] ) ? date_i18n( get_option( 'date_format' ), strtotime( $nfe['issuedOn'] ) ) : '', @@ -597,8 +590,7 @@ public function nfe_admin_order_preview() { * @return void */ public function register_enqueue_css() { - wp_register_style( 'nfe-woo-admin-css', plugins_url( 'woo-nfe/assets/css/nfe' ) . '.css' ); - wp_enqueue_style( 'nfe-woo-admin-css' ); + wp_enqueue_style( 'nfe-woo-admin-css', plugins_url( 'woo-nfe/assets/css/nfe' ) . '.css', [], '1.2.8', false ); } /** @@ -636,7 +628,7 @@ protected function array_insert_after( $needle, $haystack, $new_key, $new_value * Issue Helper Method. * * @param array $download NFe info. - * @param WC_Order $order Order. + * @param WC_Order $order Order object. * * @return bool */ @@ -652,8 +644,8 @@ protected function should_we_issue( $download, $order ) { return false; } - // Bail if there is no address. - if ( ! nfe_order_address_filled( $order->get_id() ) ) { + // Bail if there is no address and it is required. + if ( nfe_require_address() && ! nfe_order_address_filled( $order->get_id() ) ) { return false; } diff --git a/includes/admin/class-api.php b/includes/admin/class-api.php index 0f3db56..bf21385 100755 --- a/includes/admin/class-api.php +++ b/includes/admin/class-api.php @@ -15,7 +15,7 @@ class NFe_Woo { /** - * WC_Logger Instance + * WC_Logger Logger instance. * * @var boolean */ @@ -23,6 +23,8 @@ class NFe_Woo { /** * NFe_Woo Instance. + * + * @return NFe_Woo */ public static function instance() { // Store the instance locally to avoid private static replication. @@ -48,12 +50,11 @@ private function __construct() {} * * @param array $order_ids Orders to issue the NFe. * - * @return bool|string + * @return bool|NFe_ServiceInvoice */ - public function issue_invoice( $order_ids = array() ) { - $key = $this->get_key(); - $company_id = $this->get_company(); - $issue_when_status = nfe_get_field( 'issue_when_status' ); + public function issue_invoice( $order_ids = [] ) { + $key = $this->get_key(); + $company_id = $this->get_company(); NFe_io::setApiKey( $key ); @@ -61,12 +62,14 @@ public function issue_invoice( $order_ids = array() ) { $order = nfe_wc_get_order( $order_id ); + // translators: Log message. $log = sprintf( __( 'NFe issuing process started! Order: #%d', 'woo-nfe' ), $order_id ); $this->logger( $log ); $order->add_order_note( $log ); // If value is 0.00, don't issue it. if ( '0.00' === $order->get_total() ) { + // translators: Log message. $log = sprintf( __( 'Not possible to issue NFe without an order value! Order: #%d', 'woo-nfe' ), $order_id ); $this->logger( $log ); $order->add_order_note( $log ); @@ -76,16 +79,20 @@ public function issue_invoice( $order_ids = array() ) { $datainvoice = $this->order_info( $order_id ); - // Check if there was a problem while fetching the city code from IBGE. - if ( empty( $datainvoice['borrower']['address']['city']['code'] ) ) { + // Check if there was a problem while fetching the city code from IBGE. And if the adderss is required. + if ( nfe_require_address() && empty( $datainvoice['borrower']['address']['city']['code'] ) ) { $log = __( 'There was a problem fetching IBGE code! Check your CEP information.', 'woo-nfe' ); $this->logger( $log ); $order->add_order_note( $log ); + + // Bail early so that it doesn't create an invoice without address. + return false; } $invoice = NFe_ServiceInvoice::create( $company_id, $datainvoice ); if ( isset( $invoice->message ) ) { + // translators: Log message. $log = sprintf( __( 'An error occurred while issuing a NFe: %s', 'woo-nfe' ), print_r( $invoice->message, true ) ); $this->logger( $log ); $order->add_order_note( $log ); @@ -93,19 +100,25 @@ public function issue_invoice( $order_ids = array() ) { return false; } + // translators: Log message. $log = sprintf( __( 'NFe sent sucessfully to issue! Order: #%d', 'woo-nfe' ), $order_id ); $this->logger( $log ); $order->add_order_note( $log ); - $nfe = array( + // Update invoice information. + $meta = update_post_meta( $order_id, 'nfe_issued', [ 'id' => $invoice->id, 'status' => $invoice->flowStatus, 'issuedOn' => $invoice->issuedOn, 'amountNet' => $invoice->amountNet, 'checkCode' => $invoice->checkCode, 'number' => $invoice->number, - ); - update_post_meta( $order_id, 'nfe_issued', $nfe ); + ] ); + + if ( ! $meta ) { + // translators: Log message. + $this->logger( sprintf( __( 'There was a problem while updating the Order #%d with the NFe information.', 'woo-nfe' ), $order_id ) ); + } } return $invoice; @@ -117,9 +130,10 @@ public function issue_invoice( $order_ids = array() ) { * @throws Exception Exception. * * @param array $order_ids Array of order ids. - * @return string Pdf url from NFe.io + * + * @return Exception|NFe_ServiceInvoice */ - public function download_pdf_invoice( $order_ids = array() ) { + public function download_pdf_invoice( $order_ids = [] ) { $key = $this->get_key(); $company_id = $this->get_company(); @@ -132,10 +146,12 @@ public function download_pdf_invoice( $order_ids = array() ) { try { $pdf = NFe_ServiceInvoice::pdf( $company_id, $nfe['id'] ); + // translators: Log message. $log = sprintf( __( 'NFe PDF Donwload successfully. Order: #%d', 'woo-nfe' ), $order_id ); $this->logger( $log ); $order->add_order_note( $log ); } catch ( Exception $e ) { + // translators: Log message. $log = sprintf( __( 'There was a problem when trying to download NFe PDF! Error: %s', 'woo-nfe' ), print_r( $e->getMessage(), true ) ); $this->logger( $log ); $order->add_order_note( $log ); @@ -168,24 +184,24 @@ public function order_info( $order_id ) { 'country' => $this->remover_caracter( $this->billing_country( $order_id ) ), 'state' => $this->remover_caracter( $this->check_customer_info( 'state', $order_id ) ), 'city' => [ - 'code' => $this->ibge_code( $order_id ), - 'name' => $this->remover_caracter( $this->check_customer_info( 'city', $order_id ) ), + 'code' => $this->ibge_code( $order_id ), + 'name' => $this->remover_caracter( $this->check_customer_info( 'city', $order_id ) ), ], ]; $borrower = [ - 'name' => $this->check_customer_info( 'name', $order_id ), - 'email' => get_post_meta( $order_id, '_billing_email', true ), - 'federalTaxNumber' => $this->removepontotraco( $this->check_customer_info( 'number', $order_id ) ), - 'address' => $address, + 'name' => $this->check_customer_info( 'name', $order_id ), + 'email' => get_post_meta( $order_id, '_billing_email', true ), + 'federalTaxNumber' => $this->removepontotraco( $this->check_customer_info( 'number', $order_id ) ), + 'address' => $address, ]; $data = [ - 'cityServiceCode' => $this->city_service_info( 'code', $order_id ), - 'federalServiceCode' => $this->city_service_info( 'fed_code', $order_id ), - 'description' => $this->remover_caracter( $this->city_service_info( 'desc', $order_id ) ), - 'servicesAmount' => $order->get_total(), - 'borrower' => $borrower, + 'cityServiceCode' => $this->city_service_info( 'code', $order_id ), + 'federalServiceCode' => $this->city_service_info( 'fed_code', $order_id ), + 'description' => $this->remover_caracter( $this->city_service_info( 'desc', $order_id ) ), + 'servicesAmount' => $order->get_total(), + 'borrower' => $borrower, ]; // Removes empty, false and null fields from the array. @@ -196,6 +212,7 @@ public function order_info( $order_id ) { * Hack to bring support to Brazilian ISO code (Ex.: BRA instead of BR) * * @param int $order_id Order ID. + * * @return string|null */ protected function billing_country( $order_id ) { @@ -221,32 +238,84 @@ protected function billing_country( $order_id ) { /** * Fetche the IBGE Code. * - * @param int $order_id Order ID. + * @param int $order_id Order ID. + * * @return string|null */ protected function ibge_code( $order_id ) { - $key = $this->get_key(); $post_code = get_post_meta( $order_id, '_billing_postcode', true ); if ( empty( $post_code ) ) { - - if ( false === nfe_require_address() ) { + if ( ! nfe_require_address() ) { return $this->get_company_info( 'code' ); } return null; } - $url = 'https://open.nfe.io/v1/addresses/' . $post_code . '?api_key=' . $key; + $url = 'https://open.nfe.io/v1/addresses/' . $post_code . '?api_key=' . $this->get_key(); $response = wp_remote_get( esc_url_raw( $url ) ); if ( is_wp_error( $response ) ) { - return; + return null; } $address = json_decode( wp_remote_retrieve_body( $response ), true ); + $code = $address['city']['code']; - return $address['city']['code']; + if ( empty( $code ) ) { + return null; + } + + return $code; + } + + /** + * Get current company info. + * + * @param string $field Field. + * + * @return string|null + */ + protected function get_company_info( $field ) { + + // Get companies. + $url = 'https://api.nfe.io/v1/companies/' . $this->get_company() . '?api_key=' . $this->get_key(); + $response = wp_remote_get( esc_url_raw( $url ) ); + + if ( is_wp_error( $response ) ) { + return null; + } + + $company = json_decode( wp_remote_retrieve_body( $response ), true ); + + if ( 'city' === $field ) { + $name = $company['companies']['address']['city']['name']; + + if ( empty( $name ) ) { + return null; + } + + return $name; + } + + if ( 'code' === $field ) { + $code = $company['companies']['address']['city']['code']; + + if ( empty( $code ) ) { + return null; + } + + return $code; + } + + $field_value = $company['companies']['address'][ $field ]; + + if ( empty( $field_value ) ) { + return null; + } + + return $field_value; } /** @@ -255,9 +324,11 @@ protected function ibge_code( $order_id ) { * @param string $field The field info being fetched. * @param int $order_id Order ID. * - * @return string + * @return string|null */ protected function city_service_info( $field = '', $order_id ) { + + // Bail early. if ( empty( $field ) ) { return; } @@ -309,7 +380,7 @@ protected function city_service_info( $field = '', $order_id ) { * @param string $field Field to fetch info from. * @param int $order The order ID. * - * @return string|empty Returns the customer info specific to the person type being fetched. + * @return string|null Returns the customer info specific to the person type being fetched. */ protected function check_customer_info( $field = '', $order ) { @@ -317,38 +388,60 @@ protected function check_customer_info( $field = '', $order ) { return; } - // Person Type. - $type = get_post_meta( $order, '_billing_persontype', true ); + // Only check those fields. + if ( in_array( $field, [ 'number', 'name', 'type' ], true ) ) { + + // Person Type. + $type = get_post_meta( $order, '_billing_persontype', true ); + + // Customer info. + $cpf = get_post_meta( $order, '_billing_cpf', true ); + $customer = get_post_meta( $order, '_billing_first_name', true ) . ' ' . get_post_meta( $order, '_billing_last_name', true ); + + // Company info. + $cnpj = get_post_meta( $order, '_billing_cnpj', true ); + $company = get_post_meta( $order, '_billing_company', true ); + + if ( ! empty( $type ) ) { + if ( '1' === $type ) { + $id = $this->cpf( $cpf ); + $name = $customer; + $type = __( 'Customers', 'woo-nfe' ); + } else { + $id = $this->cnpj( $cnpj ); + $name = $company; + $type = __( 'Company', 'woo-nfe' ); + } + } + } switch ( $field ) { case 'number': - if ( ! empty( $type ) ) { - if ( '1' === $type ) { - $output = $this->cpf( get_post_meta( $order, '_billing_cpf', true ) ); + if ( empty( $type ) ) { + if ( ! empty( $cpf ) ) { + $output = $this->cpf( $cpf ); } else { - $output = $this->cnpj( get_post_meta( $order, '_billing_cnpj', true ) ); + $output = $this->cnpj( $cnpj ); } + } else { + $output = $id; } break; case 'name': - if ( ! empty( $type ) ) { - if ( '1' === $type ) { - $output = get_post_meta( $order, '_billing_first_name', true ) . ' ' . get_post_meta( $order, '_billing_last_name', true ); + if ( empty( $type ) ) { + if ( ! empty( $customer ) ) { + $output = $customer; } else { - $output = get_post_meta( $order, '_billing_company', true ); + $output = $company; } + } else { + $output = $name; } break; case 'type': - if ( ! empty( $type ) ) { - if ( '1' === $type ) { - $output = __( 'Customers', 'woo-nfe' ); - } else { - $output = __( 'Company', 'woo-nfe' ); - } - } + $output = $type; break; case 'city': @@ -413,39 +506,6 @@ protected function check_customer_info( $field = '', $order ) { return $output; } - /** - * Get current company info. - * - * @param string $field Field. - * - * @return string - */ - protected function get_company_info( $field ) { - - // Get info. - $key = $this->get_key(); - $company_id = $this->get_company(); - - $url = 'https://api.nfe.io/v1/companies/' . $company_id . '?api_key=' . $key; - $response = wp_remote_get( esc_url_raw( $url ) ); - - if ( is_wp_error( $response ) ) { - return; - } - - $company = json_decode( wp_remote_retrieve_body( $response ), true ); - - if ( 'city' === $field ) { - return $company['companies']['address']['city']['name']; - } - - if ( 'code' === $field ) { - return $company['companies']['address']['city']['code']; - } - - return $company['companies']['address'][ $field ]; - } - /** * Remove Ponto Traco. * @@ -473,7 +533,8 @@ protected function remover_caracter( $string ) { * CPF Converter * * @param string $cpf CPF. - * @return void + * + * @return void|string */ public function cpf( $cpf ) { if ( ! $cpf ) { @@ -490,7 +551,8 @@ public function cpf( $cpf ) { * CNPJ Converter * * @param string $cnpj CNPJ. - * @return string + * + * @return void|string */ public function cnpj( $cnpj ) { if ( ! $cnpj ) { @@ -507,7 +569,8 @@ public function cnpj( $cnpj ) { * CEP Converter. * * @param string $cep Content. - * @return string + * + * @return void|string */ public function cep( $cep ) { if ( ! $cep ) { @@ -524,6 +587,7 @@ public function cep( $cep ) { * Clears * * @param string $string Content. + * * @return string */ public function clear( $string ) { @@ -537,6 +601,7 @@ public function clear( $string ) { * * @param string $val Value that's gonna be masked. * @param string $mask Mask pattern. + * * @return string */ public function mask( $val, $mask ) { @@ -715,13 +780,22 @@ protected function country_iso_codes() { * Logging method. * * @param string $message Message. + * + * @return void */ public static function logger( $message ) { - if ( nfe_get_field( 'debug' ) === 'yes' ) { + $debug = nfe_get_field( 'debug' ); + + if ( empty( $debug ) ) { + return; + } + + if ( 'yes' === $debug ) { if ( empty( self::$logger ) ) { - self::$logger = new WC_Logger(); + self::$logger = wc_get_logger(); } - self::$logger->add( 'nfe_api', $message ); + + self::$logger->info( $message, [ 'source' => 'nfe_api' ] ); } } } diff --git a/includes/admin/class-settings.php b/includes/admin/class-settings.php index 215b64c..d97db0a 100755 --- a/includes/admin/class-settings.php +++ b/includes/admin/class-settings.php @@ -44,18 +44,14 @@ public function init_form_fields() { $lists = $this->get_companies(); if ( empty( $lists ) ) { - $company_list = array_merge(array( - '' => __( 'No company found', 'woo-nfe' ), - ), $lists ); + $company_list = array_merge( [ '' => __( 'No company found', 'woo-nfe' ), ], $lists ); } else { - $company_list = array_merge(array( - '' => __( 'Select a company...', 'woo-nfe' ), - ), $lists ); + $company_list = array_merge( [ '' => __( 'Select a company...', 'woo-nfe' ), ], $lists ); } } else { - $company_list = array( + $company_list = [ 'no-company' => __( 'Enter your API key to see your company(ies).', 'woo-nfe' ), - ); + ]; } $this->form_fields = array( @@ -218,13 +214,13 @@ protected function get_companies() { // Bail early with error message. if ( ! empty( $companies->message ) || empty( $companies ) || empty( $companies['companies'] ) ) { - add_action( 'admin_notices', array( $this, 'nfe_api_error_msg' ) ); - add_action( 'network_admin_notices', array( $this, 'nfe_api_error_msg' ) ); + add_action( 'admin_notices', [ $this, 'nfe_api_error_msg' ] ); + add_action( 'network_admin_notices', [ $this, 'nfe_api_error_msg' ] ); return false; } - $company_list = array(); + $company_list = []; foreach ( $companies['companies'] as $company ) { $company_list[ $company->id ] = ucwords( strtolower( $company->name ) ); } diff --git a/includes/admin/class-webhook.php b/includes/admin/class-webhook.php index 125e0c6..a6911c9 100755 --- a/includes/admin/class-webhook.php +++ b/includes/admin/class-webhook.php @@ -23,15 +23,19 @@ class WC_NFe_Webhook_Handler { * Base Construct. */ public function __construct() { - add_action( 'woocommerce_api_' . WC_API_CALLBACK, array( $this, 'handle' ) ); + add_action( 'woocommerce_api_' . WC_API_CALLBACK, [ $this, 'handle' ] ); } /** * Handling incoming webhooks. + * + * @throws Exception Exception. + * + * @return void */ public function handle() { $raw_body = file_get_contents( 'php://input' ); - $body = json_decode( $raw_body ); + $body = json_decode( $raw_body ); // translators: Fired when a new webhook is called. $this->logger( sprintf( __( 'New webhook called. %s', 'woo-nfe' ), $raw_body ) ); @@ -71,25 +75,24 @@ protected function process_event( $body ) { $this->logger( __( 'Getting Order ID of the webhook.', 'woo-nfe' ) ); $order = $this->get_order_by_nota_id( $body->id ); - $nfe = array( + $this->logger( __( 'Updating Order with NFe info.', 'woo-nfe' ) ); + + $meta = update_post_meta( $order->get_id(), 'nfe_issued', [ 'id' => $body->id, 'status' => $body->flowStatus, 'issuedOn' => $body->issuedOn, 'amountNet' => $body->amountNet, 'checkCode' => $body->checkCode, 'number' => $body->number, - ); - - $this->logger( __( 'Updating Order with NFe info.', 'woo-nfe' ) ); - - $meta = update_post_meta( $order->get_id(), 'nfe_issued', $nfe ); + ] ); if ( ! $meta ) { $this->logger( sprintf( __( 'There was a problem while updating the Order #%d with the NFe information.', 'woo-nfe' ), $order->get_id() ) ); + return; } // translators: Order updated with its status. - $msg = sprintf( __( 'Order updated. Order: #%d NFe status: %s .', 'woo-nfe' ), $order->get_id(), $body->flowStatus ); + $msg = sprintf( __( 'Order updated. Order: #%d NFe status: %s.', 'woo-nfe' ), $order->get_id(), nfe_status_label( $body->flowStatus ) ); $this->logger( $msg ); $order->add_order_note( $msg ); } @@ -132,10 +135,10 @@ public static function logger( $message ) { if ( 'yes' === $debug ) { if ( empty( self::$logger ) ) { - self::$logger = new WC_Logger(); + self::$logger = wc_get_logger(); } - self::$logger->add( 'nfe_webhook', $message ); + self::$logger->debug( $message, [ 'source' => 'nfe_webhook' ] ); } } } diff --git a/includes/admin/emails/class-nfe-email-receipt-issued.php b/includes/admin/emails/class-nfe-email-receipt-issued.php index 2bec7b8..fc73d9c 100644 --- a/includes/admin/emails/class-nfe-email-receipt-issued.php +++ b/includes/admin/emails/class-nfe-email-receipt-issued.php @@ -1,6 +1,6 @@ title = __( 'NFe Receipt Issued', 'woo-nfe' ); $this->description = __( 'Safe copy emails are sent when a customer issues an receipt. The e-mail is sent to the admin as a saving measure.', 'woo-nfe' ); - $this->heading = __( 'NFe Receipt Issued', 'woo-nfe' ); + $this->heading = __( 'NFe Receipt Issued', 'woo-nfe' ); // translators: placeholder is {blogname}, a variable that will be substituted when email is sent out. - $this->subject = sprintf( _x( '[%s] NFe Receipt Issued', 'default email subject for safe copy emails sent to the admin or a custom email chosen in the NFe settings page', 'woo-nfe' ), '{blogname}' ); + $this->subject = sprintf( _x( '[%s] NFe Receipt Issued', 'default email subject for safe copy emails sent to the admin or a custom email chosen in the NFe settings page', 'woo-nfe' ), '{blogname}' ); $this->template_base = WOOCOMMERCE_NFE_PATH . 'templates/'; $this->template_html = 'emails/nfe-receipt-issued.php'; @@ -38,7 +38,7 @@ public function __construct() { add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) ); - add_action( 'woocommerce_renewal_order_payment_complete', array( $this, 'trigger') ); + add_action( 'woocommerce_renewal_order_payment_complete', array( $this, 'trigger' ) ); parent::__construct(); } @@ -52,7 +52,7 @@ public function __construct() { */ public function trigger( $order_id ) { // Check if order exists first. - $order = nfe_wc_get_order( $order_id ); + $order = nfe_wc_get_order( $order_id ); $order_id = $order->get_id(); if ( ! $order_id ) { @@ -65,7 +65,7 @@ public function trigger( $order_id ) { } $this->object = $order; - $this->recipient = $this->object->billing_email; + $this->recipient = $this->object->get_billing_email(); if ( ! $this->is_enabled() || ! $this->get_recipient() ) { return; @@ -75,7 +75,7 @@ public function trigger( $order_id ) { } /** - * get_content_html + * get_content_html. * * @return string */ diff --git a/includes/frontend/class-frontend.php b/includes/frontend/class-frontend.php index ca9e86c..3d4ce49 100644 --- a/includes/frontend/class-frontend.php +++ b/includes/frontend/class-frontend.php @@ -21,13 +21,13 @@ class WC_NFe_FrontEnd { */ public function __construct() { // Filters. - add_filter( 'woocommerce_my_account_my_orders_columns', [ $this, 'nfe_column' ] ); - add_filter( 'woocommerce_my_account_my_address_description', [ $this, 'account_desc' ] ); + add_filter( 'woocommerce_my_account_my_orders_columns', [ $this, 'nfe_column' ] ); + add_filter( 'woocommerce_my_account_my_address_description', [ $this, 'account_desc' ] ); // Actions. add_action( 'woocommerce_my_account_my_orders_column_sales-receipt', [ $this, 'column_content' ] ); - add_action( 'woocommerce_order_details_after_order_table', [ $this, 'column_content' ] ); - add_action( 'woocommerce_before_edit_address_form_billing', [ $this, 'billing_notice' ] ); + add_action( 'woocommerce_order_details_after_order_table', [ $this, 'column_content' ] ); + add_action( 'woocommerce_before_edit_address_form_billing', [ $this, 'billing_notice' ] ); } /** @@ -36,7 +36,7 @@ public function __construct() { * @return void */ public function billing_notice() { - if ( nfe_get_field('nfe_enable') === 'yes' ) { + if ( nfe_get_field( 'nfe_enable' ) === 'yes' ) { echo '

' . esc_html__( 'The following address will also be used when issuing a NFe Sales Receipt.', 'woo-nfe' ) . '
'; } } @@ -53,6 +53,8 @@ public function account_desc() { /** * NFe Column Header on Recent Orders * + * @param array $columns Columns. + * * @return array */ public function nfe_column( $columns ) { @@ -75,12 +77,13 @@ public function nfe_column( $columns ) { * @since 1.0.9 * * @param WC_Order $order Order object. + * * @return void */ public function column_content( $order ) { // Get order information. - $order_id = $order->get_id(); - $nfe = get_post_meta( $order_id, 'nfe_issued', true ); + $order_id = $order->get_id(); + $nfe = get_post_meta( $order_id, 'nfe_issued', true ); if ( 'yes' !== nfe_get_field( 'nfe_enable' ) ) { return; diff --git a/includes/nfe-functions.php b/includes/nfe-functions.php index 259f850..15a6984 100644 --- a/includes/nfe-functions.php +++ b/includes/nfe-functions.php @@ -14,6 +14,7 @@ * Function to fetch fields from the NFe WooCommerce Integration. * * @param string $value Value to fetch. + * * @return string */ function nfe_get_field( $value = '' ) { @@ -32,6 +33,7 @@ function nfe_get_field( $value = '' ) { * Make sure address is required and if all the fields are available. * * @param int $order_id Order ID. + * * @return bool */ function nfe_order_address_filled( $order_id ) { @@ -41,7 +43,7 @@ function nfe_order_address_filled( $order_id ) { return true; } - $fields = array( + $fields = [ 'neighborhood' => get_post_meta( $order_id, '_billing_neighborhood', true ), 'address_1' => get_post_meta( $order_id, '_billing_address_1', true ), 'number' => get_post_meta( $order_id, '_billing_number', true ), @@ -49,7 +51,7 @@ function nfe_order_address_filled( $order_id ) { 'state' => get_post_meta( $order_id, '_billing_state', true ), 'city' => get_post_meta( $order_id, '_billing_city', true ), 'country' => get_post_meta( $order_id, '_billing_country', true ), - ); + ]; $count = 0; foreach ( $fields as $field => $value ) { @@ -119,8 +121,8 @@ function nfe_get_order_by_nota_value( $value ) { 'post_status' => 'any', 'meta_query' => array( // WPCS: slow query ok. array( - 'key' => 'nfe_issued', - 'value' => sprintf( ':"%s";', $value ), + 'key' => 'nfe_issued', + 'value' => sprintf( ':"%s";', $value ), 'compare' => 'LIKE', ), ), @@ -158,3 +160,33 @@ function nfe_require_address() { return true; } + +/** + * Get NFe status label. + * + * @param string $status Status. + * + * @return string + */ +function nfe_status_label( $status ) { + + // Check processing status first. + if ( in_array( $status, nfe_processing_status(), true ) ) { + return __( 'Processing NFe', 'woo-nfe' ); + } + + $valid_stati = [ + 'Issued' => __( 'NFe Issued', 'woo-nfe' ), + 'Cancelled' => __( 'NFe Cancelled', 'woo-nfe' ), + 'CancelledFailed' => __( 'NFe Cancelling Failed', 'woo-nfe' ), + 'IssueFailed' => __( 'NFe Issuing Failed', 'woo-nfe' ), + ]; + + foreach ( $valid_stati as $key => $title ) { + if ( $status === $key ) { + return $title; + } + } + + return ''; +} diff --git a/package.json b/package.json index 5d7df72..acfa9ae 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nfe", "title": "WooCommerce NFe", - "version": "1.2.7", + "version": "1.2.8", "author": "NFe.io", "homepage": "https://github.com/nfe/woo-nfe", "description": "WooCommerce extension for issuing invoices using the NFe.io API", @@ -14,11 +14,7 @@ "grunt": "^0.4.5", "grunt-checktextdomain": "^1.0.1", "grunt-wp-i18n": "^0.5.4", - "load-grunt-tasks": "^3.3.0", - "wp-shuttle": "github:wpsh/wpsh" - }, - "scripts": { - "wpsh": "wpsh" + "load-grunt-tasks": "^3.3.0" }, "engines": { "node": ">=0.8.0", diff --git a/woo-nfe.php b/woo-nfe.php index fab0998..1357b7c 100644 --- a/woo-nfe.php +++ b/woo-nfe.php @@ -11,7 +11,7 @@ * Plugin Name: WooCommerce NFe * Plugin URI: https://github.com/nfe/woo-nfe * Description: WooCommerce extension for the NFe API - * Version: 1.2.7 + * Version: 1.2.8 * Author: NFe.io * Author URI: https://nfe.io * Developer: Project contributors @@ -21,7 +21,7 @@ * Network: false * * WC requires at least: 3.3.5 - * WC tested up to: 3.4.3 + * WC tested up to: 3.5.1 * * Copyright: © 2018 NFe.io * License: GNU General Public License v3.0 @@ -101,14 +101,8 @@ private function setup_globals() { */ private function includes() { - // NFe Client-PHP API - Composer Support. - $composer_path = $this->plugin_dir . 'vendor/autoload.php'; - - if ( ! file_exists( $composer_path ) ) { - require( $this->plugin_dir . 'li/client-php/lib/init.php' ); - } else { - require( $composer_path ); - } + // NFe Client-PHP API. + require( $this->plugin_dir . 'li/client-php/lib/init.php' ); // Admin. require( $this->includes_dir . 'nfe-functions.php' ); @@ -140,12 +134,6 @@ private function dependencies() { add_action( 'admin_notices', [ $this, 'woocommerce_missing_notice' ] ); return; } - - // Checks if WooCommerce Extra Checkout Fields for Brazil is installed. - if ( ! class_exists( 'Extra_Checkout_Fields_For_Brazil' ) ) { - add_action( 'admin_notices', [ $this, 'extra_checkout_fields_missing_notice' ] ); - return; - } } /** @@ -172,8 +160,8 @@ private function setup_hooks() { } // Filters. - add_filter( 'woocommerce_integrations', array( $this, 'nfe_integration' ) ); - add_filter( 'plugin_action_links_' . $this->basename , array( $this, 'plugin_action_links' ) ); + add_filter( 'woocommerce_integrations', array( $this, 'nfe_integration' ) ); + add_filter( 'plugin_action_links_' . $this->basename, [ $this, 'plugin_action_links' ] ); } /** @@ -227,15 +215,6 @@ public function woocommerce_missing_notice() { include $this->includes_dir . 'admin/views/html-notice-missing-woocommerce.php'; } - /** - * WooCommerce Extra Checkout Fields for Brazil missing notice. - * - * @since 1.0.0 - */ - public function extra_checkout_fields_missing_notice() { - include $this->includes_dir . 'admin/views/html-notice-missing-woocommerce-extra-checkout-fields.php'; - } - /** * Action links. * @@ -256,7 +235,7 @@ public function plugin_action_links( $links ) { * @param string $version Version to check against. * @return bool */ - protected function version_check( $version = '3.3.3' ) { + protected function version_check( $version = '3.5.1' ) { if ( class_exists( 'WooCommerce' ) ) { global $woocommerce; if ( version_compare( $woocommerce->version, $version, '>=' ) ) {