TaxAddressManager.php 3.88 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Tax\Model;

use Magento\Customer\Model\Address;
use Magento\Customer\Model\Session;
use Magento\Tax\Api\TaxAddressManagerInterface;

/**
 * Class to save address in customer session.
 */
class TaxAddressManager implements TaxAddressManagerInterface
{
    /**
     * Customer session model.
     *
     * @var Session
     */
    private $customerSession;

    /**
     * @param Session $customerSession
     */
    public function __construct(Session $customerSession)
    {
        $this->customerSession = $customerSession;
    }

    /**
     * Set default Tax Billing and Shipping address into customer session after address save.
     *
     * @param Address $address
     * @return void
     */
    public function setDefaultAddressAfterSave(Address $address)
    {
        if ($this->isDefaultBilling($address)) {
            $this->customerSession->setDefaultTaxBillingAddress(
                [
                    'country_id' => $address->getCountryId(),
                    'region_id' => $address->getRegion() ? $address->getRegionId() : null,
                    'postcode' => $address->getPostcode(),
                ]
            );
        }
        if ($this->isDefaultShipping($address)) {
            $this->customerSession->setDefaultTaxShippingAddress(
                [
                    'country_id' => $address->getCountryId(),
                    'region_id' => $address->getRegion() ? $address->getRegionId() : null,
                    'postcode' => $address->getPostcode(),
                ]
            );
        }
    }

    /**
     * Set default Tax Shipping and Billing addresses into customer session after login.
     *
     * @param \Magento\Customer\Api\Data\AddressInterface[] $addresses
     * @return void
     */
    public function setDefaultAddressAfterLogIn(array $addresses)
    {
        $defaultShippingFound = false;
        $defaultBillingFound = false;
        foreach ($addresses as $address) {
            if ($address->isDefaultBilling()) {
                $defaultBillingFound = true;
                $this->customerSession->setDefaultTaxBillingAddress(
                    [
                        'country_id' => $address->getCountryId(),
                        'region_id' => $address->getRegion() ? $address->getRegionId() : null,
                        'postcode' => $address->getPostcode(),
                    ]
                );
            }
            if ($address->isDefaultShipping()) {
                $defaultShippingFound = true;
                $this->customerSession->setDefaultTaxShippingAddress(
                    [
                        'country_id' => $address->getCountryId(),
                        'region_id' => $address->getRegion() ? $address->getRegionId() : null,
                        'postcode' => $address->getPostcode(),
                    ]
                );
            }
            if ($defaultShippingFound && $defaultBillingFound) {
                break;
            }
        }
    }

    /**
     * Check whether specified billing address is default for customer from address.
     *
     * @param Address $address
     * @return bool
     */
    private function isDefaultBilling(Address $address)
    {
        return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
            || $address->getIsPrimaryBilling()
            || $address->getIsDefaultBilling();
    }

    /**
     * Check whether specified shipping address is default for customer from address.
     *
     * @param Address $address
     * @return bool
     */
    private function isDefaultShipping(Address $address)
    {
        return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
            || $address->getIsPrimaryShipping()
            || $address->getIsDefaultShipping();
    }
}