<?php
//----------------------------------------------------------------------
// src/Logging/FinancialLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\PropertyAccess\PropertyAccess;
use App\Entity\Client\Client;
use App\Entity\Client\Financial;
use App\Entity\Platform\Supplier;
use App\Logging\DeletionContextLogger;
use App\Services\LogTools;
class FinancialLog
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->contextLogger = $contextLogger;
}
public function logCreation(Financial $financial)
{
return [];
}
public function logChanges(Financial $financial, $changes)
{
// Try to identify owner of financial
$logs = $this->logClient($financial, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logSupplier($financial, $changes);
if (!empty($logs))
{
return $logs;
}
// Nothing was found
return [];
}
public function logRemoval(Financial $financial)
{
return [];
}
public function logClient(Financial $financial, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($financial);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Object
$action = null;
$client = $this->em->getRepository(Client::class)->findOneByFinancial($financial);
if ($client !== null)
{
if ($client->isIndividual())
{
$action = "individual_edit_financial_";
}
else
{
$action = "store_edit_financial_";
}
}
if ($action === null)
{
// The owner of the given financial was not identified
return [];
}
// If we are here it means that we have an owner and an action
// Fill the common arguments
if ($client->isIndividual())
{
$object = $client->getIndividual();
$objectClient = $client;
$objectEntity = "Individual";
$society = $client->getIndividual()->getSociety();
$societyGroup = $client->getSocietyGroup();
}
else
{
$object = $client->getStore();
$objectClient = null;
$objectEntity = "Store";
$society = null;
$societyGroup = $client->getSocietyGroup();
}
$args = array(
"action" => $action,
"object_id" => $object->getId(),
"object_bundle" => "Client",
"object_entity" => $objectEntity,
"object_display" => $object->display(),
"object_client_id" => $objectClient !== null ? $objectClient->getId() : null,
"object_client_display" => $objectClient !== null ? $objectClient->getName() : null,
"info" => $info,
"society_group" => $societyGroup,
"society" => $society,
"special_author" => $specialAuthor,
);
$setsOfValues = $this->computeFinancialChanges($financial, $changes);
if (!empty($setsOfValues))
{
foreach ($setsOfValues as $key => $values)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$args["action"] = $action.$values["name"];
$pendingLogArgs[] = $args;
}
}
return $pendingLogArgs;
}
public function logSupplier(Financial $financial, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($financial);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Object
$action = null;
$supplier = $this->em->getRepository(Supplier::class)->findOneByFinancial($financial);
if ($supplier !== null)
{
$action = "supplier_edit_financial_";
}
if ($action === null)
{
// The owner of the given financial was not identified
return [];
}
// If we are here it means that we have an owner and an action
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $supplier->getId(),
"object_bundle" => "Platform",
"object_entity" => "Supplier",
"object_display" => $supplier->display(),
"info" => $info,
"society_group" => $supplier->getSocietyGroup(),
"society" => $supplier->getSociety(),
"special_author" => $specialAuthor,
);
$setsOfValues = $this->computeFinancialChanges($financial, $changes);
if (!empty($setsOfValues))
{
foreach ($setsOfValues as $key => $values)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$args["action"] = $action.$values["name"];
$pendingLogArgs[] = $args;
}
}
return $pendingLogArgs;
}
// TODO : When all Financial logging is done here
// remove this method from FinancialTools
public function computeFinancialChanges(Financial $financial, $changesSet)
{
if (count($changesSet) < 1)
return null;
$basicsChanges = array('account');
$labelChanges = array('paymentType', 'paymentDeadline');
$logs = [];
foreach ($changesSet as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
$before = $change[0];
$after = $change[1];
if (in_array($key, $basicsChanges))
{
$logs[$key]['old'] = $before;
$logs[$key]['new'] = $after;
$logs[$key]['name'] = $name;
continue;
}
if (in_array($key, $labelChanges))
{
if ($before !== null) $logs[$key]['old'] = $before->getValue();
else $logs[$key]['old'] = "";
if ($after !== null) $logs[$key]['new'] = $after->getValue();
else $logs[$key]['new'] = "";
$logs[$key]['name'] = $name;
continue;
}
}
return $logs;
}
}