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.

111 lines
3.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Generator;
  3. use App\Solver\Order\OrderShopSolver;
  4. use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
  5. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  6. use Lc\CaracoleBundle\Model\Section\SectionModel;
  7. use Lc\SovBundle\Solver\Setting\SettingSolver;
  8. class OrderReferenceGenerator
  9. {
  10. protected SettingSolver $settingSolver;
  11. protected OrderShopSolver $orderShopSolver;
  12. public function __construct(SettingSolver $settingSolver, OrderShopSolver $orderShopSolver)
  13. {
  14. $this->settingSolver = $settingSolver;
  15. $this->orderShopSolver= $orderShopSolver;
  16. }
  17. public function buildReference(OrderShopInterface $orderShop): string
  18. {
  19. $complementaryIndex = null;
  20. if ($orderShop->getMainOrderShop()) {
  21. $complementaryIndex = 0;
  22. foreach ($orderShop->getMainOrderShop()->getComplementaryOrderShops() as $complementaryOrder) {
  23. if ($this->orderShopSolver->isValid($complementaryOrder)) $complementaryIndex++;
  24. }
  25. $orderShop = $complementaryOrder->getMainOrderShop();
  26. }
  27. switch ($orderShop->getSection()->getCycleType()) {
  28. case SectionModel::CYCLE_TYPE_DAY:
  29. $reference = $this->buildReferenceCycleDay($orderShop);
  30. break;
  31. case SectionModel::CYCLE_TYPE_WEEK:
  32. $reference = $this->buildReferenceCycleWeek($orderShop,);
  33. break;
  34. case SectionModel::CYCLE_TYPE_MONTH:
  35. $reference = $this->buildReferenceCycleMonth($orderShop);
  36. break;
  37. case SectionModel::CYCLE_TYPE_YEAR:
  38. $reference = $this->buildReferenceCycleYear($orderShop);
  39. break;
  40. default:
  41. $reference = 'C' . $orderShop->getId();
  42. break;
  43. }
  44. if($complementaryIndex) {
  45. $reference = $reference.'C'.$this->numberPad($complementaryIndex, 1);
  46. }
  47. return $reference;
  48. }
  49. public function buildReferenceCycleDay(OrderShopInterface $orderShop): string
  50. {
  51. return $this->getPrefixReference($orderShop) .
  52. 'J' . $orderShop->getDistribution()->getCycleNumber() .
  53. 'C' . $this->numberPad($orderShop->getCycleId(), 3) .
  54. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  55. }
  56. public function buildReferenceCycleWeek(OrderShopInterface $orderShop): string
  57. {
  58. return $this->getPrefixReference($orderShop) .
  59. 'S' . $orderShop->getDistribution()->getCycleNumber() .
  60. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  61. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  62. }
  63. public function buildReferenceCycleMonth(OrderShopInterface $orderShop): string {
  64. return $this->getPrefixReference($orderShop) .
  65. 'M' . $orderShop->getDistribution()->getCycleNumber() .
  66. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  67. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  68. }
  69. public function buildReferenceCycleYear(OrderShopInterface $orderShop): string
  70. {
  71. return $this->getPrefixReference($orderShop) .
  72. 'C' . $this->numberPad($orderShop->getCycleId(), 5) .
  73. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  74. }
  75. public function getPrefixReference(OrderShopInterface $orderShop): string
  76. {
  77. return ''.$this->settingSolver->getSettingValue(
  78. $orderShop->getSection(),
  79. SectionSettingDefinition::SETTING_REFERENCE_PREFIX
  80. );
  81. }
  82. protected function numberPad($number, $length): string
  83. {
  84. return str_pad($number, $length, "0", STR_PAD_LEFT);
  85. }
  86. protected function formatYear(int $year):int{
  87. return substr($year, -2);
  88. }
  89. }