Skip to content

Commit

Permalink
Merge pull request #1367 in NEXT/platform from next-1881/currency-pre…
Browse files Browse the repository at this point in the history
…cision to master

* commit '5ea622d80bd3bfde3ba423820728f10f235d594e':
  NEXT-1881 - Add snippets
  NEXT-1881 - Add precision to currencies and provide them in context object for price roundings
  • Loading branch information
jenskueper committed Mar 25, 2019
2 parents 3215b57 + 5ea622d commit 65c34cc
Show file tree
Hide file tree
Showing 55 changed files with 446 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
v-model="currency.factor">
</sw-field>
{% endblock %}

{% block sw_settings_currency_detail_content_field_decimal_precision %}
<sw-field type="number"
:label="$tc('sw-settings-currency.detail.labelPrecision')"
:placeholder="$tc('sw-settings-currency.detail.placeholderPrecision')"
v-model="currency.decimalPrecision">
</sw-field>
{% endblock %}
</sw-container>
</sw-card>
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@
</sw-grid-column>
{% endblock %}

{% block sw_settings_currency_list_columns_decimal_precision %}
<sw-grid-column flex="minmax(100px, 1fr)"
editable
:label="$tc('sw-settings-currency.list.columnDecimalPrecision')">
{% block sw_settings_currency_list_columns_decimal_precision_label %}
{{ item.decimalPrecision }}
{% endblock %}

{% block sw_settings_currency_list_grid_columns_decimal_precision_editor %}
<sw-field type="number" v-model="item.decimalPrecision" slot="inline-edit"></sw-field>
{% endblock %}
</sw-grid-column>
{% endblock %}

