~zaber/openobject-addons/stable_5.0-extra-addons

« back to all changes in this revision

Viewing changes to sale_margin/object/sale_margin.py

  • Committer: Sebastien LANGE
  • Date: 2009-12-05 15:41:56 UTC
  • Revision ID: sebastien.lange@syleam.fr-20091205154156-8ag8u7qb7h0bd6pz
[IMP] sale_margin : add pourcent margin in sale order line and sale order
the original module is renamed in sale_margin_old

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
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
 
8
#
 
9
#    This file is a part of sale_margin
 
10
#
 
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.
 
15
#
 
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.
 
20
#
 
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/>.
 
23
#
 
24
##############################################################################
 
25
 
 
26
from osv import fields,osv
 
27
import pooler
 
28
from tools import config
 
29
import time
 
30
 
 
31
class sale_order_line(osv.osv):
 
32
    _inherit = "sale.order.line"
 
33
 
 
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.
 
36
        :param pv: sale price
 
37
        :param pa: purchase price
 
38
        :param margin: amount margin"""
 
39
 
 
40
        res={}
 
41
        res['value']={}
 
42
        if product:
 
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']
 
53
        return res
 
54
 
 
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.
 
57
        :param pv: sale price
 
58
        :param pa: purchase price
 
59
        :param margin: amount margin"""
 
60
 
 
61
        res={}
 
62
        res['value']={}
 
63
        if product:
 
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']
 
74
        return res
 
75
 
 
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.
 
78
        :param pv: sale price
 
79
        :param pa: purchase price
 
80
        :param margin: amount margin"""
 
81
 
 
82
        temp={}
 
83
        temp['product']=''
 
84
        if price_unit>0:
 
85
            temp['price_unit']=price_unit
 
86
            if product:
 
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)
 
89
        if product:
 
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']
 
102
 
 
103
        return res
 
104
 
 
105
    _columns = {
 
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']))),
 
109
    }
 
110
 
 
111
sale_order_line()
 
112
 
 
113
class sale_order(osv.osv):
 
114
    _inherit = "sale.order"
 
115
 
 
116
    def _amount_all(self, cr, uid, ids, field_name, arg, context):
 
117
        """Calculate the amount total of margin"""
 
118
 
 
119
        res = {}
 
120
        res=super(sale_order, self)._amount_all(cr, uid, ids, field_name, arg, context)
 
121
        for order in self.browse(cr, uid, ids):
 
122
            val = 0.0
 
123
 
 
124
            for line in order.order_line:
 
125
                val += line.margin
 
126
            res[order.id]['margin'] = val
 
127
        return res
 
128
 
 
129
    def _get_order(self, cr, uid, ids, context={}):
 
130
        result = {}
 
131
        for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
 
132
            result[line.order_id.id] = True
 
133
        return result.keys()
 
134
 
 
135
    _columns = {
 
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',
 
138
            store = {
 
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),
 
141
            },
 
142
            multi='sums',digits=(16, int(config['price_accuracy']))),
 
143
        'amount_tax': fields.function(_amount_all, method=True, string='Taxes',
 
144
            store = {
 
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),
 
147
            },
 
148
            multi='sums'),
 
149
        'amount_total': fields.function(_amount_all, method=True, string='Total',
 
150
            store = {
 
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),
 
153
            },
 
154
            multi='sums',digits=(16, int(config['price_accuracy']))),
 
155
    }
 
156
 
 
157
sale_order()
 
158
 
 
159
class stock_picking(osv.osv):
 
160
    _inherit = 'stock.picking'
 
161
 
 
162
    _columns = {
 
163
        'invoice_ids': fields.many2many('account.invoice', 'picking_invoice_rel', 'picking_id', 'invoice_id', 'Invoices', domain=[('type','=','in_invoice')]),
 
164
    }
 
165
 
 
166
    def create_invoice(self, cr, uid, ids, *args):
 
167
        # need to carify with new requirement
 
168
        res = False
 
169
        invoice_ids = []
 
170
        margin_deduce = 0.0
 
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]]})
 
176
        return True
 
177
 
 
178
stock_picking()
 
179
 
 
180
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: