<?php
namespace src\handlers;

use \src\models\Purchase;
use \src\controllers\UtilsController;
use \src\models\Request;
use \src\models\Deliverie;
use \src\models\Product;
use \src\models\Interaction;

class FiltersHandler {

	public static function filterCLients($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['fs_status'] = '!=';
		$query['f_status'] = '';

		// Busca por digitação
		$query['f_others'] = 'name';
		$query['fs_others'] = '!=';
		$query['fo_value'] = '';

		if(!empty($filter['searchType'])) {
			$query['fs_status'] = '=';
			$query['f_status'] = ($filter['searchType'] == 1 ? 1 : 0);
		}

		if(!empty($filter['tag'])) {
			$query['f_others'] = $filter['opt'];
			$query['fs_others'] = 'LIKE';
			$query['fo_value'] = '%' . $filter['tag'] . '%';
		}

        // Inclusão de filtros de relatórios
        // Busca por Estado de Localização
        $query['f_state'] = 'state';
        $query['fs_state'] = '!=';
        $query['fv_state'] = '';

        if(!empty($filter['state'])) {
            $query['fs_state'] = 'LIKE';
            $query['fv_state'] = '%' . $filter['state'] . '%';
        }

        // Busca por PF/PJ
        $query['f_type'] = 'type';
        $query['fs_type'] = '!=';
        $query['fv_type'] = '';

        if(!empty($filter['type'])) {
            $query['fs_type'] = '=';
            $query['fv_type'] = $filter['type'];
        }
        // Fim Inclusão de filtros de relatórios

		return $query;
	}

	public static function filterFarms($filter) {		
		$query['fs_status'] = '!=';
		$query['f_status'] = '';

		if(!empty($filter['searchType'])) {
			$query['fs_status'] = '=';
			$query['f_status'] = ($filter['searchType'] == 1 ? 1 : 0);
		}

        // Busca por Estado de Localização
        $query['f_state'] = 'state';
        $query['fs_state'] = '!=';
        $query['fv_state'] = '';

        if(!empty($filter['state'])) {
            $query['fs_state'] = 'LIKE';
            $query['fv_state'] = '%' . $filter['state'] . '%';
        }

        // Busca por PF/PJ
        $query['f_client'] = 'id_client';
        $query['fs_client'] = '!=';
        $query['fv_client'] = '';

        if(!empty($filter['client'])) {
            $query['fs_client'] = '=';
            $query['fv_client'] = $filter['client'];
        }

		return $query;
	}

	public static function filterProducts($filter) {
        $arrayFilter = array();

        // Filtro de estoque
        $arrayFilter['stock'] = 'amount';
        $arrayFilter['stock_simbol'] = '>';
        $arrayFilter['stock_value'] = '-1';

        if(isset($filter['onlystock']) && $filter['onlystock'] == 'on') {
	        $arrayFilter['stock'] = 'amount';
	        $arrayFilter['stock_simbol'] = '>';
	        $arrayFilter['stock_value'] = '0';
        }

        // Outros Filtros
        if(isset($filter['searchType'])) {
        	switch ($filter['searchType']) {
        		case 1:
    				$arrayFilter['type_name'] = 'status';
    				$arrayFilter['type_simbol'] = '=';
    				$arrayFilter['type_value'] = '1';
    				break;
    			case 2:
    				$arrayFilter['type_name'] = 'status';
    				$arrayFilter['type_simbol'] = '=';
    				$arrayFilter['type_value'] = '0';
    				break;
    			case 3:
    				$arrayFilter['type_name'] = 'name';
    				$arrayFilter['type_simbol'] = 'LIKE';
    				$arrayFilter['type_value'] = '%'. $filter['searchTerm'] .'%';
    				break;
    			case 4:
    				$arrayFilter['type_name'] = 'id_provider';
    				$arrayFilter['type_simbol'] = '=';
    				$arrayFilter['type_value'] = $filter['searchProvider'];
    			break;
        		
        		default:
        			$arrayFilter['type_name'] = 'name';
    				$arrayFilter['type_simbol'] = '!=';
    				$arrayFilter['type_value'] = '';
        			break;
        	}
        }

        // Busca secundária
        $arrayFilter['sec_name'] = 'id';
		$arrayFilter['sec_simbol'] = '!=';
		$arrayFilter['sec_value'] = '';

		if(!empty($filter['tag'])) {
			switch ($filter['opt']) {
				case 'id':
					$arrayFilter['sec_name'] = 'id';
					$arrayFilter['sec_simbol'] = '=';
					$arrayFilter['sec_value'] = $filter['tag'];
					break;
			}
		}

        return $arrayFilter;
	}

	public static function filterSalesman($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['fs_status'] = '!=';
		$query['f_status'] = '';

		// Busca por digitação
		$query['f_others'] = 'name';
		$query['fs_others'] = '!=';
		$query['fo_value'] = '';

		if(!empty($filter['searchType'])) {
			$query['fs_status'] = '=';
			$query['f_status'] = ($filter['searchType'] == 1 ? 1 : 0);
		}

		if(!empty($filter['tag'])) {
			$query['f_others'] = $filter['opt'];
			$query['fs_others'] = 'LIKE';
			$query['fo_value'] = '%' . $filter['tag'] . '%';
		}		

		return $query;
	}

	public static function filterProviders($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['fs_status'] = '!=';
		$query['f_status'] = '';

		// Busca por digitação
		$query['f_others'] = 'company_name';
		$query['fs_others'] = '!=';
		$query['fo_value'] = '';

		if(!empty($filter['searchType'])) {
			$query['fs_status'] = '=';
			$query['f_status'] = ($filter['searchType'] == 1 ? 1 : 0);
		}

		if(!empty($filter['tag'])) {
			$query['f_others'] = $filter['opt'];
			$query['fs_others'] = 'LIKE';
			$query['fo_value'] = '%' . $filter['tag'] . '%';
		}

		// Filtros referentes a relatórios

		// Apenas produtos com estoque
		$query['stock'] = 'amount';
        $query['stock_simbol'] = '>';
        $query['stock_value'] = '-1';

		if(isset($filter['onlystock']) && !empty($filter['onlystock'])) {
			$query['stock'] = 'amount';
			$query['stock_simbol'] = '>';
			$query['stock_value'] = 0;
		}

		// Busca por Estado de Localização
        $query['f_pstate'] = 'state';
        $query['fs_pstate'] = '!=';
        $query['fp_state'] = '';

        if(!empty($filter['state'])) {
            $query['fs_pstate'] = 'LIKE';
            $query['fp_state'] = '%' . $filter['state'] . '%';
        }

        // Busca por Grupo de produtos
        $query['f_pgroup'] = 'id_group';
        $query['fs_pgroup'] = '!=';
        $query['fp_group'] = '';

        if(!empty($filter['p_group'])) {
            $query['fs_pgroup'] = '=';
            $query['fp_group'] = $filter['p_group'];
        }

        // Busca por Fornecedor
        $query['f_provider'] = 'id';
        $query['fs_provider'] = '!=';
        $query['fv_provider'] = '';

        if(!empty($filter['provider'])) {
            $query['fs_provider'] = '=';
            $query['fv_provider'] = $filter['provider'];
        }
        
		return $query;
	}

	public static function filterShippingsCompany($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['fs_status'] = '!=';
		$query['f_status'] = '';

		// Busca por digitação
		$query['f_others'] = 'company_name';
		$query['fs_others'] = '!=';
		$query['fo_value'] = '';

		if(!empty($filter['searchType'])) {
			$query['fs_status'] = '=';
			$query['f_status'] = ($filter['searchType'] == 1 ? 1 : 0);
		}

		if(!empty($filter['tag'])) {
			$query['f_others'] = $filter['opt'];
			$query['fs_others'] = 'LIKE';
			$query['fo_value'] = '%' . $filter['tag'] . '%';
		}		

		return $query;
	}

	public static function filterUsers($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['fs_status'] = '!=';
		$query['f_status'] = '';

		// Busca por digitação
		$query['f_others'] = 'name';
		$query['fs_others'] = '!=';
		$query['fo_value'] = '';

		if(!empty($filter['searchType'])) {
			$query['fs_status'] = '=';
			$query['f_status'] = ($filter['searchType'] == 1 ? 1 : 0);
		}

		if(!empty($filter['tag'])) {
			$query['f_others'] = $filter['opt'];
			$query['fs_others'] = 'LIKE';
			$query['fo_value'] = '%' . $filter['tag'] . '%';
		}		

		return $query;
	}

	public static function filterPurchases($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['f_type'] = 'status';
		$query['f_simbol'] = '!=';
		$query['f_value'] = '';

		// Busca por digitação
		$query['f_others'] = 'note_number';
		$query['fs_others'] = '!=';
		$query['fo_value'] = '';

		// Opção de busca por produto ou fornecedor
		//Array do segundo parametro para usaar a mesma função na pesquisa para relatórios
		$purchases = Purchase::getPurchasesWithProducts($filter['searchProduct'], array('simbol' => '!=', 'value' => ''));

		if($filter['searchType'] == 1) {		
			$purchasesIds = array();

			foreach ($purchases as $key => $purchase) {
				array_push($purchasesIds, $purchase['id_purchase']);
			}

			if(!$purchasesIds) {
				array_push($purchasesIds, '0');
			}

			$query['f_type'] = 'id';
			$query['f_simbol'] = 'in';
			$query['f_value'] = $purchasesIds;
		}

		if($filter['searchType'] == 2) {
			$query['f_type'] = 'id_provider';
			$query['f_simbol'] = '=';
			$query['f_value'] = $filter['searchProvider'];
		}		

		if(!empty($filter['tag'])) {
			$query['f_others'] = $filter['opt'];
			$query['fs_others'] = 'LIKE';
			$query['fo_value'] = '%' . $filter['tag'] . '%';
		}

		return $query;
	}


