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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 
<?php
/* --------------------------------------------------------------
   CartDropdownController.inc.php 2017-09-19
   http://www.gambio.de
   Copyright (c) 2017 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class CartDropdownController
 *
 * @extends    HttpViewController
 * @category   System
 * @package    HttpViewControllers
 */
class CartDropdownController extends HttpViewController
{
    /** @var shoppingCart_ORIGIN */
    protected $shoppingCart;
    
    /** @var xtcPrice_ORIGIN */
    protected $xtcPrice;
    
    /** @var int */
    protected $customersStatusOtDiscountFlag;
    
    /** @var float */
    protected $customersStatusOtDiscount;
    
    /** @var int */
    protected $customersStatusShowPriceTax;
    
    /** @var int */
    protected $customersStatusAddTaxOt;
    
    /** @var int */
    protected $customersStatusShowPrice;
    
    
    /**
     * @param HttpContextReaderInterface     $httpContextReader
     * @param HttpResponseProcessorInterface $httpResponseProcessor
     * @param ContentViewInterface           $defaultContentView
     */
    public function __construct(HttpContextReaderInterface $httpContextReader,
                                HttpResponseProcessorInterface $httpResponseProcessor,
                                ContentViewInterface $defaultContentView)
    {
        parent::__construct($httpContextReader, $httpResponseProcessor, $defaultContentView);
        
        $this->shoppingCart = $_SESSION['cart'];
        
        $this->xtcPrice = $GLOBALS['xtPrice'];
        
        $this->customersStatusOtDiscountFlag = (int)(boolean)$_SESSION['customers_status']['customers_status_ot_discount_flag'];
        $this->customersStatusOtDiscount     = (float)$_SESSION['customers_status']['customers_status_ot_discount'];
        $this->customersStatusShowPriceTax   = (int)(boolean)$_SESSION['customers_status']['customers_status_show_price_tax'];
        $this->customersStatusAddTaxOt       = (int)(boolean)$_SESSION['customers_status']['customers_status_add_tax_ot'];
        $this->customersStatusShowPrice      = (int)(boolean)$_SESSION['customers_status']['customers_status_show_price'];
    }
    
    
    /**
     * @todo get rid of old AjaxHandler
     * @todo use GET and POST REST-API like
     *
     * @return HttpControllerResponse
     */
    public function actionDefault()
    {
        $cartSum      = trim($this->xtcPrice->xtcFormat($this->_getCartTotal(), true));
        $productCount = gm_get_conf('SHOW_PRODUCTS_COUNT') === 'true' ? count($this->shoppingCart->contents) :
            $this->shoppingCart->count_products();
        $cartDropdown = $this->_getCartDropdown();
        
        $result = $this->_getResponseArray(new StringType($cartSum), new DecimalType($productCount),
                                           new StringType($cartDropdown));
        
        return MainFactory::create('JsonHttpControllerResponse', $result);
    }
    
    
    /**
     * @return float
     */
    protected function _getCartTotal()
    {
        $total    = $this->shoppingCart->show_total();
        $discount = 0.0;
        
        if($this->customersStatusOtDiscountFlag === 1 && $this->customersStatusOtDiscount !== 0)
        {
            if($this->customersStatusShowPriceTax === 0 && $this->customersStatusAddTaxOt === 1)
            {
                $price = $total - $this->shoppingCart->show_tax(false);
            }
            else
            {
                $price = $total;
            }
            $discount = $this->xtcPrice->xtcGetDC($price, $this->customersStatusOtDiscount);
        }
        
        if($this->customersStatusShowPrice === 1)
        {
            if($this->customersStatusShowPriceTax === 0 && $this->customersStatusAddTaxOt === 0)
            {
                $total -= $discount;
            }
            if($this->customersStatusShowPriceTax === 0 && $this->customersStatusAddTaxOt === 1)
            {
                $total = $total - $this->shoppingCart->show_tax(false) - $discount;
            }
            if($this->customersStatusShowPriceTax === 1)
            {
                $total -= $discount;
            }
        }
        
        return (float)$total;
    }
    
    
    /**
     * @return mixed|string
     */
    protected function _getCartDropdown()
    {
        $cartDropdownContentView = MainFactory::create_object('ShoppingCartDropdownBoxContentView');
        $cartDropdownContentView->set_('coo_cart', $this->shoppingCart);
        $cartDropdownContentView->set_('language_id', $_SESSION['languages_id']);
        $cartDropdownContentView->set_('language_code', $_SESSION['language_code']);
        $cartDropdownContentView->set_('customers_status_ot_discount_flag', $this->customersStatusOtDiscountFlag);
        $cartDropdownContentView->set_('customers_status_ot_discount', $this->customersStatusOtDiscount);
        $cartDropdownContentView->set_('customers_status_show_price_tax', $this->customersStatusShowPriceTax);
        $cartDropdownContentView->set_('customers_status_add_tax_ot', $this->customersStatusAddTaxOt);
        $cartDropdownContentView->set_('customers_status_show_price', $this->customersStatusShowPrice);
        $cartDropdownContentView->set_('customers_status_payment_unallowed',
                                       $_SESSION['customers_status']['customers_status_payment_unallowed']);
        
        return $cartDropdownContentView->get_html();
    }
    
    
    protected function _getResponseArray(StringType $cartSum, DecimalType $productsCount, StringType $cartDropdown)
    {
        $result = array(
            'success' => true,
            'content' => array(
                'price'    => array(
                    'selector' => 'cartDropdownProducts',
                    'type'     => 'text',
                    'value'    => $cartSum->asString()
                ),
                'count'    => array(
                    'selector' => 'cartDropdownProductsCount',
                    'type'     => 'text',
                    'value'    => $productsCount->asDecimal()
                ),
                'dropdown' => array(
                    'selector' => 'cartDropdown',
                    'type'     => 'replace',
                    'value'    => $cartDropdown->asString()
                ),
            )
        );
        
        return $result;
    }
}