Skip to content

Instantly share code, notes, and snippets.

@yuklia
Created December 8, 2016 10:35
Show Gist options
  • Save yuklia/45c75ca34978173fd63b15e9d0b843f0 to your computer and use it in GitHub Desktop.
Save yuklia/45c75ca34978173fd63b15e9d0b843f0 to your computer and use it in GitHub Desktop.
<?php
namespace VOID\GiftCards\Model\Quote;
/**
* Class Discount
* @package VOID\GiftCards\Model\Quote
*/
class Discount extends \MageWorx\GiftCards\Model\Quote\Discount
{
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
/**
* Collect address discount amount
*
=======
/**
>>>>>>> Add MageWorx\GiftCards\Discount as preference
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
=======
>>>>>>> Add MageWorx\GiftCards\Discount as preference
*/
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
parent::collect($quote, $shippingAssignment, $total);
$store = $this->storeManager->getStore($quote->getStoreId());
$address = $shippingAssignment->getShipping()->getAddress();
$items = $shippingAssignment->getItems();
if (!count($items)) {
return $this;
}
$eventArgs = [
'website_id' => $store->getWebsiteId(),
'customer_group_id' => $quote->getCustomerGroupId(),
];
$quote->setUseGiftCard(true);
$giftcardsIds = $this->giftcardsSession->getGiftCardsIds();
$this->giftCardBalance = 0;
$this->baseGiftCardBalance = 0;
$this->origGiftCardBalance = 0;
$this->origBaseGiftCardBalance = 0;
$this->shippingDiscountAdditional = 0;
$this->baseShippingDiscountAdditional = 0;
$this->shippingDiscount = 0;
$this->baseShippingDiscount = 0;
$giftCardsDiscount = 0;
$baseGiftCardsDiscount = 0;
$usedCards = [];
if ($this->giftcardsSession->getActive() && $giftcardsIds) {
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
$cards = $this->giftcardsCollection
->create()
->addFieldToFilter('card_id', ['in' => array_keys($giftcardsIds)])
->load();
=======
$cards = $this->giftcardsCollection->create()->addFieldToFilter('card_id', ['in' => array_keys($giftcardsIds)])->load();
>>>>>>> Add MageWorx\GiftCards\Discount as preference
foreach ($cards as $card) {
$this->cards[$card->getId()] = ['balance' => $card->getCardBalance(), 'code' => $card->getCardCode()];
}
foreach ($this->cards as $id => $_card) {
$this->giftCardBalance = $_card['balance'];
$this->baseGiftCardBalance = $_card['balance'];
$this->origGiftCardBalance = $_card['balance'];
$this->origBaseGiftCardBalance = $_card['balance'];
/** @var \Magento\Quote\Model\Quote\Item $item */
foreach ($items as $item) {
// to determine the child item discount, we calculate the parent
if ($item->getParentItem()) {
continue;
}
$eventArgs['item'] = $item;
$this->eventManager->dispatch('sales_quote_address_discount_item', $eventArgs);
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
$this->setItemDiscount($item);
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
=======
// $giftCardsDiscount += $item->getDiscountAmount();
>>>>>>> Add MageWorx\GiftCards\Discount as preference
$baseGiftCardsDiscount += $item->getBaseDiscountAmount();
foreach ($item->getChildren() as $child) {
$this->setItemDiscount($child);
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
=======
// $giftCardsDiscount += $child->getDiscountAmount();
>>>>>>> Add MageWorx\GiftCards\Discount as preference
$baseGiftCardsDiscount += $child->getBaseDiscountAmount();
}
} else {
$this->setItemDiscount($item);
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
=======
// $giftCardsDiscount += $item->getDiscountAmount();
>>>>>>> Add MageWorx\GiftCards\Discount as preference
$baseGiftCardsDiscount += $item->getBaseDiscountAmount();
}
}
if ($this->giftCardBalance > 0 && $address->getShippingAmount()) {
$shippingDiscount = $address->getShippingDiscountAmount();
if ($address->getShippingAmount() > $this->giftCardBalance + $shippingDiscount) {
$address->setShippingDiscountAmount($this->giftCardBalance + $shippingDiscount);
$this->shippingDiscount += $this->giftCardBalance;
$this->giftCardBalance = 0;
} else {
$delta = $address->getShippingAmount() - $shippingDiscount;
$address->setShippingDiscountAmount($delta + $shippingDiscount);
$this->shippingDiscount += $delta;
$this->giftCardBalance -= $delta;
}
$baseShippingDiscount = $address->getBaseShippingDiscountAmount();
if ($address->getBaseShippingAmount() > $this->baseGiftCardBalance + $baseShippingDiscount) {
$address->setBaseShippingDiscountAmount($this->baseGiftCardBalance + $baseShippingDiscount);
$this->baseShippingDiscount += $this->baseGiftCardBalance;
$this->baseGiftCardBalance = 0;
} else {
$delta = $address->getBaseShippingAmount() - $baseShippingDiscount;
$address->setBaseShippingDiscountAmount($delta + $baseShippingDiscount);
$this->baseShippingDiscount += $delta;
$this->baseGiftCardBalance -= $delta;
}
}
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
$originalBalanceDifference = $this->origGiftCardBalance - $item->getDiscountAmount();
$usedCards[$id]['balance'] = $originalBalanceDifference - $address->getShippingDiscountAmount();
}
/** Process shipping amount discount */
if ($this->shippingDiscount) {
=======
$usedCards[$id]['balance'] = $this->origGiftCardBalance - $item->getDiscountAmount() - $address->getShippingDiscountAmount();
}
/** Process shipping amount discount */
if ($this->shippingDiscount) {
// $giftCardsDiscount += $this->shippingDiscount;
>>>>>>> Add MageWorx\GiftCards\Discount as preference
$baseGiftCardsDiscount += $this->baseShippingDiscount;
}
$description = '';
$descriptionArray = $address->getDiscountDescriptionArray();
if ($descriptionArray === null) {
$descriptionArray = [];
}
foreach ($this->cards as $key => $_card) {
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
$description .= ' ' . $_card['code'];
=======
$description .= ' '.$_card['code'];
>>>>>>> Add MageWorx\GiftCards\Discount as preference
$descriptionArray[$key] = $_card['code'];
}
$address->setDiscountDescriptionArray($descriptionArray);
$this->prepareDescription($address);
/**
* Apply this value to Order Summary block
* @param $cards
* @return int
*/
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
$calculateGiftCardsDiscount = function ($cards) {
$giftCardsDiscount = 0;
foreach ($cards as $index => $value) {
$giftCardsDiscount += $cards[$index]['balance'];
=======
$calculateGiftCardsDiscount = function($cards) {
$giftCardsDiscount = 0;
foreach ($cards as $index => $value){
$giftCardsDiscount+= $cards[$index]['balance'];
>>>>>>> Add MageWorx\GiftCards\Discount as preference
}
return $giftCardsDiscount;
};
$giftCardsDiscount = $calculateGiftCardsDiscount($this->cards);
$total->addTotalAmount('mageworx_giftcards', -$giftCardsDiscount);
$total->addBaseTotalAmount('mageworx_giftcards', -$baseGiftCardsDiscount);
$total->setDiscountDescription($address->getDiscountDescription());
$total->setSubtotalWithDiscount($total->getSubtotal() - $giftCardsDiscount);
$total->setBaseSubtotalWithDiscount($total->getBaseSubtotal() - $baseGiftCardsDiscount);
$quote->setMageworxGiftcardsAmount(-$giftCardsDiscount);
$quote->setBaseMageworxGiftcardsAmount(-$baseGiftCardsDiscount);
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
$quote->setMageworxGiftcardsDescription(' Gift Card (' . implode(',', $descriptionArray) . ')');
=======
$quote->setMageworxGiftcardsDescription(' Gift Card ('.implode(',', $descriptionArray).')');
>>>>>>> Add MageWorx\GiftCards\Discount as preference
$quote->setSubtotalWithDiscount($total->getSubtotal() - $giftCardsDiscount);
$quote->setBaseSubtotalWithDiscount($total->getBaseSubtotal() - $baseGiftCardsDiscount);
$quote->setGiftCardsIds($this->cards);
/**
* @param $cards
* @param $key
* @return int
*/
<<<<<<< de6854e41a98cad112625f2c152bae357e1c6623
$calculateRemaining = function ($cards, $key) {
$sum = 0;
foreach ($cards as $index => $value) {
if ($index === $key) {
break;
}
$sum += $cards[$index]['balance'];
=======
$calculateRemaining = function($cards, $key) {
$sum = 0;
foreach ($cards as $index => $value){
if($index === $key){
break;
}
$sum+= $cards[$index]['balance'];
>>>>>>> Add MageWorx\GiftCards\Discount as preference
}
return $sum;
};
$frontData = [];
foreach ($this->cards as $key => $value) {
$frontData[$key]['applied'] = $value['balance'];
$frontData[$key]['remaining'] = $calculateRemaining($this->cards, $key) + $value['balance'];
$frontData[$key]['code'] = $value['code'];
}
$this->giftcardsSession->setFrontOptions($frontData);
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment