forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
318 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/Sylius/Bundle/CoreBundle/EventListener/OrderExchangeRateListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Sylius/Bundle/CoreBundle/Templating/Helper/OrderExchangeRateHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
src/Sylius/Bundle/CoreBundle/Twig/OrderExchangeRateExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.