Commit be091829 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Rappel contrôlé pour les conflits et prise en compte de l'écoulement des jours pour les conflits

parent f792962f
Loading
Loading
Loading
Loading
+34 −48
Original line number Diff line number Diff line
@@ -69,12 +69,12 @@ class OscarTimesheetDeclarerRecallCommand extends OscarAdvancedCommandAbstract
                null,
                InputOption::VALUE_OPTIONAL,
                "Forcer l'envoi du mail même si ça n'est pas necessaire",
                null
                false
            )
            ->addArgument(
                self::ARG_PERSONID,
                InputArgument::OPTIONAL,
                "ID du déclarant (Utiliser 'all' pour appliquer à tous les déclarants)"
                "ID du déclarant"
            );
    }

@@ -87,6 +87,7 @@ class OscarTimesheetDeclarerRecallCommand extends OscarAdvancedCommandAbstract

        /// OPTIONS and PARAMETERS
        $processDate = $input->getOption(self::OPT_PROCESSDATE);
        $processDate = new \DateTime($processDate);

        $force = $input->getOption(self::OPTION_FORCE) === null;

@@ -108,15 +109,7 @@ class OscarTimesheetDeclarerRecallCommand extends OscarAdvancedCommandAbstract
            $periodText = " (Pour la période $periodlabel)";
        }

        $declarersRecalled = [];

        if( $personId == "all" ){
            $declarersIds = $period == null ?
                    $this->getPersonService()->getDeclarersIds() :
                    $this->getPersonService()->getDeclarersIdsPeriod($period);
        }

        elseif ($personId == null) {
        if ($personId == null) {
            $this->getIO()->title("Liste des déclarants $periodText");

            $declarers = $this->getPersonService()->getPersonsByIds(
@@ -135,16 +128,10 @@ class OscarTimesheetDeclarerRecallCommand extends OscarAdvancedCommandAbstract
                ];
            }
            $this->getIO()->table($headers, $rows);
            $this->getIO()->comment("Vous pouvez utiliser l'ID de la personne en fin de commande ou 'all' pour appliquer à tous les déclarants");
            return 0;
            $this->getIO()->comment("Vous pouvez utiliser l'ID de la personne en fin de commande");
        } else {
            $declarersIds = [$personId];
        }

        if( count($declarersIds) > 0 ) {
            foreach ($declarersIds as $idDeclarer) {
            // Déclarant
                $declarer = $this->getPersonService()->getPersonById($idDeclarer, true);
            $declarer = $this->getPersonService()->getPersonById($personId, true);

            if (!$period) {
                $periods = $this->getTimesheetService()->getPeriodsPerson($declarer);
@@ -159,12 +146,15 @@ class OscarTimesheetDeclarerRecallCommand extends OscarAdvancedCommandAbstract
            } else {
                $this->getIO()->title("Rappel pour $declarer $periodText");
                if ($this->getTimesheetService()->isDeclarerAtPeriod($declarer, $period)) {
                        $result = $this->getTimesheetService()->recallProcess($declarer->getId(), $period, null, $force);
                    $result = $this->getTimesheetService()->recallProcess($declarer->getId(), $period, $processDate, $force);

                    $this->getIO()->writeln(sprintf("Infos : <bold>%s</bold>", $result['recall_info']));
                    $this->getIO()->writeln(sprintf("Date du traitement : <bold>%s</bold>", $processDate->format('Y-m-d H:i:s')));
                    $this->getIO()->writeln(sprintf("Temps MIN/MAX attendu : <bold>%s/%s</bold>", $result['min'], $result['max']));
                    $this->getIO()->writeln(sprintf("Temps DELCARE/ATTENDU : <bold>%s</bold> / <bold>%s</bold>", $result['total'], $result['needed']));
                    $this->getIO()->writeln(sprintf("Mail envoyé : <bold>%s</bold>", $result['mailSend'] ? 'OUI' : 'NON'));
                    $this->getIO()->writeln(sprintf("Jour écoulé depuis le dernier envoi : <bold>%s</bold>", $result['since_last']));
                    $this->getIO()->writeln(sprintf("Jours entre chaque envoi : <bold>%s</bold>", $result['days_beetween']));
                    $this->getIO()->writeln(sprintf("Conflit : <bold>%s</bold>", $result['hasConflict'] ? 'OUI' : 'non'));
                    $this->getIO()->writeln(sprintf("Dernière relance envoyée : <bold>%s</bold>", $result['lastSend']));
                    $this->getIO()->writeln(sprintf("Relance envoyées pour cette période : <bold>%s</bold>", $result['recalls']));
@@ -174,10 +164,6 @@ class OscarTimesheetDeclarerRecallCommand extends OscarAdvancedCommandAbstract
                }
            }
        }
        } else {
            $this->getIO()->error("Aucun déclarant");
        }


        return 1;
    }