	public static function filterRequests($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['f_date_init'] = 'date_created';
		$query['f_date_init_simbol'] = '!=';
		$query['f_date_init_value'] = '';

		$query['f_date_end'] = 'date_created';
		$query['f_date_end_simbol'] = '!=';
		$query['f_date_end_value'] = '';


		if(!empty($filter['dateInit']) && !empty($filter['dateEnd'])) {
			if(UtilsController::isDate($filter['dateInit'])) {
				if(UtilsController::isDate($filter['dateEnd'])) {
					$dateInit = UtilsController::convertDate($filter['dateInit']);
					$dateEnd = UtilsController::convertDate($filter['dateEnd']);

					$query['f_date_init'] = 'date_created';
					$query['f_date_init_simbol'] = '>=';
					$query['f_date_init_value'] = $dateInit.' 00:00:00';

					$query['f_date_end'] = 'date_created';
					$query['f_date_end_simbol'] = '<=';
					$query['f_date_end_value'] = $dateEnd.' 23:59:59';
				}
			}
		}

		$query['f_status'] = 'status';
		$query['fs_simbol'] = '!=';
		$query['fs_value'] = '6';

		if(!empty($filter['searchType'])) {
			$query['fs_simbol'] = '=';
			$query['fs_value'] = $filter['searchType'];
		}


		$query['f_text'] = 'id_user';
		$query['fs_text_simbol'] = '!=';
		$query['fs_text_value'] = '';

		if(!empty($filter['tag'])) {

			switch ($filter['opt']) {
				case 1:
					$query['f_text'] = 'id_client';
					$query['fs_text_simbol'] = '=';
					$query['fs_text_value'] = $filter['optionList'];
					break;
				case 2:
					$query['f_text'] = 'id_provider';
					$query['fs_text_simbol'] = '=';
					$query['fs_text_value'] = $filter['optionList'];
					break;
				case 3:
					$query['f_text'] = 'id';
					$query['fs_text_simbol'] = '=';
					$query['fs_text_value'] = $filter['tag'];
					break;
				case 4:
					$query['f_text'] = 'id_salesman';
					$query['fs_text_simbol'] = '=';
					$query['fs_text_value'] = $filter['optionList'];
					break;
			}
		}
		// print_r($query);exit;

		return $query;
	}

	public static function filterBillsToPay($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['f_date_init'] = 'id';
		$query['f_date_init_simbol'] = '!=';
		$query['f_date_init_value'] = '';

		$query['f_date_end'] = 'expirate_date';
		$query['f_date_end_simbol'] = '!=';
		$query['f_date_end_value'] = '';


		if(!empty($filter['dateInit']) && !empty($filter['dateEnd'])) {
			if(UtilsController::isDate($filter['dateInit'])) {
				if(UtilsController::isDate($filter['dateEnd'])) {
					$dateInit = UtilsController::convertDate($filter['dateInit']);
					$dateEnd = UtilsController::convertDate($filter['dateEnd']);

					$query['f_date_init'] = 'expirate_date';
					$query['f_date_init_simbol'] = '>=';
					$query['f_date_init_value'] = $dateInit;

					$query['f_date_end'] = 'expirate_date';
					$query['f_date_end_simbol'] = '<=';
					$query['f_date_end_value'] = $dateEnd;
				}
			}
		}

		// Area de busca por status da conta
		$query['f_billsstatus'] = 'status';
		$query['fs_simbol'] = '=';
		$query['fs_value'] = 1;

		if(isset($filter['status']) && !empty($filter['status'])) {
			$query['fs_simbol'] = '=';
			$query['fs_value'] = $filter['status'];
		}
		// Fim Area de busca por status da conta

		// Area de busca por tipo de conta
		$query['f_billstype'] = 'value_to_pay';
		$query['fb_simbol'] = '!=';
		$query['fb_value'] = '';

		if(isset($filter['b_type']) && !empty($filter['b_type'])) {
			$query['f_billstype'] = 'billstopay_type';
			$query['fb_simbol'] = 'in';
			$query['fb_value'] = $filter['b_type'];
		}
		// Fim Area de busca por tipo de conta

		// Area de busca por forma de pagamento
		$query['f_formpay'] = 'main_value';
		$query['f_fpsimbol'] = '!=';
		$query['f_fpvalue'] = '';

		if(isset($filter['p_form']) && !empty($filter['p_form'])) {
			$query['f_formpay'] = 'form_to_pay';
			$query['f_fpsimbol'] = 'in';
			$query['f_fpvalue'] = $filter['p_form'];
		}
		// Fim Area de busca por forma de pagamento

		// Area de busca por situação do pagamento
		$query['f_bsituation'] = 'id_situation';
		$query['f_bsimbol'] = '!=';
		$query['f_bvalue'] = '';

		if(isset($filter['b_situation']) && !empty($filter['b_situation'])) {
			$query['f_bsimbol'] = 'in';
			$query['f_bvalue'] = $filter['b_situation'];
		}
		// Fim Area de busca por situação do pagamento

		// Area de filtro para busca por digitação
		$query['f_text'] = 'id_user';
		$query['f_text_simbol'] = '!=';
		$query['f_text_value'] = '';

		if(!empty($filter['searchBy'])) {

			switch ($filter['searchBy']) {
				case 'provider_description':
					$query['f_text'] = $filter['searchBy'];
					$query['f_text_simbol'] = 'LIKE';
					$query['f_text_value'] = '%'.$filter['searchText'].'%';
				break;
				case 'document_number':
					$query['f_text'] = $filter['searchBy'];
					$query['f_text_simbol'] = 'LIKE';
					$query['f_text_value'] = '%'.$filter['searchText'].'%';
				break;
				case 'id_request':
					$query['f_text'] = $filter['searchBy'];
					$query['f_text_simbol'] = '=';
					$query['f_text_value'] = $filter['searchText'];
				break;
			}
		}
		// Fim Area de filtro para busca por digitação

		return $query;
	}

