Skip to content

Commit

Permalink
add vat number field to channel billing data
Browse files Browse the repository at this point in the history
  • Loading branch information
gewebe committed Jan 28, 2024
1 parent bcb9c10 commit 47c6b6d
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 6 deletions.
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Quality Score][ico-code-quality]][link-code-quality]

## Features
* New fields for VAT number and validation status at `Address` entity
* New fields for VAT number at `Address` and `ShopBillingData` entity
* Configure VAT number field requirement:
* Optional / Required
* Required if customer filled “Company” field
Expand Down Expand Up @@ -62,8 +62,6 @@ cp -R vendor/gewebe/sylius-vat-plugin/templates/SyliusShopBundle/* templates/bun

### Extend `Address` entity

- If you use `annotations` mapping:

```php
# src/Entity/Addressing/Address.php
Expand All @@ -83,8 +81,7 @@ class Address extends BaseAddress implements VatNumberAddressInterface
use VatNumberAwareTrait;
```

- If you use `yaml` mapping add also:

If you use `yaml` mapping add also:
```yaml
# config/doctrine/Address.orm.yaml
Expand All @@ -105,6 +102,41 @@ App\Entity\Addressing\Address:
nullable: true
```

### Extend `ShopBillingData` entity

```php
# src/Entity/Channel/ShopBillingData.php
namespace App\Entity\Channel;
use Doctrine\ORM\Mapping as ORM;
use Gewebe\SyliusVATPlugin\Entity\ShopBillingDataVatNumberAwareTrait;
use Gewebe\SyliusVATPlugin\Entity\ShopBillingDataVatNumberInterface;
use Sylius\Component\Core\Model\ShopBillingData as BaseShopBillingData;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_shop_billing_data")
*/
class ShopBillingData extends BaseShopBillingData implements ShopBillingDataVatNumberInterface
{
use ShopBillingDataVatNumberAwareTrait;
```

If you use `yaml` mapping add also:
```yaml
# config/doctrine/ShopBillingData.orm.yaml
App\Entity\Channel\ShopBillingData:
type: entity
table: sylius_shop_billing_data
fields:
vatNumber:
type: string
column: vat_number
nullable: true
```

### Update your database schema

```bash
Expand Down
8 changes: 8 additions & 0 deletions config/doctrine/ShopBillingData.orm.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
App\Entity\Channel\ShopBillingData:
type: entity
table: sylius_shop_billing_data
fields:
vatNumber:
type: string
column: vat_number
nullable: true
5 changes: 5 additions & 0 deletions config/services/form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ services:
- '%gewebe_sylius_vat_plugin.required.default%'
tags:
- { name: form.type_extension, extended_type: Sylius\Bundle\AddressingBundle\Form\Type\AddressType }

gewebe_sylius_vat_plugin.form.extension.shop_billing_data:
class: Gewebe\SyliusVATPlugin\Form\Extension\ShopBillingDataTypeExtension
tags:
- { name: form.type_extension, extended_type: Sylius\Bundle\CoreBundle\Form\Type\ShopBillingDataType }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@channel_vat_number
Feature: Editing shop billing data with vat number on channel
In order to have proper shop billing data with vat number on shop-related documents
As an Administrator
I want to be able to edit shop billing data with vat number on a channel

Background:
Given the store operates on a channel named "Web"
And the store ships to "Austria"
And channel "Web" billing data is "Joda", "Burgring 1", "1010" "Vienna", "Austria" with "123" tax ID, "ATU" VAT No.
And I am logged in as an administrator

@ui
Scenario: Editing shop billing data with vat number on channel
When I want to modify a channel "Web"
And I specify company as "Jodeliti"
And I specify tax ID as "99-999/9999"
And I specify VAT number as "ATU12345678"
And I specify shop billing address as "Opernring 1", "8010" "Graz", "Austria"
And I save my changes
Then I should be notified that it has been successfully edited
And this channel company should be "Jodeliti"
And this channel tax ID should be "99-999/9999"
And this channel VAT number should be "ATU12345678"
And this channel shop billing address should be "Opernring 1", "8010" "Graz", "Austria"
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Feature: Seeing tax total dependent on country and vat

# channel
And the store operates on a channel named "Web"
And channel "Web" billing data is "YourShirtShop", "Rue Belliard", "1000" "Brussels", "Belgium" with "1234567890" tax ID
And channel "Web" billing data is "ShirtShop", "Rue Belliard", "1000" "Brussels", "Belgium" with "123" tax ID, "BE199" VAT No.
And default tax zone is "BE"

# taxes
Expand Down
32 changes: 32 additions & 0 deletions spec/Entity/ShopBillingDataSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace spec\Gewebe\SyliusVATPlugin\Entity;

use Gewebe\SyliusVATPlugin\Entity\ShopBillingData;
use Gewebe\SyliusVATPlugin\Entity\ShopBillingDataVatNumberInterface;
use PhpSpec\ObjectBehavior;

final class ShopBillingDataSpec extends ObjectBehavior
{
function it_is_address()
{
$this->shouldHaveType(ShopBillingData::class);
}

function it_implements_address_interface(): void
{
$this->shouldImplement(ShopBillingDataVatNumberInterface::class);
}

function it_has_vat_number(): void
{
$this->getVatNumber()->shouldReturn(null);
$this->hasVatNumber()->shouldReturn(false);

$this->setVatNumber('ATU12345678');
$this->getVatNumber()->shouldReturn('ATU12345678');
$this->hasVatNumber()->shouldReturn(true);
}
}
19 changes: 19 additions & 0 deletions src/Entity/ShopBillingData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Gewebe\SyliusVATPlugin\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\ShopBillingData as BaseShopBillingData;

/**
* Example ShopBillingData entity with vat number implemented as trait
*
* #@ORM\Entity
* #@ORM\Table(name="sylius_shop_billing_data")
*/
class ShopBillingData extends BaseShopBillingData implements ShopBillingDataVatNumberInterface
{
use ShopBillingDataVatNumberAwareTrait;
}
38 changes: 38 additions & 0 deletions src/Entity/ShopBillingDataVatNumberAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Gewebe\SyliusVATPlugin\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* Trait that implements the shop billing data vat number functionality
* Used in:
* <li>@see ShopBillingData</li>
*/
trait ShopBillingDataVatNumberAwareTrait
{
/**
* @ORM\Column(name="vat_number", type="string", nullable=true)
*
* @Groups({"admin:shop_billing_data:read"})
*/
protected ?string $vatNumber = null;

public function getVatNumber(): ?string
{
return $this->vatNumber;
}

public function setVatNumber(?string $vatNumber): void
{
$this->vatNumber = $vatNumber;
}

public function hasVatNumber(): bool
{
return is_string($this->vatNumber) && strlen($this->vatNumber) > 0;
}
}
19 changes: 19 additions & 0 deletions src/Entity/ShopBillingDataVatNumberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Gewebe\SyliusVATPlugin\Entity;

use Sylius\Component\Core\Model\ShopBillingDataInterface;

/**
* ShopBillingData model with vat number
*/
interface ShopBillingDataVatNumberInterface extends ShopBillingDataInterface
{
public function getVatNumber(): ?string;

public function setVatNumber(?string $vatNumber): void;

public function hasVatNumber(): bool;
}
32 changes: 32 additions & 0 deletions src/Form/Extension/ShopBillingDataTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Gewebe\SyliusVATPlugin\Form\Extension;

use Sylius\Bundle\CoreBundle\Form\Type\ShopBillingDataType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class ShopBillingDataTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add(
'vatNumber',
TextType::class,
[
'label' => 'gewebe_sylius_vat_plugin.ui.vat_number',
'required' => false,
],
);
}

