1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# Copyright (c) 2009 Vauxoo C.A. (http://openerp.com.ve/) All Rights Reserved.
5
# Javier Duran <javier@vauxoo.com>
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
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.
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.
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.
29
##############################################################################
34
from tools.translate import _
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"/>
42
_transaction_fields = {
43
'uom_c_id': {'string':'Consolidate Unit', 'type':'many2one', 'relation': 'product.uom.consol','required':True},
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')
54
create or replace view report_profit as (
57
to_char(i.date_invoice, 'YYYY-MM-DD') as name,
58
l.product_id as product_id,
61
l.quantity as quantity,
62
case when i.type='out_refund'
68
case when i.type='out_refund'
74
case when i.type='out_refund'
79
end as price_subtotal,
80
case when i.type='out_refund'
82
(l.quantity*l.last_price)*(-1)
84
(l.quantity*l.last_price)
85
end as last_cost_subtotal,
86
case when i.type='out_refund'
88
(price_subtotal-l.quantity*l.last_price)*(-1)
90
(price_subtotal-l.quantity*l.last_price)
92
case when i.type='out_refund'
94
((price_subtotal-l.quantity*l.last_price)*(-1)/(price_subtotal)*100)
96
((price_subtotal-l.quantity*l.last_price)/(price_subtotal)*100)
101
c.p_uom_c_id as p_uom_c_id,
102
case when i.type='out_refund'
104
(l.quantity*c.factor_consol)*(-1)
106
(l.quantity*c.factor_consol)
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 (
121
inner join product_uom_consol_line c on (u.id=c.p_uom_id)
122
where c.p_uom_c_id=%s
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
127
""", (data['form']['uom_c_id'],))
129
SELECT id FROM report_profit"""
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:
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
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)
147
mod_obj = pool.get('ir.model.data')
148
act_obj = pool.get('ir.actions.act_window')
150
xml_id = 'action_profit_product_tree'
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
162
class wiz_last_cost2(wizard.interface):
166
'result': {'type': 'form', 'arch':_transaction_form, 'fields':_transaction_fields, 'state':[('end','Cancel'),('change','Update')]}
170
'result': {'type': 'action', 'action':_data_save, 'state':'end'}
173
wiz_last_cost2('profit.update.costprice2')
176
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: