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.

65 lines
1.7KB

  1. <?php
  2. namespace common\logic\Producer\ProducerPriceRange\Repository;
  3. use common\helpers\Price;
  4. use common\logic\AbstractRepository;
  5. use common\logic\Producer\ProducerPriceRange\Model\ProducerPriceRange;
  6. class ProducerPriceRangeRepository extends AbstractRepository
  7. {
  8. protected ProducerPriceRangeRepositoryQuery $query;
  9. public function loadDependencies(): void
  10. {
  11. $this->query = $this->loadService(ProducerPriceRangeRepositoryQuery::class);
  12. }
  13. /**
  14. * Retourne les options de base nécessaires à la fonction de recherche.
  15. *
  16. */
  17. public function getDefaultOptionsSearch(): array
  18. {
  19. return [
  20. 'with' => [],
  21. 'join_with' => [],
  22. 'orderby' => '',
  23. 'attribute_id_producer' => ''
  24. ];
  25. }
  26. public function findOneProducerPriceRangeById(int $id)
  27. {
  28. return ProducerPriceRange::searchOne([
  29. 'id' => $id
  30. ]);
  31. }
  32. public function queryProducerPriceRanges()
  33. {
  34. return ProducerPriceRange::find()->orderBy('range_begin ASC');
  35. }
  36. public function findProducerPriceRanges()
  37. {
  38. return $this->queryProducerPriceRanges()->all();
  39. }
  40. public function getAmountToBeBilledByTurnover(float $turnover = null, $format = false)
  41. {
  42. $amountToBeBilled = 0;
  43. $producerPriceRangeArray = $this->findProducerPriceRanges();
  44. foreach ($producerPriceRangeArray as $priceRange) {
  45. if ($turnover >= $priceRange->range_begin && $turnover < $priceRange->range_end) {
  46. $amountToBeBilled = $priceRange->price;
  47. }
  48. }
  49. if ($format) {
  50. return Price::format($amountToBeBilled, 0);
  51. } else {
  52. return $amountToBeBilled;
  53. }
  54. }
  55. }