<?php
//----------------------------------------------------------------------
// src/AppRemix/Logging/InvoiceLog.php
//----------------------------------------------------------------------
namespace App\AppRemix\Logging\Objects;
use Doctrine\Persistence\ManagerRegistry;
use App\AppRemix\Entity\Invoice;
use App\AppRemix\Logging\Tools;
use App\Services\LogTools;
class InvoiceLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(Invoice $invoice)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($invoice);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($invoice, $loggingData);
//----------------------------------------------------------------------
$action = "invoice_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Invoice $invoice, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($invoice);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($invoice, $loggingData);
//----------------------------------------------------------------------
$editMode = "invoice_edit";
$args["action"] = $editMode;
$basicChanges = array(
"tva",
"type",
"annulled",
);
$jsonChanges = array(
"billingAddress",
);
// Method getLogLabel() should be defined for these objects/entities.
// A late attach logs invoice_edit_client with the client's label.
$objectChanges = array(
"creditInvoice",
"client",
);
$dateChanges = array(
"sentToClient",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
// Something to skip ?
if (strpos($key, "total") === 0)
{
continue;
}
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $editMode."_".$name;
$before = $change[0];
$after = $change[1];
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
if (in_array($key, $jsonChanges))
{
$pendingLogArgs[] = $this->tools->handleJsonChanges($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;
}
}
// See what changed and log (relationships OneToMany and ManyToMany second)
if ($invoice->getItems()->isDirty())
{
$old = "";
foreach ($invoice->getItems()->getSnapshot() as $item)
{
$old .= $item->displayFull();
$old .= " | ";
}
if ($old != "")
{
$old = substr($old, 0, -1);
$old = substr($old, 0, -1);
$old = substr($old, 0, -1);
}
$new = "";
foreach ($invoice->getItems() as $item)
{
$new .= $item->displayFull();
$new .= " | ";
}
if ($new != "")
{
$new = substr($new, 0, -1);
$new = substr($new, 0, -1);
$new = substr($new, 0, -1);
}
$args["action"] = $editMode."_items";
$args["old_value"] = $old;
$args["new_value"] = $new;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Invoice $invoice)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(Invoice $invoice, $loggingData)
{
$client = $invoice->getClient();
$args = array(
"object_id" => $invoice->getId(),
"object_entity" => "Invoice",
"object_display" => $invoice->display(),
"society" => $invoice->getSociety(),
// Null for a client created in this flush — handleClient() fills it post-flush.
"client_id" => $client?->getId(),
"client_display" => $client?->displayForListing(),
"info" => $loggingData['info'],
);
return $args;
}
}