Skip to content
This repository has been archived by the owner on Aug 1, 2018. It is now read-only.

Commit

Permalink
Merge branch 'shanavas786-add-region-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix128 committed Aug 22, 2017
2 parents c8aa29d + 901bdcb commit 154353a
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Api/CashondeliveryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ interface CashondeliveryInterface
* Get base amount
* @param array $totals
* @param string $country
* @param string $region
* @return double
*/
public function getBaseAmount(array $totals, $country);
public function getBaseAmount(array $totals, $country, $region);

/**
* Get base tax amount
Expand Down
3 changes: 2 additions & 1 deletion Api/CashondeliveryTableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ interface CashondeliveryTableInterface
*
* @param double $amount
* @param string $country
* @param string $region
* @return double
*/
public function getFee($amount, $country);
public function getFee($amount, $country, $region);

/**
* Get table as array
Expand Down
5 changes: 3 additions & 2 deletions Model/Cashondelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ public function getCalcBase(array $totals)
* Get base amount
* @param array $totals
* @param string $country
* @param string $region
* @return double
*/
public function getBaseAmount(array $totals, $country)
public function getBaseAmount(array $totals, $country, $region)
{
$calcBase = $this->getCalcBase($totals);
return $this->cashondeliveryTableInterface->getFee($calcBase, $country);
return $this->cashondeliveryTableInterface->getFee($calcBase, $country, $region);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion Model/CashondeliveryCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function getQuote()
$this->quote = $this->checkoutSession->getQuote();
$this->quote->collectTotals();
}

return $this->quote;
}

Expand Down Expand Up @@ -83,6 +83,11 @@ public function getFeeLabel()
$amount = $this->getAmount();
$taxAmount = $this->getTaxAmount();

// Need not display label if extra fee is zero
if ($amount == 0 && $taxAmount == 0) {
return '';
}

return __('You will be charged by an extra fee of %1 (+%2 taxes)', [
$this->priceCurrencyInterface->format($amount),
$this->priceCurrencyInterface->format($taxAmount),
Expand Down
9 changes: 5 additions & 4 deletions Model/CashondeliveryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CashondeliveryTable extends AbstractModel implements CashondeliveryTableIn
protected $filesystem;
protected $file;

protected $_columns = ['country', 'from_amount', 'fee', 'website'];
protected $_columns = ['country', 'region', 'from_amount', 'fee', 'website'];

public function __construct(
\Magento\Framework\Model\Context $context,
Expand Down Expand Up @@ -62,11 +62,12 @@ protected function _construct()
*
* @param double $amount
* @param string $country
* @param string $region
* @return double
*/
public function getFee($amount, $country)
public function getFee($amount, $country, $region)
{
return $this->_getResource()->getFee($amount, $country);
return $this->_getResource()->getFee($amount, $country, $region);
}

/**
Expand Down Expand Up @@ -126,7 +127,7 @@ public function saveFromFile($fileName)
$stream = $tmpDirectory->openFile($path);

$headers = $stream->readCsv();
if ($headers === false || count($headers) < 3) {
if ($headers === false || count($headers) < 5) {
$stream->close();
throw new LocalizedException(__('Invalid columns count.'));
}
Expand Down
12 changes: 8 additions & 4 deletions Model/ResourceModel/CashondeliveryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ protected function _construct()
*
* @param double $amount
* @param string $country
* @param string $region
* @return double
*/
public function getFee($amount, $country)
public function getFee($amount, $country, $region)
{
$table = $this->getMainTable();

Expand All @@ -62,16 +63,19 @@ public function getFee($amount, $country)
.'country = '.$connection->quote($country).' OR '
.'country = '.$connection->quote('*')
.') AND ('
.'from_amount < '.doubleval($amount) . ' AND '
.'('
.'region = '.$connection->quote($region).' OR '
.'region = '.$connection->quote('*')
.') AND ('
.'from_amount < '.doubleval($amount).' AND ('
.'website = '.$connection->quote($currentWebsite).' OR '
.'website = '.$connection->quote('*')
.')'
.')'
.')'
)
->order('from_amount desc')
->order(new \Zend_Db_Expr("website = '*'"))
->order(new \Zend_Db_Expr("country = '*'"))
->order(new \Zend_Db_Expr("region = '*'"))
->limit(1);

$row = $connection->fetchRow($qry);
Expand Down
3 changes: 2 additions & 1 deletion Model/Total/Quote/Cashondelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public function collect(
}

$country = $quote->getShippingAddress()->getCountryModel()->getData('iso2_code');
$region = $quote->getShippingAddress()->getRegion();

$baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country);
$baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country, $region);
$amount = $this->priceCurrencyInterface->convert($baseAmount);

if ($this->_canApplyTotal($quote)) {
Expand Down
4 changes: 3 additions & 1 deletion Model/Total/Quote/CashondeliveryTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function collect(
}

$country = $quote->getShippingAddress()->getCountryModel()->getData('iso2_code');
$baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country);
$region = $quote->getShippingAddress()->getRegion();

$baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country, $region);

