Merge from trunk
[odoo/odoo.git] / addons / hr_timesheet_invoice / hr_timesheet_invoice.py
index 10ae4d9..d0bbce0 100644 (file)
@@ -1,22 +1,21 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
-#
-#    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 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
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#    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/>.     
 #
 ##############################################################################
 
@@ -29,7 +28,7 @@ class hr_timesheet_invoice_factor(osv.osv):
     _description = "Invoice rate"
     _columns = {
         'name': fields.char('Internal name', size=128, required=True),
-        'customer_name': fields.char('Visible name', size=128),
+        'customer_name': fields.char('Name', size=128),
         'factor': fields.float('Discount (%)', required=True),
     }
     _defaults = {
@@ -44,7 +43,7 @@ class account_analytic_account(osv.osv):
         res = {}
         for account in self.browse(cr, uid, ids):
             invoiced = {}
-            cr.execute('select distinct l.invoice_id from hr_analytic_timesheet h left join account_analytic_line l on (h.line_id=l.id) where account_id=1', (account.id,))
+            cr.execute('select distinct(l.invoice_id) from hr_analytic_timesheet h left join account_analytic_line l on (h.line_id=l.id) where account_id=%s', (account.id,))
             invoice_ids = filter(None, map(lambda x: x[0], cr.fetchall()))
             for invoice in self.pool.get('account.invoice').browse(cr, uid, invoice_ids, context):
                 res.setdefault(account.id, 0.0)
@@ -53,15 +52,23 @@ class account_analytic_account(osv.osv):
             res[id] = round(res.get(id, 0.0),2)
         return res
 
+    # def _get_account_currency(self, cr, uid, ids, field_name, arg, context={}):
+    #     result=super(account_analytic_account, self)._get_account_currency(cr, uid, ids, field_name, arg, context)
+    #     for rec in self.browse(cr, uid, ids, context):
+    #         result[rec.id] = rec.pricelist_id and (rec.pricelist_id.currency_id.id,rec.pricelist_id.currency_id.code) or result[rec.id]
+    #     return result
+
     _inherit = "account.analytic.account"
     _columns = {
+        # 'currency_id': fields.function(_get_account_currency, method=True, type='many2one', relation='res.currency', string='Account currency', store=True),
         'pricelist_id' : fields.many2one('product.pricelist', 'Sale Pricelist'),
         'amount_max': fields.float('Max. Invoice Price'),
         'amount_invoiced': fields.function(_invoiced_calc, method=True, string='Invoiced Amount',
             help="Total invoiced"),
         'to_invoice': fields.many2one('hr_timesheet_invoice.factor','Reinvoice Costs',
-            help="Check this field if you plan to automatically generate invoices based " \
-            "on the costs in this analytic account: timesheets, expenses, ..."),
+            help="Fill this field if you plan to automatically generate invoices based " \
+            "on the costs in this analytic account: timesheets, expenses, ..." \
+            "You can configure an automatic invoice rate on analytic accounts."),
     }
     _defaults = {
         'pricelist_id': lambda self,cr, uid, ctx: ctx.get('pricelist_id', False),
@@ -73,26 +80,27 @@ class account_analytic_line(osv.osv):
     _inherit = 'account.analytic.line'
     _columns = {
         'invoice_id': fields.many2one('account.invoice', 'Invoice'),
-        'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Invoicing'),
+        'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Type of Invoicing'),
     }
 
     def unlink(self, cursor, user, ids, context=None):
-        self._check(cursor, user, ids)
+        # self._check(cursor, user, ids)
         return super(account_analytic_line,self).unlink(cursor, user, ids,
                 context=context)
 
     def write(self, cr, uid, ids, vals, context=None):
-        self._check(cr, uid, ids)
+        self._check_inv(cr, uid, ids,vals)
         return super(account_analytic_line,self).write(cr, uid, ids, vals,
                 context=context)
 
-    def _check(self, cr, uid, ids):
+    def _check_inv(self, cr, uid, ids,vals):
         select = ids
         if isinstance(select, (int, long)):
             select = [ids]
-        for line in self.browse(cr, uid, select):
-            if line.invoice_id:
-                raise osv.except_osv(_('Error !'),
+        if ( not vals.has_key('invoice_id')) or vals['invoice_id' ] == False:
+            for line in self.browse(cr, uid, select):
+                if line.invoice_id:
+                    raise osv.except_osv(_('Error !'),
                         _('You can not modify an invoiced analytic line!'))
         return True