src/Services/Common/RefGeneratorTools.php line 16

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Common/RefGeneratorTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Common;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Common\RefGenerator;
  8. use App\Entity\Platform\Devis\Devis;
  9. use App\Entity\Platform\Invoice\Invoice;
  10. use App\Services\LogTools;
  11. class RefGeneratorTools
  12. {
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logTools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.     }
  18.     /**
  19.      * Plan.io Task #4218
  20.      *
  21.      * From the iTask Listing we can add multiple invoices, with a given date.
  22.      * Since calls to RefGenerator require a flush for each generated reference
  23.      * invoices needed to be flushed one by one to avoid creating the same ref.
  24.      * However, since invoices can be persited in cascade with other elements,
  25.      * the flush for the first invoice whold try to flush all invoices,
  26.      * thus killing the Entity Manager :) [ref cannot be null] ...
  27.      *
  28.      * craftRefMultiple takes a list of objects (Invoices for now)
  29.      * and sets their respective ref using temporary objects that are flushed in the end
  30.      * togheter with their respective invoices.
  31.      */
  32.     public function craftRefMultiple($objects$code)
  33.     {
  34.         // Invoices for now
  35.         // Info for #4326
  36.         // This is used by iTask Listing
  37.         // For now, invoices created using iTasks are not drafts
  38.         // However, if that should change in the future, CODE_INVOICE_DRAFT should be added here
  39.         // and properly handled
  40.         if (!in_array($code, array(
  41.             RefGenerator::CODE_INVOICE,
  42.         )))
  43.             return false;
  44.         // Create an array indexed with society.id / year / month
  45.         // that holds the RefGenerator values to be incremented
  46.         $tempUtility = array();
  47.         foreach ($objects as $key => $object)
  48.         {
  49.             if (!($object instanceof Invoice)) continue;
  50.             if ($object->getCreationDate() === null)
  51.                 $today = new \DateTime();
  52.             else
  53.                 $today $object->getCreationDate();
  54.             $yearNumber $today->format('Y');
  55.             $monthNumber $today->format('n');
  56.             $society $object->getSociety();
  57.             $sid $society->getId();
  58.             // Construct the temporary array, if not already created
  59.             if (!array_key_exists($sid$tempUtility))
  60.             {
  61.                 $tempUtility[$sid] = array();
  62.             }
  63.             if (!array_key_exists($yearNumber$tempUtility[$sid]))
  64.             {
  65.                 $tempUtility[$sid][$yearNumber] = array();
  66.             }
  67.             if (!array_key_exists($monthNumber$tempUtility[$sid][$yearNumber]))
  68.             {
  69.                 $tempUtility[$sid][$yearNumber][$monthNumber] = array();
  70.             }
  71.             // At this point we have the array space, so add the correct RefGenerator object
  72.             if (empty($tempUtility[$sid][$yearNumber][$monthNumber]))
  73.             {
  74.                 // Does it exist in the DataBase ?
  75.                 // Patch invoice plan.io task #4290
  76.                 // Ok we have only invoices here for now
  77.                 // But keep year/month in the else in case we have other objects in futur dev
  78.                 if($code == RefGenerator::CODE_INVOICE)
  79.                 {
  80.                     $utility $this->em->getRepository(RefGenerator::class)
  81.                         ->findOneBy(array(
  82.                             'society'            =>    $society,
  83.                             'code'                =>    $code,
  84.                         ));
  85.                 }
  86.                 else
  87.                 {
  88.                     $utility $this->em->getRepository(RefGenerator::class)
  89.                         ->findOneBy(array(
  90.                             'society'            =>    $society,
  91.                             'code'                =>    $code,
  92.                             'year'                =>    $yearNumber,
  93.                             'month'                =>    $monthNumber,
  94.                         ));
  95.                 }
  96.                 if ($utility === null)
  97.                 {
  98.                     // Nope => Create it
  99.                     $utility = new RefGenerator();
  100.                     $utility->setSociety($society);
  101.                     $utility->setCode($code);
  102.                     $utility->setYear($yearNumber);
  103.                     $utility->setMonth($monthNumber);
  104.                     $utility->setNb(0);
  105.                     // Do not forget to persit it ;)
  106.                     $this->em->persist($utility);
  107.                 }
  108.                 // Link the RefGenerator object to the temporary array
  109.                 $tempUtility[$sid][$yearNumber][$monthNumber] = $utility;
  110.             }
  111.             // Use the RefGenerator from the temporary array
  112.             // This can be either an object fetched from the DataBase,
  113.             // or an object just created
  114.             // In both cases it will act as a buffer to hold the
  115.             // incrementations of the object's value
  116.             $nb $tempUtility[$sid][$yearNumber][$monthNumber]->getNb();
  117.             $object->setRef($nb 1);
  118.             // Increment the object in the tempUtility buffer
  119.             $tempUtility[$sid][$yearNumber][$monthNumber]->setNb($nb 1);
  120.         }
  121.         return true;
  122.     }
  123.     // Plan.io Task #4326
  124.     public function craftInvoiceRef(Invoice $invoice)
  125.     {
  126.         if ($invoice->isDraft())
  127.             return $this->craftRef($invoiceRefGenerator::CODE_INVOICE_DRAFT);
  128.         return $this->craftRef($invoiceRefGenerator::CODE_INVOICE);
  129.     }
  130.     public function craftRef($object$code)
  131.     {
  132.         if ($object === null)
  133.             return false;
  134.         if (!in_array($code, array(
  135.             RefGenerator::CODE_CUSTOM,
  136.             RefGenerator::CODE_DEVIS,
  137.             RefGenerator::CODE_INVOICE,
  138.             RefGenerator::CODE_INVOICE_DRAFT,
  139.             RefGenerator::CODE_COMMAND,
  140.             RefGenerator::CODE_MISSION,
  141.             RefGenerator::CODE_WEBAPP_RFI,
  142.             RefGenerator::CODE_WEBAPP_RFI_GC,
  143.             RefGenerator::CODE_WEBAPP_ANOMALY,
  144.             RefGenerator::CODE_WEBAPP_ANOMALY_GC,
  145.             RefGenerator::CODE_WEBAPP_REPORT,
  146.             RefGenerator::CODE_WEBAPP_KVISIT_REPORT,
  147.             RefGenerator::CODE_DEMAND,
  148.             RefGenerator::CODE_COST,
  149.             RefGenerator::CODE_RH_FORM_LEAVE,
  150.             RefGenerator::CODE_RH_FORM_ACCIDENT,
  151.             RefGenerator::CODE_EXTERNAL_MESSAGE,
  152.             RefGenerator::CODE_APPLICATION,
  153.             RefGenerator::CODE_RH_FORM_HOURS,
  154.             RefGenerator::CODE_IKEA_SERVICE_ORDER,
  155.         )))
  156.             return false;
  157.         if ($object->getCreationDate() === null)
  158.             $today = new \DateTime();
  159.         else
  160.             $today $object->getCreationDate();
  161.         $yearNumber $today->format('Y');
  162.         $monthNumber $today->format('n');
  163.         $society $object->getSociety();
  164.         // Patch invoice plan.io Task #4290
  165.         if ($code == RefGenerator::CODE_INVOICE)
  166.         {
  167.             $utility $this->em->getRepository(RefGenerator::class)
  168.             ->findOneBy(array(
  169.                 'society'            =>    $society,
  170.                 'code'                =>    $code,
  171.             ));
  172.         }
  173.         else
  174.         {
  175.             $utility $this->em->getRepository(RefGenerator::class)
  176.                 ->findOneBy(array(
  177.                     'society'            =>    $society,
  178.                     'code'                =>    $code,
  179.                     'year'                =>    $yearNumber,
  180.                     'month'                =>    $monthNumber,
  181.                 ));
  182.         }
  183.         if ($utility === null)
  184.         {
  185.             // Create it
  186.             $utility = new RefGenerator();
  187.             $utility->setSociety($society);
  188.             $utility->setCode($code);
  189.             $utility->setYear($yearNumber);
  190.             $utility->setMonth($monthNumber);
  191.             $utility->setNb(0);
  192.         }
  193.         $nb $utility->getNb();
  194.         $object->setRef($nb 1);
  195.         $utility->setNb($nb 1);
  196.         $this->em->persist($utility);
  197.         return true;
  198.     }
  199.     /*
  200.         $object                        :    The object for which we are setting the ref (CustomReport for now)
  201.         $code                        :    The code of the RefGenerator (CODE_CUSTOM for now)
  202.         $template                    :    WebappMakerBundle :: Template
  203.     */
  204.     public function craftCustomRef($object$code$template)
  205.     {
  206.         if ($object === null)
  207.             return false;
  208.         if (!in_array($code, array(
  209.             RefGenerator::CODE_CUSTOM,
  210.         )))
  211.             return false;
  212.         if ($object->getCreationDate() === null)
  213.             $today = new \DateTime();
  214.         else
  215.             $today $object->getCreationDate();
  216.         $yearNumber $today->format('Y');
  217.         $monthNumber $today->format('n');
  218.         $society $object->getSociety();
  219.         $utility $this->em->getRepository(RefGenerator::class)
  220.             ->findOneBy(array(
  221.                 'society'                =>    $society,
  222.                 'code'                    =>    $code,
  223.                 'year'                    =>    $yearNumber,
  224.                 'month'                    =>    $monthNumber,
  225.                 'webappMakerTemplate'    =>    $template,
  226.             ));
  227.         if ($utility === null)
  228.         {
  229.             // Create it
  230.             $utility = new RefGenerator();
  231.             $utility->setWebappMakerTemplate($template);
  232.             $utility->setSociety($society);
  233.             $utility->setCode($code);
  234.             $utility->setYear($yearNumber);
  235.             $utility->setMonth($monthNumber);
  236.             $utility->setNb(0);
  237.         }
  238.         $nb $utility->getNb();
  239.         // Set the ref of the given object
  240.         $object->setRef($nb 1$template);
  241.         // Increment the number in the RefGenerator
  242.         $utility->setNb($nb 1);
  243.         // Persist
  244.         $this->em->persist($utility);
  245.         return true;
  246.     }
  247. }