<?php

namespace console\commands;

use domain\Distribution\Distribution\DistributionModule;
use domain\Producer\Producer\ProducerModule;
use yii\console\Controller;

class ActiveDistributionsInAdvanceController extends Controller
{
    // ./yii active-distributions-in-advance/index
    public function actionIndex()
    {
        $producerModule = ProducerModule::getInstance();
        $distributionModule = DistributionModule::getInstance();

        $producersArray = $producerModule->findProducersActive();
        foreach($producersArray as $producer) {
            \Yii::$app->logic->setProducerContext($producer);
            $weeksDistributionsActivatedInAdvance = $producerModule->getSolver()->getConfig('option_weeks_distributions_activated_in_advance');
            if($weeksDistributionsActivatedInAdvance) {
                $date = new \DateTime('+'.$weeksDistributionsActivatedInAdvance.' weeks');
                if(!$producerModule->getSolver()->isOnLeavePeriod($producer, $date)) {
                    $distributionModule->getBuilder()->activeDistributionByDateIfDelivery($date);

                    $distribution = $distributionModule->getRepository()
                        ->findOneDistribution($date->format('Y-m-d'), true);
                    if($distribution) {
                        echo $producer->name. ' : distribution du ' . $date->format('d/m/Y') .' activée' ."\n";
                    }
                }
            }
        }
    }
}

?>