+8 −7
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
namespace Oscar\Entity;

use Doctrine\ORM\Mapping as ORM;
use Oscar\Utils\PeriodInfos;

/**
 * Cette classe référence les rôles GLOBAUX sur l'application.
@@ -25,46 +26,46 @@ class RecallDeclaration
     * @var integer
     * @ORM\Column(type="integer")
     */
    private $periodYear;
    private int $periodYear;

    /**
     * Mois de référence
     * @var integer
     * @ORM\Column(type="integer", nullable=false)
     */
    private $periodMonth;
    private int $periodMonth;


    /**
     * @var Person
     * @ORM\ManyToOne(targetEntity="Person")
     */
    private $person;
    private Person $person;

    /**
     * @var string
     * @ORM\Column(type="string")
     */
    private $context = "declarer";
    private string $context = "declarer";

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime")
     *
     */
    private $startProcess;
    private \DateTime $startProcess;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime")
     */
    private $lastSend;
    private \DateTime $lastSend;

    /**
     * @var string
     * @ORM\Column(type="text",nullable=true)
     */
    private $history;
    private string $history = "";

    /**
     * Historique des envois.
+68 −37
Original line number Diff line number Diff line
@@ -2579,7 +2579,10 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
        $periodsBounds = $this->getTimesheetRepository()->getPeriodsPerson($declarer->getId());
        $periods = [];
        foreach ($periodsBounds as $bounds) {
            $periods = array_merge($periods, DateTimeUtils::allperiodsBetweenTwo($bounds['dateStart'], $bounds['dateEnd']));
            $periods = array_merge(
                $periods,
                DateTimeUtils::allperiodsBetweenTwo($bounds['dateStart'], $bounds['dateEnd'])
            );
        }
        return $periods;
    }
@@ -2743,8 +2746,7 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
                $message = "Le déclarant a bien envoyé sa déclaration";
                $needSend = false;
            }
        }
        // Aucune validation en cours
        } // Aucune validation en cours
        else {
            $needSend = true;
            if ($total < $min) {
@@ -2807,23 +2809,18 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
        $declarer = $this->getPersonService()->getPersonById($declarerId);
        $this->getLoggerService()->debug("Procédure de rappel pour $declarer pour la période $period");


        // Informations générales
        $result = $this->getPersonRecallDeclarationPeriod($declarerId, $period);
        $result['mailSend'] = false;
        $periodInfos = PeriodInfos::getPeriodInfosObj($period);
        $recallSend = null;


        // Ancienne relance

        /** @var RecallDeclarationRepository $recallDeclarationRepository */
        $recallDeclarationRepository = $this->getRecallDeclarationRepository();

        $periodInfos = PeriodInfos::getPeriodInfosObj($period);

        // Configuration de la relance
        $declarerFirstDay = $this->getOscarConfigurationService()->getDeclarersRelanceJour1();
        $declarerSecondDay = $this->getOscarConfigurationService()->getDeclarersRelanceJour2();
        $declarerFirstMsg = $this->getOscarConfigurationService()->getDeclarersRelance1();
        $declarerSecondMsg = $this->getOscarConfigurationService()->getDeclarersRelance2();
        $message = $declarerFirstMsg;

        // Récupération de l'historique des rappels
        $recalls = $recallDeclarationRepository->getRecallDeclarationsPersonPeriod(
            $declarerId,
@@ -2833,28 +2830,45 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat

        $result['lastSend'] = "Aucun";
        $result['recalls'] = 0;
        $result['since_last'] = 0;
        $result['days_beetween'] = '#';

        if (count($recalls) == 0) {
            $recallSend = new RecallDeclaration();
            $this->getEntityManager()->persist($recallSend);
            $recallSend->setStartProcess($processDate);
        }
        else {
            $recallSend = $recalls[0];
        }

        ////////////////////////////////////////////////////////////////////////////////////
        /// On détermine si on est :
        ///  - En conflit
        ///  - En première relance
        ///  - En autre relance

        // Premier rappel
        if (count($recalls) == 0) {
            $declarerFirstDay = $this->getOscarConfigurationService()->getDeclarersRelanceJour1();
            $message = $this->getOscarConfigurationService()->getDeclarersRelance1();

            // On test si le jour d'envois est valide (Valeur de J1)
            $processDay = (int)$processDate->format('d');

            if ($declarerFirstDay <= $processDay) {
            if( $result['hasConflict'] ){
                $result['sending'] = true;
                $result['recall_info'] = "Premier envoi (conflit)";
            }
            else if ($declarerFirstDay <= $processDay) {
                $result['sending'] = true;
                $result['recall_info'] = "Premier envoi";
                $recallSend = new RecallDeclaration();
                $this->getEntityManager()->persist($recallSend);
                $recallSend->setStartProcess($processDate);
            } else {
                $result['sending'] = false;
                $result['recall_info'] = "Le jour de relance n'est pas encore atteint";
            }
        }

        // Au-delà du premier rappel
        } // Au-delà du premier rappel
        elseif (count($recalls) == 1) {
            /** @var RecallDeclaration $recallSend */
            $recallSend = $recalls[0];

            $result['lastSend'] = $recallSend->getLastSend()->format('Y-m-d H:i:s');
            $result['recalls'] = $recallSend->getNbrShipments();
@@ -2862,13 +2876,21 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
            // Date d'envois
            $lastSend = $recallSend->getLastSend();
            $daySend = (int)$lastSend->format('d');
            $message = $declarerSecondMsg;
            $dayBeetweenSends = $result['hasConflict'] ?
                $this->getOscarConfigurationService()->getDeclarersRelanceConflitJour() :
                $this->getOscarConfigurationService()->getDeclarersRelanceJour2();

            $message = $result['hasConflict'] ?
                $this->getOscarConfigurationService()->getDeclarersRelanceConflitMessage() :
                $this->getOscarConfigurationService()->getDeclarersRelance2();

            $result['days_beetween'] = $dayBeetweenSends;

            /** @var \DateInterval $interval */
            $interval = $lastSend->diff($processDate);
            $effectifDaysSinceLastSend = $interval->days;
            $result['since_last'] = $effectifDaysSinceLastSend = $interval->days;

            if ($effectifDaysSinceLastSend >= $declarerSecondDay) {
            if ($effectifDaysSinceLastSend >= $dayBeetweenSends) {
                $result['needSend'] = true;
                $result['recall_info'] = "Relance";
            } else {
@@ -2879,18 +2901,22 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
            throw new OscarException("Doublon présent pour le système de contrôle des rappels pour $declarer");
        }

        if ($result['hasConflict'] === true) {
            $message = $this->getOscarConfigurationService()->getDeclarersRelanceConflitMessage();
        }

        if ($result['needSend'] || ($force === true && $result['ignoreForced'] == false)) {

            $result['recall_info'] = "Mail envoyé" . ($force ? ' (forcé)' : '');
            $result['mailSend'] = true;
            $repport = $this->sendMailRecallDeclarer(
                $declarer,
                $periodInfos->getPeriodCode(),
                $message,
                $recallSend,
                $processDate,
                $force
            );
            $result['mailSend'] = true;
            return array_merge($result, $repport);
        }

@@ -2910,8 +2936,14 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
        string $period,
        string $messageTemplate,
        RecallDeclaration $recallDeclaration,
        $forced=false ):array
    {
        ?\DateTime $processDate = null,
        $forced = false
    ): array {

        if( $processDate == null ){
            $processDate = new \DateTime();
        }

        $periodInfos = PeriodInfos::getPeriodInfosObj($period);

        // Replace
@@ -2930,8 +2962,8 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat

            // Enregistrement du rappel
            $recallDeclaration->setContext('declarer')
                ->logShipments("Envois d'un rappel", null, $forced)
                ->setLastSend(new \DateTime())
                ->logShipments("Envois d'un rappel", $processDate, $forced)
                ->setLastSend($processDate)
                ->setPeriodMonth($periodInfos->getMonth())
                ->setPeriodYear($periodInfos->getYear())
                ->setPerson($declarer);
@@ -2939,13 +2971,12 @@ class TimesheetService implements UseOscarUserContextService, UseOscarConfigurat
            $this->getEntityManager()->flush($recallDeclaration);

            return $recallDeclaration->getRepport();

        } catch (\Exception $e) {

            throw new OscarException(
                "Un problème est survenu lors de la procédure de rappel pour $declarer pour la période $period : " . $e->getMessage()
            );
        }

    }