Skip to content

Commit

Permalink
[flowchart modules] Optimize flowchart
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHachard committed Aug 11, 2024
1 parent ce94866 commit 32f0fda
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion purchase_stock_flowchart/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "This module enhances the Purchase Order by adding a flowchart that visually represents the stock flow, providing a clearer understanding of inventory movements associated with each order.",
'images': ['static/description/banner.png'], # 560x280 px
"category": 'Inventory/Purchase',
"version": "0.0.2",
"version": "0.0.3",
"license": "AGPL-3",
"author": "Victor",
"price": 0,
Expand Down
17 changes: 11 additions & 6 deletions purchase_stock_flowchart/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ def _get_purchase_picking_flowchart_graph(self, more_info=False):
self._get_flowchart_element(more_info=more_info),
self._get_flowchart_click()
]
i18n, menu_id = None, None
if self.picking_ids:
i18n = dict(self.env['stock.picking'].fields_get(allfields=['state'])['state']['selection'])
menu_id = self.env.ref('stock.menu_stock_root').id
for hierarchy in self.picking_ids._get_picking_hierarchy():
if len(hierarchy) > 1:
flowchart.append(hierarchy[0]._get_flowchart_sub_graph())
flowchart.append(hierarchy[0]._get_flowchart_sub_graph(i18n, menu_id))
flowchart.append(f'po{self.id} --> sub{hierarchy[0].id}')
elif len(hierarchy) == 1:
flowchart.append(f'po{self.id} --> {hierarchy[0]._get_flowchart_element()}')
flowchart.append(hierarchy[0]._get_flowchart_click())
flowchart.append(f'po{self.id} --> {hierarchy[0]._get_flowchart_element(i18n)}')
flowchart.append(hierarchy[0]._get_flowchart_click(menu_id))
return '\n'.join(flowchart)

def _get_flowchart_style(self):
Expand Down Expand Up @@ -64,7 +68,8 @@ def _get_flowchart_element(self, i18n=None, more_info=False):
return f'po{self.id}("{self.name} - {_(i18n[self.state])}\n{self.partner_id.display_name}"):::{state}'
return f'po{self.id}({self.name} - {_(i18n[self.state])}):::{state}'

def _get_flowchart_click(self):
def _get_flowchart_click(self, menu_id=None):
self.ensure_one()
menu_id = self.env.ref('purchase.menu_purchase_root')
return f'click po{self.id} "/web#id={self.id}&model=purchase.order&view_type=form&menu_id={menu_id.id}" _blank'
if not menu_id:
menu_id = self.env.ref('purchase.menu_purchase_root').id
return f'click po{self.id} "/web#id={self.id}&model=purchase.order&view_type=form&menu_id={menu_id}" _blank'
2 changes: 1 addition & 1 deletion sale_stock_flowchart/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "This module enhances the Sale Order by adding a flowchart that visually represents the stock flow, providing a clearer understanding of inventory movements associated with each order.",
'images': ['static/description/banner.png'], # 560x280 px
"category": 'Sales/Sales',
"version": "0.0.2",
"version": "0.0.3",
"license": "AGPL-3",
"author": "Victor",
"price": 0,
Expand Down
17 changes: 11 additions & 6 deletions sale_stock_flowchart/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ def _get_sale_picking_flowchart_graph(self, more_info=False):
self._get_flowchart_element(more_info=more_info),
self._get_flowchart_click()
]
i18n, menu_id = None, None
if self.picking_ids:
i18n = dict(self.env['stock.picking'].fields_get(allfields=['state'])['state']['selection'])
menu_id = self.env.ref('stock.menu_stock_root').id
for hierarchy in self.picking_ids._get_picking_hierarchy():
if len(hierarchy) > 1:
flowchart.append(hierarchy[0]._get_flowchart_sub_graph())
flowchart.append(hierarchy[0]._get_flowchart_sub_graph(i18n, menu_id))
flowchart.append(f'so{self.id} --> sub{hierarchy[0].id}')
elif len(hierarchy) == 1:
flowchart.append(f'so{self.id} --> {hierarchy[0]._get_flowchart_element()}')
flowchart.append(hierarchy[0]._get_flowchart_click())
flowchart.append(f'so{self.id} --> {hierarchy[0]._get_flowchart_element(i18n)}')
flowchart.append(hierarchy[0]._get_flowchart_click(menu_id))
return '\n'.join(flowchart)

