~asteam/asdaily/santhosh

« back to all changes in this revision

Viewing changes to addons/product_margin/product_margin.py

  • Committer: santhoshk755 at gmail
  • Date: 2014-09-09 08:37:42 UTC
  • Revision ID: santhoshk755@gmail.com-20140909083742-7u5ixs24jy3x8xxg
santhosh

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import time
 
23
 
 
24
from openerp.osv import fields, osv
 
25
 
 
26
 
 
27
class product_product(osv.osv):
 
28
    _inherit = "product.product"
 
29
 
 
30
    def _product_margin(self, cr, uid, ids, field_names, arg, context=None):
 
31
        res = {}
 
32
        if context is None:
 
33
            context = {}
 
34
 
 
35
        for val in self.browse(cr, uid, ids, context=context):
 
36
            res[val.id] = {}
 
37
            date_from = context.get('date_from', time.strftime('%Y-01-01'))
 
38
            date_to = context.get('date_to', time.strftime('%Y-12-31'))
 
39
            invoice_state = context.get('invoice_state', 'open_paid')
 
40
            if 'date_from' in field_names:
 
41
                res[val.id]['date_from'] = date_from
 
42
            if 'date_to' in field_names:
 
43
                res[val.id]['date_to'] = date_to
 
44
            if 'invoice_state' in field_names:
 
45
                res[val.id]['invoice_state'] = invoice_state
 
46
            invoice_types = ()
 
47
            states = ()
 
48
            if invoice_state == 'paid':
 
49
                states = ('paid',)
 
50
            elif invoice_state == 'open_paid':
 
51
                states = ('open', 'paid')
 
52
            elif invoice_state == 'draft_open_paid':
 
53
                states = ('draft', 'open', 'paid')
 
54
            if "force_company" in context:
 
55
                company_id = context['force_company']
 
56
            else:
 
57
                company_id = self.pool.get("res.users").browse(cr, uid, uid, context=context).company_id.id
 
58
 
 
59
            #Cost price is calculated afterwards as it is a property
 
60
            sqlstr="""select
 
61
                    sum(l.price_unit * l.quantity)/sum(nullif(l.quantity * pu.factor / pu2.factor,0)) as avg_unit_price,
 
62
                    sum(l.quantity * pu.factor / pu2.factor) as num_qty,
 
63
                    sum(l.quantity * (l.price_subtotal/(nullif(l.quantity,0)))) as total,
 
64
                    sum(l.quantity * pu.factor * pt.list_price / pu2.factor) as sale_expected
 
65
                from account_invoice_line l
 
66
                left join account_invoice i on (l.invoice_id = i.id)
 
67
                left join product_product product on (product.id=l.product_id)
 
68
                left join product_template pt on (pt.id = l.product_id)
 
69
                    left join product_uom pu on (pt.uom_id = pu.id)
 
70
                    left join product_uom pu2 on (l.uos_id = pu2.id)
 
71
                where l.product_id = %s and i.state in %s and i.type IN %s and (i.date_invoice IS NULL or (i.date_invoice>=%s and i.date_invoice<=%s and i.company_id=%s))
 
72
                """
 
73
            invoice_types = ('out_invoice', 'in_refund')
 
74
            cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to, company_id))
 
75
            result = cr.fetchall()[0]
 
76
            res[val.id]['sale_avg_price'] = result[0] and result[0] or 0.0
 
77
            res[val.id]['sale_num_invoiced'] = result[1] and result[1] or 0.0
 
78
            res[val.id]['turnover'] = result[2] and result[2] or 0.0
 
79
            res[val.id]['sale_expected'] = result[3] and result[3] or 0.0
 
80
            res[val.id]['sales_gap'] = res[val.id]['sale_expected']-res[val.id]['turnover']
 
81
            prod_obj = self.pool.get("product.product")
 
82
            ctx = context.copy()
 
83
            ctx['force_company'] = company_id
 
84
            prod = prod_obj.browse(cr, uid, val.id, context=ctx)
 
85
            invoice_types = ('in_invoice', 'out_refund')
 
86
            cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to, company_id))
 
87
            result = cr.fetchall()[0]
 
88
            res[val.id]['purchase_avg_price'] = result[0] and result[0] or 0.0
 
89
            res[val.id]['purchase_num_invoiced'] = result[1] and result[1] or 0.0
 
90
            res[val.id]['total_cost'] = result[2] and result[2] or 0.0
 