{% block sw_settings_currency_list_columns_actions %}
<sw-grid-column flex="minmax(70px, 70px)"
align="center"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"columnShortName": "Kurzname",
"columnSymbol": "Symbol",
"columnFactor": "Faktor",
"columnDecimalPrecision": "Nachkommastellen",
"buttonAddCurrency": "Währung anlegen",
"contextMenuEdit": "Bearbeiten",
"modalTitleDelete": "Währung löschen",
Expand All @@ -21,6 +22,8 @@
},
"detail": {
"textHeadline": "Neue Währung",
"labelPrecision": "Nachkommastellen",
"placeholderPrecision": "Nachkommastellen eingeben...",
"titleCard": "Einstellungen",
"titleSaveSuccess": "Erfolg",
"messageSaveSuccess": "Die Währung \"{name}\" wurde erfolgreich gespeichert.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"columnShortName": "Short name",
"columnSymbol": "Symbol",
"columnFactor": "Factor",
"columnDecimalPrecision": "Decimal place",
"buttonAddCurrency": "Add currency",
"contextMenuEdit": "Edit",
"modalTitleDelete": "Delete currency",
Expand All @@ -21,6 +22,8 @@
},
"detail": {
"textHeadline": "New currency",
"labelPrecision": "Decimal place",
"placeholderPrecision": "Enter decimal place",
"titleCard": "Settings",
"titleSaveSuccess": "Success",
"messageSaveSuccess": "Currency \"{name}\" has been saved successfully.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('core/data/EntityStore.js', () => {
const entity = store.create();
entity.factor = 6844.41;
entity.symbol = 'Ƀ';
entity.decimalPrecision = 2;
entity.shortName = 'BTC';
entity.name = 'Bitcoin';

Expand Down Expand Up @@ -97,6 +98,7 @@ describe('core/data/EntityStore.js', () => {
// Create a new entry
const entity = new EntityProxy('currency', serviceContainer.currencyService);
entity.factor = 6844.41;
entity.decimalPrecision = 2;
entity.symbol = 'Ƀ';
entity.shortName = 'BTC';
entity.name = 'Bitcoin';
Expand Down Expand Up @@ -127,6 +129,7 @@ describe('core/data/EntityStore.js', () => {
// Create a new entry
const entity = store.create();
entity.factor = 6844.41;
entity.decimalPrecision = 2;
entity.symbol = 'Ƀ';
entity.shortName = 'BTC';
entity.name = 'Bitcoin';
Expand Down Expand Up @@ -165,6 +168,7 @@ describe('core/data/EntityStore.js', () => {
const entity = store.create();
entity.factor = 6844.41;
entity.symbol = 'Ƀ';
entity.decimalPrecision = 2;
entity.shortName = 'BTC';
entity.name = 'Bitcoin';

Expand Down Expand Up @@ -275,6 +279,7 @@ describe('core/data/EntityStore.js', () => {

const entity = store.create();
entity.factor = 6844.41;
entity.decimalPrecision = 2;
entity.symbol = 'Ƀ';
entity.shortName = 'BTC';
entity.name = 'Bitcoin';
Expand All @@ -294,6 +299,7 @@ describe('core/data/EntityStore.js', () => {
const entity = new EntityProxy('currency', 'currencyService');
entity.setLocalData({
factor: 6844.41,
decimalPrecision: 2,
symbol: 'Ƀ',
shortName: 'BTC',
name: 'Bitcoin'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"shortName": "JPY",
"name": "Yen",
"factor": "01",
"symbol": "¥"
"symbol": "¥",
"decimalPrecision": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
.fillField('input[name=sw-field--currency-name]', 'Yen')
.fillField('input[name=sw-field--currency-shortName]', 'JPY')
.fillField('input[name=sw-field--currency-symbol]', '¥')
.fillField('input[name=sw-field--currency-decimalPrecision]', '2')
.fillField('input[name=sw-field--currency-factor]', '1.0076')
.click(page.elements.currencySaveAction)
.checkNotification('Currency "Yen" has been saved successfully.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const fixture = {
shortName: 'AD',
name: 'Aserbaidschanische Drachme',
factor: '0.12',
symbol: 'A'
symbol: 'A',
decimalPrecision: '2'
};

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions src/Core/Checkout/Cart/Delivery/DeliveryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private function recalculatePosition(
$definition = new QuantityPriceDefinition(
$item->getPrice()->getUnitPrice(),
$item->getPrice()->getTaxRules(),
$context->getContext()->getCurrencyPrecision(),
$quantity,
true
);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Checkout/Cart/Delivery/DeliveryCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function calculateShippingCosts(float $price, LineItemCollection $calcul
$calculatedLineItems->getPrices()->sum()
);

$definition = new QuantityPriceDefinition($price, $rules, 1, true);
$definition = new QuantityPriceDefinition($price, $rules, $context->getContext()->getCurrencyPrecision(), 1, true);

return $this->priceCalculator->calculate($definition, $context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Checkout/Cart/Price/AbsolutePriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function calculate(float $price, PriceCollection $prices, CheckoutContext
{
$taxRules = $this->percentageTaxRuleBuilder->buildRules($prices->sum());

$priceDefinition = new QuantityPriceDefinition($price, $taxRules, 1, true);
$priceDefinition = new QuantityPriceDefinition($price, $taxRules, $context->getContext()->getCurrencyPrecision(), 1, true);

return $this->priceCalculator->calculate($priceDefinition, $context);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Checkout/Cart/Price/AmountCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function calculateGrossAmount(PriceCollection $prices, PriceCollection $
$taxes = $this->taxAmountCalculator->calculate($allPrices, $context);

$net = $total->getTotalPrice() - $taxes->getAmount();
$net = $this->rounding->round($net);
$net = $this->rounding->round($net, $context->getContext()->getCurrencyPrecision());

return new CartPrice(
$net,
Expand All @@ -113,7 +113,7 @@ private function calculateNetAmount(PriceCollection $prices, PriceCollection $sh
$taxes = $this->taxAmountCalculator->calculate($all, $context);

$gross = $total->getTotalPrice() + $taxes->getAmount();
$gross = $this->rounding->round($gross);
$gross = $this->rounding->round($gross, $context->getContext()->getCurrencyPrecision());

return new CartPrice(
$total->getTotalPrice(),
Expand Down
8 changes: 5 additions & 3 deletions src/Core/Checkout/Cart/Price/GrossPriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public function calculate(QuantityPriceDefinition $definition): CalculatedPrice
$unitPrice = $this->getUnitPrice($definition);

$price = $this->priceRounding->round(
$unitPrice * $definition->getQuantity()
$unitPrice * $definition->getQuantity(),
$definition->getPrecision()
);

$calculatedTaxes = $this->taxCalculator->calculateGrossTaxes($price, $definition->getTaxRules());
$calculatedTaxes = $this->taxCalculator->calculateGrossTaxes($price, $definition->getPrecision(), $definition->getTaxRules());

return new CalculatedPrice(
$unitPrice,
Expand All @@ -65,9 +66,10 @@ private function getUnitPrice(QuantityPriceDefinition $definition): float

$price = $this->taxCalculator->calculateGross(
$definition->getPrice(),
$definition->getPrecision(),
$definition->getTaxRules()
);

return $this->priceRounding->round($price);
return $this->priceRounding->round($price, $definition->getPrecision());
}
}
10 changes: 7 additions & 3 deletions src/Core/Checkout/Cart/Price/NetPriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ public function calculate(QuantityPriceDefinition $definition): CalculatedPrice
$unitPrice = $this->getUnitPrice($definition);

$price = $this->priceRounding->round(
$unitPrice * $definition->getQuantity()
$unitPrice * $definition->getQuantity(),
$definition->getPrecision()
);

$taxRules = $definition->getTaxRules();

$calculatedTaxes = $this->taxCalculator->calculateNetTaxes($price, $definition->getTaxRules());
$calculatedTaxes = $this->taxCalculator->calculateNetTaxes($price, $definition->getPrecision(), $definition->getTaxRules());

return new CalculatedPrice($unitPrice, $price, $calculatedTaxes, $taxRules, $definition->getQuantity());
}
Expand All @@ -59,6 +60,9 @@ private function getUnitPrice(QuantityPriceDefinition $definition): float
return $definition->getPrice();
}

return $this->priceRounding->round($definition->getPrice());
return $this->priceRounding->round(
$definition->getPrice(),
$definition->getPrecision()
);
}
}
7 changes: 5 additions & 2 deletions src/Core/Checkout/Cart/Price/PercentagePriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ public function calculate(float $percentage, PriceCollection $prices, CheckoutCo
{
$price = $prices->sum();

$discount = $this->rounding->round($price->getTotalPrice() / 100 * $percentage);
$discount = $this->rounding->round(
$price->getTotalPrice() / 100 * $percentage,
$context->getContext()->getCurrencyPrecision()
);

$rules = $this->percentageTaxRuleBuilder->buildRules($price);

$definition = new QuantityPriceDefinition($discount, $rules, 1, true);
$definition = new QuantityPriceDefinition($discount, $rules, $context->getContext()->getCurrencyPrecision(), 1, true);

return $this->priceCalculator->calculate($definition, $context);
}
Expand Down
14 changes: 2 additions & 12 deletions src/Core/Checkout/Cart/Price/PriceRounding.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@

class PriceRounding
{
/**
* @var int
*/
protected $precisions;

public function __construct(int $precisions)
{
$this->precisions = $precisions;
}

public function round(float $price): float
public function round(float $price, int $precision): float
{
return round($price, $this->precisions);
return round($price, $precision);
}
}
13 changes: 12 additions & 1 deletion src/Core/Checkout/Cart/Price/Struct/AbsolutePriceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ class AbsolutePriceDefinition extends Struct implements PriceDefinitionInterface
*/
protected $filter;

public function __construct(float $price, ?Rule $filter = null)
/**
* @var int
*/
protected $precision;

public function __construct(float $price, int $precision, ?Rule $filter = null)
{
$this->price = $price;
$this->filter = $filter;
$this->precision = $precision;
}

public function getFilter(): ?Rule
Expand All @@ -34,4 +40,9 @@ public function getPrice(): float
{
return $this->price;
}

public function getPrecision(): int
{
return $this->precision;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ class PercentagePriceDefinition extends Struct implements PriceDefinitionInterfa
*/
protected $filter;

public function __construct(float $percentage, ?Rule $filter = null)
/**
* @var int
*/
protected $precision;

public function __construct(float $percentage, int $precision, ?Rule $filter = null)
{
$this->percentage = $percentage;
$this->filter = $filter;
$this->precision = $precision;
}

public function getPercentage(): float
Expand All @@ -34,4 +40,9 @@ public function getFilter(): ?Rule
{
return $this->filter;
}

public function getPrecision(): int
{
return $this->precision;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

interface PriceDefinitionInterface
{
/**
* Returns the decimal precision for the price. Necessary for \Shopware\Core\Checkout\Cart\Price\PriceRounding::round
*/
public function getPrecision(): int;
}
18 changes: 18 additions & 0 deletions src/Core/Checkout/Cart/Price/Struct/QuantityPriceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ class QuantityPriceDefinition extends Struct implements PriceDefinitionInterface
*/
protected $isCalculated;

/**
* @var int
*/
protected $precision;

public function __construct(
float $price,
TaxRuleCollection $taxRules,
int $precision,
int $quantity = 1,
bool $isCalculated = false
) {
$this->price = $price;
$this->taxRules = $taxRules;
$this->quantity = $quantity;
$this->isCalculated = $isCalculated;
$this->precision = $precision;
}

public static function fromArray(array $data): self
Expand All @@ -55,6 +62,7 @@ function (array $tax) {
return new self(
(float) $data['price'],
new TaxRuleCollection($taxRules),
(int) $data['precision'],
(int) $data['quantity'],
(bool) $data['isCalculated']
);
Expand Down Expand Up @@ -84,4 +92,14 @@ public function setQuantity(int $quantity): void
{
$this->quantity = $quantity;
}

public function getPrecision(): int
{
return $this->precision;
}

public function setPrecision(int $precision): void
{
$this->precision = $precision;
}
}
1 change: 1 addition & 0 deletions src/Core/Checkout/Cart/PriceActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function calculate(Request $request, Context $context): JsonResponse
$definition = new QuantityPriceDefinition(
$price,
new TaxRuleCollection([new TaxRule($tax->getTaxRate())]),
$context->getCurrencyPrecision(),
$quantity,
$preCalculated
);
Expand Down

0 comments on commit 65c34cc

Please sign in to comment.