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.

108 line
3.5KB

  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. $complementaryIndex = null;
  17. if ($orderShop->getMainOrderShop()) {
  18. $complementaryIndex = 1;
  19. foreach ($orderShop->getMainOrderShop()->getComplementaryOrderShops() as $complementaryOrder) {
  20. if ($complementaryOrder->isValid()) $complementaryIndex++;
  21. }
  22. $orderShop = $complementaryOrder->getMainOrderShop();
  23. }
  24. switch ($orderShop->getSection()->getCycleType()) {
  25. case SectionModel::CYCLE_TYPE_DAY:
  26. $reference = $this->buildReferenceCycleDay($orderShop);
  27. break;
  28. case SectionModel::CYCLE_TYPE_WEEK:
  29. $reference = $this->buildReferenceCycleWeek($orderShop,);
  30. break;
  31. case SectionModel::CYCLE_TYPE_MONTH:
  32. $reference = $this->buildReferenceCycleMonth($orderShop);
  33. break;
  34. case SectionModel::CYCLE_TYPE_YEAR:
  35. $reference = $this->buildReferenceCycleYear($orderShop);
  36. break;
  37. default:
  38. $reference = 'C' . $orderShop->getId();
  39. break;
  40. }
  41. if($complementaryIndex){
  42. $reference = $reference.'C'.$this->numberPad($complementaryIndex, 1);
  43. }
  44. return $reference;
  45. }
  46. public function buildReferenceCycleDay(OrderShopInterface $orderShop): string
  47. {
  48. return $this->getPrefixReference($orderShop) .
  49. 'C' . $this->numberPad($orderShop->getCycleId(), 3);
  50. }
  51. public function buildReferenceCycleWeek(OrderShopInterface $orderShop): string
  52. {
  53. return $this->getPrefixReference($orderShop) .
  54. 'S' . $orderShop->getDistribution()->getCycleNumber() .
  55. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  56. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  57. }
  58. public function buildReferenceCycleMonth(OrderShopInterface $orderShop): string {
  59. return $this->getPrefixReference($orderShop) .
  60. 'M' . $orderShop->getDistribution()->getCycleNumber() .
  61. 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
  62. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  63. }
  64. public function buildReferenceCycleYear(OrderShopInterface $orderShop): string
  65. {
  66. return $this->getPrefixReference($orderShop) .
  67. 'C' . $this->numberPad($orderShop->getCycleId(), 5) .
  68. 'A' . $this->formatYear($orderShop->getDistribution()->getYear());
  69. }
  70. public function getPrefixReference(OrderShopInterface $orderShop): string
  71. {
  72. return ''.$this->settingSolver->getSettingValue(
  73. $orderShop->getSection(),
  74. SectionSettingDefinition::SETTING_REFERENCE_PREFIX
  75. );
  76. }
  77. protected function numberPad($number, $length): string
  78. {
  79. return str_pad($number, $length, "0", STR_PAD_LEFT);
  80. }
  81. protected function formatYear(int $year):int{
  82. return substr($year, -2);
  83. }
  84. }