Skip to content

Commit

Permalink
[ADD] sale_gathering_index: add new sale gathering module with index
Browse files Browse the repository at this point in the history
  • Loading branch information
lef-adhoc committed Jan 19, 2024
1 parent bc6e7b5 commit 3afc902
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
70 changes: 70 additions & 0 deletions sale_gathering_index/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.. |company| replace:: ADHOC SA

.. |company_logo| image:: https://raw.githubusercontent.com/ingadhoc/maintainer-tools/master/resources/adhoc-logo.png
:alt: ADHOC SA
:target: https://www.adhoc.com.ar

.. |icon| image:: https://raw.githubusercontent.com/ingadhoc/maintainer-tools/master/resources/adhoc-icon.png

.. image:: https://img.shields.io/badge/license-AGPL--3-blue.png
:target: https://www.gnu.org/licenses/agpl
:alt: License: AGPL-3

====================
Sale Gathering Index
====================

#.

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

To install this module, you need to:

#. Only need to install the module.

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

To configure this module, you need to:

#. Nothing to configure.

Usage
=====

To use this module, you need to:

#. Just use.

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: http://runbot.adhoc.com.ar/

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

Bugs are tracked on `GitHub Issues
<https://github.com/ingadhoc/sale/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.

Credits
=======

Images
------

* |company| |icon|

Contributors
------------

Maintainer
----------

|company_logo|

This module is maintained by the |company|.

To contribute to this module, please visit https://www.adhoc.com.ar.
5 changes: 5 additions & 0 deletions sale_gathering_index/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from . import models
42 changes: 42 additions & 0 deletions sale_gathering_index/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
##############################################################################
#
# Copyright (C) 2015 ADHOC SA (http://www.adhoc.com.ar)
# All Rights Reserved.
#
# 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/>.
#
##############################################################################
{
'name': 'Sale Gathering Index',
'version': "16.0.1.0.0",
'category': 'Sales & Stock',
'sequence': 14,
'summary': '',
'author': 'ADHOC SA',
'website': 'www.adhoc.com.ar',
'license': 'AGPL-3',
'images': [
],
'depends': [
'sale_gathering'
],
'data': [
'views/sale_order_views.xml'
],
'demo': [
],
'installable': True,
'auto_install': False,
'application': False,
}
53 changes: 53 additions & 0 deletions sale_gathering_index/i18n/es_AR.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * sale_gathering_index
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-01-03 14:16+0000\n"
"PO-Revision-Date: 2024-01-03 14:16+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"
"Plural-Forms: \n"

#. module: sale_gathering_index
#: model:ir.model.fields,field_description:sale_gathering_index.field_sale_order__gathering_balance_indexed
msgid "Gathering Balance Indexed"
msgstr "Balance de acopio indexado"

#. module: sale_gathering_index
#: model:ir.model.fields,field_description:sale_gathering_index.field_sale_order__index
msgid "Index"
msgstr "Índice"

#. module: sale_gathering_index
#: model:ir.model,name:sale_gathering_index.model_sale_order
msgid "Sales Order"
msgstr "Pedido de venta"

#. module: sale_gathering_index
#: model:ir.model,name:sale_gathering_index.model_sale_order_line
msgid "Sales Order Line"
msgstr "Línea de pedido de venta"

#. module: sale_gathering_index
#. odoo-python
#: code:addons/sale_gathering_index/models/sale_order_line.py:0
#, python-format
msgid ""
"You can't add an already gathered product more than once. Please modify the "
"quantity of the existing line."
msgstr "No puedes agregar un producto de acopio más de una vez. Por favor modifique el la cantidad de la línea existente."

#. module: sale_gathering_index
#. odoo-python
#: code:addons/sale_gathering_index/models/sale_order_line.py:0
#, python-format
msgid ""
"You can't modify the quantity of an added product. Please add a new line."
msgstr "No puedes modificar la cantidad de un producto agregado. Por favor agregar una nueva línea."
6 changes: 6 additions & 0 deletions sale_gathering_index/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from . import sale_order
from . import sale_order_line
36 changes: 36 additions & 0 deletions sale_gathering_index/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from odoo import models, fields, api


class SaleOrder(models.Model):
_inherit = 'sale.order'

index = fields.Float('Index', compute="_compute_index")
gathering_balance_indexed = fields.Float(
compute="_compute_gathering_balance_indexed",
digits='Product Price',
)

@api.depends('order_line.product_template_id.list_price')
def _compute_index(self):
for order in self:
if order.is_gathering:
order_lines_gathered = order.order_line.filtered(lambda x: x.initial_qty_gathered > 0)

if not order_lines_gathered:
order.index = 0.0
continue

qty = sum(order_lines_gathered.mapped('initial_qty_gathered'))

index = sum(
(line.product_template_id.list_price - line.price_unit) / line.price_unit * (line.initial_qty_gathered / qty)
for line in order_lines_gathered
)

order.index = index
else:
order.index = 0.0

@api.depends('gathering_balance', 'index')
def _compute_gathering_balance_indexed(self):
self.gathering_balance_indexed = self.gathering_balance * (self.index + 1)
48 changes: 48 additions & 0 deletions sale_gathering_index/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from odoo import models, api, _
from odoo.exceptions import UserError


class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'

@api.model_create_multi
def create(self, vals_list):
order_id = vals_list[0].get('order_id') if vals_list else False
if (
order_id
and self.env['sale.order'].browse(order_id).is_gathering
and self.env['sale.order'].browse(order_id).state == 'sale'
):
existing_product_ids = set(self.env['sale.order.line'].search([
('order_id', '=', order_id),
('initial_qty_gathered', '>', 0)
]).mapped('product_template_id.id'))
new_product_ids = set(vals.get('product_template_id') for vals in vals_list)
if existing_product_ids.intersection(new_product_ids):
raise UserError(_("You can't add an already gathered product more than once. Please modify the quantity of the existing line."))
lines = super(SaleOrderLine, self).create(vals_list)
for line in lines:
line._update_line_for_gathering()
return lines
else:
return super(SaleOrderLine, self).create(vals_list)

def write(self, vals):
if (
self.order_id.is_gathering
and self.order_id.state == 'sale'
and 'product_uom_qty' in vals
and vals['product_uom_qty'] > self.product_uom_qty
and 'initial_qty_gathered' not in vals
and self.initial_qty_gathered == 0
):
raise UserError(_("You can't modify the quantity of an added product. Please add a new line."))
return super(SaleOrderLine, self).write(vals)

def _update_line_for_gathering(self):
if (
self.order_id.is_gathering and self.order_id.state == 'sale'
and self.display_type not in ['line_section', 'line_note']
):
self.price_unit /= (1 + self.order_id.index)
self.name += f' ({str(round(self.order_id.index*100, 2))}%) (${str(round(self.product_template_id.list_price, 2))})'
22 changes: 22 additions & 0 deletions sale_gathering_index/views/sale_order_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_order_form" model="ir.ui.view">
<field name="name">sale.order.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="payment_term_id" position="after">
<field name="index" attrs="{'invisible': [('is_gathering', '=', False)]}" widget="percentage"/>
</field>
<xpath expr="//field[@name='order_line']/tree//field[@name='name']" position="attributes">
<attribute name="attrs">{'readonly': [('parent.is_gathering', '=', True), ('display_type', 'not in', ['line_section', 'line_note'])]}</attribute>
</xpath>
<xpath expr="//field[@name='order_line']/tree//field[@name='price_unit']" position="attributes">
<attribute name="attrs">{'readonly': [('parent.is_gathering', '=', True), ('state', '=', 'sale')]}</attribute>
</xpath>
<group name="sale_total" position="inside">
<field name="gathering_balance_indexed" widget="monetary" options="{'currency_field': 'currency_id'}"/>
</group>
</field>
</record>
</odoo>

0 comments on commit 3afc902

Please sign in to comment.