Skip to content

Commit

Permalink
[IMP] product_abc_classification_sale_stock: pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rousseldenis committed Sep 18, 2023
1 parent 58eb9e7 commit 9948e15
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 96 deletions.
1 change: 0 additions & 1 deletion product_abc_classification_sale_stock/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="abc_classification_profile_sale_stock" model="abc.classification.profile">
<field name="name">Sale stock profile</field>
<field name="profile_type">sale_stock</field>
<field name="warehouse_id" ref="stock.warehouse0"/>
<field name="warehouse_id" ref="stock.warehouse0" />
<field name="period">365</field>
<field name="level_ids" eval="[(6,0,[ref('abc_classification_level_a'), ref('abc_classification_level_b'), ref('abc_classification_level_c')])]"/>
<field
name="level_ids"
eval="[(6,0,[ref('abc_classification_level_a'), ref('abc_classification_level_b'), ref('abc_classification_level_c')])]"
/>
</record>
</odoo>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import csv
from cStringIO import StringIO
from datetime import datetime, timedelta
from operator import attrgetter

from cStringIO import StringIO

from odoo import _, api, fields, models
from odoo.tools import float_round
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_round


class AbcClassificationProfile(models.Model):
Expand Down Expand Up @@ -38,9 +38,9 @@ def _check_warehouse_id(self):
for rec in self:
if rec.profile_type == "sale_stock" and not rec.warehouse_id:
raise ValidationError(
_(
"You must specify a warehouse for {profile_name}"
).forman(profile_name=rec.name)
_("You must specify a warehouse for {profile_name}").forman(
profile_name=rec.name
)
)

@api.model
Expand Down Expand Up @@ -74,8 +74,7 @@ def _get_all_product_ids(self):
return {r[0] for r in self.env.cr.fetchall()}

