openapi: 3.0.0
info:
  title: Safe Invoice Serial Counter API
  description: API for managing safe invoice serial counters
  version: 1.0.0
servers:
  - url: https://api.repzo.me/v1
    description: Production server
security:
  - ApiKeyAuth: []
tags:
  - name: Safe Invoice Serial Counter
    description: Safe invoice serial counter management operations
paths:
  /safe-invoice-serial-counter:
    get:
      tags:
        - Safe Invoice Serial Counter
      summary: Get all safe invoice serial counters
      description: Retrieve a list of all safe invoice serial counters with optional filtering and pagination
      parameters:
        - name: page
          in: query
          description: Page number for pagination
          required: false
          schema:
            type: integer
            minimum: 1
            default: 1
        - name: per_page
          in: query
          description: Number of items per page
          required: false
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 25
        - name: sort
          in: query
          description: Sort field
          required: false
          schema:
            type: string
        - name: company_namespace
          in: query
          description: Company namespace for filtering
          required: false
          schema:
            type: array
            items:
              type: string
        - name: rep
          in: query
          description: Filter by representative ID
          required: false
          schema:
            type: string
        - name: warehouse
          in: query
          description: Filter by warehouse ID
          required: false
          schema:
            type: string
        - name: _id
          in: query
          description: Filter by ID
          required: false
          schema:
            type: array
            items:
              type: string
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: array
                    items:
                      $ref: "#/components/schemas/SafeInvoiceSerialCounter"
                  paging:
                    $ref: "#/components/schemas/PagingInfo"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "500":
          $ref: "#/components/responses/InternalServerError"
    post:
      tags:
        - Safe Invoice Serial Counter
      summary: Create a new safe invoice serial counter
      description: Create a new safe invoice serial counter entry
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CreateSafeInvoiceSerialCounterRequest"
      responses:
        "201":
          description: Safe invoice serial counter created successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/SafeInvoiceSerialCounter"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "422":
          $ref: "#/components/responses/ValidationError"
        "500":
          $ref: "#/components/responses/InternalServerError"
  /safe-invoice-serial-counter/{id}:
    get:
      tags:
        - Safe Invoice Serial Counter
      summary: Get safe invoice serial counter by ID
      description: Retrieve a specific safe invoice serial counter by its ID
      parameters:
        - name: id
          in: path
          required: true
          description: Safe invoice serial counter ID
          schema:
            type: string
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/SafeInvoiceSerialCounter"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "500":
          $ref: "#/components/responses/InternalServerError"
    patch:
      tags:
        - Safe Invoice Serial Counter
      summary: Update safe invoice serial counter
      description: Update an existing safe invoice serial counter
      parameters:
        - name: id
          in: path
          required: true
          description: Safe invoice serial counter ID
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/UpdateSafeInvoiceSerialCounterRequest"
      responses:
        "200":
          description: Safe invoice serial counter updated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/SafeInvoiceSerialCounter"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "422":
          $ref: "#/components/responses/ValidationError"
        "500":
          $ref: "#/components/responses/InternalServerError"
    delete:
      tags:
        - Safe Invoice Serial Counter
      summary: Delete safe invoice serial counter
      description: Delete a safe invoice serial counter by ID
      parameters:
        - name: id
          in: path
          required: true
          description: Safe invoice serial counter ID
          schema:
            type: string
      responses:
        "200":
          description: Safe invoice serial counter deleted successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "500":
          $ref: "#/components/responses/InternalServerError"
  /safe-invoice-serial-counter/next:
    post:
      tags:
        - Safe Invoice Serial Counter
      summary: Get next serial number
      description: Get the next available serial number for invoice generation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                rep:
                  type: string
                  description: Representative ID
                warehouse:
                  type: string
                  description: Warehouse ID
                invoice_type:
                  type: string
                  description: Type of invoice
                  enum: ["sales", "return", "proforma"]
              required:
                - rep
                - warehouse
                - invoice_type
      responses:
        "200":
          description: Next serial number retrieved successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: object
                    properties:
                      next_serial:
                        type: integer
                        description: Next available serial number
                        example: 1001
                      formatted_serial:
                        type: string
                        description: Formatted serial number
                        example: "INV-2024-001001"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "500":
          $ref: "#/components/responses/InternalServerError"
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: "Use format: Bearer {api_key}"
  schemas:
    SafeInvoiceSerialCounter:
      type: object
      properties:
        _id:
          type: string
          description: Unique identifier
          example: "507f1f77bcf86cd799439011"
        rep:
          type: string
          description: Representative ID
          example: "507f1f77bcf86cd799439012"
        warehouse:
          type: string
          description: Warehouse ID
          example: "507f1f77bcf86cd799439013"
        invoice_type:
          type: string
          description: Type of invoice
          enum: ["sales", "return", "proforma"]
          example: "sales"
        current_serial:
          type: integer
          description: Current serial number
          example: 1000
        last_used_date:
          type: string
          format: date-time
          description: Date when serial was last used
        prefix:
          type: string
          description: Serial number prefix
          example: "INV-2024-"
        suffix:
          type: string
          description: Serial number suffix
        pad_length:
          type: integer
          description: Length to pad the serial number
          example: 6
        company_namespace:
          type: array
          items:
            type: string
          description: Company namespace
        createdAt:
          type: string
          format: date-time
          description: Creation timestamp
        modifiedAt:
          type: string
          format: date-time
          description: Last modification timestamp
        SVClient:
          type: integer
          description: Client version
        __v:
          type: integer
          description: Document version
      required:
        - rep
        - warehouse
        - invoice_type
        - current_serial
    CreateSafeInvoiceSerialCounterRequest:
      type: object
      properties:
        rep:
          type: string
          description: Representative ID
          example: "507f1f77bcf86cd799439012"
        warehouse:
          type: string
          description: Warehouse ID
          example: "507f1f77bcf86cd799439013"
        invoice_type:
          type: string
          description: Type of invoice
          enum: ["sales", "return", "proforma"]
          example: "sales"
        current_serial:
          type: integer
          description: Starting serial number
          default: 0
        prefix:
          type: string
          description: Serial number prefix
          example: "INV-2024-"
        suffix:
          type: string
          description: Serial number suffix
        pad_length:
          type: integer
          description: Length to pad the serial number
          default: 6
        company_namespace:
          type: array
          items:
            type: string
          description: Company namespace
      required:
        - rep
        - warehouse
        - invoice_type
    UpdateSafeInvoiceSerialCounterRequest:
      type: object
      properties:
        current_serial:
          type: integer
          description: Current serial number
        prefix:
          type: string
          description: Serial number prefix
        suffix:
          type: string
          description: Serial number suffix
        pad_length:
          type: integer
          description: Length to pad the serial number
        company_namespace:
          type: array
          items:
            type: string
          description: Company namespace
    PagingInfo:
      type: object
      properties:
        total:
          type: integer
          description: Total number of items
        page:
          type: integer
          description: Current page number
        per_page:
          type: integer
          description: Items per page
        pages:
          type: integer
          description: Total number of pages
  responses:
    BadRequest:
      description: Bad request
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Invalid request parameters"
    Unauthorized:
      description: Unauthorized
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Authentication required"
    Forbidden:
      description: Forbidden
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Insufficient permissions"
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Resource not found"
    ValidationError:
      description: Validation error
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Validation failed"
    InternalServerError:
      description: Internal server error
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Internal server error"
