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

feat: new DocTypes "Code List" and "Common Code" #43425

Merged
merged 46 commits into from
Nov 17, 2024

Conversation

barredterra
Copy link
Collaborator

@barredterra barredterra commented Sep 29, 2024

code_list

common_code_list

common_code

In [1]: code_list = "urn:xoev-de:kosit:codeliste:untdid.5305_3"

In [2]: get_codes_for(code_list, "Account", "Tax Assets - WPL")
Out[2]: ('A',)

In [3]: get_docnames_for(code_list, "Account", "A")
Out[3]: ('Tax Assets - WPL',)

In [4]: get_default_code(code_list)
Out[4]: 'S'

Screenshots based on https://www.xrepository.de/api/xrepository/urn:xoev-de:kosit:codeliste:untdid.5305_3:technischerBestandteilGenericode:datei:UNTDID_5305-3.xml

After this is merged, check if we should revert #40593 and #40663

no-docs (for now)

@barredterra
Copy link
Collaborator Author

barredterra commented Sep 30, 2024

  • It is not obvious (and was not intended by me) that the CanonicalVersionUri now has to be used as the name of the code list (for the import to work). E.g. I would not expect the CanonicalVersionUri of the code list linked above to be referenced in most eInvoices. So a more generic name would be useful for retrieving the necessary code list. In the screenshot above, I used the Identification/ShortName.
  • "Additional Data" from the code list is not yet imported (e.g. descriptions, as available in the code list linked above).

@blaggacao
Copy link
Collaborator

More exploration from my side: barredterra#1 (Edi Template & Edi Log, squashed your naming fix
there, @barredterra )

@blaggacao
Copy link
Collaborator

blaggacao commented Sep 30, 2024

It is not obvious (and was not intended by me) that the CanonicalVersionUri now has to be used as the name of the code list (for the import to work). E.g. I would not expect the CanonicalVersionUri of the code list linked above to be referenced in most eInvoices.

Firstly, most of the time, however, unlike the common codes, these lists are akin to real constants in an e-invoice template. I would typically hard-code them for each distinct edi template. Here is an example:

XML Example
        <cbc:IdentificationCode
          listAgencyID="6"
          listAgencyName="United Nations Economic Commission for Europe"
          listSchemeURI="urn:oasis:names:specification:ubl:codelist:gc:CountryIdentificationCode-2.1"
        >CO</cbc:IdentificationCode>

Secondly, the title is still user friendly, for display purposes.

Screenshot

image

Thirdly, as per the Genericode XML Schema, CanonicalVersionUri is the primary key of a list, so we would have to go out of our way to work around that. https://docs.oasis-open.org/codelist/genericode/v1.0/os/xsd/genericode.xsd

XML Reference
<xsd:complexType name="Identification">
<xsd:annotation>
<xsd:documentation>Identification and location information (metadata).</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:group ref="gc:NameSet">
...
</xsd:group>
<xsd:element name="Version" type="xsd:token">
...
</xsd:element>
<xsd:element name="CanonicalUri" type="xsd:anyURI">
<xsd:annotation>
<xsd:documentation>Canonical URI which uniquely identifies all versions (collectively).</xsd:documentation>
<xsd:documentation>
<rule:text id="R25" category="document">Must be an absolute URI, must not be relative.</rule:text>
</xsd:documentation>
<xsd:documentation>
<rule:text id="R26" category="application">Must not be used as a de facto location URI.</rule:text>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:group ref="gc:VersionLocationUriSet">
...
</xsd:group>
<xsd:element name="Agency" minOccurs="0" type="gc:Agency">
...
</xsd:element>
</xsd:sequence>
</xsd:complexType>

...

<xsd:group name="VersionLocationUriSet">
<xsd:annotation>
<xsd:documentation>Identification and location URIs for a version.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="CanonicalVersionUri" type="xsd:anyURI">
...
</xsd:element>
<xsd:element name="LocationUri" minOccurs="0" maxOccurs="unbounded" type="xsd:anyURI">
...
</xsd:element>
<xsd:element name="AlternateFormatLocationUri" minOccurs="0" maxOccurs="unbounded" type="gc:MimeTypedUri">
...
</xsd:element>
</xsd:sequence>
</xsd:group>

Note, that CanonicalUri will be shared between lists, and within VerstionLocationUriSet (the unique identifying set), CanoncialVersionUri is the only mandatory one.