public static function getExtendedTypes(): array
{
return [
ShopBillingDataType::class,
];
}
}
6 changes: 6 additions & 0 deletions tests/Application/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ sylius_addressing:
address:
classes:
model: Tests\Gewebe\SyliusVATPlugin\Application\src\Entity\Addressing\Address

sylius_core:
resources:
shop_billing_data:
classes:
model: Tests\Gewebe\SyliusVATPlugin\Application\src\Entity\Channel\ShopBillingData
6 changes: 6 additions & 0 deletions tests/Application/config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ sylius_addressing:
address:
classes:
model: Tests\Gewebe\SyliusVATPlugin\Application\src\Entity\Addressing\Address

sylius_core:
resources:
shop_billing_data:
classes:
model: Tests\Gewebe\SyliusVATPlugin\Application\src\Entity\Channel\ShopBillingData
21 changes: 21 additions & 0 deletions tests/Application/src/Entity/Channel/ShopBillingData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Gewebe\SyliusVATPlugin\Application\src\Entity\Channel;

use Doctrine\ORM\Mapping as ORM;
use Gewebe\SyliusVATPlugin\Entity\ShopBillingDataVatNumberAwareTrait;
use Gewebe\SyliusVATPlugin\Entity\ShopBillingDataVatNumberInterface;
use Sylius\Component\Core\Model\ShopBillingData as BaseShopBillingData;

/**
* ShopBillingData entity with vat number implemented as trait
*
* @ORM\Entity
* @ORM\Table(name="sylius_shop_billing_data")
*/
class ShopBillingData extends BaseShopBillingData implements ShopBillingDataVatNumberInterface
{
use ShopBillingDataVatNumberAwareTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h4 class="ui top attached large header">{{ form_label(form.shopBillingData) }}</h4>
<div class="ui attached segment">
<div class="three fields">
{{ form_row(form.shopBillingData.company) }}
{{ form_row(form.shopBillingData.taxId) }}
{{ form_row(form.shopBillingData.vatNumber) }}
</div>
<div class="two fields">
{{ form_row(form.shopBillingData.countryCode) }}
{{ form_row(form.shopBillingData.street) }}
</div>
<div class="two fields">
{{ form_row(form.shopBillingData.city) }}
{{ form_row(form.shopBillingData.postcode) }}
</div>
</div>
46 changes: 46 additions & 0 deletions tests/Behat/Context/Setup/ChannelContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Tests\Gewebe\SyliusVATPlugin\Behat\Context\Setup;

use Behat\Behat\Context\Context;
use Doctrine\Persistence\ObjectManager;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Tests\Gewebe\SyliusVATPlugin\Application\src\Entity\Channel\ShopBillingData;

final class ChannelContext implements Context
{
public function __construct(
private ObjectManager $channelManager,
) {
}

/**
* @Given channel :channel billing data is :company, :street, :postcode :city, :country with :taxId tax ID, :vatNumber VAT No.
*/
public function channelBillingDataWithVatNumberIs(
ChannelInterface $channel,
string $company,
string $street,
string $postcode,
string $city,
CountryInterface $country,
string $taxId,
string $vatNumber,
): void {
$shopBillingData = new ShopBillingData();
$shopBillingData->setCompany($company);
$shopBillingData->setStreet($street);
$shopBillingData->setPostcode($postcode);
$shopBillingData->setCity($city);
$shopBillingData->setCountryCode($country->getCode());
$shopBillingData->setTaxId($taxId);
$shopBillingData->setVatNumber($vatNumber);

$channel->setShopBillingData($shopBillingData);

$this->channelManager->flush();
}
}
Loading

0 comments on commit 47c6b6d

Please sign in to comment.