Skip to content

Commit

Permalink
fix(cron): corrige emissão de notas em dia especifico
Browse files Browse the repository at this point in the history
Corrige a geração de notas para quando é configurado número de dias para 
emissão após o pagamento da fatura.

Ref: #103
  • Loading branch information
andrekutianski committed Dec 22, 2021
1 parent 846e4c5 commit 5e06bf0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion modules/addons/gofasnfeio/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});

Expand Down
62 changes: 52 additions & 10 deletions modules/addons/gofasnfeio/hooks/dailycronjob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}

}

0 comments on commit 5e06bf0

Please sign in to comment.