Skip to content

Commit

Permalink
Release 1.0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Schurter committed Dec 11, 2018
1 parent 58eb383 commit bac44b5
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 142 deletions.
4 changes: 3 additions & 1 deletion Helper/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ public function reduceAmount(array $items, $expectedAmount)

$appliedAmount = 0;
foreach ($items as $item) {
$item->setAmountIncludingTax($item->getAmountIncludingTax() * $factor);
if ($item->getUniqueId() != 'shipping') {
$item->setAmountIncludingTax($item->getAmountIncludingTax() * $factor);
}
$appliedAmount += $item->getAmountIncludingTax();
}

Expand Down
26 changes: 23 additions & 3 deletions Helper/LineItemReduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,39 @@
namespace PostFinanceCheckout\Payment\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

/**
* Helper to provide line item reduction related functionality.
*/
class LineItemReduction extends AbstractHelper
{

/**
*
* @var Data
*/
protected $_helper;

/**
*
* @param Context $context
* @param Data $helper
*/
public function __construct(Context $context, Data $helper)
{
parent::__construct($context);
$this->_helper = $helper;
}

/**
* Gets the amount of the line item's reductions.
*
* @param \PostFinanceCheckout\Sdk\Model\LineItem[] $lineItems
* @param \PostFinanceCheckout\Sdk\Model\LineItemReduction[] $reductions
* @param string $currency
*/
public function getReducedAmount(array $lineItems, array $reductions)
public function getReducedAmount(array $lineItems, array $reductions, $currency)
{
$lineItemMap = array();
foreach ($lineItems as $lineItem) {
Expand All @@ -34,11 +53,12 @@ public function getReducedAmount(array $lineItems, array $reductions)
$amount = 0;
foreach ($reductions as $reduction) {
$lineItem = $lineItemMap[$reduction->getLineItemUniqueId()];
$amount += $lineItem->getUnitPriceIncludingTax() * $reduction->getQuantityReduction();
$unitPrice = $lineItem->getAmountIncludingTax() / $lineItem->getQuantity();
$amount += $unitPrice * $reduction->getQuantityReduction();
$amount += $reduction->getUnitPriceReduction() *
($lineItem->getQuantity() - $reduction->getQuantityReduction());
}

return $amount;
return $this->_helper->roundAmount($amount, $currency);
}
}
163 changes: 47 additions & 116 deletions Model/Service/AbstractLineItemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,23 @@ protected function convertLineItems($entity)

foreach ($entity->getAllItems() as $entityItem) {
if ($this->isIncludeItem($entityItem)) {
$items = \array_merge($items, $this->convertItem($entityItem, $entity));
$items[] = $this->convertItem($entityItem, $entity);
}
}

$items = \array_merge($items, $this->convertShippingLineItem($entity));
$shippingLineItems = $this->convertShippingLineItem($entity);
if ($shippingLineItems instanceof LineItemCreate) {
$items[] = $shippingLineItems;
}

$transport = new DataObject([
'items' => $items
]);
$this->_eventManager->dispatch('postfinancecheckout_payment_convert_line_items', [
'transport' => $transport,
'entity' => $entity
]);
$this->_eventManager->dispatch('postfinancecheckout_payment_convert_line_items',
[
'transport' => $transport,
'entity' => $entity
]);
return $transport->getData('items');
}

Expand Down Expand Up @@ -158,47 +162,27 @@ protected function isIncludeItem($entityItem)
*
* @param \Magento\Quote\Model\Quote\Item|\Magento\Sales\Model\Order\Item|\Magento\Sales\Model\Order\Invoice\Item $entityItem
* @param \Magento\Quote\Model\Quote|\Magento\Sales\Model\Order|\Magento\Sales\Model\Order\Invoice $entity
* @return LineItemCreate[]
* @return LineItemCreate
*/
protected function convertItem($entityItem, $entity)
{
$items = [];

$items[] = $this->convertProductItem($entityItem, $entity);
$amountIncludingTax = $entityItem->getRowTotal() - $entityItem->getDiscountAmount() + $entityItem->getTaxAmount() +
$entityItem->getDiscountTaxCompensationAmount();

$discountItem = $this->convertDiscountItem($entityItem, $entity);
if ($discountItem instanceof LineItemCreate) {
$items[] = $discountItem;
}

return $items;
}

