i'm using odoo 10 and i want to calculate arrival_time
i Have two field an Float field datetime field , what i want is when the user put a value in that float field it will be add to that arrival time
Here is my code:
from odoo import models
from odoo import fields
from odoo import api
from datetime import datetime
class TransportManagementLines(models.Model):
_name = 'transport.management.lines'
_description = 'Transport Management Lines'
_rec_name = 'chauffeur'
chauffeur = fields.Many2one('transport.management.matricule',string='Nom Chauffeur', required="1",track_visibility='always')
starting_cities = fields.Many2one('transport.management.trajet',string='Trajet De Transport', required="1",track_visibility='onchange')
starting_time = fields.Datetime(string="Heure Départ", required="1",track_visibility='onchange')
arrival_time = fields.Datetime(string="Heure d'arriver", required="1", compute="set_arrival_time", store=True)
@api.depends('starting_cities','starting_time')
def set_arrival_time(self):
for rec in self:
if rec.starting_time and rec.starting_cities:
rec.arrival_time = rec.starting_time + datetime.timedelta(seconds=(self.env['transport.management.trajet'].search([("nom_depart","in",rec.starting_cities.ids)],limit=1).travel_time * 3600))
else:
rec.arrival_time = False
class TransportManagementTrajet(models.Model):
_name = 'transport.management.trajet'
_description = 'Transport Management Trajet'
_rec_name = 'nom_depart'
nom_depart = fields.Many2one('transport.management.config',string="Villes De Trajet", required="1")
travel_distance = fields.Integer(string='Nombre de KM', required="1")
travel_time = fields.Float(string="Durée de Trajet", required="1")```
Odoo stores datetime fields as a string, so before adding to it timedelta, you need to convert it to datetime type.
This should work:
rec.arrival_time = fields.Datetime.from_string(rec.starting_time) + datetime.timedelta(seconds=(self.env['transport.management.trajet'].search([("nom_depart","in",rec.starting_cities.ids)],limit=1).travel_time * 3600))