Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert default address type #1440

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 80 additions & 12 deletions src/CoreShop/Bundle/CoreBundle/Form/Type/Checkout/AddressType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
use CoreShop\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use CoreShop\Component\Address\Formatter\AddressFormatterInterface;
use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Core\Model\CustomerInterface;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

final class AddressType extends AbstractResourceType
Expand All @@ -32,28 +36,53 @@ final class AddressType extends AbstractResourceType
private $addressFormatHelper;

/**
* @param string $dataClass FQCN
* @var TranslatorInterface
*/
private $translator;

/**
* @param string $dataClass
* @param string[] $validationGroups
* @param AddressFormatterInterface $addressFormatHelper
* @param TranslatorInterface $translator
*/
public function __construct(
$dataClass,
array $validationGroups,
AddressFormatterInterface $addressFormatHelper
AddressFormatterInterface $addressFormatHelper,
TranslatorInterface $translator
) {
parent::__construct($dataClass, $validationGroups);

$this->addressFormatHelper = $addressFormatHelper;
$this->translator = $translator;
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$defaultShippingAddress = null;
$defaultInvoiceAddress = null;

if ($options['customer']->getDefaultAddress() instanceof AddressInterface) {
/** @var AddressInterface $address */
$address = $options['customer']->getDefaultAddress();
$addressIdentifier = $address->getAddressIdentifier();

if (null === $addressIdentifier) {
$defaultShippingAddress = $address;
$defaultInvoiceAddress = $address;
} else {
$defaultShippingAddress = $addressIdentifier->getName() === 'shipping' ? $address : null;
$defaultInvoiceAddress = $addressIdentifier->getName() === 'invoice' ? $address : null;
}
}

$builder
->add('shippingAddress', AddressChoiceType::class, [
'constraints' => [new NotBlank()],
'constraints' => [new NotBlank(['groups' => $this->validationGroups])],
'customer' => $options['customer']->getId(),
'label' => 'coreshop.form.address.shipping',
'allowed_address_identifier' => [null, 'shipping'],
Expand All @@ -67,10 +96,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)

return [];
},
'empty_data' => $options['customer']->getDefaultAddress(),
'empty_data' => $defaultShippingAddress,
])
->add('invoiceAddress', AddressChoiceType::class, [
'constraints' => [new NotBlank()],
'constraints' => [new NotBlank(['groups' => $this->validationGroups])],
'customer' => $options['customer']->getId(),
'label' => 'coreshop.form.address.invoice',
'allowed_address_identifier' => [null, 'invoice'],
Expand All @@ -84,28 +113,67 @@ public function buildForm(FormBuilderInterface $builder, array $options)

return [];
},
'empty_data' => $options['customer']->getDefaultAddress(),
'empty_data' => $defaultInvoiceAddress,
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event) {
/** @var CartInterface $cart */
$cart = $event->getData();
$checkboxData = true;
if ($cart->getShippingAddress() instanceof AddressInterface && $cart->getInvoiceAddress() instanceof AddressInterface) {
if ($cart->getShippingAddress()->getId() !== $cart->getInvoiceAddress()->getId()) {
$checkboxDisabled = false;

if ($event->getForm()->has('shippingAddress') &&
$event->getForm()->get('shippingAddress')->getConfig()->hasOption('choices')
) {
$choiceList = $event->getForm()->get('shippingAddress')->getConfig()->getOption('choices');

if (!is_array($choiceList) || count($choiceList) === 0) {
$checkboxData = null;
$checkboxDisabled = true;
}
}

if ($cart->getShippingAddress() instanceof AddressInterface &&
$cart->getInvoiceAddress() instanceof AddressInterface &&
$cart->getShippingAddress()->getId() !== $cart->getInvoiceAddress()->getId()
) {
$checkboxData = null;
}

$event->getForm()->add('useInvoiceAsShipping', CheckboxType::class, [
'required' => false,
'mapped' => false,
'disabled' => $checkboxDisabled,
'label' => 'coreshop.form.address.use_invoice_as_shipping',
'data' => $checkboxData,
]);
})
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$formData = $event->getData();
if (isset($formData['invoiceAddress']) && (isset($formData['useInvoiceAsShipping']) && '1' === $formData['useInvoiceAsShipping'])) {
$formData['shippingAddress'] = $formData['invoiceAddress'];
$event->setData($formData);

if (isset($formData['invoiceAddress'], $formData['useInvoiceAsShipping']) && '1' === $formData['useInvoiceAsShipping']) {
$valid = true;

if ($event->getForm()->has('shippingAddress') &&
$event->getForm()->get('shippingAddress')->getConfig()->hasOption('choices')
) {
$invoiceAddressId = $formData['invoiceAddress'];
$choiceList = $event->getForm()->get('shippingAddress')->getConfig()->getOption('choices');

if (is_array($choiceList) && count($choiceList) > 0) {
$valid = count(array_filter($choiceList, static function (AddressInterface $address) use ($invoiceAddressId) {
return $address->getId() === (int) $invoiceAddressId;
})) > 0;
}
}

if ($valid === true) {
$formData['shippingAddress'] = $formData['invoiceAddress'];
$event->setData($formData);
} else {
$message = $this->translator->trans('coreshop.checkout.address.invoice_as_shipping_invalid');
$event->getForm()->addError(new FormError($message));
}

}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ services:
- '%coreshop.model.cart.class%'
- '%coreshop.form.type.checkout.address.validation_groups%'
- '@coreshop.address.formatter'
- '@translator'
tags:
- {name: form.type}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@ coreshop_customer_transformer_assignment_form_success: 'Customer has been succes
coreshop_customer_transformer_assignment_form_new_company_title: 'New Company Data'
coreshop_customer_transformer_assignment_new_form_button: 'Create Company and assign Customer'
coreshop_customer_transformer_assignment_new_form_description: 'Once this form is submitted, a new company will be created. Please check the data carefully. In addition, you must define whether the customer addresses are to be retained by the customer or transferred to the new company.'
coreshop_customer_transformer_assignment_new_form_maybe_duplicates_title: 'Similar Companies'
coreshop_customer_transformer_assignment_new_form_maybe_duplicates_title: 'Similar Companies'
Original file line number Diff line number Diff line change
Expand Up @@ -1573,3 +1573,10 @@ translations:
en: 'Total to be paid'
de: 'Zu zahlender Betrag'
it: 'Totale da pagare'

coreshop.checkout.address.invoice_as_shipping_invalid:
languages:
de_CH: 'Die Verwendung der Rechnungsadresse als Versandadresse ist hier nicht möglich. Haben Sie eine Adresse gewählt die strikt als Rechnungsadresse deklariert ist?'
de: 'Die Verwendung der Rechnungsadresse als Versandadresse ist hier nicht möglich. Haben Sie eine Adresse gewählt die strikt als Rechnungsadresse deklariert ist?'
en: 'Using the invoice address as shipping address is not possible. Maybe your selected invoice address is a strict type of invoice address?'
it: ''
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,20 @@ $(document).ready(function () {
$shippingAddAddressButton = $shippingPanel.parent().find('.card-footer'),
$useIasS = $addressStep.find('[name="useInvoiceAsShipping"]');

if ($invoiceAddress.find('option:selected').length) {
var address = $invoiceAddress.find('option:selected').data('address');
if (address) {
$invoicePanel.html(address.html);
}
}

if ($shippingAddress.find('option:selected').length) {
var address = $shippingAddress.find('option:selected').data('address');
if (address) {
$shippingPanel.html(address.html);
}
}

$invoiceAddress.on('change', function () {
var address = $(this).find('option:selected').data('address');
var selected = $(this).find('option:selected');
var address = selected.data('address');
var addressType = selected.data('address-type');

if ($useIasS) {
if (addressType === 'invoice') {
$useIasS.prop("disabled", true);
$useIasS.prop("checked", false);
$useIasS.change();
} else {
$useIasS.prop("disabled", false);
}
}

if (address) {
address = address.html;
Expand Down Expand Up @@ -144,6 +142,32 @@ $(document).ready(function () {
}
}
});

if ($invoiceAddress.find('option:selected').length) {
var address = $invoiceAddress.find('option:selected').data('address');
var addressType = $invoiceAddress.find('option:selected').data('address-type');

if ($useIasS) {
if (addressType === 'invoice') {
$useIasS.prop("disabled", true);
$useIasS.prop("checked", false);
$useIasS.change();
} else {
$useIasS.prop("disabled", false);
}
}

if (address) {
$invoicePanel.html(address.html);
}
}

if ($shippingAddress.find('option:selected').length) {
var address = $shippingAddress.find('option:selected').data('address');
if (address) {
$shippingPanel.html(address.html);
}
}
};

}(window.shop = window.shop || {}, jQuery));
}(window.shop = window.shop || {}, jQuery));
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</div>

{% if hasShippableItems %}
<div class="col-12 col-sm-6 shipping-address-selector"
style="{% if hasShippableItems == true and form.useInvoiceAsShipping.vars.data %} display:none; {% endif %}">
{% set hideShipmentSelector = form.shippingAddress.vars.choices|length > 0 and form.useInvoiceAsShipping is defined and form.useInvoiceAsShipping.vars.data and form.shippingAddress.vars.valid != false %}
<div class="col-12 col-sm-6 shipping-address-selector" style="{% if hideShipmentSelector == true %} display:none; {% endif %}">
{{ form_row(form.shippingAddress) }}
</div>
{% endif %}
Expand Down