Product wine aging history

This commit is contained in:
Raimon Esteve 2022-05-05 19:01:29 +02:00
parent 90c4d9ad88
commit fd16f62279
9 changed files with 117 additions and 13 deletions

View File

@ -20,6 +20,7 @@ def register():
contract.AgronomicsContract,
contract.AgronomicsContractLine,
history.WineAgingHistory,
history.ProductWineAgingHistory,
party.Party,
plot.Enclosure,
plot.Crop,

View File

@ -1,8 +1,12 @@
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from sql import Literal, Null
from sql.aggregate import Min, Sum
from sql.functions import CurrentTimestamp
from trytond.pool import Pool
from trytond.model import ModelSQL, ModelView, fields
from trytond.pyson import Eval
from trytond.transaction import Transaction
class WineAgingHistory(ModelSQL, ModelView):
@ -14,7 +18,7 @@ class WineAgingHistory(ModelSQL, ModelView):
readonly=True)
product = fields.Many2One('product.product', "Product", required=True,
readonly=True)
material = fields.Selection('get_materials', "Material",
material = fields.Many2One('stock.location.material', "Material",
readonly=True)
date_start = fields.Date("Date Start", required=True, readonly=True)
date_end = fields.Date("Date End", readonly=True,
@ -28,15 +32,6 @@ class WineAgingHistory(ModelSQL, ModelView):
duration = fields.Function(fields.Integer("Duration"),
'get_duration')
@classmethod
def get_materials(field_name):
pool = Pool()
LocationMaterial = pool.get('stock.location.material')
materials = [(None, "")]
for material in LocationMaterial.search([]):
materials.append((material.name, material.name))
return materials
@classmethod
def get_duration(cls, records, name):
res = dict((x.id, None) for x in records)
@ -48,3 +43,39 @@ class WineAgingHistory(ModelSQL, ModelView):
@classmethod
def delete(cls, records):
pass
class ProductWineAgingHistory(ModelSQL, ModelView):
"Product Wine Aging History"
__name__ = 'product.wine.wine_aging.history'
product = fields.Many2One('product.product', "Product")
material = fields.Many2One('stock.location.material', "Material")
duration = fields.Integer("Duration")
@classmethod
def table_query(cls):
pool = Pool()
WineAgingHistory = pool.get('wine.wine_aging.history')
wine_aging_history = WineAgingHistory.__table__()
product_id = Transaction().context.get('product')
sql_where = None
if product_id:
sql_where = (wine_aging_history.product == product_id)
query = wine_aging_history.select(
(Min(wine_aging_history.id * 2)).as_('id'),
Literal(0).as_('create_uid'),
CurrentTimestamp().as_('create_date'),
cls.write_uid.sql_cast(Literal(Null)).as_('write_uid'),
cls.write_date.sql_cast(Literal(Null)).as_('write_date'),
wine_aging_history.product.as_('product'),
wine_aging_history.material.as_('material'),
Sum(wine_aging_history.date_end - wine_aging_history.date_start).as_('duration'),
group_by=[wine_aging_history.product, wine_aging_history.material])
if sql_where:
query.where = sql_where
return query

View File

@ -49,5 +49,52 @@ this repository contains the full copyright notices and license terms. -->
<field name="menu" ref="menu_wine_aging_history"/>
<field name="group" ref="group_agronomics_admin"/>
</record>
<!-- product wine aging history -->
<record model="ir.ui.view" id="product_wine_aging_history_form_view">
<field name="model">product.wine.wine_aging.history</field>
<field name="type">form</field>
<field name="name">product_wine_aging_history_form</field>
</record>
<record model="ir.ui.view" id="product_wine_aging_history_list_view">
<field name="model">product.wine.wine_aging.history</field>
<field name="type">tree</field>
<field name="name">product_wine_aging_history_list</field>
</record>
<record model="ir.action.act_window" id="act_product_wine_aging_history">
<field name="name">Wine Aging History</field>
<field name="res_model">product.wine.wine_aging.history</field>
</record>
<record model="ir.action.act_window.view" id="act_product_wine_aging_history_view1">
<field name="sequence" eval="10"/>
<field name="view" ref="product_wine_aging_history_list_view"/>
<field name="act_window" ref="act_product_wine_aging_history"/>
</record>
<record model="ir.action.act_window.view" id="act_product_wine_aging_history_view2">
<field name="sequence" eval="20"/>
<field name="view" ref="product_wine_aging_history_form_view"/>
<field name="act_window" ref="act_product_wine_aging_history"/>
</record>
<record model="ir.model.access" id="access_product_wine_aging_history">
<field name="model" search="[('model', '=', 'product.wine.wine_aging.history')]"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="False"/>
<field name="perm_create" eval="False"/>
<field name="perm_delete" eval="False"/>
</record>
<menuitem name="Product History" sequence="0" id="menu_product_wine_aging_history"
parent="menu_wine_aging_history" action="act_product_wine_aging_history"/>
<record model="ir.ui.menu-res.group" id="menu_product_wine_aging_history_group_agronomics">
<field name="menu" ref="menu_product_wine_aging_history"/>
<field name="group" ref="group_agronomics"/>
</record>
<record model="ir.ui.menu-res.group" id="menu_product_wine_aging_history_group_agronomics_admin">
<field name="menu" ref="menu_product_wine_aging_history"/>
<field name="group" ref="group_agronomics_admin"/>
</record>
</data>
</tryton>

View File

@ -25,6 +25,7 @@ class Move(metaclass=PoolMeta):
with Transaction().set_context(context):
location_quantity = sum(Product.products_by_location(
[move.to_location.id]).values())
if (location_quantity > move.to_location.max_capacity):
if (move.to_location.max_capacity
and (location_quantity > move.to_location.max_capacity)):
raise UserError(gettext(
'agronomics.msg_move_amount_exceed'))

View File

@ -115,6 +115,11 @@ class Product(WineMixin, metaclass=PoolMeta):
quality_tests = fields.One2Many('quality.test', 'document', 'Quality Tests')
quality_samples = fields.Many2Many('product.product-quality.sample',
'product', 'sample', 'Quality Samples')
wine_aging = fields.One2Many('product.wine.wine_aging.history', 'product',
"Wine Aging", readonly=True,
context={
'product': Eval('id'),
}, depends=['id'])
@classmethod
def deactivate_no_stock_variants_cron(cls):

View File

@ -459,6 +459,9 @@ class Production(metaclass=PoolMeta):
pool = Pool()
Date = pool.get('ir.date')
WineAgingHistory = pool.get('wine.wine_aging.history')
LocationMaterial = pool.get('stock.location.material')
materials = dict((x.name, x) for x in LocationMaterial.search([]))
today = Date.today()
histories = WineAgingHistory.search([
@ -470,9 +473,9 @@ class Production(metaclass=PoolMeta):
new_histories += WineAgingHistory.create([{
'production': output.production_output,
'location': output.to_location,
'material': output.to_location.material,
'material': materials.get(output.to_location.material),
'product': output.product,
'date_start': today,
'date_start': output.production_output.effective_date,
'date_end': None
}])
if histories:

View File

@ -182,5 +182,8 @@
<separator name="wine_quality_comment" colspan="6"/>
<field name="wine_quality_comment" widget="richtext" toolbar="0" yexpand="1" yfill="1" colspan="6"/>
</page>
<page string="Wine Aging" id="wine-aging" col="6">
<field name="wine_aging" colspan="6"/>
</page>
</xpath>
</data>

View File

@ -0,0 +1,8 @@
<form>
<label name="product"/>
<field name="product"/>
<label name="material"/>
<field name="material"/>
<label name="duration"/>
<field name="duration"/>
</form>

View File

@ -0,0 +1,5 @@
<tree>
<field name="product"/>
<field name="material"/>
<field name="duration"/>
</tree>