Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11.0 Migration partner_location_auto_create #443

Closed
wants to merge 8 commits into from
Closed
89 changes: 57 additions & 32 deletions partner_location_auto_create/README.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
.. 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

============================
Partner Location Auto Create
============================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! 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%2Fstock--logistics--warehouse-lightgray.png?logo=github
:target: https://github.com/OCA/stock-logistics-warehouse/tree/11.0/partner_location_auto_create
:alt: OCA/stock-logistics-warehouse
.. |badge4| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/153/11.0
:alt: Try me on Runbot

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

This module allows to create locations automatically when creating a partner.
One use case of this feature is to allow a company not only to manage the stocks
in its own warehouses, but also the warehouses of its suppliers.
Expand All @@ -17,53 +33,62 @@ the fields property_stock_customer and property_stock_supplier will be filled au

The modules also adds a button on the partner form to view the related locations.

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

To install this module, you just need to select the module and ensure yourself dependencies are available.

Configuration
=============
**Table of contents**

* Add users to the group "Sales Pricelists" or "Purchase Pricelists" to view the stock location properties on the partner view.
* In the company form, select the default customer and supplier locations. This will set the default parent locations for the partner locations.
.. contents::
:local:

Usage
=====

To use this module, you need to :
To use this module, you need to:

- Create a partner
- Click on the locations button and add specific locations for the partner
#. Just create a new partner -> The custom location should be created
automatically

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

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

Known issues / Roadmap
======================
Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-warehouse/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.

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

Credits
=======

Module developed and tested with Odoo version 8.0
Authors
~~~~~~~

* brain-tec AG
* Savoir-faire Linux

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

* David DUFRESNE <david.dufresne@savoirfairelinux.com>
* Sandy CARTER <sandy.carter@savoirfairelinux.com>
* Adriana IERFINO <adriana.ierfino@savoirfairelinux.com>
* Bruno JOLIVEAU <bruno.joliveau@savoirfairelinux.com>
* David Dufresne <david.dufresne@savoirfairelinux.com>
* Sandy Carter <sandy.carter@savoirfairelinux.com>
* Adriana Ierfino <adriana.ierfino@savoirfairelinux.com>
* Bruno Joliveau <bruno.joliveau@savoirfairelinux.com>
* Kumar Aberer <kumar.aberer@braintec-group.com> (https://braintec-group.com)
*

Maintainer
----------

Odoo Community Association
Maintainers
~~~~~~~~~~~

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.
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

This module is part of the `OCA/stock-logistics-warehouse <https://github.com/OCA/stock-logistics-warehouse/tree/11.0/partner_location_auto_create>`_ project on GitHub.

To contribute to this module, please visit http://odoo-community.org.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
25 changes: 4 additions & 21 deletions partner_location_auto_create/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Savoir-faire Linux
# (<http://www.savoirfairelinux.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Savoir-faire Linux
# © 2018 brain-tec AG (Kumar Aberer <kumar.aberer@braintec-group.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import models
from .hooks import post_init_hook
34 changes: 9 additions & 25 deletions partner_location_auto_create/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Savoir-faire Linux
# (<http://www.savoirfairelinux.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Savoir-faire Linux
# © 2018 brain-tec AG (Kumar Aberer <kumar.aberer@braintec-group.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
'name': 'Partner Location Auto Create',
'version': '0.1',
'author': 'Savoir-faire Linux,Odoo Community Association (OCA)',
'version': '11.0.1.0.0',
'author': 'brain-tec AG, Savoir-faire Linux,Odoo Community Association ('
'OCA)',
'category': 'Warehouse',
'license': 'AGPL-3',
'complexity': 'normal',
Expand All @@ -35,10 +18,11 @@
'demo': [],
'data': [
'views/res_partner_view.xml',
'views/res_company_view.xml',
'views/res_config_settings_view.xml',
'views/stock_location_view.xml',
],
'post_init_hook': 'post_init_hook',
'test': [],
'auto_install': False,
'installable': False,
'installable': True,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty key and flags.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

24 changes: 24 additions & 0 deletions partner_location_auto_create/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# © 2018 brain-tec AG (Kumar Aberer <kumar.aberer@braintec-group.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, SUPERUSER_ID
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is needed anymore. We have a sudo now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed to create a new Environment for the installation hook. Sudo does not help here

import logging
logger = logging.getLogger(__name__)


def post_init_hook(cr, _):
"""Create custom location for existing partners"""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})

