src/Logging/Activity/InstallmentLog.php line 22

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/InstallmentLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Entity\Platform\Installment;
  9. use App\Logging\DeletionContextLogger;
  10. use App\Logging\Tools;
  11. use App\Services\LogTools;
  12. class InstallmentLog
  13. {
  14.     // private array $pendingLogArgs = [];
  15.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger, Tools $tools)
  16.     {
  17.         $this->em = $doctrine->getManager();
  18.         $this->logTools = $logTools;
  19.         $this->contextLogger = $contextLogger;
  20.         $this->tools = $tools;
  21.     }
  22.     public function logCreation(Installment $installment)
  23.     {
  24.         $pendingLogArgs = [];
  25.         //----------------------------------------------------------------------
  26.         // Fetch eventual info stored in the object
  27.         $loggingData = $this->logTools->handleLoggingData($installment);
  28.         $info = $loggingData['info'];
  29.         $specialAuthor = $loggingData['special_author'];
  30.         $ignore = $loggingData['ignore'];
  31.         if ($ignore) return $pendingLogArgs;
  32.         //----------------------------------------------------------------------
  33.         if ($installment->getDevis() !== null)
  34.         {
  35.             $module = "devis";
  36.             $action = "devis_edit_installment";
  37.             $object = $installment->getDevis();
  38.             $objectClient = $installment->getDevis()->getReceiver();
  39.             $objectMission = $installment->getDevis()->getMission();
  40.             $objectEntity = "Devis";
  41.         }
  42.         else
  43.         {
  44.             // Plan.io Task #4326 : Exclude Drafts
  45.             if ($installment->getInvoice() !== null)
  46.             {
  47.                 if ($installment->getInvoice()->isDraft())
  48.                 {
  49.                     // Nothing to do here
  50.                     return $pendingLogArgs;
  51.                 }
  52.                 $module = "invoice";
  53.                 $action = "invoice_edit_installment";
  54.                 $object = $installment->getInvoice();
  55.                 $objectClient = $installment->getInvoice()->getReceiver();
  56.                 $objectMission = $installment->getInvoice()->getMission();
  57.                 $objectEntity = "Invoice";
  58.             }
  59.             else
  60.             {
  61.                 if ($installment->getCommand() !== null)
  62.                 {
  63.                     $module = "command";
  64.                     $action = "command_edit_installment";
  65.                     $object = $installment->getCommand();
  66.                     $objectClient = $installment->getCommand()->getReceiver();
  67.                     $objectMission = null;
  68.                     $objectEntity = "Command";
  69.                 }
  70.                 else
  71.                 {
  72.                     // Nothing to do here
  73.                     return $pendingLogArgs;
  74.                 }
  75.             }
  76.         }
  77.         // Fill the common arguments
  78.         $args = array(
  79.             "action"                    =>    $action,
  80.             "object_id"                    =>    $object->getId(),
  81.             "object_bundle"                =>    "Platform",
  82.             "object_entity"                =>    $objectEntity,
  83.             "object_display"            =>    $object->getRef(),
  84.             "object_client_id"            =>    $objectClient !== null ? $objectClient->getId() : null,
  85.             "object_client_display"        =>    $objectClient !== null ? $objectClient->getName() : null,
  86.             "object_mission_id"            =>    $objectMission !== null ? $objectMission->getId() : null,
  87.             "object_mission_display"    =>    $objectMission !== null ? $objectMission->getRef() : null,
  88.             "info"                        =>    $info,
  89.             "society_group"                =>    $object->getSociety()->getGroup(),
  90.             "society"                    =>    $object->getSociety(),
  91.             "special_author"            =>    $specialAuthor,
  92.         );
  93.         $args["action"]        = $action."_add";
  94.         $args["new_value"]    = $installment->getValue();
  95.         $pendingLogArgs[] = $args;
  96.         return $pendingLogArgs;
  97.     }
  98.     public function logChanges(Installment $installment, $changes)
  99.     {
  100.         $pendingLogArgs = [];
  101.         //----------------------------------------------------------------------
  102.         // Fetch eventual info stored in the object
  103.         $loggingData = $this->logTools->handleLoggingData($installment);
  104.         $info = $loggingData['info'];
  105.         $specialAuthor = $loggingData['special_author'];
  106.         $ignore = $loggingData['ignore'];
  107.         if ($ignore) return $pendingLogArgs;
  108.         //----------------------------------------------------------------------
  109.         if ($installment->getDevis() !== null)
  110.         {
  111.             $module = "devis";
  112.             $action = "devis_edit_installment";
  113.             $object = $installment->getDevis();
  114.             $objectClient = $installment->getDevis()->getReceiver();
  115.             $objectMission = $installment->getDevis()->getMission();
  116.             $objectEntity = "Devis";
  117.         }
  118.         else
  119.         {
  120.             if ($installment->getInvoice() !== null)
  121.             {
  122.                 if ($installment->getInvoice()->isDraft())
  123.                 {
  124.                     // Nothing to do here
  125.                     return $pendingLogArgs;
  126.                 }
  127.                 $module = "invoice";
  128.                 $action = "invoice_edit_installment";
  129.                 $object = $installment->getInvoice();
  130.                 $objectClient = $installment->getInvoice()->getReceiver();
  131.                 $objectMission = $installment->getInvoice()->getMission();
  132.                 $objectEntity = "Invoice";
  133.             }
  134.             else
  135.             {
  136.                 if ($installment->getCommand() !== null)
  137.                 {
  138.                     $module = "command";
  139.                     $action = "command_edit_installment";
  140.                     $object = $installment->getCommand();
  141.                     $objectClient = $installment->getCommand()->getReceiver();
  142.                     $objectMission = null;
  143.                     $objectEntity = "Command";
  144.                 }
  145.                 else
  146.                 {
  147.                     // Nothing to do here
  148.                     return $pendingLogArgs;
  149.                 }
  150.             }
  151.         }
  152.         // Fill the common arguments
  153.         $args = array(
  154.             "action"                    =>    $action,
  155.             "object_id"                    =>    $object->getId(),
  156.             "object_bundle"                =>    "Platform",
  157.             "object_entity"                =>    $objectEntity,
  158.             "object_display"            =>    $object->getRef(),
  159.             "object_client_id"            =>    $objectClient !== null ? $objectClient->getId() : null,
  160.             "object_client_display"        =>    $objectClient !== null ? $objectClient->getName() : null,
  161.             "object_mission_id"            =>    $objectMission !== null ? $objectMission->getId() : null,
  162.             "object_mission_display"    =>    $objectMission !== null ? $objectMission->getRef() : null,
  163.             "info"                        =>    $info,
  164.             "society_group"                =>    $object->getSociety()->getGroup(),
  165.             "society"                    =>    $object->getSociety(),
  166.             "special_author"            =>    $specialAuthor,
  167.         );
  168.         $basicChanges = array(
  169.             "value",
  170.             "info",
  171.             // Plan.io Task #4685
  172.             "paymentFee",
  173.         );
  174.         $labelChanges = array(
  175.             "status",
  176.             "paymentType",
  177.         );
  178.         $dateChanges = array(
  179.             "paymentDate",
  180.             "deadlineDate",
  181.         );
  182.         // See what changed and log (members first)
  183.         foreach ($changes as $key => $change)
  184.         {
  185.             $name = $this->logTools->camelToSnakeCase($key);
  186.             $args["action"]    = $action."_".$name;
  187.             $before = $change[0];
  188.             $after = $change[1];
  189.             if (in_array($key, $basicChanges))
  190.             {
  191.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  192.                 continue;
  193.             }
  194.             if (in_array($key, $labelChanges))
  195.             {
  196.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
  197.                 continue;
  198.             }
  199.             if (in_array($key, $dateChanges))
  200.             {
  201.                 // dateChanges can be null
  202.                 $changeLogs = $this->tools->handleDateChanges($args, $before, $after);
  203.                 if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
  204.                 continue;
  205.             }
  206.         }
  207.         return $pendingLogArgs;
  208.     }
  209.     public function logRemoval(Installment $installment)
  210.     {
  211.         $pendingLogArgs = [];
  212.         //----------------------------------------------------------------------
  213.         // Fetch eventual info stored in the object
  214.         $loggingData = $this->logTools->handleLoggingData($installment);
  215.         $info = $loggingData['info'];
  216.         $specialAuthor = $loggingData['special_author'];
  217.         $ignore = $loggingData['ignore'];
  218.         if ($ignore) return $pendingLogArgs;
  219.         //----------------------------------------------------------------------
  220.         // So this is kinda interesting
  221.         // I don't want to log the removal of installments when validation of a draft
  222.         // But when a draft is validated and its installments deleted
  223.         // no data is saved to the context for those drafts
  224.         // So this method will simply fail grafully :)
  225.         $cachedData = $this->contextLogger->getContext($installment);
  226.         if ($cachedData === null || empty($cachedData))
  227.         {
  228.             // Nothing to do here
  229.             return $pendingLogArgs;
  230.         }
  231.         if (array_key_exists('devis', $cachedData) && $cachedData['devis'] !== null)
  232.         {
  233.             $module = "devis";
  234.             $action = "devis_edit_installment";
  235.             $object = $cachedData['devis'];
  236.             $objectClient = $object->getReceiver();
  237.             $objectMission = $object->getMission();
  238.             $objectEntity = "Devis";
  239.         }
  240.         else
  241.         {
  242.             if (array_key_exists('invoice', $cachedData) && $cachedData['invoice'] !== null)
  243.             {
  244.                 $module = "invoice";
  245.                 $action = "invoice_edit_installment";
  246.                 $object = $cachedData['invoice'];
  247.                 $objectClient = $cachedData['invoice']->getReceiver();
  248.                 $objectMission = $object->getMission();
  249.                 $objectEntity = "Invoice";
  250.             }
  251.             else
  252.             {
  253.                 if (array_key_exists('command', $cachedData) && $cachedData['command'] !== null)
  254.                 {
  255.                     $module = "command";
  256.                     $action = "command_edit_installment";
  257.                     $object = $cachedData['command'];
  258.                     $objectClient = $object->getReceiver();
  259.                     $objectMission = null;
  260.                     $objectEntity = "Command";
  261.                 }
  262.                 else
  263.                 {
  264.                     // Nothing to do here
  265.                     return $pendingLogArgs;
  266.                 }
  267.             }
  268.         }
  269.         // Fill the common arguments
  270.         $args = array(
  271.             "action"                    =>    $action,
  272.             "object_id"                    =>    $object->getId(),
  273.             "object_bundle"                =>    "Platform",
  274.             "object_entity"                =>    $objectEntity,
  275.             "object_display"            =>    $object->getRef(),
  276.             "object_client_id"            =>    $objectClient !== null ? $objectClient->getId() : null,
  277.             "object_client_display"        =>    $objectClient !== null ? $objectClient->getName() : null,
  278.             "object_mission_id"            =>    $objectMission !== null ? $objectMission->getId() : null,
  279.             "object_mission_display"    =>    $objectMission !== null ? $objectMission->getRef() : null,
  280.             "info"                        =>    $info,
  281.             "society_group"                =>    $object->getSociety()->getGroup(),
  282.             "society"                    =>    $object->getSociety(),
  283.             "special_author"            =>    $specialAuthor,
  284.         );
  285.         $args["action"]        = $action."_delete";
  286.         $args["old_value"]    = $installment->getValue();
  287.         $pendingLogArgs[] = $args;
  288.         return $pendingLogArgs;
  289.     }
  290. }