2010-03-10 09:41:29 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# 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.
#
# 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.
#
# 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/>.
#
##############################################################################
import tools
2012-10-15 11:25:30 +00:00
import decimal_precision as dp
2010-03-10 09:41:29 +00:00
from osv import fields , osv
class account_invoice_report ( osv . osv ) :
_name = " account.invoice.report "
_description = " Invoices Statistics "
_auto = False
_rec_name = ' date '
2012-10-15 11:25:30 +00:00
def _compute_amounts_in_user_currency ( self , cr , uid , ids , field_names , args , context = None ) :
""" Compute the amounts in the currency of the user
"""
if context is None :
context = { }
currency_obj = self . pool . get ( ' res.currency ' )
currency_rate_obj = self . pool . get ( ' res.currency.rate ' )
user_currency_id = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . company_id . currency_id . id
currency_rate_id = currency_rate_obj . search ( cr , uid , [ ( ' rate ' , ' = ' , 1 ) ] , limit = 1 , context = context ) [ 0 ]
base_currency_id = currency_rate_obj . browse ( cr , uid , currency_rate_id , context = context ) . currency_id . id
res = { }
ctx = context . copy ( )
for item in self . browse ( cr , uid , ids , context = context ) :
ctx [ ' date ' ] = item . date
price_total = currency_obj . compute ( cr , uid , base_currency_id , user_currency_id , item . price_total , context = ctx )
price_average = currency_obj . compute ( cr , uid , base_currency_id , user_currency_id , item . price_average , context = ctx )
residual = currency_obj . compute ( cr , uid , base_currency_id , user_currency_id , item . residual , context = ctx )
res [ item . id ] = {
' user_currency_price_total ' : price_total ,
' user_currency_price_average ' : price_average ,
' user_currency_residual ' : residual ,
}
return res
2010-03-10 09:41:29 +00:00
_columns = {
' date ' : fields . date ( ' Date ' , readonly = True ) ,
' year ' : fields . char ( ' Year ' , size = 4 , readonly = True ) ,
2010-04-20 11:04:24 +00:00
' day ' : fields . char ( ' Day ' , size = 128 , readonly = True ) ,
2010-08-14 03:25:58 +00:00
' month ' : fields . selection ( [ ( ' 01 ' , ' January ' ) , ( ' 02 ' , ' February ' ) , ( ' 03 ' , ' March ' ) , ( ' 04 ' , ' April ' ) ,
2010-03-10 09:41:29 +00:00
( ' 05 ' , ' May ' ) , ( ' 06 ' , ' June ' ) , ( ' 07 ' , ' July ' ) , ( ' 08 ' , ' August ' ) , ( ' 09 ' , ' September ' ) ,
2010-06-16 11:51:39 +00:00
( ' 10 ' , ' October ' ) , ( ' 11 ' , ' November ' ) , ( ' 12 ' , ' December ' ) ] , ' Month ' , readonly = True ) ,
2010-08-14 03:25:58 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' , readonly = True ) ,
2010-03-10 09:41:29 +00:00
' product_qty ' : fields . float ( ' Qty ' , readonly = True ) ,
2012-04-25 07:08:25 +00:00
' uom_name ' : fields . char ( ' Reference Unit of Measure ' , size = 128 , readonly = True ) ,
2010-06-16 11:51:39 +00:00
' payment_term ' : fields . many2one ( ' account.payment.term ' , ' Payment Term ' , readonly = True ) ,
' period_id ' : fields . many2one ( ' account.period ' , ' Force Period ' , domain = [ ( ' state ' , ' <> ' , ' done ' ) ] , readonly = True ) ,
2010-10-09 16:01:43 +00:00
' fiscal_position ' : fields . many2one ( ' account.fiscal.position ' , ' Fiscal Position ' , readonly = True ) ,
2010-04-07 09:07:16 +00:00
' currency_id ' : fields . many2one ( ' res.currency ' , ' Currency ' , readonly = True ) ,
2010-05-31 10:18:33 +00:00
' categ_id ' : fields . many2one ( ' product.category ' , ' Category of Product ' , readonly = True ) ,
2010-06-16 11:51:39 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Journal ' , readonly = True ) ,
2010-08-14 03:25:58 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , readonly = True ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , readonly = True ) ,
2012-05-04 12:13:26 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Salesperson ' , readonly = True ) ,
2010-08-14 03:25:58 +00:00
' price_total ' : fields . float ( ' Total Without Tax ' , readonly = True ) ,
2012-10-15 11:25:30 +00:00
' user_currency_price_total ' : fields . function ( _compute_amounts_in_user_currency , string = " Total Without Tax " , type = ' float ' , digits_compute = dp . get_precision ( ' Account ' ) , multi = " _compute_amounts " ) ,
2010-08-31 07:01:48 +00:00
' price_average ' : fields . float ( ' Average Price ' , readonly = True , group_operator = " avg " ) ,
2012-10-15 11:25:30 +00:00
' user_currency_price_average ' : fields . function ( _compute_amounts_in_user_currency , string = " Average Price " , type = ' float ' , digits_compute = dp . get_precision ( ' Account ' ) , multi = " _compute_amounts " ) ,
2010-09-08 09:24:35 +00:00
' currency_rate ' : fields . float ( ' Currency Rate ' , readonly = True ) ,
2010-03-10 09:41:29 +00:00
' nbr ' : fields . integer ( ' # of Lines ' , readonly = True ) ,
2010-03-10 11:05:01 +00:00
' type ' : fields . selection ( [
( ' out_invoice ' , ' Customer Invoice ' ) ,
( ' in_invoice ' , ' Supplier Invoice ' ) ,
( ' out_refund ' , ' Customer Refund ' ) ,
( ' in_refund ' , ' Supplier Refund ' ) ,
] , ' Type ' , readonly = True ) ,
2010-03-10 09:41:29 +00:00
' state ' : fields . selection ( [
( ' draft ' , ' Draft ' ) ,
( ' proforma ' , ' Pro-forma ' ) ,
( ' proforma2 ' , ' Pro-forma ' ) ,
( ' open ' , ' Open ' ) ,
( ' paid ' , ' Done ' ) ,
( ' cancel ' , ' Cancelled ' )
2012-10-12 11:42:58 +00:00
] , ' Invoice Status ' , readonly = True ) ,
2010-04-29 08:27:53 +00:00
' date_due ' : fields . date ( ' Due Date ' , readonly = True ) ,
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , readonly = True ) ,
2011-04-13 07:32:11 +00:00
' account_line_id ' : fields . many2one ( ' account.account ' , ' Account Line ' , readonly = True ) ,
2010-07-19 13:47:09 +00:00
' partner_bank_id ' : fields . many2one ( ' res.partner.bank ' , ' Bank Account ' , readonly = True ) ,
2010-08-14 03:25:58 +00:00
' residual ' : fields . float ( ' Total Residual ' , readonly = True ) ,
2012-10-15 11:25:30 +00:00
' user_currency_residual ' : fields . function ( _compute_amounts_in_user_currency , string = " Total Residual " , type = ' float ' , digits_compute = dp . get_precision ( ' Account ' ) , multi = " _compute_amounts " ) ,
2010-08-14 03:25:58 +00:00
' delay_to_pay ' : fields . float ( ' Avg. Delay To Pay ' , readonly = True , group_operator = " avg " ) ,
2010-09-24 11:50:17 +00:00
' due_delay ' : fields . float ( ' Avg. Due Delay ' , readonly = True , group_operator = " avg " ) ,
2012-11-21 05:58:17 +00:00
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' ) ,
2010-03-10 09:41:29 +00:00
}
_order = ' date desc '
def init ( self , cr ) :
2010-03-12 11:44:19 +00:00
tools . drop_view_if_exists ( cr , ' account_invoice_report ' )
2010-03-10 09:41:29 +00:00
cr . execute ( """
create or replace view account_invoice_report as (
2010-07-08 09:48:42 +00:00
select min ( ail . id ) as id ,
ai . date_invoice as date ,
to_char ( ai . date_invoice , ' YYYY ' ) as year ,
to_char ( ai . date_invoice , ' MM ' ) as month ,
to_char ( ai . date_invoice , ' YYYY-MM-DD ' ) as day ,
ail . product_id ,
ai . partner_id as partner_id ,
ai . payment_term as payment_term ,
ai . period_id as period_id ,
2010-12-20 12:24:22 +00:00
( case when u . uom_type not in ( ' reference ' ) then
2011-04-07 12:29:55 +00:00
( select name from product_uom where uom_type = ' reference ' and active and category_id = u . category_id LIMIT 1 )
2010-12-20 12:24:22 +00:00
else
u . name
end ) as uom_name ,
2010-07-08 09:48:42 +00:00
ai . currency_id as currency_id ,
ai . journal_id as journal_id ,
ai . fiscal_position as fiscal_position ,
ai . user_id as user_id ,
2012-11-21 05:58:17 +00:00
ai . section_id as section_id ,
2010-07-08 09:48:42 +00:00
ai . company_id as company_id ,
count ( ail . * ) as nbr ,
ai . type as type ,
ai . state ,
pt . categ_id ,
ai . date_due as date_due ,
ai . account_id as account_id ,
2011-04-13 07:32:11 +00:00
ail . account_id as account_line_id ,
2010-07-19 13:47:09 +00:00
ai . partner_bank_id as partner_bank_id ,
2010-07-08 09:48:42 +00:00
sum ( case when ai . type in ( ' out_refund ' , ' in_invoice ' ) then
2011-04-13 07:32:11 +00:00
- ail . quantity / u . factor
2010-07-08 09:48:42 +00:00
else
2010-12-20 12:24:22 +00:00
ail . quantity / u . factor
2010-07-08 09:48:42 +00:00
end ) as product_qty ,
2011-04-13 07:32:11 +00:00
2010-07-08 09:48:42 +00:00
sum ( case when ai . type in ( ' out_refund ' , ' in_invoice ' ) then
2011-04-13 07:32:11 +00:00
- ail . price_subtotal
2010-07-08 09:48:42 +00:00
else
2011-04-13 07:32:11 +00:00
ail . price_subtotal
2010-09-08 09:24:35 +00:00
end ) / cr . rate as price_total ,
2011-04-13 07:32:11 +00:00
2010-08-31 07:01:48 +00:00
( case when ai . type in ( ' out_refund ' , ' in_invoice ' ) then
2011-04-13 07:32:11 +00:00
sum ( - ail . price_subtotal )
2010-08-31 07:01:48 +00:00
else
2011-04-13 07:32:11 +00:00
sum ( ail . price_subtotal )
end ) / ( CASE WHEN sum ( ail . quantity / u . factor ) < > 0
THEN
( case when ai . type in ( ' out_refund ' , ' in_invoice ' )
then sum ( - ail . quantity / u . factor )
else sum ( ail . quantity / u . factor ) end )
ELSE 1
2011-01-25 14:18:33 +00:00
END )
/ cr . rate as price_average ,
2010-09-08 09:24:35 +00:00
cr . rate as currency_rate ,
2010-07-08 12:07:06 +00:00
sum ( ( select extract ( epoch from avg ( date_trunc ( ' day ' , aml . date_created ) - date_trunc ( ' day ' , l . create_date ) ) ) / ( 24 * 60 * 60 ) : : decimal ( 16 , 2 )
2010-07-08 09:48:42 +00:00
from account_move_line as aml
left join account_invoice as a ON ( a . move_id = aml . move_id )
left join account_invoice_line as l ON ( a . id = l . invoice_id )
where a . id = ai . id ) ) as delay_to_pay ,
2010-09-24 11:50:17 +00:00
sum ( ( select extract ( epoch from avg ( date_trunc ( ' day ' , a . date_due ) - date_trunc ( ' day ' , a . date_invoice ) ) ) / ( 24 * 60 * 60 ) : : decimal ( 16 , 2 )
from account_move_line as aml
left join account_invoice as a ON ( a . move_id = aml . move_id )
left join account_invoice_line as l ON ( a . id = l . invoice_id )
where a . id = ai . id ) ) as due_delay ,
2010-07-08 10:48:52 +00:00
( case when ai . type in ( ' out_refund ' , ' in_invoice ' ) then
2011-04-13 07:32:11 +00:00
- ai . residual
2010-07-08 10:48:52 +00:00
else
ai . residual
2011-04-13 07:32:11 +00:00
end ) / ( CASE WHEN
2011-01-25 14:18:33 +00:00
( select count ( l . id ) from account_invoice_line as l
left join account_invoice as a ON ( a . id = l . invoice_id )
2011-04-13 07:32:11 +00:00
where a . id = ai . id ) < > 0
2011-01-25 14:18:33 +00:00
THEN
( select count ( l . id ) from account_invoice_line as l
left join account_invoice as a ON ( a . id = l . invoice_id )
2011-04-13 07:32:11 +00:00
where a . id = ai . id )
ELSE 1
2011-01-25 14:18:33 +00:00
END ) / cr . rate as residual
2010-07-08 09:48:42 +00:00
from account_invoice_line as ail
left join account_invoice as ai ON ( ai . id = ail . invoice_id )
2011-04-13 07:32:11 +00:00
left join product_product pr on ( pr . id = ail . product_id )
left join product_template pt on ( pt . id = pr . product_tmpl_id )
2010-09-08 09:24:35 +00:00
left join product_uom u on ( u . id = ail . uos_id ) ,
2010-09-24 11:50:17 +00:00
res_currency_rate cr
2010-09-08 09:24:35 +00:00
where cr . id in ( select id from res_currency_rate cr2 where ( cr2 . currency_id = ai . currency_id )
2012-10-15 11:25:30 +00:00
and ( ( ai . date_invoice is not null and cr . name < = ai . date_invoice ) or ( ai . date_invoice is null and cr . name < = NOW ( ) ) ) order by name desc limit 1 )
2010-07-08 09:48:42 +00:00
group by ail . product_id ,
ai . date_invoice ,
2010-07-08 10:48:52 +00:00
ai . id ,
2010-09-08 09:24:35 +00:00
cr . rate ,
2010-07-08 09:48:42 +00:00
to_char ( ai . date_invoice , ' YYYY ' ) ,
to_char ( ai . date_invoice , ' MM ' ) ,
to_char ( ai . date_invoice , ' YYYY-MM-DD ' ) ,
ai . partner_id ,
ai . payment_term ,
ai . period_id ,
u . name ,
ai . currency_id ,
ai . journal_id ,
ai . fiscal_position ,
ai . user_id ,
ai . company_id ,
ai . type ,
ai . state ,
pt . categ_id ,
ai . date_due ,
ai . account_id ,
2011-04-13 07:32:11 +00:00
ail . account_id ,
2010-07-19 13:47:09 +00:00
ai . partner_bank_id ,
2010-08-14 03:25:58 +00:00
ai . residual ,
2010-12-20 12:24:22 +00:00
ai . amount_total ,
u . uom_type ,
2012-11-21 05:58:17 +00:00
u . category_id ,
ai . section_id
2010-03-10 09:41:29 +00:00
)
""" )
2010-08-14 03:25:58 +00:00
2010-03-10 09:41:29 +00:00
account_invoice_report ( )
2010-08-14 03:25:58 +00:00
2010-09-08 09:24:35 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: