Skip to content

Commit

Permalink
[MIG] account_invoice_pricelist: black, isort, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelregidor authored and hildickethan committed Sep 24, 2021
1 parent cb82c9e commit 9e45fe0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 134 deletions.
1 change: 0 additions & 1 deletion account_invoice_pricelist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

from . import models
27 changes: 10 additions & 17 deletions account_invoice_pricelist/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Account - Pricelist on Invoices',
'version': '12.0.1.0.3',
'summary': 'Add partner pricelist on invoices',
'category': 'Accounting & Finance',
'author': 'GRAP,'
'Therp BV,'
'Tecnativa,'
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/account-invoicing',
'license': 'AGPL-3',
'depends': [
'account',
],
'data': [
'views/account_invoice_view.xml',
],
'installable': True,
"name": "Account - Pricelist on Invoices",
"version": "12.0.1.0.3",
"summary": "Add partner pricelist on invoices",
"category": "Accounting & Finance",
"author": "GRAP," "Therp BV," "Tecnativa," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
"license": "AGPL-3",
"depends": ["account",],
"data": ["views/account_invoice_view.xml",],
"installable": True,
}
59 changes: 34 additions & 25 deletions account_invoice_pricelist/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api
from odoo import api, fields, models


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

pricelist_id = fields.Many2one(
comodel_name='product.pricelist',
string='Pricelist',
comodel_name="product.pricelist",
string="Pricelist",
readonly=True,
states={'draft': [('readonly', False)]},
states={"draft": [("readonly", False)]},
)

@api.onchange('partner_id', 'company_id')
@api.onchange("partner_id", "company_id")
def _onchange_partner_id_account_invoice_pricelist(self):
result = super(AccountInvoice, self)._onchange_partner_id()
if self.partner_id and self.type in ('out_invoice', 'out_refund')\
and self.partner_id.property_product_pricelist:
if (
self.partner_id
and self.type in ("out_invoice", "out_refund")
and self.partner_id.property_product_pricelist
):
self.pricelist_id = self.partner_id.property_product_pricelist
return result

@api.multi
def button_update_prices_from_pricelist(self):
for inv in self.filtered(lambda r: r.state == 'draft'):
inv.invoice_line_ids.filtered('product_id').update_from_pricelist()
self.filtered(lambda r: r.state == 'draft').compute_taxes()
for inv in self.filtered(lambda r: r.state == "draft"):
inv.invoice_line_ids.filtered("product_id").update_from_pricelist()
self.filtered(lambda r: r.state == "draft").compute_taxes()

@api.model
def _prepare_refund(self, invoice, date_invoice=None, date=None,
description=None, journal_id=None):
def _prepare_refund(
self, invoice, date_invoice=None, date=None, description=None, journal_id=None
):
"""Pricelist should also be set on refund."""
values = super(AccountInvoice, self)._prepare_refund(
invoice, date_invoice=date_invoice, date=date,
description=description, journal_id=journal_id)
invoice,
date_invoice=date_invoice,
date=date,
description=description,
journal_id=journal_id,
)
if invoice.pricelist_id:
values.update({
'pricelist_id': invoice.pricelist_id.id,
})
values.update(
{"pricelist_id": invoice.pricelist_id.id,}
)
return values


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

@api.onchange('product_id', 'quantity', 'uom_id')
@api.onchange("product_id", "quantity", "uom_id")
def _onchange_product_id_account_invoice_pricelist(self):
if not self.invoice_id.pricelist_id or not self.invoice_id.partner_id:
return
Expand All @@ -56,15 +64,16 @@ def _onchange_product_id_account_invoice_pricelist(self):
pricelist=self.invoice_id.pricelist_id.id,
uom=self.uom_id.id,
fiscal_position=(
self.invoice_id.partner_id.property_account_position_id.id)
self.invoice_id.partner_id.property_account_position_id.id
),
)
tax_obj = self.env['account.tax']
tax_obj = self.env["account.tax"]
self.price_unit = tax_obj._fix_tax_included_price_company(
product.price, product.taxes_id, self.invoice_line_tax_ids,
self.company_id)
product.price, product.taxes_id, self.invoice_line_tax_ids, self.company_id
)

