src/Logging/Activity/EInvoicingInboundInvoiceLog.php line 20

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/Activity/EInvoicingInboundInvoiceLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\EInvoicing\EInvoicingInboundInvoice;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. /**
  11.  * Activity log handler for received invoices, electronic and manual alike.
  12.  *
  13.  * Plan.io Task #4683
  14.  */
  15. class EInvoicingInboundInvoiceLog
  16. {
  17.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
  18.     {
  19.         $this->em = $doctrine->getManager();
  20.         $this->logTools = $logTools;
  21.         $this->tools = $tools;
  22.     }
  23.     public function logCreation(EInvoicingInboundInvoice $invoice)
  24.     {
  25.         $pendingLogArgs = [];
  26.         //----------------------------------------------------------------------
  27.         // Fetch eventual info stored in the object
  28.         $loggingData = $this->logTools->handleLoggingData($invoice);
  29.         $ignore = $loggingData['ignore'];
  30.         if ($ignore) return $pendingLogArgs;
  31.         //----------------------------------------------------------------------
  32.         // Init base log args
  33.         // This does not contain action
  34.         $args = $this->initArgs($invoice, $loggingData);
  35.         //----------------------------------------------------------------------
  36.         // A manual invoice is entered by a user, an electronic one is delivered by the PDP
  37.         if ($invoice->isManual())
  38.         {
  39.             $action = "einvoicing_inbound_invoice_add";
  40.         }
  41.         else
  42.         {
  43.             $action = "einvoicing_inbound_invoice_receive";
  44.         }
  45.         $args["action"] = $action;
  46.         $pendingLogArgs[] = $args;
  47.         return $pendingLogArgs;
  48.     }
  49.     public function logChanges(EInvoicingInboundInvoice $invoice, $changes)
  50.     {
  51.         $pendingLogArgs = [];
  52.         //----------------------------------------------------------------------
  53.         // Fetch eventual info stored in the object
  54.         $loggingData = $this->logTools->handleLoggingData($invoice);
  55.         $ignore = $loggingData['ignore'];
  56.         if ($ignore) return $pendingLogArgs;
  57.         //----------------------------------------------------------------------
  58.         // Init base log args
  59.         // This does not contain action
  60.         $args = $this->initArgs($invoice, $loggingData);
  61.         //----------------------------------------------------------------------
  62.         $action = "einvoicing_inbound_invoice_edit";
  63.         $basicChanges = array(
  64.             "invoiceNumber",
  65.             "amountExclTax",
  66.             "amountVat",
  67.             "amountInclTax",
  68.             "accounted",
  69.         );
  70.         $dateChanges = array(
  71.             "invoiceDate",
  72.             "dueDate",
  73.         );
  74.         $labelChanges = array(
  75.             "tva",
  76.             "category",
  77.             // Plan.io Task #4683 : accounting status shared with the expense reports
  78.             "costStatus",
  79.         );
  80.         // Method getLogLabel() should be defined for these objects/entities
  81.         $objectChanges = array(
  82.             "society",
  83.             "supplier",
  84.         );
  85.         // See what changed and log (members first)
  86.         foreach ($changes as $key => $change)
  87.         {
  88.             $name = $this->logTools->camelToSnakeCase($key);
  89.             $args["action"]    = $action."_".$name;
  90.             $before = $change[0];
  91.             $after = $change[1];
  92.             if (in_array($key, $basicChanges))
  93.             {
  94.                 if ($before == $after) continue;
  95.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($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.             if (in_array($key, $labelChanges))
  106.             {
  107.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
  108.                 continue;
  109.             }
  110.             if (in_array($key, $objectChanges))
  111.             {
  112.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
  113.                 continue;
  114.             }
  115.         }
  116.         // Handle ManyToMany
  117.         if ($invoice->getCommands()->isDirty())
  118.         {
  119.             $oldData = "";
  120.             $newData = "";
  121.             foreach ($invoice->getCommands()->getSnapshot() as $command)
  122.             {
  123.                 $oldData .= $command->display();
  124.                 $oldData .= ", ";
  125.             }
  126.             foreach ($invoice->getCommands() as $command)
  127.             {
  128.                 $newData .= $command->display();
  129.                 $newData .= ", ";
  130.             }
  131.             if ($oldData != "")
  132.             {
  133.                 $oldData = substr($oldData, 0, -2);
  134.             }
  135.             if ($newData != "")
  136.             {
  137.                 $newData = substr($newData, 0, -2);
  138.             }
  139.             $args["action"] = "einvoicing_inbound_invoice_edit_commands";
  140.             $args["old_value"] = $oldData;
  141.             $args["new_value"] = $newData;
  142.             $pendingLogArgs[] = $args;
  143.         }
  144.         return $pendingLogArgs;
  145.     }
  146.     public function logRemoval(EInvoicingInboundInvoice $invoice)
  147.     {
  148.         $pendingLogArgs = [];
  149.         //----------------------------------------------------------------------
  150.         // Fetch eventual info stored in the object
  151.         $loggingData = $this->logTools->handleLoggingData($invoice);
  152.         $ignore = $loggingData['ignore'];
  153.         if ($ignore) return $pendingLogArgs;
  154.         //----------------------------------------------------------------------
  155.         // Init base log args
  156.         // This does not contain action
  157.         $args = $this->initArgs($invoice, $loggingData);
  158.         //----------------------------------------------------------------------
  159.         $args["action"] = "einvoicing_inbound_invoice_delete";
  160.         $pendingLogArgs[] = $args;
  161.         return $pendingLogArgs;
  162.     }
  163.     private function initArgs(EInvoicingInboundInvoice $invoice, $loggingData)
  164.     {
  165.         $society = $invoice->getSociety();
  166.         $societyGroup = $invoice->getSocietyGroup();
  167.         $args = array(
  168.             "object_id"                    =>    $invoice->getId(),
  169.             "object_bundle"                =>    "EInvoicing",
  170.             "object_entity"                =>    "EInvoicingInboundInvoice",
  171.             "object_display"            =>    $invoice->display(),
  172.             "society_group"                =>    $societyGroup,
  173.             "society"                    =>    $society,
  174.             "info"                        =>    $loggingData['info'],
  175.             "special_author"            =>    $loggingData['special_author'],
  176.         );
  177.         return $args;
  178.     }
  179. }