İçereği Atla
Menü
Bu soru işaretlendi
2 Cevaplar
1219 Görünümler

Hi,

I have a custom module that contains two date fields, 'Inception Date' and 'Expiry Date', and I want to create an Automated Action to check a checkbox field 'Leap Year' if either of those dates are in a leap year.

I've got the Trigger as 'On Creation & Update', and the Trigger Fields as 'Inception Date' and 'Expiry Date'.

I've got the Action to Do as 'Update Record', writing True as the value for Leap Year.

How do I set the 'Apply on' conditions so that this action only happens if the Inception Date or Expiry Fields are in a leap year? Or is there a better way to achieve this same thing?

Any help appreciated!

Percy

Avatar
Vazgeç
En İyi Yanıt

Hey Percy Hosken,


I hope you are doing well.


There are two ways to fullfill this requirement. 

Please find code in comment. 

Hope it will be helpful to you.

Thanks & Regards,
Email:   odoo@aktivsoftware.com  

Skype: kalpeshmaheshwari

Avatar
Vazgeç

Please find code Example here :-
1) Here, you can find an example of automated action to set boolean to true if the date is in a leap year.

Note - Check Indentation of the code after pasting the code.

- Create an automated action by adding
name, model( custom model as per your requirement),
trigger(on creation and updations as per your requirement), Trigger fields (Expiry Date & Inception Date), Action To Do ( Execute Python Code ).

- In the Python Code Below Paste this code:-

for rec in records:
if rec.inception_date:
year = rec.inception_date.year
leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
if leap_year:
rec.write({"leap_year": True})
if rec.expiry_date and not rec.set:
year = rec.expiry_date.year
leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
if leap_year:
rec.write({"leap_year": True})

2) Using a compute method which will be dependent on the 2 date fields.

leap_year = fields.Boolean(string="Leap Year", compute="compute_leap_year", store=True)

Place the below code in your custom model
Note - Check Indentation of the code after pasting the code.

@api.depends(‘inception_date’,’expiry_date’)
def compute_leap_year(self):
for rec in self:
if rec.inception_date:
year = rec.inception_date.year
leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
if leap_year:
rec.write({"leap_year": True})
if rec.expiry_date and not rec.set:
year = rec.expiry_date.year
leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
if leap_year:
rec.write({"leap_year": True})

Üretici

Thank you! That was exactly the information that I needed

En İyi Yanıt

Hi Percy,

I suggest you do as follows:

leap_year = fields.Boolean(string="Leap Year", compute="_compute_leap_year", store=True)

@api.depends("date_start", "date_end")
def _compute_leap_year(self):
for line in self:
leap_year = False
if
line.date_start % 4 == 0:
if line.date_start % 100 == 0:
if line.date_start % 400 == 0:
line.leap_year = True
else
:
line.leap_year = True
if not
leap_year:
if line.date_end % 4 == 0:
if line.date_end % 100 == 0:
if line.date_end % 400 == 0:
line.leap_year = True
else
:
line.leap_year = True
line.leap_year = leap_year


Avatar
Vazgeç