def _get_data(self, from_date=None):
"""Get a list of statics info from the DB ordered by number of lines desc
"""
"""Get a list of statics info from the DB ordered by number of lines desc"""
self.ensure_one()
from_date = (
from_date
Expand Down Expand Up @@ -176,9 +175,7 @@ def _build_ordered_level_cumulative_percentage(self):
The ordering is based on the level with the higher percentage first
"""
self.ensure_one()
levels = self.level_ids.sorted(
key=attrgetter("percentage"), reverse=True
)
levels = self.level_ids.sorted(key=attrgetter("percentage"), reverse=True)
cum_percentages = []
previous_percentage = None
for i, level in enumerate(levels):
Expand Down Expand Up @@ -235,7 +232,7 @@ def _sale_stock_data_to_vals(self, sale_stock_data, create=False):

@api.multi
def _compute_abc_classification(self):
to_compute = self.filtered((lambda p: p.profile_type == "sale_stock"))
to_compute = self.filtered(lambda p: p.profile_type == "sale_stock")
remaining = self - to_compute
res = None
if remaining:
Expand All @@ -247,9 +244,7 @@ def _compute_abc_classification(self):
for profile in to_compute:
sale_stock_data_list, total = profile._get_data()
existing_level_ids_to_remove = profile._get_existing_level_ids()
level_percentage = (
profile._build_ordered_level_cumulative_percentage()
)
level_percentage = profile._build_ordered_level_cumulative_percentage()
level, percentage = level_percentage.pop(0)
previous_data = {}
total_products = len(sale_stock_data_list)
Expand All @@ -267,23 +262,18 @@ def _compute_abc_classification(self):
)
# Compute percentages and cumulative percentages for the products
sale_stock_data.percentage = (
(100.0 * sale_stock_data.number_so_lines / total)
if total
else 0.0
(100.0 * sale_stock_data.number_so_lines / total) if total else 0.0
)

sale_stock_data.cumulated_percentage = (
sale_stock_data.percentage
if i == 0
else (
sale_stock_data.percentage
+ previous_data.cumulated_percentage
sale_stock_data.percentage + previous_data.cumulated_percentage
)
)
if float_round(sale_stock_data.cumulated_percentage, 0) > 100:
raise UserError(
_("Cumulative percentage greater than 100.")
)
raise UserError(_("Cumulative percentage greater than 100."))

sale_stock_data.sum_cumulated_percentages = (
sale_stock_data.cumulated_percentage
Expand All @@ -308,9 +298,7 @@ def _compute_abc_classification(self):
sale_stock_data.computed_level = level
if product_abc_classification:
# The line is still significant...
existing_level_ids_to_remove.remove(
product_abc_classification.id
)
existing_level_ids_to_remove.remove(product_abc_classification.id)
if product_abc_classification.level_id != level:
vals = profile._sale_stock_data_to_vals(
sale_stock_data, create=False
Expand All @@ -320,9 +308,7 @@ def _compute_abc_classification(self):
vals = profile._sale_stock_data_to_vals(
sale_stock_data, create=True
)
product_abc_classification = ProductClassification.create(
vals
)
product_abc_classification = ProductClassification.create(vals)
sale_stock_data.total_so_lines = total
sale_stock_data.product_level = product_abc_classification
previous_data = sale_stock_data
Expand All @@ -331,7 +317,7 @@ def _compute_abc_classification(self):
return res

def _log_history(self, sale_stock_data_list):
""" Log collected and computed values into
"""Log collected and computed values into
abc.sale_stock.level.history
"""
Expand All @@ -349,7 +335,7 @@ def _log_history(self, sale_stock_data_list):


class SaleStockData(object):
""" Sale stock collected data
"""Sale stock collected data
This class is used to store all the data collectd and computed for
a abc classification product level. It also provide methods used to bulk
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AbcSaleStockLevelHistory(models.Model):
""" ABC Classification Product Level History
"""ABC Classification Product Level History
This model is used to display the history of values collected and involved
into the computation of the ABC classification level.
Expand Down Expand Up @@ -61,7 +60,10 @@ class AbcSaleStockLevelHistory(models.Model):
ondelete="cascade",
)
warehouse_id = fields.Many2one(
"stock.warehouse", "Warehouse", readonly=False, ondelete="cascade",
"stock.warehouse",
"Warehouse",
readonly=False,
ondelete="cascade",
)
ranking = fields.Integer(
"Ranking",
Expand All @@ -70,10 +72,14 @@ class AbcSaleStockLevelHistory(models.Model):
help="Ranking by number of oder lines",
)
number_so_lines = fields.Integer(
"Number of sale order lines", required=True, readonly=True,
"Number of sale order lines",
required=True,
readonly=True,
)
total_so_lines = fields.Integer(
"Total of sale order lines", required=True, readonly=True,
"Total of sale order lines",
required=True,
readonly=True,
)
percentage = fields.Float(
"Percentage",
Expand Down Expand Up @@ -109,8 +115,7 @@ class AbcSaleStockLevelHistory(models.Model):
"Cumulated percentage of products",
required=True,
readonly=True,
help="Cumulated percentage of total products analyzed with a "
"better ranking",
help="Cumulated percentage of total products analyzed with a " "better ranking",
digits=(7, 4),
group_operator=None,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->

<odoo>

<record model="ir.model.access" id="abc_sale_stock_level_history_access_name"> <!-- TODO acl id -->
<field name="name">abc.sale_stock.level.history access name</field> <!-- TODO acl name -->
<field name="model_id" ref="model_abc_sale_stock_level_history"/>
<record
model="ir.model.access"
id="abc_sale_stock_level_history_access_name"
> <!-- TODO acl id -->
<field
name="name"
>abc.sale_stock.level.history access name</field> <!-- TODO acl name -->
<field name="model_id" ref="model_abc_sale_stock_level_history" />
<!-- TODO review and adapt -->
<field name="group_id" ref="base.group_user"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
<field name="group_id" ref="base.group_user" />
<field name="perm_read" eval="1" />
<field name="perm_create" eval="0" />
<field name="perm_write" eval="0" />
<field name="perm_unlink" eval="0" />
</record>

</odoo>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

Expand Down Expand Up @@ -275,4 +274,3 @@ def test_02(self):
self.assertEqual(len(levels.sale_stock_level_history_ids), 1)
self.stock_profile._compute_abc_classification()
self.assertEqual(len(levels.sale_stock_level_history_ids), 2)

Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->

<odoo>

<record model="ir.ui.view" id="abc_classification_product_level_form_view">
<field name="name">abc.classification.product.level.form (in product_abc_classification_sale_stock)</field>
<field
name="name"
>abc.classification.product.level.form (in product_abc_classification_sale_stock)</field>
<field name="model">abc.classification.product.level</field>
<field name="inherit_id" ref="product_abc_classification_base.abc_classification_product_level_form_view"/>
<field
name="inherit_id"
ref="product_abc_classification_base.abc_classification_product_level_form_view"
/>
<field name="arch" type="xml">
<group name="main_info" position="after">
<group name="sale_stock_history" string="History" attrs="{'invisible': [('profile_type', '!=', 'sale_stock')]}"/>
<group
name="sale_stock_history"
string="History"
attrs="{'invisible': [('profile_type', '!=', 'sale_stock')]}"
/>
<field name="sale_stock_level_history_ids" nolabel="1">
<tree default_order="to_date DESC">
<field name="from_date"/>
<field name="to_date"/>
<field name="computed_level_id"/>
<field name="ranking"/>
<field name="number_so_lines"/>
<field name="total_so_lines"/>
<field name="percentage"/>
<field name="cumulated_percentage"/>
<field name="from_date" />
<field name="to_date" />
<field name="computed_level_id" />
<field name="ranking" />
<field name="number_so_lines" />
<field name="total_so_lines" />
<field name="percentage" />
<field name="cumulated_percentage" />
</tree>
</field>
</group>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="abc_classification_profile_form_view" model="ir.ui.view">
<field name="name">abc.classification.profile.form (in product_abc_classification_sale_stock)</field>
<field
name="name"
>abc.classification.profile.form (in product_abc_classification_sale_stock)</field>
<field name="model">abc.classification.profile</field>
<field name="inherit_id" ref="product_abc_classification_base.abc_classification_profile_form_view"/>
<field
name="inherit_id"
ref="product_abc_classification_base.abc_classification_profile_form_view"
/>
<field name="arch" type="xml">
<field name="profile_type" position="after">
<field name="warehouse_id" attrs="{'invisible': [('profile_type', '!=', 'sale_stock')], 'required': [('profile_type', '=', 'sale_stock')]}"/>
<field
name="warehouse_id"
attrs="{'invisible': [('profile_type', '!=', 'sale_stock')], 'required': [('profile_type', '=', 'sale_stock')]}"
/>
</field>
</field>
</record>
Expand Down
Loading

0 comments on commit 9948e15

Please sign in to comment.