[FIX] default_get corrected
[odoo/odoo.git] / addons / hr_timesheet / hr_timesheet.py
index 9950a1d..fc120aa 100644 (file)
@@ -1,26 +1,26 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
 #
-#    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
+#    You should have received a copy of the GNU Affero General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
 import time
+
 from osv import fields
 from osv import osv
 from osv.orm import except_orm
@@ -30,7 +30,7 @@ class hr_employee(osv.osv):
     _name = "hr.employee"
     _inherit = "hr.employee"
     _columns = {
-        'product_id': fields.many2one('product.product', 'Product'),
+        'product_id': fields.many2one('product.product', 'Product', help="Specifies employee's designation as a product with type 'service'."),
         'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal')
     }
 hr_employee()
@@ -43,7 +43,8 @@ class hr_analytic_timesheet(osv.osv):
     _inherits = {'account.analytic.line': 'line_id'}
     _order = "id desc"
     _columns = {
-        'line_id' : fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade'),
+        'line_id' : fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade'), 
+        'partner_id': fields.related('account_id', 'partner_id', type='many2one', string='Partner Id',relation='account.analytic.account',store=True),
     }
 
     def unlink(self, cr, uid, ids, context={}):
@@ -58,7 +59,9 @@ class hr_analytic_timesheet(osv.osv):
         res = {}
 #        if prod_id and unit_amount:
         if prod_id:
-            res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount,unit, context)
+            # find company
+            company_id=self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context=context)
+            res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount,company_id,unit, context)
         return res
 
     def _getEmployeeProduct(self, cr, uid, context):
@@ -83,7 +86,7 @@ class hr_analytic_timesheet(osv.osv):
         emp_obj = self.pool.get('hr.employee')
         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
         if emp_id:
-            emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
+            emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
             if bool(emp.product_id):
                 a =  emp.product_id.product_tmpl_id.property_account_expense.id
                 if not a:
@@ -96,7 +99,7 @@ class hr_analytic_timesheet(osv.osv):
         emp_obj = self.pool.get('hr.employee')
         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
         if emp_id:
-            emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
+            emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
             if emp.journal_id:
                 return emp.journal_id.id
         return False
@@ -112,7 +115,7 @@ class hr_analytic_timesheet(osv.osv):
     }
     def on_change_account_id(self, cr, uid, ids, account_id):
         return {'value':{}}
-    
+
     def on_change_date(self, cr, uid, ids, date):
         if ids:
             new_date = self.read(cr,uid,ids[0],['date'])['date']
@@ -120,20 +123,18 @@ class hr_analytic_timesheet(osv.osv):
                 warning = {'title':'User Alert!','message':'Changing the date will let this entry appear in the timesheet of the new date.'}
                 return {'value':{},'warning':warning}
         return {'value':{}}
-    
+
     def create(self, cr, uid, vals, context={}):
-        try:
-            res = super(hr_analytic_timesheet, self).create(cr, uid, vals, context)
-            return res
-        except Exception,e:
-            if '"journal_id" viol' in e.args[0]:
-                raise except_orm(_('ValidateError'),
-                     _('No analytic journal available for this employee.\nDefine an employee for the selected user and assign an analytic journal.'))
-            elif '"account_id" viol' in e.args[0]:
-                raise except_orm(_('ValidateError'),
-                     _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
-            else:
-                raise except_orm(_('UnknownError'), str(e))
+        emp_obj = self.pool.get('hr.employee')
+        emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
+        ename = ''
+        if emp_id:
+            ename = emp_obj.browse(cr, uid, emp_id[0], context=context).name
+        if not vals.get('journal_id',False):
+           raise osv.except_osv(_('Warning !'), _('Analytic journal is not defined for employee %s \nDefine an employee for the selected user and assign an analytic journal!')%(ename,))
+        if not vals.get('account_id',False):
+           raise osv.except_osv(_('Warning !'), _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
+        return super(hr_analytic_timesheet, self).create(cr, uid, vals, context=context)
 
     def on_change_user_id(self, cr, uid, ids, user_id):
         if not user_id: