~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to report_profit/tmp/wiz/wizard_last_cost_price2.py

  • Committer: Openerp User
  • Author(s): Vauxoo
  • Date: 2012-07-26 20:13:13 UTC
  • Revision ID: openerp@ingelub-erp-20120726201313-cgffyvd3zmqhn2w4

[FIX] bug a la hora de confirmar una orden de venta cuando el producto es de tipo servicio 
tomaba dicho porducto como si este fuera almacenable y tuviera stock cosa que no es asi 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2009 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
_transaction_form = '''<?xml version="1.0"?>
 
37
<form string="Update Last Cost Price">
 
38
    <separator string="Analyze Unit of Last Cost Price" colspan="4"/>
 
39
     <field name="uom_c_id" widget="selection"/>
 
40
</form>'''
 
41
 
 
42
_transaction_fields = {    
 
43
    'uom_c_id': {'string':'Consolidate Unit', 'type':'many2one', 'relation': 'product.uom.consol','required':True},
 
44
   
 
45
}
 
46
 
 
47
def _data_save(self, cr, uid, data, context):
 
48
    pool = pooler.get_pool(cr.dbname)
 
49
    prod_obj = pool.get('product.product')
 
50
    line_inv_obj = pool.get('account.invoice.line')
 
51
    updated_inv_line = []
 
52
 
 
53
    cr.execute("""
 
54
        create or replace view report_profit as (
 
55
            select
 
56
                l.id as id,
 
57
                to_char(i.date_invoice, 'YYYY-MM-DD') as name,
 
58
                l.product_id as product_id,
 
59
                p.id as partner_id,
 
60
                u.id as user_id,
 
61
                l.quantity as quantity,
 
62
                case when i.type='out_refund'
 
63
                    then
 
64
                        l.price_unit*(-1)
 
65
                    else
 
66
                        l.price_unit 
 
67
                end as price_unit,
 
68
                case when i.type='out_refund'
 
69
                    then
 
70
                        l.last_price*(-1)
 
71
                    else
 
72
                        l.last_price 
 
73
                end as last_cost,                 
 
74
                case when i.type='out_refund'
 
75
                    then
 
76
                        l.price_subtotal*(-1)
 
77
                    else
 
78
                        l.price_subtotal 
 
79
                end as price_subtotal,
 
80
                case when i.type='out_refund'
 
81
                    then
 
82
                        (l.quantity*l.last_price)*(-1)
 
83
                    else
 
84
                        (l.quantity*l.last_price) 
 
85
                end as last_cost_subtotal,
 
86
                case when i.type='out_refund'
 
87
                    then
 
88
                        (price_subtotal-l.quantity*l.last_price)*(-1)
 
89
                    else
 
90
                        (price_subtotal-l.quantity*l.last_price)
 
91
                end as profit,
 
92
                case when i.type='out_refund'
 
93
                    then
 
94
                        ((price_subtotal-l.quantity*l.last_price)*(-1)/(price_subtotal)*100)
 
95
                    else
 
96
                        ((price_subtotal-l.quantity*l.last_price)/(price_subtotal)*100)
 
97
                end as perc,
 
98
                l.uos_id as uom_id,
 
99
                p.name as partner,
 
100
                i.type as type,
 
101
                c.p_uom_c_id as p_uom_c_id,
 
102
                case when i.type='out_refund'     
 
103
                    then                           
 
104
                        (l.quantity*c.factor_consol)*(-1)
 
105
                    else
 
106
                        (l.quantity*c.factor_consol)
 
107
                end as qty_consol,
 
108
                t.categ_id as cat_id
 
109
            from account_invoice i
 
110
                inner join res_partner p on (p.id=i.partner_id)
 
111
                left join res_users u on (u.id=p.user_id)
 
112
                right join account_invoice_line l on (i.id=l.invoice_id)
 
113
                left join product_uom m on (m.id=l.uos_id)
 
114
                left join product_uom_consol_line c on (m.id=c.p_uom_id)
 
115
                left join product_template t on (t.id=l.product_id)
 
116
                left join product_product d on (d.product_tmpl_id=l.product_id)
 
117
            where l.quantity != 0 and i.type in ('out_invoice', 'out_refund') and i.state in ('open', 'paid') and l.uos_id in (
 
118
                select
 
119
                    u.id as id
 
120
                from product_uom u
 
121
                    inner join product_uom_consol_line c on (u.id=c.p_uom_id)
 
122
                where c.p_uom_c_id=%s
 
123
            )
 
124
            group by l.id,to_char(i.date_invoice, 'YYYY-MM-DD'),l.product_id,p.id,u.id,l.quantity,l.price_unit,l.last_price,l.price_subtotal,l.uos_id,p.name,i.type,c.p_uom_c_id,c.factor_consol,t.categ_id
 
125
 
 
126
        )
 
127
    """, (data['form']['uom_c_id'],))
 
128
    sql = """
 
129
        SELECT id FROM report_profit"""
 
130
    cr.execute(sql)
 
131
    res = cr.fetchall()
 
132
    for line in line_inv_obj.browse(cr, uid, map(lambda x:x[0],res)):
 
133
        if line.invoice_id.state in ('open', 'paid'):
 
134
            inv_id = line.invoice_id.parent_id and line.invoice_id.parent_id.id or line.invoice_id.id            
 
135
            prod_price = prod_obj._product_get_price(cr, uid, [line.product_id.id], inv_id, False, line.invoice_id.date_invoice, context, ('open', 'paid'), 'in_invoice')
 
136
            if (not prod_price[line.product_id.id]) and line.product_id.seller_ids:
 
137
                supinfo_ids = []
 
138
                for sup in line.product_id.seller_ids:
 
139
                    supinfo_ids.append(sup.id)
 
140
                cr.execute('select max(price) from pricelist_partnerinfo where suppinfo_id in ('+','.join(map(str,supinfo_ids))+')')
 
141
                record = cr.fetchone()
 
142
                prod_price[line.product_id.id] = record and record[0] or False
 
143
 
 
144
            line_inv_obj.write(cr, uid,line.id, {'last_price':prod_price[line.product_id.id]}, context=context)
 
145
            updated_inv_line.append(line.id)
 
146
    #we get the view id
 
147
    mod_obj = pool.get('ir.model.data')
 
148
    act_obj = pool.get('ir.actions.act_window')
 
149
 
 
150
    xml_id = 'action_profit_product_tree'
 
151
 
 
152
    #we get the model
 
153
    result = mod_obj._get_id(cr, uid, 'report_profit', xml_id)
 
154
    id = mod_obj.read(cr, uid, result, ['res_id'])['res_id']
 
155
    # we read the act window
 
156
    result = act_obj.read(cr, uid, id)
 
157
    result['res_id'] = updated_inv_line
 
158
 
 
159
 
 
160
    return result
 
161
 
 
162
class wiz_last_cost2(wizard.interface):
 
163
    states = {
 
164
        'init': {
 
165
            'actions': [],
 
166
            'result': {'type': 'form', 'arch':_transaction_form, 'fields':_transaction_fields, 'state':[('end','Cancel'),('change','Update')]}
 
167
        },
 
168
        'change': {
 
169
            'actions': [],
 
170
            'result': {'type': 'action', 'action':_data_save, 'state':'end'}
 
171
        }
 
172
    }
 
173
wiz_last_cost2('profit.update.costprice2')
 
174
 
 
175
 
 
176
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
177