Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtc: Update AEC XML document validations #199

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

yaselc
Copy link

@yaselc yaselc commented Mar 6, 2021

Add new validators to rtc.data_models described in the document "Instructivo Técnico Registro Público Electrónico de Transferencia de Crédito" (retrieved on 2021-03-03)
Source: Instructivo Técnico Registro Público Electrónico de Transferencia de Crédito

  • rtc.data_models_aec: The cedente_rut should be the DTE's emisor_rut or the last cesionario_rut when the sequence is greater than 1
  • rtc.data_models: The fecha_cesion_dt should be before or equal to the current day
  • rtc.data_models: The fecha_ultimo_vencimiento should be after or equal to the fecha_emision of the DTE


tz_utils.validate_dt_tz(value, tz)

current_date_in_tz = tz_utils.get_now_tz_aware().astimezone(tz)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK it is not necessary to .astimezone(tz) when comparing TZ-aware datetime values, because the "real" underlying datetime is not affected by the TZ used for representation. Am I right @jtrh ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was wrong (see my latest review)

@glarrain
Copy link
Contributor

@yaselc please update PR regarding the referenced documentation

@yaselc yaselc force-pushed the feature/update-aec-xml-document-validations branch from 490fc71 to 7c6d1ea Compare March 15, 2021 12:51
if isinstance(v, datetime):
tz_utils.validate_dt_tz(v, cls.DATETIME_FIELDS_TZ)
validate_cesion_fecha(v, cls.DATETIME_FIELDS_TZ)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent naming

Copy link
Author

@yaselc yaselc Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


now_tz_aware = tz_utils.get_now_tz_aware()

if not (value.date() <= now_tz_aware.date()):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not correct because the output of now_tz_aware.date() depends on the TZ of now_tz_aware, which has not been set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Let's bring back current_date_in_tz = tz_utils.get_now_tz_aware().astimezone(tz).

if isinstance(v, datetime):
tz_utils.validate_dt_tz(v, cls.DATETIME_FIELDS_TZ)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to nesting tz_utils.validate_dt_tz() into validate_cesion_fecha()?


