Skip to content

Commit

Permalink
Merge pull request #28 from tomkalon/OP-319
Browse files Browse the repository at this point in the history
OP-319 Added the ability to select package dimensions
  • Loading branch information
senghe authored Sep 5, 2024
2 parents 9e72cf1 + fe10a65 commit 2047a12
Show file tree
Hide file tree
Showing 37 changed files with 1,029 additions and 79 deletions.
208 changes: 189 additions & 19 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,108 @@ bitbag_shipping_export_plugin.yaml to the config/packages and config/routes dire
It also adding the appropriate entry to config/bundles.php.
If it doesn't, so please remember to do the same as above for SyliusShippingExportPlugin configuration.

### Create a new controller:
```php
// src/Controller/ShippingExportController
<?php
declare(strict_types=1);
namespace App\Controller;
use BitBag\SyliusInPostPlugin\Controller\SelectParcelTemplateTrait;
use BitBag\SyliusShippingExportPlugin\Event\ExportShipmentEvent;
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Webmozart\Assert\Assert;
final class ShippingExportController extends ResourceController
{
public const SELECT_PARCEL_TEMPLATE_EVENT = 'select_parcel_template';
use SelectParcelTemplateTrait;
public function exportAllNewShipmentsAction(Request $request): RedirectResponse
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
Assert::implementsInterface($this->repository, ShippingExportRepositoryInterface::class);
$shippingExports = $this->repository->findAllWithNewOrPendingState();
if (0 === count($shippingExports)) {
/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('error', 'bitbag.ui.no_new_shipments_to_export');
return $this->redirectToReferer($request);
}
foreach ($shippingExports as $shippingExport) {
$this->eventDispatcher->dispatch(
ExportShipmentEvent::SHORT_NAME,
$configuration,
$shippingExport,
);
}
return $this->redirectToReferer($request);
}
public function exportSingleShipmentAction(Request $request): RedirectResponse
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
/** @var ResourceInterface|null $shippingExport */
$shippingExport = $this->repository->find($request->get('id'));
Assert::notNull($shippingExport);
$this->eventDispatcher->dispatch(
ExportShipmentEvent::SHORT_NAME,
$configuration,
$shippingExport,
);
return $this->redirectToReferer($request);
}
private function redirectToReferer(Request $request): RedirectResponse
{
$referer = $request->headers->get('referer');
if (null !== $referer) {
return new RedirectResponse($referer);
}
return $this->redirectToRoute($request->attributes->get('_route'));
}
}
```

Complete the **config/packages/bitbag_shipping_export_plugin.yaml** file with the following data:

```yaml
# config/packages/bitbag_shipping_export_plugin.yaml
imports:
- { resource: "@BitBagSyliusShippingExportPlugin/Resources/config/config.yml" }
sylius_resource:
resources:
bitbag.shipping_export:
classes:
model: App\Entity\Shipping\ShippingExport
controller: App\Controller\ShippingExportController
```
Remember that in case of different mapping, the model path may be different.
Default:
```yaml
model: App\Entity\ShippingExport
```

### Extend entities with parameters

You can implement this using xml-mapping or attributes. Instructions for both settings are described below.
Expand All @@ -63,7 +165,6 @@ sylius_shipping:
shipping_method:
classes:
model: App\Entity\ShippingMethod
```

Add trait and interface to your Order and ShippingMethod entity classes:
Expand Down Expand Up @@ -101,6 +202,22 @@ class ShippingMethod extends BaseShippingMethod implements ImageAwareInterface
use ShippingMethodImageTrait;
}
```
```php
<?php
declare(strict_types=1);
namespace App\Entity;
use BitBag\SyliusInPostPlugin\Entity\ShippingExportInterface;
use BitBag\SyliusInPostPlugin\Model\ParcelTemplateTrait;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingExport as BaseShippingExport;
class ShippingExport extends BaseShippingExport implements ShippingExportInterface
{
use ParcelTemplateTrait;
}
```
Remember to mark it appropriately in the config/doctrine.yaml configuration file.
```
doctrine:
Expand All @@ -118,10 +235,9 @@ Define new Entity mapping inside your src/Resources/config/doctrine directory.
```xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
<entity name="App\Entity\Order" table="sylius_order">
Expand Down Expand Up @@ -156,6 +272,19 @@ Define new Entity mapping inside your src/Resources/config/doctrine directory.
</entity>
</doctrine-mapping>
```
```xml
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="App\Entity\ShippingExport" table="bitbag_shipping_export">
<field name="parcel_template" nullable="true" />
</entity>
</doctrine-mapping>
```
#### You can do it with attributes if you prefer. Remember to mark it appropriately in the config/doctrine.yaml configuration file.
```
doctrine:
Expand All @@ -166,7 +295,6 @@ doctrine:
App:
...
type: attribute
```
```php
<?php
Expand Down Expand Up @@ -203,7 +331,6 @@ class Order extends BaseOrder implements InPostPointsAwareInterface
$this->point = $point;
}
}

```

```php
Expand Down Expand Up @@ -239,9 +366,44 @@ class ShippingMethod extends BaseShippingMethod implements ImageAwareInterface
{
$this->image = $image;
}

// other methods
}
```
```php
<?php

declare(strict_types=1);

namespace App\Entity\Shipping;

use BitBag\SyliusInPostPlugin\Entity\ShippingExportInterface;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingExport as BaseShippingExport;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="bitbag_shipping_export")
*/
#[ORM\Entity]
#[ORM\Table(name: 'bitbag_shipping_export')]
class ShippingExport extends BaseShippingExport implements ShippingExportInterface
{
#[ORM\Column(type: 'string', nullable: true)]
protected ?string $parcel_template = null;

public function getParcelTemplate(): ?string
{
return $this->parcel_template;
}

public function setParcelTemplate(?string $parcel_template): void
{
$this->parcel_template = $parcel_template;
}
}
```

Finish the installation by updating the database schema (check in advance: [Known Issues](known_issues.md)):

```
Expand Down Expand Up @@ -273,10 +435,7 @@ vendor/bitbag/inpost-plugin/tests/Application/templates/bundles/SyliusShopBundle
webpack_encore:
output_path: '%kernel.project_dir%/public/build/default'
builds:
admin: '%kernel.project_dir%/public/build/admin'
shop: '%kernel.project_dir%/public/build/shop'
app.admin: '%kernel.project_dir%/public/build/app/admin'
app.shop: '%kernel.project_dir%/public/build/app/shop'
...
inpost_admin: '%kernel.project_dir%/public/build/bitbag/inpost/admin'
inpost_shop: '%kernel.project_dir%/public/build/bitbag/inpost/shop'
```
Expand Down Expand Up @@ -317,14 +476,7 @@ By a standard, the `webpack.config.js` file should be available in your reposito
framework:
assets:
packages:
admin:
json_manifest_path: '%kernel.project_dir%/public/build/admin/manifest.json'
shop:
json_manifest_path: '%kernel.project_dir%/public/build/shop/manifest.json'
app.admin:
json_manifest_path: '%kernel.project_dir%/public/build/app/admin/manifest.json'
app.shop:
json_manifest_path: '%kernel.project_dir%/public/build/app/shop/manifest.json'
...
inpost_shop:
json_manifest_path: '%kernel.project_dir%/public/build/bitbag/inpost/shop/manifest.json'
inpost_admin:
Expand All @@ -340,6 +492,24 @@ By a standard, the `webpack.config.js` file should be available in your reposito
```


## Default parameters configuration
In the .env file, the default parcel size and label type can be specified by adding:

````
BITBAG_INPOST_DEFAULT_PARCEL_TEMPLATE='medium'
BITBAG_INPOST_DEFAULT_LABEL_TYPE='normal'
````

Three types of parcel templates are allowed:
- 'small'
- 'medium'
- 'large'

Two types of labels are allowed:
- 'normal'
- 'A6'


## Testing & running the plugin

```bash
Expand Down
2 changes: 1 addition & 1 deletion features/adding_image_to_shipping_method.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Feature: Adding shipping method image
When I want to modify a shipping method "InPost"
And I upload the "image/shipping_logo.jpg" image as shipping method logo
And I save my changes
Then I should be notified that it has been successfully edited
Then I should be notified that it has been successfully edited
28 changes: 28 additions & 0 deletions features/changing_shipping_method_parcel_template.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@managing_shipping_export_parcel_template_inpost
Feature: Changing shipping export parcel template
To send a query to the Inpost API with a different shipment template
As an Administrator
I need to be able to choose a parcel template

Background:
Given the store operates on a single channel in the "United States" named "Web-US"
And I am logged in as an administrator
And the store has "Inpost" shipping method with "$10.00" fee
And there is a registered "inpost" shipping gateway for this shipping method named "INPOST_PL"
And it has "Access token" field set to "123"
And it has "Organization ID" field set to "123"
And it has "Environment" field set to "sandbox"
And it has "service" field set to "inpost_locker_standard"
And the store has a product "Chicken" priced at "$2.00" in "Web-US" channel
And customer "[email protected]" has placed 1 orders on the "Web-US" channel in each buying 5 "Chicken" products
And the customer set the shipping address "Mike Ross" addressed it to "350 5th Ave", "10118" "New York" in the "United States" to orders
And those orders were placed with "Inpost" shipping method
And set product weight to "10"
And set units to the shipment

@ui
Scenario: Seeing shipments to export
When I go to the shipping export page
Then I should see 1 shipments with "New" state
Then I select parcel template
Then I should see that shipping export parcel template is set
4 changes: 3 additions & 1 deletion spec/Api/WebClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ final class WebClientSpec extends ObjectBehavior

public const LABEL_TYPE = "normal";

public const PARCEL_TEMPLATE = "medium";

public function let(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
): void
{
$this->beConstructedWith($client, $requestFactory, $streamFactory, self::LABEL_TYPE);
$this->beConstructedWith($client, $requestFactory, $streamFactory, self::LABEL_TYPE, self::PARCEL_TEMPLATE);
}

public function it_is_initializable(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace spec\BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener;

use BitBag\SyliusInPostPlugin\Entity\ShippingExportInterface;
use BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener\SelectParcelTemplateAction;
use BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener\SelectParcelTemplateActionInterface;
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class SelectParcelTemplateActionSpec extends ObjectBehavior
{
public function let(
ShippingExportRepositoryInterface $shippingExportRepository,
RequestStack $requestStack,
TranslatorInterface $translator,
): void {
$this->beConstructedWith($shippingExportRepository, $requestStack, $translator);
}

public function it_is_initializable()
{
$this->shouldHaveType(SelectParcelTemplateAction::class);
$this->shouldBeAnInstanceOf(SelectParcelTemplateActionInterface::class);
}

public function it_should_save_shipping_export_changes(
ShippingExportInterface $shippingExport,
ShippingExportRepositoryInterface $shippingExportRepository,
RequestStack $requestStack,
SessionInterface $session,
FlashBagInterface $flashBag
): void {
$shippingExportRepository->add($shippingExport)->shouldBeCalled();

$requestStack->getSession()->willReturn($session);
$session->getBag('flashes')->willReturn($flashBag);

$this->execute($shippingExport);
}

}
Loading

0 comments on commit 2047a12

Please sign in to comment.