~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to account_subscription/account_model.py

  • Committer: jf
  • Date: 2012-06-13 12:43:21 UTC
  • mfrom: (827.5.11 uf-635)
  • Revision ID: jf@tempo4-20120613124321-2b8cwgl86gyy2tb7
UF-635 [DEV] Documents workflow: Graphic representation
lp:~unifield-team/unifield-wm/uf-635 revno 838

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
##############################################################################
 
4
#
 
5
#    OpenERP, Open Source Management Solution
 
6
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (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 Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
from osv import fields, osv
 
23
import time
 
24
from datetime import datetime
 
25
from dateutil.relativedelta import relativedelta
 
26
from tools.translate import _
 
27
 
 
28
class account_model_line(osv.osv):
 
29
    _name = "account.model.line"
 
30
    _inherit = "account.model.line"
 
31
    
 
32
    _columns = {
 
33
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
 
34
        'account_user_type_code': fields.related('account_id', 'user_type_code', type="char", string="Account User Type Code", store=False)
 
35
    }
 
36
    
 
37
    def button_analytic_distribution(self, cr, uid, ids, context=None):
 
38
        """
 
39
        Launch analytic distribution wizard on an invoice line
 
40
        """
 
41
        # Some verifications
 
42
        if not context:
 
43
            context = {}
 
44
        if isinstance(ids, (int, long)):
 
45
            ids = [ids]
 
46
        if not ids:
 
47
            raise osv.except_osv(_('Error'), _('No model line given. Please save your model line before.'))
 
48
        model_line = self.browse(cr, uid, ids[0], context=context)
 
49
        distrib_id = False
 
50
        amount = abs(model_line.debit - model_line.credit)
 
51
        currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
 
52
        # Get analytic distribution id from this line
 
53
        distrib_id = model_line.analytic_distribution_id and model_line.analytic_distribution_id.id or False
 
54
        # Prepare values for wizard
 
55
        vals = {
 
56
            'total_amount': amount,
 
57
            'model_line_id': model_line.id,
 
58
            'currency_id': currency,
 
59
            'state': 'dispatch',
 
60
            'account_id': model_line.account_id.id,
 
61
        }
 
62
        if distrib_id:
 
63
            vals.update({'distribution_id': distrib_id,})
 
64
        # Create the wizard
 
65
        wiz_obj = self.pool.get('analytic.distribution.wizard')
 
66
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
 
67
        # Update some context values
 
68
        context.update({
 
69
            'active_id': ids[0],
 
70
            'active_ids': ids,
 
71
        })
 
72
        # Open it!
 
73
        return {
 
74
                'name': 'Analytic distribution',
 
75
                'type': 'ir.actions.act_window',
 
76
                'res_model': 'analytic.distribution.wizard',
 
77
                'view_type': 'form',
 
78
                'view_mode': 'form',
 
79
                'target': 'new',
 
80
                'res_id': [wiz_id],
 
81
                'context': context,
 
82
        }
 
83
    
 
84
account_model_line()
 
85
 
 
86
class account_model(osv.osv):
 
87
    _name = "account.model"
 
88
    _inherit = "account.model"
 
89
    
 
90
    # @@@override@account.account_model.generate()
 
91
    def generate(self, cr, uid, ids, datas={}, context=None):
 
92
        move_ids = []
 
93
        entry = {}
 
94
        account_move_obj = self.pool.get('account.move')
 
95
        account_move_line_obj = self.pool.get('account.move.line')
 
96
        pt_obj = self.pool.get('account.payment.term')
 
97
        ana_obj = self.pool.get('analytic.distribution')
 
98
 
 
99
        if context is None:
 
100
            context = {}
 
101
 
 
102
        if datas.get('date', False):
 
103
            context.update({'date': datas['date']})
 
104
 
 
105
        period_id = self.pool.get('account.period').find(cr, uid, dt=context.get('date', False))
 
106
        if not period_id:
 
107
            raise osv.except_osv(_('No period found !'), _('Unable to find a valid period !'))
 
108
        period_id = period_id[0]
 
109
 
 
110
        for model in self.browse(cr, uid, ids, context=context):
 
111
            entry['name'] = model.name%{'year':time.strftime('%Y'), 'month':time.strftime('%m'), 'date':time.strftime('%Y-%m')}
 
112
            move_id = account_move_obj.create(cr, uid, {
 
113
                'ref': entry['name'],
 
114
                'period_id': period_id,
 
115
                'journal_id': model.journal_id.id,
 
116
                'date': context.get('date',time.strftime('%Y-%m-%d'))
 
117
            })
 
118
            move_ids.append(move_id)
 
119
            for line in model.lines_id:
 
120
                val = {
 
121
                    'move_id': move_id,
 
122
                    'journal_id': model.journal_id.id,
 
123
                    'period_id': period_id,
 
124
                }
 
125
                if line.account_id.user_type_code == 'expense':
 
126
                    if not line.analytic_distribution_id:
 
127
                        raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution on the '%s' line!") % (line.name))
 
128
                    if not model.journal_id.analytic_journal_id:
 
129
                        raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (model.journal_id.name,))
 
130
                    new_distribution_id = ana_obj.copy(cr, uid, line.analytic_distribution_id.id, {}, context=context)
 
131
                    val.update({'analytic_distribution_id': new_distribution_id})
 
132
                    
 
133
                date_maturity = time.strftime('%Y-%m-%d')
 
134
                if line.date_maturity == 'partner':
 
135
                    if not line.partner_id:
 
136
                        raise osv.except_osv(_('Error !'), _("Maturity date of entry line generated by model line '%s' of model '%s' is based on partner payment term!" \
 
137
                                                                "\nPlease define partner on it!")%(line.name, model.name))
 
138
                    if line.partner_id.property_payment_term:
 
139
                        payment_term_id = line.partner_id.property_payment_term.id
 
140
                        pterm_list = pt_obj.compute(cr, uid, payment_term_id, value=1, date_ref=date_maturity)
 
141
                        if pterm_list:
 
142
                            pterm_list = [l[0] for l in pterm_list]
 
143
                            pterm_list.sort()
 
144
                            date_maturity = pterm_list[-1]
 
145
 
 
146
                val.update({
 
147
                    'name': line.name,
 
148
                    'quantity': line.quantity,
 
149
                    'debit': line.debit,
 
150
                    'credit': line.credit,
 
151
                    'account_id': line.account_id.id,
 
152
                    'move_id': move_id,
 
153
                    'partner_id': line.partner_id.id,
 
154
                    'date': context.get('date',time.strftime('%Y-%m-%d')),
 
155
                    'date_maturity': date_maturity
 
156
                })
 
157
                c = context.copy()
 
158
                c.update({'journal_id': model.journal_id.id,'period_id': period_id})
 
159
                account_move_line_obj.create(cr, uid, val, context=c)
 
160
 
 
161
        return move_ids
 
162
 
 
163
account_model()
 
164
 
 
165
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: