|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
-
-
- namespace Lc\CaracoleBundle\Statistic;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\OptionsResolver\OptionsResolver;
-
- class Statistic
- {
- protected $properties = array();
- protected $averageProperties = array();
- protected $labels;
- protected $dateStart;
- protected $dateEnd;
- protected $interval;
- protected $em;
- protected $resultsSort;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- ?\DateTime $dateStart = null,
- ?\DateTime $dateEnd = null,
- ?string $interval = null
- ) {
- $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']);
- }
- }
- }
- foreach ($this->getProperties() as $propertyName => $property) {
- $this->properties[$propertyName]['average_period'] = number_format(
- $this->properties[$propertyName]['total_period'] / count($this->getLabels()),
- 2
- );
- }
- foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
- $this->averageProperties[$averagePropertyName]['average_period'] = number_format(
- $this->averageProperties[$averagePropertyName]['total_period'] / count($this->getLabels()),
- 2
- );
- }
- }
-
-
- 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_replace($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,
- 'average_period' => 0,
- 'dividend' => null,
- 'divider' => null,
- ]
- );
- }
- }
|