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

class Product extends Model {

	public static function saveProduct($product, $note) {
		$p = Product::insert($product)
		->execute();

		Product::insert(['id_product' => $p, 'note' => $note])
			->table('products_notes')
		->execute();
	}

	public static function updateProduct($idProduct, $product, $note) {
		Product::update($product)
			->where('id', $idProduct)
		->execute();

		if($note) {
			Product::update(['note' => $note])
				->table('products_notes')
				->where('id_product', $idProduct)
			->execute();
		}
	}

	public static function getList($offset, $limit, $filter) {
		$data = Product::select()
			->where($filter['stock'], $filter['stock_simbol'], $filter['stock_value'])
			->where($filter['type_name'], $filter['type_simbol'], $filter['type_value'])
			->where($filter['sec_name'], $filter['sec_simbol'], $filter['sec_value'])
			->orderBy(['name' => 'asc', 'status' => '!= 1'])
			->limit($limit)
			->offset($offset)
		->get();

		return $data;
	}

	// Busca de produtos para relatórios
	public static function getListReports($filter, $provider) {
		$data = Product::select()
			->where($filter['stock'], $filter['stock_simbol'], $filter['stock_value'])
			->where($filter['f_pgroup'], $filter['fs_pgroup'], $filter['fp_group'])
			->where('id_provider', '=', $provider)
		->get();

		return $data;
	}

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

		return $data;		
	}

	// Todos produtos de um grupo
	public static function getAllFromGroup($group) {
		$data = Product::select('id')
			->where('id_group', $group)
		->get();

		return $data;		
	}

	public static function getAllFromProvider($provider) {
		$data = Product::select()
			->where('id_provider', $provider)
			->orderBy(['name' => 'asc', 'status' => '!= 1'])
		->get();

		return $data;		
	}

	public static function allFromProviderWStock($provider) {
		$data = Product::select()
			->where('id_provider', $provider)
			->where('amount', '>' , 0)
			->orderBy(['name' => 'asc', 'status' => '!= 1'])
		->get();

		return $data;		
	}

	public static function getProduct($product) {
		$data = Product::select()
			->where('id', $product)
		->one();

		$note = Product::select('note')
			->table('products_notes')
			->where('id_product', $product)
		->one();

		if($note) {
			$data['note'] = $note['note'];
		}
		return $data;
	}

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

		return $data;
	}

	public static function getName($product) {
		$data = Product::select('name')
			->where('id', $product)
		->one();

		return $data['name'];
	}

	public static function allProductGroup() {
		$groups = Product::select()
			->table('products_group')
		->get();

		return $groups;
	}

	public static function allProductUnit() {
		$units = Product::select()
			->table('products_units')
		->get();

		return $units;
	}

	public static function allProductPresentation() {
		$presentations = Product::select()
			->table('products_presentation')
		->get();

		return $presentations;
	}

	public static function getProductGroup($group) {
		$group = Product::select('id, name')
			->table('products_group')
			->where('id', $group)
		->one();

		return $group;
	}

	public static function getProductUnit($unity) {
		$units = Product::select('id, name')
			->table('products_units')
			->where('id', $unity)
		->one();

		return $units;
	}

	public static function getProductPresentation($presentation) {
		$presentation = Product::select('id, name')
			->table('products_presentation')
			->where('id', $presentation)
		->one();

		return $presentation;
	}

	public static function getCount($filter) {
		$c = Product::select()
			->where($filter['stock'], $filter['stock_simbol'], $filter['stock_value'])
			->where($filter['type_name'], $filter['type_simbol'], $filter['type_value'])
			->where($filter['sec_name'], $filter['sec_simbol'], $filter['sec_value'])
		->count();

		return $c;
	}
}