Skip to content

Commit

Permalink
[BACKPORT] product_triple_discount
Browse files Browse the repository at this point in the history
  • Loading branch information
legalsylvain committed Jul 4, 2018
1 parent 43e2129 commit 1b6d1cc
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Account Invoice Triple Discount',
'version': '10.0.1.1.0',
'name': 'Purchase Order Triple Discount',
'version': '8.0.1.0.0',
'category': 'Purchase Management',
'author': 'Tecnativa, '
'GRAP, '
'Odoo Community Association (OCA)',
'website': 'https://tecnativa.com',
'license': 'AGPL-3',
Expand All @@ -17,5 +18,8 @@
'data': [
'views/purchase_view.xml',
],
'demo': [
'demo/purchase_order.xml',
],
'installable': True,
}
38 changes: 38 additions & 0 deletions purchase_triple_discount/demo/purchase_order.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<openerp><data>

<record id="order" model="purchase.order">
<field name="name">Purchase Order</field>
<field name="partner_id" ref="base.res_partner_1"/>
<field name="location_id" ref="stock.stock_location_stock"/>
<field name="pricelist_id" ref="purchase.list0"/>
</record>

<record id="order_line1" model="purchase.order.line">
<field name="name">Line 1</field>
<field name="order_id" ref="order"/>
<field name="product_qty">1.0</field>
<field name="product_id" ref="product.product_product_7"/>
<field name="product_uom" ref="product.product_uom_unit"/>
<field name="taxes_id" eval="[(6, 0, [ref('account_invoice_triple_discount.tax')])]"/>
<field name="price_unit">600</field>
<field name="date_planned">2018-01-19</field>
</record>

<record id="order_line2" model="purchase.order.line">
<field name="name">Line 2</field>
<field name="order_id" ref="order"/>
<field name="product_qty">10.0</field>
<field name="product_id" ref="product.product_product_28"/>
<field name="product_uom" ref="product.product_uom_unit"/>
<field name="taxes_id" eval="[(6, 0, [ref('account_invoice_triple_discount.tax')])]"/>
<field name="price_unit">60</field>
<field name="date_planned">2018-01-19</field>
</record>

</data></openerp>
28 changes: 19 additions & 9 deletions purchase_triple_discount/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@
# Quentin THEURET <[email protected]>, 2017
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 10.0\n"
"Project-Id-Version: Odoo Server 8.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-11-25 08:26+0000\n"
"PO-Revision-Date: 2017-11-25 08:26+0000\n"
"Last-Translator: Quentin THEURET <[email protected]>, 2017\n"
"Language-Team: French (https://www.transifex.com/oca/teams/23907/fr/)\n"
"POT-Creation-Date: 2018-03-02 20:55+0000\n"
"PO-Revision-Date: 2018-03-02 20:55+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Language: fr\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"Plural-Forms: \n"

#. module: purchase_triple_discount
#: model:ir.model.fields,field_description:purchase_triple_discount.field_purchase_order_line_discount2
#: field:purchase.order.line,discount2:0
msgid "Disc. 2 (%)"
msgstr "Rem. 2 (%)"

#. module: purchase_triple_discount
#: model:ir.model.fields,field_description:purchase_triple_discount.field_purchase_order_line_discount3
#: field:purchase.order.line,discount3:0
msgid "Disc. 3 (%)"
msgstr "Rem. 3 (%)"

Expand All @@ -44,7 +43,18 @@ msgstr "La remise 3 doit être inférieure à 100%."
msgid "Invoice"
msgstr "Facture"

#. module: purchase_triple_discount
#: model:ir.model,name:purchase_triple_discount.model_purchase_order
msgid "Purchase Order"
msgstr "Bon de commande"

#. module: purchase_triple_discount
#: model:ir.model,name:purchase_triple_discount.model_purchase_order_line
msgid "Purchase Order Line"
msgstr "Ligne de commande d'achat"

#. module: purchase_triple_discount
#: model:ir.model,name:purchase_triple_discount.model_stock_move
msgid "Stock Move"
msgstr "Mouvement de stock"

1 change: 1 addition & 0 deletions purchase_triple_discount/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from . import account_invoice
from . import purchase_order
from . import stock_move
2 changes: 1 addition & 1 deletion purchase_triple_discount/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from openerp import models


class AccountInvoice(models.Model):
Expand Down
50 changes: 40 additions & 10 deletions purchase_triple_discount/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo.addons import decimal_precision as dp
from openerp import api, fields, models
from openerp.addons import decimal_precision as dp


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

@api.model
def _calc_line_base_price(self, line):
res = super(PurchaseOrderLine, self)._calc_line_base_price(line)
return res * (1 - line.discount2 / 100.0) *\
(1 - line.discount3 / 100.0)

@api.depends('discount2', 'discount3')
def _compute_amount(self):
super(PurchaseOrderLine, self)._compute_amount()
Expand All @@ -32,11 +38,35 @@ def _compute_amount(self):
'Discount 3 must be lower than 100%.'),
]

def _get_discounted_price_unit(self):
price_unit = super(
PurchaseOrderLine, self)._get_discounted_price_unit()
if self.discount2:
price_unit *= (1 - self.discount2 / 100.0)
if self.discount3:
price_unit *= (1 - self.discount3 / 100.0)
return price_unit

class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

@api.model
def _prepare_inv_line(self, account_id, line):
res = super(PurchaseOrder, self)._prepare_inv_line(account_id, line)
res.update({
'discount2': line.discount2,
'discount3': line.discount3,
})
return res

@api.model
def _prepare_order_line_move(
self, order, order_line, picking_id, group_id):
res = super(PurchaseOrder, self)._prepare_order_line_move(
order, order_line, picking_id, group_id)
for vals in res:
vals['price_unit'] = (vals.get('price_unit', 0.0) *
(1 - (order_line.discount2 / 100)) *
(1 - (order_line.discount3 / 100)))
return res