logger.info('Create custom locations for partners')

locations = env['stock.location'].search([])
partners = env['res.partner'].search([('is_company', '=', True)])
counter = 0
for partner in partners:
if not locations.filtered(lambda l: l.partner_id == partner):
logger.info('Create custom location for partner id=%s (%s/%s)'
% (partner.id, counter, len(partners)))
partner._create_main_partner_location()
counter += 1
1 change: 1 addition & 0 deletions partner_location_auto_create/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
from . import res_company
from . import res_partner
from . import stock_location
from . import res_config_settings
24 changes: 3 additions & 21 deletions partner_location_auto_create/models/res_company.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Savoir-faire Linux
# (<http://www.savoirfairelinux.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Savoir-faire Linux
# © 2018 brain-tec AG (Kumar Aberer <kumar.aberer@braintec-group.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, fields, api
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openerp => odoo


Expand Down
13 changes: 13 additions & 0 deletions partner_location_auto_create/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# © 2018 brain-tec AG (Kumar Aberer <kumar.aberer@braintec-group.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models, fields


class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

default_customer_location = fields.Many2one(
related='company_id.default_customer_location')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly=True

default_supplier_location = fields.Many2one(
related='company_id.default_supplier_location')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

73 changes: 30 additions & 43 deletions partner_location_auto_create/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Savoir-faire Linux
# (<http://www.savoirfairelinux.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Savoir-faire Linux
# © 2018 brain-tec AG (Kumar Aberer <kumar.aberer@braintec-group.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, fields, api, _
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openerp => odoo


Expand All @@ -32,10 +14,11 @@ class ResPartner(models.Model):
location_ids = fields.One2many(
'stock.location', 'partner_id', string='Locations')

@api.one
@api.multi
@api.depends('location_ids')
def _compute_locations_count(self):
self.locations_count = len(self.location_ids)
for partner in self:
partner.locations_count = len(partner.location_ids)

@api.multi
def button_locations(self):
Expand Down Expand Up @@ -63,21 +46,24 @@ def get_main_location(self, usage):
return self.location_ids.filtered(
lambda l: l.usage == usage and l.main_partner_location)

@api.one
@api.multi
def _create_main_partner_location(self):
if self.customer and self.property_stock_customer.partner_id != self:
location_customer = (
self.get_main_location('customer') or
self._create_main_location('customer'))
for partner in self:
if partner.customer and partner.property_stock_customer.\
partner_id != partner:
location_customer = (
partner.get_main_location('customer') or
partner._create_main_location('customer'))

self.write({'property_stock_customer': location_customer})
partner.property_stock_customer = location_customer

if self.supplier and self.property_stock_supplier.partner_id != self:
location_supplier = (
self.get_main_location('supplier') or
self._create_main_location('supplier'))
if partner.supplier and partner.property_stock_supplier.\
partner_id != partner:
location_supplier = (
partner.get_main_location('supplier') or
partner._create_main_location('supplier'))

self.write({'property_stock_supplier': location_supplier})
partner.property_stock_supplier = location_supplier

@api.multi
def _create_main_location(self, usage):
Expand All @@ -97,7 +83,7 @@ def _create_main_location(self, usage):
'main_partner_location': True,
})

@api.one
@api.multi
def _remove_locations(self):
"""
Unlink all locations related to the partner
Expand All @@ -108,14 +94,15 @@ def _remove_locations(self):
by mistake.
"""
move_obj = self.env['stock.move']
for location in self.location_ids:
moves = move_obj.search([
'|',
('location_id', 'child_of', location.id),
('location_dest_id', 'child_of', location.id),
])
if not moves:
location.unlink()
for partner in self:
for location in partner.location_ids:
moves = move_obj.search([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have locations with huge moves, this could be a bottleneck.
I would have used search_count() instead or search with a limit of 1 before the loop and create a dict with result grouping by location id and then iterating.

'|',
('location_id', 'child_of', location.id),
('location_dest_id', 'child_of', location.id),
])
if not moves:
location.unlink()

@api.model
def create(self, vals):
Expand Down
Loading