Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
166 Weergaven

HELLO,

I need to calculate a field which shows the last order date from each customers.

The field I created in customer model its call "x_studio_fecha_ultima_compra"

Whereas the formula max was right aplied, the depedence field it did not work and its drive me crazy.

please, help me how to have to call the field date order from sale order model, and how to have to calculate the maximum value of date order field for each customer.


regards,






Avatar
Annuleer
Auteur

Someone? can help me??

Auteur Beste antwoord

this is what I wrote,


and the result, it doesnt calculate yet.

this is the field...




Avatar
Annuleer
Beste antwoord

Hi,

  • dependancies : sale_order_ids.date_order,sale_order_ids.state
    • sale_order_ids.date_order : when this date change, we want update our field
    • sale_order_ids.state : when the state change too. Because if a client change his mind and cancel an order/sale, we must change the date.
  • calculate :
for record in self:
    confirmed_client_orders = record.sale_order_ids.filtered(lambda o: o.state == 'sale')
    if confirmed_client_orders:
        # Obtenir la date de la dernière commande
        record['x_studio_fecha_ultima_compra'] = max(confirmed_client_orders.mapped('date_order'))
    else:
        # Pas de commandes confirmées
        record['x_studio_fecha_ultima_compra'] = False

Avatar
Annuleer
Auteur

Hi, Thanks.
I changed the code and It solved the windows error about dependences fields, but it doesnt show any date in the calculate fields yet.

Hi,

When you havé the correct code, it won’t update all your client and set the last order date in your field.

The code is only call when one Of the to field in your dépendances are updated.

So to test, you have either to change a state Of an existing order (cancel it and then confirm it) or create and confirme a new order.
If this work by updating your new field, the code is correct.

Then if you want to update all your client, you can add a third dependencies in the field, try a field you don’t use in your client.

Then export all your client with this field présent in your exported Column (it’s to save your old value).
After that, import a new/change value for this field in all your client. This will call the code because there is a change in this field. And after that ré-import your saved exported file who containe your saved value.

And now, you can delete this third field in your dependencies because, it was just to make a whole update Of your client but not needed when the solution is correct.

I’ve tried the code and it works in my database…
Hope you’l get it.