src/Logging/Activity/DevisLog.php line 20

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/DevisLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Platform\Devis\Devis;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. use App\Services\Location\AddressTools;
  11. class DevisLog
  12. {
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsAddressTools $addressToolsTools $tools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.         $this->addressTools $addressTools;
  18.         $this->tools $tools;
  19.     }
  20.     public function logCreation(Devis $devis)
  21.     {
  22.         $pendingLogArgs = [];
  23.         //----------------------------------------------------------------------
  24.         // Fetch eventual info stored in the object
  25.         $loggingData $this->logTools->handleLoggingData($devis);
  26.         $info $loggingData['info'];
  27.         $specialAuthor $loggingData['special_author'];
  28.         $ignore $loggingData['ignore'];
  29.         if ($ignore) return $pendingLogArgs;
  30.         //----------------------------------------------------------------------
  31.         // Init base log args
  32.         // This does not contain action
  33.         $args $this->initArgs($devis$loggingData);
  34.         //----------------------------------------------------------------------
  35.         $args["action"] = "devis_add";
  36.         if ($devis->isSimulation())
  37.         {
  38.             $args['action'] = "devis_simulation_add";
  39.         }
  40.         $pendingLogArgs[] = $args;
  41.         return $pendingLogArgs;
  42.     }
  43.     public function logChanges(Devis $devis$changes)
  44.     {
  45.         $pendingLogArgs = [];
  46.         //----------------------------------------------------------------------
  47.         // Fetch eventual info stored in the object
  48.         $loggingData $this->logTools->handleLoggingData($devis);
  49.         $info $loggingData['info'];
  50.         $specialAuthor $loggingData['special_author'];
  51.         $ignore $loggingData['ignore'];
  52.         if ($ignore) return $pendingLogArgs;
  53.         //----------------------------------------------------------------------
  54.         // Init base log args
  55.         // This does not contain action
  56.         $args $this->initArgs($devis$loggingData);
  57.         //----------------------------------------------------------------------
  58.         if ($devis->isSimulation())
  59.             $editMode "devis_simulation_edit";
  60.         else
  61.             $editMode "devis_edit";
  62.         $args["action"] = $editMode;
  63.         $basicChanges = array(
  64.             "ref",
  65.             "title",
  66.             "info",
  67.             "mentionInfo",
  68.             "refOrder",
  69.             "ikeaOrderNumber",
  70.             "otherCostProvision",
  71.             "visibleToClient",            // Plan.io Task #4327
  72.             "ikeaSent",
  73.             "desiredFinish",
  74.             "linearMetersLower",
  75.             "linearMetersUpper",
  76.             "linearMetersColumn",
  77.             "linearMetersIsland",
  78.             "linearMetersIntegratedAppliance",
  79.         );
  80.         $labelChanges = array(
  81.             "status",
  82.             "tva",
  83.         );
  84.         // This is triggered when we are actually changing
  85.         // the object referenced in Devis :: $interventionAddress
  86.         $addressChanges = array(
  87.             "interventionAddress",
  88.             "billingAddress",
  89.         );
  90.         // Method getLogLabel() should be defined for these objects/entities
  91.         $objectChanges = array(
  92.             "template",
  93.             "emitter",
  94.             "society",
  95.             "manager",
  96.             "author",
  97.         );
  98.         $dateChanges = array(
  99.             "creationDate",
  100.             "savDeliveryDate",
  101.             "changeVisibilityDateMethodCustom",            // Plan.io Task #4612
  102.             "changeVisibilityDateMethodComplete",            // Plan.io Task #4612
  103.         );
  104.         $specialLoggingData = array(
  105.             "devis_auto_edit_status_added_to_task",
  106.             "devis_auto_edit_status_removed_from_task"
  107.         );
  108.         // See what changed and log (members first)
  109.         foreach ($changes as $key => $change)
  110.         {
  111.             // Something to skip ?
  112.             if (strpos($key"total") === || strpos($key"solde") === 0)
  113.             {
  114.                 continue;
  115.             }
  116.             $name $this->logTools->camelToSnakeCase($key);
  117.             $args["action"]    = $editMode."_".$name;
  118.             $before $change[0];
  119.             $after $change[1];
  120.             if ($key == "signature")
  121.             {
  122.                 // Leave both before and after empty
  123.                 $pendingLogArgs[] = $args;
  124.                 continue;
  125.             }
  126.             if (in_array($key$basicChanges))
  127.             {
  128.                 $args $this->tools->handleBasicChanges($args$before$after);
  129.                 if ($key == "ikeaSent")
  130.                 {
  131.                     $ikeaAction "ikea_service_order_devis_sent_to_ikea";
  132.                     $args["action"] = $ikeaAction;                    
  133.                     $pendingLogArgs[] = $args;
  134.                     $ikeaArgs $this->tools->handleIkeaServiceOrder($after$devis$ikeaAction$info$specialAuthor);
  135.                     foreach ($ikeaArgs as $argsItem)
  136.                     {
  137.                         $pendingLogArgs[] = $argsItem;
  138.                     }
  139.                 }
  140.                 else
  141.                 {
  142.                     $pendingLogArgs[] = $args;
  143.                 }
  144.                 continue;
  145.             }
  146.             if (in_array($key$labelChanges))
  147.             {
  148.                 $args $this->tools->handleLabelChanges($args$before$after);
  149.                 // Handle special cases where the action is a bit different
  150.                 if ($key == "status" && in_array($info$specialLoggingData))
  151.                 {
  152.                     $args["action"] = $info;
  153.                     $args["info"] = null;
  154.                 }
  155.                 $pendingLogArgs[] = $args;
  156.                 continue;
  157.             }
  158.             if (in_array($key$objectChanges))
  159.             {
  160.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  161.                 continue;
  162.             }
  163.             if (in_array($key$addressChanges))
  164.             {
  165.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  166.                 continue;
  167.             }
  168.             if (in_array($key$dateChanges))
  169.             {
  170.                 // dateChanges can be null
  171.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  172.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  173.                 continue;
  174.             }
  175.         }
  176.         if ($devis->getProducts()->isDirty())
  177.         {
  178.             // Make a copy of product changes
  179.             $this->saveDevisProducts($devis,
  180.                 $devis->getProducts()->getSnapshot(), $devis->getProducts());
  181.         }
  182.         // See what changed and log (relationships OneToMany and ManyToMany second)
  183.         if ($devis->getProducts()->isDirty())
  184.         {
  185.             $old "";
  186.             foreach ($devis->getProducts()->getSnapshot() as $product)
  187.             {
  188.                 $old .= $product->getRef();
  189.                 $old .= ", ";
  190.             }
  191.             if ($old != "")
  192.             {
  193.                 $old substr($old0, -1);
  194.                 $old substr($old0, -1);
  195.             }
  196.             $new "";
  197.             foreach ($devis->getProducts() as $product)
  198.             {
  199.                 $new .= $product->getRef();
  200.                 $new .= ", ";
  201.             }
  202.             if ($new != "")
  203.             {
  204.                 $new substr($new0, -1);
  205.                 $new substr($new0, -1);
  206.             }
  207.             $args["action"]        = $editMode."_products";
  208.             $args["old_value"]    = $old;
  209.             $args["new_value"]    = $new;
  210.             $pendingLogArgs[] = $args;
  211.         }
  212.         if ($devis->getCharges()->isDirty())
  213.         {
  214.             // Make a copy of changes
  215.             $this->saveDevisCharges($devis,
  216.                 $devis->getCharges()->getSnapshot(), $devis->getCharges());
  217.         }
  218.         // See what changed and log (relationships OneToMany and ManyToMany second)
  219.         if ($devis->getCharges()->isDirty())
  220.         {
  221.             $old "";
  222.             foreach ($devis->getCharges()->getSnapshot() as $charge)
  223.             {
  224.                 $old .= $charge->getTotalDuration();
  225.                 $old .= ", ";
  226.             }
  227.             if ($old != "")
  228.             {
  229.                 $old substr($old0, -1);
  230.                 $old substr($old0, -1);
  231.             }
  232.             $new "";
  233.             foreach ($devis->getCharges() as $charge)
  234.             {
  235.                 $new .= $charge->getTotalDuration();
  236.                 $new .= ", ";
  237.             }
  238.             if ($new != "")
  239.             {
  240.                 $new substr($new0, -1);
  241.                 $new substr($new0, -1);
  242.             }
  243.             $args["action"]        = $editMode."_charges";
  244.             $args["old_value"]    = $old;
  245.             $args["new_value"]    = $new;
  246.             $pendingLogArgs[] = $args;
  247.         }
  248.         // Devis :: $interventionAddress and Devis :: $billingAddress
  249.         // are handled in AddressLog
  250.         // Triggered when the object does not change, but its members do
  251.         return $pendingLogArgs;
  252.     }
  253.     // This only concerns Simulations
  254.     public function logRemoval(Devis $devis)
  255.     {
  256.         if ($devis->isNotSimulation())
  257.         {
  258.             return [];
  259.         }
  260.         $pendingLogArgs = [];
  261.         //----------------------------------------------------------------------
  262.         // Fetch eventual info stored in the object
  263.         $loggingData $this->logTools->handleLoggingData($devis);
  264.         $info $loggingData['info'];
  265.         $specialAuthor $loggingData['special_author'];
  266.         $ignore $loggingData['ignore'];
  267.         if ($ignore) return $pendingLogArgs;
  268.         //----------------------------------------------------------------------
  269.         // Init base log args
  270.         // This does not contain action
  271.         $args $this->initArgs($devis$loggingData);
  272.         //----------------------------------------------------------------------
  273.         $args["action"] = "devis_simulation_delete";
  274.         $pendingLogArgs[] = $args;
  275.         return $pendingLogArgs;
  276.     }
  277.     
  278.     private function initArgs(Devis $devis$loggingData)
  279.     {
  280.         $receiver $devis->getReceiver();
  281.         $society $devis->getSociety();
  282.         $societyGroup $society->getSocietyGroup();
  283.         $mission $devis->getMission();
  284.         $args = array(
  285.             "object_id"                    =>    $devis->getId(),
  286.             "object_bundle"                =>    "Platform",
  287.             "object_entity"                =>    "Devis",
  288.             "object_display"            =>    $devis->display(),
  289.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  290.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  291.             "object_mission_id"            =>    $mission !== null $mission->getId() : null,
  292.             "object_mission_display"    =>    $mission !== null $mission->getRef() : null,
  293.             "society_group"                =>    $societyGroup,
  294.             "society"                    =>    $society,
  295.             "info"                        =>    $loggingData['info'],
  296.             "special_author"            =>    $loggingData['special_author'],
  297.         );
  298.         return $args;
  299.     }
  300.     public function saveDevisProducts($devis$oldProducts$newProducts)
  301.     {
  302.         // [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]
  303.         if ($devis === null)
  304.             return null;
  305.         $today = new \DateTime();
  306.         $stamp $today->format("Y_m");
  307.         // ROOT/custom_logs/product_logs/
  308.         $logPath $this->logTools->getProductLogsDir();
  309.         if (!file_exists($logPath))
  310.         {
  311.             if (!mkdir($logPath0775true))
  312.             {
  313.                 // Something went bad
  314.                 $this->logTools->errorlog('Could not create folder '.$logPath);
  315.                 return null;
  316.             }
  317.         }
  318.         $logFile $logPath."devis_products_".$stamp.".log";
  319.         error_log("--------------------------------------------------\n"3$logFile);
  320.         error_log("[".$today->format("Y-m-d H:i:s")."] ".$devis->getRef()." [".$devis->getId()."]\n"3$logFile);
  321.         $oldNb count($oldProducts);
  322.         error_log("Old Products : ".$oldNb."\n"3$logFile);
  323.         foreach ($oldProducts as $product)
  324.         {
  325.             error_log($product->getAsString()."\n"3$logFile);
  326.         }
  327.         $newNb count($newProducts);
  328.         error_log("New Products : ".$newNb."\n"3$logFile);
  329.         foreach ($newProducts as $product)
  330.         {
  331.             error_log($product->getAsString()."\n"3$logFile);
  332.         }
  333.         error_log("--------------------------------------------------\n"3$logFile);
  334.     }
  335.     public function saveDevisCharges($devis$oldCharges$newCharges)
  336.     {
  337.         if ($devis === null)
  338.             return null;
  339.         $today = new \DateTime();
  340.         $stamp $today->format("Y_m");
  341.         // ROOT/custom_logs/charge_logs/
  342.         $logPath $this->logTools->getChargeLogsDir();
  343.         if (!file_exists($logPath))
  344.         {
  345.             if (!mkdir($logPath0775true))
  346.             {
  347.                 // Something went bad
  348.                 $this->logTools->errorlog('Could not create folder '.$logPath);
  349.                 return null;
  350.             }
  351.         }
  352.         $logFile $logPath."devis_charges_".$stamp.".log";
  353.         error_log("--------------------------------------------------\n"3$logFile);
  354.         error_log("[".$today->format("Y-m-d H:i:s")."] ".$devis->getRef()." [".$devis->getId()."]\n"3$logFile);
  355.         $oldNb count($oldCharges);
  356.         error_log("Old Charges : ".$oldNb."\n"3$logFile);
  357.         foreach ($oldCharges as $charge)
  358.         {
  359.             error_log($charge->getAsString()."\n"3$logFile);
  360.         }
  361.         $newNb count($newCharges);
  362.         error_log("New Charges : ".$newNb."\n"3$logFile);
  363.         foreach ($newCharges as $charge)
  364.         {
  365.             error_log($charge->getAsString()."\n"3$logFile);
  366.         }
  367.         error_log("--------------------------------------------------\n"3$logFile);
  368.     }
  369. }