	public static function filterBillsToReceiver($filter) {
		/*
			* fs -> filter_simbol
			* fo -> filter_others
		*/
		//Caso não tenha pesquisa vamos pegar todos os resultados
		$query['f_date_init'] = 'id';
		$query['f_date_init_simbol'] = '!=';
		$query['f_date_init_value'] = '';

		$query['f_date_end'] = 'expirate_date';
		$query['f_date_end_simbol'] = '!=';
		$query['f_date_end_value'] = '';


		if(!empty($filter['dateInit']) && !empty($filter['dateEnd'])) {
			if(UtilsController::isDate($filter['dateInit'])) {
				if(UtilsController::isDate($filter['dateEnd'])) {
					$dateInit = UtilsController::convertDate($filter['dateInit']);
					$dateEnd = UtilsController::convertDate($filter['dateEnd']);

					$query['f_date_init'] = 'expirate_date';
					$query['f_date_init_simbol'] = '>=';
					$query['f_date_init_value'] = $dateInit;

					$query['f_date_end'] = 'expirate_date';
					$query['f_date_end_simbol'] = '<=';
					$query['f_date_end_value'] = $dateEnd;
				}
			}
		}

		// Area de busca por status da conta
		$query['f_billsstatus'] = 'status';
		$query['fs_simbol'] = '=';
		$query['fs_value'] = 1;

		if(isset($filter['status']) && !empty($filter['status'])) {
			$query['fs_simbol'] = '=';
			$query['fs_value'] = $filter['status'];
		}
		// Fim Area de busca por status da conta

		// Area de busca por situação do pagamento
		$query['f_bsituation'] = 'id_situation';
		$query['f_bsimbol'] = '!=';
		$query['f_bvalue'] = '';

		if(isset($filter['b_situation']) && !empty($filter['b_situation'])) {
			$query['f_bsimbol'] = 'in';
			$query['f_bvalue'] = $filter['b_situation'];
		}
		// Fim Area de busca por situação do pagamento

		// Area de busca por origem do recebimento conta
		$query['f_billsorigin'] = 'main_value';
		$query['fo_simbol'] = '!=';
		$query['fo_value'] = '';

		if(isset($filter['receiver_origin']) && !empty($filter['receiver_origin'])) {
			$query['f_billsorigin'] = 'receiver_origin';
			$query['fo_simbol'] = '=';
			$query['fo_value'] = $filter['receiver_origin'];
		}
		// Fim Area de busca por origem do recebimento conta
		
		// Area de filtro para busca por digitação
		$query['f_text'] = 'id_user';
		$query['f_text_simbol'] = '!=';
		$query['f_text_value'] = '';

		if(!empty($filter['searchBy'])) {

			switch ($filter['searchBy']) {
				case 'provider_description':
					$query['f_text'] = $filter['searchBy'];
					$query['f_text_simbol'] = 'LIKE';
					$query['f_text_value'] = '%'.$filter['searchText'].'%';
				break;
				case 'document_number':
					$query['f_text'] = $filter['searchBy'];
					$query['f_text_simbol'] = 'LIKE';
					$query['f_text_value'] = '%'.$filter['searchText'].'%';
				break;
				case 'id_request':
					$query['f_text'] = $filter['searchBy'];
					$query['f_text_simbol'] = '=';
					$query['f_text_value'] = $filter['searchText'];
				break;
			}
		}
		// Fim Area de filtro para busca por digitação

		return $query;
	}

