[FIX] don't display a '#' in empty URI fields
[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         % for address in account.partner_id.address:
36         . ${address.name}, ${address.phone}, ${address.email}
37         % endfor
38
39   % endfor
40 % endfor
41
42 You can use the report in the menu: Sales > Invoicing > Overdue Accounts
43
44 Regards,
45
46 --
47 OpenERP
48 """
49
50 class analytic_account(osv.osv):
51     _inherit = 'account.analytic.account'
52
53     def cron_account_analytic_account(self, cr, uid, context=None):
54         domain = [
55             ('name', 'not ilike', 'maintenance'),
56             ('partner_id', '!=', False),
57             ('user_id', '!=', False),
58             ('user_id.user_email', '!=', False),
59             ('state', 'in', ('draft', 'open')),
60             '|', ('date',  '<', time.strftime('%Y-%m-%d')), ('date', '=', False),
61         ]
62
63         account_ids = self.search(cr, uid, domain, context=context, order='name asc')
64         accounts = self.browse(cr, uid, account_ids, context=context)
65
66         users = dict()
67         for account in accounts:
68             users.setdefault(account.user_id, dict()).setdefault(account.partner_id, []).append(account)
69
70             account.write({'state' : 'pending'}, context=context)
71
72         for user, data in users.iteritems():
73             subject = '[OPENERP] Reporting: Analytic Accounts'
74             body = Template(MAKO_TEMPLATE).render_unicode(user=user, partners=data)
75             tools.email_send('noreply@openerp.com', [user.user_email, ], subject, body)
76
77         return True
78
79 analytic_account()
80
81 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: