<commit_before><?php
/**
 * Created by PhpStorm.
 * User: scuib
 * Date: 04.11.2017
 * Time: 15:40
 */

namespace dao;

use database\Database;
use domain\Kosten;
use dao\MieterDAO;

class KostenDAO extends BasicDAO{


    /**
     * @access public
     * @param Kosten
     * @ParamType kosten Kosten
     * @ReturnType Kosten
     */
    public function create(Kosten $kosten) {
        $stmt = $this->pdoInstance->prepare('
            INSERT INTO kosten (betrag, rechnungen_fk, mieter_fk)
            VALUES (:betrag , :rechnungen_fk, :mieter_fk)');
        //  $stmt->bindValue(':id', $mieter->getId());
        $stmt->bindValue(':betrag', $kosten->getBetrag());
        $stmt->bindValue(':rechnungen_fk', $kosten->getRechnungen_fk());
        //$stmt->bindValue(':mieter_fk', 1);
        $stmt->bindValue(':mieter_fk', $kosten->getMieter_fk());

        $stmt->execute();
        //return $this->read($this->pdoInstance->lastInsertId());
    }

    /**
     * @access public
     * @param int mieterId
     * @return Mieter
     * @ParamType mieterId int
     * @ReturnType Mieter
     */
    public function read($mieterId) {
        $stmt = $this->pdoInstance->prepare('
            SELECT * FROM mietertabelle WHERE id = :id;');
        $stmt->bindValue(':id', $mieterId);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_CLASS, "domain\Mieter")[0];
    }
    public function getTotalHeizkosten($mieterId) {

        $totalbetrag = 0;
        $mieterDAO = new MieterDAO();
        $mieter = $mieterDAO->readAll();

        $stmt = $this->pdoInstance->prepare('
            SELECT sum(kosten.betrag) FROM kosten JOIN rechnungen ON kosten.rechnungen_fk = rechnungen.id WHERE kosten.mieter_fk = :id AND rechnungen.typ = Heizkosten LIMIT 1;');
        $stmt->bindValue(':id', $mieterId);
        $stmt->execute();

        return $stmt->fetch()[0];
    }

    /**
     * @access public
     * @param Mieter
     * @return Mieter
     * @ParamType  Mieter
     * @ReturnType Mieter
     */
    public function update(Mieter $mieter) {
        $stmt = $this->pdoInstance->prepare('
             UPDATE mietertabelle SET 
                id = :id,
                vorname = :vorname,
                nachname = :nachname,
                adresse = :adresse,
                mietzins = :mietzins
            WHERE id = :id');
        $stmt->bindValue(':id', $mieter->getId());
        $stmt->bindValue(':vorname', $mieter->getVorname());
        $stmt->bindValue(':nachname', $mieter->getNachname());
        $stmt->bindValue(':adresse', $mieter->getAdresse());<commit_msg>div<commit_after><?php
/**
 * Created by PhpStorm.
 * User: scuib
 * Date: 04.11.2017
 * Time: 15:40
 */

namespace dao;

use database\Database;
use domain\Kosten;
use dao\MieterDAO;

class KostenDAO extends BasicDAO{


    /**
     * @access public
     * @param Kosten
     * @ParamType kosten Kosten
     * @ReturnType Kosten
     */
    public function create(Kosten $kosten) {
        $stmt = $this->pdoInstance->prepare('
            INSERT INTO kosten (betrag, rechnungen_fk, mieter_fk)
            VALUES (:betrag , :rechnungen_fk, :mieter_fk)');
        //  $stmt->bindValue(':id', $mieter->getId());
        $stmt->bindValue(':betrag', $kosten->getBetrag());
        $stmt->bindValue(':rechnungen_fk', $kosten->getRechnungen_fk());
        //$stmt->bindValue(':mieter_fk', 1);
        $stmt->bindValue(':mieter_fk', $kosten->getMieter_fk());

        $stmt->execute();
        //return $this->read($this->pdoInstance->lastInsertId());
    }

    /**
     * @access public
     * @param int mieterId
     * @return Mieter
     * @ParamType mieterId int
     * @ReturnType Mieter
     */
    public function read($mieterId) {
        $stmt = $this->pdoInstance->prepare('
            SELECT * FROM mietertabelle WHERE id = :id;');
        $stmt->bindValue(':id', $mieterId);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_CLASS, "domain\Mieter")[0];
    }
    public function getTotalHeizkosten($mieterId) {

        $totalbetrag = 0;
        $mieterDAO = new MieterDAO();
        $mieter = $mieterDAO->readAll();

        $stmt = $this->pdoInstance->prepare('
            SELECT sum(betrag) FROM kosten WHERE mieter_fk = :id LIMIT 1;');
        $stmt->bindValue(':id', $mieterId);
        $stmt->execute();

        return $stmt->fetch()[0];
    }

    /**
     * @access public
     * @param Mieter
     * @return Mieter
     * @ParamType  Mieter
     * @ReturnType Mieter
     */
    public function update(Mieter $mieter) {
        $stmt = $this->pdoInstance->prepare('
             UPDATE mietertabelle SET 
                id = :id,
                vorname = :vorname,
                nachname = :nachname,
                adresse = :adresse,
                mietzins = :mietzins
            WHERE id = :id');
        $stmt->bindValue(':id', $mieter->getId());
        $stmt->bindValue(':vorname', $mieter->getVorname());
        $stmt->bindValue(':nachname', $mieter->getNachname());
        $stmt->bindValue(':adresse', $mieter->getAdresse());
{'fraction_non_alphanumeric': 0.16022861647615294, 'fraction_numerical': 0.006700827749310209, 'mean_word_length': 2.338815789473684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576814', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1691, 'n_words': 317}