Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

85 lines
2.8KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Generator;
  3. use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
  4. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  5. use Lc\CaracoleBundle\Model\Section\SectionModel;
  6. use Lc\SovBundle\Solver\Setting\SettingSolver;
  7. class OrderReferenceGenerator
  8. {
  9. protected SettingSolver $settingSolver;
  10. public function __construct(SettingSolver $settingSolver)
  11. {
  12. $this->settingSolver = $settingSolver;
  13. }
  14. public function buildReference(OrderShopInterface $orderShop): string
  15. {
  16. switch ($orderShop->getSection()->getCycleType()) {
  17. case SectionModel::CYCLE_TYPE_DAY:
  18. return $this->buildReferenceCycleDay($orderShop);
  19. case SectionModel::CYCLE_TYPE_WEEK:
  20. return $this->buildReferenceCycleWeek($orderShop);
  21. case SectionModel::CYCLE_TYPE_MONTH:
  22. return $this->buildReferenceCycleMonth($orderShop);
  23. case SectionModel::CYCLE_TYPE_YEAR:
  24. return $this->buildReferenceCycleYear($orderShop);
  25. }
  26. return 'C' . $orderShop->getId();
  27. }
  28. public function buildReferenceCycleDay(OrderShopInterface $orderShop): string
  29. {
  30. return $this->getPrefixReference($orderShop) .
  31. 'C' . $this->numberPad($orderShop->getCycleId(), 3);
  32. }
  33. public function buildReferenceCycleWeek(OrderShopInterface $orderShop): string
  34. {
  35. return $this->getPrefixReference($orderShop) .
  36. 'S' . $orderShop->getDistribution()->getCycleNumber() .
  37. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  38. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  39. }
  40. public function buildReferenceCycleMonth(OrderShopInterface $orderShop): string {
  41. return $this->getPrefixReference($orderShop) .
  42. 'M' . $orderShop->getDistribution()->getCycleNumber() .
  43. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  44. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  45. }
  46. public function buildReferenceCycleYear(OrderShopInterface $orderShop): string
  47. {
  48. return $this->getPrefixReference($orderShop) .
  49. 'C' . $this->numberPad($orderShop->getCycleId(), 5) .
  50. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  51. }
  52. public function getPrefixReference(OrderShopInterface $orderShop): string
  53. {
  54. return ''.$this->settingSolver->getSettingValue(
  55. $orderShop->getSection(),
  56. SectionSettingDefinition::SETTING_REFERENCE_PREFIX
  57. );
  58. }
  59. protected function numberPad($number, $length): string
  60. {
  61. return str_pad($number, $length, "0", STR_PAD_LEFT);
  62. }
  63. protected function formatYear(int $year):int{
  64. return substr($year, -2);
  65. }
  66. }