<?xml version="1.0" encoding="utf-8"?>

<xs:schema version="1.0.0"
           xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:esocial="http://www.esocial.gov.br/schema/lote/eventos/envio/retornoProcessamento/v1_3_0"
           targetNamespace="http://www.esocial.gov.br/schema/lote/eventos/envio/retornoProcessamento/v1_3_0"
           elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:element name="eSocial">
    <xs:annotation>
      <xs:documentation>Elemento raiz do eSocial.</xs:documentation>
    </xs:annotation>

    <xs:complexType>
      <xs:sequence>
        <xs:element name="retornoProcessamentoLoteEventos">
          <xs:annotation>
            <xs:documentation>
              Xml que contém o retorno do processamento dos eventos do lote.
            </xs:documentation>
          </xs:annotation>

          <xs:complexType>
            <xs:sequence>
              <xs:element name="ideEmpregador" type="esocial:TIdeEmpregador" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Identificação do empregador.</xs:documentation>
                </xs:annotation>
              </xs:element>

              <xs:element name="ideTransmissor"  type="esocial:TIdeTransmissor" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Identificação do transmissor.</xs:documentation>
                </xs:annotation>
              </xs:element>

              <xs:element name="status" type="esocial:TStatus">
                <xs:annotation>
                  <xs:documentation>Contém o status atual do lote.</xs:documentation>
                </xs:annotation>
              </xs:element>

              <xs:element name="dadosRecepcaoLote" type="esocial:TDadosRecepcao" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Contém os dados de recepção do lote.</xs:documentation>
                </xs:annotation>
              </xs:element>

              <xs:element name="dadosProcessamentoLote" type="esocial:TDadosProcessamento" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Contém os dados de processamento do lote.</xs:documentation>
                </xs:annotation>
              </xs:element>

              <xs:element name="retornoEventos" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Contém o(s) resultado(s) do processamento dos eventos do lote.</xs:documentation>
                </xs:annotation>

                <xs:complexType>
                  <xs:sequence maxOccurs="50">
                    <xs:element name="evento">
                      <xs:complexType>

                        <xs:sequence>
                          <xs:element name="retornoEvento" type="esocial:TArquivoEsocial">
                            <xs:annotation>
                              <xs:documentation>Contém o recibo do evento se o mesmo foi recebido com sucesso, ou as inconsistências caso não tenha sido recebido.</xs:documentation>
                            </xs:annotation>
                          </xs:element>

                          <xs:element name="tot" minOccurs="0" maxOccurs="unbounded">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:any processContents="skip">
                                  <xs:annotation>
                                    <xs:documentation>Contém xml com o evento totalizador do tipo especificado no atributo tipo.</xs:documentation>
                                  </xs:annotation>
                                </xs:any>
                              </xs:sequence>

                              <xs:attribute name="tipo" use="required">
                                <xs:annotation>
                                  <xs:documentation>
                                    É neste atributo que será informado o tipo do totalizador que será retornado.
                                  </xs:documentation>
                                </xs:annotation>
                              </xs:attribute>
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>

                        <xs:attribute name="Id" use="required" type="xs:ID">
                          <xs:annotation>
                            <xs:documentation>
                              É através deste atributo que o empregador conseguirá fazer o mapeamento
                              entre o evento que ele enviou e o resultado do processamento de cada evento.
                            </xs:documentation>
                          </xs:annotation>
                        </xs:attribute>

                        <xs:attribute name="evtDupl" use="optional" type="xs:boolean">
                          <xs:annotation>
                            <xs:documentation>
                              Este atributo indica se o recibo que está sendo retornado é de um evento que já foi recebido anteriormente ou não.
                              Ele somente será preenchido no caso de ser true.
                            </xs:documentation>
                          </xs:annotation>
                        </xs:attribute>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="TDadosRecepcao">
    <xs:annotation>
      <xs:documentation>Define os dados de recepção de um arquivo de lote.</xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:element name="dhRecepcao" type="xs:dateTime">
        <xs:annotation>
          <xs:documentation>Data hora recepção.</xs:documentation>
        </xs:annotation>
      </xs:element>

      <xs:element name="versaoAplicativoRecepcao">
        <xs:annotation>
          <xs:documentation>Versão do aplicativo de recepção.</xs:documentation>
        </xs:annotation>

        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="30" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="protocoloEnvio">
        <xs:annotation>
          <xs:documentation>
            Código de retorno para localização do lote.
            Número sequencial único produzido no instante de recepção da mensagem originada no empregador.
            São válidos os seguintes intervalos, expressos na base decimal:
            A.B.AAAAMM.NNNNNNNNNNNNNNNNNNN
            A = Agente de recepção: Serpro=1 ou Caixa=2 (1 posição)
            B = Ambiente de recepção: Produção=1; Pré-produção - dados reais=2; Pré-produção - dados fictícios=3; Homologação=6; Validação=7; Testes=8; Desenvolvimento=9
            AAAAMM = Ano e Mês em que o lote foi recebido.
            N = Número sequencial (19 posições)
            Exemplo:
            SERPRO: 1.1.201605.0000000000000000001
            Caixa:  2.1.201605.0000000000000000001
          </xs:documentation>
        </xs:annotation>

        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="30" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TDadosProcessamento">
    <xs:annotation>
      <xs:documentation>Define os dados de processamento de um lote de eventos.</xs:documentation>
    </xs:annotation>

    <xs:sequence>

      <xs:element name="versaoAplicativoProcessamentoLote" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Versão do aplicativo de processamento do lote.</xs:documentation>
        </xs:annotation>

        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="30" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TIdeEmpregador">
    <xs:annotation>
      <xs:documentation>Define a identificação do empregador.</xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:element name="tpInsc" type="xs:int" />
      <xs:element name="nrInsc">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="15" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TIdeTransmissor">
    <xs:annotation>
      <xs:documentation>Define a identificação do transmissor.</xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:element name="tpInsc" type="xs:int" />
      <xs:element name="nrInsc">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="15" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TArquivoEsocial">
    <xs:annotation>
      <xs:documentation>
        Define os dados de um arquivo do eSocial (evento).
      </xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:any processContents="skip">
        <xs:annotation>
          <xs:documentation>Contém xml com o retorno do processamento do evento. (conforme Xsd retornoEvento)</xs:documentation>
        </xs:annotation>
      </xs:any>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TStatus">
    <xs:annotation>
      <xs:documentation>Contém o status atual do lote ou do Evento.</xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:element name="cdResposta" type="xs:int">
        <xs:annotation>
          <xs:documentation>
            Código de resposta do processamento do lote.
          </xs:documentation>
        </xs:annotation>
      </xs:element>

      <xs:element name="descResposta">
        <xs:annotation>
          <xs:documentation>Contém a descrição correspondente ao código de resposta.</xs:documentation>
        </xs:annotation>

        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="2048" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="tempoEstimadoConclusao" type="xs:int" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Contém o tempo estimado de conclusão do processamento do lote em segundos.</xs:documentation>
        </xs:annotation>
      </xs:element>

      <xs:element name="ocorrencias" type="esocial:TOcorrencias" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Contém as ocorrências encontradas durante a validação do evento.</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="TOcorrencias">
    <xs:annotation>
      <xs:documentation>Define uma ocorrências encontrada no processamento de um arquivo.</xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:element name="ocorrencia" minOccurs="1" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>

            <xs:element name="codigo" type="xs:int">
              <xs:annotation>
                <xs:documentation>Código da ocorrência</xs:documentation>
              </xs:annotation>
            </xs:element>

            <xs:element name="descricao">
              <xs:annotation>
                <xs:documentation>Descrição da ocorrência</xs:documentation>
              </xs:annotation>

              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:maxLength value="2048" />
                </xs:restriction>
              </xs:simpleType>
            </xs:element>

            <xs:element name="tipo" type="xs:unsignedByte">
              <xs:annotation>
                <xs:documentation>Contém o tipo da ocorrência: 1 - Erro, 2 - Advertência</xs:documentation>
              </xs:annotation>
            </xs:element>

            <xs:element name="localizacao" minOccurs="0">
              <xs:annotation>
                <xs:documentation>Contém o caminho do registro e/ou campo em que ocorreu o erro.</xs:documentation>
              </xs:annotation>

              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:maxLength value="2048" />
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>