@@ -40,6 +40,12 @@ class AccountMove(models.Model):
4040 string = "CAE/CAI/CAEA due Date" ,
4141 states = {"draft" : [("readonly" , False )]},
4242 )
43+ afip_associated_period_from = fields .Date (
44+ 'AFIP Period from'
45+ )
46+ afip_associated_period_to = fields .Date (
47+ 'AFIP Period to'
48+ )
4349 afip_qr_code = fields .Char (compute = "_compute_qr_code" , string = "AFIP QR code" )
4450 afip_message = fields .Text (
4551 string = "AFIP Message" ,
@@ -72,6 +78,44 @@ class AccountMove(models.Model):
7278 "- NO: sí el comprobante asociado (original) NO se encuentra rechazado por el comprador" ,
7379 )
7480
81+ def _get_starting_sequence (self ):
82+ """ If use documents then will create a new starting sequence using the document type code prefix and the
83+ journal document number with a 8 padding number """
84+ if self .journal_id .l10n_latam_use_documents and self .company_id .account_fiscal_country_id .code == "AR" and self .journal_id .afip_ws :
85+ if self .l10n_latam_document_type_id :
86+ number = int (
87+ self .journal_id .get_pyafipws_last_invoice (
88+ self .l10n_latam_document_type_id
89+ )
90+ )
91+ return self ._get_formatted_sequence (number )
92+ return super ()._get_starting_sequence ()
93+
94+ def _set_next_sequence (self ):
95+ self .ensure_one ()
96+ if self ._name == 'account.move' and self .journal_id .l10n_latam_use_documents and self .company_id .account_fiscal_country_id .code == "AR" and self .journal_id .afip_ws :
97+
98+ last_sequence = self ._get_last_sequence ()
99+ new = not last_sequence
100+ if new :
101+ last_sequence = self ._get_last_sequence (relaxed = True ) or self ._get_starting_sequence ()
102+
103+ format , format_values = self ._get_sequence_format_param (last_sequence )
104+ if new :
105+ format_values ['seq' ] = int (
106+ self .journal_id .get_pyafipws_last_invoice (
107+ self .l10n_latam_document_type_id
108+ )
109+ )
110+ format_values ['year' ] = self [self ._sequence_date_field ].year % (10 ** format_values ['year_length' ])
111+ format_values ['month' ] = self [self ._sequence_date_field ].month
112+ format_values ['seq' ] = format_values ['seq' ] + 1
113+
114+ self [self ._sequence_field ] = format .format (** format_values )
115+ self ._compute_split_sequence ()
116+ else :
117+ super ()._set_next_sequence ()
118+
75119 @api .depends ("journal_id" , "afip_auth_code" )
76120 def _compute_validation_type (self ):
77121 for rec in self :
@@ -184,6 +228,13 @@ def do_pyafipws_request_cae(self):
184228
185229 # Preparo los datos
186230 invoice_info = inv .map_invoice_info (afip_ws )
231+ number_parts = self ._l10n_ar_get_document_number_parts (
232+ inv .l10n_latam_document_number , inv .l10n_latam_document_type_id .code
233+ )
234+
235+ if int (invoice_info ["ws_next_invoice_number" ]) != int (number_parts ["invoice_number" ]):
236+
237+ raise UserError (_ ('Check document number. Next is %s' % invoice_info ["ws_next_invoice_number" ]))
187238
188239 # Creo la factura en el ambito de pyafipws
189240 inv .pyafipws_create_invoice (ws , invoice_info )
@@ -230,17 +281,17 @@ def do_pyafipws_request_cae(self):
230281 "CAE solicitado con exito. CAE: %s. Resultado %s"
231282 % (ws .CAE , ws .Resultado )
232283 )
233- inv .write (
234- {
284+ vals = {
235285 "afip_auth_mode" : "CAE" ,
236286 "afip_auth_code" : ws .CAE ,
237287 "afip_auth_code_due" : vto ,
238288 "afip_result" : ws .Resultado ,
239289 "afip_message" : msg ,
240290 "afip_xml_request" : ws .XmlRequest ,
241291 "afip_xml_response" : ws .XmlResponse ,
242- }
243- )
292+ }
293+
294+ inv .write (vals )
244295 # si obtuvimos el cae hacemos el commit porque estoya no se puede
245296 # volver atras
246297 # otra alternativa seria escribir con otro cursor el cae y que
0 commit comments