* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ declare(strict_types=1); namespace PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataProvider; use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface; use PrestaShop\PrestaShop\Core\Domain\Product\Combination\ValueObject\NoCombinationId; use PrestaShop\PrestaShop\Core\Domain\Product\SpecificPrice\Query\GetSpecificPriceForEditing; use PrestaShop\PrestaShop\Core\Domain\Product\SpecificPrice\QueryResult\SpecificPriceForEditing; use PrestaShop\PrestaShop\Core\Domain\Product\SpecificPrice\ValueObject\InitialPrice; use PrestaShop\PrestaShop\Core\Domain\ValueObject\Reduction; use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime; class SpecificPriceFormDataProvider implements FormDataProviderInterface { /** * @var CommandBusInterface */ private $queryBus; /** * @var int */ private $contextShopId; /** * @param CommandBusInterface $queryBus * @param int $contextShopId */ public function __construct( CommandBusInterface $queryBus, int $contextShopId ) { $this->queryBus = $queryBus; $this->contextShopId = $contextShopId; } /** * @param int $id * * @return array */ public function getData($id): array { /** @var SpecificPriceForEditing $specificPriceForEditing */ $specificPriceForEditing = $this->queryBus->handle(new GetSpecificPriceForEditing((int) $id)); $fixedPrice = $specificPriceForEditing->getFixedPrice()->getValue(); $data = [ 'product_id' => $specificPriceForEditing->getProductId(), 'groups' => [ 'currency_id' => $specificPriceForEditing->getCurrencyId(), 'country_id' => $specificPriceForEditing->getCountryId(), 'group_id' => $specificPriceForEditing->getGroupId(), 'shop_id' => $specificPriceForEditing->getShopId(), ], 'combination_id' => $specificPriceForEditing->getCombinationId(), 'from_quantity' => $specificPriceForEditing->getFromQuantity(), 'date_range' => [ 'from' => $specificPriceForEditing->getDateTimeFrom()->format(DateTime::DEFAULT_DATETIME_FORMAT), 'to' => $specificPriceForEditing->getDateTimeTo()->format(DateTime::DEFAULT_DATETIME_FORMAT), ], 'impact' => [ 'reduction' => [ 'type' => $specificPriceForEditing->getReductionType(), 'value' => (float) (string) $specificPriceForEditing->getReductionAmount(), 'include_tax' => $specificPriceForEditing->includesTax(), ], 'fixed_price_tax_excluded' => (float) (string) $fixedPrice, ], ]; if ($customerInfo = $specificPriceForEditing->getCustomerInfo()) { $data['customer'] = [ [ 'id_customer' => $customerInfo->getId(), 'fullname_and_email' => sprintf( '%s %s - %s', $customerInfo->getFirstname(), $customerInfo->getLastname(), $customerInfo->getEmail() ), ], ]; } return $data; } /** * @return array */ public function getDefaultData(): array { return [ 'from_quantity' => 1, 'impact' => [ 'reduction' => [ 'type' => Reduction::TYPE_AMOUNT, 'value' => 0, 'include_tax' => true, ], 'fixed_price_tax_excluded' => (float) InitialPrice::INITIAL_PRICE_VALUE, ], 'groups' => [ 'shop_id' => $this->contextShopId, ], 'combination_id' => NoCombinationId::NO_COMBINATION_ID, ]; } }