[ADD]: Images of sale_crm, sale_journal, sale_layout, sale_margin and sale_mrp modules.
[odoo/odoo.git] / addons / project_timesheet / project_timesheet.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 import time
22 import datetime
23
24 from osv import fields, osv
25 import pooler
26 import tools
27 from tools.translate import _
28
29 class project_project(osv.osv):
30     _inherit = 'project.project'
31     
32 project_project()
33
34 class project_work(osv.osv):
35     _inherit = "project.task.work"
36
37     def get_user_related_details(self, cr, uid, user_id):
38         res = {}
39         emp_obj = self.pool.get('hr.employee')
40         emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_id)])
41         if not emp_id:
42             user_name = self.pool.get('res.users').read(cr, uid, [user_id], ['name'])[0]['name']
43             raise osv.except_osv(_('Bad Configuration !'),
44                  _('No employee defined for user "%s". You must create one.')% (user_name,))
45         emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0])
46         if not emp.product_id:
47             raise osv.except_osv(_('Bad Configuration !'),
48                  _('No product defined on the related employee.\nFill in the timesheet tab of the employee form.'))
49
50         if not emp.journal_id:
51             raise osv.except_osv(_('Bad Configuration !'),
52                  _('No journal defined on the related employee.\nFill in the timesheet tab of the employee form.'))
53
54         a = emp.product_id.product_tmpl_id.property_account_expense.id
55         if not a:
56             a = emp.product_id.categ_id.property_account_expense_categ.id
57             if not a:
58                 raise osv.except_osv(_('Bad Configuration !'),
59                         _('No product and product category property account defined on the related employee.\nFill in the timesheet tab of the employee form.'))
60         res['product_id'] = emp.product_id.id
61         res['journal_id'] = emp.journal_id.id
62         res['general_account_id'] = a
63         res['product_uom_id'] = emp.product_id.uom_id.id
64         return res
65
66     def create(self, cr, uid, vals, *args, **kwargs):
67         obj_timesheet = self.pool.get('hr.analytic.timesheet')
68         project_obj = self.pool.get('project.project')
69         task_obj = self.pool.get('project.task')
70         uom_obj = self.pool.get('product.uom')
71         
72         vals_line = {}
73         context = kwargs.get('context', {})
74         if not context.get('no_analytic_entry',False):
75             obj_task = task_obj.browse(cr, uid, vals['task_id'])
76             result = self.get_user_related_details(cr, uid, vals.get('user_id', uid))
77             vals_line['name'] = '%s: %s' % (tools.ustr(obj_task.name), tools.ustr(vals['name']) or '/')
78             vals_line['user_id'] = vals['user_id']
79             vals_line['product_id'] = result['product_id']
80             vals_line['date'] = vals['date'][:10]
81             
82             #calculate quantity based on employee's product's uom 
83             vals_line['unit_amount'] = vals['hours']
84
85             default_uom = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id.id
86             if result['product_uom_id'] != default_uom:
87                 vals_line['unit_amount'] = uom_obj._compute_qty(cr, uid, default_uom, vals['hours'], result['product_uom_id'])
88             acc_id = obj_task.project_id and obj_task.project_id.analytic_account_id.id or False
89             if acc_id:
90                 vals_line['account_id'] = acc_id
91                 res = obj_timesheet.on_change_account_id(cr, uid, False, acc_id)
92                 if res.get('value'):
93                     vals_line.update(res['value'])
94                 vals_line['general_account_id'] = result['general_account_id']
95                 vals_line['journal_id'] = result['journal_id']
96                 vals_line['amount'] = 0.0
97                 vals_line['product_uom_id'] = result['product_uom_id']
98                 amount = vals_line['unit_amount']
99                 prod_id = vals_line['product_id']
100                 unit = False
101                 timeline_id = obj_timesheet.create(cr, uid, vals=vals_line, context=context)
102
103                 # Compute based on pricetype
104                 amount_unit = obj_timesheet.on_change_unit_amount(cr, uid, timeline_id,
105                     prod_id, amount, False, unit, vals_line['journal_id'], context=context)
106                 if amount_unit and 'amount' in amount_unit.get('value',{}):
107                     updv = { 'amount': amount_unit['value']['amount'] }
108                     obj_timesheet.write(cr, uid, [timeline_id], updv, context=context)
109                 vals['hr_analytic_timesheet_id'] = timeline_id
110         return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
111
112     def write(self, cr, uid, ids, vals, context=None):
113         if context is None:
114             context = {}
115         timesheet_obj = self.pool.get('hr.analytic.timesheet')
116         project_obj = self.pool.get('project.project')
117         uom_obj = self.pool.get('product.uom')
118         result = {}
119         
120         if isinstance(ids, (long, int)):
121             ids = [ids,]
122
123         for task in self.browse(cr, uid, ids, context=context):
124             line_id = task.hr_analytic_timesheet_id
125             if not line_id:
126                 # if a record is deleted from timesheet, the line_id will become
127                 # null because of the foreign key on-delete=set null
128                 continue
129             vals_line = {}
130             if 'name' in vals:
131                 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
132             if 'user_id' in vals:
133                 vals_line['user_id'] = vals['user_id']
134                 result = self.get_user_related_details(cr, uid, vals['user_id'])
135                 for fld in ('product_id', 'general_account_id', 'journal_id', 'product_uom_id'):
136                     if result.get(fld, False):
137                         vals_line[fld] = result[fld]
138                         
139             if 'date' in vals:
140                 vals_line['date'] = vals['date'][:10]
141             if 'hours' in vals:
142                 default_uom = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id.id
143                 vals_line['unit_amount'] = vals['hours']
144                 prod_id = vals_line.get('product_id', line_id.product_id.id) # False may be set
145
146                 if result.get('product_uom_id',False) and (not result['product_uom_id'] == default_uom):
147                     vals_line['unit_amount'] = uom_obj._compute_qty(cr, uid, default_uom, vals['hours'], result['product_uom_id'])
148                     
149                 # Compute based on pricetype
150                 amount_unit = timesheet_obj.on_change_unit_amount(cr, uid, line_id.id,
151                     prod_id=prod_id, company_id=False,
152                     unit_amount=vals_line['unit_amount'], unit=False, journal_id=vals_line['journal_id'], context=context)
153
154                 if amount_unit and 'amount' in amount_unit.get('value',{}):
155                     vals_line['amount'] = amount_unit['value']['amount']
156
157             self.pool.get('hr.analytic.timesheet').write(cr, uid, [line_id.id], vals_line, context=context)
158             
159         return super(project_work,self).write(cr, uid, ids, vals, context)
160
161     def unlink(self, cr, uid, ids, *args, **kwargs):
162         hat_obj = self.pool.get('hr.analytic.timesheet')
163         hat_ids = []
164         for task in self.browse(cr, uid, ids):
165             if task.hr_analytic_timesheet_id:
166                 hat_ids.append(task.hr_analytic_timesheet_id.id)
167 #            delete entry from timesheet too while deleting entry to task.
168         if hat_ids:
169             hat_obj.unlink(cr, uid, hat_ids, *args, **kwargs)
170         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
171
172     _columns={
173         'hr_analytic_timesheet_id':fields.many2one('hr.analytic.timesheet','Related Timeline Id', ondelete='set null'),
174     }
175
176 project_work()
177
178 class task(osv.osv):
179     _inherit = "project.task"
180
181     def unlink(self, cr, uid, ids, *args, **kwargs):
182         for task_obj in self.browse(cr, uid, ids, *args, **kwargs):
183             if task_obj.work_ids:
184                 work_ids = [x.id for x in task_obj.work_ids]
185                 self.pool.get('project.task.work').unlink(cr, uid, work_ids, *args, **kwargs)
186
187         return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
188
189     def write(self, cr, uid, ids,vals,context=None):
190         if context is None:
191             context = {}
192         if vals.get('project_id',False) or vals.get('name',False):
193             vals_line = {}
194             hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet')
195             task_obj_l = self.browse(cr, uid, ids, context=context)
196             if vals.get('project_id',False):
197                 project_obj = self.pool.get('project.project').browse(cr, uid, vals['project_id'], context=context)
198                 acc_id = project_obj.analytic_account_id.id
199
200             for task_obj in task_obj_l:
201                 if len(task_obj.work_ids):
202                     for task_work in task_obj.work_ids:
203                         if not task_work.hr_analytic_timesheet_id:
204                             continue
205                         line_id = task_work.hr_analytic_timesheet_id.id
206                         if vals.get('project_id',False):
207                             vals_line['account_id'] = acc_id
208                         if vals.get('name',False):
209                             vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/')
210                         hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {})
211         return super(task,self).write(cr, uid, ids, vals, context)
212
213 task()
214
215 class res_partner(osv.osv):
216     _inherit = 'res.partner'
217     def unlink(self, cursor, user, ids, context=None):
218         parnter_id=self.pool.get('project.project').search(cursor, user, [('partner_id', 'in', ids)])
219         if parnter_id:
220             raise osv.except_osv(_('Invalid action !'), _('Cannot delete Partner which is Assigned to project  !'))            
221         return super(res_partner,self).unlink(cursor, user, ids,
222                 context=context)
223 res_partner()
224
225 class account_analytic_line(osv.osv):
226    _inherit = "account.analytic.line"
227    def on_change_account_id(self, cr, uid, ids, account_id):
228        res = {}
229        if not account_id:
230            return res
231        res.setdefault('value',{})
232        acc = self.pool.get('account.analytic.account').browse(cr, uid, account_id)
233        st = acc.to_invoice.id
234        res['value']['to_invoice'] = st or False
235        return res  
236 account_analytic_line()
237 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: