1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27declare(strict_types=1);
28
29namespace PrestaShopBundle\Form\Admin\Sell\Product\Options;
30
31use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex;
32use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\Reference;
33use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
34use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
35use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
36use Symfony\Component\Form\Extension\Core\Type\HiddenType;
37use Symfony\Component\Form\Extension\Core\Type\MoneyType;
38use Symfony\Component\Form\Extension\Core\Type\TextType;
39use Symfony\Component\Form\FormBuilderInterface;
40use Symfony\Component\Translation\TranslatorInterface;
41use Symfony\Component\Validator\Constraints\Length;
42use Symfony\Component\Validator\Constraints\NotBlank;
43use Symfony\Component\Validator\Constraints\Type;
44
45class ProductSupplierType extends TranslatorAwareType
46{
47    /**
48     * @var FormChoiceProviderInterface
49     */
50    private $currencyByIdChoiceProvider;
51
52    /**
53     * @var string
54     */
55    private $currencyIsoCode;
56
57    /**
58     * @param TranslatorInterface $translator
59     * @param array $locales
60     * @param FormChoiceProviderInterface $currencyByIdChoiceProvider
61     * @param string $currencyIsoCode
62     */
63    public function __construct(
64        TranslatorInterface $translator,
65        array $locales,
66        FormChoiceProviderInterface $currencyByIdChoiceProvider,
67        string $currencyIsoCode
68    ) {
69        parent::__construct($translator, $locales);
70        $this->currencyByIdChoiceProvider = $currencyByIdChoiceProvider;
71        $this->currencyIsoCode = $currencyIsoCode;
72    }
73
74    /**
75     * {@inheritDoc}
76     */
77    public function buildForm(FormBuilderInterface $builder, array $options)
78    {
79        $builder
80            ->add('supplier_id', HiddenType::class, [
81                'required' => true,
82            ])
83            ->add('supplier_name', HiddenType::class, [
84                'label' => $this->trans('Supplier', 'Admin.Global'),
85                'required' => false,
86            ])
87            ->add('product_supplier_id', HiddenType::class, [
88                'required' => false,
89            ])
90            ->add('reference', TextType::class, [
91                'label' => $this->trans('Supplier reference', 'Admin.Catalog.Feature'),
92                'constraints' => [
93                    new TypedRegex(TypedRegex::TYPE_REFERENCE),
94                    new Length([
95                        'max' => Reference::MAX_LENGTH,
96                    ]),
97                ],
98            ])
99            ->add('price_tax_excluded', MoneyType::class, [
100                'label' => $this->trans('Cost price (tax excl.)', 'Admin.Catalog.Feature'),
101                'currency' => $this->currencyIsoCode,
102                'scale' => self::PRESTASHOP_DECIMALS,
103                'attr' => ['data-display-price-precision' => self::PRESTASHOP_DECIMALS],
104                'constraints' => [
105                    new NotBlank(),
106                    new Type(['type' => 'float']),
107                ],
108                'default_empty_data' => 0.0,
109            ])
110            ->add('currency_id', ChoiceType::class, [
111                'label' => $this->trans('Currency', 'Admin.Global'),
112                'required' => false,
113                // placeholder false is important to avoid empty option in select input despite required being false
114                'placeholder' => false,
115                'choices' => $this->currencyByIdChoiceProvider->getChoices(),
116            ])
117        ;
118    }
119}
120