<?php

$loader = $this->registry->get('load');
$loader->controller('checkout/cart');

class ControllerSpecific4861CheckoutCart extends ControllerCheckoutCart
{

  public function index()
  {

    parent::index();

    if ($this->cart->hasProducts() || !empty($this->session->data['vouchers'])) {

      $this->data['isLogged'] = $this->customer->isLogged();

      if ($this->config->get('descontofaixavalorpedido_status')) {
        $this->load->model('setting/extension');
        $result = $this->model_setting_extension->getExtensionsByName('total', 'descontofaixavalorpedido');

        $total_data = array();
        $desconto = 0;

        if ($this->config->get($result['code'] . '_status')) {
          $this->load->model('total/' . $result['code']);
          $this->{'model_total_' . $result['code']}->getTotal($total_data, $desconto);
        }

        if ($desconto != 0) {

          $desconto *= -1;
          $cartlists = $this->data['cartlist'];
          $subtotal = $this->cart->getSubtotal();

          foreach ($cartlists as $keyc => $cartlist) {
            foreach ($cartlist as $keyi => $item) {
              foreach ($item['products'] as $keyp => $product) {
                $total_item = ($product['price_value'] * $product['quantity']);
                $percentual = round(($total_item * 100) / $subtotal);
                $lucro = (($percentual / 100) * $desconto);
                $cartlists[$keyc][$keyi]['products'][$keyp]['lucro'] =  $this->currency->format($lucro);
              }
            }
          }

          $this->data['cartlist'] = $cartlists;
        }
      }

      $this->children = parent::getChildren('module/starter_kit');
      if (isset($this->request->get['ajax']) && $this->request->get['ajax']) {
        $this->response->setOutputJson($this->data);
      } else {
        $this->response->setOutput($this->render('checkout/cart'));
      }
    } else {
      parent::getEmptyCart();
    }
  }
}
