<?php
//----------------------------------------------------------------------
// src/Logging/Activity/ClientLog.php
// Used only for Prospect conversion / edit for now
// All other events are logged by either IndividualLog or StoreLog
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Client\Client;
use App\Entity\Client\Individual;
use App\Logging\Tools;
use App\Services\LogTools;
class ClientLog
{
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(Client $client)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
public function logChanges(Client $client, $changes)
{
$individual = $client->getIndividual();
if ($individual === null) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($client);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($client, $individual, $loggingData);
//----------------------------------------------------------------------
$action = "prospect_edit";
// This is triggered when we are actually changing the connected Address object
// The changes in the actual object are handled in AddressLog
$addressChanges = array(
);
$basicChanges = array(
"commercialConditions",
);
$basicBoolChanges = array(
"prospect",
);
$dateChanges = array(
"relaunchDate",
"contractExpiryDate",
);
$labelChanges = array(
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
// Something to skip ?
if (false)
{
continue;
}
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
if (in_array($key, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicBoolChanges))
{
$args = $this->tools->handleBasicBoolChanges($args, $before, $after);
$args["action"] = "prospect_convert_to_client";
$args["old_value"] = null;
$args["new_value"] = null;
$pendingLogArgs[] = $args;
continue;
}
if (in_array($key, $dateChanges))
{
// Display d/m/Y H:i => Add a fourth param true to handleDateChanges
// 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;
}
}
return $pendingLogArgs;
}
public function logRemoval(Client $client)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(Client $client, Individual $individual, $loggingData)
{
$objectEntity = "Client.Prospect";
$receiver = $client;
$society = $individual->getSociety();
$societyGroup = $society->getSocietyGroup();
$args = array(
"object_id" => $individual->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $individual->displayForLogging(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}