Explorar el Código

[Backend] working on

develop
Fab hace 3 años
padre
commit
bf27e79aa0
Se han modificado 1 ficheros con 152 adiciones y 1 borrados
  1. +152
    -1
      ShopBundle/Statistic/Statistic.php

+ 152
- 1
ShopBundle/Statistic/Statistic.php Ver fichero

@@ -4,10 +4,161 @@
namespace Lc\ShopBundle\Statistic;


use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Manager\EntityManager;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Statistic
{
public function get(){
protected $properties;
protected $averageProperties;
protected $labels;
protected $dateStart;
protected $dateEnd;
protected $interval;
protected $em;
protected $resultsSort;

public function __construct(EntityManagerInterface $entityManager, ?\DateTime $dateStart, ? \DateTime $dateEnd, ? string $interval)
{
$this->em = $entityManager;
$this->dateStart = $dateStart;
$this->dateEnd = $dateEnd;
$this->interval = $interval;
}

public function addProperty(string $propertyName, array $options)
{
$resolver = new OptionsResolver();
$this->configurePropertyOptions($resolver);
$this->properties[$propertyName] = $resolver->resolve($options);
$this->resultsSort[$propertyName] =$propertyName;
}

public function addAverageProperty(string $propertyName, array $options)
{
$resolver = new OptionsResolver();
$this->configurePropertyOptions($resolver);
$this->averageProperties[$propertyName] = $resolver->resolve($options);
$this->resultsSort[$propertyName] =$propertyName;

}


public function setAverageData(string $propertyName, string $key, $value){
if (isset($this->averageProperties[$propertyName]) ) {
$this->averageProperties[$propertyName]['data'][$key] += number_format($value, 2);

}else{
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
}
public function setData(string $propertyName, string $key, $value)
{
if (isset($this->properties[$propertyName])){
$this->properties[$propertyName]['data'][$key] += number_format($value, 2);
$this->properties[$propertyName]['total_period'] += number_format($value, 2);
}else{
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}


}

public function setAveragePropertiesData()
{
foreach ($this->getLabels() as $key => $label) {
foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
if ($this->getData($averageProperty['divider'], $key)) {
$this->setAverageData($averagePropertyName, $key, $this->getData($averageProperty['dividend'], $key) / $this->getData($averageProperty['divider'], $key));
}

if ($this->getTotalPeriod($averageProperty['divider'])) {
$this->averageProperties[$averagePropertyName]['total_period'] = $this->getTotalPeriod($averageProperty['dividend']) / $this->getTotalPeriod($averageProperty['divider']);
}
}
}



}


public function getLabels()
{
return $this->labels;
}

public function getProperties()
{
return $this->properties;
}

public function getData($propertyName, $key)
{
if (!isset($this->properties[$propertyName])) {
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
return $this->properties[$propertyName]['data'][$key];
}

public function getTotalPeriod($propertyName)
{
if (!isset($this->properties[$propertyName])) {
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
return $this->properties[$propertyName]['total_period'];
}

public function getAverageProperties()
{
return $this->averageProperties;
}


public function getResults()
{
$results = array_merge($this->resultsSort, $this->properties, $this->averageProperties);
return $results;
}

public function getAsArray(){
return array(
'label' => $this->getLabels(),
'data' => $this->getResults(),
);
}

public function getDateRange()
{
return new \DatePeriod($this->getDateStart(), new \DateInterval('P1' . $this->getInterval()), $this->getDateEnd());
}

public function getDateStart()
{
return $this->dateStart;
}

public function getDateEnd()
{
return $this->dateEnd;
}

public function getInterval()
{
return $this->interval;
}

public function configurePropertyOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'unit' => '',
'label' => 'Chiffre affaire produit',
'label_short' => 'CA produit',
'data' => array(),
'total_period' => 0,
'dividend' => null,
'divider' => null,
]);
}
}

Cargando…
Cancelar
Guardar