/**
* Converts the given entity item to a line item.
*
* @param \Magento\Quote\Model\Quote\Item|\Magento\Sales\Model\Order\Item|\Magento\Sales\Model\Order\Invoice\Item $entityItem
* @param \Magento\Quote\Model\Quote|\Magento\Sales\Model\Order|\Magento\Sales\Model\Order\Invoice $entity
* @return LineItemCreate
*/
protected function convertProductItem($entityItem, $entity)
{
$productItem = new LineItemCreate();
$productItem->setType(LineItemType::PRODUCT);
$productItem->setUniqueId($this->getUniqueId($entityItem));
$productItem->setAmountIncludingTax(
$this->_helper->roundAmount($entityItem->getRowTotalInclTax(), $this->getCurrencyCode($entity)));
$this->_helper->roundAmount($amountIncludingTax, $this->getCurrencyCode($entity)));
$productItem->setName($this->_helper->fixLength($entityItem->getName(), 150));
$productItem->setQuantity($entityItem->getQty() ? $entityItem->getQty() : $entityItem->getQtyOrdered());
$productItem->setShippingRequired(! $entityItem->getIsVirtual());
$productItem->setSku($this->_helper->fixLength($entityItem->getSku(), 200));
if ($entityItem->getTaxPercent() > 0) {
$tax = $this->getTax($entityItem);
if ($tax instanceof TaxCreate) {
$productItem->setTaxes([
$tax
]);
}
$tax = $this->getTax($entityItem);
if ($tax instanceof TaxCreate) {
$productItem->setTaxes([
$tax
]);
}
$attributes = $this->getAttributes($entityItem);
if (! empty($attributes)) {
Expand All @@ -207,43 +191,6 @@ protected function convertProductItem($entityItem, $entity)
return $productItem;
}

/**
* Converts the given entity item to a line item representing its discount if any.
*
* @param \Magento\Quote\Model\Quote\Item|\Magento\Sales\Model\Order\Item|\Magento\Sales\Model\Order\Invoice\Item $entityItem
* @param \Magento\Quote\Model\Quote|\Magento\Sales\Model\Order|\Magento\Sales\Model\Order\Invoice $entity
* @return LineItemCreate
*/
protected function convertDiscountItem($entityItem, $entity)
{
if ($entityItem->getDiscountAmount() > 0) {
if ($this->_taxHelper->priceIncludesTax($entityItem->getStore()) ||
! $this->_taxHelper->applyTaxAfterDiscount($entityItem->getStore())) {
$discountAmount = $entityItem->getDiscountAmount();
} else {
$discountAmount = $entityItem->getDiscountAmount() * ($entityItem->getTaxPercent() / 100 + 1);
}

$discountItem = new LineItemCreate();
$discountItem->setType(LineItemType::DISCOUNT);
$discountItem->setUniqueId($this->getUniqueId($entityItem) . '-discount');
$discountItem->setAmountIncludingTax(
$this->_helper->roundAmount($discountAmount * - 1, $this->getCurrencyCode($entity)));
$discountItem->setName((String) \__('Discount'));
$discountItem->setQuantity($entityItem->getQty() ? $entityItem->getQty() : $entityItem->getQtyOrdered());
$discountItem->setSku($this->_helper->fixLength($entityItem->getSku(), 191) . '-discount');
if ($this->_taxHelper->applyTaxAfterDiscount($entityItem->getStore()) && $entityItem->getTaxPercent()) {
$tax = $this->getTax($entityItem);
if ($tax instanceof TaxCreate) {
$discountItem->setTaxes([
$tax
]);
}
}
return $discountItem;
}
}

/**
* Gets the key of the given product option.
*
Expand All @@ -267,14 +214,18 @@ protected function getAttributeKey($option)
*/
protected function getTax($entityItem)
{
$taxClassId = $entityItem->getProduct()->getTaxClassId();
if ($taxClassId > 0) {
$taxClass = $this->_taxClassRepository->get($taxClassId);
if ($entityItem->getTaxAmount() > 0 && $entityItem->getTaxPercent() > 0) {
$taxClassId = $entityItem->getProduct()->getTaxClassId();
if ($taxClassId > 0) {
$taxClass = $this->_taxClassRepository->get($taxClassId);

$tax = new TaxCreate();
$tax->setRate($entityItem->getTaxPercent());
$tax->setTitle($taxClass->getClassName());
return $tax;
$tax = new TaxCreate();
$tax->setRate($entityItem->getTaxPercent());
$tax->setTitle($taxClass->getClassName());
return $tax;
}
} else {
return null;
}
}

Expand All @@ -294,8 +245,10 @@ abstract protected function getAttributes($entityItem);
*/
protected function convertShippingLineItem($entity)
{
return $this->convertShippingLineItemInner($entity, $entity->getShippingInclTax(),
$entity->getShippingDescription(), $entity->getShippingDiscountAmount());
return $this->convertShippingLineItemInner($entity, $entity->getShippingAmount(),
$entity->getShippingTaxAmount(),
$entity->getShippingDiscountAmount() - $entity->getShippingDiscountTaxCompensationAmount(),
$entity->getShippingDescription());
}

/**
Expand All @@ -307,61 +260,39 @@ protected function convertShippingLineItem($entity)
* @param float $shippingDiscountAmount
* @return LineItemCreate
*/
protected function convertShippingLineItemInner($entity, $shippingAmount, $shippingDescription, $shippingDiscountAmount)
protected function convertShippingLineItemInner($entity, $shippingAmount, $shippingTaxAmount, $shippingDiscountAmount,
$shippingDescription)
{
$items = [];
if ($shippingAmount > 0) {
$shippingItem = new LineItemCreate();
$shippingItem->setType(LineItemType::SHIPPING);
$shippingItem->setUniqueId('shipping');
$shippingItem->setAmountIncludingTax(
$this->_helper->roundAmount($shippingAmount, $this->getCurrencyCode($entity)));
$this->_helper->roundAmount($shippingAmount + $shippingTaxAmount - $shippingDiscountAmount,
$this->getCurrencyCode($entity)));
if ($this->_scopeConfig->getValue('postfinancecheckout_payment/line_items/overwrite_shipping_description',
ScopeInterface::SCOPE_STORE, $entity->getStoreId())) {
$shippingItem->setName(
$this->_helper->fixLength($this->_scopeConfig->getValue(
'postfinancecheckout_payment/line_items/custom_shipping_description',
ScopeInterface::SCOPE_STORE, $entity->getStoreId()), 150));
$this->_helper->fixLength(
$this->_scopeConfig->getValue(
'postfinancecheckout_payment/line_items/custom_shipping_description',
ScopeInterface::SCOPE_STORE, $entity->getStoreId()), 150));
} else {
$shippingItem->setName($this->_helper->fixLength($shippingDescription, 150));
}
$shippingItem->setQuantity(1);
$shippingItem->setSku('shipping');
$tax = $this->getShippingTax($entity);
if ($tax instanceof TaxCreate) {
$shippingItem->setTaxes([
$tax
]);
}
$items[] = $shippingItem;

$discountItem = $this->getShippingDiscountLineItem($entity, $shippingItem->getName(), $shippingDiscountAmount);
if ($discountItem instanceof LineItemCreate) {
$items[] = $discountItem;
}
}
return $items;
}