91
            res[val.id]['normal_cost'] = prod.standard_price * res[val.id]['purchase_num_invoiced']
 
92
            res[val.id]['purchase_gap'] = res[val.id]['normal_cost'] - res[val.id]['total_cost']
 
93
 
 
94
            if 'total_margin' in field_names:
 
95
                res[val.id]['total_margin'] = res[val.id]['turnover'] - res[val.id]['total_cost']
 
96
            if 'expected_margin' in field_names:
 
97
                res[val.id]['expected_margin'] = res[val.id]['sale_expected'] - res[val.id]['normal_cost']
 
98
            if 'total_margin_rate' in field_names:
 
99
                res[val.id]['total_margin_rate'] = res[val.id]['turnover'] and res[val.id]['total_margin'] * 100 / res[val.id]['turnover'] or 0.0
 
100
            if 'expected_margin_rate' in field_names:
 
101
                res[val.id]['expected_margin_rate'] = res[val.id]['sale_expected'] and res[val.id]['expected_margin'] * 100 / res[val.id]['sale_expected'] or 0.0
 
102
        return res
 
103
 
 
104
    _columns = {
 
105
        'date_from': fields.function(_product_margin, type='date', string='Margin Date From', multi='product_margin'),
 
106
        'date_to': fields.function(_product_margin, type='date', string='Margin Date To', multi='product_margin'),
 
107
        'invoice_state': fields.function(_product_margin, type='selection', selection=[
 
108
                ('paid','Paid'),('open_paid','Open and Paid'),('draft_open_paid','Draft, Open and Paid')
 
109
            ], string='Invoice State',multi='product_margin', readonly=True),
 
110
        'sale_avg_price' : fields.function(_product_margin, type='float', string='Avg. Unit Price', multi='product_margin',
 
111
            help="Avg. Price in Customer Invoices."),
 
112
        'purchase_avg_price' : fields.function(_product_margin, type='float', string='Avg. Unit Price', multi='product_margin',
 
113
            help="Avg. Price in Supplier Invoices "),
 
114
        'sale_num_invoiced' : fields.function(_product_margin, type='float', string='# Invoiced in Sale', multi='product_margin',
 
115
            help="Sum of Quantity in Customer Invoices"),
 
116
        'purchase_num_invoiced' : fields.function(_product_margin, type='float', string='# Invoiced in Purchase', multi='product_margin',
 
117
            help="Sum of Quantity in Supplier Invoices"),
 
118
        'sales_gap' : fields.function(_product_margin, type='float', string='Sales Gap', multi='product_margin',
 
119
            help="Expected Sale - Turn Over"),
 
120
        'purchase_gap' : fields.function(_product_margin, type='float', string='Purchase Gap', multi='product_margin',
 
121
            help="Normal Cost - Total Cost"),
 
122
        'turnover' : fields.function(_product_margin, type='float', string='Turnover' ,multi='product_margin',
 
123
            help="Sum of Multiplication of Invoice price and quantity of Customer Invoices"),
 
124
        'total_cost'  : fields.function(_product_margin, type='float', string='Total Cost', multi='product_margin',
 
125
            help="Sum of Multiplication of Invoice price and quantity of Supplier Invoices "),
 
126
        'sale_expected' :  fields.function(_product_margin, type='float', string='Expected Sale', multi='product_margin',
 
127
            help="Sum of Multiplication of Sale Catalog price and quantity of Customer Invoices"),
 
128
        'normal_cost'  : fields.function(_product_margin, type='float', string='Normal Cost', multi='product_margin',
 
129
            help="Sum of Multiplication of Cost price and quantity of Supplier Invoices"),
 
130
        'total_margin' : fields.function(_product_margin, type='float', string='Total Margin', multi='product_margin',
 
131
            help="Turnover - Standard price"),
 
132
        'expected_margin' : fields.function(_product_margin, type='float', string='Expected Margin', multi='product_margin',
 
133
            help="Expected Sale - Normal Cost"),
 
134
        'total_margin_rate' : fields.function(_product_margin, type='float', string='Total Margin Rate(%)', multi='product_margin',
 
135
            help="Total margin * 100 / Turnover"),
 
136
        'expected_margin_rate' : fields.function(_product_margin, type='float', string='Expected Margin (%)', multi='product_margin',
 
137
            help="Expected margin * 100 / Expected Sale"),
 
138
    }
 
139
 
 
140
 
 
141
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: