<?php

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class DBWorkWarenkorbArtikel extends DBWorkWarenkorb
{
  private $dbArtikel;

  public function __construct()
  {
    parent::__construct();
    $this->addiereMenge = false;
  }

  public function cleardata()
  {
    if (isset($this->data)) {
      unset($this->data);
    }
    $this->data = new WareArtikel();
  }

  public function initiate()
  {
    $this->sessionvar = 'WKA_VAR';
  }

  public function explodeEntry($exEntry)
  {
    parent::explodeEntry($exEntry);
    if (!isset($this->dbArtikel)) {
      $this->dbArtikel = DBWorkArtikel::getDefaultInstance();
    }
    $this->data->artikel = $this->dbArtikel->getArtikel($this->data->id);
  }

  
  public function getSumme($pMitPauschalVersand = true)
  {
    $entries = $this->getEntries();
    $result = 0.0;
    if ($entries !== null) {
      foreach ($entries as $entry) {
        if ($pMitPauschalVersand || !$entry->getVarValue('is_pauschal_versand')) {
          $result += MathOperations::stringToFloat($entry->getVarValue('summe'));
        }
      }
    }

    return $result;
  }

  
  public function getSummeGewicht($pMitPauschalVersand = true)
  {
    $entries = $this->getEntries();
    $result = 0.0;
    if ($entries !== null) {
      foreach ($entries as $entry) {
        if ($pMitPauschalVersand || !$entry->getVarValue('is_pauschal_versand')) {
          $result += $entry->getVarValue('gewicht_gesamt');
        }
      }
    }

    return $result;
  }

  public function containsPauschalArtikel()
  {
    $entries = $this->getEntries();
    if ($entries !== null) {
      foreach ($entries as $entry) {
        if ($entry->menge > 0 && $entry->getVarValue('is_pauschal_versand')) {
          return true;
        }
      }
    }

    return false;
  }

  public function containsOnlyPauschalArtikel()
  {
    $entries = $this->getEntries();
    if ($entries !== null) {
      foreach ($entries as $entry) {
        if ($entry->menge > 0 && !$entry->getVarValue('is_pauschal_versand')) {
          return false;
        }
      }
    }

    return true;
  }
}