setono/sylius-stock-movement-plugin

Plugin to handle stock related activities

Installs: 7 166

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 3

Open Issues: 13

Type:sylius-plugin


README

Latest Version Latest Unstable Version Software License Build Status Quality Score

Log all stock movements in your shop. You can use this to create very accurate reports of your inventory movements.

Installation

Step 1: Download the plugin

Open a command console, enter your project directory and execute the following command to download the latest stable version of this plugin:

$ composer require setono/sylius-stock-movement-plugin

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the plugin

Then, enable the plugin by adding it to the list of registered plugins/bundles in the config/bundles.php file of your project:

<?php

return [
    // ...
    
    League\FlysystemBundle\FlysystemBundle::class => ['all' => true],
    Setono\CronExpressionBundle\SetonoCronExpressionBundle::class => ['all' => true],
    Setono\SyliusStockMovementPlugin\SetonoSyliusStockMovementPlugin::class => ['all' => true],
    
    // It is important to add plugin before the grid bundle
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
        
    // ...
];

NOTE that you must instantiate the plugin before the grid bundle, else you will see an exception like You have requested a non-existent parameter "setono_sylius_stock_movement.model.report_configuration.class".

Step 3: Import routing

# config/routes/setono_sylius_stock_movement.yaml
setono_sylius_stock_movement:
    resource: "@SetonoSyliusStockMovementPlugin/Resources/config/routing.yaml"

Step 4: Configure plugin

# config/packages/setono_sylius_stock_movement.yaml
imports:
    - { resource: "@SetonoSyliusStockMovementPlugin/Resources/config/app/config.yaml" }

setono_sylius_stock_movement:
    templates:
        - label: Default
          template: "@SetonoSyliusStockMovementPlugin/Template/default.txt.twig"

Step 5: Update database schema

Use Doctrine migrations to create a migration file and update the database.

$ bin/console doctrine:migrations:diff
$ bin/console doctrine:migrations:migrate

Step 6: Install assets

$ php bin/console assets:install

Step 7: Using asynchronous transport (optional, but recommended)

All commands in this plugin will extend the CommandInterface. Therefore you can route all commands easily by adding this to your Messenger config:

# config/packages/messenger.yaml
framework:
    messenger:
        routing:
            # Route all command messages to the async transport
            # This presumes that you have already set up an 'async' transport
            # See docs on how to setup a transport like that: https://symfony.com/doc/current/messenger.html#transports-async-queued-messages
            'Setono\SyliusStockMovementPlugin\Message\Command\CommandInterface': async

Step 8 (optional): Create or import fixtures

  • Import fixtures:

    # config/packages/_sylius.yaml
    imports:
        # ...
        - { resource: "@SetonoSyliusStockMovementPlugin/Resources/config/app/fixtures.yaml" }
  • Or create your own:

    # config/fixtures.yaml
    sylius_fixtures:
        suites:
            YOUR_SUITE:
                fixtures:
                    setono_stock_movement:
                        options:
                            amount: 1000

API

Create a stock movement on the variant variant-code with a quantity of 1:

brew install jq
SYLIUS_HOST=http://127.0.0.1:8000
SYLIUS_ADMIN_API_ACCESS_TOKEN=$(curl $SYLIUS_HOST/api/oauth/v2/token \
    --silent --show-error \
    -d "client_id"=demo_client \
    -d "client_secret"=secret_demo_client \
    -d "grant_type"=password \
    -d "username"=api@example.com \
    -d "password"=sylius-api | jq '.access_token' --raw-output)
SYLIUS_SOME_PRODUCT_CODE=$(curl $SYLIUS_HOST/api/v1/products/?limit=1 \
    --silent --show-error \
    -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN" \
    -H "Accept: application/json" | jq '._embedded.items[0].code' --raw-output)
echo "Some product code: $SYLIUS_SOME_PRODUCT_CODE"
SYLIUS_SOME_PRODUCT_VARIANT_CODE=$(curl $SYLIUS_HOST/api/v1/products/$SYLIUS_SOME_PRODUCT_CODE/variants/?limit=1 \
    --silent --show-error \
    -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN" \
    -H "Accept: application/json"| jq '._embedded.items[0].code' --raw-output)
echo "Some product variant code: $SYLIUS_SOME_PRODUCT_VARIANT_CODE"

curl $SYLIUS_HOST/api/v1/stock-movements/ \
  -X POST \
  -H 'Accept: application/json' \
  -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d "{
	\"quantity\": 1,
	\"variant\": \"$SYLIUS_SOME_PRODUCT_VARIANT_CODE\"
}"