API

Core models

These are the code models which will normally be used for Receipt validation.

class django_afip.models.PointOfSales(*args, **kwargs)

Represents an existing AFIP point of sale.

Points of sales need to be created via AFIP’s web interface and it is recommended that you use fetch_points_of_sales() to fetch these programatically.

Parameters:
  • number (PositiveSmallIntegerField) – Number
  • issuance_type (CharField) – Indicates if thie POS emits using CAE and CAEA.
  • blocked (BooleanField) – Blocked
  • drop_date (DateField) – Drop date
  • owner_id (ForeignKey to TaxPayer) – Owner
class django_afip.models.Receipt(*args, **kwargs)

A receipt, as sent to AFIP.

Note that AFIP allows sending ranges of receipts, but this isn’t generally what you want, so we model invoices individually.

To validate a Receipt, you need to create a ReceiptBatch first.

You’ll probably want to relate some Sale or Order object from your model with each Receipt.

All document_ fields contain the recipient’s data.

If the taxpayer has taxes or pays VAT, you need to attach Tax and/or Vat instances to the Receipt.

Parameters:
  • batch_id (ForeignKey to ReceiptBatch) – Receipts are validated in batches, so it must be assigned one before validation is possible.
  • concept_id (ForeignKey to ConceptType) – Concept
  • document_type_id (ForeignKey to DocumentType) – The document type of the customer to whom this receipt is addressed
  • document_number (BigIntegerField) – The document number of the customer to whom this receipt is addressed
  • receipt_number (PositiveIntegerField) – Receipt number
  • issued_date (DateField) – Can diverge up to 5 days for good, or 10 days otherwise
  • total_amount (DecimalField) – Must be equal to untaxed amount + exempt amount + taxes + vat.
  • net_untaxed (DecimalField) – The total amount to which taxes do not apply. For C-type receipts, this must be zero.
  • net_taxed (DecimalField) – The total amount to which taxes apply. For C-type receipts, this is equal to the subtotal.
  • exempt_amount (DecimalField) – Only for categories which are tax-exempt. For C-type receipts, this must be zero.
  • service_start (DateField) – Date on which a service started. No applicable for goods.
  • service_end (DateField) – Date on which a service ended. No applicable for goods.
  • expiration_date (DateField) – Date on which this receipt expires. No applicable for goods.
  • currency_id (ForeignKey to CurrencyType) – Currency in which this receipt is issued.
  • currency_quote (DecimalField) – Quote of the day for the currency used in the receipt
  • receipt_type_id (ForeignKey to ReceiptType) – Receipt type
  • point_of_sales_id (ForeignKey to PointOfSales) – Point of sales
  • related_receipts (ManyToManyField) – Related receipts
class django_afip.models.ReceiptBatch(*args, **kwargs)

Receipts are validated in batches.

AFIP’s webservice validates receipts in batches, and this class models them. A batch of receipts is simply a set of consecutive-numbered Receipt instances, with the same ReceiptType and PointOfSales.

If you need to validate a single Receipt, it’s okay to create a batch with just one.

Parameters:
  • receipt_type_id (ForeignKey to ReceiptType) – Receipt type
  • point_of_sales_id (ForeignKey to PointOfSales) – Point of sales
validate(ticket=None)

Validates all receipts assigned to this batch.

Attempting to validate an empty batch will do nothing.

Any receipts that fail validation are removed from the batch, so you should never modify a batch after validation. Receipts that succesfully validae will have a ReceiptValidation object attatched to them with a validation date and CAE information.

Returns a list of errors as returned from AFIP’s webservices. An exception is not raised because partial failures are possible.

class django_afip.models.ReceiptValidation(*args, **kwargs)

The validation for a single Receipt.

This contains all validation-related data for a receipt, including its CAE and the CAE expiration, unless validation has failed.

The observation field may contain any data returned by AFIP regarding validation failure.

Parameters:
  • validation_id (ForeignKey to Validation) – The validation for the batch that produced this instance.
  • result (CharField) – Indicates whether the validation was succesful or not
  • cae (CharField) – The CAE as returned by the AFIP
  • cae_expiration (DateField) – The CAE expiration as returned by the AFIP
  • receipt_id (OneToOneField to Receipt) – The Receipt for which this validation applies
  • observations (ManyToManyField) – The observations as returned by the AFIP. These are generally present for failed validations.
class django_afip.models.Tax(*args, **kwargs)

A tax (type+amount) for a specific Receipt.

Parameters:
  • tax_type_id (ForeignKey to TaxType) – Tax type
  • description (CharField) – Description
  • base_amount (DecimalField) – Base amount
  • aliquot (DecimalField) – Aliquot
  • amount (DecimalField) – Amount
  • receipt_id (ForeignKey to Receipt) – Receipt
compute_amount()

Auto-assign and return the total amount for this tax.

class django_afip.models.TaxPayer(*args, **kwargs)

Represents an AFIP TaxPayer.

This class has the bare minimum required for most AFIP services.

Note that multiple instances of this object can actually represent the same taxpayer, each using a different key.

Parameters:
  • name (CharField) – A friendly name to recognize this taxpayer.
  • key (FileField) – Key
  • certificate (FileField) – Certificate
  • cuit (BigIntegerField) – Cuit
  • is_sandboxed (BooleanField) – Indicates if this taxpayer interacts with the sandbox servers rather than the production servers
create_ticket(service)

Create an AuthTicket for a given service.

fetch_points_of_sales(ticket=None)

Fetch all point of sales objects.

Fetch all point of sales from the WS and store (or update) them locally.

Returns a list of tuples with the format (pos, created,).

get_or_create_ticket(service)

Return or create a new AuthTicket for a given serivce.

Return an existing ticket for a service if one is available, otherwise, create a new one and return that.

This is generally the preferred method of obtaining tickets for any service.

get_ticket(service)

Return an existing AuthTicket for a given service.

class django_afip.models.Validation(*args, **kwargs)

The validation result for a batch.

The validation result for an attempt to validate a batch. Note that each Receipt inside the batch will have its own ReceiptValidation.

Parameters:
  • processed_date (DateTimeField) – Processed date
  • result (CharField) – Result
  • batch_id (ForeignKey to ReceiptBatch) – Receipt batch
class django_afip.models.Vat(*args, **kwargs)

A VAT (type+amount) for a specific Receipt.

Parameters:
  • vat_type_id (ForeignKey to VatType) – Vat type
  • base_amount (DecimalField) – Base amount
  • amount (DecimalField) – Amount
  • receipt_id (ForeignKey to Receipt) – Receipt

Managers

Managers should be accessed via models. For example, ReceiptBatchManager should be accessed using ReceiptBatch.objects.

class django_afip.models.ReceiptBatchManager
create(queryset)

Creates a batch with all receipts returned by queryset.

If you need to create a ReceiptBatch with a single receipt, just pass a query that returns that one:

Receipt.objects.filter(pk=pk)

Metadata models

These models represent metadata like currency types or document types. Their tables should be populated from AFIP’s webservices, using the afipmetadata command.

class django_afip.models.ConceptType(*args, **kwargs)

An AFIP concept type.

See the AFIP’s documentation for details on each concept type.

Parameters:
  • code (CharField) – Code
  • description (CharField) – Description
  • valid_from (DateField) – Valid from
  • valid_to (DateField) – Valid until
class django_afip.models.CurrencyType(*args, **kwargs)

An AFIP curreny type.

See the AFIP’s documentation for details on each currency type.

Parameters:
  • code (CharField) – Code
  • description (CharField) – Description
  • valid_from (DateField) – Valid from
  • valid_to (DateField) – Valid until
class django_afip.models.DocumentType(*args, **kwargs)

An AFIP document type.

See the AFIP’s documentation for details on each document type.

Parameters:
  • code (CharField) – Code
  • description (CharField) – Description
  • valid_from (DateField) – Valid from
  • valid_to (DateField) – Valid until
class django_afip.models.Observation(*args, **kwargs)

An observation returned by AFIP.

AFIP seems to assign re-used codes to Observation, so we actually store them as separate objects, and link to them from failed validations.

Parameters:
  • code (PositiveSmallIntegerField) – Code
  • message (CharField) – Message
class django_afip.models.ReceiptType(*args, **kwargs)

An AFIP receipt type.

See the AFIP’s documentation for details on each receipt type.

Parameters:
  • code (CharField) – Code
  • description (CharField) – Description
  • valid_from (DateField) – Valid from
  • valid_to (DateField) – Valid until
class django_afip.models.TaxType(*args, **kwargs)

An AFIP tax type.

See the AFIP’s documentation for details on each tax type.

Parameters:
  • code (CharField) – Code
  • description (CharField) – Description
  • valid_from (DateField) – Valid from
  • valid_to (DateField) – Valid until
class django_afip.models.VatType(*args, **kwargs)

An AFIP VAT type.

See the AFIP’s documentation for details on each VAT type.

Parameters:
  • code (CharField) – Code
  • description (CharField) – Description
  • valid_from (DateField) – Valid from
  • valid_to (DateField) – Valid until