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