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
|
|
|
|
from osv import fields,osv
|
|
|
|
|
|
|
|
class account_invoice_report(osv.osv):
|
|
|
|
_name = "account.invoice.report"
|
|
|
|
_description = "Invoices Statistics"
|
|
|
|
_auto = False
|
|
|
|
_rec_name = 'date'
|
|
|
|
_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),
|
2010-12-20 12:24:22 +00:00
|
|
|
'uom_name': fields.char('Reference UoM', 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),
|
|
|
|
'user_id': fields.many2one('res.users', 'Salesman', readonly=True),
|
|
|
|
'price_total': fields.float('Total Without Tax', readonly=True),
|
|
|
|
'price_total_tax': fields.float('Total With Tax', readonly=True),
|
2010-08-31 07:01:48 +00:00
|
|
|
'price_average': fields.float('Average Price', readonly=True, group_operator="avg"),
|
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')
|
2010-06-07 07:24:19 +00:00
|
|
|
], 'Invoice State', readonly=True),
|
2010-04-29 08:27:53 +00:00
|
|
|
'date_due': fields.date('Due Date', readonly=True),
|
2010-05-03 09:19:40 +00:00
|
|
|
'address_contact_id': fields.many2one('res.partner.address', 'Contact Address Name', readonly=True),
|
|
|
|
'address_invoice_id': fields.many2one('res.partner.address', 'Invoice Address Name', readonly=True),
|
2010-04-29 08:27:53 +00:00
|
|
|
'account_id': fields.many2one('account.account', 'Account',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),
|
|
|
|
'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"),
|
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
|
|
|
|
(select name from product_uom where uom_type='reference' and category_id=u.category_id)
|
|
|
|
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,
|
|
|
|
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.address_contact_id as address_contact_id,
|
|
|
|
ai.address_invoice_id as address_invoice_id,
|
|
|
|
ai.account_id as account_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
|
2010-12-20 12:24:22 +00:00
|
|
|
ail.quantity / u.factor * -1
|
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,
|
|
|
|
sum(case when ai.type in ('out_refund','in_invoice') then
|
|
|
|
ail.quantity*ail.price_unit * -1
|
|
|
|
else
|
|
|
|
ail.quantity*ail.price_unit
|
2010-09-08 09:24:35 +00:00
|
|
|
end) / cr.rate as price_total,
|
2010-08-14 03:25:58 +00:00
|
|
|
sum(case when ai.type in ('out_refund','in_invoice') then
|
|
|
|
ai.amount_total * -1
|
|
|
|
else
|
|
|
|
ai.amount_total
|
2011-01-25 14:18:33 +00:00
|
|
|
end) / (CASE WHEN
|
|
|
|
(select count(l.id) from account_invoice_line as l
|
|
|
|
left join account_invoice as a ON (a.id=l.invoice_id)
|
|
|
|
where a.id=ai.id) <> 0
|
|
|
|
THEN
|
|
|
|
(select count(l.id) from account_invoice_line as l
|
|
|
|
left join account_invoice as a ON (a.id=l.invoice_id)
|
|
|
|
where a.id=ai.id)
|
|
|
|
ELSE 1
|
|
|
|
END) / cr.rate as price_total_tax,
|
2010-08-31 07:01:48 +00:00
|
|
|
(case when ai.type in ('out_refund','in_invoice') then
|
|
|
|
sum(ail.quantity*ail.price_unit*-1)
|
|
|
|
else
|
|
|
|
sum(ail.quantity*ail.price_unit)
|
2011-01-25 14:18:33 +00:00
|
|
|
end) / (CASE WHEN
|
|
|
|
(case when ai.type in ('out_refund','in_invoice')
|
|
|
|
then sum(ail.quantity/u.factor*-1)
|
|
|
|
else sum(ail.quantity/u.factor) end) <> 0
|
|
|
|
THEN
|
|
|
|
(case when ai.type in ('out_refund','in_invoice')
|
|
|
|
then sum(ail.quantity/u.factor*-1)
|
|
|
|
else sum(ail.quantity/u.factor) end)
|
|
|
|
ELSE 1
|
|
|
|
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
|
|
|
|
ai.residual * -1
|
|
|
|
else
|
|
|
|
ai.residual
|
2011-01-25 14:18:33 +00:00
|
|
|
end)/ (CASE WHEN
|
|
|
|
(select count(l.id) from account_invoice_line as l
|
|
|
|
left join account_invoice as a ON (a.id=l.invoice_id)
|
|
|
|
where a.id=ai.id) <> 0
|
|
|
|
THEN
|
|
|
|
(select count(l.id) from account_invoice_line as l
|
|
|
|
left join account_invoice as a ON (a.id=l.invoice_id)
|
|
|
|
where a.id=ai.id)
|
|
|
|
ELSE 1
|
|
|
|
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)
|
|
|
|
left join product_template pt on (pt.id=ail.product_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)
|
|
|
|
and ((ai.date_invoice is not null and cr.name <= ai.date_invoice) or (ai.date_invoice is null and cr.name <= NOW())) 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.address_contact_id,
|
|
|
|
ai.address_invoice_id,
|
|
|
|
ai.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,
|
|
|
|
u.category_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:
|