Skip to content

Commit

Permalink
Merge pull request #716 from SimoRubi/10.0-add-sale_order_type_trigger
Browse files Browse the repository at this point in the history
[10.0][IMP] sale_order_type: find type by rule
  • Loading branch information
pedrobaeza authored Dec 21, 2018
2 parents 7c424d4 + f3b721f commit 87bfc95
Show file tree
Hide file tree
Showing 21 changed files with 881 additions and 24 deletions.
83 changes: 67 additions & 16 deletions sale_order_type/README.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
===============
Sale Order Type
===============

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! 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

Sales Order Type
================
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/sale-workflow/tree/10.0/sale_order_type
:alt: OCA/sale-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-workflow-10-0/sale-workflow-10-0-sale_order_type
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/167/10.0
:alt: Try me on Runbot

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

This module adds a typology for the sales orders. In each different type, you
can define, invoicing and refunding journal, a warehouse, a sequence,
can define: invoicing and refunding journal, a warehouse, a sequence,
the shipping policy, the invoicing policy, a payment term, a pricelist
and an incoterm.

Expand All @@ -14,6 +35,22 @@ You can see sale types as lines of business.
You are able to select a sales order type by partner so that when you add a
partner to a sales order it will get the related info to it.

Rules can also be associated with sale order types.

Inside each rule, you can select any number of products and/or product categories.

When editing a sale order that has no type, if a product matches the product of any rule then the sale order type bound to the rule is associated to the sale order.
If the rule does not match *by product*, product categories are checked.

In the sale order form you can also find the matching order type by clicking on the button *Find by rule* placed near the *Type* field.

The sale order types and the rules are inspected based on the value of their *sequence* field.

**Table of contents**

.. contents::
:local:

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

Expand All @@ -27,26 +64,37 @@ Usage

* Go to **Sales > Sales Orders** and create a new sale order. Select the new type you have created before and all settings will be propagated.
* You can also define a type for a particular partner if you go to *Sales & Purchases* and set a sale order type.
* You can also find the matching order type by clicking on the button *Find by rule* placed near the *Type* field

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/167/10.0
Known issues / Roadmap
======================

* Manage the order of *sale.order.type.rule* similar to *product.pricelist.item* (see field *applied_on*) instead of using *sequence*.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-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
`here <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_order_type%0Aversion:%20{version}%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_order_type%0Aversion:%2010.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
=======

Authors
~~~~~~~

* Grupo Vermon
* AvanzOSC
* Tecnativa
* Agile Business Group
* Niboo

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

* Carlos Sánchez Cifuentes <csanchez@grupovermon.com>
* Oihane Crucelaegui <oihanecrucelaegi@avanzosc.es>
Expand All @@ -57,18 +105,21 @@ Contributors
* Lorenzo Battistini <lorenzo.battistini@agilebg.com>
* Samuel Lefever <sam@niboo.be>
* Pierre Faniel <pierre@niboo.be>
* Simone Rubino <simone.rubino@agilebg.com>

Maintainer
----------
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 http://odoo-community.org.
This module is part of the `OCA/sale-workflow <https://github.com/OCA/sale-workflow/tree/10.0/sale_order_type>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
6 changes: 3 additions & 3 deletions sale_order_type/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{
"name": "Sale Order Type",
"version": "10.0.1.0.1",
"version": "10.0.1.1.0",
"category": "Sales Management",
"author": "Grupo Vermon,"
"AvanzOSC,"
Expand All @@ -19,8 +19,7 @@
"website": "https://github.com/OCA/sale-workflow",
"license": "AGPL-3",
"depends": [
'sale_stock',
'account',
'sale_stock'
],
"demo": [
"demo/sale_order_demo.xml",
Expand All @@ -30,6 +29,7 @@
"security/security.xml",
"views/sale_order_view.xml",
"views/sale_order_type_view.xml",
"views/sale_order_type_rule_view.xml",
"views/account_invoice_view.xml",
"views/res_partner_view.xml",
"data/default_type.xml",
Expand Down
1 change: 1 addition & 0 deletions sale_order_type/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import sale_order_type
from . import sale_order_type_rule
from . import sale_order
from . import res_partner
from . import account_invoice
12 changes: 11 additions & 1 deletion sale_order_type/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def _onchange_partner_id(self):
@api.onchange('sale_type_id')
def onchange_sale_type_id(self):
if self.sale_type_id.payment_term_id:
self.payment_term = self.sale_type_id.payment_term_id.id
self.payment_term_id = self.sale_type_id.payment_term_id.id
if self.sale_type_id.journal_id:
self.journal_id = self.sale_type_id.journal_id.id

@api.multi
def match_order_type(self):
order_types = self.env['sale.order.type'].search([])
for invoice in self:
for order_type in order_types:
if order_type.matches_invoice(invoice):
invoice.sale_type_id = order_type
invoice.onchange_sale_type_id()
break
17 changes: 17 additions & 0 deletions sale_order_type/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ def onchange_type_id(self):
if order.type_id.incoterm_id:
order.incoterm = order.type_id.incoterm_id.id

@api.multi
@api.onchange('order_line')
def _onchange_order_line(self):
for order in self:
if not order.type_id:
order.match_order_type()

@api.multi
def match_order_type(self):
order_types = self.env['sale.order.type'].search([])
for order in self:
for order_type in order_types:
if order_type.matches_order(order):
order.type_id = order_type
order.onchange_type_id()
break

@api.model
def create(self, vals):
if vals.get('name', '/') == '/'and vals.get('type_id'):
Expand Down
15 changes: 15 additions & 0 deletions sale_order_type/models/sale_order_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class SaleOrderTypology(models.Model):
_name = 'sale.order.type'
_description = 'Type of sale order'
_order = 'sequence'

@api.model
def _get_domain_sequence_id(self):
Expand Down Expand Up @@ -41,3 +42,17 @@ def default_picking_policy(self):
payment_term_id = fields.Many2one('account.payment.term', 'Payment Term')
pricelist_id = fields.Many2one('product.pricelist', 'Pricelist')
incoterm_id = fields.Many2one('stock.incoterms', 'Incoterm')
sequence = fields.Integer(default=10)
rule_ids = fields.One2many(
comodel_name='sale.order.type.rule', inverse_name='order_type_id',
copy=True)

@api.multi
def matches_order(self, order):
self.ensure_one()
return any(rule.matches_order(order) for rule in self.rule_ids)

@api.multi
def matches_invoice(self, invoice):
self.ensure_one()
return any(rule.matches_invoice(invoice) for rule in self.rule_ids)
59 changes: 59 additions & 0 deletions sale_order_type/models/sale_order_type_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Simone Rubino - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models


class SaleOrderTypeRule(models.Model):
_name = 'sale.order.type.rule'
_order = 'sequence'

name = fields.Char(required=True)
sequence = fields.Integer(default=10)
order_type_id = fields.Many2one(
comodel_name='sale.order.type', ondelete='cascade')
product_ids = fields.Many2many(
comodel_name='product.product',
string='Products')
product_category_ids = fields.Many2many(
comodel_name='product.category',
string='Product categories')

@api.multi
def matches_order(self, order):
"""Return True if the rule matches the order,
False otherwise"""
self.ensure_one()
order_products = order.order_line.mapped('product_id')
return self.matches_products(order_products) \
or self.matches_product_categories(
order_products.mapped('categ_id'))

@api.multi
def matches_products(self, products):
"""Return True if the rule matches any of the products,
False otherwise"""
self.ensure_one()
return self.product_ids and any(
[rule_product in products
for rule_product in self.product_ids])

@api.multi
def matches_product_categories(self, categories):
"""Return True if the rule matches any of the categories,
False otherwise"""
self.ensure_one()
return self.product_category_ids and any(
[rule_category in categories
for rule_category in self.product_category_ids])

@api.multi
def matches_invoice(self, invoice):
"""Return True if the rule matches the invoice,
False otherwise"""
self.ensure_one()
invoice_products = invoice.invoice_line_ids.mapped('product_id')
return self.matches_products(invoice_products) \
or self.matches_product_categories(
invoice_products.mapped('categ_id'))
4 changes: 4 additions & 0 deletions sale_order_type/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
To configure Sale Order Types you need to:

1. Go to **Sales > Configuration > Sales Orders Types**
2. Create a new sale order type with all the settings you want
10 changes: 10 additions & 0 deletions sale_order_type/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* Carlos Sánchez Cifuentes <csanchez@grupovermon.com>
* Oihane Crucelaegui <oihanecrucelaegi@avanzosc.es>
* Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
* Ana Juaristi <anajuaristi@avanzosc.es>
* Daniel Campos <danielcampos@avanzosc.es>
* Ainara Galdona <ainaragaldona@avanzosc.es>
* Lorenzo Battistini <lorenzo.battistini@agilebg.com>
* Samuel Lefever <sam@niboo.be>
* Pierre Faniel <pierre@niboo.be>
* Simone Rubino <simone.rubino@agilebg.com>
20 changes: 20 additions & 0 deletions sale_order_type/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
This module adds a typology for the sales orders. In each different type, you
can define: invoicing and refunding journal, a warehouse, a sequence,
the shipping policy, the invoicing policy, a payment term, a pricelist
and an incoterm.

You can see sale types as lines of business.

You are able to select a sales order type by partner so that when you add a
partner to a sales order it will get the related info to it.

Rules can also be associated with sale order types.

Inside each rule, you can select any number of products and/or product categories.

When editing a sale order that has no type, if a product matches the product of any rule then the sale order type bound to the rule is associated to the sale order.
If the rule does not match *by product*, product categories are checked.

In the sale order form you can also find the matching order type by clicking on the button *Find by rule* placed near the *Type* field.

The sale order types and the rules are inspected based on the value of their *sequence* field.
1 change: 1 addition & 0 deletions sale_order_type/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Manage the order of *sale.order.type.rule* similar to *product.pricelist.item* (see field *applied_on*) instead of using *sequence*.
3 changes: 3 additions & 0 deletions sale_order_type/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Go to **Sales > Sales Orders** and create a new sale order. Select the new type you have created before and all settings will be propagated.
* You can also define a type for a particular partner if you go to *Sales & Purchases* and set a sale order type.
* You can also find the matching order type by clicking on the button *Find by rule* placed near the *Type* field
3 changes: 3 additions & 0 deletions sale_order_type/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_sale_order_type_manager,access_sale_order_type_manager,model_sale_order_type,sales_team.group_sale_manager,1,1,1,1
access_sale_order_type_salesman,access_sale_order_type_salesman,model_sale_order_type,sales_team.group_sale_salesman,1,0,0,0
access_sale_order_type_account,access_sale_order_type_account,model_sale_order_type,account.group_account_invoice,1,0,0,0
access_sale_order_type_rule_manager,access_sale_order_type_rule_manager,model_sale_order_type_rule,sales_team.group_sale_manager,1,1,1,1
access_sale_order_type_rule_salesman,access_sale_order_type_rule_salesman,model_sale_order_type_rule,sales_team.group_sale_salesman,1,0,0,0
access_sale_order_type_rule_account,access_sale_order_type_rule_account,model_sale_order_type_rule,account.group_account_invoice,1,0,0,0
Loading

0 comments on commit 87bfc95

Please sign in to comment.