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

« back to all changes in this revision

Viewing changes to sale_margin/object/report_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
 
 
32
 
 
33
class report_account_invoice_product(osv.osv):
 
34
    _name = 'report.account.invoice.product'
 
35
    _auto = False
 
36
    _columns = {
 
37
        'name': fields.date('Month', readonly=True),
 
38
        'type': fields.selection([
 
39
            ('out_invoice','Customer Invoice'),
 
40
            ('in_invoice','Supplier Invoice'),
 
41
            ('out_refund','Customer Refund'),
 
42
            ('in_refund','Supplier Refund'),
 
43
        ],'Type', readonly=True),
 
44
        'state': fields.selection([
 
45
            ('draft','Draft'),
 
46
            ('proforma','Pro-forma'),
 
47
            ('open','Open'),
 
48
            ('paid','Paid'),
 
49
            ('cancel','Canceled')
 
50
        ],'State', readonly=True),
 
51
        'product_id': fields.many2one('product.product', 'Product', readonly=True),
 
52
        'amount': fields.float('Amount', readonly=True),
 
53
        'cost_price': fields.float('Cost Price', readonly=True),
 
54
        'margin': fields.float('Margin', readonly=True),
 
55
        'quantity': fields.float('Quantity', readonly=True),
 
56
    }
 
57
    _order = 'name desc'
 
58
    def init(self, cr):
 
59
        cr.execute("""
 
60
            create or replace view report_account_invoice_product as (
 
61
                select
 
62
                    min(l.id) as id,
 
63
                    to_char(i.date_invoice, 'YYYY-MM-01') as name,
 
64
                    i.type,
 
65
                    i.state,
 
66
                    l.product_id,
 
67
                    sum(l.quantity * l.price_unit * (1.0 - l.discount/100.0)) as amount,
 
68
                    sum(l.quantity * l.cost_price) as cost_price,
 
69
                    sum((l.quantity * l.price_unit * (1.0 - l.discount/100.0) - (l.quantity * l.cost_price))) as margin,
 
70
                    sum(l.quantity) as quantity
 
71
                from account_invoice i
 
72
                    left join account_invoice_line l on (i.id = l.invoice_id)
 
73
                group by l.product_id, to_char(i.date_invoice, 'YYYY-MM-01'), i.type, i.state
 
74
            )
 
75
        """)
 
76
report_account_invoice_product()
 
77
 
 
78
class report_account_invoice_category(osv.osv):
 
79
    _name = 'report.account.invoice.category'
 
80
    _auto = False
 
81
    _columns = {
 
82
        'name': fields.date('Month', readonly=True),
 
83
        'type': fields.selection([
 
84
            ('out_invoice','Customer Invoice'),
 
85
            ('in_invoice','Supplier Invoice'),
 
86
            ('out_refund','Customer Refund'),
 
87
            ('in_refund','Supplier Refund'),
 
88
        ],'Type', readonly=True),
 
89
        'state': fields.selection([
 
90
            ('draft','Draft'),
 
91
            ('proforma','Pro-forma'),
 
92
            ('open','Open'),
 
93
            ('paid','Paid'),
 
94
            ('cancel','Canceled')
 
95
        ],'State', readonly=True),
 
96
        'categ_id': fields.many2one('product.category', 'Categories', readonly=True),
 
97
        'amount': fields.float('Amount', readonly=True),
 
98
        'cost_price': fields.float('Cost Price', readonly=True),
 
99
        'margin': fields.float('Margin', readonly=True),
 
100
        'quantity': fields.float('Quantity', readonly=True),
 
101
    }
 
102
    _order = 'name desc'
 
103
    def init(self, cr):
 
104
        cr.execute("""
 
105
            create or replace view report_account_invoice_category as (
 
106
                select
 
107
                    min(l.id) as id,
 
108
                    to_char(i.date_invoice, 'YYYY-MM-01') as name,
 
109
                    i.type,
 
110
                    i.state,
 
111
                    t.categ_id,
 
112
                    sum(l.quantity * l.price_unit * (1.0 - l.discount/100.0)) as amount,
 
113
                    sum(l.quantity * l.cost_price) as cost_price,
 
114
                    sum((l.quantity * l.price_unit * (1.0 - l.discount/100.0) - (l.quantity * l.cost_price))) as margin,
 
115
                    sum(l.quantity) as quantity
 
116
                from account_invoice i
 
117
                    left join account_invoice_line l on (i.id = l.invoice_id)
 
118
                    left join product_product p on (p.id = l.product_id)
 
119
                    left join product_template t on (t.id = p.product_tmpl_id)
 
120
                group by t.categ_id, to_char(i.date_invoice, 'YYYY-MM-01'), i.type, i.state
 
121
            )
 
122
        """)
 
123
report_account_invoice_category()
 
124
 
 
125
class report_account_invoice_partner(osv.osv):
 
126
    _name = 'report.account.invoice.partner'
 
127
    _auto = False
 
128
    _columns = {
 
129
        'name': fields.date('Month', readonly=True),
 
130
        'type': fields.selection([
 
131
            ('out_invoice','Customer Invoice'),
 
132
            ('in_invoice','Supplier Invoice'),
 
133
            ('out_refund','Customer Refund'),
 
134
            ('in_refund','Supplier Refund'),
 
135
        ],'Type', readonly=True),
 
136
        'state': fields.selection([
 
137
            ('draft','Draft'),
 
138
            ('proforma','Pro-forma'),
 
139
            ('open','Open'),
 
140
            ('paid','Paid'),
 
141
            ('cancel','Canceled')
 
142
        ],'State', readonly=True),
 
143
        'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
 
144
        'amount': fields.float('Amount', readonly=True),
 
145
        'cost_price': fields.float('Cost Price', readonly=True),
 
146
        'margin': fields.float('Margin', readonly=True),
 
147
        'quantity': fields.float('Quantity', readonly=True),
 
148
    }
 
