Friday, 15 March 2019

Onchange, Compute method, Constrains on Date fields in Odoo 11



1. Date calculation "compute" method  in Odoo 11


with Starting date and Ending date fields.

Calculates No of days => days between starting date  and ending date.
To calculate how many days project task completed.

Conditions:

1. Start date greater than or equal to today
2. Start date should not be greater than Ending date

@api.one@api.depends('start_date', 'end_date')
def _date_calc(self):
    if self.start_date and self.end_date:
        d1 = datetime.strptime(str(self.start_date), '%Y-%m-%d')
        d2 = datetime.strptime(str(self.end_date), '%Y-%m-%d')
        today_date = datetime.now().date()
        pd = datetime.strptime(str(today_date), '%Y-%m-%d')
        if d1 < pd:
            raise UserError("Start date should not be less than Today.")
        if d1 <= d2:
            d3 = d2 - d1
            self.number_of_days = str(d3.days + 1)
        else:
            self.number_of_days = 0

            raise UserError("Start date should be less than end date.")
    else:
        self.number_of_days = 0





start_date = fields.Date(string='Start Date')
end_date = fields.Date(string='End Date')
number_of_days = fields.Char(string='No. of Days', compute='_date_calc')

@api.one@api.depends('start_date', 'end_date')
def _date_calc(self):
    if self.start_date and self.end_date:
        d1 = datetime.strptime(str(self.start_date), '%Y-%m-%d')
        d2 = datetime.strptime(str(self.end_date), '%Y-%m-%d')
        today_date = datetime.now().date()
        pd = datetime.strptime(str(today_date), '%Y-%m-%d')
        if d1 < pd:
            raise UserError("Start date should not be less than Today.")
        if d1 <= d2:
            d3 = d2 - d1
            self.number_of_days = str(d3.days + 1)
        else:
            self.number_of_days = 0 
            raise UserError("Start date should be less than end date.")
    else:
        self.number_of_days = 0

Example

2. On change Date field in Odoo 11



@api.onchange('start_date')
def onchange_date(self):
    print("onchange start date......")
    if self.start_date:
        sd = datetime.strptime(str(self.start_date), '%Y-%m-%d')
        ed = datetime.strptime(str(self.start_date), '%Y-%m-%d')
        today_date = datetime.now().date()
        pd = datetime.strptime(str(today_date), '%Y-%m-%d')

        if sd < pd:
            print("sd is lesser than pd --> Abnormal")
            self.start_date = datetime.now().date()
            return {
                'warning': {
                    'title': "Error in Date Selection", 
                    'message': "Starting date should not be lesser than today",                }
                    }
        elif sd > ed:
            print("Elif --> Sd is greater than ed --> Abnormal")
            self.start_date = self.start_date = datetime.now().date()
            return {
                'warning': {
                    'title': "Error in Date Selection", 
                    'message': "Starting date should not be greater than Ending Date", 
                           }
                    }
        else:
            print("Every thing right onchange start date....")



 
@api.onchange('end_date')
def onchange_date(self):
        print("onchange end date.....")
        if self.end_date:
            ed = datetime.strptime(str(self.end_date), '%Y-%m-%d')
            sd = datetime.strptime(str(self.start_date), '%Y-%m-%d')
            
            if ed < sd:
                print("End date is lesser than Start date -- Abnormal ")
                self.end_date = self.start_date
                return {
                    'warning': {
                        'title': "Error in Date Selection", 
                        'message': "Ending date should not be lesser than Starting date", 
                               }
                        }


3. Constrains on Date field in Odoo11

 

@api.constrains('start_date', 'end_date')
def validate_date(self):
    """Checking the start and end dates of the syllabus, 
      raise warning if start date is not anterior"""  
    for rec in self:   
         if rec.start_date > rec.end_date:
            raise ValidationError(_('Start date should not come after End date'))

 

 


No comments:

Post a Comment