<?php
//----------------------------------------------------------------------
// src/Logging/AddressLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\PropertyAccess\PropertyAccess;
use App\Entity\Access;
use App\Entity\Client\Individual;
use App\Entity\Client\Store;
use App\Entity\HR\Application\Application;
use App\Entity\HR\HumanResource;
use App\Entity\Location\Address;
use App\Entity\Mission\Mission;
use App\Entity\Platform\Command\Command;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Invoice\Invoice;
use App\Entity\Platform\Supplier;
use App\Entity\SocietyGroup;
use App\Logging\DeletionContextLogger;
use App\Services\LogTools;
class AddressLog
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->contextLogger = $contextLogger;
}
public function logCreation(Address $address)
{
return [];
}
public function logChanges(Address $address, $changes)
{
// Try to identify owner of address
$logs = $this->logApplication($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logCommand($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logDevis($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logHumanResource($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logIndividual($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logInvoice($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logMission($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logSocietyGroup($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logStore($address, $changes);
if (!empty($logs))
{
return $logs;
}
$logs = $this->logSupplier($address, $changes);
if (!empty($logs))
{
return $logs;
}
// Nothing was found
return [];
}
public function logRemoval(Address $address)
{
return [];
}
public function logApplication(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Devis
$action = null;
$application = $this->em->getRepository(Application::class)->findOneByAddress($address);
if ($application !== null)
{
$action = "application_edit_address";
}
if ($action === null)
{
// The owner of the given address 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" => $application->getId(),
"object_bundle" => "HR",
"object_entity" => "Application",
"object_display" => $application->getRef(),
"info" => $info,
"society_group" => $application->getSocietyGroup(),
"society" => $application->getSociety(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logHumanResource(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Devis
$action = null;
$humanResource = $this->em->getRepository(HumanResource::class)->findOneByAddress($address);
if ($humanResource !== null)
{
$action = "human_resource_edit_address";
}
if ($action === null)
{
// The owner of the given address 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" => $humanResource->getId(),
"object_bundle" => "HR",
"object_entity" => "HumanResource",
"object_display" => $humanResource->display(),
"object_human_resource_id" => $humanResource !== null ? $humanResource->getId() : null,
"object_human_resource_display" => $humanResource !== null ? $humanResource->display() : null,
"info" => $info,
"society_group" => $humanResource->getSocietyGroup(),
"society" => $humanResource->getSociety(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logIndividual(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Individual
$action = null;
$individual = $this->em->getRepository(Individual::class)->findOneByAddress($address);
if ($individual !== null)
{
$action = "individual_edit_address";
}
else
{
$individual = $this->em->getRepository(Individual::class)->findOneByBillingAddress($address);
if ($individual !== null)
{
$action = "individual_edit_billing_address";
}
}
if ($action === null)
{
// The owner of the given address 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" => $individual->getId(),
"object_bundle" => "Platform",
"object_entity" => "Individual",
"object_display" => $individual->display(),
"object_client_id" => $individual->getClient()->getId(),
"object_client_display" => $individual->getClient()->getName(),
"info" => $info,
"society_group" => $individual->getSociety()->getGroup(),
"society" => $individual->getSociety(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logCommand(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Devis
$action = null;
$command = $this->em->getRepository(Command::class)->findOneByInterventionAddress($address);
if ($command !== null)
{
$action = "command_edit_intervention_address";
}
else
{
$command = $this->em->getRepository(Command::class)->findOneByBillingAddress($address);
if ($command !== null)
{
$action = "command_edit_billing_address";
}
else
{
$command = $this->em->getRepository(Command::class)->findOneByDeliveryAddress($address);
if ($command !== null)
{
$action = "command_edit_delivery_address";
}
}
}
if ($action === null)
{
// The owner of the given address was not identified
return [];
}
// If we are here it means that we have an owner and an action
$receiver = $command->getReceiver();
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $command->getId(),
"object_bundle" => "Platform",
"object_entity" => "Command",
"object_display" => $command->getRef(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"info" => $info,
"society_group" => $command->getSociety()->getGroup(),
"society" => $command->getSociety(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logDevis(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Devis
$action = null;
$devis = $this->em->getRepository(Devis::class)->findOneByInterventionAddress($address);
if ($devis !== null)
{
$action = "devis_edit_intervention_address";
}
else
{
$devis = $this->em->getRepository(Devis::class)->findOneByBillingAddress($address);
if ($devis !== null)
{
$action = "devis_edit_billing_address";
}
}
if ($action === null)
{
// The owner of the given address was not identified
return [];
}
// If we are here it means that we have an owner and an action
$receiver = $devis->getReceiver();
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $devis->getId(),
"object_bundle" => "Platform",
"object_entity" => "Devis",
"object_display" => $devis->display(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"info" => $info,
"society_group" => $devis->getSociety()->getGroup(),
"society" => $devis->getSociety(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logInvoice(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Devis
$action = null;
$invoice = $this->em->getRepository(Invoice::class)->findOneByInterventionAddress($address);
if ($invoice !== null)
{
$action = "invoice_edit";
if ($invoice->isDraft())
{
$action = "invoice_draft_edit";
}
$action .= "_intervention_address";
}
else
{
$invoice = $this->em->getRepository(Invoice::class)->findOneByBillingAddress($address);
if ($invoice !== null)
{
$action = "invoice_edit";
if ($invoice->isDraft())
{
$action = "invoice_draft_edit";
}
$action .= "_billing_address";
}
}
if ($invoice === null)
{
// The owner of the given address was not identified
return [];
}
// If we are here it means that we have an owner and an action
$receiver = $invoice->getReceiver();
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $invoice->getId(),
"object_bundle" => "Platform",
"object_entity" => "Invoice",
"object_display" => $invoice->display(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"info" => $info,
"society_group" => $invoice->getSociety()->getGroup(),
"society" => $invoice->getSociety(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logMission(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Devis
$action = null;
$mission = $this->em->getRepository(Mission::class)->findOneByInterventionAddress($address);
if ($mission !== null)
{
$action = "mission_edit_intervention_address";
}
else
{
$mission = $this->em->getRepository(Mission::class)->findOneByBillingAddress($address);
if ($mission !== null)
{
$action = "mission_edit_billing_address";
}
}
if ($mission === null)
{
// The owner of the given address was not identified
return [];
}
// If we are here it means that we have an owner and an action
$receiver = $mission->getReceiver();
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $mission->getId(),
"object_bundle" => "Mission",
"object_entity" => "Mission",
"object_display" => $mission->display(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"info" => $info,
"society_group" => $mission->getSociety()->getGroup(),
"society" => $mission->getSociety(),
"special_author" => $specialAuthor,
);
// Handle sharing
$societyGroupAuthor = $mission->getSocietyGroupAuthor();
$societyGroupOwner = $mission->getSocietyGroupOwner();
$societyAuthor = $mission->getSociety();
$societyOwner = $mission->getSocietyOwner();
$double = false;
if (!$societyGroupAuthor->equals($societyGroupOwner))
{
$double = true;
$argsBis = $args;
$argsBis["society_group"] = $societyGroupOwner;
$argsBis["society"] = $societyOwner;
}
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
if ($double)
{
$argsBis["old_value"] = $args["old_value"];
$argsBis["new_value"] = $args["new_value"];
$pendingLogArgs[] = $argsBis;
}
}
return $pendingLogArgs;
}
public function logSocietyGroup(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Store
$action = null;
$societyGroup = $this->em->getRepository(SocietyGroup::class)->findOneByAddress($address);
if ($societyGroup !== null)
{
$action = "society_group_edit_address";
}
else
{
$societyGroup = $this->em->getRepository(SocietyGroup::class)->findOneByBillingAddress($address);
if ($societyGroup !== null)
{
$action = "society_group_edit_billing_address";
}
}
if ($action === null)
{
// The owner of the given address 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" => $societyGroup->getId(),
"object_bundle" => "Platform",
"object_entity" => "SocietyGroup",
"object_display" => $societyGroup->display(),
"info" => $info,
"society_group" => $societyGroup,
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logStore(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Store
$action = null;
$store = $this->em->getRepository(Store::class)->findOneByAddress($address);
if ($store !== null)
{
$action = "store_change_default_address";
}
else
{
$store = $this->em->getRepository(Store::class)->findOneByBillingAddress($address);
if ($store !== null)
{
$action = "store_change_default_billing_address";
}
}
if ($action === null)
{
// The owner of the given address 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" => $store->getId(),
"object_bundle" => "Client",
"object_entity" => "Store",
"object_display" => $store->display(),
"info" => $info,
"society_group" => $store->getSocietyGroup(),
"special_author" => $specialAuthor,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logSupplier(Address $address, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($address);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Store
$action = null;
$supplier = $this->em->getRepository(Supplier::class)->findOneByAddress($address);
if ($supplier !== null)
{
$action = "supplier_edit_address";
}
else
{
$supplier = $this->em->getRepository(Supplier::class)->findOneByBillingAddress($address);
if ($supplier !== null)
{
$action = "supplier_edit_billing_address";
}
}
if ($action === null)
{
// The owner of the given address 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,
);
$values = $this->computeAddressChanges($address, $changes);
if ($values !== null)
{
$args["old_value"] = $values["old"];
$args["new_value"] = $values["new"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
// TODO : When all Address logging is done here
// remove this method from AddressTools
public function computeAddressChanges(Address $address, $changesSet)
{
if (count($changesSet) < 1)
return null;
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$old = array();
$new = array();
$changesToWatch = array("address", "complement", "postalCode", "town", "country");
foreach ($changesSet as $key => $change)
{
if (in_array($key, $changesToWatch) && $change[1] != null)
{
$old[$key] = $change[0];
$new[$key] = $change[1];
}
}
$oldValue = "";
$newValue = "";
foreach ($changesToWatch as $key => $property)
{
if (array_key_exists($property, $old))
{
$oldValue .= $old[$property];
$newValue .= $new[$property];
}
else
{
$oldValue .= $propertyAccessor->getValue($address, $property);
$newValue .= $propertyAccessor->getValue($address, $property);
}
if ($key == 2)
{
$oldValue = substr($oldValue, 0, -1);
$newValue = substr($newValue, 0, -1);
$oldValue .= ", ";
$newValue .= ", ";
}
else
{
$oldValue .= " ";
$newValue .= " ";
}
}
// Did Something change ?
if ($oldValue === $newValue)
{
return null;
}
return array(
"old" => $oldValue,
"new" => $newValue,
);
}
}