Launchpad automatic translations update.
[odoo/odoo.git] / addons / account_analytic_analysis / cron_account_analytic_account.py
1 #!/usr/bin/env python
2 from osv import osv
3 from mako.template import Template
4 import time
5 try:
6     import cStringIO as StringIO
7 except ImportError:
8     import StringIO
9
10 import tools
11
12 MAKO_TEMPLATE = u"""Hello ${user.name},
13
14 Here is a list of contracts that have to be renewed for two
15 possible reasons:
16   - the end of contract date is passed
17   - the customer consumed more hours than expected
18
19 Can you contact the customer in order to sell a new or renew its contract.
20 The contract has been set with a pending state, can you update the status
21 of the analytic account following this rule:
22   - Set Done: if the customer does not want to renew
23   - Set Open: if the customer purchased an extra contract
24
25 Here is the list of contracts to renew:
26 % for partner, accounts in partners.iteritems():
27   * ${partner.name}
28   % for account in accounts:
29     - Name: ${account.name}
30       % if account.quantity_max != 0.0:
31       - Quantity: ${account.quantity}/${account.quantity_max} hours
32       % endif
33       - Dates: ${account.date_start} to ${account.date and account.date or '???'}
34       - Contacts:
35         ${account.partner_id.name}, ${account.partner_id.phone}, ${account.partner_id.email}
36
37   % endfor
38 % endfor
39
40 You can use the report in the menu: Sales > Invoicing > Overdue Accounts
41
42 Regards,
43
44 --
45 OpenERP
46 """
47
48 class analytic_account(osv.osv):
49     _inherit = 'account.analytic.account'
50
51     def cron_account_analytic_account(self, cr, uid, context=None):
52         domain = [
53             ('name', 'not ilike', 'maintenance'),
54             ('partner_id', '!=', False),
55             ('user_id', '!=', False),
56             ('user_id.user_email', '!=', False),
57             ('state', 'in', ('draft', 'open')),
58             '|', ('date',  '<', time.strftime('%Y-%m-%d')), ('date', '=', False),
59         ]
60
61         account_ids = self.search(cr, uid, domain, context=context, order='name asc')
62         accounts = self.browse(cr, uid, account_ids, context=context)
63
64         users = dict()
65         for account in accounts:
66             users.setdefault(account.user_id, dict()).setdefault(account.partner_id, []).append(account)
67
68             account.write({'state' : 'pending'}, context=context)
69
70         for user, data in users.iteritems():
71             subject = '[OPENERP] Reporting: Analytic Accounts'
72             body = Template(MAKO_TEMPLATE).render_unicode(user=user, partners=data)
73             tools.email_send('noreply@openerp.com', [user.user_email, ], subject, body)
74
75         return True
76
77 analytic_account()
78
79 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: