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

class Purchase extends Model {

	public static function purchaseSave($purchase) {
		$pId = Purchase::insert($purchase)
		->execute();

		return $pId;
	}

	public static function updatePurchase($ref, $data) {
		Purchase::update($data)
			->where('id', $ref)
		->execute();
	}

	public static function insertItems($itemPurchase) {
		Purchase::insert($itemPurchase)
			->table('purchases_items')
		->execute();
	}

	public static function insertPayment($purchasePayments) {
		$id = Purchase::insert($purchasePayments)
			->table('purchases_payments')
		->execute();

		return $id;
	}

	public static function getPurchase($purchase) {
		$data = Purchase::select()
			->where('id', $purchase)
		->one();

		return $data;
	}	

	public static function getPurchases($offset, $limit, $filter) {
		$data = Purchase::select()
			->where($filter['f_type'], $filter['f_simbol'], $filter['f_value'])
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', '!=', '3')
			->offset($offset)
			->limit($limit)
			->orderBy(['status' => '!= 3', 'note_date' => 'desc'])
		->get();

		return $data;
	}

	// Busca para relatórios
	public static function getPurchasesReports($offset, $limit, $filter) {
		$data = Purchase::select()
			->where($filter['f_group'], $filter['fs_group'], $filter['fg_value'])
			->where($filter['f_provider'], $filter['fs_provider'], $filter['fp_value'])
			->where($filter['fp_payment'], $filter['fs_payment'], $filter['fv_payment'])
			->where($filter['f_date_init'], $filter['f_date_init_simbol'], $filter['f_date_init_value'])
			->where($filter['f_date_end'], $filter['f_date_end_simbol'], $filter['f_date_end_value'])
			->where('status', '!=', '3')
			->offset($offset)
			->limit($limit)
			->orderBy(['note_date' => 'desc'])
		->get();

		return $data;
	}

	// Retorna todos os produto do pedido
	public static function getProductsPurchase($purchase) {
		$data = Purchase::select()
			->table('purchases_items')
			->where('id_purchase', $purchase)
		->get();

		return $data;
	}

	public static function getPurchasePayments($purchase) {
		$data = Purchase::select()
			->table('purchases_payments')
			->where('id_purchase', $purchase)
		->get();

		return $data;
	}

	// Retorna um único produto do pedido, inicialmente usado na página de compras
	public static function getProductPurchase($purchase) {
		$data = Purchase::select('id_product')
			->table('purchases_items')
			->where('id_purchase', $purchase)
		->first();

		if($data) {
			$product = Product::getProduct($data['id_product']);
			return $product['name'];
		}
	}

	public static function getCount($filter) {
		$c = Purchase::select()
			->where($filter['f_type'], $filter['f_simbol'], $filter['f_value'])
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', '!=', '3')
		->count();

		return $c;
	}

	public static function countPayments($purchase) {
		$c = Purchase::select()
			->table('purchases_payments')
			->where('id_purchase', $purchase)
		->count();

		return $c;
	}

	public static function getPurchasesWithProducts($product, $filter) {
		$data = Purchase::select('id_purchase')
			->table('purchases_items')
			->where('id_product', $product)
			->where('id_product_group', $filter['simbol'], $filter['value'])
		->get();

		return $data;
	}

	// Buscar items de compras por grupo de produto
	public static function getPurchasesWithProductsGroups($group) {
		$data = Purchase::select('id_purchase')
			->table('purchases_items')
			->where('id_product_group', $group)
		->get();

		return $data;
	}

	public static function existsNote($note) {
		$response = false;

		$c = Purchase::select()
			->where('note_number', $note)
		->count();

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

		return $response;
	}

	public static function getAdavanceLastPurshase($id_product) {
		$response = '';

		$data = Purchase::select('id_purchase')
			->table('purchases_items')
			->where('id_product', $id_product)
		->last('id');

		if($data) {
			$response = Purchase::select('advance')
				->where('status', '!=', 3)
				->andWhere('id', $data['id_purchase'])
			->last('id');
		}

		return $response;
	}

}