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.7KB

  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. 'C' . $this->numberPad($orderShop->getCycleId(), 3);
  53. }
  54. public function buildReferenceCycleWeek(OrderShopInterface $orderShop): string
  55. {
  56. return $this->getPrefixReference($orderShop) .
  57. 'S' . $orderShop->getDistribution()->getCycleNumber() .
  58. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  59. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  60. }
  61. public function buildReferenceCycleMonth(OrderShopInterface $orderShop): string {
  62. return $this->getPrefixReference($orderShop) .
  63. 'M' . $orderShop->getDistribution()->getCycleNumber() .
  64. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  65. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  66. }
  67. public function buildReferenceCycleYear(OrderShopInterface $orderShop): string
  68. {
  69. return $this->getPrefixReference($orderShop) .
  70. 'C' . $this->numberPad($orderShop->getCycleId(), 5) .
  71. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  72. }
  73. public function getPrefixReference(OrderShopInterface $orderShop): string
  74. {
  75. return ''.$this->settingSolver->getSettingValue(
  76. $orderShop->getSection(),
  77. SectionSettingDefinition::SETTING_REFERENCE_PREFIX
  78. );
  79. }
  80. protected function numberPad($number, $length): string
  81. {
  82. return str_pad($number, $length, "0", STR_PAD_LEFT);
  83. }
  84. protected function formatYear(int $year):int{
  85. return substr($year, -2);
  86. }
  87. }