<?php
//----------------------------------------------------------------------
// src/Services/Common/RefGeneratorTools.php
//----------------------------------------------------------------------
namespace App\Services\Common;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Common\RefGenerator;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Invoice\Invoice;
use App\Services\LogTools;
class RefGeneratorTools
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
}
/**
* Plan.io Task #4218
*
* From the iTask Listing we can add multiple invoices, with a given date.
* Since calls to RefGenerator require a flush for each generated reference
* invoices needed to be flushed one by one to avoid creating the same ref.
* However, since invoices can be persited in cascade with other elements,
* the flush for the first invoice whold try to flush all invoices,
* thus killing the Entity Manager :) [ref cannot be null] ...
*
* craftRefMultiple takes a list of objects (Invoices for now)
* and sets their respective ref using temporary objects that are flushed in the end
* togheter with their respective invoices.
*/
public function craftRefMultiple($objects, $code)
{
// Invoices for now
// Info for #4326
// This is used by iTask Listing
// For now, invoices created using iTasks are not drafts
// However, if that should change in the future, CODE_INVOICE_DRAFT should be added here
// and properly handled
if (!in_array($code, array(
RefGenerator::CODE_INVOICE,
)))
return false;
// Create an array indexed with society.id / year / month
// that holds the RefGenerator values to be incremented
$tempUtility = array();
foreach ($objects as $key => $object)
{
if (!($object instanceof Invoice)) continue;
if ($object->getCreationDate() === null)
$today = new \DateTime();
else
$today = $object->getCreationDate();
$yearNumber = $today->format('Y');
$monthNumber = $today->format('n');
$society = $object->getSociety();
$sid = $society->getId();
// Construct the temporary array, if not already created
if (!array_key_exists($sid, $tempUtility))
{
$tempUtility[$sid] = array();
}
if (!array_key_exists($yearNumber, $tempUtility[$sid]))
{
$tempUtility[$sid][$yearNumber] = array();
}
if (!array_key_exists($monthNumber, $tempUtility[$sid][$yearNumber]))
{
$tempUtility[$sid][$yearNumber][$monthNumber] = array();
}
// At this point we have the array space, so add the correct RefGenerator object
if (empty($tempUtility[$sid][$yearNumber][$monthNumber]))
{
// Does it exist in the DataBase ?
// Patch invoice plan.io task #4290
// Ok we have only invoices here for now
// But keep year/month in the else in case we have other objects in futur dev
if($code == RefGenerator::CODE_INVOICE)
{
$utility = $this->em->getRepository(RefGenerator::class)
->findOneBy(array(
'society' => $society,
'code' => $code,
));
}
else
{
$utility = $this->em->getRepository(RefGenerator::class)
->findOneBy(array(
'society' => $society,
'code' => $code,
'year' => $yearNumber,
'month' => $monthNumber,
));
}
if ($utility === null)
{
// Nope => Create it
$utility = new RefGenerator();
$utility->setSociety($society);
$utility->setCode($code);
$utility->setYear($yearNumber);
$utility->setMonth($monthNumber);
$utility->setNb(0);
// Do not forget to persit it ;)
$this->em->persist($utility);
}
// Link the RefGenerator object to the temporary array
$tempUtility[$sid][$yearNumber][$monthNumber] = $utility;
}
// Use the RefGenerator from the temporary array
// This can be either an object fetched from the DataBase,
// or an object just created
// In both cases it will act as a buffer to hold the
// incrementations of the object's value
$nb = $tempUtility[$sid][$yearNumber][$monthNumber]->getNb();
$object->setRef($nb + 1);
// Increment the object in the tempUtility buffer
$tempUtility[$sid][$yearNumber][$monthNumber]->setNb($nb + 1);
}
return true;
}
// Plan.io Task #4326
public function craftInvoiceRef(Invoice $invoice)
{
if ($invoice->isDraft())
return $this->craftRef($invoice, RefGenerator::CODE_INVOICE_DRAFT);
return $this->craftRef($invoice, RefGenerator::CODE_INVOICE);
}
public function craftRef($object, $code)
{
if ($object === null)
return false;
if (!in_array($code, array(
RefGenerator::CODE_CUSTOM,
RefGenerator::CODE_DEVIS,
RefGenerator::CODE_INVOICE,
RefGenerator::CODE_INVOICE_DRAFT,
RefGenerator::CODE_COMMAND,
RefGenerator::CODE_MISSION,
RefGenerator::CODE_WEBAPP_RFI,
RefGenerator::CODE_WEBAPP_RFI_GC,
RefGenerator::CODE_WEBAPP_ANOMALY,
RefGenerator::CODE_WEBAPP_ANOMALY_GC,
RefGenerator::CODE_WEBAPP_REPORT,
RefGenerator::CODE_WEBAPP_KVISIT_REPORT,
RefGenerator::CODE_DEMAND,
RefGenerator::CODE_COST,
RefGenerator::CODE_RH_FORM_LEAVE,
RefGenerator::CODE_RH_FORM_ACCIDENT,
RefGenerator::CODE_EXTERNAL_MESSAGE,
RefGenerator::CODE_APPLICATION,
RefGenerator::CODE_RH_FORM_HOURS,
RefGenerator::CODE_IKEA_SERVICE_ORDER,
)))
return false;
if ($object->getCreationDate() === null)
$today = new \DateTime();
else
$today = $object->getCreationDate();
$yearNumber = $today->format('Y');
$monthNumber = $today->format('n');
$society = $object->getSociety();
// Patch invoice plan.io Task #4290
if ($code == RefGenerator::CODE_INVOICE)
{
$utility = $this->em->getRepository(RefGenerator::class)
->findOneBy(array(
'society' => $society,
'code' => $code,
));
}
else
{
$utility = $this->em->getRepository(RefGenerator::class)
->findOneBy(array(
'society' => $society,
'code' => $code,
'year' => $yearNumber,
'month' => $monthNumber,
));
}
if ($utility === null)
{
// Create it
$utility = new RefGenerator();
$utility->setSociety($society);
$utility->setCode($code);
$utility->setYear($yearNumber);
$utility->setMonth($monthNumber);
$utility->setNb(0);
}
$nb = $utility->getNb();
$object->setRef($nb + 1);
$utility->setNb($nb + 1);
$this->em->persist($utility);
return true;
}
/*
$object : The object for which we are setting the ref (CustomReport for now)
$code : The code of the RefGenerator (CODE_CUSTOM for now)
$template : WebappMakerBundle :: Template
*/
public function craftCustomRef($object, $code, $template)
{
if ($object === null)
return false;
if (!in_array($code, array(
RefGenerator::CODE_CUSTOM,
)))
return false;
if ($object->getCreationDate() === null)
$today = new \DateTime();
else
$today = $object->getCreationDate();
$yearNumber = $today->format('Y');
$monthNumber = $today->format('n');
$society = $object->getSociety();
$utility = $this->em->getRepository(RefGenerator::class)
->findOneBy(array(
'society' => $society,
'code' => $code,
'year' => $yearNumber,
'month' => $monthNumber,
'webappMakerTemplate' => $template,
));
if ($utility === null)
{
// Create it
$utility = new RefGenerator();
$utility->setWebappMakerTemplate($template);
$utility->setSociety($society);
$utility->setCode($code);
$utility->setYear($yearNumber);
$utility->setMonth($monthNumber);
$utility->setNb(0);
}
$nb = $utility->getNb();
// Set the ref of the given object
$object->setRef($nb + 1, $template);
// Increment the number in the RefGenerator
$utility->setNb($nb + 1);
// Persist
$this->em->persist($utility);
return true;
}
}