Guest User

Untitled

a guest
Jan 7th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Apptha\Deal\Model\Total;
  4. use \Magento\Checkout\Model\Session;
  5. class Customfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
  6. {
  7.  
  8. protected $quoteValidator = null;
  9.  
  10. public function __construct(
  11. \Magento\Framework\Event\ManagerInterface $eventManager,
  12. \Magento\Store\Model\StoreManagerInterface $storeManager,
  13. \Magento\SalesRule\Model\Validator $validator,
  14. \Magento\Quote\Model\QuoteValidator $quoteValidator,
  15. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  16. \Apptha\Deal\Helper\Data $dealHelper,
  17. \Apptha\Customergroup\Helper\Data $customergroupHelper)
  18. {
  19. $this->quoteValidator = $quoteValidator;
  20. $this->setCode('customfee');
  21. $this->eventManager = $eventManager;
  22. $this->calculator = $validator;
  23. $this->storeManager = $storeManager;
  24. $this->priceCurrency = $priceCurrency;
  25. $this->dealHelper = $dealHelper;
  26. $this->customergroupHelper = $customergroupHelper;
  27. }
  28. public function collect(
  29. \Magento\Quote\Model\Quote $quote,
  30. \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
  31. \Magento\Quote\Model\Quote\Address\Total $total
  32. ) {
  33. if (!count($shippingAssignment->getItems())) {
  34. return $this;
  35. }
  36. parent::collect($quote, $shippingAssignment, $total);
  37. $label = 'Amount to be paid online';
  38. $TotalAmount = $total->getSubtotal();
  39. $buyerCommission = $this->getCommissionAmount($quote);
  40. $discountAmount = 0;
  41. if($buyerCommission){
  42. $discountAmount = "-".$TotalAmount + ($TotalAmount*($buyerCommission/100)); // Set amount to be added
  43. $appliedCartDiscount = 0;
  44. }
  45.  
  46. $total->setDiscountDescription($label);
  47. $total->setDiscountAmount($discountAmount);
  48. $total->setBaseDiscountAmount($discountAmount);
  49. $total->setSubtotalWithDiscount($discountAmount);
  50. $total->setBaseSubtotalWithDiscount($discountAmount);
  51.  
  52. if(isset($appliedCartDiscount))
  53. {
  54. $total->addTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
  55. $total->addBaseTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
  56. }
  57. else
  58. {
  59. $total->addTotalAmount($this->getCode(), $discountAmount);
  60. $total->addBaseTotalAmount($this->getCode(), $discountAmount);
  61. }
  62. $total->setCustomfee($discountAmount);
  63. $total->setBaseCustomfee($discountAmount);
  64. return $this;
  65. }
  66.  
  67. protected function clearValues(Address\Total $total)
  68. {
  69. $total->setTotalAmount('subtotal', 0);
  70. $total->setBaseTotalAmount('subtotal', 0);
  71. $total->setTotalAmount('tax', 0);
  72. $total->setBaseTotalAmount('tax', 0);
  73. $total->setTotalAmount('discount_tax_compensation', 0);
  74. $total->setBaseTotalAmount('discount_tax_compensation', 0);
  75. $total->setTotalAmount('shipping_discount_tax_compensation', 0);
  76. $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
  77. $total->setSubtotalInclTax(0);
  78. $total->setBaseSubtotalInclTax(0);
  79. }
  80. public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
  81. {
  82. $TotalAmount = $total->getSubtotal();
  83. $buyerCommission = $this->getCommissionAmount($quote);
  84. if(isset($buyerCommission)){
  85. $discountAmount = $TotalAmount*($buyerCommission/100);
  86. return [
  87. 'code' => 'customfee',
  88. 'title' => 'Custom Fee',
  89. 'value' => $discountAmount
  90. ];
  91. }
  92. }
  93.  
  94. public function getCommissionAmount($quote){
  95. $groupId = $this->customergroupHelper->getGroupId();
  96. $groupName = $this->customergroupHelper->getGroupName($groupId);
  97.  
  98. $items = $quote->getAllItems();
  99. foreach($items as $item) {
  100. $productId = $item->getProductId();
  101. }
  102.  
  103. $buyerCommission = $this->checkCommission();
  104.  
  105. if($buyerCommission == 0 && isset($productId)){
  106. $productData = $this->dealHelper->getProductData($productId);//change code here
  107. if($groupName == "Merchants"){
  108. $commissionText = "merchant_commission";
  109. }else{
  110. $commissionText = "buyer_commission";
  111. }
  112. return $productData->getData($commissionText);
  113. }else if($buyerCommission != 0){
  114. return $buyerCommission;
  115. }
  116. }
  117.  
  118. public function checkCommission(){
  119. $buyerCommission = 0;
  120. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  121. $customerSession = $objectManager->get('\Magento\Customer\Model\Session');
  122. if($customerSession->getCustomer()->getId()){
  123. $customerDetails = $this->dealHelper->getCustomerData($customerSession->getCustomer()->getId());
  124. $buyerCommission = $customerDetails->getBuyerCommission();
  125. }
  126. return $buyerCommission;
  127. }
  128.  
  129. }
Add Comment
Please, Sign In to add comment