Skip to content

Commit

Permalink
wms_connector_picking_batch: Adds picking batch specialization for wm…
Browse files Browse the repository at this point in the history
…s_connector
  • Loading branch information
FranzPoize committed May 8, 2024
1 parent 24b3096 commit b338229
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 0 deletions.
1 change: 1 addition & 0 deletions wms_connector_picking_batch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions wms_connector_picking_batch/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Wms Connector",
"summary": """
WMS Connector""",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "Akretion,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/wms",
"depends": ["stock", "sale", "attachment_synchronize"],
"data": [
"security/wms_product_sync.xml",
"views/wms_product_sync.xml",
"views/stock_picking.xml",
"views/stock_warehouse.xml",
"views/attachment_queue_view.xml",
],
"demo": [
"demo/storage_backend.xml",
],
}
1 change: 1 addition & 0 deletions wms_connector_picking_batch/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import stock_picking_batch
73 changes: 73 additions & 0 deletions wms_connector_picking_batch/models/stock_picking_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from odoo import _, api, fields, models
from odoo.exceptions import UserError


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

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"
)

wms_import_attachment_id = fields.Many2one(
"attachment.queue", index=True, readonly=True
)
wms_export_date = fields.Datetime(tracking=True)

def _get_wms_export_task(self):
return self.picking_type_id.warehouse_id.sudo().wms_task_id

def _compute_wms_sync_cancel_supported(self):
self.wms_sync_cancel_supported = False

@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
and rec.picking_type_id.code in ("incoming", "outgoing")
)

def _is_user_allowed_to_cancel(self):
return self.env.user.has_group("stock.group_stock_manager")

def action_force_cancel_wms(self):
if not self._is_user_allowed_to_cancel():
raise UserError(_("You are not allowed to cancel this batch transfer"))
self.wms_export_date = None
self.wms_export_attachment_id = None
return self.with_context(skip_wms_cancel_check=True).action_cancel()

