src/AppRemix/Logging/Objects/DevisLog.php line 20

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/DevisLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Devis;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class DevisLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
  14.     {
  15.         $this->em = $doctrine->getManager();
  16.         $this->logTools = $logTools;
  17.         $this->tools = $tools;
  18.     }
  19.     public function logCreation(Devis $devis)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($devis);
  25.         $info = $loggingData['info'];        
  26.         $ignore = $loggingData['ignore'];
  27.         if ($ignore) return $pendingLogArgs;
  28.         //----------------------------------------------------------------------
  29.         // Init base log args
  30.         // This does not contain action
  31.         $args = $this->initArgs($devis, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "devis_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Devis $devis, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($devis);
  44.         $info = $loggingData['info'];        
  45.         $ignore = $loggingData['ignore'];
  46.         if ($ignore) return $pendingLogArgs;
  47.         //----------------------------------------------------------------------
  48.         // Init base log args
  49.         // This does not contain action
  50.         $args = $this->initArgs($devis, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $editMode = "devis_edit";
  53.         $args["action"] = $editMode;
  54.         $basicChanges = array(            
  55.             "tva",
  56.         );
  57.         $jsonChanges = array(
  58.             "interventionAddress",
  59.         );
  60.         // Method getLogLabel() should be defined for these objects/entities.
  61.         // A late attach logs devis_edit_client with the client's label.
  62.         $objectChanges = array(
  63.             "client",
  64.         );
  65.         $dateChanges = array(            
  66.             "sentToClient",
  67.         );
  68.         // See what changed and log (members first)
  69.         foreach ($changes as $key => $change)
  70.         {
  71.             // Something to skip ?
  72.             if (strpos($key, "total") === 0)
  73.             {
  74.                 continue;
  75.             }
  76.             $name = $this->logTools->camelToSnakeCase($key);
  77.             $args["action"]    = $editMode."_".$name;
  78.             $before = $change[0];
  79.             $after = $change[1];
  80.             if (in_array($key, $basicChanges))
  81.             {
  82.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  83.                 continue;
  84.             }
  85.             if (in_array($key, $objectChanges))
  86.             {
  87.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
  88.                 continue;
  89.             }
  90.             if (in_array($key, $jsonChanges))
  91.             {
  92.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  93.                 continue;
  94.             }
  95.             if (in_array($key, $dateChanges))
  96.             {
  97.                 // dateChanges can be null
  98.                 $changeLogs = $this->tools->handleDateChanges($args, $before, $after);
  99.                 if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
  100.                 continue;
  101.             }
  102.         }
  103.         // See what changed and log (relationships OneToMany and ManyToMany second)
  104.         if ($devis->getItems()->isDirty())
  105.         {
  106.             $old = "";
  107.             foreach ($devis->getItems()->getSnapshot() as $item)
  108.             {
  109.                 $old .= $item->displayFull();
  110.                 $old .= " | ";
  111.             }
  112.             if ($old != "")
  113.             {
  114.                 $old = substr($old, 0, -1);
  115.                 $old = substr($old, 0, -1);
  116.                 $old = substr($old, 0, -1);
  117.             }
  118.             $new = "";
  119.             foreach ($devis->getItems() as $item)
  120.             {
  121.                 $new .= $item->displayFull();
  122.                 $new .= " | ";
  123.             }
  124.             if ($new != "")
  125.             {
  126.                 $new = substr($new, 0, -1);
  127.                 $new = substr($new, 0, -1);
  128.                 $new = substr($new, 0, -1);
  129.             }
  130.             $args["action"]        = $editMode."_items";
  131.             $args["old_value"]    = $old;
  132.             $args["new_value"]    = $new;
  133.             $pendingLogArgs[] = $args;
  134.         }
  135.         // The financial state is derived, so it has no changeset entry of its own
  136.         $stateLogs = $this->handleFinancialState($devis, $changes, $args, $editMode);
  137.         if ($stateLogs !== null) $pendingLogArgs[] = $stateLogs;
  138.         return $pendingLogArgs;
  139.     }
  140.     public function logRemoval(Devis $devis)
  141.     {
  142.         $pendingLogArgs = [];
  143.         return $pendingLogArgs;
  144.     }
  145.     /**
  146.      * Logs the devis's financial state whenever a flush moves it.
  147.      *
  148.      * Devis::getFinancialState() is computed and never persisted, so Doctrine reports no change for
  149.      * it. It reads five inputs — annulled, whether a client is set, totalTTC, totalRequested and
  150.      * totalPaid — and every money write path (devis/add/payment, devis/add/invoice, invoice/annul,
  151.      * payment/confirm, payment/unconfirm) updates the money columns in the same flush as the
  152.      * Payment it belongs to. A late client attach is in the same changeset, so the draft to
  153.      * not_invoiced transition is logged here too. Both ends of the transition are
  154.      * available: the changeset for what moved, the entity itself for what did not.
  155.      *
  156.      * Returns null when the state did not change.
  157.      */
  158.     private function handleFinancialState(Devis $devis, $changes, $args, $editMode)
  159.     {
  160.         $before = $devis->resolveFinancialState(
  161.             (bool) $this->valueBefore($changes, "annulled", $devis->isAnnulled()),
  162.             $this->valueBefore($changes, "client", $devis->getClient()) !== null,
  163.             $this->valueBefore($changes, "totalTTC", $devis->getTotalTTC()),
  164.             $this->valueBefore($changes, "totalRequested", $devis->getTotalRequested()),
  165.             $this->valueBefore($changes, "totalPaid", $devis->getTotalPaid())
  166.         );
  167.         $after = $devis->getFinancialState();
  168.         if ($before === $after)
  169.         {
  170.             return null;
  171.         }
  172.         $args["action"] = $editMode."_financial_state";
  173.         return $this->tools->handleFinancialStateChanges($args, $before, $after);
  174.     }
  175.     /**
  176.      * The value a member held before this flush: the changeset when Doctrine saw it move,
  177.      * the current value otherwise.
  178.      */
  179.     private function valueBefore($changes, $key, $current)
  180.     {
  181.         if (array_key_exists($key, $changes))
  182.         {
  183.             return $changes[$key][0];
  184.         }
  185.         return $current;
  186.     }
  187.     private function initArgs(Devis $devis, $loggingData)
  188.     {
  189.         $client = $devis->getClient();
  190.         $args = array(
  191.             "object_id"                    =>    $devis->getId(),            
  192.             "object_entity"                =>    "Devis",
  193.             "object_display"            =>    $devis->display(),
  194.             
  195.             "society"                    =>    $devis->getSociety(),
  196.             // Null for a client created in this flush — handleClient() fills it post-flush.
  197.             "client_id"                    =>    $client?->getId(),
  198.             "client_display"            =>    $client?->displayForListing(),
  199.             "info"                        =>    $loggingData['info'],            
  200.         );
  201.         return $args;
  202.     }
  203. }