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.

ProducerPriceRangeRepository.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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->loadQuery(ProducerPriceRangeRepositoryQuery::class);
  12. }
  13. /**
  14. * Retourne les options de base nécessaires à la fonction de recherche.
  15. */
  16. public function getDefaultOptionsSearch(): array
  17. {
  18. return [
  19. self::WITH => [],
  20. self::JOIN_WITH => [],
  21. self::ORDER_BY => 'range_begin ASC',
  22. self::ATTRIBUTE_ID_PRODUCER => ''
  23. ];
  24. }
  25. public function findOneProducerPriceRangeById(int $id)
  26. {
  27. return $this->createDefaultQuery()
  28. ->filterById($id)
  29. ->findOne();
  30. }
  31. public function queryProducerPriceRanges()
  32. {
  33. return $this->createDefaultQuery();
  34. }
  35. public function findProducerPriceRanges()
  36. {
  37. return $this->queryProducerPriceRanges()->find();
  38. }
  39. public function getProducerPriceRangeByTurnover(float $turnover = null): ?ProducerPriceRange
  40. {
  41. $producerPriceRangeArray = $this->findProducerPriceRanges();
  42. foreach ($producerPriceRangeArray as $producerPriceRange) {
  43. if ($turnover >= $producerPriceRange->range_begin && $turnover < $producerPriceRange->range_end) {
  44. return $producerPriceRange;
  45. }
  46. }
  47. return null;
  48. }
  49. public function getAmountToBeBilledByTurnover(float $turnover = null, $format = false)
  50. {
  51. $amountToBeBilled = 0;
  52. $producerPriceRangeArray = $this->findProducerPriceRanges();
  53. foreach ($producerPriceRangeArray as $priceRange) {
  54. if ($turnover >= $priceRange->range_begin && $turnover < $priceRange->range_end) {
  55. $amountToBeBilled = $priceRange->price;
  56. }
  57. }
  58. if ($format) {
  59. return Price::format($amountToBeBilled, 0);
  60. } else {
  61. return $amountToBeBilled;
  62. }
  63. }
  64. }