<?php

namespace console\commands;

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

class InitPointSalePaymentMethodsFromSettingsController extends Controller
{
    // ./yii init-point-sale-payment-methods-from-settings/index
    public function actionIndex()
    {
        $producerModule = ProducerModule::getInstance();
        $pointSaleModule = PointSaleModule::getInstance();

        $producersArray = $producerModule->findProducers();
        foreach($producersArray as $producer) {
            echo '> '.$producer->name."\n";
            \Yii::$app->logic->setProducerContext($producer);
            $pointSaleArray = $pointSaleModule->getRepository()->findPointSales();
            $creditFunctioningProducer = $producerModule->getConfig('credit_functioning');
            $isOnlinePaymentActiveAndTypeOrder = $producerModule->isOnlinePaymentActiveAndTypeOrder($producer, false);
            foreach($pointSaleArray as $pointSale) {
                if($pointSale->payment_method_credit && !$pointSale->credit_functioning) {
                    $pointSale->credit_functioning = $creditFunctioningProducer;
                    if($pointSale->credit_functioning == Producer::CREDIT_FUNCTIONING_OPTIONAL) {
                        $pointSale->payment_method_onsite = true;
                    }
                    $pointSale->save();
                    echo 'Point de vente "'.$pointSale->name. '" configuré sur "'.$pointSale->credit_functioning.'"'."\n";
                }
                $return = $pointSaleModule->getBuilder()->initPaymentMethodsByCreditFunctioning($pointSale);
                if($return) {
                    echo 'Point de vente "'.$pointSale->name. '" : modes de paiements adaptés'."\n";
                }
                if($isOnlinePaymentActiveAndTypeOrder) {
                    $pointSale->payment_method_online = true;
                    $pointSale->save();
                    echo 'Point de vente "'.$pointSale->name. '" : paiement en ligne activé'."\n";
                }
            }
        }
    }
}

?>