Files
2021-03-23 13:54:38 +01:00

138 lines
4.8 KiB
PHP

<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Beds24;
use Beds24\Model\Beds24ProductInfoQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Install\Database;
use Thelia\Module\BaseModule;
class Beds24 extends BaseModule
{
/** @var string */
const DOMAIN_NAME = 'beds24';
public static function getRemisesParJour()
{
$jsonRemises = self::getConfigValue('remises_par_jour', null);
if (null === $result = json_decode($jsonRemises, true)) {
$result = [
2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0
];
}
krsort($result);
return $result;
}
/**
* @param array $remises
*/
public static function setRemisesParJour($remises)
{
self::setConfigValue('remises_par_jour', json_encode($remises));
}
public function postActivation(ConnectionInterface $con = null)
{
/* try {
Beds24ProductInfoQuery::create()->findOne();
} catch (\Exception $ex) {*/
$database = new Database($con->getWrappedConnection());
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
//}
}
/**
* Retourne la remise en fnction du nombre de jours achetés
*
* @param \DateTime $dateDebut
* @param \DateTime $dateFin
* @return float
*/
public static function getRemiseDuree(\DateTime $dateDebut, \DateTime $dateFin, &$nombreDeJours)
{
$nombreDeJours = round(($dateFin->getTimeStamp() - $dateDebut->getTimeStamp()) / 86400);
foreach (self::getRemisesParJour() as $nbJours => $remisePcent) {
if ($nombreDeJours >= $nbJours) {
return (float) $remisePcent / 100;
}
}
return 0.0;
}
/**
* Retourne le prix total remisé avec le supplément par personne en fonction du nombre de jours achetés.
*
* @param \DateTime $dateDebut
* @param \DateTime $dateFin
* @param float $prix
* @param int $nbAdultes
* @param int $nbEnfants
* @param int $productId
* @param float|null $nombreDeJours
* @param float|null $remise
* @param float|null $prixAvantRemise
* @param float|null $prixOriginal
* @return false|float
*/
public static function getPrixTotalRemise(
\DateTime $dateDebut,
\DateTime $dateFin,
$prix,
$nbAdultes,
$nbEnfants,
$productId,
&$nombreDeJours = 0,
&$remise = 0.0,
&$prixAvantRemise = 0.0,
&$prixOriginal = 0.0
)
{
// Met de côté le prix d'origine
$prixOriginal = $prix;
// Prix avec le supplément par personne et par jour
if (null !== $productInfo = Beds24ProductInfoQuery::create()->findOneByProductId($productId)) {
// Calcul de la remise et de la durée
$remise = self::getRemiseDuree($dateDebut, $dateFin, $nombreDeJours);
$additionalAdultCost = $productInfo ? $productInfo->getAdditionalAdultCost() : [];
$additionalChildrenCost = $productInfo ? $productInfo->getAdditionalChildrenCost() : [];
$nbAdultesSupplementaires = max(0, $nbAdultes - $productInfo->getNbAdultesBase());
$nbEnfantsSupplementaires = max(0, $nbEnfants - $productInfo->getNbEnfantsBase());
if ($nbAdultesSupplementaires > 0 && isset($additionalAdultCost[$nbAdultesSupplementaires])) {
$prix += $nombreDeJours * $additionalAdultCost[$nbAdultesSupplementaires];
}
if ($nbEnfantsSupplementaires > 0 && isset($additionalChildrenCost[$nbEnfantsSupplementaires])) {
$prix += $nombreDeJours * $additionalChildrenCost[$nbEnfantsSupplementaires];
}
// Met de côté le prix avec supplément mais non remisé
$prixAvantRemise = $prix;
// Prix total remisé
$prix = round($prixAvantRemise * (1 - $remise), 2);
}
return $prix;
}
}