def _get_flowchart_style(self):
Expand Down Expand Up @@ -63,7 +67,8 @@ def _get_flowchart_element(self, i18n=None, more_info=False):
return f'so{self.id}("{self.name} - {_(i18n[self.state])}\n{self.partner_id.display_name}"):::{state}'
return f'so{self.id}({self.name} - {_(i18n[self.state])}):::{state}'

def _get_flowchart_click(self):
def _get_flowchart_click(self, menu_id=None):
self.ensure_one()
menu_id = self.env.ref('sale.sale_order_menu')
return f'click so{self.id} "/web#id={self.id}&model=sale.order&view_type=form&menu_id={menu_id.id}" _blank'
if not menu_id:
menu_id = self.env.ref('sale.sale_order_menu').id
return f'click so{self.id} "/web#id={self.id}&model=sale.order&view_type=form&menu_id={menu_id}" _blank'
2 changes: 1 addition & 1 deletion stock_flowchart/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"summary": "Technical module that adds a generic flowchart to stock for use by other modules.",
"description": "Technical module that adds a generic flowchart to stock for use by other modules.",
"category": "Hidden",
"version": "0.0.1",
"version": "0.0.2",
"license": "AGPL-3",
"author": "Victor",
"price": 0,
Expand Down
25 changes: 15 additions & 10 deletions stock_flowchart/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@
class StockPicking(models.Model):
_inherit = 'stock.picking'

def _get_flowchart_sub_graph(self):
def _get_flowchart_sub_graph(self, i18n=None, menu_id=None):
self.ensure_one()
if not i18n:
i18n = dict(self.env['stock.picking'].fields_get(allfields=['state'])['state']['selection'])
if not menu_id:
menu_id = self.env.ref('stock.menu_stock_root').id
click_done = []
flowchart = [f'subgraph sub{self.id} [" "]', 'direction LR']
# Add all backorder parent recursively
backorder = self
while backorder.backorder_id:
flowchart.append(f'{backorder.backorder_id._get_flowchart_element()} --> {backorder._get_flowchart_element()}')
flowchart.append(f'{backorder.backorder_id._get_flowchart_element(i18n)} --> {backorder._get_flowchart_element(i18n)}')
if backorder.backorder_id.id not in click_done:
flowchart.append(backorder.backorder_id._get_flowchart_click())
flowchart.append(backorder.backorder_id._get_flowchart_click(menu_id))
click_done.append(backorder.backorder_id.id)
if backorder.id not in click_done:
flowchart.append(backorder._get_flowchart_click())
flowchart.append(backorder._get_flowchart_click(menu_id))
click_done.append(backorder.id)
backorder = backorder.backorder_id
# Add all backorder child recursively
backorder = self
while backorder.backorder_ids:
for back in backorder.backorder_ids:
flowchart.append(f'{backorder._get_flowchart_element()} --> {back._get_flowchart_element()}')
flowchart.append(f'{backorder._get_flowchart_element(i18n)} --> {back._get_flowchart_element(i18n)}')
if backorder.id not in click_done:
flowchart.append(backorder._get_flowchart_click())
flowchart.append(backorder._get_flowchart_click(menu_id))
click_done.append(backorder.id)
if back.id not in click_done:
flowchart.append(back._get_flowchart_click())
flowchart.append(back._get_flowchart_click(menu_id))
click_done.append(back.id)
backorder = back
flowchart.append('end')
Expand Down Expand Up @@ -85,7 +89,8 @@ def _get_flowchart_element(self, i18n=None):
state = self._get_flowchart_state_color().get(self.state)
return f'sp{self.id}({self.name} - {_(i18n[self.state])}):::{state}'

def _get_flowchart_click(self):
def _get_flowchart_click(self, menu_id=None):
self.ensure_one()
stock_menu = self.env.ref('stock.menu_stock_root')
return f'click sp{self.id} "/web#id={self.id}&model=stock.picking&view_type=form&menu_id={stock_menu.id}" _blank'
if not menu_id:
menu_id = self.env.ref('stock.menu_stock_root').id
return f'click sp{self.id} "/web#id={self.id}&model=stock.picking&view_type=form&menu_id={menu_id}" _blank'

0 comments on commit 32f0fda

Please sign in to comment.