protected function getShippingDiscountLineItem($entity, $shippingDescription, $shippingDiscountAmount) {
if ($shippingDiscountAmount > 0) {
$discountItem = new LineItemCreate();
$discountItem->setType(LineItemType::DISCOUNT);
$discountItem->setUniqueId('shipping-discount');
$discountItem->setAmountIncludingTax(
$this->_helper->roundAmount($shippingDiscountAmount * - 1, $this->getCurrencyCode($entity)));
$discountItem->setName((String) \__('Shipping Discount'));
$discountItem->setQuantity(1);
$discountItem->setSku('shipping-discount');
if ($this->_taxHelper->applyTaxAfterDiscount($entity->getStore())) {
if ($shippingTaxAmount > 0) {
$tax = $this->getShippingTax($entity);
if ($tax instanceof TaxCreate) {
$discountItem->setTaxes([
$shippingItem->setTaxes([
$tax
]);
}
}
return $discountItem;
return $shippingItem;
} else {
return null;
}
}

Expand Down
29 changes: 28 additions & 1 deletion Model/Service/Invoice/LineItemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PostFinanceCheckout\Payment\Helper\LineItem as LineItemHelper;
use PostFinanceCheckout\Payment\Model\Service\AbstractLineItemService;
use PostFinanceCheckout\Sdk\Model\LineItemAttributeCreate;
use PostFinanceCheckout\Sdk\Model\TaxCreate;

/**
* Service to handle line items in invoice context.
Expand Down Expand Up @@ -85,6 +86,29 @@ protected function getAttributes($entityItem)
return $attributes;
}

/**
* Gets the tax for the given invoice item.
*
* @param Invoice\Item $entityItem
* @return TaxCreate
*/
protected function getTax($entityItem)
{
if ($entityItem->getTaxAmount() > 0 && $entityItem->getOrderItem()->getTaxPercent() > 0) {
$taxClassId = $entityItem->getOrderItem()->getProduct()->getTaxClassId();
if ($taxClassId > 0) {
$taxClass = $this->_taxClassRepository->get($taxClassId);

$tax = new TaxCreate();
$tax->setRate($entityItem->getOrderItem()->getTaxPercent());
$tax->setTitle($taxClass->getClassName());
return $tax;
}
} else {
return null;
}
}

/**
* Converts the invoice's shipping information to a line item.
*
Expand All @@ -93,7 +117,10 @@ protected function getAttributes($entityItem)
*/
protected function convertShippingLineItem($invoice)
{
return $this->convertShippingLineItemInner($invoice, $invoice->getShippingInclTax(),
return $this->convertShippingLineItemInner($invoice, $invoice->getShippingAmount(),
$invoice->getShippingTaxAmount(),
$invoice->getOrder()
->getShippingDiscountAmount() - $invoice->getShippingDiscountTaxCompensationAmount(),
$invoice->getOrder()
->getShippingDescription());
}
Expand Down
6 changes: 3 additions & 3 deletions Model/Service/LineItemReductionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ protected function fixReductions(array $reductions, Creditmemo $creditmemo)
->getPostfinancecheckoutSpaceId(),
$creditmemo->getOrder()
->getPostfinancecheckoutTransactionId());
$reducedAmount = $this->_reductionHelper->getReducedAmount($baseLineItems, $reductions);
if ($reducedAmount != $creditmemo->getGrandTotal()) {
$reducedAmount = $this->_reductionHelper->getReducedAmount($baseLineItems, $reductions, $creditmemo->getOrderCurrencyCode());
if ($reducedAmount != $this->_helper->roundAmount($creditmemo->getGrandTotal(), $creditmemo->getOrderCurrencyCode())) {
$baseAmount = $this->_lineItemHelper->getTotalAmountIncludingTax($baseLineItems);
$rate = $creditmemo->getGrandTotal() / $baseAmount;
$fixedReductions = [];
Expand All @@ -196,7 +196,7 @@ protected function fixReductions(array $reductions, Creditmemo $creditmemo)
$fixedReductions[] = $reduction;
}
}
$fixedReductionAmount = $this->_reductionHelper->getReducedAmount($baseLineItems, $fixedReductions);
$fixedReductionAmount = $this->_reductionHelper->getReducedAmount($baseLineItems, $fixedReductions, $creditmemo->getOrderCurrencyCode());
$roundingDifference = $creditmemo->getGrandTotal() - $fixedReductionAmount;
return $this->distributeRoundingDifference($fixedReductions, 0, $roundingDifference, $baseLineItems, $creditmemo->getOrderCurrencyCode());
} else {
Expand Down
9 changes: 6 additions & 3 deletions Model/Service/Quote/LineItemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ protected function getAttributes($entityItem)
protected function convertShippingLineItem($quote)
{
return $this->convertShippingLineItemInner($quote, $quote->getShippingAddress()
->getShippingInclTax(), $quote->getShippingAddress()
->getShippingDescription(), $quote->getShippingAddress()
->getShippingDiscountAmount());
->getShippingAmount(), $quote->getShippingAddress()
->getShippingTaxAmount(),
$quote->getShippingAddress()
->getShippingDiscountAmount() - $quote->getShippingAddress()
->getShippingDiscountTaxCompensationAmount(), $quote->getShippingAddress()
->getShippingDescription());
}

/**
Expand Down

0 comments on commit bac44b5

Please sign in to comment.