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

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/InvoiceLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Invoice;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class InvoiceLog
  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(Invoice $invoice)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($invoice);
  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($invoice, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "invoice_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Invoice $invoice, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($invoice);
  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($invoice, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $editMode = "invoice_edit";
  53.         $args["action"] = $editMode;
  54.         $basicChanges = array(            
  55.             "tva",
  56.             "type",
  57.             "annulled",
  58.         );
  59.         $jsonChanges = array(
  60.             "billingAddress",
  61.         );
  62.         // Method getLogLabel() should be defined for these objects/entities.
  63.         // A late attach logs invoice_edit_client with the client's label.
  64.         $objectChanges = array(
  65.             "creditInvoice",
  66.             "client",
  67.         );
  68.         $dateChanges = array(            
  69.             "sentToClient",
  70.         );
  71.         // See what changed and log (members first)
  72.         foreach ($changes as $key => $change)
  73.         {
  74.             // Something to skip ?
  75.             if (strpos($key, "total") === 0)
  76.             {
  77.                 continue;
  78.             }
  79.             $name = $this->logTools->camelToSnakeCase($key);
  80.             $args["action"]    = $editMode."_".$name;
  81.             $before = $change[0];
  82.             $after = $change[1];
  83.             if (in_array($key, $basicChanges))
  84.             {
  85.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  86.                 continue;
  87.             }
  88.             if (in_array($key, $objectChanges))
  89.             {
  90.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
  91.                 continue;
  92.             }
  93.             if (in_array($key, $jsonChanges))
  94.             {
  95.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  96.                 continue;
  97.             }
  98.             if (in_array($key, $dateChanges))
  99.             {
  100.                 // dateChanges can be null
  101.                 $changeLogs = $this->tools->handleDateChanges($args, $before, $after);
  102.                 if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
  103.                 continue;
  104.             }
  105.         }
  106.         // See what changed and log (relationships OneToMany and ManyToMany second)
  107.         if ($invoice->getItems()->isDirty())
  108.         {
  109.             $old = "";
  110.             foreach ($invoice->getItems()->getSnapshot() as $item)
  111.             {
  112.                 $old .= $item->displayFull();
  113.                 $old .= " | ";
  114.             }
  115.             if ($old != "")
  116.             {
  117.                 $old = substr($old, 0, -1);
  118.                 $old = substr($old, 0, -1);
  119.                 $old = substr($old, 0, -1);
  120.             }
  121.             $new = "";
  122.             foreach ($invoice->getItems() as $item)
  123.             {
  124.                 $new .= $item->displayFull();
  125.                 $new .= " | ";
  126.             }
  127.             if ($new != "")
  128.             {
  129.                 $new = substr($new, 0, -1);
  130.                 $new = substr($new, 0, -1);
  131.                 $new = substr($new, 0, -1);
  132.             }
  133.             $args["action"]        = $editMode."_items";
  134.             $args["old_value"]    = $old;
  135.             $args["new_value"]    = $new;
  136.             $pendingLogArgs[] = $args;
  137.         }
  138.         return $pendingLogArgs;
  139.     }
  140.     public function logRemoval(Invoice $invoice)
  141.     {
  142.         $pendingLogArgs = [];
  143.         return $pendingLogArgs;
  144.     }
  145.     private function initArgs(Invoice $invoice, $loggingData)
  146.     {
  147.         $client = $invoice->getClient();
  148.         $args = array(
  149.             "object_id"                    =>    $invoice->getId(),            
  150.             "object_entity"                =>    "Invoice",
  151.             "object_display"            =>    $invoice->display(),
  152.             
  153.             "society"                    =>    $invoice->getSociety(),
  154.             // Null for a client created in this flush — handleClient() fills it post-flush.
  155.             "client_id"                    =>    $client?->getId(),
  156.             "client_display"            =>    $client?->displayForListing(),
  157.             "info"                        =>    $loggingData['info'],            
  158.         );
  159.         return $args;
  160.     }
  161. }