Skip to Content
Menu
This question has been flagged
1 Reply
1414 Views

.py Code

from flectra import models, fields,api,_fromdatetimeimportdatetime, date, timedeltafrom flectra.tools import DEFAULT_SERVER_DATE_FORMAT# from flectra.date import date
classweekreports(models.Model):_name = 'week.task'_description ='Generate Week Base Reports'name=fields.Char(string='Name')total_days = fields.Integer(string="Total Days",store=True)active = fields.Boolean(default=True)number_of_days = fields.One2many('number.of.days','week_task',string='Number of Days')
classNumberOfDays(models.Model):_name = 'number.of.days'week_task = fields.Many2one('week.task', string='Week Tasks')no_of_days = fields.Integer(string="Number of Days")st_date = fields.Date(string='Start Date')end_date = fields.Date(string='End Date')@api.depends('st_date','end_date')@api.onchange('st_date','end_date')defchange_numbber(self):forrecinself:days = 0ifrec.st_date andrec.end_date:duration = datetime.strptime(str(rec.end_date), DEFAULT_SERVER_DATE_FORMAT) - datetime.strptime(str(rec.st_date),DEFAULT_SERVER_DATE_FORMAT)rec.no_of_days = duration.days


.xml Code

<flectra><data><recordid="app_task_id"model="ir.actions.act_window"><fieldname="name">Weeksfield><fieldname="res_model">week.taskfield><fieldname="view_mode">tree,formfield>record><recordid="week_tree"model="ir.ui.view"><fieldname="name">week.task.treefield><fieldname="model">week.taskfield><fieldname="arch"type="xml"><treestring="Weeks"><fieldname='name'/><fieldname="total_days"/><fieldname="active"/>tree>field>record>

<recordid='week_task_form_view'model='ir.ui.view'><fieldname='name'>week.task.form.viewfield><fieldname='model'>week.taskfield><fieldname='arch'type='xml'><form><header>header><sheet><group><group><fieldname='name'/>group><group><fieldname='total_days'/>group>group><notebook><pagestring="NO Of Days"><fieldname="number_of_days"><treestring="No of Days"editable="bottom"><fieldname="st_date"/><fieldname="end_date"/><fieldname="no_of_days"/>tree>field>page>notebook>sheet>form>field>record>
<menuitemid="days_task"name="Days"action="app_task_id"sequence="1"groups="base.group_user"/>data>flectra>

Avatar
Discard
Best Answer

Hello zahoor Ullah,

You can try to compute the method to calculate the total_days field from the number_of_days. I have updated your code with compute method.

Please find code in comment. 

I hope This will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

Code Example :

from datetime import datetime

from odoo import api, fields, models
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT

class WeekReports(models.Model):
_name = 'week.task'
_description = 'Generate Week Base Reports'

name = fields.Char(string='Name')
total_days = fields.Integer(string="Total Days", compute="_compute_total_days")
active = fields.Boolean(default=True)
number_of_days = fields.One2many('number.of.days', 'week_task', string='Number of Days')

@api.depends("number_of_days", "number_of_days.no_of_days")
def _compute_total_days(self):
for rec in self:
rec.total_days = sum(rec.number_of_days.mapped("no_of_days"))

class NumberOfDays(models.Model):
_name = 'number.of.days'

week_task = fields.Many2one('week.task', string='Week Tasks')
no_of_days = fields.Integer(string="Number of Days")
st_date = fields.Date(string='Start Date')
end_date = fields.Date(string='End Date')

@api.onchange('st_date', 'end_date')
def change_number(self):
for rec in self:
if rec.st_date and rec.end_date:
duration = datetime.strptime(str(rec.end_date), DEFAULT_SERVER_DATE_FORMAT) - datetime.strptime(
str(rec.st_date), DEFAULT_SERVER_DATE_FORMAT)
rec.no_of_days = duration.days
else:
rec.no_of_days = 0

Related Posts Replies Views Activity
1
Nov 24
1487
1
Nov 24
1196
2
Sep 24
1047
1
Aug 24
2456
3
Aug 24
2687