[FIX] Remove the unused translation files
[odoo/odoo.git] / addons / subscription / subscription.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 # TODO:
24 #   Error treatment: exception, request, ... -> send request to user_id
25
26 from mx import DateTime
27 import time
28 from osv import fields,osv
29 from tools.translate import _
30
31 class subscription_document(osv.osv):
32     _name = "subscription.document"
33     _description = "Subscription document"
34     _columns = {
35         'name': fields.char('Name', size=60, required=True),
36         'active': fields.boolean('Active'),
37         'model': fields.many2one('ir.model', 'Object', required=True),
38         'field_ids': fields.one2many('subscription.document.fields', 'document_id', 'Fields')
39     }
40     _defaults = {
41         'active' : lambda *a: True,
42     }
43     
44     def write(self, cr, uid, ids, vals, context=None):
45         if 'model' in vals:
46             raise osv.except_osv(_('Error !'),_('You cannot modify the Object linked to the Document Type!\nCreate another Document instead !'))
47         return super(subscription_document, self).write(cr, uid, ids, vals, context=context)
48     
49 subscription_document()
50
51 class subscription_document_fields(osv.osv):
52     _name = "subscription.document.fields"
53     _description = "Subscription document fields"
54     _rec_name = 'field'
55     _columns = {
56         'field': fields.many2one('ir.model.fields', 'Field', domain="[('model_id', '=', parent.model)]", required=True),
57         'value': fields.selection([('false','False'),('date','Current Date')], 'Default Value', size=40),
58         'document_id': fields.many2one('subscription.document', 'Subscription Document', ondelete='cascade'),
59     }
60     _defaults = {}
61 subscription_document_fields()
62
63 def _get_document_types(self, cr, uid, context={}):
64     cr.execute('select m.model, s.name from subscription_document s, ir_model m WHERE s.model = m.id order by s.name')
65     return cr.fetchall()
66
67 class subscription_subscription(osv.osv):
68     _name = "subscription.subscription"
69     _description = "Subscription"
70     _columns = {
71         'name': fields.char('Name', size=60, required=True),
72         'active': fields.boolean('Active'),
73         'partner_id': fields.many2one('res.partner', 'Partner'),
74         'notes': fields.text('Notes'),
75         'user_id': fields.many2one('res.users', 'User', required=True),
76         'interval_number': fields.integer('Interval Qty'),
77         'interval_type': fields.selection([('days', 'Days'), ('weeks', 'Weeks'), ('months', 'Months')], 'Interval Unit'),
78         'exec_init': fields.integer('Number of documents'),
79         'date_init': fields.datetime('First Date'),
80         'state': fields.selection([('draft','Draft'),('running','Running'),('done','Done')], 'Status'),
81         'doc_source': fields.reference('Source Document', required=True, selection=_get_document_types, size=128),
82         'doc_lines': fields.one2many('subscription.subscription.history', 'subscription_id', 'Documents created', readonly=True),
83         'cron_id': fields.many2one('ir.cron', 'Cron Job')
84     }
85     _defaults = {
86         'date_init': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
87         'user_id': lambda obj,cr,uid,context: uid,
88         'active': lambda *a: True,
89         'interval_number': lambda *a: 1,
90         'interval_type': lambda *a: 'months',
91         'doc_source': lambda *a: False,
92         'state': lambda *a: 'draft'
93     }
94
95     def set_process(self, cr, uid, ids, context={}):
96         for row in self.read(cr, uid, ids):
97             mapping = {'name':'name','interval_number':'interval_number','interval_type':'interval_type','exec_init':'numbercall','date_init':'nextcall'}
98             res = {'model':'subscription.subscription', 'args': repr([[row['id']]]), 'function':'model_copy', 'priority':6, 'user_id':row['user_id'] and row['user_id'][0]}
99             for key,value in mapping.items():
100                 res[value] = row[key]
101             id = self.pool.get('ir.cron').create(cr, uid, res)
102             self.write(cr, uid, [row['id']], {'cron_id':id, 'state':'running'})
103         return True
104
105     def model_copy(self, cr, uid, ids, context={}):
106         for row in self.read(cr, uid, ids):
107             if not row.get('cron_id',False):
108                 continue
109             cron_ids = [row['cron_id'][0]]
110             remaining = self.pool.get('ir.cron').read(cr, uid, cron_ids, ['numbercall'])[0]['numbercall']
111             try:
112                 (model_name, id) = row['doc_source'].split(',')
113                 id = int(id)
114                 model = self.pool.get(model_name)
115             except:
116                 raise osv.except_osv(_('Wrong Source Document !'), _('Please provide another source document.\nThis one does not exist !'))
117
118             default = {'state':'draft'}
119             doc_obj = self.pool.get('subscription.document')
120             document_ids = doc_obj.search(cr, uid, [('model.model','=',model_name)])
121             doc = doc_obj.browse(cr, uid, document_ids)[0]
122             for f in doc.field_ids:
123                 if f.value=='date':
124                     value = time.strftime('%Y-%m-%d')
125                 else:
126                     value = False
127                 default[f.field.name] = value
128
129             state = 'running'
130
131             # if there was only one remaining document to generate
132             # the subscription is over and we mark it as being done
133             if remaining == 1:
134                 state = 'done'
135             id = self.pool.get(model_name).copy(cr, uid, id, default, context)
136             self.pool.get('subscription.subscription.history').create(cr, uid, {'subscription_id': row['id'], 'date':time.strftime('%Y-%m-%d %H:%M:%S'), 'document_id': model_name+','+str(id)})
137             self.write(cr, uid, [row['id']], {'state':state})
138         return True
139
140     def set_done(self, cr, uid, ids, context={}):
141         res = self.read(cr,uid, ids, ['cron_id'])
142         ids2 = [x['cron_id'][0] for x in res if x['id']]
143         self.pool.get('ir.cron').write(cr, uid, ids2, {'active':False})
144         self.write(cr, uid, ids, {'state':'done'})
145         return True
146
147     def set_draft(self, cr, uid, ids, context={}):
148         self.write(cr, uid, ids, {'state':'draft'})
149         return True
150 subscription_subscription()
151
152 class subscription_subscription_history(osv.osv):
153     _name = "subscription.subscription.history"
154     _description = "Subscription history"
155     _rec_name = 'date'
156     _columns = {
157         'date': fields.datetime('Date'),
158         'subscription_id': fields.many2one('subscription.subscription', 'Subscription', ondelete='cascade'),
159         'document_id': fields.reference('Source Document', required=True, selection=[('account.invoice','Invoice'),('sale.order','Sale Order')], size=128),
160     }
161 subscription_subscription_history()
162
163
164 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
165