1212 * @since 2.0
1313 * @author Mimir Tech https://github.com/mimirtechco
1414 */
15-
1615class Nfe
1716{
1817 /**
@@ -250,10 +249,12 @@ private function buildItemsToTransmit($items, $invoiceId, $userId, $companyId, $
250249 $ itemsTotal = $ itemsTotal + $ value ['itemAmount ' ];
251250 }
252251 // adiciona a descrição da nota no formato parametrizado
252+ // phpcs:ignore Generic.Files.LineLength.TooLong
253253 $ nfData ['nfe_description ' ] = \NFEioServiceInvoices \Helpers \Invoices::generateNfServiceDescription ($ invoiceId , $ itemsDescription );
254254 // adiciona o valor total calculado para os itens
255255 $ nfData ['services_amount ' ] = $ itemsTotal ;
256256 // gera id unico externo
257+ // phpcs:ignore Generic.Files.LineLength.TooLong
257258 $ nfData ['nfe_external_id ' ] = $ this ->generateUniqueExternalId ($ userId , $ invoiceId , $ itemsTotal , $ companyId , $ serviceCode , $ reissue );
258259
259260 // verifica se há calculo de retenção de ISS personalizado
@@ -270,7 +271,9 @@ private function buildItemsToTransmit($items, $invoiceId, $userId, $companyId, $
270271 /**
271272 * se houver retenção personalizada e for diferente de zero, usa valor personalizado para cálculo.
272273 */
274+ // phpcs:ignore Generic.Files.LineLength.TooLong
273275 if (!is_null ($ customIssHeld ) and $ customIssHeld != 0 ) {
276+ // phpcs:ignore Generic.Files.LineLength.TooLong
274277 $ nfData ['iss_held ' ] = \NFEioServiceInvoices \Helpers \Invoices::getIssHeldAmount ($ itemsTotal , $ customIssHeld );
275278 }
276279 // se valor total dos itens for maior que zero adiciona as informações para retorno
@@ -296,6 +299,7 @@ private function buildItemsToTransmit($items, $invoiceId, $userId, $companyId, $
296299 * @param $itemsTotal
297300 * @return string
298301 */
302+ // phpcs:ignore Generic.Files.LineLength.TooLong
299303 private function generateUniqueExternalId ($ userId , $ invoiceId , $ itemsTotal , $ companyId , $ serviceCode , $ reissue = false )
300304 {
301305 $ separator = '- ' ;
@@ -307,25 +311,26 @@ private function generateUniqueExternalId($userId, $invoiceId, $itemsTotal, $com
307311 $ suffix = 'REISSUE ' ;
308312 // usa um timestamp para tornar cada reemissão unica para a criação do ID
309313 $ dateTimeNow = date ('Y-m-d H:i:s ' );
314+ // phpcs:ignore Generic.Files.LineLength.TooLong
310315 $ result = md5 ($ prefix . $ separator . $ userId . $ separator . $ invoiceId . $ separator . $ companyId . $ separator . $ serviceCode . $ separator . $ itemsTotal . $ separator . $ suffix . $ separator . $ dateTimeNow );
311316 } else {
317+ // phpcs:ignore Generic.Files.LineLength.TooLong
312318 $ result = md5 ($ prefix . $ separator . $ userId . $ separator . $ invoiceId . $ separator . $ companyId . $ separator . $ serviceCode . $ serviceCode . $ itemsTotal );
313319 }
314320
315321 return $ result ;
316322 }
317323
318324
319-
320325 /**
321326 * Cria a nota fiscal com base na fatura e insere na fila (tabela) para emissão.
322327 *
323- * @version 2.1.0
324- * @author Andre Bellafronte
325328 * @param $invoiceId int|string ID da fatura do WHMCS
326329 * @param $reissue boolean informe 'true' quando for
327330 * reemissão
328331 * @return array|bool[] status da inserção na fila
332+ * @author Andre Bellafronte
333+ * @version 2.1.0
329334 */
330335 public function queue ($ invoiceId , $ reissue = false )
331336 {
@@ -353,7 +358,7 @@ public function queue($invoiceId, $reissue = false)
353358 // dados da empresa padrao
354359 $ companyId = $ defaultCompany ->company_id ;
355360 $ defaultServiceCode = $ defaultCompany ->service_code ;
356- $ issHeld = $ defaultCompany ->iss_held ;
361+ $ issHeld = $ defaultCompany ->iss_held ;
357362 }
358363
359364 // $defaultServiceCode = $this->storage->get('service_code');
@@ -379,21 +384,31 @@ public function queue($invoiceId, $reissue = false)
379384 }
380385
381386 // prepara o item e o adiciona em um array associativo com o código do serviço
387+ // phpcs:ignore Generic.Files.LineLength.TooLong
382388 $ itemsByServiceCode [$ serviceCode ][] = $ this ->prepareItemsToTransmit ($ clientId , $ invoiceId , $ serviceCode , $ item );
383389 }
384390
391+ // phpcs:ignore Generic.Files.LineLength.TooLong
385392 $ nfToEmit = $ this ->buildItemsToTransmit ($ itemsByServiceCode , $ invoiceId , $ clientId , $ companyId , $ issHeld , $ reissue );
386393
387394 if (count ($ nfToEmit ) > 0 ) {
388395 foreach ($ nfToEmit as $ nf ) {
389396 /**
390397 * verifica se existe um external_id igual
391398 */
392- $ hasExternalId = Capsule::table ($ this ->serviceInvoicesTable )->where ('nfe_external_id ' , '= ' , $ nf ['nfe_external_id ' ])->first ();
399+ $ hasExternalId = Capsule::table ($ this ->serviceInvoicesTable )
400+ ->where ('nfe_external_id ' , '= ' , $ nf ['nfe_external_id ' ])->first ();
393401
394402 // se já houver uma nota no banco local com o mesmo external_id pula a emissão de nota
395403 if (is_array ($ hasExternalId )) {
396- logModuleCall ('nfeio_serviceinvoices ' , 'nf_queue ' , "Um external_id idêntico foi encontrado para {$ nf ['nfe_external_id ' ]}, NF não adicionada para transmissão " , $ hasExternalId );
404+ // phpcs:ignore Generic.Files.LineLength.TooLong
405+ logModuleCall (
406+ 'nfeio_serviceinvoices ' ,
407+ 'nf_queue ' ,
408+ // phpcs:ignore Generic.Files.LineLength.TooLong
409+ "Um external_id idêntico foi encontrado para {$ nf ['nfe_external_id ' ]}, NF não adicionada para transmissão " ,
410+ $ hasExternalId
411+ );
397412 continue ;
398413 }
399414
@@ -444,7 +459,7 @@ public function emit($data)
444459 $ environment = $ data ->environment ;
445460 $ clientData = \WHMCS \User \Client::find ($ clientId );
446461 $ customer = $ this ->legacyFunctions ->gnfe_customer ($ clientId , $ clientData );
447- $ emailNfeConfig = (bool ) $ this ->storage ->get ('gnfe_email_nfe_config ' );
462+ $ emailNfeConfig = (bool )$ this ->storage ->get ('gnfe_email_nfe_config ' );
448463 $ client_email = $ emailNfeConfig ? $ clientData ->email : '' ;
449464
450465 logModuleCall ('nfeio_serviceinvoices ' , 'nf_emit_for_customer ' , $ data , $ customer );
@@ -465,7 +480,11 @@ public function emit($data)
465480 $ street = $ array_adress [0 ];
466481 $ number = $ array_adress [1 ];
467482 } else {
468- $ street = str_replace (', ' , '' , preg_replace ('/[0-9]+/i ' , '' , $ clientData ->address1 ));
483+ $ street = str_replace (', ' , '' , preg_replace (
484+ '/[0-9]+/i ' ,
485+ '' ,
486+ $ clientData ->address1
487+ ));
469488 $ number = preg_replace ('/[^0-9]/ ' , '' , $ clientData ->address1 );
470489 }
471490
@@ -474,7 +493,11 @@ public function emit($data)
474493 return ;
475494 }
476495
477- $ ibgeCode = $ this ->legacyFunctions ->gnfe_ibge (preg_replace ('/[^0-9]/ ' , '' , $ clientData ->postcode ));
496+ $ ibgeCode = $ this ->legacyFunctions ->gnfe_ibge (preg_replace (
497+ '/[^0-9]/ ' ,
498+ '' ,
499+ $ clientData ->postcode
500+ ));
478501
479502 if ($ ibgeCode ['error ' ]) {
480503 $ this ->legacyFunctions ->update_status_nfe ($ invoiceId , 'Error_cep ' );
@@ -538,7 +561,12 @@ public function updateLocalNfeStatus(string $nfRemoteId, string $status, string
538561 $ result = $ this ->serviceInvoicesRepo ->updateNfStatusByNfeId ($ nfRemoteId , $ status , $ flowStatus );
539562 // caso sucesso registra log
540563 if ($ result ) {
541- logModuleCall ('nfeio_serviceinvoices ' , 'updateLocalNfeStatus ' , ['nfe_id ' => $ nfRemoteId , 'status ' => $ status , 'flow_status ' => $ flowStatus ], $ result );
564+ logModuleCall (
565+ 'nfeio_serviceinvoices ' ,
566+ 'updateLocalNfeStatus ' ,
567+ ['nfe_id ' => $ nfRemoteId , 'status ' => $ status , 'flow_status ' => $ flowStatus ],
568+ $ result
569+ );
542570 }
543571
544572 return $ result ;
@@ -552,12 +580,18 @@ public function updateLocalNfeStatus(string $nfRemoteId, string $status, string
552580 * @param $flowStatus string|null O novo status de fluxo da NF
553581 * @return bool status da operação
554582 */
583+ // phpcs:ignore Generic.Files.LineLength.TooLong
555584 public function updateLocalNfeStatusByExternalId (string $ externalId , string $ status , string $ flowStatus = null ): bool
556585 {
557586 $ result = $ this ->serviceInvoicesRepo ->updateNfStatusByExternalId ($ externalId , $ status , $ flowStatus );
558587 // caso sucesso registra log
559588 if ($ result ) {
560- logModuleCall ('nfeio_serviceinvoices ' , 'updateLocalNfeStatusByExternalId ' , ['nfe_external_id ' => $ externalId , 'status ' => $ status , 'flow_status ' => $ flowStatus ], $ result );
589+ logModuleCall (
590+ 'nfeio_serviceinvoices ' ,
591+ 'updateLocalNfeStatusByExternalId ' ,
592+ ['nfe_external_id ' => $ externalId , 'status ' => $ status , 'flow_status ' => $ flowStatus ],
593+ $ result
594+ );
561595 }
562596
563597 return $ result ;
@@ -586,6 +620,7 @@ public function reissueNfbyId($nfId)
586620 $ tics = $ nfData ->tics ;
587621 $ dateNow = date ('Y-m-d H:i:s ' );
588622 // gera um novo ID externo unico para a reemissão do item/NF
623+ // phpcs:ignore Generic.Files.LineLength.TooLong
589624 $ externalUniqueId = $ this ->generateUniqueExternalId ($ userId , $ invoiceId , $ amount , $ companyId , $ serviceCode , true );
590625
591626 $ reissueNfData = [
@@ -622,10 +657,10 @@ public function reissueNfbyId($nfId)
622657 /**
623658 * Realiza a reemissão da(s) nota(s) com base no ID da fatura.
624659 *
625- * @version 2.1
626- * @author Andre Bellafronte <andre@eunarede.com>
627660 * @param $invoiceId integer ID da fatura
628661 * @return string[] status do resultado
662+ * @version 2.1
663+ * @author Andre Bellafronte <andre@eunarede.com>
629664 */
630665 public function reissueNfSeriesByInvoiceId ($ invoiceId )
631666 {
@@ -638,8 +673,12 @@ public function reissueNfSeriesByInvoiceId($invoiceId)
638673
639674 // verifica se todas as notas já existentes para a fatura estão canceladas para permitir a reemissão da série
640675 $ hasAllCancelled = $ this ->hasAllNfCancelled ($ invoiceId );
676+ // phpcs:ignore Generic.Files.LineLength.TooLong
641677 if (!$ hasAllCancelled ) {
642- return ['status ' => 'error ' , 'message ' => "Impossível reemitir fatura # {$ invoiceId }: ainda existem notas que não foram canceladas para a mesma. " ];
678+ return [
679+ 'status ' => 'error ' ,
680+ // phpcs:ignore Generic.Files.LineLength.TooLong
681+ 'message ' => "Impossível reemitir fatura # {$ invoiceId }: ainda existem notas que não foram canceladas para a mesma. " ];
643682 }
644683
645684 $ result = $ this ->queue ($ invoiceId , true );
@@ -653,15 +692,16 @@ public function reissueNfSeriesByInvoiceId($invoiceId)
653692 * Verifica se a fatura informada possui todas as notas vinculadas com mesmo status 'Cancelled'.
654693 * Isso previne que notas sejam reemitidas se anterior não estiver cancelada.
655694 *
656- * @version 2.1
657- * @author Andre Bellafronte <andre@eunarede.com>
658695 * @param $invoiceId integer ID da fatura a ser verificado
659696 * @return bool retorna `true` somente quando todas as notas existentes para a fatura possuírem status 'Cancelled'.
697+ * @version 2.1
698+ * @author Andre Bellafronte <andre@eunarede.com>
660699 */
661700 public function hasAllNfCancelled ($ invoiceId )
662701 {
663702 $ status = [];
664- $ query = Capsule::table ($ this ->serviceInvoicesTable )->where ('invoice_id ' , $ invoiceId )->distinct ()->pluck ('status ' );
703+ $ query = Capsule::table ($ this ->serviceInvoicesTable )->where ('invoice_id ' , $ invoiceId )
704+ ->distinct ()->pluck ('status ' );
665705
666706 foreach ($ query as $ value ) {
667707 $ status [] = $ value ;
@@ -677,10 +717,10 @@ public function hasAllNfCancelled($invoiceId)
677717 /**
678718 * Cancela as notas fiscais existentes para uma fatura.
679719 *
680- * @version 2.1
681- * @author Andre Bellafronte <andre@eunarede.com>
682720 * @param $invoiceId integer ID da fatura
683721 * @return string[] status e mensagem do resultado da operação.
722+ * @version 2.1
723+ * @author Andre Bellafronte <andre@eunarede.com>
684724 */
685725 public function cancelNfSeriesByInvoiceId ($ invoiceId )
686726 {
@@ -702,7 +742,7 @@ public function cancelNfSeriesByInvoiceId($invoiceId)
702742 if ($ result ->flowStatus && $ result ->status ) {
703743 $ this ->updateLocalNfeStatus ($ nf ->nfe_id , $ result ->status , $ result ->flowStatus );
704744 } else {
705- // caso nao tenha objeto, forca cancelamento local com status flow personalizado
745+ // caso nao tenha objeto, forca cancelamento local com status flow personalizado
706746 $ this ->updateLocalNfeStatus ($ nf ->nfe_id , 'Cancelled ' , 'ApiNoResponse ' );
707747 }
708748 }
0 commit comments