$baseTaxAmount = $this->cashOnDeliveryInterface->getBaseTaxAmount($baseAmount);
$taxAmount = $this->priceCurrencyInterface->convert($baseTaxAmount);
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cash On Delivery for Magento 2

This module is a Cash On Delivery implementation for Magento2 allowing you to define an additional fee based on destination country and total amount.
This module is a Cash On Delivery implementation for Magento2 allowing you to define an additional fee based on destination country, region and total amount.

<img src="https://github.com/magespecialist/m2-MSP_CashOnDlivery/blob/master/screenshots/screen4.png" />

Expand All @@ -9,10 +9,10 @@ This module is a Cash On Delivery implementation for Magento2 allowing you to de
* Multiple currencies allowed
* Multi store allowed
* Percent or static fee supported
* Fee per country / amount
* Fee per country / region / amount
* Default fee fallback
* Multi website support
* Specific regions limitation
* Multi website support
* Exclude specific regions

## Installing in your Magento

Expand All @@ -36,9 +36,10 @@ This module is a Cash On Delivery implementation for Magento2 allowing you to de

### CSV syntax

MSP Cash On Delivery CSV file syntax is really simple. You have 3 columns: **country**, **from_amount**, **fee**
MSP Cash On Delivery CSV file syntax is really simple. You have 5 columns: **country**, **region**, **from_amount**, **fee**, **website**

* **country**: ISO 2 letters country code. Use * as wildcard to indicate all countries
* **region**: Region name. Use * as wildcard to indicate all regions
* **from_amount**: Indicates the minimum amount to apply the additional fee
* **fee**: The fee to apply (in base currency). Adding **%** after the fee indicates a percent value
* **website**: Magento website code (e.g.: *base*). Use * as wildcard to indicate all websites
Expand Down
4 changes: 2 additions & 2 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ protected function _setupTable(SchemaSetupInterface $setup, ModuleContextInterfa
Table::TYPE_DECIMAL,
'10,4',
[],
'From amount'
'Fee'
)
->addColumn(
'is_pct',
Table::TYPE_BOOLEAN,
null,
[],
'From amount'
'Is fee percetage?'
);

$setup->getConnection()->createTable($table);
Expand Down
21 changes: 19 additions & 2 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
* @param ModuleDataSetupInterface $setup
* @return void
*/
protected function upgradeTo100(ModuleDataSetupInterface $setup)
protected function upgradeTo010000(ModuleDataSetupInterface $setup)
{
$attributes = [
'msp_cod_amount' => 'Cash On Delivery Amount',
Expand All @@ -61,6 +61,19 @@ protected function upgradeTo100(ModuleDataSetupInterface $setup)
}
}

/**
* Upgrade to version 1.2.0
* @param ModuleDataSetupInterface $setup
* @return void
*/
protected function upgradeTo010200(ModuleDataSetupInterface $setup)
{
$tableName = $setup->getTable('msp_cashondelivery_table');
$setup->getConnection()->update($tableName, [
'region' => '*',
]);
}


/**
* Upgrades data for a module
Expand All @@ -74,7 +87,11 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.0') < 0) {
$this->upgradeTo100($setup);
$this->upgradeTo010000($setup);
}

if (version_compare($context->getVersion(), '1.2.0') < 0) {
$this->upgradeTo010200($setup);
}

$setup->endSetup();
Expand Down
18 changes: 16 additions & 2 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class UpgradeSchema implements UpgradeSchemaInterface
{
protected function upgradeTo110(SchemaSetupInterface $setup)
protected function upgradeTo010100(SchemaSetupInterface $setup)
{
$tableName = $setup->getTable('msp_cashondelivery_table');
$setup->getConnection()->addColumn($tableName, 'website', [
Expand All @@ -22,6 +22,16 @@ protected function upgradeTo110(SchemaSetupInterface $setup)
]);
}

protected function upgradeTo010200(SchemaSetupInterface $setup)
{
$tableName = $setup->getTable('msp_cashondelivery_table');
$setup->getConnection()->addColumn($tableName, 'region', [
'type' => Table::TYPE_TEXT,
'nullable' => false,
'comment' => 'Region ID',
]);
}

/**
* Upgrades DB schema for a module
*
Expand All @@ -34,7 +44,11 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
$setup->startSetup();

if (version_compare($context->getVersion(), '1.1.0') < 0) {
$this->upgradeTo110($setup);
$this->upgradeTo010100($setup);
}

if (version_compare($context->getVersion(), '1.2.0') < 0) {
$this->upgradeTo010200($setup);
}

$setup->endSetup();
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MSP_CashOnDelivery" setup_version="1.1.8">
<module name="MSP_CashOnDelivery" setup_version="1.2.0">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/layout/checkout_cart_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.cart.totals">
<arguments>
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/layout/checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.root">
<arguments>
Expand Down

0 comments on commit 154353a

Please sign in to comment.