Browse Source

[REF] blacken stock coverage

pull/139/head
robin.keunen 5 years ago
parent
commit
2de207e51a
  1. 26
      stock_coverage/__openerp__.py
  2. 73
      stock_coverage/models/product_template.py
  3. 75
      stock_coverage/tests/test_stock_coverage.py

26
stock_coverage/__openerp__.py

@ -1,24 +1,16 @@
# -*- encoding: utf-8 -*-
{
'name': 'Product - Stock Coverage',
'version': '9.0.1',
'category': 'Product',
'description': """
"name": "Product - Stock Coverage",
"version": "9.0.1",
"category": "Product",
"description": """
Shows figures in the product form related to stock coverage
There are settings in Inventory/settings to define the calculation range and
the display range.
""",
'author': 'coop it easy',
'website': 'coopiteasy.be',
'license': 'AGPL-3',
'depends': [
'product',
'purchase',
'point_of_sale',
'stock'
],
'data': [
'views/product_template_view.xml',
'data/cron.xml',
],
"author": "coop it easy",
"website": "coopiteasy.be",
"license": "AGPL-3",
"depends": ["product", "purchase", "point_of_sale", "stock"],
"data": ["views/product_template_view.xml", "data/cron.xml"],
}

73
stock_coverage/models/product_template.py

@ -7,41 +7,38 @@ class ProductTemplate(models.Model):
_inherit = "product.template"
consumption_calculation_method = fields.Selection(
selection=[('sales_history', 'Sales History')],
string='Consumption Calculation Method',
default='sales_history',
)
calculation_range = fields.Integer(
'Calculation range (days)',
default=14,
selection=[("sales_history", "Sales History")],
string="Consumption Calculation Method",
default="sales_history",
)
calculation_range = fields.Integer("Calculation range (days)", default=14)
average_consumption = fields.Float(
string='Average Consumption',
compute='_compute_average_daily_consumption',
string="Average Consumption",
compute="_compute_average_daily_consumption",
readonly=True,
digits=(100, 2),
)
total_consumption = fields.Float(
string='Total Consumption',
string="Total Consumption",
default=0,
compute='_compute_total_consumption',
compute="_compute_total_consumption",
store=True,
readonly=True,
digits=(100, 2),
)
estimated_stock_coverage = fields.Float(
string='Estimated Stock Coverage (days)',
compute='_compute_estimated_stock_coverage',
string="Estimated Stock Coverage (days)",
compute="_compute_estimated_stock_coverage",
default=0,
digits=(100, 2),
readonly=True,
)
@api.multi
@api.depends('total_consumption')
@api.depends("total_consumption")
def _compute_average_daily_consumption(self):
for template in self:
if template.calculation_range > 0:
@ -53,35 +50,41 @@ class ProductTemplate(models.Model):
return True
@api.multi
@api.depends('calculation_range')
@api.depends("calculation_range")
def _compute_total_consumption(self):
for template in self:
products = (
self.env['product.product']
.search([('product_tmpl_id', '=', template.id)]))
products = self.env["product.product"].search(
[("product_tmpl_id", "=", template.id)]
)
today = dt.date.today()
pol_date_limit = (
today - dt.timedelta(days=template.calculation_range))
order_lines = (
self.env['pos.order.line']
.search([
('product_id', 'in', products.ids),
('create_date', '>', fields.Datetime.to_string(pol_date_limit)) # noqa
])
pol_date_limit = today - dt.timedelta(
days=template.calculation_range
)
order_lines = self.env["pos.order.line"].search(
[
("product_id", "in", products.ids),
(
"create_date",
">",
fields.Datetime.to_string(pol_date_limit),
), # noqa
]
)
if order_lines:
order_lines = order_lines.filtered(
lambda ol: ol.order_id.state in ['done', 'invoiced', 'paid']) # noqa
template.total_consumption = sum(order_lines.mapped('qty'))
lambda ol: ol.order_id.state
in ["done", "invoiced", "paid"]
) # noqa
template.total_consumption = sum(order_lines.mapped("qty"))
else:
template.total_consumption = 0
return True
@api.multi
@api.depends('total_consumption')
@api.depends("total_consumption")
def _compute_estimated_stock_coverage(self):
for product_template in self:
qty = product_template.qty_available
@ -96,10 +99,7 @@ class ProductTemplate(models.Model):
@api.model
def _batch_compute_total_consumption(self):
products = (
self.env['product.template']
.search([('active', '=', True)])
)
products = self.env["product.template"].search([("active", "=", True)])
query = """
select
@ -121,5 +121,6 @@ class ProductTemplate(models.Model):
results = {pid: qty for pid, qty in self.env.cr.fetchall()}
for product in products:
product.total_consumption = results.get(product.id,
product.total_consumption)
product.total_consumption = results.get(
product.id, product.total_consumption
)

75
stock_coverage/tests/test_stock_coverage.py

@ -4,52 +4,61 @@ import datetime as dt
from openerp.tests.common import TransactionCase
_datetimes = map(
lambda d: d.strftime('%Y-%m-%d %H:%M:%S'),
(dt.datetime.now() - dt.timedelta(days=d) for d in range(0, 24, 2)))
_quantities = [0.64, 6.45, 9.65, 1.76, 9.14, 3.99,
6.92, 2.25, 6.91, 1.44, 6.52, 1.44]
lambda d: d.strftime("%Y-%m-%d %H:%M:%S"),
(dt.datetime.now() - dt.timedelta(days=d) for d in range(0, 24, 2)),
)
_quantities = [
0.64,
6.45,
9.65,
1.76,
9.14,
3.99,
6.92,
2.25,
6.91,
1.44,
6.52,
1.44,
]
class TestProductTemplate(TransactionCase):
def setUp(self, *args, **kwargs):
result = super(TestProductTemplate, self).setUp(*args, **kwargs)
test_product_template = (
self.env['product.template']
.create({'name': 'test product template',
'calculation_range': 14,
'consumption_calculation_method': 'sales_history',
'product_template_id': 0,
})
test_product_template = self.env["product.template"].create(
{
"name": "test product template",
"calculation_range": 14,
"consumption_calculation_method": "sales_history",
"product_template_id": 0,
}
)
pid = (
self.env['product.product']
.search([('product_tmpl_id', '=', test_product_template.id)])
self.env["product.product"]
.search([("product_tmpl_id", "=", test_product_template.id)])
.ids
).pop()
for date, qty in zip(_datetimes, _quantities):
(self.env['pos.order.line']
.create({'create_date': date,
'qty': qty,
'product_id': pid,
})
(
self.env["pos.order.line"].create(
{"create_date": date, "qty": qty, "product_id": pid}
)
)
def _product_available(*args, **kwargs):
products = (
self.env['product.product']
.search([
('product_tmpl_id', '=', test_product_template.id)])
products = self.env["product.product"].search(
[("product_tmpl_id", "=", test_product_template.id)]
)
mock_data = {
'qty_available': 53.2,
'incoming_qty': 14,
'outgoing_qty': 4.1,
'virtual_available': 53.2 + 14 - 4.1,
"qty_available": 53.2,
"incoming_qty": 14,
"outgoing_qty": 4.1,
"virtual_available": 53.2 + 14 - 4.1,
}
return {pid: mock_data for pid in products.ids}
@ -66,13 +75,13 @@ class TestProductTemplate(TransactionCase):
def test_create(self):
"""Create a simple product template"""
Template = self.env['product.template']
product = Template.create({'name': 'Test create product'})
self.assertEqual(product.name, 'Test create product')
Template = self.env["product.template"]
product = Template.create({"name": "Test create product"})
self.assertEqual(product.name, "Test create product")
def test_compute_average_daily_consumption(self):
"""Test computed field average_daily_consumption"""
ProductTemplate = self.env['product.template']
ProductTemplate = self.env["product.template"]
product_template = ProductTemplate.browse(self.product_template_id)
computed_value = product_template.average_consumption
@ -81,7 +90,7 @@ class TestProductTemplate(TransactionCase):
def test_compute_total_consumption(self):
"""Test total consumption was computed in setup"""
ProductTemplate = self.env['product.template']
ProductTemplate = self.env["product.template"]
product_template = ProductTemplate.browse(self.product_template_id)
computed_value = product_template.total_consumption
expected_value = 57.11

Loading…
Cancel
Save