Set version 4.1.1 and improve report_intrastat
[odoo/odoo.git] / addons / report_intrastat / report_intrastat.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id: __init__.py 1005 2005-07-25 08:41:42Z nicoe $
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 from osv import osv, fields
31
32 class res_country(osv.osv):
33         _name = 'res.country'
34         _inherit = 'res.country'
35         _columns = {
36                 'intrastat': fields.boolean('Intrastat member'),
37         }
38         _defaults = {
39                 'intrastat': lambda *a: False,
40         }
41 res_country()
42
43 class report_intrastat_code(osv.osv):
44         _name = "report.intrastat.code"
45         _description = "Intrastat code"
46         _columns = {
47                 'name': fields.char('Intrastat Code', size=16),
48                 'description': fields.char('Description', size=64),
49         }
50 report_intrastat_code()
51
52 class product_template(osv.osv):
53         _name = "product.template"
54         _inherit = "product.template"
55         _columns = {
56                 'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code'),
57         }
58 product_template()
59
60 class report_intrastat(osv.osv):
61         _name = "report.intrastat"
62         _description = "Intrastat report"
63         _auto = False
64         _columns = {
65                 'name': fields.date('Month', readonly=True),
66                 'code': fields.char('Country code', size="2", readonly=True),
67                 'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code', readonly=True),
68                 'weight': fields.float('Weight', readonly=True),
69                 'value': fields.float('Value', readonly=True),
70                 'type': fields.selection([('import', 'Import'), ('export', 'Export')], 'Type')
71         }
72         def init(self, cr):
73                 cr.execute("""
74                         create or replace view report_intrastat as (
75                                 select
76                                         substring(m.create_date for 7)||'-01' as name,
77                                         min(m.id) as id,
78                                         pt.intrastat_id as intrastat_id,
79                                         case when l.usage in ('supplier', 'customer') then pc.code else c.code end as code,
80                                         sum(case when pol.price_unit is not null
81                                                 then pol.price_unit * m.product_qty 
82                                                 else
83                                                         case when sol.price_unit is not null
84                                                         then sol.price_unit * m.product_qty 
85                                                         else pt.standard_price * m.product_qty 
86                                                         end
87                                                 end) as value,
88                                         sum(pt.weight_net * m.product_qty) as weight,
89                                         case when l.usage in ('supplier', 'customer') then 'import' else 'export' end as type
90                                 from
91                                         stock_move m
92                                         left join (product_template pt
93                                                 left join product_product pp on (pp.product_tmpl_id = pt.id))
94                                         on (m.product_id = pt.id)
95                                         left join (res_partner_address a
96                                                 left join res_country c on (c.id = a.country_id))
97                                         on (a.id = m.address_id)
98                                         left join (stock_picking sp
99                                                 left join (res_partner_address pa
100                                                         left join res_country pc on (pc.id = pa.country_id))
101                                                 on (pa.id = sp.address_id))
102                                         on (sp.id = m.picking_id)
103                                         left join stock_location l on (l.id = m.location_id)
104                                         left join stock_location dl on (dl.id = m.location_dest_id)
105                                         left join purchase_order_line pol on (pol.id = m.purchase_line_id)
106                                         left join sale_order_line sol on (sol.id = m.sale_line_id)
107                                 where
108                                         m.state != 'draft'
109                                         and ((l.usage in ('supplier', 'customer') and dl.usage not in ('supplier', 'customer'))
110                                                 or (dl.usage in ('supplier', 'customer') and l.usage not in ('supplier', 'customer')))
111                                         and (c.intrastat is not null or pc.intrastat is not null)
112                                 group by substring(m.create_date for 7), pt.intrastat_id, c.code, pc.code, l.usage, dl.usage
113                         )""")
114 report_intrastat()
115