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.

Note that deleting or altering these models will not affect upstream point of sales.

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.

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:
  • point_of_sales_id (ForeignKey to PointOfSales) – Point of sales
  • receipt_type_id (ForeignKey to ReceiptType) – Receipt type
  • 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) – If left blank, the next valid number will assigned when validating the receipt.
  • issued_date (DateField) – Can diverge up to 5 days for good, or 10 days otherwise
  • total_amount (DecimalField) – Must be equal to the sum of net_taxed, exempt_amount, net_taxes, and all taxes and vats.
  • 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
  • related_receipts (ManyToManyField) – Related receipts
is_validated

Returns True if this instance is validated.

Note that resolving this property requires a DB query, so if you’ve a very large amount of receipts you should prefetch (see django’s select_related) the validation field. Even so, a DB query may be triggered.

If you need a large list of validated receipts, you should actually filter them via a QuerySet:

Receipt.objects.filter(validation__result==RESULT_APPROVED)
Return type:bool
total_tax

Returns the sum of all Tax objects.

total_vat

Returns the sum of all Vat objects.

validate(ticket=None, raise_=False)

Validates this receipt.

This is a shortcut to ReceiptQuerySet‘s method of the same name. Calling this validates only this instance.

Parameters:
  • ticket (AuthTicket) – Use this ticket. If None, one will be loaded or created automatically.
  • raise (bool) – If True, an exception will be raised when validation fails.
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:
  • result (CharField) – Indicates whether the validation was succesful or not
  • processed_date (DateTimeField) – Processed date
  • 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
  • certificate_expiration (DateTimeField) – Stores expiration for the current certificate. Note that this field is updated pre-save, so the value may be invalid for unsaved models.
  • active_since (DateField) – Date since which this taxpayer has been legally active.
certificate_object

Returns the certificate as an OpenSSL object

Returns the certificate as an OpenSSL object (rather than as a file object).

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,).

generate_csr(basename='djangoafip')

Creates a CSR for this TaxPayer’s key

Creates a file-like object that contains the CSR which can be used to request a new certificate from AFIP.

generate_key(force=False)

Creates a key file for this TaxPayer

Creates a key file for this TaxPayer if it does not have one, and immediately saves it.

Returns True if and only if a key was created.

get_certificate_expiration()

Gets the certificate expiration from the certificate

Gets the certificate expiration from the certificate file. Note that this value is stored into certificate_expiration when an instance is saved, so you should generally prefer that method (since this one requires reading and parsing the entire certificate).

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.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

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.

django_afip.models.populate_all()

Fetch and store all metadata from the AFIP.

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

Managers

Managers should be accessed via models. For example, ReceiptManager should be accessed using Receipt.objects.

class django_afip.models.ReceiptManager

The default manager for the Receipt class.

You should generally access this using Receipt.objects.

fetch_last_receipt_number(point_of_sales, receipt_type)

Returns the number for the last validated receipt.

class django_afip.models.ReceiptPDFManager
create_for_receipt(receipt, **kwargs)

Creates a ReceiptPDF object for a given receipt. Does not actually generate the related PDF file.

All attributes will be completed with the information for the relevant TaxPayerProfile instance.

Parameters:receipt (Receipt) – The receipt for the PDF which will be generated.

QuerySets

QuerySets are generally accessed via their models. For example, Receipt.objects.filter() will return a ReceiptQuerySet.

class django_afip.models.ReceiptQuerySet(model=None, query=None, using=None, hints=None)

The default queryset obtains when querying via ReceiptManager.

check_groupable()

Checks that all receipts returned by this queryset are groupable.

“Groupable” means that they can be validated together: they have the same POS and receipt type.

Returns the same queryset is all receipts are groupable, otherwise, raises CannotValidateTogether.

validate(ticket=None)

Validates all receipts matching this queryset.

Note that, due to how AFIP implements its numbering, this method is not thread-safe, or even multiprocess-safe.

Because of this, it is possible that not all instances matching this queryset are validated properly. Obviously, only successfully validated receipts will be updated.

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

Receipts that succesfully validate will have a ReceiptValidation object attatched to them with a validation date and CAE information.

Already-validated receipts are ignored.

Attempting to validate an empty queryset will simply return an empty list.