Skip to content

Commit

Permalink
[9.0][ADD] account_invoice_fixed_discount
Browse files Browse the repository at this point in the history
  • Loading branch information
LoisRForgeFlow committed Jun 12, 2018
1 parent 2869c61 commit 9ffa7d2
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 0 deletions.
77 changes: 77 additions & 0 deletions account_invoice_fixed_discount/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

==============================
Account Invoice Fixed Discount
==============================

This module extends the functionality of Invoicing to allow you to apply fixed
amount discounts at invoice line level.

The module also extends the invoice report to show fixed discount.

Installation
============

**Warning**: This module is incompatible with
``account_invoice_triple_discount`` which also belongs to `OCA/account-invoicing
<https://github.com/OCA/account-invoicing>`_.

Configuration
=============

To configure this module, you need to:

#. Go to * Sale > Configuration > Settings*.
#. In the *Discount* option select *Allow discounts on sales order lines*.

Usage
=====

To use this module, you need to:

#. Go to *Invoicing*.
#. Create a Invoice and specify a fixed discount in a line.

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/95/9.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/account-invoicing/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Lois Rilo <[email protected]>
* Jordi Ballester <[email protected]>


Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
5 changes: 5 additions & 0 deletions account_invoice_fixed_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
21 changes: 21 additions & 0 deletions account_invoice_fixed_discount/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Account Fixed Discount",
"summary": "Allows to apply fixed amount discounts in invoices.",
"version": "9.0.1.0.0",
"category": "Accounting & Finance",
"website": "https://odoo-community.org/",
"author": "Eficent, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"account",
],
"data": [
"views/account_invoice_view.xml",
"reports/report_account_invoice.xml",
],
}
5 changes: 5 additions & 0 deletions account_invoice_fixed_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import account_invoice
76 changes: 76 additions & 0 deletions account_invoice_fixed_discount/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import api, fields, models, _
import openerp.addons.decimal_precision as dp
from openerp.exceptions import ValidationError


class AccountInvoice(models.Model):
_inherit = "account.invoice"

@api.multi
def get_taxes_values(self):
self.ensure_one()
vals = {}
for line in self.invoice_line_ids.filtered('discount_fixed'):
vals[line] = {
'price_unit': line.price_unit,
'discount_fixed': line.discount_fixed,
}
price_unit = line.price_unit - line.discount_fixed
line.update({
'price_unit': price_unit,
'discount_fixed': 0.0,
})
tax_grouped = super(AccountInvoice, self).get_taxes_values()
for line in vals.keys():
line.update(vals[line])
return tax_grouped


class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"

discount_fixed = fields.Float(
string="Discount (Fixed)",
digits=dp.get_precision('Product Price'),
help="Fixed amount discount.")

@api.onchange('discount')
def _onchange_discount(self):
if self.discount:
self.discount_fixed = 0.0

@api.onchange('discount_fixed')
def _onchange_discount_fixed(self):
if self.discount_fixed:
self.discount = 0.0

@api.multi
@api.constrains('discount', 'discount_fixed')
def _check_only_one_discount(self):
for line in self:
if line.discount and line.discount_fixed:
raise ValidationError(
_("You can only set one type of discount per line."))

@api.one
@api.depends('price_unit', 'discount', 'invoice_line_tax_ids', 'quantity',
'product_id', 'invoice_id.partner_id',
'invoice_id.currency_id', 'invoice_id.company_id',
'discount_fixed')
def _compute_price(self):
prev_price_unit = self.price_unit
prev_discount_fixed = self.discount_fixed
price_unit = self.price_unit - self.discount_fixed
self.update({
'price_unit': price_unit,
'discount_fixed': 0.0,
})
super(AccountInvoiceLine, self)._compute_price()
self.update({
'price_unit': prev_price_unit,
'discount_fixed': prev_discount_fixed,
})
27 changes: 27 additions & 0 deletions account_invoice_fixed_discount/reports/report_account_invoice.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2017 Eficent Business and IT Consulting Services S.L.
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<template id="report_invoice_document"
inherit_id="account.report_invoice_document">

<xpath expr="//t[@t-set='display_discount']" position="after">
<t t-set="display_discount_fixed"
t-value="any([l.discount_fixed for l in o.invoice_line_ids])"/>
</xpath>

<xpath expr="//th[@t-if='display_discount']" position="after">
<th t-if="display_discount_fixed" class="text-right"
groups="sale.group_discount_per_so_line">Disc. Fixed Amount</th>
</xpath>

<xpath expr="//td[@t-if='display_discount']" position="after">
<td t-if="display_discount_fixed" class="text-right" groups="sale.group_discount_per_so_line">
<span t-field="l.discount_fixed"/>
</td>
</xpath>

</template>

</odoo>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions account_invoice_fixed_discount/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import test_account_fixed_discount
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp.tests import SavepointCase
from openerp.exceptions import ValidationError


class TestInvoiceFixedDiscount(SavepointCase):

@classmethod
def setUpClass(cls):
super(TestInvoiceFixedDiscount, cls).setUpClass()
cls.partner = cls.env['res.partner'].create({'name': 'Test'})
cls.tax = cls.env['account.tax'].create({
'name': 'TAX 15%',
'amount_type': 'percent',
'type_tax_use': 'sale',
'amount': 15.0,
})
cls.account_type = cls.env['account.account.type'].create({
'name': 'Test',
'type': 'receivable',
})
cls.account = cls.env['account.account'].create({
'name': 'Test account',
'code': 'TEST',
'user_type_id': cls.account_type.id,
'reconcile': True,
})
cls.invoice = cls.env['account.invoice'].create({
'name': "Test Customer Invoice",
'journal_id': cls.env['account.journal'].search(
[('type', '=', 'sale')])[0].id,
'partner_id': cls.partner.id,
'account_id': cls.account.id,
})
cls.invoice_line = cls.env['account.invoice.line']
cls.invoice_line1 = cls.invoice_line.create({
'invoice_id': cls.invoice.id,
'name': 'Line 1',
'price_unit': 200.0,
'account_id': cls.account.id,
'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
'quantity': 1,
})

def test_01_discounts(self):
""" Tests multiple discounts in line with taxes."""
# Apply a fixed discount
self.invoice_line1.discount_fixed = 10.0
self.invoice._onchange_invoice_line_ids()
self.assertEqual(self.invoice.amount_total, 218.50)
# Try to add also a % discount
with self.assertRaises(ValidationError):
self.invoice_line1.write({'discount': 50.0})
# Apply a % discount
self.invoice_line1._onchange_discount_fixed()
self.invoice_line1.discount_fixed = 0.0
self.invoice_line1.discount = 50.0
self.invoice_line1._onchange_discount()
self.invoice._onchange_invoice_line_ids()
self.assertEqual(self.invoice.amount_total, 115.00)

def test_02_discounts_multiple_lines(self):
""" Tests multiple lines with mixed taxes and dicount types."""
self.invoice_line2 = self.invoice_line.create({
'invoice_id': self.invoice.id,
'name': 'Line 1',
'price_unit': 500.0,
'account_id': self.account.id,
'quantity': 1,
})
self.assertEqual(self.invoice_line2.price_subtotal, 500.0)
# Add a fixed discount
self.invoice_line2.discount_fixed = 100.0
self.invoice._onchange_invoice_line_ids()
self.assertEqual(self.invoice_line2.price_subtotal, 400.0)
self.assertEqual(self.invoice.amount_total, 630.0)
self.invoice_line1.discount = 50.0
self.invoice._onchange_invoice_line_ids()
self.assertEqual(self.invoice.amount_total, 515.0)
41 changes: 41 additions & 0 deletions account_invoice_fixed_discount/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2017 Eficent Business and IT Consulting Services S.L.
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->

<odoo>

<record id="view_invoice_line_tree" model="ir.ui.view">
<field name="name">account.invoice.line.tree - sale_fixed_discount</field>
<field name="model">account.invoice.line</field>
<field name="inherit_id" ref="account.view_invoice_line_tree"/>
<field name="arch" type="xml">
<field name="discount" position="before">
<field name="discount_fixed" groups="sale.group_discount_per_so_line"/>
</field>
</field>
</record>

<record id="view_invoice_line_form" model="ir.ui.view">
<field name="name">account.invoice.line.form - sale_fixed_discount</field>
<field name="model">account.invoice.line</field>
<field name="inherit_id" ref="account.view_invoice_line_form"/>
<field name="arch" type="xml">
<field name="discount" position="before">
<field name="discount_fixed" groups="sale.group_discount_per_so_line"/>
</field>
</field>
</record>

<record id="invoice_form" model="ir.ui.view">
<field name="name">account.invoice.form - sale_fixed_discount</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='invoice_line_ids']/tree/field[@name='discount']"
position="before">
<field name="discount_fixed" groups="sale.group_discount_per_so_line"/>
</xpath>
</field>
</record>

</odoo>

0 comments on commit 9ffa7d2

Please sign in to comment.