[imp] add confirm and constraints and sale order line summary

This commit is contained in:
Niels Göttsch 2024-07-17 23:13:04 +02:00
parent 4cc617ae66
commit 140c30796a
4 changed files with 107 additions and 27 deletions

View file

@ -5,9 +5,12 @@ from odoo.exceptions import UserError
class SaleOrder(models.Model):
_inherit = "sale.order"
batch_id = fields.Many2one("sale.order.batch")
batch_id = fields.Many2one("sale.order.batch", copy=False)
def action_add_to_batch(self):
"""
TODO: Add Wizard to choose between batches if more than one existis. Picking the first one for now.
"""
batch = self.env["sale.order.batch"].search([("state", "=", "open")], limit=1)
if not batch:
batch = self.env["sale.order.batch"].create()
@ -24,3 +27,24 @@ class SaleOrder(models.Model):
if invalid_orders:
raise UserError(_(f"Sale Order not in State Draft or Sent: {', '.join(invalid_orders)}"))
return super().write(vals)
def action_confirm(self):
invalid_orders = []
if not self.env.context.get("bypass_batch", False):
for order in self:
if order.batch_id:
invalid_orders.append(order.name)
if invalid_orders:
raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}"))
return super().action_confirm()
def action_view_sale_order_batch(self):
return {
"name": _("Sale Order Batch"),
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "form",
"res_model": "sale.order.batch",
"res_id": self.batch_id.id,
# 'target': '',
}

View file

@ -42,6 +42,7 @@ class SaleOrderBatch(models.Model):
sale_order_ids = fields.One2many("sale.order", "batch_id")
sale_order_count = fields.Integer(compute="_compute_sale_order_count")
sale_order_line_ids = fields.Many2many("sale.order.line", compute="_compute_sale_order_line_ids", store=True)
amount_total = fields.Float(compute="_compute_amount_total", string="Total")
product_ids = fields.Many2many("product.product", compute="_compute_product_ids")
product_count = fields.Integer(compute="_compute_product_count")
@ -68,6 +69,11 @@ class SaleOrderBatch(models.Model):
for sale_order in self:
sale_order.sale_order_line_ids = sale_order.sale_order_ids.mapped("order_line")
@api.depends("sale_order_ids.amount_total")
def _compute_amount_total(self):
for batch in self:
batch.amount_total = sum(batch.sale_order_ids.mapped("amount_total"))
@api.depends("product_ids")
def _compute_product_count(self):
for batch in self:
@ -97,6 +103,13 @@ class SaleOrderBatch(models.Model):
result = {"type": "ir.actions.act_window_close"}
return result
def action_confirm(self):
for batch in self:
orders = batch.with_context(bypass_batch=True).sale_order_ids
orders.action_confirm()
batch.state = "closed"
return True
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
@ -111,5 +124,4 @@ class SaleOrderBatch(models.Model):
vals["name"] = self.env["ir.sequence"].next_by_code("sale.order.batch", sequence_date=seq_date) or _(
"New"
)
return super().create(vals_list)

View file

@ -5,10 +5,11 @@
<field name="model">sale.order.batch</field>
<field name="arch" type="xml">
<tree>
<field name="state"/>
<field name="name"/>
<field name="validity_date"/>
<field name="date_order"/>
<field name="validity_date" optional="hide"/>
<field name="sale_order_ids" widget="many2many_tags"/>
<field name="state"/>
</tree>
</field>
</record>
@ -19,6 +20,15 @@
<field name="arch" type="xml">
<form>
<header>
<button
name="action_confirm"
id="action_confirm"
data-hotkey="v"
string="Confirm"
class="btn-primary"
type="object"
attrs="{'invisible': [('state', '!=','open')]}"
/>
<field name="state" widget="statusbar" options="{'clickable': 'true'}"/>
</header>
<sheet>
@ -51,12 +61,35 @@
name="sale_order_ids"
widget="many2many_tags"
domain="[('state','in',['draft','sent']),('batch_id','=',False)]"
attrs="{'readonly': [('state','=','closed')]}"
/>
</group>
<group>
<field name="validity_date"/>
<field name="date_order" attrs="{'readonly': [('state','=','closed')]}"/>
<field name="validity_date" attrs="{'invisiable': [('state','=','closed')]}"/>
</group>
</group>
<notebook>
<page string="Sale Order Lines" name="sale_order_line">
<field name="sale_order_line_ids">
<tree>
<field name="order_partner_id"/>
<field name="product_template_id"/>
<field name="product_uom_qty"/>
<field name="price_unit"/>
<field name="price_subtotal"/>
</tree>
</field>
<group name="note_group" col="6" class="mt-2 mt-md-0">
<group colspan="4">
</group>
<group class="oe_subtotal_footer oe_right" colspan="2" name="sale_total">
<field name="amount_total" colspan="2" readonly="1"/>
</group>
<div class="clearfix"/>
</group>
</page>
</notebook>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers"/>
@ -72,12 +105,8 @@
<field name="arch" type="xml">
<search>
<field name="name"/>
<filter
string="Open"
name="filter_property_open"
domain="[('state', '=', 'open')
]"
/>
<filter string="Open" name="filter_batch_open" domain="[('state', '=', 'open')
]"/>
</search>
</field>
</record>
@ -87,18 +116,6 @@
<field name="type">ir.actions.act_window</field>
<field name="res_model">sale.order.batch</field>
<field name="view_mode">tree,form</field>
<field name="domain">[]</field>
<field name="context">{'search_default_filter_property_open': 1}</field>
<field name="context">{'search_default_filter_batch_open': 1}</field>
</record>
<record id="action_open_orders" model="ir.actions.act_window">
<field name="name">Action Open Orders</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">sale.order</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('batch_id','=',self.acti)]</field>
<field name="context">{'search_default_filter_property_open': 1}</field>
</record>
</odoo>

View file

@ -1,13 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_order_form_inherit_batch" model="ir.ui.view">
<record id="view_order_form" model="ir.ui.view">
<field name="name">sale.order.form.inherit.batch</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='action_draft']" position="after">
<button name="action_add_to_batch" type="object" string="Add to Batch" data-hotkey="b"/>
<!-- attrs="invisible: {"|",('batch_id', '=', True),('state','not in',['draft', 'sent'])}" -->
<field name="batch_id" invisible="True"/>
<button
name="action_add_to_batch"
type="object"
string="Add to Batch"
data-hotkey="b"
attrs="{'invisible': ['|',('batch_id', '!=', False),('state','not in',['draft', 'sent'])]}"
/>
</xpath>
<xpath expr="//div[@name='button_box']" position="inside">
<button
class="oe_stat_button"
name="action_view_sale_order_batch"
type="object"
icon="fa-tasks"
attrs="{'invisible': [('batch_id', '=', False)]}"
>
<field name="batch_id" attrs="{'readonly': True}"/>
</button>
</xpath>
<xpath expr="//button[@name='action_confirm']" position="attributes">
<attribute
name="attrs"
>{'invisible': ['|',('batch_id','!=',False),('state', 'not in', ['sent'])]}</attribute>
</xpath>
<xpath expr="//button[@name='action_confirm'][2]" position="attributes">
<attribute
name="attrs"
>{'invisible': ['|',('batch_id','!=',False),('state', 'not in', ['draft'])]}</attribute>
</xpath>
</field>
</record>