	// Filtro de relatórios de compras
	public static function filterPurchasesReports($filter) {
		// Grupo de Produtos como filtro único
		$query['f_group'] = 'id';
		$query['fs_group'] = '!=';
		$query['fg_value'] = '';

		if(isset($filter['p_group']) && !empty($filter['p_group'])) {
			$purchaseGroupList = array();
			$itemsOfGroup = Purchase::getPurchasesWithProductsGroups($filter['p_group']);

			foreach ($itemsOfGroup as $key => $item) {
				$purchaseGroupList[] = $item['id_purchase'];
			}

			if(!$purchaseGroupList) {
				array_push($purchaseGroupList, '0');
			}
			
			$query['f_group'] = 'id';
			$query['fs_group'] = 'in';
			$query['fg_value'] = $purchaseGroupList;			
		}

		// Grupo de Produtos
		$p_group['simbol'] = '!=';
		$p_group['value'] = '';

		if(isset($filter['p_group']) && !empty($filter['p_group'])) {
			$p_group['simbol'] = '=';
			$p_group['value'] = $filter['p_group'];
		}

		$query['f_provider'] = 'id_provider';
		$query['fs_provider'] = '!=';
		$query['fp_value'] = '';

		if(!empty($filter['by_selected'])) {

			if($filter['by'] == 'provider') {

				$query['fs_provider'] = '=';
				$query['fp_value'] = $filter['by_selected'];

			} elseif($filter['by'] == 'product') {
				$itemsOfProduct = Purchase::getPurchasesWithProducts($filter['by_selected'], $p_group);

				foreach ($itemsOfProduct as $key => $item) {
					$purchaseList[] = $item['id_purchase'];
				}

				$query['f_provider'] = 'id';
				$query['fs_provider'] = 'in';
				$query['fp_value'] = $purchaseList;
			}
		}

		// Forma de Pagamento
		$query['fp_payment'] = 'id';
		$query['fs_payment'] = '!=';
		$query['fv_payment'] = '';

		if(!empty($filter['payment_form'])) {
			$query['fp_payment'] = 'id_payment_form';
			$query['fs_payment'] = 'in';
			$query['fv_payment'] = $filter['payment_form'];
		}

		// Intervalo de datas
		$query['f_date_init'] = 'release_date';
		$query['f_date_init_simbol'] = '!=';
		$query['f_date_init_value'] = '';

		$query['f_date_end'] = 'release_date';
		$query['f_date_end_simbol'] = '!=';
		$query['f_date_end_value'] = '';


		if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
			if(UtilsController::isDate($filter['d_init'])) {
				if(UtilsController::isDate($filter['d_end'])) {
					$dateInit = UtilsController::convertDate($filter['d_init']);
					$dateEnd = UtilsController::convertDate($filter['d_end']);
					
					$query['f_date_init_simbol'] = '>=';
					$query['f_date_init_value'] = $dateInit;
					
					$query['f_date_end_simbol'] = '<=';
					$query['f_date_end_value'] = $dateEnd;
				}
			}
		}

		return $query;
	}

	// Entregas Relatórios
	public static function getFilterReportsDeliveries($filter) {
		$query['d_init'] = 'date_deliverie';
		$query['d_init_simbol'] = '!=';
		$query['d_init_value'] = '';

		$query['d_end'] = 'date_deliverie';
		$query['d_end_simbol'] = '!=';
		$query['d_end_value'] = '';

		if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
			if(UtilsController::isDate($filter['d_init'])) {
				if(UtilsController::isDate($filter['d_end'])) {
					$dateInit = UtilsController::convertDate($filter['d_init']);
					$dateEnd = UtilsController::convertDate($filter['d_end']);
					
					$query['d_init_simbol'] = '>=';
					$query['d_init_value'] = $dateInit;
					
					$query['d_end_simbol'] = '<=';
					$query['d_end_value'] = $dateEnd;
				}
			}
		}

		$query['d_situation'] = 'status';
		$query['d_situation_simbol'] = '!=';
		$query['d_situation_value'] = '';


		if(isset($filter['status']) && !empty($filter['status'])) {
			$query['d_situation_simbol'] = 'in';
			$query['d_situation_value'] = $filter['status'];
		}


		$query['d_request'] = 'id_deliverie';
		$query['d_request_simbol'] = '!=';
		$query['d_request_value'] = '';

		if($filter['request']) {
			$deliveriesList = Deliverie::getDeliveries($filter['request']);

			if($deliveriesList) {

				foreach ($deliveriesList as $item) {
					$newArray[] = $item['id'];
				}

				$query['d_request_simbol'] = 'in';
				$query['d_request_value'] = $newArray;
			}
		}

		$query['d_shipping'] = 'id';
		$query['d_shipping_simbol'] = '!=';
		$query['d_shipping_value'] = '';

		if($filter['doc_number']) {
			$query['d_shipping'] = 'shipping_invoice';
			$query['d_shipping_simbol'] = 'LIKE';
			$query['d_shipping_value'] = '%'. $filter['doc_number'] .'%';
		}

		$query['d_provider'] = 'id_provider';
		$query['d_provider_simbol'] = '!=';
		$query['d_provider_value'] = '';

		if($filter['provider']) {
			$query['d_provider_simbol'] = '=';
			$query['d_provider_value'] = $filter['provider'];
		}

		$query['d_pgroup'] = 'product_group';
		$query['d_pgroup_simbol'] = '!=';
		$query['d_pgroup_value'] = '';

		if($filter['p_group']) {
			$query['d_pgroup_simbol'] = '=';
			$query['d_pgroup_value'] = $filter['p_group'];
		}

		$query['d_product'] = 'id_product';
		$query['d_product_simbol'] = '!=';
		$query['d_product_value'] = '';

		if($filter['product']) {
			$query['d_product_simbol'] = '=';
			$query['d_product_value'] = $filter['product'];
		}

		$query['d_client'] = 'id_client';
		$query['d_client_simbol'] = '!=';
		$query['d_client_value'] = '';

		if($filter['client']) {
			$query['d_client_simbol'] = '=';
			$query['d_client_value'] = $filter['client'];
		}

		$query['d_shipp_company'] = 'id';
		$query['d_shipp_company_simbol'] = '!=';
		$query['d_shipp_company_value'] = '';

		if($filter['shipping_comp']) {
			$query['d_shipp_company'] = 'id_shipping_company';
			$query['d_shipp_company_simbol'] = '=';
			$query['d_shipp_company_value'] = $filter['shipping_comp'];
		}

		return $query;
	}

	// Relatórios Contas a Pagar
	public static function filterReportsBillsToPay($filter) {
		$query['d_init'] = 'expirate_date';
		$query['d_init_simbol'] = '!=';
		$query['d_init_value'] = '';

		$query['d_end'] = 'expirate_date';
		$query['d_end_simbol'] = '!=';
		$query['d_end_value'] = '';

		if($filter['expirate_where']) {
			$query['d_init'] = $filter['expirate_where'];
			$query['d_end'] = $filter['expirate_where'];
		}

		if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
			if(UtilsController::isDate($filter['d_init'])) {
				if(UtilsController::isDate($filter['d_end'])) {
					$dateInit = UtilsController::convertDate($filter['d_init']);
					$dateEnd = UtilsController::convertDate($filter['d_end']);
					
					$query['d_init_simbol'] = '>=';
					$query['d_init_value'] = $dateInit;
					
					$query['d_end_simbol'] = '<=';
					$query['d_end_value'] = $dateEnd;
				}
			}
		}

		$query['d_status'] = 'status';
		$query['d_status_simbol'] = '!=';
		$query['d_status_value'] = '';


		if(isset($filter['status']) && !empty($filter['status'])) {
			$query['d_status_simbol'] = '=';
			$query['d_status_value'] = $filter['status'];
		}


		$query['d_docnumber'] = 'id';
		$query['d_docnumber_simbol'] = '!=';
		$query['d_docnumber_value'] = '';

		if($filter['document_number']) {
			$query['d_docnumber'] = 'document_number';
			$query['d_docnumber_simbol'] = '=';
			$query['d_docnumber_value'] = $filter['document_number'];
		}

		$query['d_billstype'] = 'id';
		$query['d_billstype_simbol'] = '!=';
		$query['d_billstype_value'] = '';

		if(isset($filter['bills_type']) && !empty($filter['bills_type'])) {
			$query['d_billstype'] = 'billstopay_type';
			$query['d_billstype_simbol'] = 'in';
			$query['d_billstype_value'] = $filter['bills_type'];
		}

		$query['d_provider'] = 'id';
		$query['d_provider_simbol'] = '!=';
		$query['d_provider_value'] = '';

		if($filter['provider_name']) {
			$query['d_provider'] = 'provider_description';
			$query['d_provider_simbol'] = 'LIKE';
			$query['d_provider_value'] = '%'. $filter['provider_name'] .'%';
		}

		$query['d_formpay'] = 'id';
		$query['d_formpay_simbol'] = '!=';
		$query['d_formpay_value'] = '';

		if(isset($filter['payment_form']) && !empty($filter['payment_form'])) {
			$query['d_formpay'] = 'form_to_pay';
			$query['d_formpay_simbol'] = 'in';
			$query['d_formpay_value'] = $filter['payment_form'];
		}

		$query['d_situation'] = 'id_situation';
		$query['d_situation_simbol'] = '!=';
		$query['d_situation_value'] = '';

		if(isset($filter['situation_account']) && !empty($filter['situation_account'])) {
			$query['d_situation_simbol'] = 'in';
			$query['d_situation_value'] = $filter['situation_account'];
		}

		return $query;
	}

	// Relatórios Contas a Receber
	public static function filterReportsBillsToReceiver($filter) {
		$query['d_init'] = 'expirate_date';
		$query['d_init_simbol'] = '!=';
		$query['d_init_value'] = '';

		$query['d_end'] = 'expirate_date';
		$query['d_end_simbol'] = '!=';
		$query['d_end_value'] = '';

		if($filter['expirate_where']) {
			$query['d_init'] = $filter['expirate_where'];
			$query['d_end'] = $filter['expirate_where'];
		}

		if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
			if(UtilsController::isDate($filter['d_init'])) {
				if(UtilsController::isDate($filter['d_end'])) {
					$dateInit = UtilsController::convertDate($filter['d_init']);
					$dateEnd = UtilsController::convertDate($filter['d_end']);
					
					$query['d_init_simbol'] = '>=';
					$query['d_init_value'] = $dateInit;
					
					$query['d_end_simbol'] = '<=';
					$query['d_end_value'] = $dateEnd;
				}
			}
		}

		$query['d_status'] = 'status';
		$query['d_status_simbol'] = '!=';
		$query['d_status_value'] = '';


		if(isset($filter['status']) && !empty($filter['status'])) {
			$query['d_status_simbol'] = '=';
			$query['d_status_value'] = $filter['status'];
		}

		$query['d_provider'] = 'id';
		$query['d_provider_simbol'] = '!=';
		$query['d_provider_value'] = '';

		if($filter['provider_name']) {
			$query['d_provider'] = 'provider_description';
			$query['d_provider_simbol'] = 'LIKE';
			$query['d_provider_value'] = '%'. $filter['provider_name'] .'%';
		}

		$query['d_billstype'] = 'id';
		$query['d_billstype_simbol'] = '!=';
		$query['d_billstype_value'] = '';

		if(isset($filter['origin_type']) && !empty($filter['origin_type'])) {
			$query['d_billstype'] = 'receiver_origin';
			$query['d_billstype_simbol'] = 'in';
			$query['d_billstype_value'] = $filter['origin_type'];
		}

		$query['d_formpay'] = 'id';
		$query['d_formpay_simbol'] = '!=';
		$query['d_formpay_value'] = '';

		if(isset($filter['payment_form']) && !empty($filter['payment_form'])) {
			$query['d_formpay'] = 'form_to_pay';
			$query['d_formpay_simbol'] = 'in';
			$query['d_formpay_value'] = $filter['payment_form'];
		}

		$query['d_situation'] = 'id_situation';
		$query['d_situation_simbol'] = '!=';
		$query['d_situation_value'] = '';

		if(isset($filter['situation_account']) && !empty($filter['situation_account'])) {
			$query['d_situation_simbol'] = 'in';
			$query['d_situation_value'] = $filter['situation_account'];
		}

		$query['d_docnumber'] = 'id';
		$query['d_docnumber_simbol'] = '!=';
		$query['d_docnumber_value'] = '';

		if($filter['document_number']) {
			$query['d_docnumber'] = 'document_number';
			$query['d_docnumber_simbol'] = '=';
			$query['d_docnumber_value'] = $filter['document_number'];
		}

		$query['d_reqnumber'] = 'id';
		$query['d_reqnumber_simbol'] = '!=';
		$query['d_reqnumber_value'] = '';

		if($filter['request_number']) {
			$query['d_reqnumber'] = 'id_request';
			$query['d_reqnumber_simbol'] = '=';
			$query['d_reqnumber_value'] = $filter['request_number'];
		}		

		return $query;
	}

	// Pedidos Relatórios
	public static function getFilterReportsRequests($filter) {
		$query['d_init'] = 'date_created';
		$query['d_init_simbol'] = '!=';
		$query['d_init_value'] = '';

		$query['d_end'] = 'date_created';
		$query['d_end_simbol'] = '!=';
		$query['d_end_value'] = '';

		if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
			if(UtilsController::isDate($filter['d_init'])) {
				if(UtilsController::isDate($filter['d_end'])) {
					$dateInit = UtilsController::convertDate($filter['d_init']);
					$dateEnd = UtilsController::convertDate($filter['d_end']);
					
					$query['d_init_simbol'] = '>=';
					$query['d_init_value'] = $dateInit.' 00:00:00';
					
					$query['d_end_simbol'] = '<=';
					$query['d_end_value'] = $dateEnd.' 23:59:59';
				}
			}
		}

		// Verificar grupo de produtos
		$arrayProduct = array();
		$whereIntern['d_productsgroup'] = 'id';
		$whereIntern['d_productsgroup_simbol'] = '!=';
		$whereIntern['d_productsgroup_value'] = '';

		if(!empty($filter['p_group'])) {
			$list = Product::getAllFromGroup($filter['p_group']);

			foreach ($list as $item) {
				$arrayProduct[] = $item['id'];
			}

			$whereIntern['d_productsgroup'] = 'id_product';
			$whereIntern['d_productsgroup_simbol'] = 'in';
			$whereIntern['d_productsgroup_value'] = $arrayProduct;
		}

		// Verificar produtos selecionados
		$whereIntern['d_products'] = 'id_product';
		$whereIntern['d_products_simbol'] = '!=';
		$whereIntern['d_products_value'] = '';

		if(isset($filter['products']) && !empty($filter['products'])) {

			$whereIntern['d_products'] = 'id_product';
			$whereIntern['d_products_simbol'] = 'in';
			$whereIntern['d_products_value'] = $filter['products'];
		}

		// Capturar os pedidos (Todos ou de determinados produtos)
		$query['requestProduct'] = Request::getFromRequest($whereIntern);

		$query['d_situation'] = 'status';
		$query['d_situation_simbol'] = '!=';
		$query['d_situation_value'] = '';

		if(isset($filter['status']) && !empty($filter['status'])) {
			$query['d_situation_simbol'] = 'in';
			$query['d_situation_value'] = $filter['status'];
		}		

		$query['d_client'] = 'id';
		$query['d_client_simbol'] = '!=';
		$query['d_client_value'] = '';

		if($filter['client']) {
			$query['d_client'] = 'id_client';
			$query['d_client_simbol'] = '=';
			$query['d_client_value'] = $filter['client'];
		}

		$query['d_provider'] = 'id';
		$query['d_provider_simbol'] = '!=';
		$query['d_provider_value'] = '';

		if($filter['provider']) {
			$query['d_provider'] = 'id_provider';
			$query['d_provider_simbol'] = '=';
			$query['d_provider_value'] = $filter['provider'];
		}

		$query['d_salesman'] = 'id';
		$query['d_salesman_simbol'] = '!=';
		$query['d_salesman_value'] = '';

		if($filter['salesman']) {
			$query['d_salesman'] = 'id_salesman';
			$query['d_salesman_simbol'] = '=';
			$query['d_salesman_value'] = $filter['salesman'];
		}		

		return $query;
	}

	public static function filterInteractions($filter, $user) {
		// $query['clientsList'] = Interaction::getClientBySalesman($user);

		$query['f_interaction_user'] = 'id_salesman';
		$query['f_interaction_user_simbol'] = '=';
		$query['f_interaction_user_value'] = $user;

		// Busca por texto
		$query['f_interaction'] = 'id';
		$query['f_interaction_simbol'] = '!=';
		$query['f_interaction_value'] = '';

		if(!empty($filter['opt'])) {
			$query['f_interaction'] = $filter['opt'];
			$query['f_interaction_simbol'] = 'LIKE';
			$query['f_interaction_value'] = '%'. $filter['tag'] .'%';
		}
		// Fim Busca por texto

		$query['f_interactiontype'] = 'type';
		$query['f_interactiontype_simbol'] = '!=';
		$query['f_interactiontype_value'] = '';

		if(!empty($filter['type_interaction'])) {
			$query['f_interactiontype_simbol'] = '=';
			$query['f_interactiontype_value'] = $filter['type_interaction'];
		}

		// Filtro sessão categoria de interção (Produtos)
		$query['f_interaction_category'] = 'category';
		$query['f_interaction_category_simbol'] = '!=';
		$query['f_interaction_category_value'] = '';

		if(!empty($filter['category'] && $filter['category'] != 3)) {
			$query['f_interaction_category_simbol'] = '=';
			$query['f_interaction_category_value'] = $filter['category'];
		}

		// $query['d_init'] = 'id';
		// $query['d_init_simbol'] = '!=';
		// $query['d_init_value'] = '';

		// $query['d_end'] = 'id';
		// $query['d_end_simbol'] = '!=';
		// $query['d_end_value'] = '';

		// if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
		// 	if(UtilsController::isDate($filter['d_init'])) {
		// 		if(UtilsController::isDate($filter['d_end'])) {
		// 			$dateInit = UtilsController::convertDate($filter['d_init']);
		// 			$dateEnd = UtilsController::convertDate($filter['d_end']);
					
		// 			$query['d_init'] = 'next_interation';
		// 			$query['d_init_simbol'] = '>=';
		// 			$query['d_init_value'] = $dateInit;
					
		// 			$query['d_end'] = 'next_interation';
		// 			$query['d_end_simbol'] = '<=';
		// 			$query['d_end_value'] = $dateEnd;
		// 		}
		// 	}
		// }

		return $query;
	}


	// Filtro Interações nível administrador
	public static function filterInteractionsAdministrative($filter) {
		$query['f_interaction'] = 'id';
		$query['f_interaction_simbol'] = '!=';
		$query['f_interaction_value'] = '';

		if(!empty($filter['opt'])) {
			$query['f_interaction'] = $filter['opt'];
			$query['f_interaction_simbol'] = 'LIKE';
			$query['f_interaction_value'] = '%'. $filter['tag'] .'%';
		}

		$query['f_interactiontype'] = 'type';
		$query['f_interactiontype_simbol'] = '!=';
		$query['f_interactiontype_value'] = '';

		if(!empty($filter['type_interaction'])) {
			$query['f_interactiontype_simbol'] = '=';
			$query['f_interactiontype_value'] = $filter['type_interaction'];
		}

		// Filtro sessão categoria de interção (Produtos)
		$query['f_interaction_category'] = 'category';
		$query['f_interaction_category_simbol'] = '!=';
		$query['f_interaction_category_value'] = '';

		if(!empty($filter['category'] && $filter['category'] != 3)) {
			$query['f_interaction_category_simbol'] = '=';
			$query['f_interaction_category_value'] = $filter['category'];
		}

		// Filtro sessão vendedor
		$query['f_interaction_salesman'] = 'id_salesman';
		$query['f_interaction_salesman_simbol'] = '!=';
		$query['f_interaction_salesman_value'] = '';

		if(!empty($filter['salesman'])) {
			$query['f_interaction_salesman_simbol'] = '=';
			$query['f_interaction_salesman_value'] = $filter['salesman'];
		}

		$query['d_init'] = 'id';
		$query['d_init_simbol'] = '!=';
		$query['d_init_value'] = '';

		$query['d_end'] = 'id';
		$query['d_end_simbol'] = '!=';
		$query['d_end_value'] = '';

		if(!empty($filter['d_init']) && !empty($filter['d_end'])) {
			if(UtilsController::isDate($filter['d_init'])) {
				if(UtilsController::isDate($filter['d_end'])) {
					$dateInit = UtilsController::convertDate($filter['d_init']);
					$dateEnd = UtilsController::convertDate($filter['d_end']);
					
					$query['d_init'] = 'next_interation';
					$query['d_init_simbol'] = '>=';
					$query['d_init_value'] = $dateInit;
					
					$query['d_end'] = 'next_interation';
					$query['d_end_simbol'] = '<=';
					$query['d_end_value'] = $dateEnd;
				}
			}
		}

		return $query;
	}

}