Edit:

I think this is what you where looking for:

image

blaggacao
blaggacao previously approved these changes Sep 30, 2024
Copy link
Collaborator

@blaggacao blaggacao left a comment

Choose a reason for hiding this comment

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

I have contributed the Genericode Importer which triggered a certain amount of polishing and I think this version is ready to merge, now.

We should keep in mind, that we're actively developing the EDI functionality for EU & Colombia in an iterative approach against develop - so more to come, soon.

@blaggacao blaggacao marked this pull request as ready for review September 30, 2024 16:36
@barredterra
Copy link
Collaborator Author

barredterra commented Sep 30, 2024

If we have to use this very technical name, we should at least offer the import from the list view (or from an unsaved form) instead of from the saved form view. It would be kind of weird if the user first needs to manually extract some values from the XML, in order to then import the rest of it.

My approach was originally targeted at, e.g., being able to tell the users to create a Code List called "UNTDID.5305" and make sure it contains the tax codes they care about. The alternative, equally good or even better, would be being able to tell them to just import this code list. Telling them to extract the CanonicalVersionUri does not seem realistic, however.

@blaggacao
Copy link
Collaborator

blaggacao commented Oct 1, 2024

[ ... ] we should at least offer the import from the list view (or from an unsaved form) instead of from the saved form view. It would be kind of weird if the user first needs to manually extract some values from the XML, in order to then import the rest of it.

Telling them to extract the CanonicalVersionUri does not seem realistic, however.

Indeed! — I think this concern is already gracefully implemented?

General Scenario: Add more codes from the same list (e.g. different filters) — form import

Specific Scenario: I accidentially selected the wrong code list for upload

image


General Scenario: Add a new code list and some codes — list import

Implemented on the list views of Code List and Common Code

Specific Scenario: Step Two (immediately after upload); Selection of Import filters

image
Note: no extraction of CanonicalVersionUri by the user; filterable are fields with cardinality <= 5

@blaggacao
Copy link
Collaborator

d540d03

image

@blaggacao
Copy link
Collaborator

We may need to relax the unique condition on code for Common Code. This is a valid Code List:

      <Row>
         <Value ColumnRef="code">
            <SimpleValue>1.50</SimpleValue>
         </Value>
         <Value ColumnRef="name">
            <SimpleValue>ReteFuente</SimpleValue>
         </Value>
         <Value ColumnRef="description">
            <SimpleValue>Compras con tarjeta débito o crédito</SimpleValue>
         </Value>
      </Row>
      <Row>
         <Value ColumnRef="code">
            <SimpleValue>1.50</SimpleValue>
         </Value>
         <Value ColumnRef="name">
            <SimpleValue>ReteFuente</SimpleValue>
         </Value>
         <Value ColumnRef="description">
            <SimpleValue>Compras de bienes o productos agrícolas o pecuarios sin procesamiento industrial</SimpleValue>
         </Value>
      </Row>

@blaggacao blaggacao force-pushed the code-lists branch 2 times, most recently from 4f3cc07 to b751b44 Compare October 1, 2024 11:19
@blaggacao
Copy link
Collaborator

Problem addressed, also in the importer:

image

image

@blaggacao
Copy link
Collaborator

52dcb2b addresses the usability concern even further:

image

@blaggacao
Copy link
Collaborator

blaggacao commented Oct 1, 2024

Testing Resume

  • 23 Code Lists, 27 XML files
  • Faulty XML: ok
  • Undeclared Columns: ok
  • Non unique common_code: ok (eg tax rates) -> added description
  • Guard against accidential structured data manipulation
  • Partial loading of a list
  • Later completion of a list - USER ATTENTION! Duplicates are legal in the data structure, double loading must be avoided with care (no other way)
  • Loading values from different files into the same code list
Screenshot

image

@barredterra
Copy link
Collaborator Author

barredterra commented Oct 1, 2024

Indeed! — I think this concern is already gracefully implemented?

Ah, my bad, I've overlooked this.


I think, from a developer perspective, it would be perfect if we can:

(1) Given a ref_doctype, ref_docname and code list, retrieve the applicable code.
(2) Given a code and code list, retrieve the applicable ref_doctype and ref_docname.

Do you think this unambiguous mapping is feasible?

I'm thinking of adding a util and validation for the second case.


Selecting "URL" on file upload fails. Would be nice if we can support this as well.


@blaggacao
Copy link
Collaborator

blaggacao commented Oct 1, 2024

(2) Given a code and code list, retrieve the applicable ref_doctype and ref_docname.

Given ...
	def validate_distinct_references(self):
		"""Ensure no two Common Codes of the same Code List are linked to the same document."""
		for link in self.applies_to:
			existing_links = frappe.get_all(
				"Common Code",
				filters=[
					["name", "!=", self.name],
					["code_list", "=", self.code_list],
					["Dynamic Link", "link_doctype", "=", link.link_doctype],
					["Dynamic Link", "link_name", "=", link.link_name],
				],
				fields=["name", "common_code"],
			)

			if existing_links:
				existing_link = existing_links[0]
				frappe.throw(
					_("{0} {1} is already linked to Common Code {2}.").format(
						link.link_doctype,
						link.link_name,
						get_link_to_form("Common Code", existing_link["name"], existing_link["common_code"]),
					)
				)

I think we can restrict certain Code Lists (optionally) to a certain doctype and thereby meet the full condition necessary to implement such a util for such code lists.

But you're absolutely right: on the parsing end of the flow, this would quite definitely improve DX.

@blaggacao
Copy link
Collaborator

blaggacao commented Oct 1, 2024

Selecting "URL" on file upload fails. Would be nice if we can support this as well.

94bd743

Also library links (for example to post-load more codes from an already partially imported list)

Depends on: frappe/frappe#27954

@blaggacao
Copy link
Collaborator

blaggacao commented Oct 1, 2024

We probably should also return additional_data from the get_code_for because more often than not, this is actually relevant data when rendering a e-invoice template. I'll check this and confirm during dogfooding.


Wile dogfooding I realise:

As user, I want an easy way to associate all Code List documents with my current document, which are configured to be associatable with that document type (by means of a Code List - Doctype association). Especially for documents which are still metadata, but fall within the life-cycle management of non-power users, such as Suppliers, Customers & Items.


We also should probably implement a stringer (__str__) on CommonCode which automatically renders it's common_code into a jinja template without need for more verbosity.


get_code_for should probably throw a user facing error if no association is found but was expected, indicating that xyz association is missing and needs to be set up.

    # should throw if tax_category_id was not set
    tax_category_id = frappe.get_doc("Code List", TAX_CATEGORY_CODE_LIST).get_code_for(
        party.doctype, party.name
    )

Code List should have a default code, so that it is easier to fetch:

    tax_id_type = frappe.get_doc("Common Code", party.tax_id_type or tax_type_codelist.default)

Would be handy too (.get_doc proxy):

    tax_id_type = codelist.get_doc(party.tax_id_type)  # raises
    tax_id_type = codelist.get_doc(party.tax_id_type, None)
    tax_id_type = codelist.get_doc(party.tax_id_type, codelist.default)

Ultimately, what I want is this, to keep my implementation at the leaf as clean as possible:

from .code_list import substitute


customer = frappe.get_doc("Customer", "Foo")

customer_for_render = substitute(customer)
# replaces all links to Common Code to the respective common code
# and infers defaults based on the field's filter
# {"code_list": "My List"} - take default of that list, if there is

If my edge case invalidates a default, I can still override it in sitiu

@blaggacao
Copy link
Collaborator

Rebase done in order to prepare internal production backport which is hard to extract with a merge commit from develop.

barredterra and others added 3 commits November 16, 2024 17:05
Description is the field that usually contains words that humans can make sense of. Show this to help select the correct list.
This way we can link multiple docs to one Common Code and sort them by preference.
Copy link
Collaborator

@blaggacao blaggacao left a comment

Choose a reason for hiding this comment

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

LGTM, I'm using this via branch barredterra#1 and it seems stable enough for this use case

Some of these notes might or might not serve for future evolution.

@barredterra barredterra merged commit 2a8dbc5 into frappe:develop Nov 17, 2024
18 checks passed
mergify bot pushed a commit that referenced this pull request Nov 17, 2024
Co-authored-by: David <[email protected]>
(cherry picked from commit 2a8dbc5)

# Conflicts:
#	erpnext/modules.txt
mergify bot pushed a commit that referenced this pull request Nov 17, 2024
@blaggacao
Copy link
Collaborator

@barredterra This would need backports of frappe/frappe#27954

@barredterra barredterra deleted the code-lists branch November 18, 2024 19:51
barredterra added a commit that referenced this pull request Nov 18, 2024
barredterra added a commit that referenced this pull request Nov 18, 2024
frappe-pr-bot pushed a commit that referenced this pull request Nov 20, 2024
# [14.77.0](v14.76.0...v14.77.0) (2024-11-20)

### Bug Fixes

* added test cases ([5fa4fd8](5fa4fd8))
* apply posting date sorting to invoices in Payment Reconciliation similar to payments ([cc9b22c](cc9b22c))
* backport german translations from develop ([#44046](#44046)) ([c23868a](c23868a))
* broken apply on other item pricing rule ([f03e301](f03e301))
* broken UI on currency exchange ([36898f6](36898f6))
* check if pricing rule matches with coupon code ([#44104](#44104)) ([24b5b3c](24b5b3c))
* correctly set 'cannot_add_rows' property on allocations table field ([137ef78](137ef78))
* disable conversion to user tz for sales order calender ([356da69](356da69))
* Get Entries not showing accounts with no gain or loss in Exchange Rate Revaluation issue ([33e835c](33e835c))
* linters ([c3e2ff2](c3e2ff2))
* non group pos warehouse ([ec40131](ec40131))
* payment reco for jv with negative dr or cr amount ([23fb4f3](23fb4f3))
* remove trailing whitespace ([1019d98](1019d98))
* set conversion factor before applying price list ([e09f101](e09f101))
* set default party type in Payment Entry ([f3cbbef](f3cbbef))
* stock ledger variance report filter options (backport [#44137](#44137)) ([#44149](#44149)) ([f871f08](f871f08))
* validate sales team to ensure all sales person are enabled ([bd0f11e](bd0f11e))

### Features

* new DocTypes "Code List" and "Common Code" (backport [#43425](#43425)) ([#44172](#44172)) ([a7de8c1](a7de8c1))
frappe-pr-bot pushed a commit that referenced this pull request Nov 20, 2024
# [15.43.0](v15.42.0...v15.43.0) (2024-11-20)

### Bug Fixes

* added disable_rounded_total field ([c98a0cc](c98a0cc))
* added test cases ([234741f](234741f))
* apply posting date sorting to invoices in Payment Reconciliation similar to payments ([41c8cfa](41c8cfa))
* broken apply on other item pricing rule ([5d6451f](5d6451f))
* broken UI on currency exchange ([f460391](f460391))
* bulk update invoice remarks during site upgrade ([cc07402](cc07402)), closes [#43634](#43634)
* check if pricing rule matches with coupon code ([#44104](#44104)) ([6089661](6089661))
* correctly set 'cannot_add_rows' property on allocations table field ([c59a778](c59a778))
* disable conversion to user tz for sales order calender ([83b9680](83b9680))
* Get Entries not showing accounts with no gain or loss in Exchange Rate Revaluation issue ([1fe5342](1fe5342))
* linters ([381101f](381101f))
* non group pos warehouse ([4335659](4335659))
* payment reco for jv with negative dr or cr amount ([7483839](7483839))
* remove trailing whitespace ([5bd633b](5bd633b))
* remove validate_name_in_customer function ([6bff9d3](6bff9d3))
* set conversion factor before applying price list ([5848de7](5848de7))
* set debit in transaction currency in GL Entry ([c0d3f8c](c0d3f8c))
* set default party type in Payment Entry ([08f6cee](08f6cee))
* **setup:** Fix typo in COA setup ([7abcfca](7abcfca))
* stock ledger variance report filter options (backport [#44137](#44137)) ([#44150](#44150)) ([b6fe1f5](b6fe1f5))
* update project cost from timesheet (backport [#44211](#44211)) ([#44212](#44212)) ([ad0c655](ad0c655))
* validate sales team to ensure all sales person are enabled ([f3c3f17](f3c3f17))
* validation for serial no (backport [#44133](#44133)) ([#44151](#44151)) ([725d107](725d107))

### Features

* inventory dimension for rejected materials (backport [#44156](#44156)) ([#44165](#44165)) ([d61f696](d61f696))
* new DocTypes "Code List" and "Common Code" (backport [#43425](#43425)) ([#44173](#44173)) ([b130e20](b130e20))
* round off for opening entries ([8e6249d](8e6249d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants