Skip to content

Commit 2aae0a9

Browse files
committed
[IMP] excise_management: Replace compute method with automated actions
- Removed compute field logic for 'x_lines_ids' based on 'x_stock_move_ids'. - Added server action 'ir_action_create_excise_report_line' to generate excise report lines when report is created/updated. - Added server action 'ir_action_update_excise_report_line_on_stock_move_done' to update/create corresponding excise lines when stock moves are marked as 'done'. - Introduced 'base.automation' rules to trigger: • Excise report line creation on report create/write. • Excise line update on stock move state change to 'done'.
1 parent 96de1bd commit 2aae0a9

File tree

4 files changed

+109
-53
lines changed

4 files changed

+109
-53
lines changed

excise_management/data/base_automation.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@
2020
<field name="name">Add/Remove Excise Taxes on Fiscal Position</field>
2121
<field name="trigger_field_ids" eval="[(6, 0, [ref('fiscal_position_is_fiscal_deposit')])]"/>
2222
</record>
23+
<record id="base_automation_create_excise_report_line" model="base.automation">
24+
<field name="model_id" ref="excise_report"/>
25+
<field name="action_server_ids" eval="[(6, 0, [ref('ir_action_create_excise_report_line')])]"/>
26+
<field name="trigger">on_create</field>
27+
<field name="name">Create Excise Report Line</field>
28+
</record>
29+
<record id="base_automation_update_excise_report_line_onupdate_stock_move" model="base.automation">
30+
<field name="model_id" ref="stock.model_stock_move"/>
31+
<field name="action_server_ids" eval="[(6, 0, [ref('ir_action_update_excise_report_line_onupdate_stock_move')])]"/>
32+
<field name="trigger">on_create_or_write</field>
33+
<field name="name">Update Excise line: when changing state and date</field>
34+
<field name="trigger_field_ids" eval="[(6, 0, [ref('stock.field_stock_move__date'), ref('stock.field_stock_move__state')])]"/>
35+
</record>
2336
</odoo>