if not (value.date() <= now_tz_aware.date()):
raise ValueError(
'Value of "fecha_cesion_dt" must be before or equal to the current day.', value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of "current day" depends on the timezone

cesion_value: date, dte_value: date
) -> None:
"""
Validate 'fecha_ultimo_vencimiento' of the "cesión" is after or equal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any documentation for this rule?

@@ -321,6 +351,9 @@ class CesionL0:
- Same timestamp as the "Registro AoR DTE" event ``DTE Cedido``.
- The above statements were empirically verified for
``CesionNaturalKey(dte_key=DteNaturalKey(Rut('99***140-4'), 33, 3105), seq=2)``.
- When receiving an XML AEC document, the SII validates this date is before or
equal to the current day.
Source: (https://www.sii.cl/factura_electronica/ins_tecnico.pdf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference a permalink please, and indicate exactly where in that document the rule is (and perhaps even quote some text, if possible)

@glarrain glarrain added enhancement New feature or request and removed feature labels Mar 23, 2021
@glarrain
Copy link
Contributor

Also, please update the PR description and all the commits messages so we have stable references (SII's hosted static files can not be trusted)

@glarrain
Copy link
Contributor

glarrain commented Mar 23, 2021

Also, since this PR involves a critical part of validation, please run it against many many many real cases (you will need to code something for that I suppose).

I'd also like to know the difference (timedelta) between fecha_firma_dt and fecha_cesion_dt for all those cases. I've got a feeling that difference must be very little (for valid "cesiones" I mean).

What if we add a warning (for more than X seconds of difference) instead of the "validation" that we are removing?

@jtrh your thoughts on this please

@jtrh
Copy link
Contributor

jtrh commented Mar 24, 2021

I'd also like to know the difference (timedelta) between fecha_firma_dt and fecha_cesion_dt for all those cases. I've got a feeling that difference must be very little (for valid "cesiones" I mean).

What if we add a warning (for more than X seconds of difference) instead of the "validation" that we are removing?

@jtrh your thoughts on this please

I would keep the validation of fecha_firma_dt and fecha_cesion_dt, but change it from an equality comparison to the following condition: the absolute value of the difference between the two datetimes must be ≤ 60 seconds.


if not (value.date() <= now_tz_aware.date()):
raise ValueError(
'Value of "fecha_cesion_dt" must be before or equal to the current day.', value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the value of fecha_cesion_dt (value), I would include the value of current day in the exception message so that it's clear to the user which date was used for the comparison, which is especially useful when debugging time zone--related issues.

@jtrh
Copy link
Contributor

jtrh commented Mar 24, 2021

I would keep the validation of fecha_firma_dt and fecha_cesion_dt, but change it from an equality comparison to the following condition: the absolute value of the difference between the two datetimes must be ≤ 60 seconds.

Also, I would do the above change in a separate pull request. The other validations are nice-to-haves, but I think we should prioritize this one so we can speed up the fix of https://github.com/fyntex/fd-cl-data/issues/671.

@yaselc yaselc force-pushed the feature/update-aec-xml-document-validations branch from 7c6d1ea to e0b1809 Compare March 25, 2021 15:23
@glarrain
Copy link
Contributor

LGTM. @jtrh your call.

@yaselc
Copy link
Author

yaselc commented Mar 31, 2021

I would keep the validation of fecha_firma_dt and fecha_cesion_dt, but change it from an equality comparison to the following condition: the absolute value of the difference between the two datetimes must be ≤ 60 seconds.

Also, I would do the above change in a separate pull request. The other validations are nice-to-haves, but I think we should prioritize this one so we can speed up the fix of fyntex/fd-cl-data#671.

@jtrh I moved this specific validation to PR #208, with an analysis of a representative set of AEC, because I still have doubts about it.

@yaselc yaselc force-pushed the feature/update-aec-xml-document-validations branch 3 times, most recently from f132946 to 656b66e Compare April 8, 2021 04:59
@jtrh
Copy link
Contributor

jtrh commented May 5, 2021

CC: @jtrobles-cdd

@jtrobles-cdd
Copy link
Member

Is this PR ready for a new review?

The 'cedente_rut' should be the DTE's 'emisor_rut' or the
last 'cesionario_rut' when the sequence is greater than 2
Condition sourced from document "Instructivo Técnico
Registro Público Electrónico de Transferencia de Crédito"
Source: (https://github.com/cl-sii-extraoficial/archivos-oficiales/blob/master/src/docs/rtc/2013-02-11-instructivo-tecnico.pdf)
@ycouce-cdd ycouce-cdd force-pushed the feature/update-aec-xml-document-validations branch from 656b66e to b1ca3c7 Compare May 14, 2021 01:29
@codecov-commenter
Copy link

codecov-commenter commented May 14, 2021

Codecov Report

Merging #199 (b1ca3c7) into develop (b97bb11) will increase coverage by 0.13%.
The diff coverage is 91.48%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #199      +/-   ##
===========================================
+ Coverage    81.02%   81.16%   +0.13%     
===========================================
  Files           32       32              
  Lines         2525     2565      +40     
  Branches       375      384       +9     
===========================================
+ Hits          2046     2082      +36     
+ Misses         306      305       -1     
- Partials       173      178       +5     
Impacted Files Coverage Δ
cl_sii/rtc/data_models.py 91.03% <90.62%> (-0.32%) ⬇️
cl_sii/rtc/data_models_aec.py 90.09% <93.33%> (-0.36%) ⬇️
cl_sii/libs/tz_utils.py 67.64% <0.00%> (+2.94%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b97bb11...b1ca3c7. Read the comment docs.

@ycouce-cdd
Copy link
Member

Is this PR ready for a new review?

It's now... thanks

@yaselc yaselc assigned ycouce-cdd and unassigned yaselc May 17, 2021
Copy link
Member

@jtrobles-cdd jtrobles-cdd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking into account what @ycouce-cdd and me discussed yesterday (2021-05-24), are there any validations that should be removed because they conflict with the SII's de facto rules?

@jtrobles-cdd jtrobles-cdd added bug Something isn't working and removed bugfix labels Feb 3, 2022
@ycouce-cdd
Copy link
Member

Taking into account what @ycouce-cdd and me discussed yesterday (2021-05-24), are there any validations that should be removed because they conflict with the SII's de facto rules?

It took me a long time to answer, sorry for that, but practice showed us that one thing is what the rules say and another is how they are applied. Obviously, it is not wise to implement validations that probably the SII's own system doesn't apply, it would only make sense if we wanted to generate our own AECs, but I would not use it to restrict the ones we accept.
The smartest thing to do for now is to keep this PR on hold.

@jtrobles-cdd jtrobles-cdd marked this pull request as draft September 28, 2022 22:57
@reviewpad reviewpad bot mentioned this pull request Mar 30, 2023
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working component: rtc enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants