{*******************************************************************************
Title: T2Ti ERP                                                                 
Description: Controller do lado Cliente relacionado à tabela [EMPRESA] 
                                                                                
The MIT License                                                                 
                                                                                
Copyright: Copyright (C) 2017 T2Ti.COM
                                                                                
Permission is hereby granted, free of charge, to any person                     
obtaining a copy of this software and associated documentation                  
files (the "Software"), to deal in the Software without                         
restriction, including without limitation the rights to use,                    
copy, modify, merge, publish, distribute, sublicense, and/or sell               
copies of the Software, and to permit persons to whom the                       
Software is furnished to do so, subject to the following                        
conditions:                                                                     
                                                                                
The above copyright notice and this permission notice shall be                  
included in all copies or substantial portions of the Software.                 
                                                                                
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,                 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES                 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                        
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT                     
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,                    
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING                    
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

       The author may be contacted at:
           t2ti.com@gmail.com

@author Albert Eije (t2ti.com@gmail.com)
@version 1.0
*******************************************************************************}
unit EmpresaController;

interface

uses
  Classes, Dialogs, SysUtils, DBClient, DB, Windows, Forms, Controller, Rtti, Atributos,
  Generics.Collections,

  EmpresaDTO, EmpresaEnderecoDTO, EmpresaContatoDTO, EmpresaTelefoneDTO,

  SynCommons,          // framework core
  mORMot,              // RESTful server & ORM
  mORMotHttpClient     // HTTP client to a mORMot RESTful server
  ;


type
  TEmpresaController = class(TController)
  private
    class var FDataSet: TClientDataSet;
  public
    class procedure Consulta(pCampos, pValores: String; pPagina: String);
    class function ConsultaDTO(pFiltro: String): TEmpresa;

    class function GetDataSet: TClientDataSet; override;
    class procedure SetDataSet(pDataSet: TClientDataSet); override;
  end;

implementation

class procedure TEmpresaController.Consulta(pCampos, pValores: String; pPagina: String);
var
  Retorno: TObjectList<TEmpresa>;
  RetornoObjeto: TEmpresa;

  Client: TSQLRest;
  aModel: TSQLModel;
begin
  try
    aModel := TSQLModel.Create(
    [
      TEmpresa,
      TEmpresa_Endereco,
      TEmpresa_Contato,
      TEmpresa_Telefone
    ], 'root');

    Client := TSQLHttpClientWinHTTP.Create('34.230.92.11','888',aModel);

    Retorno := Client.RetrieveList<TEmpresa>(pCampos, [pValores]);

    for RetornoObjeto in Retorno do
      RetornoObjeto.PK := RetornoObjeto.GetID;

    if Assigned(Retorno) then
      TratarRetorno<TEmpresa>(Retorno);
  finally
  end;
end;

class function TEmpresaController.ConsultaDTO(pFiltro: String): TEmpresa;
var
  Client: TSQLRest;
  aModel: TSQLModel;

  EnderecosEnumerator: TEnumerator<TEmpresa_Endereco>;
begin
  try
    aModel := TSQLModel.Create(
    [
      TEmpresa,
      TEmpresa_Endereco,
      TEmpresa_Contato,
      TEmpresa_Telefone
    ], 'root');

    Client := TSQLHttpClientWinHTTP.Create('34.230.92.11','888',aModel);

    Result := TEmpresa.Create;
    Client.Retrieve(pFiltro, Result);

    // Listas
    Result.ListaEmpresaEnderecoDTO := Client.RetrieveList<TEmpresa_Endereco>('IdEmpresa = ?', [Result.ID]);

    // Procura o endereço principal para uso
    EnderecosEnumerator := Result.ListaEmpresaEnderecoDTO.GetEnumerator;
    try
      with EnderecosEnumerator do
      begin
        while MoveNext do
        begin
          if Current.Principal = 'S' then
          begin
            Result.EnderecoPrincipal := Current;
          end;
        end;
      end;
    finally
      FreeAndNil(EnderecosEnumerator);
    end;

  finally
  end;
end;

class function TEmpresaController.GetDataSet: TClientDataSet;
begin
  Result := FDataSet;
end;

class procedure TEmpresaController.SetDataSet(pDataSet: TClientDataSet);
begin
  FDataSet := pDataSet;
end;

initialization
  Classes.RegisterClass(TEmpresaController);

finalization
  Classes.UnRegisterClass(TEmpresaController);

end.

