Skip to content

Commit

Permalink
wms_connector: clean view, and simplify code, add cancel check
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Nov 29, 2023
1 parent e1be055 commit 5021f42
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 50 deletions.
58 changes: 35 additions & 23 deletions wms_connector/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class StockPicking(models.Model):
_inherit = ["synchronize.exportable.mixin", "stock.picking"]
_name = "stock.picking"

wms_connector_exported = fields.Boolean(
String="Exported to WMS",
compute="_compute_wms_exported",
readonly=True,
store=True,
)
is_wms_exportable = fields.Boolean(
compute="_compute_is_wms_exportable", readonly=True, store=True
)
wms_sync_cancel_supported = fields.Boolean(
compute="_compute_wms_sync_cancel_supported"
)

@api.depends("wms_export_attachment")
def _compute_wms_exported(self):
for rec in self:
rec.wms_connector_exported = bool(rec.wms_export_attachment)
def _compute_wms_sync_cancel_supported(self):
self.wms_sync_cancel_supported = False

@api.depends("picking_type_id.warehouse_id.active_wms_sync")
@api.depends(
"picking_type_id.warehouse_id.active_wms_sync",
"picking_type_id.code",
)
def _compute_is_wms_exportable(self):
for rec in self:
rec.is_wms_exportable = rec.picking_type_id.warehouse_id.active_wms_sync

def action_show_export(self):
self.ensure_one()
return {
"name": "WMS export",
"type": "ir.actions.act_window",
"res_model": "attachment.queue",
"view_mode": "form",
"res_id": self.wms_export_attachment.id,
}
rec.is_wms_exportable = (
rec.picking_type_id.warehouse_id.active_wms_sync
and rec.picking_type_id.code in ("incoming", "outgoing")
)

def action_force_cancel_wms(self):
self.env.user.has_group("stock.group_stock_manager")
return self.with_context(skip_wms_cancel_check=True).action_cancel()

def action_cancel(self):
for record in self:
if (
not self._context.get("skip_wms_cancel_check")
and record.wms_export_date
and not record.wms_sync_cancel_supported
):
raise UserError(
_(
"The picking %s can not be deleted as it have been sent "
"to the WMS, ask your manager to force the cancellation"
)
% record.name
)
return super().action_cancel()
4 changes: 2 additions & 2 deletions wms_connector/models/synchronize_exportable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SynchronizeExportableMixin(models.AbstractModel):
_description = "Synchronizable export mixin"

wms_export_date = fields.Datetime(readonly=True)
wms_export_attachment = fields.Many2one(
wms_export_attachment_id = fields.Many2one(
"attachment.queue", index=True, readonly=True
)
wms_export_error = fields.Char(readonly=True, index=True)
Expand Down Expand Up @@ -59,7 +59,7 @@ def synchronize_export(self):

def track_export(self, attachment):
self.wms_export_date = datetime.datetime.now()
self.wms_export_attachment = attachment
self.wms_export_attachment_id = attachment

def _prepare_export_data(self, idx) -> list:
raise NotImplementedError
Expand Down
61 changes: 36 additions & 25 deletions wms_connector/views/stock_picking.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,73 @@
<odoo>

<record model="ir.ui.view" id="stock_picking_form_view">
<field name="name">stock.picking.form (in wms_connector)</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form" />
<field name="arch" type="xml">
<header position="inside">
<field name="wms_connector_exported" invisible="True" />
<field name="is_wms_exportable" invisible="True" />
<button
name="button_trigger_export"
type="object"
string="Export to WMS"
attrs="{'invisible': ['|', ('wms_connector_exported', '=', True), ('is_wms_exportable', '=', False)]}"
attrs="{'invisible': [
'|',
'|',
('wms_export_date', '!=', False),
('is_wms_exportable', '=', False),
('state', 'in', ('cancel', 'done', 'draft')),
]}"
/>
</header>
<xpath expr="//div[@name='button_box']" position="inside">
<button
name="action_show_export"
attrs="{'invisible': [('wms_connector_exported', '=', False)]}"
class="oe_stat_button"
name="action_force_cancel_wms"
type="object"
icon="fa-list"
help="View export attachment"
>
<div class="o_form_field o_stat_info">
<span class="oe_stat_text">WMS export</span>
</div>
</button>
</xpath>
string="Force Cancel"
groups="stock.group_stock_manager"
attrs="{'invisible': [
'|',
('wms_export_date', '=', False),
('state', '=', 'cancel'),
]}"
confirm="Before cancelling, ensure that your have cancelled the picking in the
external WMS"
/>
</header>
<field name="date_done" position="before">
<field
name="wms_export_date"
attrs="{'invisible': [('is_wms_exportable', '=', False)]}"
/>
</field>
<field name="group_id" position="after">
<field
name="wms_export_attachment_id"
attrs="{'invisible': [('wms_export_attachment_id', '=', False)]}"
/>
</field>
</field>
</record>

<record model="ir.ui.view" id="stock_picking_search">
<field name="name">stock.picking.search (in wms_connector)</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_internal_search" />
<field name="arch" type="xml">
<filter name="starred" position="after">
<field name="wms_connector_exported" invisible="True" />
<separator />
<filter
string="Not WMS exported"
string="To export"
name="not_wms_exported"
domain="[('wms_connector_exported', '=', False)]"
domain="[('wms_export_date', '=', False)]"
/>
</filter>
</field>
</record>

<record model="ir.ui.view" id="stock_picking_tree_view">
<field name="name">stock.picking.tree (in wms_connector)</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.vpicktree" />
<field name="arch" type="xml">
<field name="state" position="after">
<field name="wms_connector_exported" invisible="True" />
<field name="is_wms_exportable" invisible="True" />
<field name="wms_connector_exported" optional="True" />
<field name="date_assigned" position="after">
<field name="wms_export_date" string="Exported at" optional="True" />
</field>
</field>
</record>
Expand Down

0 comments on commit 5021f42

Please sign in to comment.