~vauxoo/addons-vauxoo/7.0-project_issue_conf-dev_luis

« back to all changes in this revision

Viewing changes to report_profit/wizard/wizard_last_cost_price_sum.py

  • Committer: Javier Duran
  • Date: 2012-01-30 16:55:47 UTC
  • Revision ID: javier@squezee-vir-20120130165547-8bn2rxdgxzplqpfy
[ADD] se agrega el modulo report profit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2010 Vauxoo C.A. (http://openerp.com.ve/) All Rights Reserved.
 
5
#                    Javier Duran <javier@vauxoo.com>
 
6
 
7
#
 
8
# WARNING: This program as such is intended to be used by professional
 
9
# programmers who take the whole responsability of assessing all potential
 
10
# consequences resulting from its eventual inadequacies and bugs
 
11
# End users who are looking for a ready-to-use solution with commercial
 
12
# garantees and support are strongly adviced to contract a Free Software
 
13
# Service Company
 
14
#
 
15
# This program is Free Software; you can redistribute it and/or
 
16
# modify it under the terms of the GNU General Public License
 
17
# as published by the Free Software Foundation; either version 2
 
18
# of the License, or (at your option) any later version.
 
19
#
 
20
# This program is distributed in the hope that it will be useful,
 
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
# GNU General Public License for more details.
 
24
#
 
25
# You should have received a copy of the GNU General Public License
 
26
# along with this program; if not, write to the Free Software
 
27
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
28
#
 
29
##############################################################################
 
30
 
 
31
import wizard
 
32
import osv
 
33
import pooler
 
34
from tools.translate import _
 
35
 
 
36
 
 
37
_transaction_form = '''<?xml version="1.0"?>
 
38
<form string="Last Cost Price Sum">
 
39
    <separator string="Sum By?" colspan="4"/>
 
40
    <field name="date_start" />
 
41
    <field name="date_end" />
 
42
    <newline/>
 
43
    <group col="4" colspan="2">
 
44
    <group col="4" colspan="2">
 
45
        <field name="u_check" />
 
46
        <field name="user_res_id" nolabel="1" attrs="{'readonly':[('u_check','!=',True)]}"/>
 
47
        <field name="u_sel" nolabel="1" attrs="{'readonly':[('u_check','!=',True)]}"/>
 
48
        <field name="p_check" />
 
49
        <field name="partner_res_id" nolabel="1" attrs="{'readonly':[('p_check','!=',True)]}"/>
 
50
        <field name="p_sel" nolabel="1" attrs="{'readonly':[('p_check','!=',True)]}"/>
 
51
        <field name="c_check" />
 
52
        <field name="cat_res_id" nolabel="1" attrs="{'readonly':[('c_check','!=',True)]}"/>
 
53
        <field name="c_sel" nolabel="1" attrs="{'readonly':[('c_check','!=',True)]}"/>
 
54
    </group>
 
55
 
 
56
    </group>  
 
57
</form>'''
 
58
 
 
59
_transaction_fields = {
 
60
    'date_start': {'string':'Start Date','type':'date','required': True},
 
61
    'date_end': {'string':'End Date','type':'date','required': True},
 
62
    'user_res_id': {'string':'Salesman', 'type':'many2one', 'relation': 'res.users','required':False},
 
63
    'partner_res_id': {'string':'Partner', 'type':'many2one', 'relation': 'res.partner','required':False},
 
64
    'cat_res_id': {'string':'Category', 'type':'many2one', 'relation': 'product.category','required':False},
 
65
    'u_check': {'string':'Check salesman?', 'type':'boolean'},
 
66
    'p_check': {'string':'Check partner?', 'type':'boolean'},
 
67
    'c_check': {'string':'Check category?', 'type':'boolean'},
 
68
    'u_sel':{
 
69
        'string':"Sequence",
 
70
        'type':'selection',
 
71
        'selection':[('one','1'),('two','2'),('three','3'),('none','No Filter')],
 
72
        'default': lambda *a:'none'
 
73
    },
 
74
    'p_sel':{
 
75
        'string':"Sequence",
 
76
        'type':'selection',
 
77
        'selection':[('one','1'),('two','2'),('three','3'),('none','No Filter')],
 
78
        'default': lambda *a:'none'
 
79
    },
 
80
    'c_sel':{
 
81
        'string':"Sequence",
 
82
        'type':'selection',
 
83
        'selection':[('one','1'),('two','2'),('three','3'),('none','No Filter')],
 
84
        'default': lambda *a:'none'
 
85
    },
 
86
 
 
87
 
 
88
}
 
89
 
 
90
def _data_save(self, cr, uid, data, context):
 
91
    form = data['form']
 
92
    if not form['u_check']  and not form['p_check'] and not form['c_check']:
 
93
        raise wizard.except_wizard(_('User Error'), _('You have to check one box !'))
 
94
    pool = pooler.get_pool(cr.dbname)
 
95
    prod_obj = pool.get('product.product')
 
96
    line_inv_obj = pool.get('account.invoice.line')
 
97
    updated_rep_line = []
 
98
    cond=''
 
99
    valor=''
 
100
 
 
101
#TODO
 
102
 
 
103
    if form['u_check']:
 
104
        vis=('user','user_id','user_id','user_id')
 
105
        xml_id = 'action_profit_user_product_tree'
 
106
        if form['user_res_id']:
 
107
            valor = form['user_res_id']
 
108
 
 
109
 
 
110
    if form['p_check']:
 
111
        vis=('partner','partner_id','partner_id','partner_id')
 
112
        xml_id = 'action_profit_partner_product_tree'
 
113
        if form['partner_res_id']:
 
114
            valor = form['partner_res_id']
 
115
 
 
116
 
 
117
    if form['c_check']:
 
118
        vis=('category','cat_id','cat_id','cat_id')
 
119
        xml_id = 'action_profit_category_product_tree'
 
120
        if form['cat_res_id']:
 
121
            valor = form['cat_res_id']
 
122
 
 
123
 
 
124
    if form['u_check'] and form['p_check']:
 
125
        vis=('uxp','((user_id*1000000)+partner_id)','user_id,partner_id','user_id,partner_id')
 
126
        xml_id = 'action_profit_uxp_product_tree'
 
127
        valor = ''
 
128
        if form['user_res_id']:
 
129
            cond = ' and user_id=%s' % form['user_res_id']
 
130
        if form['partner_res_id']:
 
131
            cond = ' and partner_id=%s' % form['partner_res_id']
 
132
        if form['user_res_id'] and form['partner_res_id']:
 
133
            cond = ' and user_id=%s and partner_id=%s' % (form['user_res_id'],form['partner_res_id'])
 
134
 
 
135
 
 
136
    if form['u_check'] and form['c_check']:
 
137
        vis=('uxc','((user_id*1000000)+cat_id)','user_id,cat_id','user_id,cat_id')
 
138
        xml_id = 'action_profit_uxc_product_tree'
 
139
        valor = ''
 
140
        if form['user_res_id']:
 
141
            cond = ' and user_id=%s' % form['user_res_id']
 
142
        if form['cat_res_id']:
 
143
            cond = ' and cat_id=%s' % form['cat_res_id']
 
144
        if form['user_res_id'] and form['cat_res_id']:
 
145
            cond = ' and user_id=%s and cat_id=%s' % (form['user_res_id'],form['cat_res_id'])
 
146
 
 
147
 
 
148
    if form['p_check'] and form['c_check']:
 
149
        vis=('pxc','((cat_id*1000000)+partner_id)','partner_id,cat_id','partner_id,cat_id')
 
150
        xml_id = 'action_profit_pxc_product_tree'
 
151
        valor = ''
 
152
        if form['partner_res_id']:
 
153
            cond = ' and partner_id=%s' % form['partner_res_id']
 
154
        if form['cat_res_id']:
 
155
            cond = ' and cat_id=%s' % form['cat_res_id']
 
156
        if form['partner_res_id'] and form['cat_res_id']:
 
157
            cond = ' and partner_id=%s and cat_id=%s' % (form['partner_res_id'],form['cat_res_id'])
 
158
 
 
159
 
 
160
    if form['u_check'] and form['p_check'] and form['c_check']:
 
161
        vis=('upc','((user_id*100000000000)+(cat_id*1000000)+partner_id)','user_id,partner_id,cat_id','user_id,partner_id,cat_id')
 
162
        xml_id = 'action_profit_upc_product_tree'
 
163
        valor = ''
 
164
        if form['partner_res_id']:
 
165
            cond = ' and partner_id=%s' % form['partner_res_id']
 
166
        if form['cat_res_id']:
 
167
            cond = ' and cat_id=%s' % form['cat_res_id']
 
168
        if form['partner_res_id'] and form['cat_res_id']:
 
169
            cond = ' and partner_id=%s and cat_id=%s' % (form['partner_res_id'],form['cat_res_id'])
 
170
        if form['user_res_id'] and form['partner_res_id']:
 
171
            cond = ' and user_id=%s and partner_id=%s' % (form['user_res_id'],form['partner_res_id'])
 
172
        if form['user_res_id'] and form['cat_res_id']:
 
173
            cond = ' and user_id=%s and cat_id=%s' % (form['user_res_id'],form['cat_res_id'])
 
174
        if form['user_res_id'] and form['partner_res_id'] and form['cat_res_id']:
 
175
            cond = ' and user_id=%s and partner_id=%s and cat_id=%s' % (form['user_res_id'],form['partner_res_id'],form['cat_res_id'])
 
176
 
 
177
 
 
178
    cond = valor and ' and '+vis[1]+'=%s' % valor or cond
 
179
    sql = """
 
180
        create or replace view report_profit_%s as (
 
181
        select
 
182
            %s as id,
 
183
            %s,        
 
184
            SUM(last_cost) as sum_last_cost,
 
185
            SUM(price_subtotal) as sum_price_subtotal,
 
186
            SUM(qty_consol) as sum_qty_consol,
 
187
            p_uom_c_id
 
188
        from report_profit p
 
189
        where p.name>='%s' and p.name<='%s'%s
 
190
        group by %s,p_uom_c_id
 
191
    )
 
192
""" % (vis[0],vis[1],vis[2],form['date_start'],form['date_end'],cond,vis[3])
 
193
 
 
194
    sql2 = """SELECT id FROM report_profit_%s""" % vis[0]
 
195
 
 
196
    cr.execute(sql)
 
197
    cr.execute(sql2)
 
198
    res = cr.fetchall()
 
199
    updated_rep_line = map(lambda x:x[0],res)
 
200
 
 
201
    if None in updated_rep_line:
 
202
        raise wizard.except_wizard(_('User Error'), _('You have to check salesman or product !'))
 
203
 
 
204
    mod_obj = pool.get('ir.model.data')
 
205
    act_obj = pool.get('ir.actions.act_window')
 
206
 
 
207
 
 
208
    #we get the model
 
209
    result = mod_obj._get_id(cr, uid, 'report_profit', xml_id)
 
210
    id = mod_obj.read(cr, uid, result, ['res_id'])['res_id']
 
211
    # we read the act window
 
212
    result = act_obj.read(cr, uid, id)
 
213
    result['res_id'] = updated_rep_line
 
214
 
 
215
 
 
216
    return result
 
217
 
 
218
class wiz_last_cost_sum(wizard.interface):
 
219
    states = {
 
220
        'init': {
 
221
            'actions': [],
 
222
            'result': {'type': 'form', 'arch':_transaction_form, 'fields':_transaction_fields, 'state':[('end','Cancel'),('change','Update')]}
 
223
        },
 
224
        'change': {
 
225
            'actions': [],
 
226
            'result': {'type': 'action', 'action':_data_save, 'state':'end'}
 
227
        }
 
228
    }
 
229
wiz_last_cost_sum('profit.update.costprice.sum')
 
230
 
 
231
 
 
232
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
233