<?php
//----------------------------------------------------------------------
// src/AppRemix/Logging/DevisLog.php
//----------------------------------------------------------------------
namespace App\AppRemix\Logging\Objects;
use Doctrine\Persistence\ManagerRegistry;
use App\AppRemix\Entity\Devis;
use App\AppRemix\Logging\Tools;
use App\Services\LogTools;
class DevisLog
{
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(Devis $devis)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($devis);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($devis, $loggingData);
//----------------------------------------------------------------------
$action = "devis_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Devis $devis, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($devis);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($devis, $loggingData);
//----------------------------------------------------------------------
$editMode = "devis_edit";
$args["action"] = $editMode;
$basicChanges = array(
"tva",
);
$jsonChanges = array(
"interventionAddress",
);
// Method getLogLabel() should be defined for these objects/entities.
// A late attach logs devis_edit_client with the client's label.
$objectChanges = array(
"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 ($devis->getItems()->isDirty())
{
$old = "";
foreach ($devis->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 ($devis->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;
}
// The financial state is derived, so it has no changeset entry of its own
$stateLogs = $this->handleFinancialState($devis, $changes, $args, $editMode);
if ($stateLogs !== null) $pendingLogArgs[] = $stateLogs;
return $pendingLogArgs;
}
public function logRemoval(Devis $devis)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
/**
* Logs the devis's financial state whenever a flush moves it.
*
* Devis::getFinancialState() is computed and never persisted, so Doctrine reports no change for
* it. It reads five inputs — annulled, whether a client is set, totalTTC, totalRequested and
* totalPaid — and every money write path (devis/add/payment, devis/add/invoice, invoice/annul,
* payment/confirm, payment/unconfirm) updates the money columns in the same flush as the
* Payment it belongs to. A late client attach is in the same changeset, so the draft to
* not_invoiced transition is logged here too. Both ends of the transition are
* available: the changeset for what moved, the entity itself for what did not.
*
* Returns null when the state did not change.
*/
private function handleFinancialState(Devis $devis, $changes, $args, $editMode)
{
$before = $devis->resolveFinancialState(
(bool) $this->valueBefore($changes, "annulled", $devis->isAnnulled()),
$this->valueBefore($changes, "client", $devis->getClient()) !== null,
$this->valueBefore($changes, "totalTTC", $devis->getTotalTTC()),
$this->valueBefore($changes, "totalRequested", $devis->getTotalRequested()),
$this->valueBefore($changes, "totalPaid", $devis->getTotalPaid())
);
$after = $devis->getFinancialState();
if ($before === $after)
{
return null;
}
$args["action"] = $editMode."_financial_state";
return $this->tools->handleFinancialStateChanges($args, $before, $after);
}
/**
* The value a member held before this flush: the changeset when Doctrine saw it move,
* the current value otherwise.
*/
private function valueBefore($changes, $key, $current)
{
if (array_key_exists($key, $changes))
{
return $changes[$key][0];
}
return $current;
}
private function initArgs(Devis $devis, $loggingData)
{
$client = $devis->getClient();
$args = array(
"object_id" => $devis->getId(),
"object_entity" => "Devis",
"object_display" => $devis->display(),
"society" => $devis->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;
}
}