diff --git a/modules/automation/automation.py b/modules/automation/automation.py index c3d2d48..8958b7a 100644 --- a/modules/automation/automation.py +++ b/modules/automation/automation.py @@ -22,6 +22,7 @@ class AutomationDocument(ModelSQL, ModelView, Workflow): ('invoice', 'Invoice'), ('statement_of_facts', 'Statement of Facts'), ('weight_report', 'Weight Report'), + ('controller', 'Controller'), ('bol', 'Bill of Lading'), ('controller_invoice', 'Controller Invoice'), ], 'Type') diff --git a/modules/document_incoming/inbound_email.py b/modules/document_incoming/inbound_email.py index 682facd..9c96175 100755 --- a/modules/document_incoming/inbound_email.py +++ b/modules/document_incoming/inbound_email.py @@ -46,11 +46,51 @@ class Rule(metaclass=PoolMeta): DocumentIncoming = pool.get('document.incoming') return DocumentIncoming.fields_get(['type'])['type']['selection'] + # def run(self, email_): + # pool = Pool() + # DocumentIncoming = pool.get('document.incoming') + # super().run(email_) + # if (self.action == 'document.incoming|from_inbound_email' + # and self.document_incoming_process): + # document = email_.result + # DocumentIncoming.process([document], with_children=True) + def run(self, email_): pool = Pool() DocumentIncoming = pool.get('document.incoming') super().run(email_) - if (self.action == 'document.incoming|from_inbound_email' - and self.document_incoming_process): - document = email_.result + + if self.action != 'document.incoming|from_inbound_email': + return + + data = email_.as_dict() + attachments = data.get('attachments') or [] + + # 👉 S'il y avait des pièces jointes, le module standard a déjà créé les documents + if attachments: + if self.document_incoming_process and email_.result: + DocumentIncoming.process([email_.result], with_children=True) + return + + # 🚨 Aucune pièce jointe → on crée un document depuis le corps du mail + body = data.get('text') or data.get('html') + if not body: + return # rien à créer + + filename = (data.get('subject') or "email").strip() + filename = f"{filename}.txt" + + document, = DocumentIncoming.create([{ + 'type': self.document_incoming_type, + 'company': self.document_incoming_company.id + if self.document_incoming_company else None, + 'filename': filename, + 'data': body.encode('utf-8'), + 'mime_type': 'text/plain', + 'source': 'email', + }]) + + email_.result = document + + if self.document_incoming_process: DocumentIncoming.process([document], with_children=True) diff --git a/modules/document_incoming_wr/document.py b/modules/document_incoming_wr/document.py index b559dcf..513bf75 100644 --- a/modules/document_incoming_wr/document.py +++ b/modules/document_incoming_wr/document.py @@ -23,6 +23,8 @@ class Incoming(metaclass=PoolMeta): super().__setup__() cls.type.selection.append( ('weight_report', "Weight Report")) + cls.type.selection.append( + ('controller', "Controller")) @classmethod def _get_results(cls): @@ -39,6 +41,15 @@ class Incoming(metaclass=PoolMeta): WR.run_metadata([wr]) return wr + + def _process_controller(self): + WR = Pool().get('automation.document') + wr = WR() + wr.document = self.id + wr.type = 'controller' + wr.state = 'draft' + WR.save([wr]) + return wr # @property # def supplier_invoice_company(self):