Purchase LlamaIndex Import
On this page
Tells people about a purchase order that llamaindex_document_import created from a
vendor's order confirmation, and answers the shop mailbox that forwarded the vendor's
email. Also provides the one shared way to post notices about such an order
(purchase.order._edi_post_notice()), reused by other modules such asrepair_purchase_order_match.
It replaces edi_purchase_order_import, which did the same for orders thatimport_from_email created. The method names are kept, so the modules that extend them
only change a dependency. Until the cut-over, edi_purchase_order_import depends on
this module and adds only what is specific to exchange records (see its README).
What it adds
purchase.order._llamaindex_imported(document), the hookllamaindex_document_importcalls on the record it created:an order the import confirmed (the rule's confirm method,
button_confirm, runs when nothing needs review) gets_edi_confirm_and_notify(): the confirmation notice, outstanding, with email;an order left as a request for quotation (a fuzzy or auto-created match, a confirm that failed) gets a notice that it needs review, linking the document, not outstanding, with email.
purchase.order._edi_confirm_and_notify()confirms the order if it is still a draft, then posts the confirmation notice.repair_purchase_order_matchextends it to match the order to a repair order.**
purchase.order.\_edi_post_notice(body, email=True, outstanding=True, **message_post_kwargs)** posts a notice (see below).purchase.order._edi_clear_notices()marks the inbox notifications of the outstanding notices of the order as read, for everybody.purchase.order._edi_notice_still_needed(), an overridable hook (defaultTrue).product.product._llamaindex_find(): a line's product is found by the vendor's product code (product.supplierinfo.product_code, ignoring case) when its exact name and identifier fields find nothing. When the document names a vendor that exists (its exact name), only that vendor's codes count. One product found is trusted, so the order can still be confirmed; more than one is left for review.purchase.order.llamaindex_imported, Imported: whether a document import created the order. Not stored, and searchable.purchase.order._llamaindex_document(), the document that created the order, and the helpers_edi_reply_addresses(),_edi_inbound_reply_to(),_edi_originating_email()and_edi_notify_partners()described below.mail.message.subtypepurchase_llamaindex_import.mt_edi_notice, an internal subtype nobody follows, andmail.message.edi_notice_outstanding, the clearable marker.Settings > Purchase: Notify Group on Imported Purchase Orders(
purchase_llamaindex_import.notify_group_id) and Allowed Reply Addresses(purchase_llamaindex_import.reply_allowed). Installing the module copies the values ofedi_purchase_order_import's parameters, when they are set.
Notices
_edi_post_notice() does three things, and nothing else:
An internal note on the purchase order, with the
mt_edi_noticesubtype. Nobody is subscribed to that subtype (it is not a default one and is hidden from the follower options), and it is internal, so neither the vendor, a portal follower nor any other follower is notified by it. The author is Odoo (base.partner_root) unlessauthor_idis given, and the inbox notification is titled with the order's reference,_edi_reference()(P00204 – NAPA: number and vendor), unlessrecord_nameis given.An inbox notification (
needaction) for the notify group (default Purchase: Administrator). Never an email: the notice is posted with theedi_notice_mode="inbox"notification parameter, and_notify_get_recipients()forces every internal user to theinboxnotification type, whatever their preference, and drops anyone else (partners without a user, portal users).At most one email (
email=True), sent only to the reply addresses of the order, as a reply to the inbound email:Subject:Re: <inbound subject>(an existingRe:is not doubled);In-Reply-To: the inbound email'sMessage-Id;References: the inboundMessage-Idfirst, then what the order's own thread contributes, then the note itself;Message-Id: the note's own;links to records in the body are made absolute (
/odoo/<model>/<id>).
It is built by hand (a
mail.maillinked to the note) rather than through the follower-based notification, so nobody else can receive it. Without reply addresses nothing is emailed. Honorsmail_notify_force_send=Falselike Odoo's own notifications.
The import runs in a queue job once the mail gateway has posted the inbound email on the
document, so a notice always has a higher message id than that email. The gateway routes
a reply to the message with the highest id among its References, so the shop's reply
lands on the purchase order.
Reply addresses
llamaindex_document_import stores on the document the addresses a reply should go to
(reply_to: the shop mailbox that forwarded the vendor's email, or the person who
forwarded it by hand). _edi_reply_addresses() returns them, minus
every address of the order's vendor (its commercial partner and their contacts);
every address that is not allowed (
_edi_reply_address_allowed()). The stored addresses come from headers anybody who can write to the import address controls, so only these are emailed: the emails of internal users and of companies of the database, and what Allowed Reply Addresses lists: addresses (orders@shop.example) and domains (@shop.example), separated by commas, spaces or new lines. Set it to the shop mailbox the forwarding rule belongs to, or no notice is emailed (the inbox notification still is).
An order without a document, or whose document was uploaded or printed, has none.
Replies to the notice
The gateway posts the shop's answer on the order as a discussion message, which the
vendor (a follower) would be emailed. message_post() therefore turns an email reply to
a notice into an internal note (mail.mt_note).
Outstanding notices
A notice posted with outstanding=True (the default) is flagged
(mail.message.edi_notice_outstanding) as one that stops being relevant once the order
has been taken care of. _edi_clear_notices() then marks its inbox notifications as
read for all users and sends the usual mail.message/mark_as_read bus message, with
the new inbox counter, to each affected user. It is idempotent and quiet, and works
whoever calls it. The notices it clears are those of _edi_outstanding_notices(), which
a module extends when it also posts notices about an order elsewhere.
_edi_notice_still_needed() (default True) is consulted just before posting an
outstanding notice. When it answers False the note is still posted, and still emailed
if asked, but nobody gets an inbox notification for it and it is not flagged.
Access rights
The code runs in the import's queue job, as whoever runs it, who may be a low-privilege
user. sudo() is used to read the document, the notify group and its members, the
vendor's addresses, the emails of users and companies and the allowed reply addresses
(only ids and email addresses come out of it), to post the note and create its inbox
notifications and mail.mail, and in _edi_clear_notices() to write is_read on the
inbox notifications of the outstanding notices of the very orders it is called on. These
are private methods, and _edi_post_notice() returns the message in the caller's
environment.
Configuration
A rule on purchase.order in Document Import > Configuration > Rules, withbutton_confirm as its confirm method, and the two settings above.
Tests
tests/test_purchase_import.py feeds raw emails (Gmail forwarding rule headers
included) through the mail gateway to the import address, then runs the import with
LlamaCloud mocked. It checks the single email to the shop mailbox only, its threading
headers, the manual-forward fallback, no reply address, inbox-only notification, the
review notice of an order left as a request for quotation, routing of the shop's reply
to the order, _edi_clear_notices(), the _edi_notice_still_needed() hook, running as
a low-privilege user, the allowed reply addresses, absolute links, the shop's answer
staying an internal note, an email in the thread of an earlier one, the flag being the
system's, and the settings kept from edi_purchase_order_import.