Skip to content

Commit

Permalink
[MIG] purchase_triple_discount: Migration to 11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chienandalu committed Aug 5, 2019
1 parent 2db596a commit 82b7883
Show file tree
Hide file tree
Showing 19 changed files with 816 additions and 47 deletions.
1 change: 1 addition & 0 deletions oca_dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
account-invoicing
server-tools
product-attribute
server-ux
Expand Down
89 changes: 61 additions & 28 deletions purchase_triple_discount/README.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
.. 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

========================
Purchase Triple Discount
========================
===============================
Account Invoice Triple Discount
===============================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/purchase-workflow/tree/11.0/purchase_triple_discount
:alt: OCA/purchase-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/purchase-workflow-11-0/purchase-workflow-11-0-purchase_triple_discount
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/142/11.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to have three successive discounts on every purchase order
line.

**Table of contents**

.. contents::
:local:

Usage
=====

Expand All @@ -30,47 +54,56 @@ Unit price: 600.00 ->
- Disc. 1 = 50% -> Amount = 300.00
- Disc. 2 = -5% -> Amount = 315.00

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

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

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/purchase-workflow/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.
* When the purchase order is validated, the discounts will be added to the
corresponding vendor pricelist.
* Vendor pricelists can be edited as well with their corresponding new second
and third discounts.
* A default second or third discount can be set in every vendor
*Sale & Purchases* tab.

Known issues / Roadmap
======================

* Include second and third discount in purchase order report.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/purchase-workflow/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/purchase-workflow/issues/new?body=module:%20purchase_triple_discount%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Images
------
Authors
~~~~~~~

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

Contributors
------------
~~~~~~~~~~~~

* David Vidal <david.vidal@tecnativa.com>
* `Tecnativa <https://www.tecnativa.com>`_:

