<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Control_Special {

    public static function updateKKProfileAttributes( TB_Server_Core_RequestData $params )
    {
        $resp = new TB_Server_Core_Response();

        $sessionProfile = TB_Server_Core_Session::get()->getProfile();
        if (!isset($sessionProfile))
        {
            throw new \Exception('No session profile - no stats');
        }

        $team = TB_Shared_Ent_TeamData_Team::get( $params->get( 'groupId' ) );
        if ( !isset( $team ) )
        {
            throw new \Exception( 'No team - no stats' );
        }

        $profileToUpdate = TB_Shared_Ent_TeamData_Profile::get( $params->get( 'profileId' ) );
        if ( !isset( $profileToUpdate ) )
        {
            throw new \Exception( 'Profile to update not found' );
        }
        if ( false === $profileToUpdate->isInTeam( $team->id ) )
        {
            throw new \Exception( 'Profile to update not in given team.' );
        }

        if ( false === $sessionProfile->isAdminOfTeam( $team->id ) )
        {
            throw new \Exception( 'Not admin of team.' );
        }

        // KK specific validation
        if ( false === $sessionProfile->isAdminOfTeam( 10 ) )
        {
            throw new \Exception( 'No special K&K access.' );
        }

        $new = $params->get( 'attributes' );
        if ( false === is_array( $new ) )
        {
            throw new \Exception( 'Invalid new params' );
        }

        $newAtts = array(
            "hourlyRate" => array_key_exists( 'hourlyRate', $new ) ? _xss( $new[ 'hourlyRate' ] ) : 0,
            "hourMax" => array_key_exists( 'hourMax', $new ) ? _xss( $new[ 'hourMax' ] ) : 0,
            "notice" => array_key_exists( 'notice', $new ) ? _xss( $new[ 'notice' ] ) : 0,
        );

        $profileAtts = TB_Shared_Ent_TeamData_KKSpecialEmployee::getByTeamIdAndProfileId( $team->id, $profileToUpdate->id);
        if ( null === $profileAtts )
        {
            $profileAtts = new TB_Shared_Ent_TeamData_KKSpecialEmployee();
            $profileAtts->team_id = $team->id;
            $profileAtts->profile_id = $profileToUpdate->id;
        }

        $profileAtts->attributes_js = $newAtts;
        $profileAtts->save();

        $resp->addData( 'profile_attributes', $newAtts );

        return $resp;
    }

    public static function getKKProfileAttributes( TB_Server_Core_RequestData $params )
    {
        $resp = new TB_Server_Core_Response();

        $sessionProfile = TB_Server_Core_Session::get()->getProfile();
        if (!isset($sessionProfile))
        {
            throw new \Exception('No session profile - no stats');
        }

        $team = TB_Shared_Ent_TeamData_Team::get( $params->get( 'groupId' ) );
        if ( !isset( $team ) )
        {
            throw new \Exception( 'No team - no stats' );
        }

        if ( false === $sessionProfile->isAdminOfTeam( $team->id ) )
        {
            throw new \Exception( 'Not admin of team.' );
        }

        // KK specific validation
        if ( false === $sessionProfile->isAdminOfTeam( 10 ) )
        {
            throw new \Exception( 'No special K&K access.' );
        }

        $patts = TB_Shared_Ent_TeamData_KKSpecialEmployee::getByTeamId( $team->id );

        $resp->addData( 'special_profile_attributes', $patts );

        return $resp;

    }

    public static function getWorkshifts( TB_Server_Core_RequestData $params )
    {
        $resp = new TB_Server_Core_Response();

        $sessionProfile = TB_Server_Core_Session::get()->getProfile();
        if (!isset($sessionProfile))
        {
            throw new \Exception('No session profile - no stats');
        }

        $team = TB_Shared_Ent_TeamData_Team::get( $params->get( 'groupId' ) );
        if ( !isset( $team ) )
        {
            throw new \Exception( 'No team - no stats' );
        }

        if ( false === $sessionProfile->isAdminOfTeam( $team->id ) )
        {
            throw new \Exception( 'Not admin of team.' );
        }

        // KK specific validation
        if ( false === $sessionProfile->isAdminOfTeam( 10 ) )
        {
            throw new \Exception( 'No special K&K access.' );
        }

        $startDate = $params->get( 'startDate' ) . ' 00:00:00';
        $endDate = $params->get( 'endDate' ) . ' 23:59:59';

        $workshifts = TB_Shared_Ent_TeamData_Appointment::getAppointmentAttendeesForTeamIdAndTimespan( $team->id, $startDate, $endDate );
        $memberProfileIds = array();
        $members = array();
        foreach( $workshifts as $ws )
        {
            if ( in_array( $ws[ 'profile_id' ], $memberProfileIds ) )
            {
                continue;
            }

            array_push( $memberProfileIds, $ws[ 'profile_id' ] );
            $entMember = TB_Shared_Ent_TeamData_Profile::get( $ws[ 'profile_id' ] );
            if ( $entMember )
            {
                array_push( $members, $entMember->asArray() );
            }
        }

        $resp->addData( 'members', $members );
        $resp->addData( 'workshifts', $workshifts );

        return $resp;
    }

    public static function updateWorkshift( TB_Server_Core_RequestData $params )
    {
        $resp = new TB_Server_Core_Response();

        $sessionProfile = TB_Server_Core_Session::get()->getProfile();
        if (!isset($sessionProfile))
        {
            throw new \Exception('No session profile - no stats');
        }

        $team = TB_Shared_Ent_TeamData_Team::get( $params->get( 'groupId' ) );
        if ( !isset( $team ) )
        {
            throw new \Exception( 'No team - no stats' );
        }

        if ( false === $sessionProfile->isAdminOfTeam( $team->id ) )
        {
            throw new \Exception( 'Not admin of team.' );
        }

        // KK specific validation
        if ( false === $sessionProfile->isAdminOfTeam( 10 ) )
        {
           throw new \Exception( 'No special K&K access.' );
        }

        $app = TB_Shared_Ent_TeamData_Appointment::get( $params->get( 'appointmentId' ) );
        if ( !isset( $app ) || $app->team_id != $team->id )
        {
            throw new \Exception( 'Invalid appointment for workshift' );
        }

        $hourlyRate = $params->get( 'hourlyRate', 0 );
        $grossHour = $params->get( 'grossHour', 0 );
        $pauseHour = $params->get( 'pauseHour', 0 );
        $netHour = $params->get( 'netHour', 0 );
        $additionalPay = $params->get( 'additionalPay', 0 );
        $notice = $params->get( 'notice' );

        $entSpecialWorkshift = TB_Shared_Ent_TeamData_KKSpecialProfileWorkshift::getByAppointmentIdAndTeamIdAndProfileId(
            $app->id,
            $team->id,
            $params->get( 'profileId' )
        );

        if ( NULL === $entSpecialWorkshift )
        {
            $entSpecialWorkshift = new TB_Shared_Ent_TeamData_KKSpecialProfileWorkshift();
            $entSpecialWorkshift->appointment_id = $app->id;
            $entSpecialWorkshift->team_id = $team->id;
            $entSpecialWorkshift->profile_id = $params->get( 'profileId' );
        }

        $entSpecialWorkshift->hourly_rate = $hourlyRate;
        $entSpecialWorkshift->gross_hour = $grossHour;
        $entSpecialWorkshift->pause_hour = $pauseHour;
        $entSpecialWorkshift->net_hour = $netHour;
        $entSpecialWorkshift->additional_pay = $additionalPay;
        $entSpecialWorkshift->notice = $notice;

        $entSpecialWorkshift->save();

        $resp->addData( 'workshift', $entSpecialWorkshift->asArray() );

        return $resp;
    }
}