<?php
//----------------------------------------------------------------------
// src/Logging/CommandLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Logging\Tools;
use App\Services\LogTools;
use App\Entity\Platform\Command\Command;
class CommandLog
{
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(Command $command)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($command);
$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($command, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "command_add";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Command $command, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($command);
$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($command, $loggingData);
//----------------------------------------------------------------------
$basicChanges = array(
"ref",
"info",
"title",
"refInvoice",
"mentionInfo",
);
$labelChanges = array(
"status",
"type",
"tva",
);
// This is triggered when we are actually changing
// the object referenced in Devis :: $interventionAddress
$addressChanges = array(
"deliveryAddress",
"interventionAddress",
"billingAddress",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"society",
);
$dateChanges = array(
"creationDate",
"endDate",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
// Something to skip ?
if (strpos($key, "total") === 0 || strpos($key, "solde") === 0)
{
continue;
}
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = "command_edit_".$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, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
if (in_array($key, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($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;
}
}
if ($command->getProducts()->isDirty())
{
// Make a copy of product changes
$this->saveCommandProducts($command,
$command->getProducts()->getSnapshot(), $command->getProducts());
}
// Plan.io Task #4597
if ($command->getTasks()->isDirty())
{
$old = "";
foreach ($command->getTasks()->getSnapshot() as $task)
{
$old .= $task->getName();
$old .= " || ";
}
$new = "";
foreach ($command->getTasks() as $task)
{
$new .= $task->getName();
$new .= " || ";
}
$args["action"] = "command_edit_tasks";
$args["old_value"] = $old;
$args["new_value"] = $new;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Command $command)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($command);
$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($command, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "command_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Command $command, $loggingData)
{
$receiver = $command->getReceiver();
$society = $command->getSociety();
$societyGroup = $society->getSocietyGroup();
$args = array(
"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,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
public function saveCommandProducts($command, $oldProducts, $newProducts)
{
// [id][ref][title][product/discount][description][refCost][publicPrice][internalPrice][quantity][percentage][unit_id][unit_abbrv][invoiced][invoicedPercentage][totalPublicHT][totalInternalHT][question_id][question][readonlyPrice][samePrice][originalProduct_id][originalProduct_ref]
if ($command === null)
return null;
$today = new \DateTime();
$stamp = $today->format("Y_m");
// ROOT/custom_logs/product_logs/
$logPath = $this->logTools->getProductLogsDir();
if (!file_exists($logPath))
{
if (!mkdir($logPath, 0775, true))
{
// Something went bad
$this->logTools->errorlog('Could not create folder '.$logPath);
return null;
}
}
$logFile = $logPath."command_products_".$stamp.".log";
error_log("--------------------------------------------------\n", 3, $logFile);
error_log("[".$today->format("Y-m-d H:i:s")."] ".$command->getRef()." [".$command->getId()."]\n", 3, $logFile);
$oldNb = count($oldProducts);
error_log("Old Products : ".$oldNb."\n", 3, $logFile);
foreach ($oldProducts as $product)
{
error_log($product->getAsString()."\n", 3, $logFile);
}
$newNb = count($newProducts);
error_log("New Products : ".$newNb."\n", 3, $logFile);
foreach ($newProducts as $product)
{
error_log($product->getAsString()."\n", 3, $logFile);
}
error_log("--------------------------------------------------\n", 3, $logFile);
}
}