<?php
//----------------------------------------------------------------------
// src/Logging/InstallmentLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\Platform\Installment;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\LogTools;
class InstallmentLog
{
// private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->contextLogger = $contextLogger;
$this->tools = $tools;
}
public function logCreation(Installment $installment)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($installment);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
if ($installment->getDevis() !== null)
{
$module = "devis";
$action = "devis_edit_installment";
$object = $installment->getDevis();
$objectClient = $installment->getDevis()->getReceiver();
$objectMission = $installment->getDevis()->getMission();
$objectEntity = "Devis";
}
else
{
// Plan.io Task #4326 : Exclude Drafts
if ($installment->getInvoice() !== null)
{
if ($installment->getInvoice()->isDraft())
{
// Nothing to do here
return $pendingLogArgs;
}
$module = "invoice";
$action = "invoice_edit_installment";
$object = $installment->getInvoice();
$objectClient = $installment->getInvoice()->getReceiver();
$objectMission = $installment->getInvoice()->getMission();
$objectEntity = "Invoice";
}
else
{
if ($installment->getCommand() !== null)
{
$module = "command";
$action = "command_edit_installment";
$object = $installment->getCommand();
$objectClient = $installment->getCommand()->getReceiver();
$objectMission = null;
$objectEntity = "Command";
}
else
{
// Nothing to do here
return $pendingLogArgs;
}
}
}
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $object->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $object->getRef(),
"object_client_id" => $objectClient !== null ? $objectClient->getId() : null,
"object_client_display" => $objectClient !== null ? $objectClient->getName() : null,
"object_mission_id" => $objectMission !== null ? $objectMission->getId() : null,
"object_mission_display" => $objectMission !== null ? $objectMission->getRef() : null,
"info" => $info,
"society_group" => $object->getSociety()->getGroup(),
"society" => $object->getSociety(),
"special_author" => $specialAuthor,
);
$args["action"] = $action."_add";
$args["new_value"] = $installment->getValue();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Installment $installment, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($installment);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
if ($installment->getDevis() !== null)
{
$module = "devis";
$action = "devis_edit_installment";
$object = $installment->getDevis();
$objectClient = $installment->getDevis()->getReceiver();
$objectMission = $installment->getDevis()->getMission();
$objectEntity = "Devis";
}
else
{
if ($installment->getInvoice() !== null)
{
if ($installment->getInvoice()->isDraft())
{
// Nothing to do here
return $pendingLogArgs;
}
$module = "invoice";
$action = "invoice_edit_installment";
$object = $installment->getInvoice();
$objectClient = $installment->getInvoice()->getReceiver();
$objectMission = $installment->getInvoice()->getMission();
$objectEntity = "Invoice";
}
else
{
if ($installment->getCommand() !== null)
{
$module = "command";
$action = "command_edit_installment";
$object = $installment->getCommand();
$objectClient = $installment->getCommand()->getReceiver();
$objectMission = null;
$objectEntity = "Command";
}
else
{
// Nothing to do here
return $pendingLogArgs;
}
}
}
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $object->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $object->getRef(),
"object_client_id" => $objectClient !== null ? $objectClient->getId() : null,
"object_client_display" => $objectClient !== null ? $objectClient->getName() : null,
"object_mission_id" => $objectMission !== null ? $objectMission->getId() : null,
"object_mission_display" => $objectMission !== null ? $objectMission->getRef() : null,
"info" => $info,
"society_group" => $object->getSociety()->getGroup(),
"society" => $object->getSociety(),
"special_author" => $specialAuthor,
);
$basicChanges = array(
"value",
"info"
);
$labelChanges = array(
"status",
"paymentType",
);
$dateChanges = array(
"paymentDate",
"deadlineDate",
);
// 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))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($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;
}
}
return $pendingLogArgs;
}
public function logRemoval(Installment $installment)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($installment);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// So this is kinda interesting
// I don't want to log the removal of installments when validation of a draft
// But when a draft is validated and its installments deleted
// no data is saved to the context for those drafts
// So this method will simply fail grafully :)
$cachedData = $this->contextLogger->getContext($installment);
if ($cachedData === null || empty($cachedData))
{
// Nothing to do here
return $pendingLogArgs;
}
if (array_key_exists('devis', $cachedData) && $cachedData['devis'] !== null)
{
$module = "devis";
$action = "devis_edit_installment";
$object = $cachedData['devis'];
$objectClient = $object->getReceiver();
$objectMission = $object->getMission();
$objectEntity = "Devis";
}
else
{
if (array_key_exists('invoice', $cachedData) && $cachedData['invoice'] !== null)
{
$module = "invoice";
$action = "invoice_edit_installment";
$object = $cachedData['invoice'];
$objectClient = $cachedData['invoice']->getReceiver();
$objectMission = $object->getMission();
$objectEntity = "Invoice";
}
else
{
if (array_key_exists('command', $cachedData) && $cachedData['command'] !== null)
{
$module = "command";
$action = "command_edit_installment";
$object = $cachedData['command'];
$objectClient = $object->getReceiver();
$objectMission = null;
$objectEntity = "Command";
}
else
{
// Nothing to do here
return $pendingLogArgs;
}
}
}
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $object->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $object->getRef(),
"object_client_id" => $objectClient !== null ? $objectClient->getId() : null,
"object_client_display" => $objectClient !== null ? $objectClient->getName() : null,
"object_mission_id" => $objectMission !== null ? $objectMission->getId() : null,
"object_mission_display" => $objectMission !== null ? $objectMission->getRef() : null,
"info" => $info,
"society_group" => $object->getSociety()->getGroup(),
"society" => $object->getSociety(),
"special_author" => $specialAuthor,
);
$args["action"] = $action."_delete";
$args["old_value"] = $installment->getValue();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
}