[imp] make validity_date a real field, fix company relation, fix security groups

This commit is contained in:
NIels Göttsch 2024-10-19 22:11:19 +02:00 committed by madmooose
parent a7fc0a7dc8
commit 811a04aadd
4 changed files with 58 additions and 26 deletions

View file

@ -1,4 +1,4 @@
from odoo import _, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError
@ -42,15 +42,21 @@ class SaleOrder(models.Model):
raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}"))
return super().action_confirm()
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get("batch_id"):
vals["validity_date"] = (
self.env["sale.order.batch"].search([("id", "=", vals.get("batch_id"))]).validity_date
)
return super().create(vals_list)
def write(self, vals):
if "batch_id" in vals:
invalid_orders = []
for order in self:
if order.state not in ["draft", "sent"]:
invalid_orders.append(order.name)
if invalid_orders:
raise UserError(_(f"Sale Order not in State Draft or Sent: {', '.join(invalid_orders)}"))
if vals.get("batch_id"):
vals["validity_date"] = (
self.env["sale.order.batch"].search([("id", "=", vals.get("batch_id"))]).validity_date
)
res = super().write(vals)
if "batch_id" in vals:
if vals.get("batch_id"):
self.order_line._update_batch_product()
return res

View file

@ -1,15 +1,19 @@
from datetime import timedelta
from odoo import _, api, fields, models
STATES = [("open", "Open"), ("close", "Close")]
READONLY_FIELD_STATES = {state: [("readonly", True)] for state in {"close"}}
class SaleOrderBatch(models.Model):
"""Group serveral Sale Orders into a batch"""
_name = "sale.order.batch"
_description = "Sale Order Batch"
_inherit = "mail.thread"
_description = "Sales Order Batch"
_inherit = ["mail.thread", "mail.activity.mixin"]
_order = "date_order desc, id desc"
_check_company_auto = True
@ -42,7 +46,16 @@ class SaleOrderBatch(models.Model):
help="Creation date of order batch,\nConfirmation date of confirmed orders.",
default=fields.Datetime.now,
)
validity_date = fields.Date(compute="_compute_validity_date")
validity_date = fields.Date(
string="Expiration",
compute="_compute_validity_date",
inverse="_inverse_validity_date",
store=True,
readonly=False,
copy=False,
precompute=True,
states=READONLY_FIELD_STATES,
)
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)
@ -53,14 +66,28 @@ class SaleOrderBatch(models.Model):
product_count = fields.Integer(compute="_compute_product_count")
partner_credit_warning = fields.Text(compute="_compute_partner_credit_warning")
@api.depends("sale_order_ids.validity_date")
@api.depends("company_id")
def _compute_validity_date(self):
enabled_feature = bool(self.env["ir.config_parameter"].sudo().get_param("sale.use_quotation_validity_days"))
if not enabled_feature:
self.validity_date = False
return
today = fields.Date.context_today(self)
for batch in self:
if batch.sale_order_ids:
batch.validity_date = min(batch.sale_order_ids.mapped("validity_date"))
days = batch.company_id.quotation_validity_days
if days > 0:
batch.validity_date = today + timedelta(days)
else:
batch.validity_date = False
def _inverse_validity_date(self):
"""
Set validity date on all Sale Orders
"""
for batch in self:
for order in batch.sale_order_ids:
order.validity_date = batch.validity_date
@api.depends("sale_order_ids.order_line")
def _compute_sale_order_line_ids(self):
for batch in self:
@ -136,8 +163,7 @@ class SaleOrderBatch(models.Model):
else:
action = {"type": "ir.actions.act_window_close"}
context = {"default_move_type": "out_invoice"}
action["context"] = context
action["context"] = {"default_move_type": "out_invoice"}
return action
def action_confirm(self):
@ -153,12 +179,5 @@ class SaleOrderBatch(models.Model):
if "company_id" in vals:
self = self.with_company(vals["company_id"])
if vals.get("name", _("New")) == _("New"):
seq_date = (
fields.Datetime.context_timestamp(self, fields.Datetime.to_datetime(vals["date_order"]))
if "date_order" in vals
else None
)
vals["name"] = self.env["ir.sequence"].next_by_code("sale.order.batch", sequence_date=seq_date) or _(
"New"
)
vals["name"] = self.env["ir.sequence"].next_by_code("sale.order.batch") or _("New")
return super().create(vals_list)

View file

@ -1,4 +1,5 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_sale_order_batch_invoicing_payments,sale.order.batch,model_sale_order_batch,account.group_account_invoice,1,1,0,0
access_sale_order_batch,sale.order.batch,model_sale_order_batch,sales_team.group_sale_salesman,1,1,1,0
access_sale_order_batch_manager,sale.order.batch.manager,model_sale_order,sales_team.group_sale_manager,1,1,1,1
access_sale_order_batch_manager,sale.order.batch.manager,model_sale_order_batch,sales_team.group_sale_manager,1,1,1,1
access_sale_order_batch_product,sale.order.batch.product,model_sale_order_batch_product,sales_team.group_sale_salesman,1,1,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_sale_order_batch_invoicing_payments sale.order.batch model_sale_order_batch account.group_account_invoice 1 1 0 0
3 access_sale_order_batch sale.order.batch model_sale_order_batch sales_team.group_sale_salesman 1 1 1 0
4 access_sale_order_batch_manager sale.order.batch.manager model_sale_order model_sale_order_batch sales_team.group_sale_manager 1 1 1 1
5 access_sale_order_batch_product sale.order.batch.product model_sale_order_batch_product sales_team.group_sale_salesman 1 1 1 1

View file

@ -8,6 +8,8 @@
<field name="name"/>
<field name="date_order"/>
<field name="validity_date" optional="hide"/>
<field name="company_id" groups="base.group_multi_company" optional="show" readonly="1"/>
<field name="company_id" groups="!base.group_multi_company" invisible="1"/>
<field name="sale_order_ids" widget="many2many_tags"/>
<field name="state"/>
</tree>
@ -91,6 +93,8 @@
<group>
<field name="date_order" attrs="{'readonly': [('state','=','closed')]}"/>
<field name="validity_date" attrs="{'invisiable': [('state','=','closed')]}"/>
<field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
<field name="company_id" invisible="1" groups="!base.group_multi_company"/>
</group>
</group>
<notebook>
@ -105,6 +109,7 @@
<field name="date_order"/>
<field name="amount_untaxed"/>
<field name="amount_total"/>
<field name="company_id" invisible="1"/>
<field
name="invoice_status"
decoration-success="invoice_status == 'invoiced'"
@ -129,6 +134,7 @@
<field name="product_template_id" optional="hide"/>
<field name="name"/>
<field name="product_uom_qty"/>
<field name="company_id" invisible="1"/>
<field name="price_unit"/>
<field name="price_subtotal"/>
</tree>