<?php
//----------------------------------------------------------------------
// src/Logging/Activity/EInvoicingInboundInvoiceLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\EInvoicing\EInvoicingInboundInvoice;
use App\Logging\Tools;
use App\Services\LogTools;
/**
* Activity log handler for received invoices, electronic and manual alike.
*
* Plan.io Task #4683
*/
class EInvoicingInboundInvoiceLog
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(EInvoicingInboundInvoice $invoice)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($invoice);
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($invoice, $loggingData);
//----------------------------------------------------------------------
// A manual invoice is entered by a user, an electronic one is delivered by the PDP
if ($invoice->isManual())
{
$action = "einvoicing_inbound_invoice_add";
}
else
{
$action = "einvoicing_inbound_invoice_receive";
}
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(EInvoicingInboundInvoice $invoice, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($invoice);
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($invoice, $loggingData);
//----------------------------------------------------------------------
$action = "einvoicing_inbound_invoice_edit";
$basicChanges = array(
"invoiceNumber",
"amountExclTax",
"amountVat",
"amountInclTax",
"accounted",
);
$dateChanges = array(
"invoiceDate",
"dueDate",
);
$labelChanges = array(
"tva",
"category",
// Plan.io Task #4683 : accounting status shared with the expense reports
"costStatus",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"society",
"supplier",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
if (in_array($key, $basicChanges))
{
if ($before == $after) continue;
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $dateChanges))
{
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
continue;
}
if (in_array($key, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
}
// Handle ManyToMany
if ($invoice->getCommands()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($invoice->getCommands()->getSnapshot() as $command)
{
$oldData .= $command->display();
$oldData .= ", ";
}
foreach ($invoice->getCommands() as $command)
{
$newData .= $command->display();
$newData .= ", ";
}
if ($oldData != "")
{
$oldData = substr($oldData, 0, -2);
}
if ($newData != "")
{
$newData = substr($newData, 0, -2);
}
$args["action"] = "einvoicing_inbound_invoice_edit_commands";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(EInvoicingInboundInvoice $invoice)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($invoice);
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($invoice, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "einvoicing_inbound_invoice_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(EInvoicingInboundInvoice $invoice, $loggingData)
{
$society = $invoice->getSociety();
$societyGroup = $invoice->getSocietyGroup();
$args = array(
"object_id" => $invoice->getId(),
"object_bundle" => "EInvoicing",
"object_entity" => "EInvoicingInboundInvoice",
"object_display" => $invoice->display(),
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}