Skip to content

Commit

Permalink
Merge branch 'orderExchangeRate'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Alexa committed Aug 19, 2016
2 parents 01c00b8 + 4604683 commit 4334758
Show file tree
Hide file tree
Showing 19 changed files with 318 additions and 35 deletions.
35 changes: 35 additions & 0 deletions app/migrations/Version20160712142447.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Sylius\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160712142447 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_order ADD exchange_rate NUMERIC(10, 5) NOT NULL');
$this->addSql('UPDATE sylius_order o LEFT JOIN sylius_currency c ON c.code = o.currency SET o.exchange_rate = c.exchange_rate');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE sylius_order DROP exchange_rate');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Sylius\Bundle\CoreBundle\EventListener;

use Sylius\Component\Cart\Event\CartEvent;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class OrderExchangeRateListener
* @package Sylius\Bundle\CoreBundle\EventListener
*/
class OrderExchangeRateListener
{
/**
* @var RepositoryInterface
*/
private $currencyRepository;

/**
* Cache for the exchange rates.
*
* @var array
*/
private $cache;

/**
* OrderExchangeRateListener constructor.
*
* @param RepositoryInterface $currencyRepository
*/
public function __construct(RepositoryInterface $currencyRepository)
{
$this->currencyRepository = $currencyRepository;
}

/**
* @param CartEvent $event
*/
public function cartInitialize(CartEvent $event)
{
$this->processOrderExchangeRate($event->getCart());
}

/**
* @param GenericEvent $event
*/
public function checkoutFinalize(GenericEvent $event)
{
$this->processOrderExchangeRate($event->getSubject());
}

/**
* @param mixed $order
*/
public function processOrderExchangeRate($order)
{
if (!$order instanceof OrderInterface) {
throw new UnexpectedTypeException(
$order,
'Sylius\Component\Core\Model\OrderInterface'
);
}

$currency = $this->getCurrency($order->getCurrency());

$order->setExchangeRate($currency->getExchangeRate());
}

/**
* @param string $code
*
* @return CurrencyInterface
*/
private function getCurrency($code)
{
if (isset($this->cache[$code])) {
return $this->cache[$code];
}

return $this->cache[$code] = $this->currencyRepository->findOneBy(array('code' => $code));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<gedmo:versioned />
</field>

<field name="exchangeRate" column="exchange_rate" type="decimal" precision="10" scale="5">
<gedmo:versioned />
</field>

<field type="string" name="checkoutState" column="checkout_state" />
<field type="string" name="paymentState" column="payment_state" />
<field type="string" name="shippingState" column="shipping_state" />
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@
<argument type="service" id="sylius.context.currency" />
<tag name="kernel.event_listener" event="sylius.cart.initialize" method="processOrderCurrency" />
</service>
<service id="sylius.listener.order_exchange_rate" class="Sylius\Bundle\CoreBundle\EventListener\OrderExchangeRateListener">
<argument type="service" id="sylius.repository.currency" />
<tag name="kernel.event_listener" event="sylius.cart.initialize" method="cartInitialize" />
<tag name="kernel.event_listener" event="sylius.checkout.finalize.pre_complete" method="checkoutFinalize" />
</service>
<service id="sylius.listener.restricted_zone" class="%sylius.listener.restricted_zone.class%">
<argument type="service" id="sylius.checker.restricted_zone" />
<argument type="service" id="sylius.cart_provider" />
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/templating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

<parameters>
<parameter key="sylius.templating.helper.restricted_zone.class">Sylius\Bundle\CoreBundle\Templating\Helper\RestrictedZoneHelper</parameter>
<parameter key="sylius.templating.helper.order_exchange_rate.class">Sylius\Bundle\CoreBundle\Templating\Helper\OrderExchangeRateHelper</parameter>
</parameters>

<services>
<service id="sylius.templating.helper.restricted_zone" class="%sylius.templating.helper.restricted_zone.class%">
<argument type="service" id="sylius.checker.restricted_zone" />
<tag name="templating.helper" alias="sylius_restricted_zone" />
</service>

<service id="sylius.templating.helper.order_exchange_rate" class="%sylius.templating.helper.order_exchange_rate.class%">
<tag name="templating.helper" alias="sylius_order_exchange_rate" />
</service>
</services>

</container>
7 changes: 7 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@

<parameters>
<parameter key="sylius.twig.extension.restricted_zone.class">Sylius\Bundle\CoreBundle\Twig\RestrictedZoneExtension</parameter>
<parameter key="sylius.twig.extension.order_exchange_rate.class">Sylius\Bundle\CoreBundle\Twig\OrderExchangeRateExtension</parameter>
</parameters>

<services>
<service id="sylius.twig.extension.restricted_zone" class="%sylius.twig.extension.restricted_zone.class%" public="false">
<argument type="service" id="sylius.templating.helper.restricted_zone" />
<tag name="twig.extension" />
</service>

<service id="sylius.twig.extension.order_exchange_rate" class="%sylius.twig.extension.order_exchange_rate.class%" public="false">
<argument type="service" id="sylius.templating.helper.order_exchange_rate" />
<argument type="service" id="sylius.templating.helper.money" />
<tag name="twig.extension" />
</service>
</services>

</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Sylius\Bundle\CoreBundle\Templating\Helper;

use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\Templating\Helper\Helper;

/**
* Class OrderExchangeRateHelper
* @package Sylius\Bundle\CoreBundle\Templating
*/
class OrderExchangeRateHelper extends Helper
{
/**
* @param int $price
* @param OrderInterface $order
*
* @return int
*/
public function calculatePrice($price, OrderInterface $order)
{
return (int) round($price * $order->getExchangeRate());
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'order_exchange_rate';
}
}
68 changes: 68 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Twig/OrderExchangeRateExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Sylius\Bundle\CoreBundle\Twig;

use Sylius\Bundle\CoreBundle\Templating\Helper\OrderExchangeRateHelper;
use Sylius\Bundle\CurrencyBundle\Templating\Helper\MoneyHelper;
use Sylius\Component\Core\Model\OrderInterface;

/**
* Class OrderExchangeRateExtension
* @package Sylius\Bundle\CoreBundle\Twig
*/
class OrderExchangeRateExtension extends \Twig_Extension
{
/**
* @var OrderExchangeRateHelper
*/
private $orderExchangeRateHelper;

/**
* @var MoneyHelper
*/
private $moneyHelper;

/**
* Constructor.
*
* @param OrderExchangeRateHelper $orderExchangeRateHelper
* @param MoneyHelper $moneyHelper
*/
public function __construct(OrderExchangeRateHelper $orderExchangeRateHelper, MoneyHelper $moneyHelper)
{
$this->orderExchangeRateHelper = $orderExchangeRateHelper;
$this->moneyHelper = $moneyHelper;
}

/**
* {@inheritdoc}
*/
public function getFilters()
{
return array(
new \Twig_SimpleFilter('sylius_order_price', array($this, 'calculatePrice')),
);
}

/**
* @param int $price
* @param OrderInterface $order
* @param bool $decimal
*
* @return int
*/
public function calculatePrice($price, OrderInterface $order, $decimal = false)
{
$amount = $this->orderExchangeRateHelper->calculatePrice($price, $order);

return $this->moneyHelper->formatAmount($amount, $order->getCurrency(), $decimal);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_order_exchange_rate';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<span class="pull-right">
{{ misc.state_label(payment.state, 'payment') }}
</span>
{{ payment.method }} - {{ payment.amount|sylius_price(payment.currency) }}
{{ payment.method }} - {{ payment.amount|sylius_order_price(order) }}
</a>
{% if hide_form|default(false) is sameas(false) %}
{{ misc.payment_form(payment) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
{{ form_widget(form.items[loop.index0].unitPrice) }}
</td>
<td class="total text-right">
{{ item.total|sylius_price(order.currency) }}
{{ item.total|sylius_order_price(order) }}
</td>
</tr>
{% else %}
Expand All @@ -130,7 +130,7 @@
<tr>
<td class="active text-right" colspan="7">
<strong>{{ 'sylius.order.items_total'|trans }}</strong>:
<span class="amount">{{ order.itemsTotal|sylius_price(order.currency) }}</span>
<span class="amount">{{ order.itemsTotal|sylius_order_price(order) }}</span>
</td>
</tr>
<tr class="taxes">
Expand All @@ -139,7 +139,7 @@
{% if not order.adjustments(taxAdjustment).isEmpty() %}
<ul class="list-unstyled">
{% for adjustment in order.adjustments(taxAdjustment) %}
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_price(order.currency) }} </li>
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_order_price(order) }} </li>
{% endfor %}
</ul>
{% else %}
Expand All @@ -148,7 +148,7 @@
</td>
<td class="text-right" colspan="3">
<strong>{{ 'sylius.order.tax_total'|trans }}</strong>:
<span class="amount">{{ order.adjustmentsTotal(taxAdjustment)|sylius_price(order.currency) }}</span>
<span class="amount">{{ order.adjustmentsTotal(taxAdjustment)|sylius_order_price(order) }}</span>
</td>
</tr>
<tr class="shipping-charges">
Expand All @@ -157,7 +157,7 @@
{% if not order.adjustments(shippingAdjustment).isEmpty() %}
<ul class="list-unstyled">
{% for adjustment in order.adjustments(shippingAdjustment) %}
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_price(order.currency) }} </li>
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_order_price(order) }} </li>
{% endfor %}
</ul>
{% else %}
Expand All @@ -166,7 +166,7 @@
</td>
<td class="text-right" colspan="3">
<strong>{{ 'sylius.order.shipping_total'|trans }}</strong>:
<span class="amount">{{ order.adjustmentsTotal(shippingAdjustment)|sylius_price(order.currency) }}</span>
<span class="amount">{{ order.adjustmentsTotal(shippingAdjustment)|sylius_order_price(order) }}</span>
</td>
</tr>
<tr class="promotion-discount">
Expand All @@ -175,7 +175,7 @@
{% if not order.adjustments(promotionAdjustment).isEmpty() %}
<ul class="list-unstyled">
{% for adjustment in order.adjustments(promotionAdjustment) %}
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_price(order.currency) }} </li>
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_order_price(order) }} </li>
{% endfor %}
</ul>
{% else %}
Expand All @@ -184,7 +184,7 @@
</td>
<td class="text-right" colspan="3">
<strong>{{ 'sylius.checkout.finalize.order.promotion_total'|trans }}</strong>:
<span class="amount">{{ order.adjustmentsTotal(promotionAdjustment)|sylius_price(order.currency) }}</span>
<span class="amount">{{ order.adjustmentsTotal(promotionAdjustment)|sylius_order_price(order) }}</span>
</td>
</tr>
{% if not order.adjustments.isEmpty() %}
Expand All @@ -200,7 +200,7 @@
<tr>
<td class="text-right active text-success lead" colspan="6">
<strong>{{ 'sylius.order.total'|trans }}</strong>:
<span class="amount">{{ order.total|sylius_price(order.currency) }}</span>
<span class="amount">{{ order.total|sylius_order_price(order) }}</span>
</td>
</tr>
</tfoot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{{ adjustment.description }}
</td>
<td>
{{ adjustment.amount|sylius_price(order.currency) }}
{{ adjustment.amount|sylius_order_price(order) }}
</td>
<td>
{{ misc.state_label(adjustment.locked) }}
Expand Down
Loading

0 comments on commit 4334758

Please sign in to comment.