Skip to content

Commit

Permalink
GRKAS1-29: Add configuration to customize ProductUnitPrecision (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
dxops authored Feb 11, 2020
1 parent b60a1bb commit 736618c
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 22 deletions.
80 changes: 65 additions & 15 deletions Entity/AkeneoSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ class AkeneoSettings extends Transport
* @ORM\Column(name="akeneo_sync_products", type="string", length=255, nullable=false)
*/
protected $syncProducts;
/**
* @var string
*
* @ORM\Column(name="akeneo_product_unit_attribute", type="string", length=255, nullable=true)
*/
protected $productUnitAttribute;
/**
* @var string
*
* @ORM\Column(name="akeneo_unit_precision_attr", type="string", length=255, nullable=true)
*/
protected $productUnitPrecisionAttribute;
/**
* @var string[]
*
Expand Down Expand Up @@ -157,7 +169,7 @@ class AkeneoSettings extends Transport
/**
* @var boolean
*
* @ORM\Column(name="akeneo_merge_image_to_parent", type="boolean", options={"default"="false"})
* @ORM\Column(name="akeneo_merge_image_to_parent", type="boolean", options={"default"=false})
*/
private $akeneoMergeImageToParent = false;

Expand Down Expand Up @@ -241,20 +253,22 @@ public function getSettingsBag()
if (null === $this->settings) {
$this->settings = new ParameterBag(
[
'clientId' => $this->getClientId(),
'secret' => $this->getSecret(),
'akeneoChannels' => $this->getAkeneoChannels(),
'akeneoActiveChannel' => $this->getAkeneoActiveChannel(),
'username' => $this->getUsername(),
'password' => $this->getPassword(),
'token' => $this->getToken(),
'refreshToken' => $this->getRefreshToken(),
'syncProducts' => $this->getSyncProducts(),
'akeneoCurrencies' => $this->getAkeneoCurrencies(),
'akeneoActiveCurrencies' => $this->getAkeneoActiveCurrencies(),
'akeneoLocales' => $this->getAkeneoLocales()->toArray(),
'akeneoLocalesList' => $this->getAkeneoLocalesList(),
'akeneoAttributesList' => $this->getAkeneoAttributesList(),
'clientId' => $this->getClientId(),
'secret' => $this->getSecret(),
'akeneoChannels' => $this->getAkeneoChannels(),
'akeneoActiveChannel' => $this->getAkeneoActiveChannel(),
'username' => $this->getUsername(),
'password' => $this->getPassword(),
'token' => $this->getToken(),
'refreshToken' => $this->getRefreshToken(),
'syncProducts' => $this->getSyncProducts(),
'productUnitAttribute' => $this->getProductUnitAttribute(),
'productUnitPrecisionAttribute' => $this->getProductUnitPrecisionAttribute(),
'akeneoCurrencies' => $this->getAkeneoCurrencies(),
'akeneoActiveCurrencies' => $this->getAkeneoActiveCurrencies(),
'akeneoLocales' => $this->getAkeneoLocales()->toArray(),
'akeneoLocalesList' => $this->getAkeneoLocalesList(),
'akeneoAttributesList' => $this->getAkeneoAttributesList(),
]
);
}
Expand Down Expand Up @@ -438,6 +452,42 @@ public function setSyncProducts($syncProducts)
return $this;
}

/**
* @return string
*/
public function getProductUnitAttribute()
{
return $this->productUnitAttribute;
}

/**
* @param string $productUnitAttribute
*/
public function setProductUnitAttribute($productUnitAttribute)
{
$this->productUnitAttribute = $productUnitAttribute;

return $this;
}

/**
* @return string
*/
public function getProductUnitPrecisionAttribute()
{
return $this->productUnitPrecisionAttribute;
}

/**
* @param string $productUnitPrecisionAttribute
*/
public function setProductUnitPrecisionAttribute($productUnitPrecisionAttribute)
{
$this->productUnitPrecisionAttribute = $productUnitPrecisionAttribute;

return $this;
}

/**
* @return array/null
*/
Expand Down
16 changes: 16 additions & 0 deletions Form/Type/AkeneoSettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => true,
]
)
->add(
'productUnitAttribute',
TextType::class,
[
'label' => 'oro.akeneo.integration.settings.product_unit_attribute.label',
'required' => false,
]
)
->add(
'productUnitPrecisionAttribute',
TextType::class,
[
'label' => 'oro.akeneo.integration.settings.product_unit_precision_attribute.label',
'required' => false,
]
)
->add(
'akeneoActiveCurrencies',
ChoiceType::class,
Expand Down
45 changes: 40 additions & 5 deletions ImportExport/DataConverter/ProductDataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Oro\Bundle\LocaleBundle\Formatter\DateTimeFormatter;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\ProductBundle\ImportExport\DataConverter\ProductDataConverter as BaseProductDataConverter;
use Oro\Bundle\ProductBundle\Provider\ProductUnitsProvider;

/**
* Converts data for imported row.
Expand Down Expand Up @@ -57,6 +58,14 @@ class ProductDataConverter extends BaseProductDataConverter implements ContextAw
/** @var array */
protected $fieldMapping = [];

/** @var ProductUnitsProvider */
protected $productUnitsProvider;

public function setProductUnitsProvider(ProductUnitsProvider $productUnitsProvider): void
{
$this->productUnitsProvider = $productUnitsProvider;
}

/**
* {@inheritdoc}
*/
Expand All @@ -73,11 +82,7 @@ public function convertToImportFormat(array $importedRecord, $skipNullValues = t
unset($importedRecord['_links']);

$importedRecord['sku'] = $importedRecord['identifier'] ?? $importedRecord['code'];
$importedRecord['primaryUnitPrecision'] = [
'unit' => ['code' => $this->configManager->get('oro_product.default_unit')],
'precision' => $this->configManager->get('oro_product.default_unit_precision'),
'sell' => 1,
];
$importedRecord['primaryUnitPrecision'] = $this->getPrimaryUnitPrecision($importedRecord);

if (!empty($importedRecord['family'])) {
$importedRecord['attributeFamily'] = [
Expand Down Expand Up @@ -463,4 +468,34 @@ protected function getBackendHeader()
{
throw new \Exception('Normalization is not implemented!');
}

protected function getPrimaryUnitPrecision(array $importedRecord): array
{
$unit = $this->configManager->get('oro_product.default_unit');
$precision = $this->configManager->get('oro_product.default_unit_precision');

$unitAttribute = $this->getTransport()->getProductUnitAttribute();
$unitPrecisionAttribute = $this->getTransport()->getProductUnitPrecisionAttribute();

$availableUnits = $this->productUnitsProvider->getAvailableProductUnits();

if (isset($importedRecord['values'][$unitAttribute])) {
$unitData = reset($importedRecord['values'][$unitAttribute]);
if (isset($unitData['data']) && in_array($unitData['data'], $availableUnits)) {
$unit = $unitData['data'];
}
}
if (isset($importedRecord['values'][$unitPrecisionAttribute])) {
$unitPrecisionData = reset($importedRecord['values'][$unitPrecisionAttribute]);
if (isset($unitPrecisionData['data'])) {
$precision = (int)$unitPrecisionData['data'];
}
}

return [
'unit' => ['code' => $unit],
'precision' => $precision,
'sell' => true,
];
}
}
6 changes: 4 additions & 2 deletions Migrations/Schema/OroAkeneoBundleInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class OroAkeneoBundleInstaller implements Installation, ExtendExtensionAwareInte
*/
public function getMigrationVersion()
{
return 'v1_6';
return 'v1_7';
}

/**
Expand Down Expand Up @@ -95,6 +95,8 @@ protected function updateIntegrationTransportTable(Schema $schema)
$table->addColumn('akeneo_secret', 'string', ['notnull' => false, 'length' => 100]);
$table->addColumn('akeneo_url', 'string', ['notnull' => false, 'length' => 100]);
$table->addColumn('akeneo_sync_products', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('akeneo_product_unit_attribute', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('akeneo_unit_precision_attr', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('akeneo_channels', 'array', ['notnull' => false, 'comment' => '(DC2Type:array)']);
$table->addColumn('akeneo_username', 'string', ['notnull' => false, 'length' => 200]);
$table->addColumn('akeneo_password', 'string', ['notnull' => false, 'length' => 200]);
Expand All @@ -115,7 +117,7 @@ protected function updateIntegrationTransportTable(Schema $schema)
$table->addColumn('rootcategory_id', 'integer', ['notnull' => false]);
$table->addColumn('pricelist_id', 'integer', ['notnull' => false]);
$table->addColumn('akeneo_attributes_image_list', 'text', ['notnull' => false]);
$table->addColumn('akeneo_merge_image_to_parent', 'boolean', ['notnull' => false]);
$table->addColumn('akeneo_merge_image_to_parent', 'boolean', ['notnull' => false, 'default' => false]);
$table->addIndex(['rootcategory_id'], 'idx_d7a389a852d2453c', []);
$table->addIndex(['pricelist_id'], 'idx_d7a389a846b960c4', []);
}
Expand Down
41 changes: 41 additions & 0 deletions Migrations/Schema/v1_7/OroAkeneoMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Oro\Bundle\AkeneoBundle\Migrations\Schema\v1_7;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;

/**
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
*/
class OroAkeneoMigration implements Migration
{
/**
* {@inheritdoc}
*/
public function getMigrationVersion()
{
return 'v1_7';
}

/**
* {@inheritdoc}
*/
public function up(Schema $schema, QueryBag $queries)
{
/* Tables generation */
$this->updateOroIntegrationTransportTable($schema);
}

/**
* Create oro_integration_transport table
*/
protected function updateOroIntegrationTransportTable(Schema $schema)
{
$table = $schema->getTable('oro_integration_transport');
$table->addColumn('akeneo_product_unit_attribute', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('akeneo_unit_precision_attr', 'string', ['notnull' => false, 'length' => 255]);
}
}
1 change: 1 addition & 0 deletions Resources/config/importexport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ services:
- [setEntityConfigManager, ["@oro_entity_config.config_manager"]]
- [setDateTimeFormatter, ["@oro_locale.formatter.date_time"]]
- [setAttachmentsDir, ["%kernel.project_dir%/var/import_export/attachments/"]]
- [setProductUnitsProvider, ["@oro_product.provider.product_units_provider"]]

oro_akeneo.importexport.strategy.product:
class: '%oro_akeneo.importexport.strategy.product.class%'
Expand Down
4 changes: 4 additions & 0 deletions Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ oro:
merge_image:
label: 'Merge images from simple products to configurable product'
tooltip: 'Add all images from the children to the configurable product'
product_unit_attribute:
label: 'Product Unit Attribute Name'
product_unit_precision_attribute:
label: 'Product Unit Precision Attribute Name'
popup:
channel: 'This is a list of channels configured for the current Akeneo PIM instance. Select the required channel for product and category import. To refresh the list, click Refresh Channels'
currencies: 'The control configures mapping between the currencies enabled in the current Akeneo PIM instance and the currencies enabled in this application. To skip price import for a specific currency, do not add mapping to it. To refresh the list, click Refresh Currencies'
Expand Down
2 changes: 2 additions & 0 deletions Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
</div>

{{ form_row(form.syncProducts) }}
{{ form_row(form.productUnitAttribute) }}
{{ form_row(form.productUnitPrecisionAttribute) }}

<div class="control-group control-group-choice">
<div class="control-label wrap">
Expand Down

0 comments on commit 736618c

Please sign in to comment.