您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

173 行
6.2KB

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