# Cambiar las reglas de autofactura

`POST https://api.fiscalisto.com/v1/issuers/{id}/self_invoicing`

Hasta cuándo se puede facturar un ticket y cada cuánto se timbra la factura global.

## Parámetros

| Nombre | Dónde | Obligatorio | Descripción |
| --- | --- | --- | --- |
| `id` | ruta | sí | El id del objeto. |
| `Idempotency-Key` | encabezado |  | Opcional (los SDK la mandan solos). Si la respuesta se pierde, repita la solicitud con el mismo valor durante 24 horas: contesta lo mismo, con idempotent-replayed: true, y no lo hace dos veces (ni crea, ni manda correos, ni cobra). |

## Cuerpo

| Campo | Tipo | Obligatorio | Descripción |
| --- | --- | --- | --- |
| `window` | `month`, `days` |  | Hasta cuándo se puede facturar un ticket: `month`, hasta el fin del mes de la venta (lo usual ante el SAT); `days`, `window_days` días después. |
| `window_days` | integer |  | Días para facturar con `window: days`. (de 1 a 365) |
| `global_periodicity` | `01`, `02`, `03`, `04` |  | c_Periodicidad de la factura global con los tickets que nadie facturó: 01 diaria, 02 semanal, 03 quincenal, 04 mensual. |

Ejemplo:

```json
{
  "window": "month",
  "global_periodicity": "04"
}
```

## Con el SDK de TypeScript

```ts
import { Fiscalisto } from "fiscalisto";

// La llave viene de FISCALISTO_API_KEY.
const fiscalisto = new Fiscalisto();

const selfInvoicing = await fiscalisto.issuers.updateSelfInvoicing("id", { window: "month", global_periodicity: "04" }, { idempotencyKey: "pedido-A-1042" });
```

## Con el SDK de Python

```python
from fiscalisto import Fiscalisto

# La llave viene de FISCALISTO_API_KEY.
fiscalisto = Fiscalisto()

self_invoicing = fiscalisto.issuers.update_self_invoicing("id", {"window": "month", "global_periodicity": "04"}, idempotency_key="pedido-A-1042")
```

## Con el SDK de PHP

```php
<?php

use Fiscalisto\Fiscalisto;

// La llave viene de FISCALISTO_API_KEY.
$fiscalisto = new Fiscalisto();

$selfInvoicing = $fiscalisto->issuers->updateSelfInvoicing('id', [
    'window' => 'month',
    'global_periodicity' => '04',
], ['idempotency_key' => 'pedido-A-1042']);
```

## Con el SDK de .NET

```csharp
using Fiscalisto;

// La llave viene de FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var selfInvoicing = await fiscalisto.Issuers.UpdateSelfInvoicingAsync("id", new UpdateSelfInvoicingParams
{
    Window = "month",
    GlobalPeriodicity = "04",
}, new RequestOptions { IdempotencyKey = "pedido-A-1042" });
```

## Con el SDK de Go

```go
package main

import (
	"context"
	"fmt"
	"log"

	fiscalisto "fiscalisto.com/go"
)

func main() {
	// La llave viene de FISCALISTO_API_KEY.
	client, err := fiscalisto.NewClient("")
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	selfInvoicing, err := client.Issuers.UpdateSelfInvoicing(ctx, "id", &fiscalisto.UpdateSelfInvoicingParams{
		Window:            fiscalisto.String("month"),
		GlobalPeriodicity: fiscalisto.String("04"),
	}, fiscalisto.WithIdempotencyKey("pedido-A-1042"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", *selfInvoicing)
}
```

## Con el SDK de Java

```java
import com.fiscalisto.FiscalistoClient;
import com.fiscalisto.RequestOptions;
import com.fiscalisto.model.UpdateSelfInvoicingParams;

// La llave viene de FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var selfInvoicing = fiscalisto.issuers().updateSelfInvoicing("id", new UpdateSelfInvoicingParams()
        .setWindow("month")
        .setGlobalPeriodicity("04"), RequestOptions.idempotencyKey("pedido-A-1042"));
System.out.println(selfInvoicing);
```

## Respuesta 200

Correcto.

Un objeto `SelfInvoicing`:

| Campo | Tipo | Siempre viene | Descripción |
| --- | --- | --- | --- |
| `object` | `"self_invoicing"` | sí | Qué tipo de objeto es («invoice», «customer»…). |
| `livemode` | boolean | sí | true en producción (llaves fk_live_), false en pruebas (fk_test_). Lo de pruebas no vale ante el SAT. |
| `issuer` | string | sí | El emisor. |
| `portal_url` | string | sí | El portal permanente del emisor: ahí cualquier comprador factura con su código y su total. |
| `window` | `month`, `days` | sí | month: se factura hasta el fin del mes de la venta. days: hasta window_days días después. |
| `window_days` | integer | sí | Días para facturar con window: days. (de -9007199254740991 a 9007199254740991) |
| `global_periodicity` | `01`, `02`, `03`, `04` | sí | Cada cuánto se timbra sola la factura global con lo que nadie facturó: 01 diaria, 02 semanal, 03 quincenal, 04 mensual. |

```json
{
  "object": "self_invoicing",
  "livemode": false,
  "issuer": "iss_OufpKqnrvx9p",
  "portal_url": "https://fiscalisto.com/autofactura/Tx8mQ2vLp9WnR4c",
  "window": "month",
  "window_days": 7,
  "global_periodicity": "04"
}
```

## Errores

Puede responder 400, 401, 403, 404, 429, 500 con el cuerpo de error de siempre; cada código está en https://fiscalisto.com/docs/api/errores.md.
