|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
-
- namespace Lc\CaracoleBundle\Generator;
-
- use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Section\SectionModel;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
-
- class OrderReferenceGenerator
- {
- protected SettingSolver $settingSolver;
-
- public function __construct(SettingSolver $settingSolver)
- {
- $this->settingSolver = $settingSolver;
- }
-
- public function buildReference(OrderShopInterface $orderShop, \DateTime $distributionDate = null): string
- {
- switch ($orderShop->getSection()->getCycle()) {
- case SectionModel::CYCLE_TYPE_DAY:
- return $this->buildReferenceCycleDay($orderShop);
- case SectionModel::CYCLE_TYPE_WEEK:
- return $this->buildReferenceCycleWeek($orderShop, $distributionDate);
- case SectionModel::CYCLE_TYPE_MONTH:
- return $this->buildReferenceCycleMonth($orderShop, $distributionDate);
- case SectionModel::CYCLE_TYPE_YEAR:
- return $this->buildReferenceCycleYear($orderShop, $distributionDate);
- }
-
- return 'C' . $orderShop->getId();
- }
-
- public function buildReferenceCycleDay(OrderShopInterface $orderShop): string
- {
- return $this->getPrefixReference($orderShop) .
- 'C' . $this->numberPad($orderShop->getCycleId(), 3);
- }
-
- public function buildReferenceCycleWeek(OrderShopInterface $orderShop, \DateTime $distributionDate): string
- {
- return $this->getPrefixReference($orderShop) .
- 'S' . $distributionDate->format('W') .
- 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
- 'A' . $distributionDate->format('y');
- }
-
- public function buildReferenceCycleMonth(OrderShopInterface $orderShop, \DateTime $distributionDate): string
- {
- return $this->getPrefixReference($orderShop) .
- 'M' . $distributionDate->format('m') .
- 'C' . $this->numberPad($orderShop->getCycleId(), 4) .
- 'A' . $distributionDate->format('y');
- }
-
- public function buildReferenceCycleYear(OrderShopInterface $orderShop, \DateTime $distributionDate): string
- {
- return $this->getPrefixReference($orderShop) .
- 'C' . $this->numberPad($orderShop->getCycleId(), 5) .
- 'A' . $distributionDate->format('y');
- }
-
- public function getPrefixReference(OrderShopInterface $orderShop): string
- {
- return $this->settingSolver->getSettingValue($orderShop->getSection(), SectionSettingDefinition::SETTING_REFERENCE_PREFIX);
- }
-
- public function numberPad($number, $length): string
- {
- return str_pad($number, $length, "0", STR_PAD_LEFT);
- }
-
- }
|