Skip to content

Commit

Permalink
Merge pull request #19 from olivertar/master
Browse files Browse the repository at this point in the history
Hacer editable el source de tipos de documento
  • Loading branch information
barbanet authored Mar 19, 2020
2 parents c1508d7 + 456da0e commit ae9393a
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 27 deletions.
33 changes: 33 additions & 0 deletions Block/Adminhtml/Order/View/CidFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Mugar\CustomerIdentificationDocument\Block\Adminhtml\Order\View;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Sales\Helper\Admin;
use Mugar\CustomerIdentificationDocument\Api\CidFieldsRepositoryInterface;

class CidFields extends \Magento\Sales\Block\Adminhtml\Order\AbstractOrder
{
/**
* ContactType constructor.
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Sales\Helper\Admin $adminHelper
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
Admin $adminHelper,
CidFieldsRepositoryInterface $cidFieldsRepository,
array $data = []
) {
$this->cidFieldsRepository = $cidFieldsRepository;
parent::__construct($context, $registry, $adminHelper, $data);
}

public function getCidFields()
{
return $this->cidFieldsRepository->getCidFields($this->getOrder());
}
}
2 changes: 1 addition & 1 deletion Block/Order/CidFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
$this->coreRegistry = $registry;
$this->cidFieldsRepository = $cidFieldsRepository;
$this->_isScopePrivate = true;
$this->_template = 'order/view/cid_fields.phtml';
$this->_template = 'Mugar_CustomerIdentificationDocument::order/view/cid_fields.phtml';
parent::__construct($context, $data);
}

Expand Down
50 changes: 50 additions & 0 deletions Block/Order/View/CidFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Customer Identification Document
*
* @category Mugar
* @package Mugar_CustomerIdentificationDocument
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Mugar (https://www.mugar.io/)
*
*/

namespace Mugar\CustomerIdentificationDocument\Block\Order\View;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order;
use Mugar\CustomerIdentificationDocument\Api\Data\CidFieldsInterface;
use Mugar\CustomerIdentificationDocument\Api\CidFieldsRepositoryInterface;

class CidFields extends Template
{

protected $coreRegistry = null;

protected $cidFieldsRepository;

public function __construct(
Context $context,
Registry $registry,
CidFieldsRepositoryInterface $cidFieldsRepository,
array $data = []
) {
$this->coreRegistry = $registry;
$this->cidFieldsRepository = $cidFieldsRepository;
$this->_isScopePrivate = true;
$this->_template = 'Mugar_CustomerIdentificationDocument::order/view/cid_fields.phtml';
parent::__construct($context, $data);
}

public function getOrder() : Order
{
return $this->coreRegistry->registry('current_order');
}

public function getCidFields(Order $order)
{
return $this->cidFieldsRepository->getCidFields($order);
}
}
61 changes: 57 additions & 4 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace Mugar\CustomerIdentificationDocument\Helper;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Mugar\CustomerIdentificationDocument\Model\Config\Source\Types;

/**
* Helper to get system config
Expand All @@ -32,6 +33,8 @@ class Data extends AbstractHelper
{
const SHIPPING_ENABLED = 'checkout/cid/shipping_enabled';
const BILLING_ENABLED = 'checkout/cid/billing_enabled';
const SHIPPING_DOCUMENT_TYPES = 'checkout/cid/shipping_types';
const BILLING_DOCUMENT_TYPES = 'checkout/cid/billing_types';

/**
* Magento\Framework\App\Config\ScopeConfigInterface
Expand All @@ -45,6 +48,8 @@ class Data extends AbstractHelper
*/
private $_storeManager;

private $_types;

/**
* Data constructor.
* @param Context $context
Expand All @@ -54,11 +59,13 @@ class Data extends AbstractHelper
public function __construct(
Context $context,
ScopeConfigInterface $scopeConfigInterface,
StoreManagerInterface $storeManagerInterface
StoreManagerInterface $storeManagerInterface,
Types $types
) {
parent::__construct($context);
$this->_scopeConfig = $scopeConfigInterface;
$this->_storeManager = $storeManagerInterface;
$this->_types = $types;
}

/**
Expand Down Expand Up @@ -87,6 +94,52 @@ public function isBillingEnabled($store = null)
return $this->getConfigValue(self::BILLING_ENABLED, $storeId);
}

/**
* get Shipping Document Types
*
* @param null $store
* @return mixed
* @throws NoSuchEntityException
*/
public function getShippingDocumentTypes($store = null)
{
$optionsSelected = [];
$storeId = is_null($store) ? $this->getStoreId() : $store->getId();
$options = $this->_types->toOptionArray();
$systemOptionsSelected = explode(',', $this->getConfigValue(self::SHIPPING_DOCUMENT_TYPES, $storeId));

foreach ($options as $option) {
if (in_array($option['value'], $systemOptionsSelected)) {
$optionsSelected[$option['value']] = $option['label']->getText();
}
}

return $optionsSelected;
}

/**
* get Shipping Document Types
*
* @param null $store
* @return mixed
* @throws NoSuchEntityException
*/
public function getBillingDocumentTypes($store = null)
{
$optionsSelected = [];
$storeId = is_null($store) ? $this->getStoreId() : $store->getId();
$options = $this->_types->toOptionArray();
$systemOptionsSelected = explode(',', $this->getConfigValue(self::BILLING_DOCUMENT_TYPES, $storeId));

foreach ($options as $option) {
if (in_array($option['value'], $systemOptionsSelected)) {
$optionsSelected[$option['value']] = $option['label']->getText();
}
}

return $optionsSelected;
}

/**
* Get configuration value
*
Expand All @@ -112,4 +165,4 @@ public function getStoreId()
{
return $this->_storeManager->getStore()->getId();
}
}
}
22 changes: 22 additions & 0 deletions Model/Config/Source/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Mugar\CustomerIdentificationDocument\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class Types implements OptionSourceInterface
{
/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public function toOptionArray()
{
return [
['value' => 1, 'label' => __('DNI')],
['value' => 2, 'label' => __('CI')],
['value' => 3, 'label' => __('Passport')]
];
}
}
29 changes: 25 additions & 4 deletions Plugin/CidConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
namespace Mugar\CustomerIdentificationDocument\Plugin;

use Magento\Checkout\Block\Checkout\LayoutProcessor;
use Magento\Framework\Exception\NoSuchEntityException;
use Mugar\CustomerIdentificationDocument\Helper\Data;

class CidConfiguration {
class CidConfiguration
{

/**
* Mugar\CustomerIdentificationDocument\Helper\Data
Expand All @@ -36,7 +36,8 @@ public function __construct(
public function afterProcess(
LayoutProcessor $processor,
array $jsLayout
){
) {

if (!$this->helper->isShippingEnabled()) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['cid-shipping-form']['config']['componentDisabled'] = true;
Expand All @@ -47,6 +48,26 @@ public function afterProcess(
['payment']['children']['cid-billing-form']['config']['componentDisabled'] = true;
}

$currentShippingOptions = $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['cid-shipping-form']['children']['cid-shipping-form-container']['children']['cid-shipping-form-fieldset']['children']['shipping_cid_type']['options'];

foreach ($this->helper->getShippingDocumentTypes() as $k => $v) {
$currentShippingOptions[] = ['value' => $v, 'label' => $v];
}

$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['cid-shipping-form']['children']['cid-shipping-form-container']['children']['cid-shipping-form-fieldset']['children']['shipping_cid_type']['options'] = $currentShippingOptions;

$currentBillingOptions = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['cid-billing-form']['children']['cid-billing-form-container']['children']['cid-billing-form-fieldset']['children']['billing_cid_type']['options'];

foreach ($this->helper->getBillingDocumentTypes() as $k => $v) {
$currentBillingOptions[] = ['value' => $v, 'label' => $v];
}

$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['cid-billing-form']['children']['cid-billing-form-container']['children']['cid-billing-form-fieldset']['children']['billing_cid_type']['options'] = $currentBillingOptions;

return $jsLayout;
}
}
}
12 changes: 11 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
<label>Show on Shipping Step</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="billing_enabled" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="shipping_types" translate="label" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Shipping Document Types</label>
<source_model>Mugar\CustomerIdentificationDocument\Model\Config\Source\Types</source_model>
<validate>required-entry validate-select</validate>
</field>
<field id="billing_enabled" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show on Payment Step</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="billing_types" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Billing Document Types</label>
<source_model>Mugar\CustomerIdentificationDocument\Model\Config\Source\Types</source_model>
<validate>required-entry validate-select</validate>
</field>
</group>
</section>
</system>
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<cid>
<shipping_enabled>0</shipping_enabled>
<billing_enabled>0</billing_enabled>
<shipping_types>1</shipping_types>
<billing_types>1</billing_types>
</cid>
</checkout>
</default>
Expand Down
6 changes: 4 additions & 2 deletions view/adminhtml/layout/sales_order_view.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<block class="Magento\Backend\Block\Template" name="order_cid_fields" template="Mugar_CustomerIdentificationDocument::order/view/cid_fields.phtml" />
<referenceContainer name="content">
<block class="Mugar\CustomerIdentificationDocument\Block\Adminhtml\Order\View\CidFields" name="order_cid_fields" template="Mugar_CustomerIdentificationDocument::order/view/cid_fields.phtml" />
</referenceContainer>
</body>
</page>
</page>
11 changes: 7 additions & 4 deletions view/frontend/layout/checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<item name="label" xsi:type="string">Please select value</item>
<item name="value" xsi:type="string"></item>
</item>
<item name="1" xsi:type="array">
<!--<item name="1" xsi:type="array">
<item name="label" xsi:type="string">DNI</item>
<item name="value" xsi:type="string">DNI</item>
</item>
Expand All @@ -57,6 +57,7 @@
<item name="label" xsi:type="string">Passport</item>
<item name="value" xsi:type="string">Passport</item>
</item>
-->
</item>
<item name="provider" xsi:type="string">checkoutProvider</item>
<item name="dataScope" xsi:type="string">cidShippingForm.shipping_cid_type</item>
Expand Down Expand Up @@ -121,7 +122,7 @@
<item name="cid-billing-form-fieldset" xsi:type="array">
<item name="component" xsi:type="string">uiComponent</item>
<item name="displayArea" xsi:type="string">cid-billing-form-fields</item>

<item name="children" xsi:type="array">
<item name="billing_cid_type" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
Expand All @@ -137,6 +138,7 @@
<item name="label" xsi:type="string">Please select value</item>
<item name="value" xsi:type="string"></item>
</item>
<!--
<item name="1" xsi:type="array">
<item name="label" xsi:type="string">DNI</item>
<item name="value" xsi:type="string">DNI</item>
Expand All @@ -149,6 +151,7 @@
<item name="label" xsi:type="string">Passport</item>
<item name="value" xsi:type="string">Passport</item>
</item>
-->
</item>
<item name="provider" xsi:type="string">checkoutProvider</item>
<item name="dataScope" xsi:type="string">cidBillingForm.billing_cid_type</item>
Expand All @@ -158,7 +161,7 @@
<item name="half-row" xsi:type="boolean">true</item>
<item name="half-row-left" xsi:type="boolean">true</item>
</item>
</item>
</item>
<item name="billing_cid_number" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
<item name="config" xsi:type="array">
Expand Down Expand Up @@ -203,4 +206,4 @@
</arguments>
</referenceBlock>
</body>
</page>
</page>
2 changes: 1 addition & 1 deletion view/frontend/layout/sales_order_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Mugar\CustomerIdentificationDocument\Block\Order\CidFields" as="ordercidfields" name="sales.order.cidfields" after="sales.order.info"/>
<block class="Mugar\CustomerIdentificationDocument\Block\Order\View\CidFields" as="ordercidfields" name="sales.order.cidfields" after="sales.order.info" />
</referenceContainer>
</body>
</page>
Loading

0 comments on commit ae9393a

Please sign in to comment.