Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/vendor/fooman/pdfcustomiser-implementation-m2/src/Block/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/fooman/pdfcustomiser-implementation-m2/src/Block/TaxTable.php
<?php
namespace Fooman\PdfCustomiser\Block;

use Fooman\PdfCustomiser\Model\ResourceModel\Order\Tax\Item\CollectionFactory;
use Magento\Framework\Module\Manager;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\Template\Context;
use Magento\Tax\Helper\Data;

/**
 * Block to display breakdown of taxes
 *
 * @author     Kristof Ringleff
 * @copyright  Copyright (c) 2009 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
class TaxTable extends \Fooman\PdfCore\Block\Pdf\PdfAbstract
{
    const XML_PATH_DISPLAY_TAX_SUMMARY = 'sales_pdf/all/alltaxsummary';

    // phpcs:ignore PSR2.Classes.PropertyDeclaration
    protected $_template = 'Fooman_PdfCustomiser::pdf/taxtable.phtml';

    /**
     * @var Data
     */
    private $taxHelper;

    /**
     * @var PriceCurrencyInterface
     */
    private $priceCurrency;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var CollectionFactory
     */
    private $taxOrderItemCollectionFactory;

    /**
     * @var Manager
     */
    private $moduleManager;

    private $accumulatedTaxes = [];
    private $unallocatedTotals = [];

    public function __construct(
        Context $context,
        PriceCurrencyInterface $priceCurrency,
        Data $taxHelper,
        CollectionFactory $taxOrderItemCollectionFactory,
        Manager $moduleManager,
        array $data = []
    ) {
        $this->scopeConfig = $context->getScopeConfig();
        $this->taxHelper = $taxHelper;
        $this->priceCurrency = $priceCurrency;
        $this->taxOrderItemCollectionFactory = $taxOrderItemCollectionFactory;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $data);
    }

    public function getItems()
    {
        $output = [];
        $this->unallocatedTotals = [];
        $this->accumulatedTaxes = [];

        if (!$this->scopeConfig->isSetFlag(
            self::XML_PATH_DISPLAY_TAX_SUMMARY,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $this->getSalesObject()->getStoreId()
        )) {
            return $output;
        }
        $this->accumulateShippingTax($this->getSalesObject());
        foreach ($this->getSalesObject()->getAllItems() as $item) {
            if ($item instanceof \Magento\Sales\Api\Data\OrderItemInterface) {
                $orderItem = $item;
            } else {
                $orderItem = $item->getOrderItem();
            }
            $taxItems = $this->getTaxItems($orderItem);
            if (count($taxItems)) {
                foreach ($taxItems as $taxItem) {
                    $this->aggregateTaxFromTaxItem($taxItem->getTaxPercent(), $taxItem, $item, $orderItem);
                }
            } else {
                $this->unallocatedTotals[] = new \Magento\Framework\DataObject(
                    [
                        'base_row_total' => $this->sumTaxBasis($item),
                        'base_tax_amount' => $item->getBaseTaxAmount()
                    ]
                );
            }
        }

        try {
            $taxes = $this->taxHelper->getCalculatedTaxes($this->getSalesObject());
        } catch (\Throwable $e) {
            $taxes = [];
        }

        $this->addFoomanSurcharges($this->getSalesObject());

        if (!empty($taxes)) {
            foreach ($taxes as $tax) {
                $this->checkIfunallocatedApplies($tax);
                $tax['base_tax_basis'] = $this->formatCurrency(
                    $this->accumulatedTaxes[$tax['percent']]['base_tax_basis']
                );
                $tax['base_subtotal'] = $this->formatCurrency(
                    $this->accumulatedTaxes[$tax['percent']]['base_tax_basis']
                    +  $this->accumulatedTaxes[$tax['percent']]['base_tax_amount']
                );
                $tax['base_tax_amount'] = $this->formatCurrency(
                    $this->accumulatedTaxes[$tax['percent']]['base_tax_amount']
                );
                $output[] = $tax;
            }
        }
        if (!empty($this->unallocatedTotals)) {
            foreach ($this->unallocatedTotals as $total) {
                $this->aggregateTax('0', $total);
            }
            $output[] = [
                'percent' => 0,
                'base_tax_basis' => $this->formatCurrency($this->accumulatedTaxes[0]['base_tax_basis']),
                'base_tax_amount' => $this->formatCurrency($this->accumulatedTaxes[0]['base_tax_amount']),
                'base_subtotal' => $this->formatCurrency(
                    $this->accumulatedTaxes[0]['base_tax_basis'] + $this->accumulatedTaxes[0]['base_tax_amount']
                )
            ];
        }

        return $output;
    }

    public function addFoomanSurcharges($salesObject)
    {
        if (!$this->moduleManager->isEnabled('Fooman_Totals')) {
            return;
        }
        $extAttr = $salesObject->getExtensionAttributes();
        if (!$extAttr) {
            return;
        }

        $totalGroup = $extAttr->getFoomanTotalGroup();
        if ($totalGroup) {
            $orderTotals = $totalGroup->getItems();
            if (!empty($orderTotals)) {
                foreach ($orderTotals as $total) {
                    $this->unallocatedTotals[] = new \Magento\Framework\DataObject(
                        [
                            'base_row_total' => $total->getBaseAmount(),
                            'base_tax_amount' => $total->getBaseTaxAmount()
                        ]
                    );
                }
            }
        }
    }

    private function getTaxItems($orderItem)
    {
        if (!$orderItem) {
            return [];
        }
        return $this->taxOrderItemCollectionFactory->create()->getTaxItemsByItemId($orderItem->getItemId());
    }

    /**
     * Magento does not explicitly keep track of the tax rate of custom totals
     * we re-calculate against the existing tax rates and match based on proximity
     * due to unknown possible rounding
     * @param array $rate
     */
    private function checkIfunallocatedApplies($rate)
    {
        foreach ($this->unallocatedTotals as $key => $total) {
            $toCheck = $total->getBaseRowTotal() * ($rate['percent']/100);
            $diff = abs($toCheck - $total->getBaseTaxAmount());
            if ($diff < 0.02) {
                $this->aggregateTax($rate['percent'], $total);
                unset($this->unallocatedTotals[$key]);
            }
        }
    }

    private function formatCurrency($amount)
    {
        if ($amount === null) {
            return '';
        }
        return $this->priceCurrency->format(
            $amount,
            true,
            PriceCurrencyInterface::DEFAULT_PRECISION,
            null,
            $this->getSalesObject()->getBaseCurrencyCode()
        );
    }

    private function aggregateTax($rate, $item)
    {
        if (isset($this->accumulatedTaxes[$rate])) {
            $this->accumulatedTaxes[$rate]['base_tax_basis'] += $this->sumTaxBasis($item);
            $this->accumulatedTaxes[$rate]['base_tax_amount'] += $item->getBaseTaxAmount();
        } else {
            $this->accumulatedTaxes[$rate]['base_tax_basis'] = $this->sumTaxBasis($item);
            $this->accumulatedTaxes[$rate]['base_tax_amount'] = $item->getBaseTaxAmount();
        }
    }

    private function accumulateShippingTax($salesObject)
    {
        if ($salesObject instanceof \Magento\Sales\Api\Data\OrderInterface) {
            $orderId = $salesObject->getEntityId();
        } else {
            $orderId = $salesObject->getOrderId();
        }
        $taxItemCollection = $this->taxOrderItemCollectionFactory->create()
            ->getTaxItemsByTypeForOrderId($orderId, 'shipping');

        foreach ($taxItemCollection as $item) {
            $rate = $item->getTaxPercent();
            if (isset($this->accumulatedTaxes[$rate])) {
                $this->accumulatedTaxes[$rate]['base_tax_basis'] += $this->sumShippingTaxBasis($salesObject);
                $this->accumulatedTaxes[$rate]['base_tax_amount'] += $salesObject->getBaseShippingTaxAmount();
            } else {
                $this->accumulatedTaxes[$rate]['base_tax_basis'] = $this->sumShippingTaxBasis($salesObject);
                $this->accumulatedTaxes[$rate]['base_tax_amount'] = $salesObject->getBaseShippingTaxAmount();
            }
        }
    }

    private function aggregateTaxFromTaxItem($rate, $taxIt, $item, $orderItem)
    {
        $orderTaxBasis = $this->sumTaxBasis($orderItem);
        if ($orderTaxBasis == 0) {
            $factor = 1;
        } else {
            $factor = $this->sumTaxBasis($item) / $orderTaxBasis;
        }
        if (isset($this->accumulatedTaxes[$rate])) {
            $this->accumulatedTaxes[$rate]['base_tax_basis'] += $this->sumTaxBasis($item);
            $this->accumulatedTaxes[$rate]['base_tax_amount'] +=
                $factor * $taxIt->getRealBaseAmount();
        } else {
            $this->accumulatedTaxes[$rate]['base_tax_basis'] = $this->sumTaxBasis($item);
            $this->accumulatedTaxes[$rate]['base_tax_amount'] =
                $factor * $taxIt->getRealBaseAmount();
        }
    }

    private function sumTaxBasis($item)
    {
        return $item->getBaseRowTotal()
            - $item->getBaseDiscountAmount()
            + $item->getBaseDiscountTaxCompensationAmount();
    }

    private function sumShippingTaxBasis($salesObject)
    {
        return $salesObject->getBaseShippingAmount()
            - $salesObject->getBaseShippingDiscountAmount()
            + $salesObject->getBaseShippingDiscountTaxCompensationAmnt();
    }
}

Spamworldpro Mini