I am trying to extend the sale order module adding a start date and an end date to each sale order line, therefore if an article is sold (given away) in specified timespace it cannot be given or sold in that time.
I added the start date and end date in the sale.order.line and worked in sale.order view like this :
views.xml :
<odoo>
<data>
<record id="view_partner_inherit_date" model="ir.ui.view">
<field name="name">Sale order (Date)</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml" >
<xpath expr="//field[@name='order_line']/tree//field[@name='product_uom_qty']" position="after">
<field string="Date debut" name="date_debut" index="true"/>
<field string="Date fin" name="date_fin" index="true"/>
</xpath>
</field>
</record>
</data>
</odoo>
and the model as follows :
from odoo import models, fields, api
from odoo.exceptions import ValidationError
from datetime import date
class sales_date(models.Model):
_inherit = 'sale.order.line'
date_debut = fields.Date(index=True, help="Date du début de la location")
date_fin = fields.Date(index=True, help="Date du fin de la location")
@api.constrains('date_debut','date_fin')
def _check_date(self):
today = date.today().strftime('%d-%m-%Y')
if(self.date_debut > self.date_fin):
raise ValidationError("La date du début doit être avant la date de fin")
class sale_date(models.Model):
_inherit = 'sale.order'
@api.one
@api.constrains('order_line','date_debut','date_fin')
def _check_dates(self):
today = date.today().strftime('%d-%m-%Y')
mylist = list()
for r in self:
for l in r.order_line:
mylist.append(l)
for r in self:
for l1 in r.order_line:
for l2 in mylist:
if((l1.id != l2.id) and (l1.product_id == l2.product_id)):
if((l1.date_debut <= l2.date_debut <= l1.date_fin) or (l1.date_debut <= l2.date_fin <= l1.date_fin)):
raise ValidationError("L'article : \""+l1.product_id.name +"\" est pris du : "+l1.date_debut+" au "+l1.date_fin+" par : "+r.partner_id.name+" dans l'ordre de vente : "+r.name)
Note : i am sorry the attributes names are in french.
The problem is that when i'm working on the same sale order i can do it and the message is shown, but if another sale.order.line on a different sale.order then the message is not shown and the element is stored in the database
Help me please Thank you !