<?php
require_once("../config/conexao.php");
require_once("../checar_loja.php");

class Conexao
{
    private static $connection;

    private function __construct(){}

    public static function getConnection($D) {
		
			if($D == 1){
				// ################################################# OFFICIALL
				define('DB_HOST'        , "192.168.0.237");
				define('DB_NAME'        , "UNI");
				// ################################################# OFFICIALL
			}else{
				// ################################################# FILIAL
				define('DB_HOST'        , "192.168.0.237");
				define('DB_NAME'        , "SOF");
				// ################################################# FILIAL
			}
			define('DB_USER'        , "projetos");
			define('DB_PASSWORD'    , "projetos");
			define('DB_DRIVER'      , "sqlsrv");
		

        $pdoConfig  = DB_DRIVER . ":". "Server=" . DB_HOST . ";";
        $pdoConfig .= "Database=".DB_NAME.";";

        try {
            if(!isset($connection)){
                $connection =  new PDO($pdoConfig, DB_USER, DB_PASSWORD);
                $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            return $connection;
         } catch (PDOException $e) {
            $mensagem = "Drivers disponiveis: " . implode(",", PDO::getAvailableDrivers());
            $mensagem .= "\nErro: " . $e->getMessage();
            throw new Exception($mensagem);
         }
    }
	 
	public static function getItens($Conexao) {
		 
		 	$file  = fopen("..\..\ASV\INSERT_UNIDOS.txt","r");
			$data1 = fgets($file);
			fclose($file);
		 
		     $query = $Conexao->query("SELECT [SQL_ICODBUS]
                                          ,[SQL_IDESCRI]
                                          ,[SQL_IINDEMB]
                                          ,[SQL_ILOCDEP]
                                          ,[SQL_IAPLICA]
                                          ,[SQL_IMARCA]
                                          ,[SQL_IFORNEC_1]
                                          ,[SQL_IREFFAB_1]
                                          ,[SQL_IFORNEC_2]
                                          ,[SQL_IREFFAB_2]
                                      FROM [SOF].[dbo].[ITENS_GIM] WHERE [SQL_IDATCAD] >= '$data1'  ORDER BY [SQL_IDATCAD] ASC");
									  
				$produtos   = $query->fetchAll();
				$count      = $query->rowCount();
				
				$result[0] = $produtos;
				$result[1] = $count;	
				
				return $result;

	 }	
	 
	 public static function getStock($Conexao) {
		 
				$query      = $Conexao->query("SELECT
													'1' AS SQL_LOJA,
													I.SQL_IQCODBUS AS SQL_IQCODBUS,
													(I.SQL_IQQTDANT+I.SQL_IQQTDENT-I.SQL_IQQTDSAI) AS SQL_SALDO
													FROM [ITEQV_GIM] I 
														INNER JOIN
															(select MAX(sql_iqanomes) as REF_ANOMES, SQL_IQCODBUS AS REF_CODIGO FROM [ITEQV_GIM] GROUP BY SQL_IQCODBUS) R
																ON I.SQL_IQCODBUS = R.REF_CODIGO AND I.SQL_IQANOMES = R.REF_ANOMES
															INNER JOIN [SOF].[dbo].[ITENS_GIM] M ON R.REF_CODIGO = M.SQL_ICODBUS");
												  
				$produtos   = $query->fetchAll();
				$count = $query->rowCount();
				
				$result[0] = $produtos;
				$result[1] = $count;	
				
				return $result;

	 }
	 
}