You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.0KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Statistic;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\OptionsResolver\OptionsResolver;
  5. class Statistic
  6. {
  7. protected $properties = array();
  8. protected $averageProperties = array();
  9. protected $labels;
  10. protected $dateStart;
  11. protected $dateEnd;
  12. protected $interval;
  13. protected $em;
  14. protected $resultsSort;
  15. public function __construct(
  16. EntityManagerInterface $entityManager,
  17. ?\DateTime $dateStart = null,
  18. ?\DateTime $dateEnd = null,
  19. ?string $interval = null
  20. ) {
  21. $this->em = $entityManager;
  22. $this->dateStart = $dateStart;
  23. $this->dateEnd = $dateEnd;
  24. $this->interval = $interval;
  25. }
  26. public function addProperty(string $propertyName, array $options)
  27. {
  28. $resolver = new OptionsResolver();
  29. $this->configurePropertyOptions($resolver);
  30. $this->properties[$propertyName] = $resolver->resolve($options);
  31. $this->resultsSort[$propertyName] = $propertyName;
  32. }
  33. public function addAverageProperty(string $propertyName, array $options)
  34. {
  35. $resolver = new OptionsResolver();
  36. $this->configurePropertyOptions($resolver);
  37. $this->averageProperties[$propertyName] = $resolver->resolve($options);
  38. $this->resultsSort[$propertyName] = $propertyName;
  39. }
  40. public function setAverageData(string $propertyName, string $key, $value)
  41. {
  42. if (isset($this->averageProperties[$propertyName])) {
  43. $this->averageProperties[$propertyName]['data'][$key] += number_format($value, 2);
  44. } else {
  45. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  46. }
  47. }
  48. public function setData(string $propertyName, string $key, $value)
  49. {
  50. if (isset($this->properties[$propertyName])) {
  51. if(isset($this->properties[$propertyName]['data'][$key])) {
  52. $this->properties[$propertyName]['data'][$key] += $value;
  53. }
  54. $this->properties[$propertyName]['total_period'] += $value;
  55. } else {
  56. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  57. }
  58. }
  59. public function setAveragePropertiesData()
  60. {
  61. foreach ($this->getLabels() as $key => $label) {
  62. foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
  63. if ($this->getData($averageProperty['divider'], $key)) {
  64. $this->setAverageData(
  65. $averagePropertyName,
  66. $key,
  67. $this->getData($averageProperty['dividend'], $key) / $this->getData(
  68. $averageProperty['divider'],
  69. $key
  70. )
  71. );
  72. }
  73. if ($this->getTotalPeriod($averageProperty['divider'])) {
  74. $this->averageProperties[$averagePropertyName]['total_period'] = $this->getTotalPeriod(
  75. $averageProperty['dividend']
  76. ) / $this->getTotalPeriod($averageProperty['divider']);
  77. }
  78. }
  79. }
  80. foreach ($this->getProperties() as $propertyName => $property) {
  81. $this->properties[$propertyName]['average_period'] = number_format(
  82. $this->properties[$propertyName]['total_period'] / count($this->getLabels()),
  83. 2
  84. );
  85. }
  86. foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
  87. $this->averageProperties[$averagePropertyName]['average_period'] = number_format(
  88. $this->averageProperties[$averagePropertyName]['total_period'] / count($this->getLabels()),
  89. 2
  90. );
  91. }
  92. }
  93. public function getLabels()
  94. {
  95. return $this->labels;
  96. }
  97. public function getProperties()
  98. {
  99. return $this->properties;
  100. }
  101. public function getData($propertyName, $key)
  102. {
  103. if (!isset($this->properties[$propertyName])) {
  104. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  105. }
  106. return $this->properties[$propertyName]['data'][$key];
  107. }
  108. public function getTotalPeriod($propertyName)
  109. {
  110. if (!isset($this->properties[$propertyName])) {
  111. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  112. }
  113. return $this->properties[$propertyName]['total_period'];
  114. }
  115. public function getAverageProperties()
  116. {
  117. return $this->averageProperties;
  118. }
  119. public function getResults()
  120. {
  121. $results = array_replace($this->resultsSort, $this->properties, $this->averageProperties);
  122. return $results;
  123. }
  124. public function getAsArray()
  125. {
  126. return array(
  127. 'label' => $this->getLabels(),
  128. 'data' => $this->getResults(),
  129. );
  130. }
  131. public function getDateRange()
  132. {
  133. return new \DatePeriod(
  134. $this->getDateStart(),
  135. new \DateInterval('P1' . $this->getInterval()),
  136. $this->getDateEnd()
  137. );
  138. }
  139. public function getDateStart()
  140. {
  141. return $this->dateStart;
  142. }
  143. public function getDateEnd()
  144. {
  145. return $this->dateEnd;
  146. }
  147. public function getInterval()
  148. {
  149. return $this->interval;
  150. }
  151. public function configurePropertyOptions(OptionsResolver $resolver)
  152. {
  153. $resolver->setDefaults(
  154. [
  155. 'unit' => '',
  156. 'label' => 'Chiffre affaire produit',
  157. 'label_short' => 'CA produit',
  158. 'data' => array(),
  159. 'total_period' => 0,
  160. 'average_period' => 0,
  161. 'dividend' => null,
  162. 'divider' => null,
  163. ]
  164. );
  165. }
  166. }