openapi: 3.0.3
info:
  title: eCosif Moviments API
  description: |
    API para gerenciamento de movimentações contábeis do sistema eCosif.
    
    Este serviço gerencia:
    - **Lotes (Batches)**: Agrupamento lógico de lançamentos
    - **Documentos**: Documentos contábeis dentro de lotes
    - **Lançamentos**: Lançamentos contábeis individuais (débito/crédito)
    - **Importação**: Importação de arquivos IPL e CSV
    - **Consolidação**: Consolidação contábil entre empresas/filiais
    - **Cálculo de Cotas**: Cálculo de cotas tributárias
    - **Abertura/Fechamento**: Controle de períodos contábeis
    
    ## 🔐 Autenticação
    
    **IMPORTANTE**: Este serviço requer autenticação JWT.
    
    ### Como obter o token:
    1. Faça login no **ecosif-auth** (porta 8080): `POST /api/auth/signin`
    2. Copie o `accessToken` retornado
    3. Clique no botão **'Authorize'** 🔒 no topo desta página
    4. Digite: `Bearer <seu-token>`
    5. Clique em **'Authorize'**
    6. Agora você pode testar todos os endpoints
    
  version: 0.7.01.202511271
  contact:
    name: eCosif Team
    email: support@ecosif.net.br
    url: https://www.ecosif.net.br
  license:
    name: Commercial License
    url: https://www.ecosif.net.br/licenses/

servers:
  - url: http://localhost:8082
    description: Servidor de Desenvolvimento

tags:
  - name: Lotes
    description: Gerenciamento de lotes contábeis
  - name: Documentos
    description: Gerenciamento de documentos contábeis
  - name: Lançamentos
    description: Gerenciamento de lançamentos contábeis
  - name: Importação
    description: Importação de arquivos
  - name: Consolidação
    description: Consolidação contábil
  - name: Calendário
    description: Operações de calendário contábil
  - name: Cotas
    description: Cálculo de cotas tributárias
  - name: Admin
    description: Operações administrativas

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: JWT token obtido através do ecosif-auth (porta 8080)

  schemas:
    BatchDTO:
      type: object
      properties:
        id:
          type: integer
          format: int64
        lote:
          type: string
          example: "001"
        empresa:
          type: string
          example: "00001"
        filial:
          type: string
          example: "00001"
        ano:
          type: integer
          example: 2025
        mes:
          type: integer
          example: 11
        descricao:
          type: string
          example: "Lote de Novembro"
    
    DocumentDTO:
      type: object
      properties:
        id:
          type: integer
          format: int64
        batchId:
          type: integer
          format: int64
        documento:
          type: string
          example: "000001"
        diaref:
          type: string
          example: "15"
        texto:
          type: string
          example: "Documento de teste"
        cdpadronizado:
          type: string
          example: "001"
    
    EntryDTO:
      type: object
      required:
        - documentId
        - lancamento
        - debcre
        - day
        - contaId
        - valor
      properties:
        id:
          type: integer
          format: int64
        documentId:
          type: integer
          format: int64
        lancamento:
          type: string
          maxLength: 5
          example: "001"
        debcre:
          type: string
          enum: [D, C]
          example: "D"
        day:
          type: string
          maxLength: 2
          example: "15"
        contaId:
          type: integer
          format: int64
        contrapartida:
          type: integer
          format: int64
          nullable: true
        valor:
          type: number
          format: double
          example: 1000.00
        historico:
          type: string
          maxLength: 255
        cdhistorico:
          type: string
          maxLength: 4
    
    ConsolidationDTO:
      type: object
      properties:
        company:
          type: string
        branch:
          type: string
        year:
          type: string
        month:
          type: string
        status:
          type: string
          enum: [PENDING, PROCESSING, COMPLETED, ERROR]
    
    ImportResultDTO:
      type: object
      properties:
        success:
          type: boolean
        totalRows:
          type: integer
        successRows:
          type: integer
        errorRows:
          type: integer
        errors:
          type: array
          items:
            type: string
        message:
          type: string
    
    ErrorResponse:
      type: object
      properties:
        message:
          type: string
        timestamp:
          type: string
          format: date-time
        status:
          type: integer
        error:
          type: string

security:
  - bearerAuth: []