def action_cancel(self):
for record in self.filtered(lambda p: p.state != "cancel"):
if (
not self._context.get("skip_wms_cancel_check")
and record.wms_export_date
and not record.wms_sync_cancel_supported
):
raise UserError(
_(
"The batch transfer %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()

def _wms_check_if_editable(self):
if self._context.get("skip_check_protected_fields"):
return True
for picking_batch in self:
if picking_batch.wms_export_date:
raise UserError(
_("The batch transfer %s have been exported and can not be modified")
% picking_batch.name
)
107 changes: 107 additions & 0 deletions wms_connector_picking_batch/models/stock_warehouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from odoo import fields, models

FILTER_VALS = {
"wms_export_batch_picking_in_filter_id": {
"name": "WMS: {} filter for picking batch in",
"model_id": "stock.picking.batch",
},
"wms_export_batch_picking_out_filter_id": {
"name": "WMS: {} filter for picking batch out",
"model_id": "stock.picking.batch",
},
}
FILTER_DOMAINS = {
"wms_export_product_filter_id": "[]",
"wms_export_picking_in_filter_id": '[("wms_export_date", "=", False),'
' ("picking_type_id", "=", {}), ("state", "=", "assigned")]',
"wms_export_picking_out_filter_id": '[("wms_export_date", "=", False),'
' ("picking_type_id", "=", {}), ("state", "=", "assigned")]',
}


MAPPINGS = {
"export_batch_pickings_in": {
"fieldname_task": "wms_export_task_id",
"fieldname_cron": "wms_export_batch_picking_in_cron_id",
"filetype": "export",
"method_type": "export",
"filepath": "IN/",
"name_fragment": "export batch pickings in",
"code": "wh = env['stock.warehouse'].browse({})\n"
"env['stock.picking.batch'].with_context(attachment_task=wh.{})._schedule_export(wh,"
"domain=wh._wms_domain_for('batch_pickings_in')),",
},
"export_batch_pickings_out": {
"fieldname_task": "wms_export_task_id",
"fieldname_cron": "wms_export_batch_picking_out_cron_id",
"filetype": "export",
"method_type": "export",
"filepath": "IN/",
"name_fragment": "export batch pickings out",
"code": "wh = env['stock.warehouse'].browse({})\n"
"env['stock.picking.batch'].with_context(attachment_task=wh.{})._schedule_export(wh,"
"domain=wh._wms_domain_for('batch_pickings_out')),",
},
}

class StockWarehouse(models.Model):
_inherit = "stock.warehouse"

wms_export_batch_picking_in_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_export_batch_picking_out_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_export_batch_picking_in_filter_id = fields.Many2one("ir.filters")
wms_export_batch_picking_out_filter_id = fields.Many2one("ir.filters")

def _wms_domain_for(self, model_domain):
domains = {
"batch_pickings_in": self.wms_export_batch_picking_in_filter_id._get_eval_domain(),
"batch_pickings_out": self.wms_export_batch_picking_out_filter_id._get_eval_domain(),
}
return domains[model_domain]

def _get_mappings(self):
mappings = super()._get_mappings()
mappings.update(MAPPINGS)
return mappings

def _get_filter_vals(self):
# TODO(franz) remove picking with batch id from the picking in and out filter
filter_vals = super()._get_filter_vals()
filter_vals.update(FILTER_VALS)
return filter_vals

def _activate_filters(self):
self.wms_export_batch_picking_in_filter_id.domain = FILTER_DOMAINS[
"wms_export_batch_picking_in_filter_id"
].format(self.in_type_id.id)
self.wms_export_batch_picking_out_filter_id.domain = FILTER_DOMAINS[
"wms_export_batch_picking_out_filter_id"
].format(self.out_type_id.id)

def button_open_wms_batch_pickings_in(self):
return {
"name": "WMS synchronized transfers",
"view_mode": "tree,form",
"views": [
(False, "tree"),
(False, "form"),
],
"res_model": "stock.picking.batch",
"type": "ir.actions.act_window",
"target": "current",
"domain": self._wms_domain_for("batch_pickings_in"),
}

def button_open_wms_batch_pickings_out(self):
return {
"name": "WMS synchronized transfers",
"view_mode": "tree,form",
"views": [
(False, "tree"),
(False, "form"),
],
"res_model": "stock.picking.batch",
"type": "ir.actions.act_window",
"target": "current",
"domain": self._wms_domain_for("batch_pickings_out"),
}
93 changes: 93 additions & 0 deletions wms_connector_picking_batch/views/stock_picking_batch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Akretion
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="stock_picking_form_view">
<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="is_wms_exportable" invisible="True" />
<button
name="button_trigger_export"
type="object"
string="Export to WMS"
attrs="{'invisible': [
'|',
'|',
('wms_export_date', '!=', False),
('is_wms_exportable', '=', False),
('state', 'in', ('cancel', 'done', 'draft')),
]}"
/>
<button
name="action_force_cancel_wms"
type="object"
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
name="wms_import_attachment_id"
attrs="{'invisible': [('wms_import_attachment_id', '=', False)]}"
/>
</field>
<header position="after">
<div
class="alert alert-danger"
role="alert"
attrs="{'invisible': [('wms_export_error', '=', False)]}"
>
<p>Synchronisation error</p>
<field name="wms_export_error" />
</div>
</header>
</field>
</record>

<record model="ir.ui.view" id="stock_picking_search">
<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">
<separator />
<filter
string="To export"
name="not_wms_exported"
domain="[('wms_export_date', '=', False)]"
/>
</filter>
</field>
</record>

<record model="ir.ui.view" id="stock_picking_tree_view">
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.vpicktree" />
<field name="arch" type="xml">
<field name="date_deadline" position="after">
<field name="wms_export_date" string="Exported at" optional="True" />
</field>
</field>
</record>


</odoo>
28 changes: 28 additions & 0 deletions wms_connector_picking_batch/views/stock_warehouse.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Akretion
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.ui.view" id="stock_warehouse_form_view">
<field name="name">stock.warehouse.form (in wms_connector)</field>
<field name="model">stock.warehouse</field>
<field name="inherit_id" ref="stock.view_warehouse" />
<field name="arch" type="xml">
<div class="oe_button_box" position="inside">
<button
name="button_open_wms_batch_pickings_in"
string="WMS sync batch transfer in"
icon="fa-refresh"
class="oe_stat_button"
type="object"
/>
<button
name="button_open_wms_pickings_out"
string="WMS sync batch transfer pickings out"
icon="fa-refresh"
class="oe_stat_button"
type="object"
/>
</div>
</field>
</record>
</odoo>

0 comments on commit b338229

Please sign in to comment.