Skip to content

Commit

Permalink
Merge PR #610 into 12.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Feb 11, 2020
2 parents 810d50e + e49a6cb commit 5960ec2
Show file tree
Hide file tree
Showing 14 changed files with 886 additions and 0 deletions.
88 changes: 88 additions & 0 deletions product_cost_price_avco_sync/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
============================
Product cost price avco sync
============================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |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--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/stock-logistics-workflow/tree/12.0/product_cost_price_avco_sync
:alt: OCA/stock-logistics-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/stock-logistics-workflow-12-0/stock-logistics-workflow-12-0-product_cost_price_avco_sync
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/154/12.0
:alt: Try me on Runbot

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

This module allows to sync cost price products with average cost method from
stock moves price unit.

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-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/stock-logistics-workflow/issues/new?body=module:%20product_cost_price_avco_sync%0Aversion:%2012.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
~~~~~~~

* Tecnativa

Contributors
~~~~~~~~~~~~

* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Dauden <[email protected]>
* Sergio Teruel <[email protected]>

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

This module is maintained by the OCA.

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

.. |maintainer-carlosdauden| image:: https://github.com/carlosdauden.png?size=40px
:target: https://github.com/carlosdauden
:alt: carlosdauden
.. |maintainer-sergio-teruel| image:: https://github.com/sergio-teruel.png?size=40px
:target: https://github.com/sergio-teruel
:alt: sergio-teruel

Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-carlosdauden| |maintainer-sergio-teruel|

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions product_cost_price_avco_sync/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import models
18 changes: 18 additions & 0 deletions product_cost_price_avco_sync/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2020 Tecnativa - Carlos Dauden
# Copyright 2020 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
'name': 'Product cost price avco sync',
'summary': 'Set product cost price from updated moves',
'version': '12.0.1.0.0',
'development_status': 'Production/Stable',
'category': 'Stock',
'website': 'https://github.com/OCA/stock-logistics-workflow',
'author': 'Tecnativa, Odoo Community Association (OCA)',
'maintainers': ['carlosdauden', 'sergio-teruel'],
'license': 'AGPL-3',
'installable': True,
'depends': [
'stock_account',
],
}
38 changes: 38 additions & 0 deletions product_cost_price_avco_sync/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * product_cost_price_avco_sync
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-10-29 10:28+0000\n"
"PO-Revision-Date: 2019-10-29 11:30+0100\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 2.0.6\n"
"Last-Translator: \n"
"Language: es\n"

#. module: product_cost_price_avco_sync
#: model:ir.model,name:product_cost_price_avco_sync.model_product_product
msgid "Product"
msgstr "Producto"

#. module: product_cost_price_avco_sync
#: model:ir.model,name:product_cost_price_avco_sync.model_purchase_order_line
msgid "Purchase Order Line"
msgstr "Línea pedido de compra"

#. module: product_cost_price_avco_sync
#: model:ir.model,name:product_cost_price_avco_sync.model_stock_move
msgid "Stock Move"
msgstr "Movimiento de existencias"

#. module: product_cost_price_avco_sync
#: model:ir.model,name:product_cost_price_avco_sync.model_stock_picking
msgid "Transfer"
msgstr "Transferir"
25 changes: 25 additions & 0 deletions product_cost_price_avco_sync/i18n/product_cost_price_avco_sync.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * product_cost_price_avco_sync
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: product_cost_price_avco_sync
#: model:ir.model,name:product_cost_price_avco_sync.model_stock_move
msgid "Stock Move"
msgstr ""

#. module: product_cost_price_avco_sync
#: model:ir.model,name:product_cost_price_avco_sync.model_stock_picking
msgid "Transfer"
msgstr ""

3 changes: 3 additions & 0 deletions product_cost_price_avco_sync/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import stock_move
from . import stock_picking
101 changes: 101 additions & 0 deletions product_cost_price_avco_sync/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright 2019 Carlos Dauden - Tecnativa <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import timedelta

from odoo import fields, models
from odoo.tools.float_utils import float_compare


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

def write(self, vals):
""" Update cost price avco """
res = super(StockMove, self).write(vals)
if (('price_unit' in vals or 'quantity_done' in vals) and
not self.env.context.get('skip_avco_sync')):
self.cost_price_avco_sync()
return res

def _create_price_history(self, value):
self.env['product.price.history'].create([{
'product_id': move.product_id.id,
'cost': value,
'datetime': move.date,
'company_id': move.company_id.id,
} for move in self])

def _previous_instant_date(self):
""" Returns previous instant before move was done"""
self.ensure_one()
date_with_delta = fields.Datetime.from_string(
self.date) - timedelta(seconds=1)
return fields.Datetime.to_string(date_with_delta)

def _remove_after_history_price(self):
PriceHistory = self.env['product.price.history']
for move in self:
PriceHistory.search([
('company_id', '=', move.company_id.id),
('product_id', '=', move.product_id.id),
('datetime', '>=', move.date),
]).unlink()

def get_average_price(self, previous_qty, previous_price):
if self.quantity_done == 0.0:
price = previous_price
elif previous_qty < 0.0:
price = self.price_unit
else:
price = ((previous_qty * previous_price) +
(self.quantity_done * self.price_unit)
) / (previous_qty + self.quantity_done)
return price

def cost_price_avco_sync(self):
procesed_moves = set()
for move in self.sorted('date'):
if (move.id in procesed_moves or move.state != 'done' or
not move._is_in() or
move.product_id.cost_method != 'average'):
continue
previous_instant = move._previous_instant_date()
previous_qty = move.product_id.with_context(
to_date=previous_instant
).qty_available
previous_price = move.product_id.get_history_price(
move.company_id.id, previous_instant)
price = move.get_average_price(previous_qty, previous_price)
move._remove_after_history_price()
move._create_price_history(price)
previous_qty += move.quantity_done
affected_moves = self.with_context(skip_avco_sync=True).search([
('product_id', '=', move.product_id.id),
('date', '>', move.date),
], order='date')
for af_move in affected_moves:
if af_move._is_in():
if af_move.inventory_id or af_move.move_orig_ids:
af_move.write({
'price_unit': price,
'value': price * af_move.quantity_done,
})
# Avoid reprocess move if included in self.write
procesed_moves.add(af_move.id)
price = af_move.get_average_price(previous_qty, price)
af_move._create_price_history(price)
previous_qty += af_move.quantity_done
elif af_move._is_out():
af_move.write({
'price_unit': -price,
'value': -price * af_move.quantity_done,
})
previous_qty -= af_move.quantity_done
if float_compare(move.product_id.standard_price, price,
precision_rounding=move.product_uom.rounding):
# Write the standard price, as SUPERUSER_ID because a warehouse
# manager may not have the right to write on products
move.product_id.with_context(
force_company=move.company_id.id,
).sudo().write({'standard_price': price})
13 changes: 13 additions & 0 deletions product_cost_price_avco_sync/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2019 Carlos Dauden - Tecnativa <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class StockPicking(models.Model):
_inherit = 'stock.picking'

def action_done(self):
""" Avoid AVCO cost price recomputation when validating picking """
return super(StockPicking, self.with_context(
skip_avco_sync=True)).action_done()
4 changes: 4 additions & 0 deletions product_cost_price_avco_sync/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Dauden <[email protected]>
* Sergio Teruel <[email protected]>
2 changes: 2 additions & 0 deletions product_cost_price_avco_sync/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows to sync cost price products with average cost method from
stock moves price unit.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5960ec2

Please sign in to comment.