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/redchamps/module-total-adjustment/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/redchamps/module-total-adjustment/Model/AdjustmentManager.php
<?php
/**
 * @author RedChamps Team
 * @copyright Copyright (c) RedChamps (https://redchamps.com/)
 * @package RedChamps_TotalAdjustment
 */
namespace RedChamps\TotalAdjustment\Model;

use Magento\Framework\Serialize\Serializer\Json;
use Psr\Log\LoggerInterface;

class AdjustmentManager
{
    /**
     * @var Json
     */
    private $serializer;
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        Json $serializer,
        LoggerInterface $logger
    ) {
        $this->serializer = $serializer;
        $this->logger = $logger;
    }

    public function encodeAdjustments($adjustments)
    {
        return $this->serializer->serialize($adjustments);
    }

    public function decodeAdjustments($adjustments)
    {
        try {
            $decodedAdjustments = $this->serializer->unserialize($adjustments);
            return is_array($decodedAdjustments)?$decodedAdjustments:[];
        } catch (\Exception $exception) {
            $this->logger->critical(
                "Error while decoding adjustments in extension RedChamps_TotalAdjustment: ".$exception->getMessage()
            );
        }
        return [];
    }

    public function getPendingAdjustments($order)
    {
        $orderAdjustments = $order->getAdjustments();
        if ($orderAdjustments) {
            $orderAdjustments = $this->decodeAdjustments($orderAdjustments);
            $invoicedAdjustments = $order->getAdjustmentsInvoiced();
            if ($invoicedAdjustments) {
                $invoicedAdjustments = $this->decodeAdjustments($invoicedAdjustments);
                foreach ($orderAdjustments as $adjustmentNumber => $adjustment) {
                    foreach ($invoicedAdjustments as $invoicedAdjustment) {
                        if ($adjustment["title"] == $invoicedAdjustment["title"]) {
                            if ($adjustment["amount"] == $invoicedAdjustment["amount"]) {
                                unset($orderAdjustments[$adjustmentNumber]);
                            } else {
                                $orderAdjustments[$adjustmentNumber]["amount"] = $adjustment["amount"] - $invoicedAdjustment["amount"];
                            }
                        }
                    }
                }
            }
        }
        return $orderAdjustments;
    }

    public function mergeAdjustments($source, $target)
    {
        if ($target) {
            $target = $this->decodeAdjustments($target);
            $source = $this->decodeAdjustments($source);
            foreach ($source as $adjustment) {
                $foundAdjustmentNumber = null;
                foreach ($target as $targetAdjustmentNumber => $targetAdjustment) {
                    if ($targetAdjustment["title"] == $adjustment["title"]) {
                        $foundAdjustmentNumber = $targetAdjustmentNumber;
                        break;
                    }
                }
                if (is_null($foundAdjustmentNumber)) {
                    $target[] = $adjustment;
                } else {
                    $target[$foundAdjustmentNumber]["amount"]+=  $adjustment["amount"];
                    $target[$foundAdjustmentNumber]["base_amount"]+= $adjustment["base_amount"];
                }
            }
            $target = $this->encodeAdjustments($target);
        } else {
            $target = $source;
        }
        return $target;
    }
}

Spamworldpro Mini