Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partner paid invoice #3

Open
wants to merge 2 commits into
base: 12.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions partner_paid_invoice/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
39 changes: 39 additions & 0 deletions partner_paid_invoice/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Odoo, Open Source Enterprise Management Solution, third party addon
# Copyright (C) 2017 Vertel AB (<http://vertel.se>).
#
# 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/>.
#
##############################################################################
{
'name' : 'Partner Paid Invoice',
'version' : '1.0',
'summary': 'Partner Paid Invoice',
'description': "Shows the total amount of paid invoice on a partner",
'category': 'Accounting',
'author': 'Vertel AB',
'website': 'http://www.vertel.se',
'images': [],
'depends': ['base', 'contacts', 'account'],
'data': [
'views/res_partner_view.xml'
],
'demo': [],
'qweb': [],
'installable': True,
'application': False,
'auto_install': False,
}
1 change: 1 addition & 0 deletions partner_paid_invoice/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import res_partner
59 changes: 59 additions & 0 deletions partner_paid_invoice/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from odoo import models, fields, api, _
from ast import literal_eval


class Partner(models.Model):
_inherit = 'res.partner'

total_paid_invoiced = fields.Monetary(compute='_paid_invoice_total', string="Total Paid Invoiced",
groups='account.group_account_invoice')

@api.multi
def _paid_invoice_total(self):
account_invoice_report = self.env['account.invoice.report']
if not self.ids:
return True

user_currency_id = self.env.user.company_id.currency_id.id
all_partners_and_children = {}
all_partner_ids = []
for partner in self:
# price_total is in the company currency
all_partners_and_children[partner] = self.with_context(active_test=False).search(
[('id', 'child_of', partner.id)]).ids
all_partner_ids += all_partners_and_children[partner]

# searching account.invoice.report via the ORM is comparatively expensive
# (generates queries "id in []" forcing to build the full table).
# In simple cases where all invoices are in the same currency than the user's company
# access directly these elements

# generate where clause to include multicompany rules
where_query = account_invoice_report._where_calc([
('partner_id', 'in', all_partner_ids), ('state', '=', 'paid'),
('type', 'in', ('out_invoice', 'out_refund'))
])
account_invoice_report._apply_ir_rules(where_query, 'read')
from_clause, where_clause, where_clause_params = where_query.get_sql()

# price_total is in the company currency
query = """
SELECT SUM(price_total) as total, partner_id
FROM account_invoice_report account_invoice_report
WHERE %s
GROUP BY partner_id
""" % where_clause
self.env.cr.execute(query, where_clause_params)
price_totals = self.env.cr.dictfetchall()
for partner, child_ids in all_partners_and_children.items():
partner.total_paid_invoiced = sum(price['total'] for price in price_totals if price['partner_id'] in child_ids)

@api.multi
def action_view_partner_invoices_paid(self):
self.ensure_one()
action = self.env.ref('account.action_invoice_refund_out_tree').read()[0]
action['domain'] = literal_eval(action['domain'])
action['domain'].append(('partner_id', 'child_of', self.id))
action['domain'].append(('state', '=', 'paid'))
return action

22 changes: 22 additions & 0 deletions partner_paid_invoice/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<odoo>
<data>
<record id="res_partner_paid_invoice_form" model="ir.ui.view">
<field name="name">res.partner.paid.invoice</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="account.partner_view_buttons"/>
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']/button[@name='action_view_partner_invoices']" position="after">
<button type="object" class="oe_stat_button" icon="fa-pencil-square-o" name="action_view_partner_invoices_paid"
attrs="{'invisible': [('customer', '=', False)]}" context="{'default_partner_id': active_id}">
<div class="o_form_field o_stat_info">
<span class="o_stat_value">
<field name="total_paid_invoiced" widget='monetary' options="{'currency_field': 'currency_id'}"/>
</span>
<span class="o_stat_text">Paid Invoice</span>
</div>
</button>
</xpath>
</field>
</record>
</data>
</odoo>