ADD FIELDS TASK AND ADVANCES IN FARMING CROP

This commit is contained in:
Elvis 2022-09-02 15:15:15 -05:00
parent b4894b9866
commit 60124c5524
11 changed files with 252 additions and 53 deletions

View File

@ -26,6 +26,7 @@ def register():
crop.Activity,
crop.FarmingVariety,
crop.FarmingVarietyActivity,
crop.FarmingActivitySupply,
crop.FarmingActivityShipmentInternal,
crop.FarmingStage,
crop.FarmingCrop,

235
crop.py
View File

@ -1,14 +1,13 @@
# 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 calendar import week
from dataclasses import field
from decimal import Decimal
from datetime import date, datetime, timedelta
from pickle import APPEND
from genericpath import exists
from itertools import product
import copy
from trytond.model import Workflow, ModelView, ModelSQL, fields
from trytond.pyson import Eval, If, In, Get
from trytond.pyson import Eval, If, Bool, In, Get
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.exceptions import UserError
@ -53,12 +52,29 @@ class FarmingVariety(ModelSQL, ModelView):
return self.product.template.name
class FarmingActivitySupply(ModelSQL, ModelView):
'Farming Activity Supply'
__name__ = 'farming.variety.activity.supply'
activity = fields.Many2One('farming.variety.activity', 'Activity')
product = fields.Many2One("product.product", "Product", required=True,
select=True)
unity = fields.Function(fields.Char('Unity'), 'get_unity')
quantity = fields.Float('Quantity', required=True)
def get_unity(self, name=None):
unity = self.product.default_uom.name
return unity
class FarmingVarietyActivity(ModelSQL, ModelView):
'Farming Variety Activity'
__name__ = 'farming.variety.activity'
variety = fields.Many2One('farming.variety', 'Variety', required=True)
sequence = fields.Integer('Sequence', required=True)
sequence = fields.Integer('Sequence', required=True )
kind = fields.Many2One('farming.activity.kind', 'Kind', required=True)
work_time = fields.Numeric('Work Time')
uom = fields.Many2One('product.uom', 'Unity')
supplies = fields.One2Many('farming.variety.activity.supply', 'activity', 'Supply')
time_of_realization = fields.Integer('Time Of Realization', required=True,
help='In weeks')
@ -102,6 +118,8 @@ class FarmingCrop(Workflow, ModelSQL, ModelView):
lots = fields.One2Many('farming.crop.lot', 'crop', 'Lots', states=STATES)
stages = fields.One2Many('farming.crop.stage', 'crop', 'Stages',
states=STATES)
activities = fields.One2Many('farming.crop.activities', 'crop', 'Activities', states=STATES)
# seed = fields.Char('Seed', states=STATES)
seed = fields.Many2One('farming.seed', 'Seed', required=True)
production_rate = fields.Float('Production Rate')
@ -142,6 +160,11 @@ class FarmingCrop(Workflow, ModelSQL, ModelView):
'cancel': {
'invisible': Eval('state') == 'finished',
},
'load_configuration': {
'invisible': (Eval('lines', [])
| (Eval('state') != 'draft')),
'depends': ['state'],
},
'production': {
'invisible': Eval('state').in_(['cancelled', 'finished']),
},
@ -150,6 +173,10 @@ class FarmingCrop(Workflow, ModelSQL, ModelView):
},
})
@staticmethod
def default_start_date():
return date.today()
@staticmethod
def default_company():
return Transaction().context.get('company') or False
@ -183,6 +210,117 @@ class FarmingCrop(Workflow, ModelSQL, ModelView):
for record in records:
record.set_number()
@classmethod
def clear_bed_lines(cls, crops):
Line = Pool().get('farming.crop.lot.bed')
lines_to_delete = []
for crop in crops:
for lot in crop.lots:
for bed in lot.beds:
lines_to_delete.append(bed)
Line.delete(lines_to_delete)
@classmethod
def create_beds_lines(cls, crops):
Line = Pool().get('farming.crop.lot.bed')
lines = []
for crop in crops:
for lot in crop.lots:
for bed in lot.lot.beds:
line = Line(
lot = lot.id,
bed = bed.id,
quantity_plants = bed.quantity,
unit = 20,
production_rate = 10,
)
lines.append(line)
Line.save(lines)
@classmethod
def create_lots_lines(cls, crops):
Line = Pool().get('farming.crop.lot')
lines = []
cls.clear_bed_lines(crops)
cls.clear_lots_lines(crops)
for crop in crops:
for lot in crop.location.lots:
line = Line(
crop = crop.id,
lot = lot.id,
cycle = 0,
beds = [],
activities = [],
total_plants = lot.quantity
)
lines.append(line)
Line.save(lines)
cls.create_beds_lines(crops)
@classmethod
def clear_lots_lines(cls, crops):
Line = Pool().get('farming.crop.lot')
lines_to_delete = []
for crop in crops:
for lot in crop.lots:
lines_to_delete.append(lot)
Line.delete(lines_to_delete)
@classmethod
def create_stages_lines(cls, crops):
Line = Pool().get('farming.crop.stage')
lines = []
cls.clear_stages_lines(crops)
for crop in crops:
for stage in crop.variety.cycles:
line = Line(
crop = crop.id,
stage = stage.stage.id,
activity_time = stage.start_time,
production_rate = stage.production_rate,
)
lines.append(line)
Line.save(lines)
@classmethod
def create_activities_lines(cls, crops):
Line = Pool().get('farming.crop.stage')
lines = []
cls.clear_stages_lines(crops)
for crop in crops:
for stage in crop.variety.cycles:
line = Line(
crop = crop.id,
stage = stage.stage.id,
activity_time = stage.start_time,
production_rate = stage.production_rate,
)
lines.append(line)
Line.save(lines)
@classmethod
def clear_stages_lines(cls, crops):
Line = Pool().get('farming.crop.stage')
lines_to_delete = []
for crop in crops:
for stage in crop.stages:
lines_to_delete.append(stage)
print(stage.stage)
Line.delete(lines_to_delete)
@classmethod
@ModelView.button
def load_configuration(cls, crops):
cls.clear_stages_lines(crops)
cls.clear_bed_lines(crops)
cls.clear_lots_lines(crops)
cls.create_stages_lines(crops)
cls.create_lots_lines(crops)
cls.create_beds_lines(crops)
def set_number(self):
Config = Pool().get('farming.configuration')
config = Config.get_config()
@ -231,60 +369,76 @@ class FarmingCrop(Workflow, ModelSQL, ModelView):
if self.variety and self.variety.production_uom:
self.production_uom = self.variety.production_uom.id
self.production_rate = self.variety.production_rate
if self.variety.standard_cycle:
self.end_date = date.today() + timedelta(weeks=self.variety.standard_cycle)
class FarmingCropLot(ModelSQL, ModelView):
'Farming Crop Lot'
__name__ = 'farming.crop.lot'
_rec_name = 'number'
crop = fields.Many2One('farming.crop', 'Crop', required=True)
crop = fields.Many2One('farming.crop', 'Crop', ondelete='CASCADE', required=True)
lot = fields.Many2One('farming.location.lot', 'Lot', required=True)
cycle = fields.Float('Cycle', digits=(16, 2), help="In weeks")
#production_rate = fields.Float('Production Rate')
beds = fields.One2Many('farming.crop.lot.bed', 'lot', 'Beds')
activities = fields.One2Many('farming.activity', 'lot', 'Activities')
total_plants = fields.Integer('Total Plants')
total_plants = fields.Integer('Total Plants', states = {'readonly': Bool(Eval('beds'))}, depends=['beds'])
#def get_production_rate(self, name=None):
# if self.total_plants and self.crop:
# production_rate = self.crop.variety.production_rate
# print(production_rate*self.total_plants)
# return production_rate*self.total_plants
#@fields.depends('beds', 'total_plants')
#def on_change_beds(self, name=None):
#res = []
#for bed in self.beds:
# print(bed)
# res.append(bed.quantity_plants)
#self.total_plants = sum(res)
#res = []
#_append = res.append
#for bed in self.beds:
# if bed.quantity_plants:
# _append(bed.quantity_plants)
#return sum(res)
#@staticmethod
#def default_beds():
# pool = Pool()
# Bed = pool.get('farming.crop.lot.bed')
# print(Bed.bed)
# beds = Bed.search([
# ('beds', '!=', ''),
# ])
# return beds
@fields.depends('beds')
def on_change_with_total_plants(self, name = None):
quantity = 0
if self.beds:
for bed in self.beds:
if bed.quantity_plants:
quantity += bed.quantity_plants
return quantity
class FarmingCropLotBed(ModelSQL, ModelView):
'Crop Lot Bed'
__name__ = 'farming.crop.lot.bed'
_rec_name = 'code'
lot = fields.Many2One('farming.crop.lot', 'Lot', required=True)
bed = fields.Many2One('farming.location.lot.bed', 'Bed', required=True)
lot = fields.Many2One('farming.crop.lot', 'Lot', ondelete='CASCADE', required=True)
bed = fields.Many2One('farming.location.lot.bed', 'Bed', ondelete='CASCADE', required=True)
quantity_plants = fields.Integer('Quantity Plants')
unit = fields.Many2One('product.uom', 'Unit')
unit = fields.Many2One('product.uom', 'Unit', ondelete='CASCADE')
production_rate = fields.Float('Production Rate', digits=(16, 2),
help="In the unit of measure")
#class CropActivity(Workflow, ModelSQL, ModelView):
# 'Crop Activity'
# __name__ = 'crop.activity'
# sequence = fields.Char('Sequence', states=STATES)
# start_date = fields.Date('Start Date', states=STATES, required=True)
# work_time = fields.Numeric('Work Time', digits=(16, 2))
# supplies = fields.One2Many('crop.activity.supply', 'activity', 'Supply')
# #notes = fields.Text('Notes', states=STATES)
# # works = fields.One2Many('farming.crop.work', 'activity', 'Works',
# # states=STATES)
# #state = fields.Selection([
# # ('draft', 'Draft'),
# # ('execution', 'Execution'),
# # ('done', 'Done'),
# # ('cancelled', 'Cancelled'),
# # ], 'State', readonly=True, required=True)
# #amount = fields.Function(fields.Numeric('Amount', digits=(16, 2)),
# # 'get_amount')
#
#
#class CropActivitySupply(ModelSQL, ModelView):
# 'Crop Activity Supply'
# __name__ = 'crop.activity.supply'
# activity = fields.Many2One('crop.activity', 'Activity')
# product = fields.Many2One("product.product", "Product", required=True,
# select=True)
# unity = fields.Function(fields.Char('Unity'), 'get_unity')
# quantity = fields.Float('Quantity', required=True)
# cost_planned = fields.Numeric('Cost_planned', readonly=True)
# cost_real = fields.Numeric('Cost real')
#
# def get_unity(self, name=None):
# unity = self.product.default_uom.name
# return unity
class Activity(Workflow, ModelSQL, ModelView):
'Activity'
@ -461,12 +615,13 @@ class CropForecastReport(Report):
for crop in crops:
stages_cycle = copy.deepcopy(stages)
production_rate = crop.variety.production_rate
records[crop] = []
quantity_produced = crop.quantity_produced
start_date = crop.start_date
print(crop)
for stage in crop.stages:
production_rate = stage.production_rate
#production_rate = stage.production_rate
if production_rate:
production_date = start_date + timedelta(days=stage.activity_time * 7)
production = (production_rate / 100) * quantity_produced

View File

@ -63,6 +63,12 @@ this repository contains the full copyright notices and license terms. -->
<menuitem parent="menu_farming_configuration" sequence="2"
action="act_farming_variety_tree" id="menu_farming_variety_tree"/>
<record model="ir.ui.view" id="farming_variety_activity_supply_view_tree">
<field name="model">farming.variety.activity.supply</field>
<field name="type">tree</field>
<field name="name">farming_activity_supply_tree</field>
</record>
<record model="ir.ui.view" id="farming_variety_activity_view_tree">
<field name="model">farming.variety.activity</field>
<field name="type">tree</field>

View File

@ -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 trytond.pyson import Eval, Bool, If
from trytond.model import ModelView, ModelSQL, fields
@ -8,7 +9,9 @@ class FarmingLocationLotBed(ModelSQL, ModelView):
__name__ = 'farming.location.lot.bed'
_rec_name = 'number'
number = fields.Char('Number', required=True)
lot = fields.Many2One('farming.location.lot', 'Lot', required=True)
quantity = fields.Integer('Quantity')
lot = fields.Many2One('farming.location.lot', 'Lot', ondelete='CASCADE', required=True)
class FarmingLocationLot(ModelSQL, ModelView):
@ -17,8 +20,18 @@ class FarmingLocationLot(ModelSQL, ModelView):
_rec_name = 'number'
number = fields.Char('Number', required=True)
location = fields.Many2One('farming.location', 'Location', required=True)
quantity = fields.Integer('Quantity', states = {'readonly': Bool(Eval('beds'))}, depends=['beds'])
beds = fields.One2Many('farming.location.lot.bed', 'lot', 'Beds')
@fields.depends('beds')
def on_change_with_quantity(self, name = None):
quantity = 0
if self.beds:
for bed in self.beds:
if bed.quantity:
quantity += bed.quantity
return quantity
class FarmingLocation(ModelSQL, ModelView):
"Farming Location"

View File

@ -135,10 +135,8 @@ class ProductionTask(ModelView, ModelSQL):
performance = fields.Function(fields.Float('Performance', digits=(4, 2)),
'get_performance')
production = fields.Many2One('production', 'Production', states=STATES)
customer= fields.Function(fields.Char('Customer'),
'get_customer')
reference = fields.Function(fields.Char('Reference'),
'get_reference')
customer = fields.Many2One('party.party','Customer')
reference = fields.Many2One('product.product', 'Product', domain=[('type', '=', 'goods')])
units = fields.Integer('Units')
uom = fields.Many2One('product.uom', 'Uom')
factor = fields.Integer('Factor')
@ -197,13 +195,15 @@ class ProductionTask(ModelView, ModelSQL):
if self.start_time and self.goal:
return self.start_time + timedelta(minutes=self.goal)
def get_customer(self, name= None):
@fields.depends('production')
def on_change_with_customer(self, name= None):
if self.production:
return self.production.customer.name
return self.production.customer.id
def get_reference(self, name= None):
@fields.depends('production')
def on_change_with_reference(self, name= None):
if self.production:
return self.production.product.name
return self.production.product.id
@fields.depends('operation', 'quantity')

View File

@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tree editable="1" expand="1">
<field name="product"/>
<field name="quantity"/>
<field name="unity"/>
</tree>

View File

@ -23,6 +23,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="quantity_produced"/>
<label name="quantity_produced_planned"/>
<field name="quantity_produced_planned"/>
<button name="load_configuration" expand="6" string="Configuration" icon="tryton-launch"/>
</group>
<notebook colspan="4">
<page string="Lots" col="4" id="lots">
@ -34,6 +35,8 @@ this repository contains the full copyright notices and license terms. -->
<!-- <page string="Activities" col="4" id="activities">
<field name="activities" colspan="4"/>
</page> -->
<page string="Activities" col="2" id="activities">
</page>
<page string="Indicators" col="2" id="indicators">
<label name="production_time"/>
<field name="production_time"/>

View File

@ -4,4 +4,6 @@ this repository contains the full copyright notices and license terms. -->
<form>
<label name="number"/>
<field name="number"/>
<label name="quantity"/>
<field name="quantity"/>
</form>

View File

@ -4,5 +4,7 @@ this repository contains the full copyright notices and license terms. -->
<form>
<label name="number"/>
<field name="number"/>
<label name="quantity"/>
<field name="quantity"/>
<field name="beds" colspan="4"/>
</form>

View File

@ -3,7 +3,7 @@
this repository contains the full copyright notices and license terms. -->
<tree>
<!-- <field name="operation"/>
<field name="effective_date"/>
<field name="employee"/>
<field name="quantity"/>
<field name="goal"/>
@ -15,14 +15,15 @@ this repository contains the full copyright notices and license terms. -->
<field name="total_time"/>
<field name="performance"/>
<field name="state"/> -->
<field name="start_time" widget="date"/>
<field name="effective_date"/>
<field name="work_center"/>
<field name="operation"/>
<field name="employee"/>
<field name="customer"/>
<field name="units"/>
<field name="start_time" widget="time" string="Start Time"/>
<field name="end_time" widget="time" string="End Time"/>
<field name="start_time" widget="time" string="Inicio"/>
<field name="end_time" widget="time" string="Final"/>
<field name="planned_end_time" widget="time"/>
<field name="performance"/>
<button name="finishButton" string="Finish"/>
<button name="duplicate_wizard" string="Duplicate"/>

View File

@ -8,4 +8,12 @@ this repository contains the full copyright notices and license terms. -->
<field name="kind"/>
<label name="time_of_realization"/>
<field name="time_of_realization"/>
<label name="hour_man"/>
<field name="hour_man"/>
<label name="uom"/>
<field name="uom"/>
<newline />
<group id="supplies" col="1" colspan="2">
<field name="supplies" colspan="4"/>
</group>
</form>