# def _get_discounted_price_unit(self):
# price_unit = super(
# PurchaseOrderLine, self)._get_discounted_price_unit()
# if self.discount2:
# price_unit *= (1 - self.discount2 / 100.0)
# if self.discount3:
# price_unit *= (1 - self.discount3 / 100.0)
# return price_unit
26 changes: 26 additions & 0 deletions purchase_triple_discount/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
# Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, api


class StockMove(models.Model):
_inherit = "stock.move"

@api.model
def _get_invoice_line_vals(self, move, partner, inv_type):
res = super(StockMove, self)._get_invoice_line_vals(
move, partner, inv_type)
order_line = False
if move.purchase_line_id:
order_line = move.purchase_line_id
elif move.origin_returned_move_id.purchase_line_id:
order_line = move.origin_returned_move_id.purchase_line_id
if order_line:
res.update({
'discount2': order_line.discount2,
'discount3': order_line.discount3,
})
return res
79 changes: 18 additions & 61 deletions purchase_triple_discount/tests/test_purchase_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,19 @@
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests import common
from openerp.tests import common


class TestPurchaseOrder(common.SavepointCase):

@classmethod
def setUpClass(cls):
super(TestPurchaseOrder, cls).setUpClass()
cls.partner = cls.env['res.partner'].create({
'name': 'Mr. Odoo',
})
cls.product1 = cls.env['product.product'].create({
'name': 'Test Product 1',
'purchase_method': 'purchase',
})
cls.product2 = cls.env['product.product'].create({
'name': 'Test Product 2',
'purchase_method': 'purchase',
})
cls.tax = cls.env['account.tax'].create({
'name': 'TAX 15%',
'amount_type': 'percent',
'type_tax_use': 'purchase',
'amount': 15.0,
})
cls.order = cls.env['purchase.order'].create({
'partner_id': cls.partner.id
})
po_line = cls.env['purchase.order.line']
cls.po_line1 = po_line.create({
'order_id': cls.order.id,
'product_id': cls.product1.id,
'date_planned': '2018-01-19 00:00:00',
'name': 'Line 1',
'product_qty': 1.0,
'product_uom': cls.product1.uom_id.id,
'taxes_id': [(6, 0, [cls.tax.id])],
'price_unit': 600.0,
})
cls.po_line2 = po_line.create({
'order_id': cls.order.id,
'product_id': cls.product2.id,
'date_planned': '2018-01-19 00:00:00',
'name': 'Line 2',
'product_qty': 10.0,
'product_uom': cls.product2.uom_id.id,
'taxes_id': [(6, 0, [cls.tax.id])],
'price_unit': 60.0,
})
def setUpClass(self):
super(TestPurchaseOrder, self).setUpClass()
self.partner = self.env.ref('base.res_partner_1')
self.receivable_account = self.env.ref('account.a_recv')
self.order = self.env.ref('purchase_triple_discount.order')
self.po_line1 = self.env.ref('purchase_triple_discount.order_line1')
self.po_line2 = self.env.ref('purchase_triple_discount.order_line2')

def test_01_purchase_order_classic_discount(self):
""" Tests with single discount """
Expand Down Expand Up @@ -101,25 +65,18 @@ def test_03_purchase_order_complex_triple_discount(self):
def test_04_purchase_order_triple_discount_invoicing(self):
""" When a confirmed order is invoiced, the resultant invoice
should inherit the discounts """
self.po_line1.discount = 50.0
self.po_line1.discount2 = 50.0
self.po_line1.discount3 = 50.0
self.po_line2.discount3 = 50.0
self.order.button_confirm()
self.invoice = self.env['account.invoice'].create({
'partner_id': self.partner.id,
'purchase_id': self.order.id,
'account_id': self.partner.property_account_payable_id.id,
'type': 'in_invoice',
})
self.invoice.purchase_order_change()
self.invoice._onchange_invoice_line_ids()
self.po_line1.discount = 10.0
self.po_line1.discount2 = 20.0
self.po_line1.discount3 = 30.0
self.po_line2.discount3 = 40.0
self.order.signal_workflow('purchase_confirm')
self.invoice = self.order.invoice_ids[0]
self.assertEqual(self.po_line1.discount,
self.invoice.invoice_line_ids[0].discount)
self.invoice.invoice_line[0].discount)
self.assertEqual(self.po_line1.discount2,
self.invoice.invoice_line_ids[0].discount2)
self.invoice.invoice_line[0].discount2)
self.assertEqual(self.po_line1.discount3,
self.invoice.invoice_line_ids[0].discount3)
self.invoice.invoice_line[0].discount3)
self.assertEqual(self.po_line2.discount3,
self.invoice.invoice_line_ids[1].discount3)
self.invoice.invoice_line[1].discount3)
self.assertEqual(self.order.amount_total, self.invoice.amount_total)
11 changes: 3 additions & 8 deletions purchase_triple_discount/views/purchase_view.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<openerp><data>

<record id="purchase_order_triple_discount_form_view" model="ir.ui.view">
<field name="name">purchase.order.triple.discount.form</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase_discount.purchase_order_form" />
<field name="inherit_id" ref="purchase_discount.purchase_discount_order_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']//tree//field[@name='discount']"
position="after">
<field name="discount2"/>
<field name="discount3"/>
</xpath>
<xpath expr="//field[@name='order_line']//form//field[@name='discount']"
position="after">
<field name="discount2"/>
<field name="discount3"/>
</xpath>
</field>
</record>

</odoo>
</data></openerp>

0 comments on commit 1b6d1cc

Please sign in to comment.