Skip to content

Commit 63c1ad8

Browse files
committed
[IMP] base_import_pdf_by_template: Add fixed value compatibility to different types of fields
TT51379
1 parent d59e4bb commit 63c1ad8

10 files changed

+129
-16
lines changed

base_import_pdf_by_template/README.rst

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ Known issues / Roadmap
102102
======================
103103

104104
- Add operator in template lines (= or ilike)
105-
- Add support for selection fields as default value.
106105
- Simplify auto-detection (defining a text only to search the system should search the
107106
corresponding regular expression).
108107
- Allow compatibility with registration process created from email alias (for purchase

base_import_pdf_by_template/demo/base_import_pdf_template.xml

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
<field name="value_type">fixed</field>
5050
<field name="fixed_value" ref="base.user_admin" />
5151
</record>
52+
<record
53+
id="demo_base_import_pdf_template_res_partner_header_05"
54+
model="base.import.pdf.template.line"
55+
>
56+
<field name="template_id" ref="demo_base_import_pdf_template_res_partner" />
57+
<field name="related_model">header</field>
58+
<field name="field_id" ref="base.field_res_partner__ref" />
59+
<field name="value_type">fixed</field>
60+
<field name="fixed_value_char">fixed-ref</field>
61+
</record>
5262
<record
5363
id="demo_base_import_pdf_template_res_partner_line_01"
5464
model="base.import.pdf.template.line"

base_import_pdf_by_template/models/base_import_pdf_template.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ def _prepare_ctx_from_model(self, model):
127127
for fixed_key in list(fixed_fields.keys()):
128128
ctx_key = "default_%s" % fixed_key
129129
fixed_value = fixed_fields[fixed_key]
130-
ctx.update({ctx_key: fixed_value.id})
130+
if isinstance(fixed_value, models.Model):
131+
fixed_value = fixed_value.id
132+
ctx.update({ctx_key: fixed_value})
131133
return ctx
132134

133135
def _get_fixed_fields_from_model(self, model):
@@ -136,7 +138,7 @@ def _get_fixed_fields_from_model(self, model):
136138
lambda x: x.model == model and x.value_type == "fixed"
137139
)
138140
for fixed_field in fixed_fields:
139-
res[fixed_field.field_name] = fixed_field.fixed_value
141+
res[fixed_field.field_name] = fixed_field._get_fixed_value()
140142
return res
141143

142144
def _get_field_header_values(self, text):
@@ -275,6 +277,16 @@ class BaseImportPdfTemplateLine(models.Model):
275277
default="variable",
276278
string="Value type",
277279
)
280+
fixed_value_char = fields.Char()
281+
fixed_value_date = fields.Date()
282+
fixed_value_datetime = fields.Datetime()
283+
fixed_value_float = fields.Float()
284+
fixed_value_html = fields.Html()
285+
fixed_value_integer = fields.Integer()
286+
fixed_value_selection = fields.Many2one(
287+
comodel_name="ir.model.fields.selection", domain="[('field_id', '=', field_id)]"
288+
)
289+
fixed_value_text = fields.Text()
278290
fixed_value = fields.Reference(
279291
selection="_selection_reference_value",
280292
string="Fixed value",
@@ -313,6 +325,27 @@ def _compute_model(self):
313325
else item.template_id.child_model
314326
)
315327

328+
def _get_fixed_field_name_ttype_mapped(self):
329+
return {
330+
"char": "fixed_value_char",
331+
"date": "fixed_value_date",
332+
"datetime": "fixed_value_datetime",
333+
"float": "fixed_value_float",
334+
"html": "fixed_value_html",
335+
"integer": "fixed_value_integer",
336+
"selection": "fixed_value_selection",
337+
"text": "fixed_value_text",
338+
"many2one": "fixed_value",
339+
}
340+
341+
def _get_fixed_value(self):
342+
self.ensure_one()
343+
f_name = self._get_fixed_field_name_ttype_mapped()[self.field_ttype]
344+
f_value = self[f_name]
345+
if self.field_ttype == "selection":
346+
f_value = f_value.value
347+
return f_value
348+
316349
def _replace_text(self, text, letters, prefix):
317350
for letter in letters:
318351
text = text.replace(letter, prefix + letter)

