[IMP]account_analytic_analysis: set ids intes of id for write bcoz in project there...
[odoo/odoo.git] / addons / analytic / analytic.py
index 35acc4a..85e21d5 100644 (file)
 ##############################################################################
 
 import time
+from datetime import datetime
 
-from osv import fields, osv
-from tools.translate import _
-import decimal_precision as dp
+from openerp.osv import fields, osv
+from openerp import tools
+from openerp.tools.translate import _
+import openerp.addons.decimal_precision as dp
 
 class account_analytic_account(osv.osv):
     _name = 'account.analytic.account'
     _inherit = ['mail.thread']
     _description = 'Analytic Account'
+    _track = {
+        'state': {
+            'analytic.mt_account_pending': lambda self, cr, uid, obj, ctx=None:  obj['state'] == 'pending',
+            'analytic.mt_account_closed': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'close',
+            'analytic.mt_account_opened': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'open',
+        },
+    }
 
     def _compute_level_tree(self, cr, uid, ids, child_ids, res, field_names, context=None):
         currency_obj = self.pool.get('res.currency')
@@ -97,24 +106,32 @@ class account_analytic_account(osv.osv):
         return self._compute_level_tree(cr, uid, ids, child_ids, res, fields, context)
 
     def name_get(self, cr, uid, ids, context=None):
-        if isinstance(ids, (int, long)):
-            ids=[ids]
-        if not ids:
-            return []
         res = []
-        for account in self.browse(cr, uid, ids, context=context):
-            data = []
-            acc = account
-            while acc:
-                data.insert(0, acc.name)
-                acc = acc.parent_id
-            data = ' / '.join(data)
-            res.append((account.id, data))
+        if not ids:
+            return res
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        for id in ids:
+            elmt = self.browse(cr, uid, id, context=context)
+            res.append((id, self._get_one_full_name(elmt)))
+        return res
+
+    def _get_full_name(self, cr, uid, ids, name=None, args=None, context=None):
+        if context == None:
+            context = {}
+        res = {}
+        for elmt in self.browse(cr, uid, ids, context=context):
+            res[elmt.id] = self._get_one_full_name(elmt)
         return res
 
-    def _complete_name_calc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
-        res = self.name_get(cr, uid, ids)
-        return dict(res)
+    def _get_one_full_name(self, elmt, level=6):
+        if level<=0:
+            return '...'
+        if elmt.parent_id and not elmt.type == 'template':
+            parent_path = self._get_one_full_name(elmt.parent_id, level-1) + " / "
+        else:
+            parent_path = ''
+        return parent_path + elmt.name
 
     def _child_compute(self, cr, uid, ids, name, arg, context=None):
         result = {}
@@ -140,8 +157,9 @@ class account_analytic_account(osv.osv):
         for account in self.browse(cr, uid, ids, context=context):
             if account.company_id:
                 if account.company_id.currency_id.id != value:
-                    raise osv.except_osv(_('Error !'), _("If you set a company, the currency selected has to be the same as it's currency. \nYou can remove the company belonging, and thus change the currency, only on analytic account of type 'view'. This can be really usefull for consolidation purposes of several companies charts with different currencies, for example."))
-        return cr.execute("""update account_analytic_account set currency_id=%s where id=%s""", (value, account.id, ))
+                    raise osv.except_osv(_('Error!'), _("If you set a company, the currency selected has to be the same as it's currency. \nYou can remove the company belonging, and thus change the currency, only on analytic account of type 'view'. This can be really usefull for consolidation purposes of several companies charts with different currencies, for example."))
+        if value:
+            return cr.execute("""update account_analytic_account set currency_id=%s where id=%s""", (value, account.id, ))
 
     def _currency(self, cr, uid, ids, field_name, arg, context=None):
         result = {}