Maintainer
----------
* David Vidal

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. 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.
This module is part of the `OCA/purchase-workflow <https://github.com/OCA/purchase-workflow/tree/11.0/purchase_triple_discount>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 0 additions & 2 deletions purchase_triple_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# -*- coding: utf-8 -*-

from . import models
9 changes: 5 additions & 4 deletions purchase_triple_discount/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# -*- coding: utf-8 -*-
# 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',
'version': '11.0.1.0.0',
'category': 'Purchase Management',
'author': 'Tecnativa, '
'Odoo Community Association (OCA)',
'website': 'https://tecnativa.com',
'website': 'https://github.com/OCA/purchase-workflow',
'license': 'AGPL-3',
'summary': 'Manage triple discount on purchase order lines',
'depends': [
'purchase_discount',
'product_supplierinfo_discount',
'account_invoice_triple_discount',
],
'data': [
'views/product_supplierinfo_view.xml',
'views/purchase_view.xml',
'views/res_partner_view.xml',
],
'installable': True,
}
5 changes: 3 additions & 2 deletions purchase_triple_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-

from . import account_invoice
from . import procurement_rule
from . import product_supplierinfo
from . import purchase_order
from . import res_partner
1 change: 0 additions & 1 deletion purchase_triple_discount/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

Expand Down
25 changes: 25 additions & 0 deletions purchase_triple_discount/models/procurement_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2019 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models


class ProcurementRule(models.Model):
_inherit = 'procurement.rule'

def _prepare_purchase_order_line(self, product_id, product_qty,
product_uom, values, po, supplier):
"""Apply the discount to the created purchase order"""
res = super(ProcurementRule, self)._prepare_purchase_order_line(
product_id, product_qty, product_uom, values, po, supplier)
date = None
if po.date_order:
date = fields.Date.to_string(
fields.Date.from_string(po.date_order))
seller = product_id._select_seller(
partner_id=supplier.name,
quantity=product_qty,
date=date, uom_id=product_uom)
if seller:
res['discount2'] = seller.discount2
res['discount3'] = seller.discount3
return res
43 changes: 43 additions & 0 deletions purchase_triple_discount/models/product_supplierinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2019 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields, api
import odoo.addons.decimal_precision as dp


class ProductSupplierInfo(models.Model):
_inherit = 'product.supplierinfo'

discount2 = fields.Float(
string='Discount 2 (%)',
digits=dp.get_precision('Discount'),
)
discount3 = fields.Float(
string='Discount 3 (%)',
digits=dp.get_precision('Discount'),
)

@api.onchange('name')
def onchange_name(self):
"""Apply the default supplier discounts of the selected supplier"""
for supplierinfo in self.filtered('name'):
supplierinfo.discount2 = (
supplierinfo.name.default_supplierinfo_discount2)
supplierinfo.discount3 = (
supplierinfo.name.default_supplierinfo_discount3)
return super().onchange_name()

@api.model
def create(self, vals):
"""Insert discounts 2 and 3 from context from purchase.order's
_add_supplier_to_product method"""
if ('discount2_map' in self.env.context and
not vals.get('discount2') and
vals['product_tmpl_id'] in self.env.context['discount2_map']):
vals['discount2'] = self.env.context['discount2_map'][
vals['product_tmpl_id']]
if ('discount3_map' in self.env.context and
not vals.get('discount3') and
vals['product_tmpl_id'] in self.env.context['discount3_map']):
vals['discount3'] = self.env.context['discount3_map'][
vals['product_tmpl_id']]
return super(ProductSupplierInfo, self).create(vals)
44 changes: 39 additions & 5 deletions purchase_triple_discount/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Tecnativa - David Vidal
# Copyright 2017-19 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


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

def _add_supplier_to_product(self):
"""Insert a mapping of products to discounts to be picked up
in supplierinfo's create()"""
self.ensure_one()
discount2_map = dict(
[(line.product_id.product_tmpl_id.id, line.discount2)
for line in self.order_line.filtered('discount2')])
discount3_map = dict(
[(line.product_id.product_tmpl_id.id, line.discount3)
for line in self.order_line.filtered('discount3')])
return super(PurchaseOrder, self.with_context(
discount2_map=discount2_map, discount3_map=discount3_map)
)._add_supplier_to_product()


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

Expand All @@ -16,13 +32,11 @@ def _compute_amount(self):
discount2 = fields.Float(
'Disc. 2 (%)',
digits=dp.get_precision('Discount'),
default=0.0,
)

discount3 = fields.Float(
'Disc. 3 (%)',
digits=dp.get_precision('Discount'),
default=0.0,
)

_sql_constraints = [
Expand All @@ -40,3 +54,23 @@ def _get_discounted_price_unit(self):
if self.discount3:
price_unit *= (1 - self.discount3 / 100.0)
return price_unit

@api.onchange('product_qty', 'product_uom')
def _onchange_quantity(self):
"""
Check if a discount is defined into the supplier info and if so then
apply it to the current purchase order line
"""
res = super(PurchaseOrderLine, self)._onchange_quantity()
if self.product_id:
date = None
if self.order_id.date_order:
date = fields.Date.to_string(
fields.Date.from_string(self.order_id.date_order))
product_supplierinfo = self.product_id._select_seller(
partner_id=self.partner_id, quantity=self.product_qty,
date=date, uom_id=self.product_uom)
if product_supplierinfo:
self.discount2 = product_supplierinfo.discount2
self.discount3 = product_supplierinfo.discount3
return res
21 changes: 21 additions & 0 deletions purchase_triple_discount/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2019 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields
from odoo.addons import decimal_precision as dp


class ResPartner(models.Model):
_inherit = "res.partner"

default_supplierinfo_discount2 = fields.Float(
string='Default Supplier Discount 2 (%)',
digits=dp.get_precision('Discount'),
help="This value will be used as the default one, for each new "
"supplierinfo line depending on that supplier.",
)
default_supplierinfo_discount3 = fields.Float(
string='Default Supplier Discount 3 (%)',
digits=dp.get_precision('Discount'),
help="This value will be used as the default one, for each new "
"supplierinfo line depending on that supplier.",
)
3 changes: 3 additions & 0 deletions purchase_triple_discount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
2 changes: 2 additions & 0 deletions purchase_triple_discount/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows to have three successive discounts on every purchase order
line.
1 change: 1 addition & 0 deletions purchase_triple_discount/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Include second and third discount in purchase order report.
24 changes: 24 additions & 0 deletions purchase_triple_discount/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Create a new purchase order and add discounts in any of the three discount
fields given. They go in order of precedence so discount 2 will be calculated
over discount 1 and discount 3 over the result of discount 2. For example,
let's divide by two on every discount:

Unit price: 600.00 ->

- Disc. 1 = 50% -> Amount = 300.00
- Disc. 2 = 50% -> Amount = 150.00
- Disc. 3 = 50% -> Amount = 75.00

You can also use negative values to charge instead of discount:

Unit price: 600.00 ->

- Disc. 1 = 50% -> Amount = 300.00
- Disc. 2 = -5% -> Amount = 315.00

* When the purchase order is validated, the discounts will be added to the
corresponding vendor pricelist.
* Vendor pricelists can be edited as well with their corresponding new second
and third discounts.
* A default second or third discount can be set in every vendor
*Sale & Purchases* tab.
Loading

0 comments on commit 82b7883

Please sign in to comment.