base_import_pdf_by_template/readme/ROADMAP.rst

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
- Add operator in template lines (= or ilike)
2-
- Add support for selection fields as default value.
32
- Simplify auto-detection (defining a text only to search the system should search the
43
corresponding regular expression).
54
- Allow compatibility with registration process created from email alias (for purchase

base_import_pdf_by_template/static/description/index.html

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
/*
1010
:Author: David Goodger (goodger@python.org)
11-
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
11+
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
1212
:Copyright: This stylesheet has been placed in the public domain.
1313
1414
Default cascading style sheet for the HTML output of Docutils.
15-
Despite the name, some widely supported CSS2 features are used.
1615
1716
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
1817
customize this style sheet.
@@ -275,7 +274,7 @@
275274
margin-left: 2em ;
276275
margin-right: 2em }
277276

278-
pre.code .ln { color: gray; } /* line numbers */
277+
pre.code .ln { color: grey; } /* line numbers */
279278
pre.code, code { background-color: #eeeeee }
280279
pre.code .comment, code .comment { color: #5C6576 }
281280
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
@@ -301,7 +300,7 @@
301300
span.pre {
302301
white-space: pre }
303302

304-
span.problematic, pre.problematic {
303+
span.problematic {
305304
color: red }
306305

307306
span.section-subtitle {
@@ -465,7 +464,6 @@ <h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
465464
<h1><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h1>
466465
<ul class="simple">
467466
<li>Add operator in template lines (= or ilike)</li>
468-
<li>Add support for selection fields as default value.</li>
469467
<li>Simplify auto-detection (defining a text only to search the system should search the
470468
corresponding regular expression).</li>
471469
<li>Allow compatibility with registration process created from email alias (for purchase
@@ -531,9 +529,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
531529
<div class="section" id="maintainers">
532530
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
533531
<p>This module is maintained by the OCA.</p>
534-
<a class="reference external image-reference" href="https://odoo-community.org">
535-
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
536-
</a>
532+
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
537533
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
538534
mission is to support the collaborative development of Odoo features and
539535
promote its widespread use.</p>

base_import_pdf_by_template/tests/test_base_import_pdf_by_template.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def test_wizard_base_import_pdf_by_template_01(self):
8787
self.assertEqual(record.country_id.code, "ES")
8888
self.assertEqual(record.industry_id.name, "Food")
8989
self.assertEqual(record.user_id, self.user)
90+
self.assertEqual(record.ref, "fixed-ref")
9091
self.assertEqual(len(record.child_ids), 3)
9192
child_1 = record.child_ids.filtered(lambda x: x.name == "Child 1")
9293
self.assertEqual(child_1.street, "Address 1")
@@ -123,6 +124,7 @@ def test_wizard_base_import_pdf_by_template_02(self):
123124
self.assertEqual(record.country_id.code, "ES")
124125
self.assertEqual(record.industry_id.name, "Food")
125126
self.assertEqual(record.user_id, self.user)
127+
self.assertEqual(record.ref, "fixed-ref")
126128
self.assertEqual(len(record.child_ids), 3)
127129
child_1 = record.child_ids.filtered(lambda x: x.name == "Child 1")
128130
self.assertEqual(child_1.street, "Address 1")
@@ -133,7 +135,7 @@ def test_wizard_base_import_pdf_by_template_02(self):
133135
child_3 = record.child_ids.filtered(lambda x: x.name == "Child 3")
134136
self.assertEqual(child_3.street, "Address 3")
135137
self.assertEqual(child_3.country_id.code, "ES")
136-
self.assertFalse(record.message_ids)
138+
self.assertTrue(record.message_ids) # Error message to set ref to childs
137139

138140
def test_wizard_base_import_pdf_by_template_error(self):
139141
self.env.ref(

base_import_pdf_by_template/views/base_import_pdf_template_line_views.xml

+43
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,53 @@
5151
name="search_subfield_id"
5252
attrs="{'invisible': [('search_field_ttype', '!=', 'one2many')]}"
5353
/>
54+
<!-- Fixed value !-->
55+
<field
56+
name="fixed_value_char"
57+
string="Fixed value"
58+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'char')]}"
59+
/>
60+
<field
61+
name="fixed_value_date"
62+
string="Fixed value"
63+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'date')]}"
64+
/>
65+
<field
66+
name="fixed_value_datetime"
67+
string="Fixed value"
68+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'datetime')]}"
69+
/>
70+
<field
71+
name="fixed_value_float"
72+
string="Fixed value"
73+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'float')]}"
74+
/>
75+
<field
76+
name="fixed_value_html"
77+
string="Fixed value"
78+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'html')]}"
79+
/>
80+
<field
81+
name="fixed_value_integer"
82+
string="Fixed value"
83+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'integer')]}"
84+
/>
85+
<field
86+
name="fixed_value_selection"
87+
string="Fixed value"
88+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'selection')]}"
89+
/>
90+
<field
91+
name="fixed_value_text"
92+
string="Fixed value"
93+
attrs="{'invisible': ['|', ('value_type', '!=', 'fixed'),('field_ttype', '!=', 'text')]}"
94+
/>
5495
<field
5596
name="default_value"
97+
string="Fixed value"
5698
attrs="{'invisible': ['|', ('value_type', '=', 'fixed'),('field_relation', '=', False)]}"
5799
/>
100+
<!-- Fixed value !-->
58101
<field
59102
name="log_distinct_value"
60103
attrs="{'invisible': ['|', ('field_id', '=', False),('value_type', '=', 'fixed')]}"

base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,19 @@ def _process_form(self):
194194
template.child_model
195195
)
196196
for field_name in list(child_fixed_values.keys()):
197-
setattr(line_form, field_name, child_fixed_values[field_name])
197+
child_field_value = child_fixed_values[field_name]
198+
try:
199+
setattr(line_form, field_name, child_field_value)
200+
except Exception:
201+
if not self.log_text:
202+
self.log_text = ""
203+
self.log_text += _(
204+
"Error to set %(field_name)s with value %(value)s"
205+
) % {
206+
"field_name": field_name,
207+
"value": child_field_value,
208+
}
209+
198210
# et the values of any line
199211
for field_name in list(line.keys()):
200212
self.with_context(

test_base_import_pdf_by_template/demo/base_import_pdf_template.xml

+17
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@
8282
<field name="pattern">ES[0-9]{10}</field>
8383
<field name="value_type">variable</field>
8484
</record>
85+
<record id="po_decathlon_line_origin" model="base.import.pdf.template.line">
86+
<field name="template_id" ref="po_decathlon" />
87+
<field name="related_model">header</field>
88+
<field name="field_id" ref="purchase.field_purchase_order__origin" />
89+
<field name="value_type">fixed</field>
90+
<field name="fixed_value_char">fixed-origin</field>
91+
</record>
8592
<record id="po_decathlon_line_product_id" model="base.import.pdf.template.line">
8693
<field name="template_id" ref="po_decathlon" />
8794
<field name="related_model">lines</field>
@@ -215,6 +222,16 @@
215222
<field name="value_type">fixed</field>
216223
<field name="fixed_value" ref="partner_tecnativa" />
217224
</record>
225+
<record id="invoice_tecnativa_line_move_type" model="base.import.pdf.template.line">
226+
<field name="template_id" ref="invoice_tecnativa" />
227+
<field name="related_model">header</field>
228+
<field name="field_id" ref="account.field_account_move__move_type" />
229+
<field name="value_type">fixed</field>
230+
<field
231+
name="fixed_value_selection"
232+
ref="account.selection__account_invoice_report__move_type__in_invoice"
233+
/>
234+
</record>
218235
<record
219236
id="invoice_tecnativa_line_product_id"
220237
model="base.import.pdf.template.line"

test_base_import_pdf_by_template/tests/test_base_import_pdf_by_template.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_purchase_order_decathlon(self):
7474
attachments = self._get_attachments(record)
7575
self.assertEqual(record.partner_id, self.partner_decathlon)
7676
self.assertEqual(record.partner_ref, "ES9812110233")
77+
self.assertEqual(record.origin, "fixed-origin")
7778
self.assertIn(attachment, attachments)
7879
self.assertEqual(len(record.order_line), 5)
7980
self.assertEqual(sum(record.order_line.mapped("product_uom_qty")), 5)
@@ -88,12 +89,13 @@ def test_purchase_order_decathlon(self):
8889
def test_account_invoice_tecnativa(self):
8990
attachment = self._create_ir_attachment("account_invoice_tecnativa.pdf")
9091
wizard = self._create_wizard_base_import_pdf_upload("account.move", attachment)
91-
# Similar context from Vendor invoices menu
92-
wizard = wizard.with_context(**{"default_move_type": "in_invoice"})
92+
# Similar context from Customer invoices menu
93+
wizard = wizard.with_context(**{"default_move_type": "out_invoice"})
9394
res = wizard.action_process()
9495
self.assertEqual(res["res_model"], "account.move")
9596
record = self.env[res["res_model"]].browse(res["res_id"])
9697
attachments = self._get_attachments(record)
98+
self.assertEqual(record.move_type, "in_invoice")
9799
self.assertEqual(record.partner_id, self.partner_tecnativa)
98100
self.assertIn(attachment, attachments)
99101
self.assertEqual(len(record.invoice_line_ids), 6)

0 commit comments

Comments
 (0)