[FIX] account move entry should be created with 0.0 value when cost price is 0.0
[odoo/odoo.git] / addons / project_caldav / project_caldav.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from caldav import calendar
24 from datetime import datetime
25 from tools.translate import _
26 from base_calendar import base_calendar
27 from project.project import task as base_project_task
28
29 class project_task(osv.osv):
30     _name = "project.task"
31     _inherit = ["calendar.todo", "project.task"]
32     _columns = {
33         # force inherit from project.project_task so that 
34         # calendar.todo.active is masked oute
35         'active': base_project_task._columns['active'],
36         'date_deadline': base_project_task._columns['date_deadline'],
37         'write_date': fields.datetime('Write Date'),
38         'create_date': fields.datetime('Create Date', readonly=True),
39         'attendee_ids': fields.many2many('calendar.attendee', \
40                                          'task_attendee_rel', 'task_id', 'attendee_id', 'Attendees'),
41         'state': fields.selection([('draft', 'Draft'),('open', 'In Progress'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'State', readonly=True, required=True,
42                                   help='If the task is created the state is \'Draft\'.\n If the task is started, the state becomes \'In Progress\'.\n If review is needed the task is in \'Pending\' state.\
43                                   \n If the task is over, the states is set to \'Done\'.'),
44     }
45     _defaults = {
46         'state': 'draft',
47     }
48
49
50     def open_task(self, cr, uid, ids, context=None):
51         """
52         Open Task Form for Project Task.
53         @param cr: the current row, from the database cursor,
54         @param uid: the current user’s ID for security checks,
55         @param ids: List of project task’s IDs
56         @param context: A standard dictionary for contextual values
57         @return: Dictionary value which open Project Task form.
58         """
59
60         data_pool = self.pool.get('ir.model.data')
61         value = {}
62         task_form_id = data_pool.get_object(cr, uid, 'project', 'view_task_form2')
63         task_tree_id = data_pool.get_object(cr, uid, 'project', 'view_task_tree2')
64         task_calendar_id = data_pool.get_object(cr, uid, 'project', 'view_task_calendar')
65         for id in ids:
66             value = {
67                     'name': _('Tasks'),
68                     'view_type': 'form',
69                     'view_mode': 'form,tree',
70                     'res_model': 'project.task',
71                     'view_id': False,
72                     'views': [(task_form_id, 'form'), (task_tree_id, 'tree'), (task_calendar_id, 'calendar')],
73                     'type': 'ir.actions.act_window',
74                     'res_id': base_calendar.base_calendar_id2real_id(id),
75                     'nodestroy': True
76                     }
77
78         return value
79
80
81     def import_cal(self, cr, uid, data, data_id=None, context=None):
82         todo_obj = self.pool.get('basic.calendar.todo')
83         vals = todo_obj.import_cal(cr, uid, data, context=context)
84         return self.check_import(cr, uid, vals, context=context)
85
86     def check_import(self, cr, uid, vals, context=None):
87         if context is None:
88             context = {}
89         ids = []
90         for val in vals:
91             obj_tm = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.project_time_mode_id
92             if not val.get('planned_hours', False):
93                 # 'Computes duration' in days
94                 plan = 0.0
95                 if val.get('date') and  val.get('date_deadline'):
96                     start = datetime.strptime(val['date'], '%Y-%m-%d %H:%M:%S')
97                     end = datetime.strptime(val['date_deadline'], '%Y-%m-%d %H:%M:%S')
98                     diff = end - start
99                     plan = (diff.seconds/float(86400) + diff.days) * obj_tm.factor
100                 val['planned_hours'] = plan
101             else:
102                 # Converts timedelta into hours
103                 hours = (val['planned_hours'].seconds / float(3600)) + \
104                                         (val['planned_hours'].days * 24)
105                 val['planned_hours'] = hours
106             exists, r_id = calendar.uid2openobjectid(cr, val['id'], self._name, val.get('recurrent_id'))
107             val.pop('id')
108             if exists:
109                 self.write(cr, uid, [exists], val)
110                 ids.append(exists)
111             else:
112                 #set user_id with id, needed later
113                 val.update({'user_id' : uid})
114                 task_id = self.create(cr, uid, val)
115                 ids.append(task_id)
116         return ids
117
118     def export_cal(self, cr, uid, ids, context=None):
119         if context is None:
120             context = {}
121         task_datas = self.read(cr, uid, ids, [], context ={'read': True})
122         tasks = []
123         for task in task_datas:
124             if task.get('planned_hours', None) and task.get('date_deadline', None):
125                 task.pop('planned_hours')
126             tasks.append(task)
127         todo_obj = self.pool.get('basic.calendar.todo')
128         ical = todo_obj.export_cal(cr, uid, tasks, context={'model': self._name})
129         calendar_val = ical.serialize()
130         return calendar_val.replace('"', '').strip()
131
132 project_task()
133
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: