Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

190 linhas
5.9KB

  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. $this->properties[$propertyName]['data'][$key] += number_format($value, 2);
  52. $this->properties[$propertyName]['total_period'] += number_format($value, 2);
  53. } else {
  54. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  55. }
  56. }
  57. public function setAveragePropertiesData()
  58. {
  59. foreach ($this->getLabels() as $key => $label) {
  60. foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
  61. if ($this->getData($averageProperty['divider'], $key)) {
  62. $this->setAverageData(
  63. $averagePropertyName,
  64. $key,
  65. $this->getData($averageProperty['dividend'], $key) / $this->getData(
  66. $averageProperty['divider'],
  67. $key
  68. )
  69. );
  70. }
  71. if ($this->getTotalPeriod($averageProperty['divider'])) {
  72. $this->averageProperties[$averagePropertyName]['total_period'] = $this->getTotalPeriod(
  73. $averageProperty['dividend']
  74. ) / $this->getTotalPeriod($averageProperty['divider']);
  75. }
  76. }
  77. }
  78. foreach ($this->getProperties() as $propertyName => $property) {
  79. $this->properties[$propertyName]['average_period'] = number_format(
  80. $this->properties[$propertyName]['total_period'] / count($this->getLabels()),
  81. 2
  82. );
  83. }
  84. foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
  85. $this->averageProperties[$averagePropertyName]['average_period'] = number_format(
  86. $this->averageProperties[$averagePropertyName]['total_period'] / count($this->getLabels()),
  87. 2
  88. );
  89. }
  90. }
  91. public function getLabels()
  92. {
  93. return $this->labels;
  94. }
  95. public function getProperties()
  96. {
  97. return $this->properties;
  98. }
  99. public function getData($propertyName, $key)
  100. {
  101. if (!isset($this->properties[$propertyName])) {
  102. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  103. }
  104. return $this->properties[$propertyName]['data'][$key];
  105. }
  106. public function getTotalPeriod($propertyName)
  107. {
  108. if (!isset($this->properties[$propertyName])) {
  109. throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
  110. }
  111. return $this->properties[$propertyName]['total_period'];
  112. }
  113. public function getAverageProperties()
  114. {
  115. return $this->averageProperties;
  116. }
  117. public function getResults()
  118. {
  119. $results = array_replace($this->resultsSort, $this->properties, $this->averageProperties);
  120. return $results;
  121. }
  122. public function getAsArray()
  123. {
  124. return array(
  125. 'label' => $this->getLabels(),
  126. 'data' => $this->getResults(),
  127. );
  128. }
  129. public function getDateRange()
  130. {
  131. return new \DatePeriod(
  132. $this->getDateStart(),
  133. new \DateInterval('P1' . $this->getInterval()),
  134. $this->getDateEnd()
  135. );
  136. }
  137. public function getDateStart()
  138. {
  139. return $this->dateStart;
  140. }
  141. public function getDateEnd()
  142. {
  143. return $this->dateEnd;
  144. }
  145. public function getInterval()
  146. {
  147. return $this->interval;
  148. }
  149. public function configurePropertyOptions(OptionsResolver $resolver)
  150. {
  151. $resolver->setDefaults(
  152. [
  153. 'unit' => '',
  154. 'label' => 'Chiffre affaire produit',
  155. 'label_short' => 'CA produit',
  156. 'data' => array(),
  157. 'total_period' => 0,
  158. 'average_period' => 0,
  159. 'dividend' => null,
  160. 'divider' => null,
  161. ]
  162. );
  163. }
  164. }