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

class Request extends Model {

	public static function requestSave($requestInfo) {
		$ref = Request::insert($requestInfo)
		->execute();

		return $ref;
	}

	public static function requestUpdate($ref, $requestInfo) {
		Request::update($requestInfo)
			->where('id', $ref)
		->execute();
	}

	public static function insertItems($requestItems) {
		Request::insert($requestItems)
			->table('request_items')
		->execute();
	}

	public static function updateItems($ref, $itemInfo) {
		Request::update($itemInfo)
			->table('request_items')
			->where('id', $ref)
		->execute();
	}

	public static function saveDetailPayment($receiverData) {
		Request::insert($receiverData)
			->table('request_payment_receiver')
		->execute();
	}

	public static function saveOthersExpenses($othersInfo) {
		Request::insert($othersInfo)
			->table('other_expenses')
		->execute();
	}

	public static function saveComissionSalesman($salesmanComission) {
		Request::insert($salesmanComission)
			->table('salesman_comission')
		->execute();
	}

	public static function saveEstimativeResult($estimateResult) {
		Request::insert($estimateResult)
			->table('estimative_request')
		->execute();
	}

	public static function deleteOtherInformations($ref, $requestId) {
		Request::delete()
			->table($ref)
			->where('id_request', $requestId)
		->execute();
	}

	public static function getRequests($offset, $limit, $filter) {
		$data = Request::select()
			->where($filter['f_status'], $filter['fs_simbol'], $filter['fs_value'])
			->where($filter['f_text'], $filter['fs_text_simbol'], $filter['fs_text_value'])
			->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'])			
			->offset($offset)
			->limit($limit)
			->orderBy(['date_created' => 'desc'])
		->get();

		return $data;
	}

	public static function getRequestsById($id, $limit) {
		$data = Request::select()
			->where('id', 'LIKE', '%'. $id .'%')
			->limit($limit)
			->orderBy(['id' => 'asc'])
		->get();

		return $data;
	}

	public static function getRequest($request) {
		$data = Request::select()
			->where('id', $request)
		->one();

		return $data;
	}

	public static function getRequestItems($request) {
		$data = Request::select()
			->table('request_items')
			->where('id_request', $request)
		->get();

		return $data;
	}

	public static function getRequestItem($request, $idProduct) {
		$data = Request::select()
			->table('request_items')
			->where('id_request', $request)
			->andWhere('id_product', $idProduct)
		->one();

		return $data;
	}

	public static function getAllStatus() {
		$data = Request::select()
			->table('request_status')
		->get();

		return $data;
	}

	public static function getNameStatus($status) {
		if($status) {
			$data = Request::select('name')
				->table('request_status')
				->where('id', $status)
			->one();

			return $data['name'];
		}
	}

	public static function getAmountRequest($request) {
		$data = Request::select()
			->table('request_items')
			->where('id_request', $request)
		->sum('quantity');

		return $data;
	}

	public static function getAmountDelivery($request, $idProduct) {
		$data = Request::select('quantity, total')
			->table('request_items')
			->where('id_request', $request)
			->where('id_product', $idProduct)
		->one();
		
		return $data;
	}

	public static function getCount($filter) {
		$c = Request::select()
			->where($filter['f_status'], $filter['fs_simbol'], $filter['fs_value'])
			->where($filter['f_text'], $filter['fs_text_simbol'], $filter['fs_text_value'])
			->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'])
		->count();

		return $c;
	}

	public static function getOtherInformations($table, $requestId) {
		$data = Request::select()
			->table($table)
			->where('id_request', $requestId)
		->one();

		return $data;
	}

	public static function getSalesThisYear($date_init, $months) {
        $this_year = array();

        for($i=0;$i<$months;$i++) {
			$first = date('Y-m-d', strtotime('+ '.$i.' Month', strtotime($date_init)));
			$last = date('Y-m-t', strtotime($first));

			$this_year[] = Request::select()
				->where('date_created', '>=', $first)
				->where('date_created', '<=', $last)
			->sum('total');
        }

        return $this_year;
    }

    // Relatórios
    public static function getFromRequest($filter) {
		$data = Request::select('id_request, id_product')
			->table('request_items')
			->where($filter['d_productsgroup'], $filter['d_productsgroup_simbol'], $filter['d_productsgroup_value'])
			->where($filter['d_products'], $filter['d_products_simbol'], $filter['d_products_value'])
			->groupBy('id_request')
		->get();

		return $data;
    }

    public static function getRequestsReports($filter, $request) {
		$array = Request::select()
			->where('id', '=', $request)
			->where($filter['d_init'], $filter['d_init_simbol'], $filter['d_init_value'])
			->where($filter['d_end'], $filter['d_end_simbol'], $filter['d_end_value'])
			->where($filter['d_situation'], $filter['d_situation_simbol'], $filter['d_situation_value'])
			->where($filter['d_client'], $filter['d_client_simbol'], $filter['d_client_value'])
			->where($filter['d_provider'], $filter['d_provider_simbol'], $filter['d_provider_value'])
			->where($filter['d_salesman'], $filter['d_salesman_simbol'], $filter['d_salesman_value'])
		->one();

		return $array;
	}
}