From 5e06bf0e9f41fee3c45b6d2c25ea91e54682b15a Mon Sep 17 00:00:00 2001 From: Andre Belafronte Date: Wed, 22 Dec 2021 11:13:06 -0300 Subject: [PATCH] =?UTF-8?q?fix(cron):=20corrige=20emiss=C3=A3o=20de=20nota?= =?UTF-8?q?s=20em=20dia=20especifico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Corrige a geração de notas para quando é configurado número de dias para emissão após o pagamento da fatura. Ref: #103 --- modules/addons/gofasnfeio/hooks.php | 2 +- .../addons/gofasnfeio/hooks/dailycronjob.php | 62 ++++++++++++++++--- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/modules/addons/gofasnfeio/hooks.php b/modules/addons/gofasnfeio/hooks.php index 8f77acc..1d7b832 100644 --- a/modules/addons/gofasnfeio/hooks.php +++ b/modules/addons/gofasnfeio/hooks.php @@ -5,7 +5,7 @@ add_hook('InvoiceCreation', 1, function ($vars) { require_once __DIR__ . '/functions.php'; require_once __DIR__ . '/sendNFE.php'; - require_once __DIR__ . '/hooks/dailycronjob.php'; + //require_once __DIR__ . '/hooks/dailycronjob.php'; require_once __DIR__ . '/hooks/invoicecreation.php'; }); diff --git a/modules/addons/gofasnfeio/hooks/dailycronjob.php b/modules/addons/gofasnfeio/hooks/dailycronjob.php index 489f653..06292be 100644 --- a/modules/addons/gofasnfeio/hooks/dailycronjob.php +++ b/modules/addons/gofasnfeio/hooks/dailycronjob.php @@ -3,23 +3,65 @@ if (!defined('WHMCS')) { exit(); } + use WHMCS\Database\Capsule; $params = gnfe_config(); -$data = getTodaysDate(false); -$dataAtual = toMySQLDate($data); + if (isset($params['issue_note_after']) && (int)$params['issue_note_after'] > 0) { - foreach (Capsule::table('tblinvoices')->whereBetween('date', [$params['initial_date'], $dataAtual])->where('status', '=', 'Paid')->get(['id', 'userid', 'datepaid', 'total']) as $invoices) { - foreach (Capsule::table('gofasnfeio')->where('status', '=', 'Waiting')->where('invoice_id', '=', $invoices->id)->get(['id', 'nfe_id', 'status', 'created_at', 'invoice_id', 'service_code', 'services_amount']) as $nfeio) { - $datepaid = date('Ymd', strtotime($invoices->datepaid)); - $datepaid_to_issue_ = '-' . $params['issue_note_after'] . ' days'; - $datepaid_to_issue = date('Ymd', strtotime($datepaid_to_issue_)); - if ((float) $invoices->total > '0.00' and (int) $datepaid_to_issue >= (int) $datepaid) { - logModuleCall('gofas_nfeio', 'dailycronjob', 'emitNFE', '', ''); + $todayDate = date("Y-m-d"); + // qtd de dias configurado para gerar nf apos pgto + $issueNoteAfterDays = $params['issue_note_after']; + // instancia o dia atual + $invoicesPaidOnDay = date_create($todayDate); + // subtrai a quantidade de dias com base no dia atual para chegar no dia que deverá ser verificado + date_sub($invoicesPaidOnDay, new DateInterval("P{$issueNoteAfterDays}D")); - emitNFE($invoices,$nfeio); + // seleciona todas as faturas que tenham sido pagas no dia calculado em $invoicesPaidOnDay + $invoicesToGenerateData = Capsule::table('tblinvoices')->whereDate('datepaid', $invoicesPaidOnDay->format('Y-m-d'))->select(['id as invoice_id', 'total'])->get(); + // coleção com os IDs das faturas encontradas + $invoicesToGenerateID = []; + // alimenta a coleção com os dados + if (count($invoicesToGenerateData) > 0) { + foreach ($invoicesToGenerateData as $invoice) { + if ($invoice->total > 0) { + $invoicesToGenerateID[] = $invoice->invoice_id; } } } + + // seleciona todas as possiveis NF já geradas para as faturas encontradas + $alreadyGenerateNFData = []; + $queryNfs = Capsule::table('gofasnfeio')->whereIn('invoice_id', $invoicesToGenerateID)->select('invoice_id')->get(); + if (count($queryNfs) > 0) { + foreach ($queryNfs as $data) { + $alreadyGenerateNFData[] = $data->invoice_id; + } + } + // calcula a diferença das coleções + $invoicesIdToGenerateNF = array_diff($invoicesToGenerateID, $alreadyGenerateNFData); + + + logModuleCall('nfeio', 'dailycronjob', array( + "todayDate =>" => $todayDate, + "issueNoteAfterDays" => $issueNoteAfterDays, + "invoicesPaidOnDay" => $invoicesPaidOnDay->format('Y-m-d'), + "toMySQLDateStart = " => $invoicesPaidOnDay->setTime(0, 0, 0)->format('Y-m-d H:i:s.000'), + "toMySQLDateEnd = " => $invoicesPaidOnDay->setTime(23, 59, 59)->format('Y-m-d H:i:s.000'), + "invoicesToGenerateData =>" => $invoicesToGenerateData, + "invoicesToGenerateID" => $invoicesToGenerateID, + "alreadyGenerateNFData" => $alreadyGenerateNFData, + "invoicesIdToGenerateNF" => $invoicesIdToGenerateNF, + ), ''); + + // percorre a coleção e emite as notas necessárias + if (count($invoicesIdToGenerateNF) > 0) { + foreach ($invoicesIdToGenerateNF as $invoice) { + $queue = gnfe_queue_nfe($invoice); + logModuleCall('nfeio', 'dailycronjob emissão de notas', $invoice, $queue); + + } + } + }