[IMP]improve code and add tooltip on task_management
[odoo/odoo.git] / addons / analytic_contract_hr_expense / analytic_contract_hr_expense.py
index 8787809..8f24f12 100644 (file)
@@ -48,7 +48,7 @@ class account_analytic_account(osv.osv):
             total_remaining += account.remaining_expense
         return total_remaining
 
-    def _get_total_remaining(self, account):
+    def _get_total_toinvoice(self, account):
         total_toinvoice = super(account_analytic_account, self)._get_total_toinvoice(account)
         if account.charge_expenses:
             total_toinvoice += account.expense_to_invoice
@@ -58,11 +58,9 @@ class account_analytic_account(osv.osv):
         res = {}
         for account in self.browse(cr, uid, ids, context=context):
             if account.est_expenses != 0:
-                res[account.id] = account.est_expenses - account.expense_invoiced
+                res[account.id] = max(account.est_expenses - account.expense_invoiced, account.expense_to_invoice)
             else:
                 res[account.id]=0.0
-        for id in ids:
-            res[id] = round(res.get(id, 0.0),2)
         return res
 
     def _expense_to_invoice_calc(self, cr, uid, ids, name, arg, context=None):
@@ -75,39 +73,42 @@ class account_analytic_account(osv.osv):
             return res
 
         if child_ids:
-            cr.execute("SELECT hel.analytic_account, SUM(hel.unit_amount*hel.unit_quantity) \
-                    FROM hr_expense_line AS hel\
-                    LEFT JOIN hr_expense_expense AS he \
-                        ON he.id = hel.expense_id\
-                    WHERE he.state = 'invoiced' \
-                        AND hel.analytic_account IN %s \
-                    GROUP BY hel.analytic_account",(child_ids,))
+            cr.execute("""SELECT account_analytic_account.id, \
+                                COALESCE(SUM (product_template.list_price * \
+                                    account_analytic_line.unit_amount * \
+                                    ((100-hr_timesheet_invoice_factor.factor)/100)), 0.0) \
+                                    AS ca_to_invoice \
+                            FROM product_template \
+                            JOIN product_product \
+                                ON product_template.id = product_product.product_tmpl_id \
+                            JOIN account_analytic_line \
+                                ON account_analytic_line.product_id = product_product.id \
+                            JOIN account_analytic_journal \
+                                ON account_analytic_line.journal_id = account_analytic_journal.id \
+                            JOIN account_analytic_account \
+                                ON account_analytic_account.id = account_analytic_line.account_id \
+                            JOIN hr_timesheet_invoice_factor \
+                                ON hr_timesheet_invoice_factor.id = account_analytic_account.to_invoice \
+                            WHERE account_analytic_account.id IN %s \
+                                AND account_analytic_line.invoice_id IS NULL \
+                                AND account_analytic_line.to_invoice IS NOT NULL \
+                                AND account_analytic_journal.type = 'purchase' \
+                            GROUP BY account_analytic_account.id;""",(child_ids,))
             for account_id, sum in cr.fetchall():
                 res[account_id] = sum
         res_final = res
         return res_final
 
     def _expense_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
+        lines_obj = self.pool.get('account.analytic.line')
         res = {}
-        res_final = {}
-        child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
-        for i in child_ids:
-            res[i] =  0.0
-        if not child_ids:
-            return res
+        for account in self.browse(cr, uid, ids, context=context):
+            res[account.id] = 0.0
+            line_ids = lines_obj.search(cr, uid, [('account_id','=', account.id), ('invoice_id','!=',False), ('to_invoice','!=', False), ('journal_id.type', '=', 'purchase')], context=context)
+            for line in lines_obj.browse(cr, uid, line_ids, context=context):
+                res[account.id] += line.invoice_id.amount_untaxed
+        return res
 
-        if child_ids:
-            cr.execute("SELECT hel.analytic_account,SUM(hel.unit_amount*hel.unit_quantity)\
-                    FROM hr_expense_line AS hel\
-                    LEFT JOIN hr_expense_expense AS he \
-                        ON he.id = hel.expense_id\
-                    WHERE he.state = 'paid' \
-                         AND hel.analytic_account IN %s \
-                    GROUP BY hel.analytic_account",(child_ids,))
-            for account_id, sum in cr.fetchall():
-                res[account_id] = sum
-        res_final = res
-        return res_final
 
     _columns = {
         'charge_expenses' : fields.boolean('Charge Expenses'),
@@ -117,38 +118,42 @@ class account_analytic_account(osv.osv):
         'est_expenses': fields.float('Estimation of Expenses to Invoice'),
     }
 
+    def on_change_template(self, cr, uid, id, template_id, context=None):
+        res = super(account_analytic_account, self).on_change_template(cr, uid, id, template_id, context=context)
+        if template_id and 'value' in res:
+            template = self.browse(cr, uid, template_id, context=context)
+            res['value']['charge_expenses'] = template.charge_expenses
+            res['value']['est_expenses'] = template.est_expenses
+        return res
+
     def open_hr_expense(self, cr, uid, ids, context=None):
-        account = self.browse(cr, uid, ids[0], context)
-        data_obj = self.pool.get('ir.model.data')
-        try:
-            journal_id = data_obj.get_object(cr, uid, 'hr_timesheet', 'analytic_journal').id
-        except ValueError:
-            journal_id = False
-        line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account','=',account.id)])
-        id2 = data_obj._get_id(cr, uid, 'hr_expense', 'view_expenses_form')
-        id3 = data_obj._get_id(cr, uid, 'hr_expense', 'view_expenses_tree')
-        if id2:
-            id2 = data_obj.browse(cr, uid, id2, context=context).res_id
-        if id3:
-            id3 = data_obj.browse(cr, uid, id3, context=context).res_id
-        domain = [('line_ids','in',line_ids)]
+        line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account', 'in', ids)])
+        domain = [('line_ids', 'in', line_ids)]
+        names = [record.name for record in self.browse(cr, uid, ids, context=context)]
+        name = _('Expenses of %s') % ','.join(names)
         return {
             'type': 'ir.actions.act_window',
-            'name': _('Expenses'),
+            'name': name,
             'view_type': 'form',
             'view_mode': 'tree,form',
-            'views': [(id3,'tree'),(id2,'form')],
             'domain' : domain,
             'res_model': 'hr.expense.expense',
             'nodestroy': True,
         }
 
-    def hr_to_invoiced_expense(self, cr, uid, ids, context=None):
-         res = self.open_hr_expense(cr,uid,ids,context)
-         account = self.browse(cr, uid, ids[0], context)
-         line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account','=',account.id)])
-         res['domain'] = [('line_ids','in',line_ids),('state','=','invoiced')]
-         return res
+    def hr_to_invoice_expense(self, cr, uid, ids, context=None):
+        domain = [('invoice_id','=',False),('to_invoice','!=',False), ('journal_id.type', '=', 'purchase'), ('account_id', 'in', ids)]
+        names = [record.name for record in self.browse(cr, uid, ids, context=context)]
+        name = _('Expenses to Invoice of %s') % ','.join(names)
+        return {
+            'type': 'ir.actions.act_window',
+            'name': name,
+            'view_type': 'form',
+            'view_mode': 'tree,form',
+            'domain' : domain,
+            'res_model': 'account.analytic.line',
+            'nodestroy': True,
+        }
 
 account_analytic_account()