src/Logging/Activity/CommandLog.php line 19

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/CommandLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Logging\Tools;
  8. use App\Services\LogTools;
  9. use App\Entity\Platform\Command\Command;
  10. class CommandLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.         $this->tools $tools;
  18.     }
  19.     public function logCreation(Command $command)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($command);
  25.         $info $loggingData['info'];
  26.         $specialAuthor $loggingData['special_author'];
  27.         $ignore $loggingData['ignore'];
  28.         if ($ignore) return $pendingLogArgs;
  29.         //----------------------------------------------------------------------
  30.         // Init base log args
  31.         // This does not contain action
  32.         $args $this->initArgs($command$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $args["action"] = "command_add";
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Command $command$changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData $this->logTools->handleLoggingData($command);
  44.         $info $loggingData['info'];
  45.         $specialAuthor $loggingData['special_author'];
  46.         $ignore $loggingData['ignore'];
  47.         if ($ignore) return $pendingLogArgs;
  48.         //----------------------------------------------------------------------
  49.         // Init base log args
  50.         // This does not contain action
  51.         $args $this->initArgs($command$loggingData);
  52.         //----------------------------------------------------------------------
  53.         $basicChanges = array(
  54.             "ref",
  55.             "info",
  56.             "title",
  57.             "refInvoice",
  58.             "mentionInfo",
  59.         );
  60.         $labelChanges = array(
  61.             "status",
  62.             "type",
  63.             "tva",
  64.         );
  65.         // This is triggered when we are actually changing
  66.         // the object referenced in Devis :: $interventionAddress
  67.         $addressChanges = array(
  68.             "deliveryAddress",
  69.             "interventionAddress",
  70.             "billingAddress",
  71.         );
  72.         // Method getLogLabel() should be defined for these objects/entities
  73.         $objectChanges = array(
  74.             "society",
  75.         );
  76.         $dateChanges = array(
  77.             "creationDate",
  78.             "endDate",
  79.         );
  80.         // See what changed and log (members first)
  81.         foreach ($changes as $key => $change)
  82.         {
  83.             // Something to skip ?
  84.             if (strpos($key"total") === || strpos($key"solde") === 0)
  85.             {
  86.                 continue;
  87.             }
  88.             $name $this->logTools->camelToSnakeCase($key);
  89.             $args["action"]    = "command_edit_".$name;
  90.             $before $change[0];
  91.             $after $change[1];
  92.             if (in_array($key$basicChanges))
  93.             {
  94.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  95.                 continue;
  96.             }
  97.             if (in_array($key$labelChanges))
  98.             {
  99.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  100.                 continue;
  101.             }
  102.             if (in_array($key$objectChanges))
  103.             {
  104.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  105.                 continue;
  106.             }
  107.             if (in_array($key$addressChanges))
  108.             {
  109.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  110.                 continue;
  111.             }
  112.             if (in_array($key$dateChanges))
  113.             {
  114.                 // dateChanges can be null
  115.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  116.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  117.                 continue;
  118.             }
  119.         }
  120.         if ($command->getProducts()->isDirty())
  121.         {
  122.             // Make a copy of product changes
  123.             $this->saveCommandProducts($command,
  124.                 $command->getProducts()->getSnapshot(), $command->getProducts());
  125.         }
  126.         // Plan.io Task #4597
  127.         if ($command->getTasks()->isDirty())
  128.         {
  129.             $old "";
  130.             foreach ($command->getTasks()->getSnapshot() as $task)
  131.             {
  132.                 $old .= $task->getName();
  133.                 $old .= " || ";
  134.             }
  135.             $new "";
  136.             foreach ($command->getTasks() as $task)
  137.             {
  138.                 $new .= $task->getName();
  139.                 $new .= " || ";
  140.             }
  141.             $args["action"]        = "command_edit_tasks";
  142.             $args["old_value"]    = $old;
  143.             $args["new_value"]    = $new;
  144.             $pendingLogArgs[] = $args;
  145.         }        
  146.         return $pendingLogArgs;
  147.     }
  148.     public function logRemoval(Command $command)
  149.     {
  150.         $pendingLogArgs = [];
  151.         //----------------------------------------------------------------------
  152.         // Fetch eventual info stored in the object
  153.         $loggingData $this->logTools->handleLoggingData($command);
  154.         $info $loggingData['info'];
  155.         $specialAuthor $loggingData['special_author'];
  156.         $ignore $loggingData['ignore'];
  157.         if ($ignore) return $pendingLogArgs;
  158.         //----------------------------------------------------------------------
  159.         // Init base log args
  160.         // This does not contain action
  161.         $args $this->initArgs($command$loggingData);
  162.         //----------------------------------------------------------------------
  163.         $args["action"] = "command_delete";
  164.         $pendingLogArgs[] = $args;
  165.         return $pendingLogArgs;
  166.     }
  167.     private function initArgs(Command $command$loggingData)
  168.     {
  169.         $receiver $command->getReceiver();
  170.         $society $command->getSociety();
  171.         $societyGroup $society->getSocietyGroup();
  172.         $args = array(
  173.             "object_id"                    =>    $command->getId(),
  174.             "object_bundle"                =>    "Platform",
  175.             "object_entity"                =>    "Command",
  176.             "object_display"            =>    $command->getRef(),
  177.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  178.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  179.             "society_group"                =>    $societyGroup,
  180.             "society"                    =>    $society,
  181.             "info"                        =>    $loggingData['info'],
  182.             "special_author"            =>    $loggingData['special_author'],
  183.         );
  184.         return $args;
  185.     }
  186.     public function saveCommandProducts($command$oldProducts$newProducts)
  187.     {
  188.         // [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]
  189.         if ($command === null)
  190.             return null;
  191.         $today = new \DateTime();
  192.         $stamp $today->format("Y_m");
  193.         // ROOT/custom_logs/product_logs/
  194.         $logPath $this->logTools->getProductLogsDir();
  195.         if (!file_exists($logPath))
  196.         {
  197.             if (!mkdir($logPath0775true))
  198.             {
  199.                 // Something went bad
  200.                 $this->logTools->errorlog('Could not create folder '.$logPath);
  201.                 return null;
  202.             }
  203.         }
  204.         $logFile $logPath."command_products_".$stamp.".log";
  205.         error_log("--------------------------------------------------\n"3$logFile);
  206.         error_log("[".$today->format("Y-m-d H:i:s")."] ".$command->getRef()." [".$command->getId()."]\n"3$logFile);
  207.         $oldNb count($oldProducts);
  208.         error_log("Old Products : ".$oldNb."\n"3$logFile);
  209.         foreach ($oldProducts as $product)
  210.         {
  211.             error_log($product->getAsString()."\n"3$logFile);
  212.         }
  213.         $newNb count($newProducts);
  214.         error_log("New Products : ".$newNb."\n"3$logFile);
  215.         foreach ($newProducts as $product)
  216.         {
  217.             error_log($product->getAsString()."\n"3$logFile);
  218.         }
  219.         error_log("--------------------------------------------------\n"3$logFile);
  220.     }
  221. }