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

Envoi et réenvoi des déclarations (inclus le retrait de type de créneaux et...

Envoi et réenvoi des déclarations (inclus le retrait de type de créneaux et ajout de créneaux non-présent dans la déclaration initiale
parent 10bd8d50
Loading
Loading
Loading
Loading
+16 −8
Changes for module/Oscar/src/Oscar/Controller/TimesheetController.php: 16 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -1792,6 +1792,7 @@ class TimesheetController extends AbstractOscarController

                /** @var ValidationPeriod $vp */
                foreach ($validationPeriods as $vp) {
                    $this->getLogger()->debug("$vp");
                    if( $vp->getStatus() == ValidationPeriod::STATUS_CONFLICT ){
                        $unauthorizedError = "Vous ne pouvez pas modifier une déclaration en cours de validation. Seul les créneaux marqués en erreur peuvent être modifiés";
                        $hasConflict = true;
@@ -1799,10 +1800,12 @@ class TimesheetController extends AbstractOscarController
                }

                // Aucune procédure de validation spécifique pour ce type de créneau
                if( !$validationPeriod || !$validationPeriod->hasConflict() ){
                if( $validationPeriod ){
                    if( !$validationPeriod->hasConflict() ){
                        return $this->getResponseBadRequest($unauthorizedError);
                    }
                }
            }

            if( $timesheetId ){
                $timesheet = $this->getEntityManager()->getRepository(TimeSheet::class)->find($timesheetId);
@@ -1914,14 +1917,19 @@ class TimesheetController extends AbstractOscarController

                    $datas = json_decode($this->params()->fromPost('datas'));

                    // FIX : Déplacer le test d'envois/réenvois
                    // nb : Le traitement de l'envoi/réenvois a été centralisé dans le TimesheetService
                    // dans la méthode d'envoi.

                    // Réenvoi de la déclaration
                    if( $action == 'resend' ){
                        $from = new \DateTime($datas->from);
                        $to = new \DateTime($datas->to);
//                        $timesheetService->sendPeriod($from, $to, $currentPerson, $comments);
                        $timesheetService->reSendPeriod($from, $to, $currentPerson, $comments);
                        return $this->getResponseOk();
                    }
//                    if( $action == 'resend' ){
//                        throw new \Exception("DEBUG");
//                        $from = new \DateTime($datas->from);
//                        $to = new \DateTime($datas->to);
////                        $timesheetService->sendPeriod($from, $to, $currentPerson, $comments);
//                        $timesheetService->reSendPeriod($from, $to, $currentPerson, $comments);
//                        return $this->getResponseOk();
//                    }


                    if( !$datas ){
+0 −1
Changes for module/Oscar/src/Oscar/Entity/TimeSheet.php: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -188,7 +188,6 @@ class TimeSheet implements ITrackable
    /**
     * @var ValidationPeriod
     * @ORM\ManyToOne(targetEntity="ValidationPeriod", inversedBy="timesheets")
     * @ORM\JoinColumn(onDelete="SET NULL")
     */
    private $validationPeriod;

+44 −9
Changes for module/Oscar/src/Oscar/Service/TimesheetService.php: 44 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -2322,6 +2322,7 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa
        $periodValidations = $this->getPeriodValidation($person, $month, $year);
        $isPeriodSend = count($periodValidations);
        $periodValidationsDt = [];
        $periodSendable = false;

        $from = $periodFirstDay->format('Y-m-d');
        $to = $periodLastDay->format('Y-m-d');
@@ -2339,6 +2340,7 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa
            if( $periodValidation->hasConflict() ){
                $hasConflict = true;
                $editable = true;
                $periodSendable = true;
            }
            $data = $periodValidation->json();
            if ($periodValidation->getObjectId() > 0) {
@@ -2352,6 +2354,7 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa
        // Période pas encore commencée
        if ($periodFutur) {
            $periodInfos = "Ce mois n'a pas encore commencé";
            $periodSendable = false;
        }

        if ($periodFinished) {
@@ -2359,7 +2362,7 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa

            // Il y'a des déclarations
            if (count($periodValidations)) {
                $submitable = false;
                $submitable = $hasConflict;
                $submitableInfos = "Vous avez déja envoyé cette période pour validation";
                $editable = $hasConflict;
                $editableInfos = "Vous avez déja envoyé cette période pour validation";
@@ -2408,7 +2411,7 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa
            $validationUp = false;

            if ($isPeriodSend) {
                $validationUp = $periodActivityValidation && $periodActivityValidation->isOpenForDeclaration();
                $validationUp = true; //$periodActivityValidation && $periodActivityValidation->isOpenForDeclaration();
            } else {
                $validationUp = true;
            }
@@ -2456,7 +2459,7 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa
            $periodHL = $this->getValidationPeriosOutOfWorkpackageAt($person, $year, $month, $key);

            if ($isPeriodSend) {
                $validationUp = $periodHL && $periodHL->isOpenForDeclaration();
                $validationUp = true; //$periodHL && $periodHL->isOpenForDeclaration();
            } else {
                $validationUp = true;
            }
@@ -3240,30 +3243,62 @@ class TimesheetService implements ServiceLocatorAwareInterface, EntityManagerAwa

    public function sendPeriod($from, $to, $sender, $comments=null)
    {

        $fromMonth = $from->format('Y-m');
        $toMonth = $to->format('Y-m');

        $this->getLogger()->debug("Envois de la période : $fromMonth - $toMonth");

        if ($fromMonth != $toMonth)
            throw new Exception("La période à traiter n'est pas un mois...");

        $mois = (integer)$from->format('m');
        $annee = (integer)$from->format('Y');

        // Créneaux de la périodes
        $timesheets = $this->getQueryTimesheetsPersonPeriod($sender, $from, $to)->getResult();

        if (count($timesheets) == 0) {
            throw new OscarException("Aucun créneau à soumettre pour cette période.");
        }

        // Déclarations de la période
        $declarations = $this->getValidationPeriods($annee, $mois, $sender);

        // Si la période a déjà des déclaration
        if (count($declarations) > 0) {

            // On test si une des validations est en conflit ?
            $error = true;
            /** @var ValidationPeriod $declaration */
            foreach ($declarations as $declaration) {
                if($declaration->hasConflict()){
                    $error = false;
                }
            }

            // Si on n'a pas de conflit => ERREUR
            if( $error ) {
                throw new OscarException("Vous avez déjà envoyé des déclarations pour cette période");
            }

            // Sinon on supprime les anciennes validations
            else {
                $this->getLogger()->debug("Suppression des anciennes déclarations");
                /** @var ValidationPeriod $v */
                foreach ($declarations as $v){
                    /** @var TimeSheet $t */
                    foreach ($v->getTimesheets() as $t){
                        $t->setValidationPeriod(null);
                    }
                    $this->getEntityManager()->remove($v);
                }
                $this->getEntityManager()->flush($v);
            }
        }

        // Créneaux de la périodes
        $timesheets = $this->getQueryTimesheetsPersonPeriod($sender, $from, $to)->getResult();

        if (count($timesheets) == 0) {
            throw new OscarException("Aucun créneau à soumettre pour cette période.");
        }


        $declarations = [];

        /** @var TimeSheet $timesheet */