@@ -153,10 +171,15 @@ class account_analytic_account(osv.osv):
         return result
 
     _columns = {
-        'name': fields.char('Account Name', size=128, required=True),
-        'complete_name': fields.function(_complete_name_calc, type='char', string='Full Account Name'),
-        'code': fields.char('Code/Reference', size=24, select=True),
-        'type': fields.selection([('view','Analytic View'), ('normal','Analytic Account'),('contract','Contract or Project'),('template','Template of Project')], 'Type of Account', required=True, help='If you select the View Type, it means you won\'t allow to create journal entries using that account.'),
+        'name': fields.char('Account/Contract Name', size=128, required=True),
+        'complete_name': fields.function(_get_full_name, type='char', string='Full Name'),
+        'code': fields.char('Reference', select=True),
+        'type': fields.selection([('view','Analytic View'), ('normal','Analytic Account'),('contract','Contract or Project'),('template','Template of Contract')], 'Type of Account', required=True,
+                                 help="If you select the View Type, it means you won\'t allow to create journal entries using that account.\n"\
+                                  "The type 'Analytic account' stands for usual accounts that you only want to use in accounting.\n"\
+                                  "If you select Contract or Project, it offers you the possibility to manage the validity and the invoicing options for this account.\n"\
+                                  "The special type 'Template of Contract' allows you to define a template with default data that you can reuse easily."),
+        'template_id': fields.many2one('account.analytic.account', 'Template of Contract'),
         'description': fields.text('Description'),
         'parent_id': fields.many2one('account.analytic.account', 'Parent Analytic Account', select=2),
         'child_ids': fields.one2many('account.analytic.account', 'parent_id', 'Child Accounts'),
@@ -166,20 +189,36 @@ class account_analytic_account(osv.osv):
         'debit': fields.function(_debit_credit_bal_qtty, type='float', string='Debit', multi='debit_credit_bal_qtty', digits_compute=dp.get_precision('Account')),
         'credit': fields.function(_debit_credit_bal_qtty, type='float', string='Credit', multi='debit_credit_bal_qtty', digits_compute=dp.get_precision('Account')),
         'quantity': fields.function(_debit_credit_bal_qtty, type='float', string='Quantity', multi='debit_credit_bal_qtty'),
-        'quantity_max': fields.float('Maximum Time', help='Sets the higher limit of time to work on the contract.'),
+        'quantity_max': fields.float('Prepaid Service Units', help='Sets the higher limit of time to work on the contract, based on the timesheet. (for instance, number of hours in a limited support contract.)'),
         'partner_id': fields.many2one('res.partner', 'Customer'),
         'user_id': fields.many2one('res.users', 'Project Manager'),
         'manager_id': fields.many2one('res.users', 'Account Manager'),
-        'date_start': fields.date('Date Start'),
+        'date_start': fields.date('Start Date'),
         'date': fields.date('Date End', select=True),
         'company_id': fields.many2one('res.company', 'Company', required=False), #not required because we want to allow different companies to use the same chart of account, except for leaf accounts.
-        'state': fields.selection([('template', 'Template'),('draft','New'),('open','In Progress'), ('cancelled', 'Cancelled'),('pending','To Renew'),('close','Closed')], 'Status', required=True,),
-        'currency_id': fields.function(_currency, fnct_inv=_set_company_currency,
+        'state': fields.selection([('template', 'Template'),('draft','New'),('open','In Progress'),('pending','To Renew'),('close','Closed'),('cancelled', 'Cancelled')], 'Status', required=True, track_visibility='onchange'),
+        'currency_id': fields.function(_currency, fnct_inv=_set_company_currency, #the currency_id field is readonly except if it's a view account and if there is no company
             store = {
                 'res.company': (_get_analytic_account, ['currency_id'], 10),
             }, string='Currency', type='many2one', relation='res.currency'),
     }
-    
+
+    def on_change_template(self, cr, uid, ids, template_id, context=None):
+        if not template_id:
+            return {}
+        res = {'value':{}}
+        template = self.browse(cr, uid, template_id, context=context)
+        if template.date_start and template.date:
+            from_dt = datetime.strptime(template.date_start, tools.DEFAULT_SERVER_DATE_FORMAT)
+            to_dt = datetime.strptime(template.date, tools.DEFAULT_SERVER_DATE_FORMAT)
+            timedelta = to_dt - from_dt
+            res['value']['date'] = datetime.strftime(datetime.now() + timedelta, tools.DEFAULT_SERVER_DATE_FORMAT)
+        res['value']['date_start'] = fields.date.today()
+        res['value']['quantity_max'] = template.quantity_max
+        res['value']['parent_id'] = template.parent_id and template.parent_id.id or False
+        res['value']['description'] = template.description
+        return res
+
     def on_change_partner_id(self, cr, uid, ids,partner_id, name, context={}):
         res={}
         if partner_id:
@@ -216,14 +255,20 @@ class account_analytic_account(osv.osv):
 
     _order = 'name asc'
     _constraints = [
-        (check_recursion, 'Error! You can not create recursive analytic accounts.', ['parent_id']),
+        (check_recursion, 'Error! You cannot create recursive analytic accounts.', ['parent_id']),
     ]
 
+    def name_create(self, cr, uid, name, context=None):
+        raise osv.except_osv(_('Warning'), _("Quick account creation disallowed."))
+
     def copy(self, cr, uid, id, default=None, context=None):
         if not default:
             default = {}
-        default['code'] = False
-        default['line_ids'] = []
+        analytic = self.browse(cr, uid, id, context=context)
+        default.update(
+            code=False,
+            line_ids=[],
+            name=_("%s (copy)") % (analytic['name']))
         return super(account_analytic_account, self).copy(cr, uid, id, default, context=context)
 
     def on_change_company(self, cr, uid, id, company_id):
@@ -251,43 +296,21 @@ class account_analytic_account(osv.osv):
         if context is None:
             context={}
         if context.get('current_model') == 'project.project':
-            cr.execute("select analytic_account_id from project_project")
-            project_ids = [x[0] for x in cr.fetchall()]
+            project_obj = self.pool.get("account.analytic.account")
+            project_ids = project_obj.search(cr, uid, args)
             return self.name_get(cr, uid, project_ids, context=context)
         if name:
-            account = self.search(cr, uid, [('code', '=', name)] + args, limit=limit, context=context)
-            if not account:
-                names=map(lambda i : i.strip(),name.split('/'))
-                for i in range(len(names)):
-                    dom=[('name', operator, names[i])]
-                    if i>0:
-                        dom+=[('id','child_of',account)]
-                    account = self.search(cr, uid, dom, limit=limit, context=context)
-                newacc = account
-                while newacc:
-                    newacc = self.search(cr, uid, [('parent_id', 'in', newacc)], limit=limit, context=context)
-                    account += newacc
-                if args:
-                    account = self.search(cr, uid, [('id', 'in', account)] + args, limit=limit, context=context)
+            account_ids = self.search(cr, uid, [('code', '=', name)] + args, limit=limit, context=context)
+            if not account_ids:
+                dom = []
+                for name2 in name.split('/'):
+                    name = name2.strip()
+                    account_ids = self.search(cr, uid, dom + [('name', 'ilike', name)] + args, limit=limit, context=context)
+                    if not account_ids: break
+                    dom = [('parent_id','in',account_ids)]
         else:
-            account = self.search(cr, uid, args, limit=limit, context=context)
-        return self.name_get(cr, uid, account, context=context)
-    
-    def create(self, cr, uid, vals, context=None):
-        contract =  super(account_analytic_account, self).create(cr, uid, vals, context=context)
-        if contract:
-            self.create_send_note(cr, uid, [contract], context=context)
-        return contract
-    # ------------------------------------------------
-    # OpenChatter methods and notifications
-    # ------------------------------------------------
-    def create_send_note(self, cr, uid, ids, context=None):
-        for obj in self.browse(cr, uid, ids, context=context):
-            self.message_subscribe(cr, uid, [obj.id], [obj.user_id.id], context=context)
-            self.message_append_note(cr, uid, [obj.id], body=_("Contract for <em>%s</em> has been <b>created</b>.") % (obj.partner_id.name), context=context)
-            
-account_analytic_account()
-
+            account_ids = self.search(cr, uid, args, limit=limit, context=context)
+        return self.name_get(cr, uid, account_ids, context=context)
 
 class account_analytic_line(osv.osv):
     _name = 'account.analytic.line'
@@ -298,30 +321,35 @@ class account_analytic_line(osv.osv):
         'date': fields.date('Date', required=True, select=True),
         'amount': fields.float('Amount', required=True, help='Calculated by multiplying the quantity and the price given in the Product\'s cost price. Always expressed in the company main currency.', digits_compute=dp.get_precision('Account')),
         'unit_amount': fields.float('Quantity', help='Specifies the amount of quantity to count.'),
-        'account_id': fields.many2one('account.analytic.account', 'Analytic Account', required=True, ondelete='cascade', select=True, domain=[('type','<>','view')]),
+        'account_id': fields.many2one('account.analytic.account', 'Analytic Account', required=True, ondelete='restrict', select=True, domain=[('type','<>','view')]),
         'user_id': fields.many2one('res.users', 'User'),
         'company_id': fields.related('account_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
 
     }
+
+    def _get_default_date(self, cr, uid, context=None):
+        return fields.date.context_today(self, cr, uid, context=context)
+
+    def __get_default_date(self, cr, uid, context=None):
+        return self._get_default_date(cr, uid, context=context)
+
     _defaults = {
-        'date': lambda *a: time.strftime('%Y-%m-%d'),
+        'date': __get_default_date,
         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context=c),
         'amount': 0.00
     }
 
     _order = 'date desc'
-    
+
     def _check_no_view(self, cr, uid, ids, context=None):
         analytic_lines = self.browse(cr, uid, ids, context=context)
         for line in analytic_lines:
             if line.account_id.type == 'view':
                 return False
         return True
-    
-    _constraints = [
-        (_check_no_view, 'You can not create analytic line on view account.', ['account_id']),
-    ]    
 
-account_analytic_line()
+    _constraints = [
+        (_check_no_view, 'You cannot create analytic line on view account.', ['account_id']),
+    ]
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: