Skip to Content
Menu
This question has been flagged
1 Odpoveď
520 Zobrazenia

So, the new fields is "x_studio_dias_ultima_compra" (res.partner model , float). It should be the difference between today() and field named "x_studio_fecha_ultima_compra_1" (res.partner model , datetime).

its calculation it works like this (and it is succesfully operative ):

for record in self:
    confirmed_client_orders = record.sale_order_ids.filtered(lambda o: o.state == 'sale')
    if confirmed_client_orders:
        record['x_studio_fecha_ultima_compra_1'] = max(confirmed_client_orders.mapped('date_order'))
    else:
        record['x_studio_fecha_ultima_compra_1'] = False

so the new calculation that I'll try without luck is:


    def _compute_dias_ultima_compra(self):
        today = date.today()
        for record in self:
            if record.x_studio_fecha_ultima_compra_1 is set:
               
                fecha_compra = record.x_studio_fecha_ultima_compra_1.date()
                diferencia = (today - fecha_compra).days
                record.x_studio_dias_ultima_compra = diferencia
            else:
                record.x_studio_dias_ultima_compra = 0

the results are... 0 in all "x_studio_dias_ultima_compra" customer's field and then when I updated some record in a customer I got an error window refered to this comand.

I dont see the mistake, please help me,

regards

Avatar
Zrušiť

Actual error message would be beneficial

Best Answer

Hi,

if record.x_studio_fecha_ultima_compra_1 is set:

is set is not a valid Python. To check if a field has a value, you must use if record.x_studio_fecha_ultima_compra_1:.


Please refer to the code below:


from datetime import date


def _compute_dias_ultima_compra(self):

    today = date.today()

    for record in self:

        if record.x_studio_fecha_ultima_compra_1:

            # Convert datetime to date (avoid comparing datetime with date)

            fecha_compra = record.x_studio_fecha_ultima_compra_1.date()

            diferencia = (today - fecha_compra).days

            record.x_studio_dias_ultima_compra = diferencia

        else:

            record.x_studio_dias_ultima_compra = 0



Hope it helps.

Avatar
Zrušiť