paths:
  /batch:
    post:
      tags:
        - Lotes
      summary: Criar lote
      description: Cria um novo lote contábil
      operationId: createBatch
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BatchDTO'
      responses:
        '201':
          description: Lote criado com sucesso
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BatchDTO'
        '400':
          description: Dados inválidos
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Não autenticado

  /batch/{id}:
    get:
      tags:
        - Lotes
      summary: Buscar lote por ID
      operationId: getBatchById
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Lote encontrado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BatchDTO'
        '404':
          description: Lote não encontrado
    
    delete:
      tags:
        - Lotes
      summary: Excluir lote
      operationId: deleteBatch
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Lote excluído com sucesso
        '404':
          description: Lote não encontrado

  /allbatch/{company}/{branch}:
    get:
      tags:
        - Lotes
      summary: Listar todos os lotes
      description: Lista todos os lotes de uma empresa/filial
      operationId: getAllBatches
      parameters:
        - name: company
          in: path
          required: true
          schema:
            type: string
        - name: branch
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Lista de lotes
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/BatchDTO'

  /document:
    post:
      tags:
        - Documentos
      summary: Criar documento
      operationId: createDocument
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DocumentDTO'
      responses:
        '201':
          description: Documento criado com sucesso
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DocumentDTO'

  /document/{id}:
    get:
      tags:
        - Documentos
      summary: Buscar documento por ID
      operationId: getDocumentById
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Documento encontrado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DocumentDTO'
    
    delete:
      tags:
        - Documentos
      summary: Excluir documento
      operationId: deleteDocument
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Documento excluído com sucesso

  /alldocument/{batchId}:
    get:
      tags:
        - Documentos
      summary: Listar documentos de um lote
      operationId: getDocumentsByBatch
      parameters:
        - name: batchId
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Lista de documentos
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/DocumentDTO'

  /entry:
    post:
      tags:
        - Lançamentos
      summary: Criar lançamento
      description: Cria um novo lançamento contábil. Valida partidas dobradas automaticamente.
      operationId: createEntry
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EntryDTO'
      responses:
        '201':
          description: Lançamento criado com sucesso
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EntryDTO'
        '400':
          description: Dados inválidos ou partidas não conferem
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /entry/{id}:
    get:
      tags:
        - Lançamentos
      summary: Buscar lançamento por ID
      operationId: getEntryById
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Lançamento encontrado
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EntryDTO'
    
    delete:
      tags:
        - Lançamentos
      summary: Excluir lançamento
      operationId: deleteEntry
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Lançamento excluído com sucesso

  /allentry/{documentId}:
    get:
      tags:
        - Lançamentos
      summary: Listar lançamentos de um documento
      operationId: getEntriesByDocument
      parameters:
        - name: documentId
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Lista de lançamentos
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/EntryDTO'

  /batch/{batchId}/import:
    post:
      tags:
        - Importação
      summary: Importar arquivo CSV
      description: Importa lançamentos de um arquivo CSV para um lote
      operationId: importCsvFile
      parameters:
        - name: batchId
          in: path
          required: true
          schema:
            type: integer
            format: int64
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
                dryRun:
                  type: boolean
                  default: false
      responses:
        '200':
          description: Importação concluída
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ImportResultDTO'
        '400':
          description: Arquivo inválido ou vazio

  /runconsolidation:
    post:
      tags:
        - Consolidação
      summary: Executar consolidação
      description: Executa consolidação contábil para empresa/filial/período. Operação assíncrona.
      operationId: runConsolidation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - company
                - branch
                - year
                - month
              properties:
                company:
                  type: string
                branch:
                  type: string
                year:
                  type: string
                month:
                  type: string
      responses:
        '200':
          description: Consolidação iniciada
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ConsolidationDTO'

  /monthOpening/{company}/{branch}:
    get:
      tags:
        - Calendário
      summary: Obter informações de abertura de mês
      operationId: getMonthOpening
      parameters:
        - name: company
          in: path
          required: true
          schema:
            type: string
        - name: branch
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Informações de abertura
          content:
            application/json:
              schema:
                type: object
                properties:
                  initialyearmonth:
                    type: string
                  endyearmonth:
                    type: string
                  newyearmonth:
                    type: string
                  firstPostingyearmonth:
                    type: string
                  lastPostingyearmonth:
                    type: string
    
    post:
      tags:
        - Calendário
      summary: Abrir novo mês
      operationId: openNewMonth
      parameters:
        - name: company
          in: path
          required: true
          schema:
            type: string
        - name: branch
          in: path
          required: true
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                newyearmonth:
                  type: string
                  example: "12/2025"
      responses:
        '201':
          description: Mês aberto com sucesso

  /calendar/closeday/{company}/{branch}:
    get:
      tags:
        - Calendário
      summary: Fechar dia
      operationId: closeDay
      parameters:
        - name: company
          in: path
          required: true
          schema:
            type: string
        - name: branch
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Dia fechado com sucesso
          content:
            application/json:
              schema:
                type: object
                properties:
                  msg:
                    type: string

  /taxquotacalculation/{company}/{branch}:
    get:
      tags:
        - Cotas
      summary: Calcular cotas tributárias
      description: Calcula valor de cotas tributárias para empresa/filial
      operationId: calculateTaxQuota
      parameters:
        - name: company
          in: path
          required: true
          schema:
            type: string
        - name: branch
          in: path
          required: true
          schema:
            type: string
        - name: confirmed
          in: query
          schema:
            type: boolean
            default: false
      responses:
        '200':
          description: Cálculo confirmado e salvo
        '302':
          description: Preview do cálculo (não confirmado)
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
                example: ["15/11/2025", "1250.50", "1000000.00", "800000.00"]

