[IMP] stock: set different label for name and complete_name
[odoo/odoo.git] / addons / report_intrastat / report_intrastat.py
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 from openerp.osv import fields, osv
23 from openerp.tools.sql import drop_view_if_exists
24 from openerp.addons.decimal_precision import decimal_precision as dp
25
26
27 class res_country(osv.osv):
28     _name = 'res.country'
29     _inherit = 'res.country'
30     _columns = {
31         'intrastat': fields.boolean('Intrastat member'),
32     }
33     _defaults = {
34         'intrastat': lambda *a: False,
35     }
36
37
38
39 class report_intrastat_code(osv.osv):
40     _name = "report.intrastat.code"
41     _description = "Intrastat code"
42     _columns = {
43         'name': fields.char('Intrastat Code'),
44         'description': fields.char('Description'),
45     }
46
47
48
49 class product_template(osv.osv):
50     _name = "product.template"
51     _inherit = "product.template"
52     _columns = {
53         'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code'),
54     }
55
56
57 class report_intrastat(osv.osv):
58     _name = "report.intrastat"
59     _description = "Intrastat report"
60     _auto = False
61     _columns = {
62         'name': fields.char('Year', required=False, readonly=True),
63         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
64                                   ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month', readonly=True),
65         'supply_units':fields.float('Supply Units', readonly=True),
66         'ref':fields.char('Source document', readonly=True),
67         'code': fields.char('Country code', size=2, readonly=True),
68         'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code', readonly=True),
69         'weight': fields.float('Weight', readonly=True),
70         'value': fields.float('Value', readonly=True, digits_compute=dp.get_precision('Account')),
71         'type': fields.selection([('import', 'Import'), ('export', 'Export')], 'Type'),
72         'currency_id': fields.many2one('res.currency', "Currency", readonly=True),
73     }
74     def init(self, cr):
75         drop_view_if_exists(cr, 'report_intrastat')
76         cr.execute("""
77             create or replace view report_intrastat as (
78                 select
79                     to_char(inv.create_date, 'YYYY') as name,
80                     to_char(inv.create_date, 'MM') as month,
81                     min(inv_line.id) as id,
82                     intrastat.id as intrastat_id,
83                     upper(inv_country.code) as code,
84                     sum(case when inv_line.price_unit is not null
85                             then inv_line.price_unit * inv_line.quantity
86                             else 0
87                         end) as value,
88                     sum(
89                         case when uom.category_id != puom.category_id then (pt.weight_net * inv_line.quantity)
90                         else (pt.weight_net * inv_line.quantity * uom.factor) end
91                     ) as weight,
92                     sum(
93                         case when uom.category_id != puom.category_id then inv_line.quantity
94                         else (inv_line.quantity * uom.factor) end
95                     ) as supply_units,
96
97                     inv.currency_id as currency_id,
98                     inv.number as ref,
99                     case when inv.type in ('out_invoice','in_refund')
100                         then 'export'
101                         else 'import'
102                         end as type
103                 from
104                     account_invoice inv
105                     left join account_invoice_line inv_line on inv_line.invoice_id=inv.id
106                     left join (product_template pt
107                         left join product_product pp on (pp.product_tmpl_id = pt.id))
108                     on (inv_line.product_id = pp.id)
109                     left join product_uom uom on uom.id=inv_line.uos_id
110                     left join product_uom puom on puom.id = pt.uom_id
111                     left join report_intrastat_code intrastat on pt.intrastat_id = intrastat.id
112                     left join (res_partner inv_address
113                         left join res_country inv_country on (inv_country.id = inv_address.country_id))
114                     on (inv_address.id = inv.partner_id)
115                 where
116                     inv.state in ('open','paid')
117                     and inv_line.product_id is not null
118                     and inv_country.intrastat=true
119                 group by to_char(inv.create_date, 'YYYY'), to_char(inv.create_date, 'MM'),intrastat.id,inv.type,pt.intrastat_id, inv_country.code,inv.number,  inv.currency_id
120             )""")
121
122
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: