<?php

/**
 *  Informatica Eindproject D4p
 *  6in3, Stedelijk Gymnasium Nijmegen
 *  Docent: Hans de Wolf
 *
 *  ==================
 *
 *  Daniel Boutros,
 *  Christiaan Goossens,
 *  Jelmer Hinssen
 */

namespace Inforbank\Application\Helper;

use Inforbank\Application;

class Rekeningen
{
    public static $BETAALREKENING = 1;
    public static $SPAARREKENING = 2;

    /**
     * Return the current user bank accounts
     * @return array
     */
    public static function getCurrentUserRekeningen()
    {
        $container = Application::getContainer();
        $user = $container->auth->getUserID();

        $db = $container->db;

        $rekeningen = $db->rekeningen->where('klantid', $user);

        $returnArray = array();

        foreach ($rekeningen as $rekening) {
            $type = $db->types->where('id', $rekening['typeid'])[$rekening['typeid']];

            $returnArray[] = array(
                "iban" => IBAN::getIBAN($rekening['rekeningnr']),
                "nr" => $rekening['rekeningnr'],
                "saldo" => (double) $rekening['saldo'],
                "naam" => $type['rekeningnaam'],
                "rente" => $type['rente']
            );
        }

        return $returnArray;
    }

    /**
     * Check if the current user is the owner of this bank account
     * @param  string  $iban
     * @return boolean
     */
    public static function isEigenRekeningIBAN($iban)
    {
        $rekeningen = Rekeningen::getCurrentUserRekeningen();
        $vanRekening = false;
        //Controleer of de rekening wel van de ingelogde klant is
        foreach ($rekeningen as $rek) {
            if ($rek['iban'] === $iban) {
                $vanRekening = $rek;
                break;
            }
        }
        return $vanRekening;
    }

    /**
     * Check if the current user is the owner of this bank account
     * @param  string  $nr Internal bank account number
     * @return boolean
     */
    public static function isEigenRekeningnr($nr)
    {
        $rekeningen = Rekeningen::getCurrentUserRekeningen();
        $vanRekening = false;

        //Controleer of de rekening wel van de ingelogde klant is
        foreach ($rekeningen as $rek) {
            if ((int) $rek['nr'] === (int) $nr) {
                $vanRekening = $rek;
                break;
            }
        }
        return $vanRekening;
    }
}
