This question has been flagged
6 Replies
7692 Views

Hello All,

I am having some issues with Date and Time when generating reports.

I fully understand that by default Odoo stores all information in its Database in UTC format where as when I see the form view it shows the correct time & date as per timezone of the user.

The timezone of my server is set PKT (GMT+5), and the same time is set in postgresql.conf.

Lets suppose its 12.01 AM 25 July and I make any purchase order at that moment; and if I generate any report to see all the purchase orders made on 25 July it will NOT show this record as in Odoo Database this transaction was stored in UTC which is 7.01 PM 24 July. So this transaction will appear in the report for 24 July but as per me it was made on 25 July.

This is one just example, this is something happening on almost everything.

Can any one be kind enough to help me fix this issue in my modules.

I am using a few custom modules on as well on Odoo CE V11 and I am really looking forward to your expert advise.

Please help me.

Thanks

Avatar
Discard
Author Best Answer

Hello Hilar,

Thanks for your answer, I dont know somehow I cant reply to you so now I am editing my answer so you can see this.

Can you please guide where to add this or if you dont mind can I paste the code for report view and py files here so you can guide accordingly.

Thanks

Avatar
Discard

Replace your the value in my answer with the DateTime field.

Check My updated answer.

Best Answer


Actually, the database stores the Datetime field according to the system timezone. In Odoo, views which will be automatically converted according to the user's timezone if it is set.

Your TZ is. GMT+5),. So your custom operations on Datetime field need the proper conversion of Timezone according to the user.

Odoo views and ORM methods are treated the TZ with moment.js and the pytz conversions. This is actually a good feature to manage different timezones in Odoo.

You can use astimezone on Datetime objects:


def astimezone(self, tz): # known case of datetime.datetime.astimezone
    """ tz -> convert to local time in new timezone tz """
    return datetime(1, 1, 1)

or 

fields.Datetime.context_timestamp(self, datetime.strptime(value, DEFAULT_SERVER_DATETIME_FORMAT))
Examples:
1. date = date.replace(tzinfo=pytz.timezone('UTC')).astimezone(timezone)
2.
import time
from datetime import datetime
from pytz import timezone
import pytz

tz = partner_id.tz or self.env._context.get('tz') # Find Timezone from user or partner or employee
att_tz = timezone(tz or 'utc') # If no tz then return in UTC
attendance_dt = datetime.strptime(date, DEFAULT_SERVER_DATETIME_FORMAT) #Input date
att_tz_dt = pytz.utc.localize(attendance_dt)
att_tz_dt = att_tz_dt.astimezone(att_tz) # converted value to tz







Avatar
Discard
Author

Hello,

Thanks

In terms of coding I am very novice.

If you dont mind, I am pasting the code lines from my module.

Can you please make the changes in that and guide me.

My timezone in GMT+5 so based on that please guide me.

Really appreciate your guidance.

Thanks in advance.

-------------------------------------

from odoo import models, fields, api

import itertools

from operator import itemgetter

import operator

class report_goods_receive_report(models.AbstractModel):

_name = 'report.dev_inter_transfer.goods_receive_report_template'

@api.multi

def get_product_ids(self,obj):

domain = []

if obj.category_id:

domain.append(('categ_id','=',obj.category_id.id))

if obj.product_ids:

domain.append(('id','in',obj.product_ids.ids))

product_ids = self.env['product.product'].search(domain)

if product_ids:

return product_ids.ids

return False

@api.multi

def get_picking_type_ids(self,obj):

if obj.warehouse_id:

picking_ids = self.env['stock.picking.type'].search([('warehouse_id','in',obj.warehouse_id.ids),('code','=','incoming')])

return picking_ids

return False

@api.multi

def get_lines(self,obj):

inter_transfer_pool = self.env['dev.inter.transfer']

product_ids = self.get_product_ids(obj)

if obj.from_date and obj.to_date:

transfer_ids = inter_transfer_pool.search([('dest_warehouse_id','in',obj.warehouse_id.ids),

('date','>=',obj.from_date),

('date','<=',obj.to_date),

('state','=','validate')])

lst =[]

for tra in transfer_ids:

for transfer in tra.line_ids:

if transfer.product_id.id in product_ids:

qty = transfer.uom_id._compute_quantity(transfer.receive_qty, transfer.product_id.uom_id)

lst.append({

'product':transfer.product_id.name or '',

'qty':qty,

'price':transfer.product_id.standard_price or 0.0,

'uom_id':transfer.product_id and transfer.product_id.uom_id and transfer.product_id.uom_id.name or '',

})

from_date = obj.from_date +' 00:00:00'

to_date = obj.to_date+' 23:59:59'

picking_type_ids = self.get_picking_type_ids(obj)

if picking_type_ids and product_ids:

line_ids = self.env['purchase.order.line'].search([('order_id.date_order','>=',from_date),

('order_id.date_order','<=',to_date),

('order_id.picking_type_id','in',picking_type_ids.ids),

('product_id','in',product_ids),

('state','in',['purchase','done'])])

for line in line_ids:

pro_qty = line.product_qty

if line.qty_received:

pro_qty = line.qty_received

qty = line.product_uom._compute_quantity(pro_qty, line.product_id.uom_po_id)

lst.append({

'product':line.product_id.name or '',

'qty':qty,

'price':line.product_id.standard_price or 0.0,

'uom_id':line.product_uom and line.product_uom.name or '',

})

if lst:

n_lines=sorted(lst,key=itemgetter('product'))

groups = itertools.groupby(n_lines, key=operator.itemgetter('product'))

lst = [{'product':k,'values':[x for x in v]} for k, v in groups]

return lst

@api.multi

def set_total(self,total1, total2):

return float(total1) + float(total2)

@api.multi

def get_report_values(self, docids, data=None):

docs = self.env['goods.receive.report'].browse(docids)

return {

'doc_ids': docs.ids,

'doc_model': 'goods.receive.report',

'docs': docs,

'get_lines':self.get_lines,

'set_total':self.set_total,

}

Best Answer

Hi Asad:

Odoo displays the timezone information based on a user's preferences. However, if all your users are located in the same timezone you can try this:

  • Activate developer mode

  • Go to Settings > Users & Companies

  • Set the filter to "Inactive Users"

  • One of the inactive users will be something called "odoobot". Update the "Timezone" in the "Preferences" tab of this user to your local timezone.

Avatar
Discard