excise_management/data/ir_actions_server.xml

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,78 @@ if from_date < datetime.date.today():
6161
<field name="state">code</field>
6262
<field name="name">Open stock moves</field>
6363
<field name="code"><![CDATA[
64-
action = {'view_mode': 'list', 'res_model': 'stock.move', 'type': 'ir.actions.act_window', 'domain': [('id', 'in', record.x_stock_move_ids.ids)], 'view_id': env.ref('excise_management.stock_move_view_list').id, 'name': 'Excises Analysis',}
64+
action = {'view_mode': 'list', 'res_model': 'stock.move', 'type': 'ir.actions.act_window', 'domain': [('id', 'in', record.x_line_ids.mapped('x_move_ids.id'))], 'view_id': env.ref('excise_management.stock_move_view_list').id, 'name': 'Excises Analysis',}
6565
]]></field>
6666
</record>
67+
<record id="ir_action_create_excise_report_line" model="ir.actions.server">
68+
<field name="code"><![CDATA[
69+
all_moves_ids = env['stock.move'].search([
70+
('date', '>=', min(records.mapped('x_from_date'), default=datetime.datetime.today())),
71+
('date', '<', max(records.mapped('x_to_date'), default=datetime.datetime.today())),
72+
('x_excise_category', '!=', False),
73+
('x_fiscal_deposit_move', 'not in', ['none', False]),
74+
('state', '=', 'done'),
75+
])
76+
for record in records:
77+
move_ids = all_moves_ids.filtered(lambda m: m.date.date() >= record.x_from_date and m.date.date() < record.x_to_date)
78+
grouped_data = {}
79+
for move in move_ids:
80+
key = (move.x_excise_category.id if move.x_excise_category else False,
81+
move.x_fiscal_deposit_move or '')
82+
if key not in grouped_data:
83+
grouped_data[key] = {'qty': 0.0, 'amount': 0.0, 'line_move_ids': []}
84+
grouped_data[key]['qty'] += move.x_excise_quantity or 0.0
85+
grouped_data[key]['amount'] += move.x_excise_amount or 0.0
86+
grouped_data[key]['line_move_ids'].append(move.id)
87+
lines = env['x_excise_report_line'].create([{
88+
'x_excise_category_id': key[0],
89+
'x_excise_move_type': key[1],
90+
'x_excise_quantity': values['qty'],
91+
'x_excise_amount': values['amount'],
92+
'x_excise_report_id': record.id,
93+
'x_move_ids': [(6, 0, values['line_move_ids'])]
94+
} for key, values in grouped_data.items()])
95+
record['x_line_ids'] = [(6, 0, lines.ids)] if bool(lines) else [(5, 0, 0)]
96+
]]></field>
97+
<field name="model_id" ref="excise_report"/>
98+
<field name="state">code</field>
99+
<field name="name">Action to Create Excise Report Line</field>
100+
<field name="usage">base_automation</field>
101+
</record>
102+
<record id="ir_action_update_excise_report_line_onupdate_stock_move" model="ir.actions.server">
103+
<field name="code"><![CDATA[
104+
if record['state'] == 'done' and record['x_excise_category'] and record['x_fiscal_deposit_move'] and record['x_fiscal_deposit_move'] != 'none':
105+
all_report_ids = env['x_excise_report'].search([])
106+
old_report_ids = all_report_ids.filtered(lambda report: report.x_from_date > record['date'].date() or report.x_to_date <= record['date'].date())
107+
for report_id in old_report_ids:
108+
line_ids = report_id.x_line_ids.filtered(lambda l: l.x_excise_category_id == record['x_excise_category'] and l.x_excise_move_type == record['x_fiscal_deposit_move'] and record.id in l.x_move_ids.ids)
109+
for line_id in line_ids:
110+
line_id['x_excise_quantity'] -= record['x_excise_quantity'] or 0.0
111+
line_id['x_excise_amount'] -= record['x_excise_amount'] or 0.0
112+
line_id['x_move_ids'] = [(3, record.id)]
113+
if not line_id['x_move_ids']:
114+
line_id.unlink()
115+
new_report_ids = all_report_ids - old_report_ids
116+
for report_id in new_report_ids:
117+
line_ids = report_id.x_line_ids.filtered(lambda l: l.x_excise_category_id == record['x_excise_category'] and l.x_excise_move_type == record['x_fiscal_deposit_move'])
118+
for line_id in line_ids:
119+
if record.id not in line_id.x_move_ids.ids:
120+
line_id['x_excise_quantity'] += record['x_excise_quantity'] or 0.0
121+
line_id['x_excise_amount'] += record['x_excise_amount'] or 0.0
122+
line_id['x_move_ids'] = [(4, record.id)]
123+
if not line_ids:
124+
env['x_excise_report_line'].create([{
125+
'x_excise_category_id': record['x_excise_category'].id,
126+
'x_excise_move_type': record['x_fiscal_deposit_move'],
127+
'x_excise_quantity': record['x_excise_quantity'],
128+
'x_excise_amount': record['x_excise_amount'],
129+
'x_excise_report_id': report_id.id,
130+
'x_move_ids': [(6, 0, [record.id])]
131+
}])
132+
]]></field>
133+
<field name="model_id" ref="stock.model_stock_move"/>
134+
<field name="state">code</field>
135+
<field name="name">Action to Update Excise line</field>
136+
<field name="usage">base_automation</field>
137+
</record>
67138
</odoo>

excise_management/data/ir_model_fields.xml

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -366,44 +366,13 @@ for record in self:
366366
<field name="field_id" ref="state_excise_report" />
367367
<field name="value">done</field>
368368
</record>
369-
<record id="excise_report_stock_moves" model="ir.model.fields">
370-
<field name="name">x_stock_move_ids</field>
371-
<field name="ttype">many2many</field>
372-
<field name="model_id" ref="excise_report"/>
373-
<field name="field_description">Stock move IDs</field>
374-
<field name="relation">stock.move</field>
375-
<field name="compute"><![CDATA[
376-
moves_ids = self.env['stock.move'].search([
377-
('date', '>=', min(self.mapped('x_from_date'), default=datetime.datetime.today())),
378-
('date', '<', max(self.mapped('x_to_date'), default=datetime.datetime.today())),
379-
('x_excise_category', '!=', False),
380-
('x_fiscal_deposit_move', 'not in', ['none', False]),
381-
('state', '=', 'done'),
382-
])
383-
for record in self:
384-
if record.x_from_date and record.x_to_date:
385-
record['x_stock_move_ids'] = moves_ids.filtered(lambda m: m.date.date() >= record.x_from_date and m.date.date() < record.x_to_date).ids
386-
]]></field>
387-
<field name="store" eval="False"/>
388-
</record>
389-
<record id="excise_report_stock_moves_count" model="ir.model.fields">
390-
<field name="compute"><![CDATA[
391-
for record in self: record['x_move_id_count'] = len(record.x_stock_move_ids)
392-
]]></field>
393-
<field name="ttype">integer</field>
394-
<field name="field_description">Stock move ID count</field>
395-
<field name="model_id" ref="excise_report"/>
396-
<field name="name">x_move_id_count</field>
397-
<field name="depends">x_stock_move_ids</field>
398-
<field name="store" eval="False"/>
399-
<field name="readonly" eval="True"/>
400-
</record>
401369
<record id="excise_report_line_excise_report" model="ir.model.fields">
402370
<field name="ttype">many2one</field>
403371
<field name="name">x_excise_report_id</field>
404372
<field name="field_description">Excise report ID</field>
405373
<field name="model_id" ref="excise_report_line" />
406374
<field name="relation">x_excise_report</field>
375+
<field name="on_delete">cascade</field>
407376
</record>
408377
<record id="excise_report_line_currency_id" model="ir.model.fields">
409378
<field name="ttype">many2one</field>
@@ -482,33 +451,36 @@ for rec in self: rec['x_currency_id'] = self.env.company.currency_id.id
482451
<field name="store">True</field>
483452
<field name="currency_field">x_currency_id</field>
484453
</record>
454+
<record id="excise_report_line_move_ids" model="ir.model.fields">
455+
<field name="ttype">many2many</field>
456+
<field name="name">x_move_ids</field>
457+
<field name="field_description">Move IDs</field>
458+
<field name="model_id" ref="excise_report_line" />
459+
<field name="relation">stock.move</field>
460+
<field name="readonly" eval="True"/>
461+
<field name="copied" eval="False"/>
462+
</record>
485463
<record id="excise_report_excise_report_line" model="ir.model.fields">
486464
<field name="ttype">one2many</field>
487-
<field name="name">x_lines_ids</field>
465+
<field name="name">x_line_ids</field>
488466
<field name="field_description">Lines IDs</field>
489467
<field name="model_id" ref="excise_report" />
490468
<field name="relation">x_excise_report_line</field>
491469
<field name="relation_field">x_excise_report_id</field>
492-
<field name="depends">x_stock_move_ids</field>
470+
<field name="store" eval="True"/>
471+
<field name="copied" eval="False"/>
472+
</record>
473+
<record id="excise_report_stock_moves_count" model="ir.model.fields">
493474
<field name="compute"><![CDATA[
494-
for record in self:
495-
grouped_data = {}
496-
for move in record.x_stock_move_ids:
497-
key = (move.x_excise_category.id if move.x_excise_category else False,
498-
move.x_fiscal_deposit_move or '')
499-
if key not in grouped_data:
500-
grouped_data[key] = {'qty': 0.0, 'amount': 0.0}
501-
grouped_data[key]['qty'] += move.x_excise_quantity or 0.0
502-
grouped_data[key]['amount'] += move.x_excise_amount or 0.0
503-
lines = self.env['x_excise_report_line'].create([{
504-
'x_excise_category_id': key[0],
505-
'x_excise_move_type': key[1],
506-
'x_excise_quantity': values['qty'],
507-
'x_excise_amount': values['amount'],
508-
} for key, values in grouped_data.items()])
509-
record['x_lines_ids'] = [(6, 0, lines.ids)] if bool(lines) else [(5, 0, 0)]
510-
]]></field>
475+
for record in self: record['x_move_id_count'] = len(record.x_line_ids.mapped('x_move_ids'))
476+
]]></field>
477+
<field name="ttype">integer</field>
478+
<field name="field_description">Stock move ID count</field>
479+
<field name="model_id" ref="excise_report"/>
480+
<field name="name">x_move_id_count</field>
481+
<field name="depends">x_line_ids, x_line_ids.x_move_ids</field>
511482
<field name="store" eval="False"/>
483+
<field name="readonly" eval="True"/>
512484
</record>
513485
<record id="res_company_reporter" model="ir.model.fields">
514486
<field name="name">x_reporter_id</field>

excise_management/data/ir_ui_view.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
</group>
203203
<notebook>
204204
<page string="Details" name="lines">
205-
<field name="x_lines_ids">
205+
<field name="x_line_ids">
206206
<list default_group_by="x_excise_category_id" create="0" edit="0" delete="0" editable="bottom">
207207
<field name="x_currency_id" column_invisible="1"/>
208208
<field name="x_excise_category_id"/>

0 commit comments

Comments
 (0)