149
    _order = 'name desc'
 
150
    def init(self, cr):
 
151
        cr.execute("""
 
152
            create or replace view report_account_invoice_partner as (
 
153
                select
 
154
                    min(l.id) as id,
 
155
                    to_char(i.date_invoice, 'YYYY-MM-01') as name,
 
156
                    i.type,
 
157
                    i.state,
 
158
                    i.partner_id,
 
159
                    sum(l.quantity * l.price_unit * (1.0 - l.discount/100.0)) as amount,
 
160
                    sum(l.quantity * l.cost_price) as cost_price,
 
161
                    sum((l.quantity * l.price_unit * (1.0 - l.discount/100.0) - (l.quantity * l.cost_price))) as margin,
 
162
                    sum(l.quantity) as quantity
 
163
                from account_invoice i
 
164
                    left join account_invoice_line l on (i.id = l.invoice_id)
 
165
                group by i.partner_id, to_char(i.date_invoice, 'YYYY-MM-01'), i.type, i.state
 
166
            )
 
167
        """)
 
168
report_account_invoice_partner()
 
169
 
 
170
class report_account_invoice_partner_product(osv.osv):
 
171
    _name = 'report.account.invoice.partner.product'
 
172
    _auto = False
 
173
    _columns = {
 
174
        'name': fields.date('Month', readonly=True),
 
175
        'type': fields.selection([
 
176
            ('out_invoice','Customer Invoice'),
 
177
            ('in_invoice','Supplier Invoice'),
 
178
            ('out_refund','Customer Refund'),
 
179
            ('in_refund','Supplier Refund'),
 
180
        ],'Type', readonly=True),
 
181
        'state': fields.selection([
 
182
            ('draft','Draft'),
 
183
            ('proforma','Pro-forma'),
 
184
            ('open','Open'),
 
185
            ('paid','Paid'),
 
186
            ('cancel','Canceled')
 
187
        ],'State', readonly=True),
 
188
        'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
 
189
        'product_id': fields.many2one('product.product', 'Product', readonly=True),
 
190
        'amount': fields.float('Amount', readonly=True),
 
191
        'cost_price': fields.float('Cost Price', readonly=True),
 
192
        'margin': fields.float('Margin', readonly=True),
 
193
        'quantity': fields.float('Quantity', readonly=True),
 
194
    }
 
195
    _order = 'name desc'
 
196
    def init(self, cr):
 
197
        cr.execute("""
 
198
            create or replace view report_account_invoice_partner_product as (
 
199
                select
 
200
                    min(l.id) as id,
 
201
                    to_char(i.date_invoice, 'YYYY-MM-01') as name,
 
202
                    i.type,
 
203
                    i.state,
 
204
                    i.partner_id,
 
205
                    l.product_id,
 
206
                    sum(l.quantity * l.price_unit * (1.0 - l.discount/100.0)) as amount,
 
207
                    sum(l.quantity * l.cost_price) as cost_price,
 
208
                    sum((l.quantity * l.price_unit * (1.0 - l.discount/100.0) - (l.quantity * l.cost_price))) as margin,
 
209
                    sum(l.quantity) as quantity
 
210
                from account_invoice i
 
211
                    left join account_invoice_line l on (i.id = l.invoice_id)
 
212
                group by i.partner_id, l.product_id, to_char(i.date_invoice, 'YYYY-MM-01'), i.type, i.state
 
213
            )
 
214
        """)
 
215
report_account_invoice_partner_product()
 
216
 
 
217
 
 
218
class report_account_invoice(osv.osv):
 
219
    _name = 'report.account.invoice'
 
220
    _auto = False
 
221
    _columns = {
 
222
        'name': fields.date('Month', readonly=True),
 
223
        'type': fields.selection([
 
224
            ('out_invoice','Customer Invoice'),
 
225
            ('in_invoice','Supplier Invoice'),
 
226
            ('out_refund','Customer Refund'),
 
227
            ('in_refund','Supplier Refund'),
 
228
        ],'Type', readonly=True),
 
229
        'state': fields.selection([
 
230
            ('draft','Draft'),
 
231
            ('proforma','Pro-forma'),
 
232
            ('open','Open'),
 
233
            ('paid','Paid'),
 
234
            ('cancel','Canceled')
 
235
        ],'State', readonly=True),
 
236
        'amount': fields.float('Amount', readonly=True),
 
237
        'cost_price': fields.float('Cost Price', readonly=True),
 
238
        'margin': fields.float('Margin', readonly=True),
 
239
        'quantity': fields.float('Quantity', readonly=True),
 
240
    }
 
241
    _order = 'name desc'
 
242
    def init(self, cr):
 
243
        cr.execute("""
 
244
            create or replace view report_account_invoice as (
 
245
                select
 
246
                    min(l.id) as id,
 
247
                    to_char(i.date_invoice, 'YYYY-MM-01') as name,
 
248
                    i.type,
 
249
                    i.state,
 
250
                    sum(l.quantity * l.price_unit * (1.0 - l.discount/100.0)) as amount,
 
251
                    sum(l.quantity * l.cost_price) as cost_price,
 
252
                    sum((l.quantity * l.price_unit * (1.0 - l.discount/100.0) - (l.quantity * l.cost_price))) as margin,
 
253
                    sum(l.quantity) as quantity
 
254
                from account_invoice i
 
255
                    left join account_invoice_line l on (i.id = l.invoice_id)
 
256
                group by to_char(i.date_invoice, 'YYYY-MM-01'), i.type, i.state
 
257
            )
 
258
        """)
 
259
report_account_invoice()
 
260
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
261