Commit 5c98421e authored by Stéphane Bouvry's avatar Stéphane Bouvry
Browse files

Ajout de fonctions utilitaires pour l'affichage des périodes

parent a7fd5c87
Loading
Loading
Loading
Loading
+39
Changes for module/Oscar/src/Oscar/Utils/DateTimeUtils.php: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,39 @@ class DateTimeUtils {
        ];
    }

    /**
     * Retourne la liste des périodes entre 2 périodes.
     *
     * @param $from
     * @param $to
     * @return array
     */
    public static function allperiodsBetweenTwo( $from, $to ){
        $start = new \DateTime($from.'-01');
        $end = new \DateTime($to.'-01');

        $startYear = (int) $start->format('Y');
        $startMonth = (int) $start->format('m');

        $endYear = (int) $end->format('Y');
        $endMonth = (int) $end->format('m');

        $periods = [];

        while( !($startYear == $endYear && $startMonth == $endMonth) ){
            $periods[] = self::getCodePeriod($startYear, $startMonth);
            $startMonth++;
            if( $startMonth > 12 ){
                $startYear++;
                $startMonth = 1;
            }
        }
        $periods[] = self::getCodePeriod($startYear, $startMonth);

        return $periods;

    }

    public static function toDatetime( $value )
    {
        if( $value == null ){
@@ -44,6 +77,12 @@ class DateTimeUtils {
        }
    }

    public static function getCodePeriod($year, $month){
        $year = (int)$year;
        $month = (int)$month;
        return sprintf('%s-%s', $year,  ($month < 10 ? '0' . $month : $month));
    }

    public static function extractPeriodDatasFromString($str){
        $re = '/([0-9]{4})\-(10|11|12|0?[1-9])$/';
        preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
+7
Changes for module/Oscar/view/oscar/timesheet/resume.phtml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: bouvry
 * Date: 13/11/18
 * Time: 10:48
 */
 No newline at end of file
+19 −3
Changes for tests/Oscar/Utils/DateTimeUtilsTest.php: 19 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -14,8 +14,6 @@ use PHPUnit\Framework\TestCase;
class DateTimeUtilsTest extends TestCase
{
    public function testSDateOK(){


        $valid = [
            ['date' => '2018-01', 'month' => 1, 'year' => 2018, 'period' => '2018-1', 'periodCode' => '2018-01' ],
            ['date' => '2018-1', 'month' => 1, 'year' => 2018, 'period' => '2018-1', 'periodCode' => '2018-01' ],
@@ -23,7 +21,6 @@ class DateTimeUtilsTest extends TestCase
            ['date' => '2018-2', 'month' => 2, 'year' => 2018, 'period' => '2018-2', 'periodCode' => '2018-02' ],
            ['date' => '2018-12', 'month' => 12, 'year' => 2018, 'period' => '2018-12', 'periodCode' => '2018-12' ],
        ];

        foreach ($valid as $dt){
            $result = DateTimeUtils::extractPeriodDatasFromString($dt['date']);

@@ -35,6 +32,25 @@ class DateTimeUtilsTest extends TestCase
        }
    }

    public function testGetCodePeriod(){
        $this->assertEquals('2018-01', DateTimeUtils::getCodePeriod('2018', '1'));
        $this->assertEquals('2017-12', DateTimeUtils::getCodePeriod('2017', '12'));
    }

    public function testAllperiodsBetweenTwo(){
        $periods = DateTimeUtils::allperiodsBetweenTwo('2018-01', '2018-12');
        $this->assertEquals(12, count($periods));
        $this->assertEquals('2018-01', $periods[0]);
        $this->assertEquals('2018-12', $periods[11]);

        $periods = DateTimeUtils::allperiodsBetweenTwo('2018-06', '2019-06');
        $this->assertEquals(13, count($periods));
        $this->assertEquals('2018-06', $periods[0]);
        $this->assertEquals('2019-06', $periods[12]);
    }



    public function testSDateKO(){