<?php
//----------------------------------------------------------------------
// src/Logging/DevisLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Platform\Devis\Devis;
use App\Logging\Tools;
use App\Services\LogTools;
use App\Services\Location\AddressTools;
class DevisLog
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, AddressTools $addressTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->addressTools = $addressTools;
$this->tools = $tools;
}
public function logCreation(Devis $devis)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($devis);
$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($devis, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "devis_add";
if ($devis->isSimulation())
{
$args['action'] = "devis_simulation_add";
}
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Devis $devis, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($devis);
$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($devis, $loggingData);
//----------------------------------------------------------------------
if ($devis->isSimulation())
$editMode = "devis_simulation_edit";
else
$editMode = "devis_edit";
$args["action"] = $editMode;
$basicChanges = array(
"ref",
"title",
"info",
"mentionInfo",
"refOrder",
"ikeaOrderNumber",
"otherCostProvision",
"visibleToClient", // Plan.io Task #4327
"ikeaSent",
"desiredFinish",
"linearMetersLower",
"linearMetersUpper",
"linearMetersColumn",
"linearMetersIsland",
"linearMetersIntegratedAppliance",
);
$labelChanges = array(
"status",
"tva",
);
// This is triggered when we are actually changing
// the object referenced in Devis :: $interventionAddress
$addressChanges = array(
"interventionAddress",
"billingAddress",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"template",
"emitter",
"society",
"manager",
"author",
);
$dateChanges = array(
"creationDate",
"savDeliveryDate",
"changeVisibilityDateMethodCustom", // Plan.io Task #4612
"changeVisibilityDateMethodComplete", // Plan.io Task #4612
);
$specialLoggingData = array(
"devis_auto_edit_status_added_to_task",
"devis_auto_edit_status_removed_from_task"
);
// 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"] = $editMode."_".$name;
$before = $change[0];
$after = $change[1];
if ($key == "signature")
{
// Leave both before and after empty
$pendingLogArgs[] = $args;
continue;
}
if (in_array($key, $basicChanges))
{
$args = $this->tools->handleBasicChanges($args, $before, $after);
if ($key == "ikeaSent")
{
$ikeaAction = "ikea_service_order_devis_sent_to_ikea";
$args["action"] = $ikeaAction;
$pendingLogArgs[] = $args;
$ikeaArgs = $this->tools->handleIkeaServiceOrder($after, $devis, $ikeaAction, $info, $specialAuthor);
foreach ($ikeaArgs as $argsItem)
{
$pendingLogArgs[] = $argsItem;
}
}
else
{
$pendingLogArgs[] = $args;
}
continue;
}
if (in_array($key, $labelChanges))
{
$args = $this->tools->handleLabelChanges($args, $before, $after);
// Handle special cases where the action is a bit different
if ($key == "status" && in_array($info, $specialLoggingData))
{
$args["action"] = $info;
$args["info"] = null;
}
$pendingLogArgs[] = $args;
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 ($devis->getProducts()->isDirty())
{
// Make a copy of product changes
$this->saveDevisProducts($devis,
$devis->getProducts()->getSnapshot(), $devis->getProducts());
}
// See what changed and log (relationships OneToMany and ManyToMany second)
if ($devis->getProducts()->isDirty())
{
$old = "";
foreach ($devis->getProducts()->getSnapshot() as $product)
{
$old .= $product->getRef();
$old .= ", ";
}
if ($old != "")
{
$old = substr($old, 0, -1);
$old = substr($old, 0, -1);
}
$new = "";
foreach ($devis->getProducts() as $product)
{
$new .= $product->getRef();
$new .= ", ";
}
if ($new != "")
{
$new = substr($new, 0, -1);
$new = substr($new, 0, -1);
}
$args["action"] = $editMode."_products";
$args["old_value"] = $old;
$args["new_value"] = $new;
$pendingLogArgs[] = $args;
}
if ($devis->getCharges()->isDirty())
{
// Make a copy of changes
$this->saveDevisCharges($devis,
$devis->getCharges()->getSnapshot(), $devis->getCharges());
}
// See what changed and log (relationships OneToMany and ManyToMany second)
if ($devis->getCharges()->isDirty())
{
$old = "";
foreach ($devis->getCharges()->getSnapshot() as $charge)
{
$old .= $charge->getTotalDuration();
$old .= ", ";
}
if ($old != "")
{
$old = substr($old, 0, -1);
$old = substr($old, 0, -1);
}
$new = "";
foreach ($devis->getCharges() as $charge)
{
$new .= $charge->getTotalDuration();
$new .= ", ";
}
if ($new != "")
{
$new = substr($new, 0, -1);
$new = substr($new, 0, -1);
}
$args["action"] = $editMode."_charges";
$args["old_value"] = $old;
$args["new_value"] = $new;
$pendingLogArgs[] = $args;
}
// Devis :: $interventionAddress and Devis :: $billingAddress
// are handled in AddressLog
// Triggered when the object does not change, but its members do
return $pendingLogArgs;
}
// This only concerns Simulations
public function logRemoval(Devis $devis)
{
if ($devis->isNotSimulation())
{
return [];
}
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($devis);
$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($devis, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "devis_simulation_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Devis $devis, $loggingData)
{
$receiver = $devis->getReceiver();
$society = $devis->getSociety();
$societyGroup = $society->getSocietyGroup();
$mission = $devis->getMission();
$args = array(
"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,
"object_mission_id" => $mission !== null ? $mission->getId() : null,
"object_mission_display" => $mission !== null ? $mission->getRef() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
public function saveDevisProducts($devis, $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 ($devis === 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."devis_products_".$stamp.".log";
error_log("--------------------------------------------------\n", 3, $logFile);
error_log("[".$today->format("Y-m-d H:i:s")."] ".$devis->getRef()." [".$devis->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);
}
public function saveDevisCharges($devis, $oldCharges, $newCharges)
{
if ($devis === null)
return null;
$today = new \DateTime();
$stamp = $today->format("Y_m");
// ROOT/custom_logs/charge_logs/
$logPath = $this->logTools->getChargeLogsDir();
if (!file_exists($logPath))
{
if (!mkdir($logPath, 0775, true))
{
// Something went bad
$this->logTools->errorlog('Could not create folder '.$logPath);
return null;
}
}
$logFile = $logPath."devis_charges_".$stamp.".log";
error_log("--------------------------------------------------\n", 3, $logFile);
error_log("[".$today->format("Y-m-d H:i:s")."] ".$devis->getRef()." [".$devis->getId()."]\n", 3, $logFile);
$oldNb = count($oldCharges);
error_log("Old Charges : ".$oldNb."\n", 3, $logFile);
foreach ($oldCharges as $charge)
{
error_log($charge->getAsString()."\n", 3, $logFile);
}
$newNb = count($newCharges);
error_log("New Charges : ".$newNb."\n", 3, $logFile);
foreach ($newCharges as $charge)
{
error_log($charge->getAsString()."\n", 3, $logFile);
}
error_log("--------------------------------------------------\n", 3, $logFile);
}
}