@api.multi
def update_from_pricelist(self):
"""overwrite current prices from pricelist"""
for line in self.filtered(lambda r: r.invoice_id.state == 'draft'):
for line in self.filtered(lambda r: r.invoice_id.state == "draft"):
line._onchange_product_id_account_invoice_pricelist()
2 changes: 1 addition & 1 deletion account_invoice_pricelist/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

For further information, please visit:

* https://www.odoo.com/forum/help-1
* https://www.odoo.com/forum/help-1
142 changes: 80 additions & 62 deletions account_invoice_pricelist/tests/test_account_invoice_pricelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,91 @@ class TestAccountInvoicePricelist(SavepointCase):
def setUpClass(self):
super(TestAccountInvoicePricelist, self).setUpClass()
self.AccountInvoice = self.env["account.invoice"]
self.ProductPricelist = self.env['product.pricelist']
self.FiscalPosition = self.env['account.fiscal.position']
self.fiscal_position = self.FiscalPosition.create({
"name": "Test Fiscal Position",
"active": True,
})
self.journal_sale = self.env["account.journal"].create({
"name": "Test sale journal",
"type": "sale",
"code": "TEST_SJ",
})
self.at_receivable = self.env["account.account.type"].create({
"name": "Test receivable account",
"type": "receivable",
})
self.a_receivable = self.env["account.account"].create({
"name": "Test receivable account",
"code": "TEST_RA",
"user_type_id": self.at_receivable.id,
"reconcile": True,
})
self.partner = self.env['res.partner'].create({
'name': 'Test Partner',
'property_product_pricelist': 1,
'property_account_receivable_id': self.a_receivable.id,
'property_account_position_id': self.fiscal_position.id,
})
self.product = self.env['product.template'].create({
'name': 'Product Test',
'list_price': 100.00,
})
self.sale_pricelist_id = self.ProductPricelist.create({
'name': 'Test Sale pricelist',
'item_ids': [(0, 0, {
'applied_on': '1_product',
'compute_price': 'fixed',
'fixed_price': 300.00,
'product_tmpl_id': self.product.id,
})],
})
self.invoice = self.AccountInvoice.create({
'partner_id': self.partner.id,
'account_id': self.a_receivable.id,
'type': 'out_invoice',
'invoice_line_ids': [
(0, 0, {
'account_id': self.a_receivable.id,
'product_id': self.product.product_variant_ids[:1].id,
'name': 'Test line',
'quantity': 1.0,
'price_unit': 100.00,
}),
(0, 0, {
'account_id': self.a_receivable.id,
'product_id': self.product.product_variant_ids[:2].id,
'name': 'Test line',
'quantity': 1.0,
'price_unit': 100.00,
})
],
})
self.ProductPricelist = self.env["product.pricelist"]
self.FiscalPosition = self.env["account.fiscal.position"]
self.fiscal_position = self.FiscalPosition.create(
{"name": "Test Fiscal Position", "active": True,}
)
self.journal_sale = self.env["account.journal"].create(
{"name": "Test sale journal", "type": "sale", "code": "TEST_SJ",}
)
self.at_receivable = self.env["account.account.type"].create(
{"name": "Test receivable account", "type": "receivable",}
)
self.a_receivable = self.env["account.account"].create(
{
"name": "Test receivable account",
"code": "TEST_RA",
"user_type_id": self.at_receivable.id,
"reconcile": True,
}
)
self.partner = self.env["res.partner"].create(
{
"name": "Test Partner",
"property_product_pricelist": 1,
"property_account_receivable_id": self.a_receivable.id,
"property_account_position_id": self.fiscal_position.id,
}
)
self.product = self.env["product.template"].create(
{"name": "Product Test", "list_price": 100.00,}
)
self.sale_pricelist_id = self.ProductPricelist.create(
{
"name": "Test Sale pricelist",
"item_ids": [
(
0,
0,
{
"applied_on": "1_product",
"compute_price": "fixed",
"fixed_price": 300.00,
"product_tmpl_id": self.product.id,
},
)
],
}
)
self.invoice = self.AccountInvoice.create(
{
"partner_id": self.partner.id,
"account_id": self.a_receivable.id,
"type": "out_invoice",
"invoice_line_ids": [
(
0,
0,
{
"account_id": self.a_receivable.id,
"product_id": self.product.product_variant_ids[:1].id,
"name": "Test line",
"quantity": 1.0,
"price_unit": 100.00,
},
),
(
0,
0,
{
"account_id": self.a_receivable.id,
"product_id": self.product.product_variant_ids[:2].id,
"name": "Test line",
"quantity": 1.0,
"price_unit": 100.00,
},
),
],
}
)

def test_account_invoice_pricelist(self):
self.invoice._onchange_partner_id_account_invoice_pricelist()
self.assertEqual(
self.invoice.pricelist_id,
self.invoice.partner_id.property_product_pricelist)
self.invoice.partner_id.property_product_pricelist,
)

def test_account_invoice_change_pricelist(self):
self.invoice.pricelist_id = self.sale_pricelist_id.id
Expand Down
56 changes: 28 additions & 28 deletions account_invoice_pricelist/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="view_account_invoice_customer_form" model="ir.ui.view">
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="inherit_id" ref="account.invoice_form" />
<field name="arch" type="xml">
<button name="action_invoice_open" position="before">
<button type="object"
name="button_update_prices_from_pricelist"
string="Update prices"
attrs="{'invisible': ['|', ('pricelist_id', '=', False), ('state', 'not in', ['draft'])]}"
help="Update price in lines from the pricelist"
<button
type="object"
name="button_update_prices_from_pricelist"
string="Update prices"
attrs="{'invisible': ['|', ('pricelist_id', '=', False), ('state', 'not in', ['draft'])]}"
help="Update price in lines from the pricelist"
/>
</button>
<field name="user_id" position="after">
<field name="pricelist_id"/>
<field name="pricelist_id" />
</field>
</field>
</record>

<record id="view_account_invoice_supplier_form" model="ir.ui.view">
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_supplier_form"/>
<field name="arch" type="xml">
<field name="inherit_id" ref="account.invoice_supplier_form" />
<field name="arch" type="xml">
<button name="action_invoice_open" position="before">
<button type="object"
name="button_update_prices_from_pricelist"
string="Update prices"
attrs="{'invisible': ['|', ('pricelist_id', '=', False), ('state', 'not in', ['draft'])]}"
help="Update price in lines from the pricelist"
<button
type="object"
name="button_update_prices_from_pricelist"
string="Update prices"
attrs="{'invisible': ['|', ('pricelist_id', '=', False), ('state', 'not in', ['draft'])]}"
help="Update price in lines from the pricelist"
/>
</button>
<field name="user_id" position="after">
<field name="pricelist_id"/>
</field>
</field>
<field name="user_id" position="after">
<field name="pricelist_id" />
</field>
</field>
</record>

<record id="view_account_invoice_search" model="ir.ui.view">
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.view_account_invoice_filter"/>
<field name="inherit_id" ref="account.view_account_invoice_filter" />
<field name="arch" type="xml">
<xpath expr="//filter[@name='group_by_partner_id']" position="after">
<filter string="Pricelist"
name="pricelist"
context="{'group_by':'pricelist_id'}"
invisible="context.get('type', '') not in ['out_invoice', 'out_refund']"/>
<filter
string="Pricelist"
name="pricelist"
context="{'group_by':'pricelist_id'}"
invisible="context.get('type', '') not in ['out_invoice', 'out_refund']"
/>
</xpath>
</field>
</record>

</odoo>

0 comments on commit 9e45fe0

Please sign in to comment.