mirror of
https://bitbucket.org/presik/trytonpsk-farming.git
synced 2023-12-14 05:23:00 +01:00
304 lines
10 KiB
Python
304 lines
10 KiB
Python
# 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 __future__ import with_statement
|
|
from decimal import Decimal
|
|
from datetime import date
|
|
|
|
from trytond.model import Workflow, ModelView, ModelSQL, fields
|
|
from trytond.pyson import Eval, If, In, Get
|
|
from trytond.transaction import Transaction
|
|
from trytond.pool import Pool
|
|
from trytond.exceptions import UserError
|
|
|
|
STATES = {
|
|
'readonly': (Eval('state') != 'draft'),
|
|
}
|
|
|
|
|
|
class Kind(ModelSQL, ModelView):
|
|
"Kind"
|
|
__name__ = "farming.activity.kind"
|
|
name = fields.Char('Name', required=True)
|
|
activity_time = fields.Float('Act. Time', required=True)
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(Kind, cls).__setup__()
|
|
cls._order.insert(0, ('name', 'ASC'))
|
|
|
|
|
|
class FarmingProduction(Workflow, ModelSQL, ModelView):
|
|
'Farming Production'
|
|
__name__ = 'farming.production'
|
|
_rec_name = 'number'
|
|
number = fields.Char('Number', readonly=True)
|
|
product = fields.Many2One('product.product', 'Product', required=True,
|
|
states=STATES)
|
|
location = fields.Many2One('farming.location', 'Location', required=True,
|
|
states=STATES)
|
|
company = fields.Many2One('company.company', 'Company', required=True,
|
|
states=STATES, domain=[('id', If(In('company',
|
|
Eval('context', {})), '=', '!='), Get(Eval('context', {}),
|
|
'company', 0)), ])
|
|
start_date = fields.Date('Start Date', states=STATES, depends=['state'])
|
|
end_date = fields.Date('End Date', states=STATES, depends=['state'])
|
|
field_size = fields.Float('Field Size', states=STATES, select=True, help='In hectares')
|
|
quantity_produced = fields.Float('Quantity Produced', states=STATES)
|
|
quantity_produced_uom = fields.Many2One('product.uom', 'Quantity UoM', states=STATES)
|
|
activities = fields.One2Many('farming.activity', 'production', 'Activities', states=STATES)
|
|
lot = fields.Char('Lot', states=STATES)
|
|
seed = fields.Char('Seed', required=True, states=STATES)
|
|
description = fields.Char('Description', states=STATES)
|
|
notes = fields.Text('Notes', states=STATES)
|
|
state = fields.Selection([
|
|
('draft', 'Draft'),
|
|
('production', 'Production'),
|
|
('finished', 'Finished'),
|
|
('cancelled', 'Cancelled'),
|
|
], 'State', readonly=True, required=True)
|
|
# Financial indicators
|
|
production_time = fields.Function(fields.Integer('Production Time'),
|
|
'get_production_time')
|
|
production_cost = fields.Function(fields.Numeric('Production Cost'),
|
|
'get_production_cost')
|
|
performance = fields.Function(fields.Numeric('Performance'),
|
|
'get_performance')
|
|
gross_profit_rate = fields.Function(fields.Float('Gross Profit Rate'),
|
|
'get_gross_profit_rate')
|
|
gross_profit = fields.Function(fields.Numeric('Gross Profit'),
|
|
'get_gross_profit')
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(FarmingProduction, cls).__setup__()
|
|
cls._order.insert(0, ('create_date', 'DESC'))
|
|
cls._order.insert(1, ('id', 'DESC'))
|
|
cls._transitions |= set((
|
|
('draft', 'production'),
|
|
('production', 'draft'),
|
|
('production', 'finished'),
|
|
('production', 'cancelled'),
|
|
))
|
|
cls._buttons.update({
|
|
'draft': {
|
|
'invisible': Eval('state').in_(['draft', 'finished'])
|
|
},
|
|
'cancel': {
|
|
'invisible': Eval('state') == 'finished',
|
|
},
|
|
'production': {
|
|
'invisible': Eval('state').in_(['cancelled', 'finished']),
|
|
},
|
|
'finished': {
|
|
'invisible': Eval('state') != 'production',
|
|
},
|
|
})
|
|
|
|
@staticmethod
|
|
def default_company():
|
|
return Transaction().context.get('company') or False
|
|
|
|
@staticmethod
|
|
def default_state():
|
|
return 'draft'
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('finished')
|
|
def finished(cls, records):
|
|
pass
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('cancelled')
|
|
def cancel(cls, services):
|
|
pass
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('draft')
|
|
def draft(cls, records):
|
|
pass
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('production')
|
|
def production(cls, records):
|
|
for record in records:
|
|
record.set_number()
|
|
|
|
def set_number(self):
|
|
Config = Pool().get('farming.configuration')
|
|
config = Config.get_config()
|
|
if not config.farming_production_sequence:
|
|
raise UserError('missing_sequence_farming_production')
|
|
|
|
number = config.farming_production_sequence.get()
|
|
self.write([self], {'number': number})
|
|
|
|
def get_production_time(self, name):
|
|
res = None
|
|
if self.start_date:
|
|
today = date.today()
|
|
if self.end_date:
|
|
res = (self.end_date - self.start_date).days
|
|
else:
|
|
res = (today - self.start_date).days
|
|
return res
|
|
|
|
def get_production_cost(self, name):
|
|
res = 0
|
|
return res
|
|
|
|
def get_performance(self, name):
|
|
res = 0
|
|
return res
|
|
|
|
def get_gross_profit_rate(self, name):
|
|
res = 0
|
|
return res
|
|
|
|
def get_gross_profit(self, name):
|
|
res = 0
|
|
return res
|
|
|
|
|
|
class Activity(Workflow, ModelSQL, ModelView):
|
|
'Activity'
|
|
__name__ = 'farming.activity'
|
|
_rec_name = 'kind'
|
|
sequence = fields.Char('Sequence', states=STATES)
|
|
production = fields.Many2One('farming.production', 'Production', states=STATES)
|
|
employee = fields.Many2One('party.party', 'Employee', states=STATES, select=True)
|
|
planned_date = fields.Date('Planned Date', states=STATES, required=True)
|
|
start_date = fields.Date('Start Date', states=STATES, required=True)
|
|
end_date = fields.Date('End Date', states=STATES)
|
|
kind = fields.Many2One('farming.activity.kind', 'Kind', states=STATES)
|
|
notes = fields.Text('Notes', states=STATES)
|
|
days = fields.Function(fields.Integer('Days', states=STATES), 'get_days')
|
|
shipments = fields.Many2Many('farming.activity-shipment.internal',
|
|
'activity', 'shipment', 'Shipments', states=STATES)
|
|
works = fields.One2Many('farming.production.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')
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(Activity, cls).__setup__()
|
|
cls._order.insert(0, ('planned_date', 'DESC'))
|
|
cls._order.insert(1, ('id', 'DESC'))
|
|
cls._transitions |= set((
|
|
('draft', 'execution'),
|
|
('execution', 'done'),
|
|
('execution', 'draft'),
|
|
('execution', 'cancelled'),
|
|
('cancelled', 'draft'),
|
|
))
|
|
cls._buttons.update({
|
|
'draft': {
|
|
'invisible': Eval('state').in_(['draft', 'done']),
|
|
},
|
|
'cancel': {
|
|
'invisible': Eval('state').in_(['cancelled', 'done']),
|
|
},
|
|
'execution': {
|
|
'invisible': Eval('state') != 'draft',
|
|
},
|
|
'done': {
|
|
'invisible': Eval('state') != 'execution',
|
|
},
|
|
})
|
|
|
|
@staticmethod
|
|
def default_company():
|
|
return Transaction().context.get('company') or False
|
|
|
|
@staticmethod
|
|
def default_state():
|
|
return 'draft'
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('done')
|
|
def done(cls, records):
|
|
pass
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('execution')
|
|
def execution(cls, records):
|
|
for record in records:
|
|
record.set_number()
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('cancelled')
|
|
def cancel(cls, records):
|
|
pass
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('draft')
|
|
def draft(cls, records):
|
|
pass
|
|
|
|
def get_amount(self, name):
|
|
res = 0
|
|
for shipment in self.shipments:
|
|
for m in shipment.outgoing_moves:
|
|
res += m.product.template.cost_price * Decimal(m.quantity)
|
|
for w in self.works:
|
|
res += w.unit_price * Decimal(w.quantity)
|
|
return res
|
|
|
|
def get_days(self, name):
|
|
res = 0
|
|
if self.start_date and self.end_date:
|
|
res = (self.end_date - self.start_date).days
|
|
return res
|
|
|
|
|
|
class FarmingWork(ModelSQL, ModelView):
|
|
'Farming Work'
|
|
__name__ = 'farming.production.work'
|
|
sequence = fields.Integer('Sequence', select=True)
|
|
activity = fields.Many2One('farming.activity', 'Activity', select=True,
|
|
ondelete='CASCADE', required=True)
|
|
method = fields.Selection([
|
|
('by_day', 'By Day'),
|
|
('by_productivity', 'By Productivity'),
|
|
], 'Method', required=True)
|
|
quantity = fields.Float('Quantity', required=True)
|
|
unit_price = fields.Numeric('Unit Price', digits=(16, 2), required=True)
|
|
amount = fields.Function(fields.Numeric('Amount', digits=(16, 2)), 'get_amount')
|
|
employee = fields.Many2One('company.employee', 'Employee')
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(FarmingWork, cls).__setup__()
|
|
|
|
def get_amount(self, name):
|
|
res = 0
|
|
if self.quantity and self.unit_price:
|
|
res = Decimal(self.quantity) * self.unit_price
|
|
return res
|
|
|
|
@staticmethod
|
|
def default_method():
|
|
return 'by_day'
|
|
|
|
|
|
class FarmingActivityShipmentInternal(ModelSQL):
|
|
'Farming Activity - Shipment Internal'
|
|
__name__ = 'farming.activity-shipment.internal'
|
|
_table = 'farming_activity_shipment_internal_rel'
|
|
activity = fields.Many2One('farming.activity', 'Activity',
|
|
ondelete='CASCADE', select=True, required=True)
|
|
shipment = fields.Many2One('stock.shipment.internal', 'Tax',
|
|
ondelete='RESTRICT', required=True)
|