Launchpad automatic translations update.
[odoo/odoo.git] / addons / report_intrastat / report_intrastat.py
index 7fe6283..a77c4f5 100644 (file)
+# -*- coding: utf-8 -*-
 ##############################################################################
 #
-# Copyright (c) 2004-2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
-# $Id$
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
 #
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
 from osv import osv, fields
+from tools.sql import drop_view_if_exists
+from decimal_precision import decimal_precision as dp
+
 
 class res_country(osv.osv):
-       _name = 'res.country'
-       _inherit = 'res.country'
-       _columns = {
-               'intrastat': fields.boolean('Intrastat member'),
-       }
-       _defaults = {
-               'intrastat': lambda *a: False,
-       }
+    _name = 'res.country'
+    _inherit = 'res.country'
+    _columns = {
+        'intrastat': fields.boolean('Intrastat member'),
+    }
+    _defaults = {
+        'intrastat': lambda *a: False,
+    }
+
 res_country()
 
+
 class report_intrastat_code(osv.osv):
-       _name = "report.intrastat.code"
-       _description = "Intrastat code"
-       _columns = {
-               'name': fields.char('Intrastat Code', size=16),
-               'description': fields.char('Description', size=64),
-       }
+    _name = "report.intrastat.code"
+    _description = "Intrastat code"
+    _columns = {
+        'name': fields.char('Intrastat Code', size=16),
+        'description': fields.char('Description', size=64),
+    }
+
 report_intrastat_code()
 
+
 class product_template(osv.osv):
-       _name = "product.template"
-       _inherit = "product.template"
-       _columns = {
-               'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code'),
-       }
+    _name = "product.template"
+    _inherit = "product.template"
+    _columns = {
+        'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code'),
+    }
+
 product_template()
 
 class report_intrastat(osv.osv):
-       _name = "report.intrastat"
-       _description = "Intrastat report"
-       _auto = False
-       _columns = {
-               'name': fields.date('Month', readonly=True),
-               'code': fields.char('Country code', size="2", readonly=True),
-               'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code', readonly=True),
-               'weight': fields.float('Weight', readonly=True),
-               'value': fields.float('Value', readonly=True),
-               'type': fields.selection([('import', 'Import'), ('export', 'Export')], 'Type'),
-               'currency_id': fields.many2one('res.currency', "Currency", readonly=True),
-       }
-       def init(self, cr):
-               cr.execute("""
-                       create or replace view report_intrastat as (
-                               select
-                                       to_char(m.create_date, 'YYYY-MM-01') as name,
-                                       min(m.id) as id,
-                                       pt.intrastat_id as intrastat_id,
-                                       case when l.usage in ('supplier', 'customer') then upper(pc.code) else upper(c.code) end as code,
-                                       sum(case when pol.price_unit is not null
-                                               then pol.price_unit * m.product_qty 
-                                               else
-                                                       case when sol.price_unit is not null
-                                                       then sol.price_unit * m.product_qty 
-                                                       else 0 
-                                                       end
-                                               end) as value,
-                                       sum(pt.weight_net * m.product_qty) as weight,
-                                       case when l.usage in ('supplier', 'customer') then 'import' else 'export' end as type,
-                                       case when ppl.currency_id is not null
-                                               then ppl.currency_id
-                                               else spl.currency_id
-                                               end as currency_id
-                               from
-                                       stock_move m
-                                       left join (product_template pt
-                                               left join product_product pp on (pp.product_tmpl_id = pt.id))
-                                       on (m.product_id = pt.id)
-                                       left join (res_partner_address a
-                                               left join res_country c on (c.id = a.country_id))
-                                       on (a.id = m.address_id)
-                                       left join (stock_picking sp
-                                               left join (res_partner_address pa
-                                                       left join res_country pc on (pc.id = pa.country_id))
-                                               on (pa.id = sp.address_id))
-                                       on (sp.id = m.picking_id)
-                                       left join stock_location l on (l.id = m.location_id)
-                                       left join stock_location dl on (dl.id = m.location_dest_id)
-                                       left join (purchase_order_line pol
-                                               left join (purchase_order po
-                                                       left join product_pricelist ppl on (ppl.id = po.pricelist_id))
-                                               on (po.id = pol.order_id))
-                                       on (pol.id = m.purchase_line_id)
-                                       left join (sale_order_line sol
-                                               left join (sale_order so
-                                                       left join product_pricelist spl on (spl.id = so.pricelist_id))
-                                               on (so.id = sol.order_id))
-                                       on (sol.id = m.sale_line_id)
-                               where
-                                       m.state != 'draft'
-                                       and ((l.usage in ('supplier', 'customer') and dl.usage not in ('supplier', 'customer'))
-                                               or (dl.usage in ('supplier', 'customer') and l.usage not in ('supplier', 'customer')))
-                                       and (c.intrastat is not null or pc.intrastat is not null)
-                               group by to_char(m.create_date, 'YYYY-MM-01'), pt.intrastat_id, c.code, pc.code, l.usage, dl.usage, ppl.currency_id, spl.currency_id
-                       )""")
-report_intrastat()
+    _name = "report.intrastat"
+    _description = "Intrastat report"
+    _auto = False
+    _columns = {
+        'name': fields.char('Year',size=64,required=False, readonly=True),
+        'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
+                                  ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
+        'supply_units':fields.float('Supply Units', readonly=True),
+        'ref':fields.char('Source document',size=64, readonly=True),
+        'code': fields.char('Country code', size=2, readonly=True),
+        'intrastat_id': fields.many2one('report.intrastat.code', 'Intrastat code', readonly=True),
+        'weight': fields.float('Weight', readonly=True),
+        'value': fields.float('Value', readonly=True, digits_compute=dp.get_precision('Account')),
+        'type': fields.selection([('import', 'Import'), ('export', 'Export')], 'Type'),
+        'currency_id': fields.many2one('res.currency', "Currency", readonly=True),
+    }
+    def init(self, cr):
+        drop_view_if_exists(cr, 'report_intrastat')
+        cr.execute("""
+            create or replace view report_intrastat as (
+                select
+                    to_char(inv.create_date, 'YYYY') as name,
+                    to_char(inv.create_date, 'MM') as month,
+                    min(inv_line.id) as id,
+                    intrastat.id as intrastat_id,
+                    upper(inv_country.code) as code,
+                    sum(case when inv_line.price_unit is not null
+                            then inv_line.price_unit * inv_line.quantity
+                            else 0
+                        end) as value,
+                    sum(
+                        case when uom.category_id != puom.category_id then (pt.weight_net * inv_line.quantity)
+                        else (pt.weight_net * inv_line.quantity * uom.factor) end
+                    ) as weight,
+                    sum(
+                        case when uom.category_id != puom.category_id then inv_line.quantity
+                        else (inv_line.quantity * uom.factor) end
+                    ) as supply_units,
 
+                    inv.currency_id as currency_id,
+                    inv.number as ref,
+                    case when inv.type in ('out_invoice','in_refund')
+                        then 'export'
+                        else 'import'
+                        end as type
+                from
+                    account_invoice inv
+                    left join account_invoice_line inv_line on inv_line.invoice_id=inv.id
+                    left join (product_template pt
+                        left join product_product pp on (pp.product_tmpl_id = pt.id))
+                    on (inv_line.product_id = pp.id)
+                    left join product_uom uom on uom.id=inv_line.uos_id
+                    left join product_uom puom on puom.id = pt.uom_id
+                    left join report_intrastat_code intrastat on pt.intrastat_id = intrastat.id
+                    left join (res_partner_address inv_address
+                        left join res_country inv_country on (inv_country.id = inv_address.country_id))
+                    on (inv_address.id = inv.address_invoice_id)
+
+                where
+                    inv.state in ('open','paid')
+                    and inv_line.product_id is not null
+                    and inv_country.intrastat=true
+                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
+            )""")
+
+report_intrastat()