Merge lp:~vauxoo/addons-vauxoo/7.0-addons-vauxoo-imp-datetime-dev-ernesto into lp:addons-vauxoo/7.0

Status: Merged
Merged at revision: 1161
Proposed branch: lp:~vauxoo/addons-vauxoo/7.0-addons-vauxoo-imp-datetime-dev-ernesto
Merge into: lp:addons-vauxoo/7.0
Diff against target: 520 lines (+264/-85)
6 files modified
invoice_datetime/__init__.py (+2/-2)
invoice_datetime/__openerp__.py (+22/-20)
invoice_datetime/invoice.py (+98/-53)
invoice_datetime/res_config.py (+26/-10)
invoice_datetime/tests/__init__.py (+27/-0)
invoice_datetime/tests/test_invoice_datetime.py (+89/-0)
To merge this branch: bzr merge lp:~vauxoo/addons-vauxoo/7.0-addons-vauxoo-imp-datetime-dev-ernesto

Description of the change

Se cambia el tipo de campo de function a datetime, se aplica la zona horaria del usuario antes de escribir o crear nuevo registro

To post a comment you must log in.
1116. By Luis Ernesto García Medina - http://www.vauxoo.com

[IMP][invoice_datetime] the time difference is improved by applying the time zone minutes

Revision history for this message
Jorge Angel Naranjo Rogel - http://www.vauxoo.com (jorge-nr) wrote :

@Wicho

De la linea 51-58 y de la 63-71 estamos repitiendo el código, lo mejor sería crear una función que reciba el datetime y si no viene sea False, y retorne el datetime en base al tz del usuario, este cambio nos puede ayudar a utilizar esa función para otros modelos y no solo para invoice.

Realiza ese ajuste y me informas cuando quede para poder generar los test yaml

review: Needs Fixing
1117. By Luis Ernesto García Medina - http://www.vauxoo.com

[IMP][invoice_datetime] use function _get_datetime_with_user_tz to get datetime of any field with user time zone

Revision history for this message
Jose Antonio Morales Ponce(vauxoo) - - http://www.vauxoo.com (josemoralesp) :
Revision history for this message
Jose Antonio Morales Ponce(vauxoo) - - http://www.vauxoo.com (josemoralesp) wrote :

The create method has an error because, you try to call a method and send the ids field and this field is not defined in our method

review: Needs Fixing
1118. By Luis Ernesto García Medina - http://www.vauxoo.com

[FIX][invoice_datetime] fixed error with "ids" variable is not available on function create

1119. By Luis Ernesto García Medina - http://www.vauxoo.com

[REF][invoice_datetime] validated that when field date is False, the function return False

1120. By Luis Ernesto García Medina - http://www.vauxoo.com

[REF][invoice_datetime] tests are added to validate that the date stored in the invoice corresponds to the date calculated in _get_datetime_with_user_tz function.

1121. By Luis Ernesto García Medina - http://www.vauxoo.com

[REF][invoice_datetime] change test to validate datetime with two time zones

1122. By Luis Ernesto García Medina - http://www.vauxoo.com

[Merge] merge with lp:addons-vauxoo/7.0

1123. By Jorge Angel Naranjo Rogel - http://www.vauxoo.com

[MERGE] Merge from lp:addons-vauxoo/7.0

1124. By Luis Ernesto García Medina - http://www.vauxoo.com

[MERGE] merge with lp:~vauxoo/addons-vauxoo/7.0-addons-vauxoo-pylint-datetime-dev-alan to apply pylint and flake8

1125. By Luis Ernesto García Medina - http://www.vauxoo.com

[FIX][invoice_datetime] fix errors with merge with parent

1126. By Luis Ernesto García Medina - http://www.vauxoo.com

[autopep8, pylint][invoice_datetime]

1127. By Luis Ernesto García Medina - http://www.vauxoo.com

[FIX][invoice_datetime] fix error with time of invoice

1128. By Luis Ernesto García Medina - http://www.vauxoo.com

[REF][invoice_datetime] applied flake8 and pylint

Revision history for this message
Jorge Angel Naranjo Rogel - http://www.vauxoo.com (jorge-nr) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'invoice_datetime/__init__.py'
2--- invoice_datetime/__init__.py 2014-10-03 00:28:23 +0000
3+++ invoice_datetime/__init__.py 2014-11-18 22:23:17 +0000
4@@ -7,7 +7,8 @@
5 # info Vauxoo (info@vauxoo.com)
6 ############################################################################
7 # Coded by: moylop260 (moylop260@vauxoo.com)
8-# Launchpad Project Manager for Publication: Nhomar Hernandez - nhomar@vauxoo.com
9+# Launchpad Project Manager for Publication:
10+# Nhomar Hernandez - nhomar@vauxoo.com
11 ############################################################################
12 #
13 # This program is free software: you can redistribute it and/or modify
14@@ -27,4 +28,3 @@
15
16 from . import invoice
17 from . import res_config
18-
19
20=== modified file 'invoice_datetime/__openerp__.py'
21--- invoice_datetime/__openerp__.py 2014-10-03 08:22:36 +0000
22+++ invoice_datetime/__openerp__.py 2014-11-18 22:23:17 +0000
23@@ -6,8 +6,10 @@
24 # All Rights Reserved.
25 # info Vauxoo (info@vauxoo.com)
26 ############################################################################
27-# Coded by: moylop260 (moylop260@vauxoo.com) modified by carlosecv74@gmail.com
28-# Launchpad Project Manager for Publication: Nhomar Hernandez - nhomar@vauxoo.com
29+# Coded by: moylop260 (moylop260@vauxoo.com)
30+# modified by carlosecv74@gmail.com
31+# Launchpad Project Manager for Publication:
32+# Nhomar Hernandez - nhomar@vauxoo.com
33 ############################################################################
34 #
35 # This program is free software: you can redistribute it and/or modify
36@@ -25,28 +27,28 @@
37 #
38 ##############################################################################
39 {
40- "name": "invoice_datetime",
41- "version": "1.0",
42- "author": "Vauxoo",
43- "category": "Localization/Mexico",
44+ "name": "invoice_datetime",
45+ "version": "1.0",
46+ "author": "Vauxoo",
47+ "category": "Localization/Mexico",
48 "description": """Add new field invoice_datetime for electronic invoice data
49 and add hour & minute to field date_invoice
50- """,
51- "website": "http://www.vauxoo.com/",
52- "license": "AGPL-3",
53+ """,
54+ "website": "http://www.vauxoo.com/",
55+ "license": "AGPL-3",
56 "depends": [
57 "account"
58- ],
59- "demo": [],
60+ ],
61+ "demo": [],
62 "data": [
63- "account_invoice_view.xml",
64+ "account_invoice_view.xml",
65 "res_config_acc_view.xml"
66- ],
67- "test": [],
68- "js": [],
69- "css": [],
70- "qweb": [],
71- "installable": True,
72- "auto_install": False,
73+ ],
74+ "test": [],
75+ "js": [],
76+ "css": [],
77+ "qweb": [],
78+ "installable": True,
79+ "auto_install": False,
80 "active": False
81-}
82\ No newline at end of file
83+}
84
85=== modified file 'invoice_datetime/invoice.py'
86--- invoice_datetime/invoice.py 2014-11-05 16:41:06 +0000
87+++ invoice_datetime/invoice.py 2014-11-18 22:23:17 +0000
88@@ -7,7 +7,8 @@
89 # info Vauxoo (info@vauxoo.com)
90 ############################################################################
91 # Coded by: moylop260 (moylop260@vauxoo.com)
92-# Launchpad Project Manager for Publication: Nhomar Hernandez - nhomar@vauxoo.com
93+# Launchpad Project Manager for Publication:
94+# * Nhomar Hernandez - nhomar@vauxoo.com
95 ############################################################################
96 #
97 # This program is free software: you can redistribute it and/or modify
98@@ -28,8 +29,8 @@
99 from openerp.osv import fields, osv
100 from openerp.tools.translate import _
101 from openerp import tools
102-from openerp import release
103 import datetime
104+from datetime import timedelta
105 from pytz import timezone
106 import pytz
107 import time
108@@ -47,61 +48,94 @@
109 date_ref, '%Y-%m-%d %H:%M:%S'))
110 except BaseException:
111 pass
112- return super(account_payment_term, self).compute(cr, uid, ids, value,
113- date_ref, context=context)
114+ return super(account_payment_term, self).compute(
115+ cr, uid, ids, value, date_ref, context=context)
116
117
118 class account_invoice(osv.Model):
119 _inherit = 'account.invoice'
120- #_order = 'invoice_datetime asc'
121-
122- def _get_date_invoice_tz(self, cr, uid, ids, field_names=None, arg=False, context=None):
123- if context is None:
124- context = {}
125- res = {}
126- if release.version >= '6':
127- tz = self.pool.get('res.users').browse(cr, uid, uid).tz
128- for invoice in self.browse(cr, uid, ids, context=context):
129- res[invoice.id] = invoice.invoice_datetime and tools.\
130- server_to_local_timestamp(invoice.invoice_datetime,
131- tools.DEFAULT_SERVER_DATETIME_FORMAT,
132- tools.DEFAULT_SERVER_DATETIME_FORMAT, tz) or False
133- elif release.version < '6':
134- # TODO: tz change for openerp5
135- for invoice in self.browse(cr, uid, ids, context=context):
136- res[invoice.id] = invoice.date_invoice
137- return res
138+ # _order = 'invoice_datetime asc'
139
140 def _get_field_params(self, cr, uid, ids, name, unknow_none, context=None):
141 if context is None:
142 context = {}
143+ account_setting_obj = self.pool.get("account.config.settings")
144 ids = isinstance(ids, (int, long)) and [ids] or ids
145 res = {}
146- key_by_company_id = "acc_invoice.date_invoice_type_" + str(self.pool.get("account.config.settings")._default_company(cr, uid))
147- res[ids[0]] = self.pool.get("ir.config_parameter").get_param(cr, uid, key_by_company_id, default='date', context=context)
148+ key_by_company_id = "acc_invoice.date_invoice_type_" + \
149+ str(account_setting_obj._default_company(cr, uid))
150+ res[ids[0]] = self.pool.get("ir.config_parameter").get_param(
151+ cr, uid, key_by_company_id, default='date', context=context)
152 return res
153
154 _columns = {
155 # Extract date_invoice from original, but add datetime
156- #'date_invoice': fields.datetime('Date Invoiced', states={'open':[
157- #('readonly',True)],'close':[('readonly',True)]},
158+ # 'date_invoice': fields.datetime('Date Invoiced', states={'open':[
159+ # ('readonly',True)],'close':[('readonly',True)]},
160 # help="Keep empty to use the current date"),
161- 'invoice_datetime': fields.datetime('Date time of invoice',
162- states={'open': [('readonly', True)], 'close': [('readonly', True)]},
163+ 'invoice_datetime': fields.datetime(
164+ 'Date time of invoice',
165+ states={
166+ 'open': [('readonly', True)],
167+ 'close': [('readonly', True)]},
168 help="Keep empty to use the current date"),
169- 'date_invoice_tz': fields.function(_get_date_invoice_tz, method=True,
170- type='datetime', string='Date Invoiced with TZ', store=True,
171+ 'date_invoice_tz': fields.datetime(
172+ string='Date Invoiced with TZ',
173 help='Date of Invoice with Time Zone'),
174- 'date_type': fields.function(_get_field_params, storage=False, type='char', string="Date type")
175+ 'date_type': fields.function(_get_field_params, storage=False,
176+ type='char', string="Date type")
177 }
178
179+ def _get_datetime_with_user_tz(self, cr, uid, datetime_invoice=False,
180+ context=None):
181+ datetime_inv_tz = False
182+ if datetime_invoice:
183+ time_tz = self.pool.get("res.users").read(
184+ cr, uid, uid, ["tz_offset"], context=context).get("tz_offset")
185+ hours_tz = int(time_tz[:-2])
186+ minut_tz = int(time_tz[-2:])
187+ if time_tz[0] == '-':
188+ minut_tz = minut_tz * -1
189+ datetime_inv_tz = (
190+ datetime_invoice + timedelta(
191+ hours=hours_tz,
192+ minutes=minut_tz)).strftime('%Y-%m-%d %H:%M:%S')
193+ return datetime_inv_tz
194+
195+ def create(self, cr, uid, vals, context=None):
196+ if 'invoice_datetime' in vals.keys():
197+ datetime_inv = vals.get("invoice_datetime") and \
198+ datetime.datetime.strptime(
199+ vals.get("invoice_datetime"), "%Y-%m-%d %H:%M:%S") or False
200+ if datetime_inv:
201+ vals.update(
202+ {'date_invoice_tz': self._get_datetime_with_user_tz(
203+ cr, uid, datetime_inv)})
204+ return super(account_invoice, self).create(
205+ cr, uid, vals, context=context)
206+
207+ def write(self, cr, uid, ids, vals, context=None):
208+ for invoice in self.browse(cr, uid, ids, context=context):
209+ datetime_inv = invoice.invoice_datetime and \
210+ datetime.datetime.strptime(invoice.invoice_datetime,
211+ "%Y-%m-%d %H:%M:%S") or False
212+ if datetime_inv:
213+ vals.update(
214+ {'date_invoice_tz': self._get_datetime_with_user_tz(
215+ cr, uid, datetime_inv)})
216+ return super(account_invoice, self).write(
217+ cr, uid, ids, vals, context=context)
218+
219 def _get_default_type(self, cr, uid, ids):
220- key_by_company_id = "acc_invoice.date_invoice_type_" + str(self.pool.get("account.config.settings")._default_company(cr, uid))
221- type_show_date = self.pool.get("ir.config_parameter").get_param(cr, uid, key_by_company_id, default='date')
222+ account_setting_obj = self.pool.get("account.config.settings")
223+ key_by_company_id = "acc_invoice.date_invoice_type_" + \
224+ str(account_setting_obj._default_company(cr, uid))
225+ type_show_date = self.pool.get("ir.config_parameter").get_param(
226+ cr, uid, key_by_company_id, default='date')
227 return type_show_date
228
229 _defaults = {
230- #'date_invoice': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
231+ # 'date_invoice': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
232 "date_type": _get_default_type
233 }
234
235@@ -111,7 +145,8 @@
236 if default is None:
237 default = {}
238 default.update({'invoice_datetime': False, 'date_invoice': False})
239- return super(account_invoice, self).copy(cr, uid, ids, default, context)
240+ return super(account_invoice, self).copy(
241+ cr, uid, ids, default, context)
242
243 def _get_time_zone(self, cr, uid, invoice_id, context=None):
244 if context is None:
245@@ -123,8 +158,9 @@
246 hours = timezone(userstz)
247 fmt = '%Y-%m-%d %H:%M:%S %Z%z'
248 now = datetime.datetime.now()
249- loc_dt = hours.localize(datetime.datetime(now.year, now.month, now.day,
250- now.hour, now.minute, now.second))
251+ loc_dt = hours.localize(datetime.datetime(now.year, now.month,
252+ now.day, now.hour,
253+ now.minute, now.second))
254 timezone_loc = (loc_dt.strftime(fmt))
255 diff_timezone_original = timezone_loc[-5:-2]
256 timezone_original = int(diff_timezone_original)
257@@ -155,9 +191,10 @@
258
259 if values.get('invoice_datetime', False) and not\
260 values.get('date_invoice', False):
261- date_invoice = fields.datetime.context_timestamp(cr, uid,
262- datetime.datetime.strptime(values['invoice_datetime'],
263- tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context)
264+ date_invoice = fields.datetime.context_timestamp(
265+ cr, uid, datetime.datetime.strptime(
266+ values['invoice_datetime'],
267+ tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context)
268 res['date_invoice'] = date_invoice
269 res['invoice_datetime'] = values['invoice_datetime']
270
271@@ -168,31 +205,39 @@
272 '%Y-%m-%d %H:%M:%S').date().strftime('%Y-%m-%d')
273 if date_invoice != values['date_invoice']:
274 if self.browse(cr, uid, ids)[0].date_type == 'datetime':
275- date_invoice = fields.datetime.context_timestamp(cr, uid,
276- datetime.datetime.strptime(values['invoice_datetime'],
277- tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context)
278+ date_invoice = fields.datetime.context_timestamp(
279+ cr, uid, datetime.datetime.strptime(
280+ values['invoice_datetime'],
281+ tools.DEFAULT_SERVER_DATETIME_FORMAT),
282+ context=context)
283 res['date_invoice'] = date_invoice
284 res['invoice_datetime'] = values['invoice_datetime']
285 elif self.browse(cr, uid, ids)[0].date_type == 'date':
286- user_hour = self._get_time_zone(cr, uid, [], context=context)
287+ user_hour = self._get_time_zone(
288+ cr, uid, [], context=context)
289 time_invoice = datetime.time(abs(user_hour), 0, 0)
290
291 date_invoice = datetime.datetime.strptime(
292 values['date_invoice'], '%Y-%m-%d').date()
293
294 dt_invoice = datetime.datetime.combine(
295- date_invoice, time_invoice).strftime('%Y-%m-%d %H:%M:%S')
296+ date_invoice,
297+ time_invoice).strftime('%Y-%m-%d %H:%M:%S')
298
299 res['invoice_datetime'] = dt_invoice
300 res['date_invoice'] = values['date_invoice']
301 else:
302- raise osv.except_osv(_('Warning!'), _('Invoice dates should be equal'))
303- #~ else:
304- #~ raise osv.except_osv(_('Warning!'), _('Invoice dates should be equal'))
305+ raise osv.except_osv(
306+ _('Warning!'),
307+ _('Invoice dates should be equal'))
308+ # ~ else:
309+ # ~ raise osv.except_osv(_('Warning!'),
310+ # _('Invoice dates should be equal'))
311
312- if not values.get('invoice_datetime', False) and\
313+ if not values.get('invoice_datetime', False) and\
314 not values.get('date_invoice', False):
315- res['date_invoice'] = fields.date.context_today(self, cr, uid, context=context)
316+ res['date_invoice'] = fields.date.context_today(
317+ self, cr, uid, context=context)
318 res['invoice_datetime'] = fields.datetime.now()
319
320 return res
321@@ -200,9 +245,9 @@
322 def action_move_create(self, cr, uid, ids, context=None):
323 for inv in self.browse(cr, uid, ids, context=context):
324 if inv.type in ('out_invoice', 'out_refund'):
325- vals_date = self.assigned_datetime(cr, uid, ids,
326- {'invoice_datetime': inv.invoice_datetime,
327- 'date_invoice': inv.date_invoice},
328+ vals_date = self.assigned_datetime(
329+ cr, uid, ids, {'invoice_datetime': inv.invoice_datetime,
330+ 'date_invoice': inv.date_invoice},
331 context=context)
332 self.write(cr, uid, ids, vals_date, context=context)
333 return super(account_invoice,
334
335=== modified file 'invoice_datetime/res_config.py'
336--- invoice_datetime/res_config.py 2014-10-03 07:19:35 +0000
337+++ invoice_datetime/res_config.py 2014-11-18 22:23:17 +0000
338@@ -31,29 +31,45 @@
339
340 _columns = {
341 'groups_id': fields.many2one('res.groups', 'Category',),
342- 'select_date': fields.selection([('date', 'Date'), ('datetime', 'Datetime')], string="Default date of invoices", help="Select the field you want to displayed in the invoice for the selected company."),
343+ 'select_date': fields.selection([('date', 'Date'), (
344+ 'datetime', 'Datetime')],
345+ string="Default date of invoices",
346+ help="Select the field you want to displayed in the \
347+ invoice for the selected company."),
348 }
349
350 def get_default_select_date(self, cr, uid, fields_name, context=None):
351- key_by_company_id = "acc_invoice.date_invoice_type_" + str(self._default_company(cr, uid))
352- type_show_date = self.pool.get("ir.config_parameter").get_param(cr, uid, key_by_company_id, default='date', context=context)
353+ key_by_company_id = "acc_invoice.date_invoice_type_" + str(
354+ self._default_company(cr, uid))
355+ type_show_date = self.pool.get("ir.config_parameter").get_param(
356+ cr, uid, key_by_company_id, default='date', context=context)
357 return {'select_date': type_show_date}
358
359 def set_default_select_date(self, cr, uid, ids, context=None):
360 """ set default sale and purchase taxes for products """
361- if uid != SUPERUSER_ID and not self.pool['res.users'].has_group(cr, uid, 'base.group_erp_manager'):
362- raise openerp.exceptions.AccessError(_("Only administrators can change the settings"))
363+ if uid != SUPERUSER_ID and not self.pool['res.users'].has_group(
364+ cr, uid, 'base.group_erp_manager'):
365+ raise openerp.exceptions.AccessError(
366+ _("Only administrators can change the settings"))
367 config_parameters = self.pool.get("ir.config_parameter")
368 for record in self.browse(cr, uid, ids, context=context):
369- company_id = str(record.company_id and record.company_id.id or self._default_company(cr, uid))
370+ company_id = str(record.company_id and
371+ record.company_id.id or
372+ self._default_company(cr, uid))
373 key_by_company_id = "acc_invoice.date_invoice_type_" + company_id
374- config_parameters.set_param(cr, uid, key_by_company_id, record.select_date or '', context=context)
375+ config_parameters.set_param(
376+ cr, uid, key_by_company_id, record.select_date or '',
377+ context=context)
378
379 def onchange_company_id(self, cr, uid, ids, company_id, context=None):
380- result = super(account_config_settings, self).onchange_company_id(cr, uid, ids, company_id, context=context)
381+ result = super(account_config_settings, self).onchange_company_id(
382+ cr, uid, ids, company_id, context=context)
383 type_date = False
384 type_date = result.get('value', {})
385- key_by_company_id = "acc_invoice.date_invoice_type_" + str(company_id or self._default_company(cr, uid))
386- type_date['select_date'] = self.pool.get("ir.config_parameter").get_param(cr, uid, key_by_company_id, default='date', context=context)
387+ key_by_company_id = "acc_invoice.date_invoice_type_" + str(
388+ company_id or self._default_company(cr, uid))
389+ type_date['select_date'] = self.pool.get(
390+ "ir.config_parameter").get_param(
391+ cr, uid, key_by_company_id, default='date', context=context)
392 result.update({'value': type_date})
393 return result
394
395=== added directory 'invoice_datetime/tests'
396=== added file 'invoice_datetime/tests/__init__.py'
397--- invoice_datetime/tests/__init__.py 1970-01-01 00:00:00 +0000
398+++ invoice_datetime/tests/__init__.py 2014-11-18 22:23:17 +0000
399@@ -0,0 +1,27 @@
400+#!/usr/bin/python
401+# -*- encoding: utf-8 -*-
402+#
403+# Module Writen to OpenERP, Open Source Management Solution
404+#
405+# Copyright (c) 2014 Vauxoo - http://www.vauxoo.com/
406+# All Rights Reserved.
407+# info Vauxoo (info@vauxoo.com)
408+#
409+# Coded by: vauxoo consultores (info@vauxoo.com)
410+#
411+#
412+# This program is free software: you can redistribute it and/or modify
413+# it under the terms of the GNU Affero General Public License as
414+# published by the Free Software Foundation, either version 3 of the
415+# License, or (at your option) any later version.
416+#
417+# This program is distributed in the hope that it will be useful,
418+# but WITHOUT ANY WARRANTY; without even the implied warranty of
419+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
420+# GNU Affero General Public License for more details.
421+#
422+# You should have received a copy of the GNU Affero General Public License
423+# along with this program. If not, see <http://www.gnu.org/licenses/>.
424+#
425+
426+from . import test_invoice_datetime
427
428=== added file 'invoice_datetime/tests/test_invoice_datetime.py'
429--- invoice_datetime/tests/test_invoice_datetime.py 1970-01-01 00:00:00 +0000
430+++ invoice_datetime/tests/test_invoice_datetime.py 2014-11-18 22:23:17 +0000
431@@ -0,0 +1,89 @@
432+#!/usr/bin/python
433+# -*- encoding: utf-8 -*-
434+#
435+# Module Writen to OpenERP, Open Source Management Solution
436+#
437+# Copyright (c) 2014 Vauxoo - http://www.vauxoo.com/
438+# All Rights Reserved.
439+# info Vauxoo (info@vauxoo.com)
440+#
441+# Coded by: vauxoo consultores (info@vauxoo.com)
442+#
443+#
444+# This program is free software: you can redistribute it and/or modify
445+# it under the terms of the GNU Affero General Public License as
446+# published by the Free Software Foundation, either version 3 of the
447+# License, or (at your option) any later version.
448+#
449+# This program is distributed in the hope that it will be useful,
450+# but WITHOUT ANY WARRANTY; without even the implied warranty of
451+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
452+# GNU Affero General Public License for more details.
453+#
454+# You should have received a copy of the GNU Affero General Public License
455+# along with this program. If not, see <http://www.gnu.org/licenses/>.
456+#
457+
458+from openerp.tests.common import TransactionCase
459+from openerp.tools import mute_logger
460+import time
461+import datetime
462+import logging
463+_logger = logging.getLogger(__name__)
464+
465+
466+class TestInvoiceDatetime(TransactionCase):
467+ def setUp(self):
468+ super(TestInvoiceDatetime, self).setUp()
469+ self.user = self.registry('res.users')
470+ self.data = self.registry('ir.model.data')
471+ self.invoice = self.registry('account.invoice')
472+
473+ @mute_logger('openerp.addons.base.ir.ir_model', 'openerp.osv.orm')
474+ def test_get_datetime(self):
475+ cr, uid = self.cr, self.uid
476+ # get datetime of server in string
477+ dt_server = time.strftime('%Y-%m-%d %H:%M:%S')
478+ # I generate object of type datetime
479+ # to send at function _get_datetime_with_user_tz
480+ datetime_object = datetime.datetime.strptime(dt_server,
481+ '%Y-%m-%d %H:%M:%S')
482+ # create list with tz to apply in datetime
483+ self.user.write(cr, uid, uid, {'tz': 'Europe/Rome'})
484+ # Assign tz to user
485+ # call the function _get_datetime_with_user_tz
486+ # to get the date with tz applied
487+ datetime_tz_rome = self.invoice._get_datetime_with_user_tz(
488+ cr, uid, datetime_object)
489+ # Create an invoice with last tz of cycle America/Mexico_City
490+ invoice_tz_rome_id = self.invoice.create(
491+ cr, uid,
492+ {'partner_id': 1,
493+ 'account_id': 1,
494+ 'invoice_datetime': dt_server})
495+ self.user.write(cr, uid, uid, {'tz': 'America/Mexico_City'})
496+ # Assign tz to user
497+ datetime_tz_mx = self.invoice._get_datetime_with_user_tz(
498+ cr, uid, datetime_object)
499+ invoice_tz_mx_id = self.invoice.create(
500+ cr, uid,
501+ {'partner_id': 1,
502+ 'account_id': 1,
503+ 'invoice_datetime': dt_server})
504+ dt_inv_tz_rome = self.invoice.read(
505+ cr, uid, invoice_tz_rome_id, []).get('date_invoice_tz')
506+ dt_inv_tz_mx = self.invoice.read(
507+ cr, uid, invoice_tz_mx_id, []).get('date_invoice_tz')
508+ _logger.info("Validate datetime of function, for Rome-Mexico_City TZ")
509+ self.assertNotEqual(
510+ datetime_tz_rome,
511+ datetime_tz_mx, 'Dates are equal, should be different')
512+ _logger.info("Validate datetime of function versus invoice datetime")
513+ self.assertEquals(datetime_tz_rome, dt_inv_tz_rome,
514+ 'Date calculated and date of invoice are not equals')
515+ self.assertEquals(datetime_tz_mx, dt_inv_tz_mx,
516+ 'Date calculated and date of invoice are not equals')
517+ _logger.info("Validate invoice datetime, for Rome-Mexico_City TZ")
518+ self.assertNotEqual(
519+ dt_inv_tz_rome,
520+ dt_inv_tz_mx, 'Dates are equal, must be different on invoices')