diff --git a/contract.py b/contract.py index 8b16d2f..e5e8a73 100644 --- a/contract.py +++ b/contract.py @@ -42,8 +42,8 @@ class AgronomicsContract(Workflow, ModelSQL, ModelView): fields.Date('Start Date'), 'on_change_with_start_date') end_date = fields.Function( fields.Date('End Date'), 'on_change_with_end_date') - producer = fields.Many2One( - 'party.party', "Producer", states=_STATES, depends=_DEPENDS, + party = fields.Many2One( + 'party.party', "Party", states=_STATES, depends=_DEPENDS, required=True) price_list_types = fields.One2Many( 'agronomics.contract-product.price_list.type-product.price_list', @@ -89,7 +89,7 @@ class AgronomicsContract(Workflow, ModelSQL, ModelView): return 'draft' def get_rec_name(self, name): - ret = self.producer.rec_name + ret = self.party and self.party.rec_name or '' if self.start_date: ret += ' - %s' % (self.start_date) return ret @@ -153,9 +153,9 @@ class AgronomicsContractLine(ModelSQL, ModelView): ondelete='CASCADE') parcel = fields.Many2One('agronomics.parcel', "Parcel", domain=[ - ('producer', '=', Eval('_parent_contract.producer')), + ('producer', '=', Eval('_parent_contract.party')), ('crop', '=', Eval('_parent_contract.crop')) - ], depends=['contract']) + ]) product = fields.Function( fields.Many2One('product.template', "Product"), 'on_change_with_product') diff --git a/message.xml b/message.xml index e005b29..2a70203 100644 --- a/message.xml +++ b/message.xml @@ -48,5 +48,10 @@ this repository contains the full copyright notices and license terms. --> The plantation "%(plantation)s" in the weighing "%(weighing)s" has no parcel of the weighing's crop. + + The weighing "%(weighing)s" has the mark "Table" but has selected denomination of origin. + + + diff --git a/plot.py b/plot.py index b31772d..661fb6d 100644 --- a/plot.py +++ b/plot.py @@ -1,5 +1,6 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from sql.aggregate import Min, Sum from trytond.model import fields, ModelSQL, ModelView from trytond.pool import Pool @@ -68,12 +69,100 @@ class Plantation(ModelSQL, ModelView): parcels = fields.One2Many('agronomics.parcel', 'plantation', "Parcel") plantation_year = fields.Integer("Plantation Year") plantation_owner = fields.Many2One('party.party', "Plantation Owner") + varieties = fields.Function(fields.Char('Varieties'), 'get_varieties', + searcher='search_varieties') + do = fields.Function(fields.Char('DO'), 'get_do', searcher='search_do') + remaining_quantity = fields.Function( + fields.Float("Remainig Quantity", digits=(16, 2)), + 'get_remaining_quantity', searcher='search_remaining_quantity') def get_rec_name(self, name): if self.code: return self.code return self.name + def get_do(self, name): + do = [] + for y in self.parcels: + do += [x.name for x in y.denomination_origin] + return ",".join(list(set(do))) + + def get_varieties(self, name): + if not self.parcels: + return [] + varieties = [y.variety.name for y in self.parcerls if y.variety] + return ",".join(list(set(varieties))) + + def get_remaining_quantity(self, name): + return sum([y.remaining_quantity or 0 for y in self.parcels]) + + @classmethod + def search_varieties(cls, name, clause): + pool = Pool() + Variety = pool.get('product.taxon') + Parcel = pool.get('agronomics.parcel') + + variety = Variety.__table__() + parcel = Parcel.__table__() + Operator = fields.SQL_OPERATORS[clause[1]] + query = parcel.join(variety, condition=parcel.variety == variety.id) + _, operator, value = clause + query = query.select(parcel.plantation) + query.where = Operator(variety.name, value) + return [('id', 'in', query)] + + @classmethod + def search_do(cls, name, clause): + pool = Pool() + DO = pool.get('agronomics.denomination_of_origin') + PARCEL_DO = pool.get('agronomics.parcel-agronomics.do') + Parcel = pool.get('agronomics.parcel') + + do = DO.__table__() + parcel = Parcel.__table__() + parcel_do = PARCEL_DO.__table__() + Operator = fields.SQL_OPERATORS[clause[1]] + query = parcel.join(parcel_do, condition=parcel.id == parcel_do.parcel) + query = query.join(do, condition=parcel_do.do==parcel_do.do) + print(query) + _, operator, value = clause + query = query.select(parcel.plantation) + query.where = Operator(do.name, value) + return [('id', 'in', query)] + + @classmethod + def search_remaining_quantity(cls, name, clause): + pool = Pool() + DO = pool.get('agronomics.denomination_of_origin') + PARCEL_DO = pool.get('agronomics.parcel-agronomics.do') + Parcel = pool.get('agronomics.parcel') + Weighing = pool.get('agronomics.weighing-agronomics.parcel') + MaxProductionAllowed = pool.get('agronomics.max.production.allowed') + + do = DO.__table__() + parcel = Parcel.__table__() + parcel_do = PARCEL_DO.__table__() + weighing = Weighing.__table__() + max_production = MaxProductionAllowed.__table__() + + _, operator, value = clause + Operator = fields.SQL_OPERATORS[operator] + + join1 = weighing.join(parcel, type_='RIGHT', + condition=((weighing.parcel==parcel.id) & + (weighing.table != True))) + join2 = join1.join(parcel_do, type_= 'LEFT', + condition=parcel.id==parcel_do.parcel) + join3 = join2.join(max_production, type_='LEFT', + condition=((max_production.crop == parcel.crop) & + (max_production.variety == parcel.variety))) + query2 = join3.select(parcel.plantation, + Sum(max_production.max_production*parcel.surface - + weighing.netweight).as_('remaining_quantity'), + group_by=parcel.plantation) + query = query2.select(query2.plantation) + query.where = Operator(query2.remaining_quantity, value) + return [('id' , 'in', query)] class Ecological(ModelSQL, ModelView): "Ecological" @@ -136,7 +225,7 @@ class Parcel(ModelSQL, ModelView): )*self.surface, 2) def get_purchased_quantity(self, name): - return sum([w.netweight for w in self.weighings if not w.table]) + return sum([(w.netweight or 0) for w in self.weighings if not w.table]) def get_remaining_quantity(self, name): return (self.max_production or 0) - (self.purchased_quantity or 0) diff --git a/view/contract_form.xml b/view/contract_form.xml index e5bb057..5b7f31a 100644 --- a/view/contract_form.xml +++ b/view/contract_form.xml @@ -5,8 +5,8 @@ this repository contains the full copyright notices and license terms. -->