1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# sale_margin module for OpenERP, add margin in sale order and invoice
5
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
# Copyright (C) 2009 EVERLIBRE (<http://www.Everlibre.fr>) Éric VERNICHON
7
# Copyright (C) 2009 SYLEAM (<http://www.Syleam.fr>) Sebastien LANGE
9
# This file is a part of sale_margin
11
# sale_margin is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation, either version 3 of the License, or
14
# (at your option) any later version.
16
# sale_margin is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
# GNU General Public License for more details.
21
# You should have received a copy of the GNU General Public License
22
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
##############################################################################
26
from osv import fields,osv
28
from tools import config
31
class sale_order_line(osv.osv):
32
_inherit = "sale.order.line"
34
def price_unit_change(self, cr, uid, ids, purchase_price,product_uom_qty,product_uos_qty,price_unit,product,discount):
35
"""If price unit change, calcul the new margin.
37
:param pa: purchase price
38
:param margin: amount margin"""
43
product_tmpl_obj = self.pool.get('product.template')
44
product_obj = self.pool.get('product.product')
45
pr=product_obj.read(cr,uid,product)
46
product_value= product_tmpl_obj.read(cr,uid,pr['product_tmpl_id'][0] )
47
pv=(price_unit*product_uom_qty*(100.0-discount)/100.0)
48
pa=(product_value['standard_price']*product_uom_qty)
49
margin=round(pv -pa,int(config['price_accuracy']))
50
res['value']['margin']=margin
51
res['value']['marginpourcent']=(((pv-pa)/pv)*100)
52
res['value']['purchase_price']=product_value['standard_price']
55
def discount_change(self, cr, uid, ids, purchase_price,product_uom_qty,product_uos_qty,price_unit,product,discount):
56
"""If discount change, calcul the new margin.
58
:param pa: purchase price
59
:param margin: amount margin"""
64
product_tmpl_obj = self.pool.get('product.template')
65
product_obj = self.pool.get('product.product')
66
pr=product_obj.read(cr,uid,product)
67
product_value= product_tmpl_obj.read(cr,uid,pr['product_tmpl_id'][0] )
68
pv=(price_unit*product_uom_qty*(100.0-discount)/100.0)
69
pa=(product_value['standard_price']*product_uom_qty)
70
margin=round(pv -pa,int(config['price_accuracy']))
71
res['value']['margin']=margin
72
res['value']['marginpourcent']=(((pv-pa)/pv)*100)
73
res['value']['purchase_price']=product_value['standard_price']
76
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,uom=False, qty_uos=0, uos=False, name='', partner_id=False,lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False,discount=0.0,price_unit=0.0):
77
"""If product change, calcul the new margin.
79
:param pa: purchase price
80
:param margin: amount margin"""
85
temp['price_unit']=price_unit
87
temp['product']=product
88
res=super(sale_order_line, self).product_id_change( cr, uid, ids, pricelist, product,qty,uom,qty_uos,uos,name,partner_id,lang,update_tax,date_order,packaging,fiscal_position,flag)
90
if product==temp['product']:
91
res['value']['price_unit']=price_unit
92
product_tmpl_obj = self.pool.get('product.template')
93
product_obj = self.pool.get('product.product')
94
pr=product_obj.read(cr,uid,product)
95
product_value= product_tmpl_obj.read(cr,uid,pr['product_tmpl_id'][0] )
96
pv=(res['value']['price_unit']*res['value']['product_uos_qty']*(100.0-discount)/100.0)
97
pa=(product_value['standard_price']*res['value']['product_uos_qty'])
98
margin=round(pv - pa,int(config['price_accuracy']))
99
res['value']['margin']=margin
100
res['value']['marginpourcent']=(((pv-pa)/pv)*100)
101
res['value']['purchase_price']=product_value['standard_price']
106
'margin': fields.float('Margin',digits=(16, int(config['price_accuracy']))),
107
'marginpourcent': fields.float('Margin %',digits=(16, int(config['price_accuracy']))),
108
'purchase_price': fields.float('Cost Price', digits=(16, int(config['price_accuracy']))),
113
class sale_order(osv.osv):
114
_inherit = "sale.order"
116
def _amount_all(self, cr, uid, ids, field_name, arg, context):
117
"""Calculate the amount total of margin"""
120
res=super(sale_order, self)._amount_all(cr, uid, ids, field_name, arg, context)
121
for order in self.browse(cr, uid, ids):
124
for line in order.order_line:
126
res[order.id]['margin'] = val
129
def _get_order(self, cr, uid, ids, context={}):
131
for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
132
result[line.order_id.id] = True
136
'margin': fields.function(_amount_all, method=True, string='Margin',multi='sums',digits=(16, int(config['price_accuracy']))),
137
'amount_untaxed': fields.function(_amount_all, method=True, string='Untaxed Amount',
139
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
140
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
142
multi='sums',digits=(16, int(config['price_accuracy']))),
143
'amount_tax': fields.function(_amount_all, method=True, string='Taxes',
145
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
146
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
149
'amount_total': fields.function(_amount_all, method=True, string='Total',
151
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
152
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
154
multi='sums',digits=(16, int(config['price_accuracy']))),
159
class stock_picking(osv.osv):
160
_inherit = 'stock.picking'
163
'invoice_ids': fields.many2many('account.invoice', 'picking_invoice_rel', 'picking_id', 'invoice_id', 'Invoices', domain=[('type','=','in_invoice')]),
166
def create_invoice(self, cr, uid, ids, *args):
167
# need to carify with new requirement
171
picking_obj = self.pool.get('stock.picking')
172
picking_obj.write(cr, uid, ids, {'invoice_state' : '2binvoiced'})
173
res = picking_obj.action_invoice_create(cr, uid, ids, type='out_invoice', context={})
174
invoice_ids = res.values()
175
picking_obj.write(cr, uid, ids,{'invoice_ids':[[6,0,invoice_ids]]})
180
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: