<?php
namespace src\models;
use \core\Model;
use \core\Database;

class Salesman extends Model {

	public static function saveSalesman($salesman, $note) {
		$ref = Salesman::insert($salesman)
		->execute();

		Salesman::insert(['id_salesman' => $ref, 'note' => $note])
			->table('salesman_notes')
		->execute();
	}

	public static function updateSalesman($salesmanId, $salesman, $note) {
		Salesman::update($salesman)
			->where('id', $salesmanId)
		->execute();

		Salesman::update(['note' => $note])
			->table('salesman_notes')
			->where('id_salesman', $salesmanId)
		->execute();
	}

	public static function getAllActive() {
		$data = Salesman::select()
			->where('status', '!=', 0)
			->orderBy(['status' => '!= 1', 'name' => 'asc'])
		->get();

		return $data;
	}

	public static function getSalesmans($offset, $limit, $filter) {
		$data = Salesman::select()
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', $filter['fs_status'], $filter['f_status'])
			->orderBy(['status' => '!= 1', 'name' => 'asc'])
			->offset($offset)
			->limit($limit)
		->get();

		return $data;
	}

	public static function getSalesmanByInteration($email) {
		$data = Salesman::select()
			->where('email', '=', $email)
		->one();

		return $data;
	}	

	// Busca de Vendedores via texto por ajax
	public static function searchByText($text) {
		$data = Salesman::select()
			->where('name', 'like', '%'.$text.'%')
			->where('status', '!=', 0)
			->orderBy(['name' => 'asc'])
			->limit(20)
		->get();

		return $data;
	}

	public static function getSalesmanName($salesman) {
		if($salesman) {
			$data = Salesman::select('name')
				->where('id', $salesman)
			->one();

			return $data['name'];
		}
	}

	public static function getSalesman($salesman) {
		$data = Salesman::select()
			->where('id', $salesman)
		->one();

		$note = Salesman::select('note')
			->table('salesman_notes')
			->where('id_salesman', $salesman)
		->one();

		$data['note'] = $note['note'];
		return $data;
	}

	public static function getCount($filter) {
		$c = Salesman::select()
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', $filter['fs_status'], $filter['f_status'])
		->count();

		return $c;
	}

	public static function existsSalesman($ref, $value) {
		$response = false;
		$c = Salesman::select('id')
            ->where($ref, $value)
        ->count();

        if($c > 0) {
            $response = true;
        }
        return $response;
	}

	public static function getBirthdays($dateInit, $dateEnd) {
		$data = array();		
		$db = Database::getInstance();

		$sql = $db->prepare("
			SELECT * FROM salesmans
			WHERE DATE_FORMAT(birth_date, '%m-%d') BETWEEN :d_ini AND :d_end			
			ORDER BY DATE_FORMAT(birth_date, '%m-%d') ASC
			");
		$sql->bindValue(':d_ini', date('m-d', strtotime($dateInit)));
		$sql->bindValue(':d_end', date('m-d', strtotime($dateEnd)));		
		$sql->execute();

		if($sql->rowCount() > 0) {
			$data = $sql->fetchAll(\PDO::FETCH_ASSOC);
		}

		return $data;
	}

	// Verificar se existe comissão salva para vendedor e fornecedor
	public static function verifyComission($provider, $salesman) {
		$data = Salesman::select()
			->table('salesman_percentage_comission')
			->where('id_provider', $provider)
			->where('id_salesman', $salesman)
		->one();

		return $data;
	}

	public static function getComissionRegistered($salesman) {
		$data = Salesman::select()
			->table('salesman_percentage_comission')
			->where('id_salesman', $salesman)
		->get();

		return $data;
	}

	public static function saveComission($data) {
		Salesman::insert($data)
			->table('salesman_percentage_comission')
		->execute();
	}

	public static function updateComission($data, $ref) {
		Salesman::update($data)
			->table('salesman_percentage_comission')
			->where('id', $ref)
		->execute();
	}
}