155 lines
5.9 KiB
Python
155 lines
5.9 KiB
Python
from trytond.pool import Pool, PoolMeta
|
|
from trytond.model import (
|
|
ModelSQL, ModelView, Workflow, fields)
|
|
from trytond.pyson import Eval, If, Bool
|
|
from trytond.modules.company.model import set_employee
|
|
from trytond.exceptions import UserError
|
|
from trytond.transaction import Transaction
|
|
from trytond.wizard import (
|
|
Button, StateAction, StateTransition, StateView, Wizard)
|
|
|
|
from trytond.modules.currency.fields import Monetary
|
|
from trytond.modules.product import price_digits
|
|
|
|
import datetime
|
|
from datetime import timedelta
|
|
|
|
|
|
class Contract(Workflow, ModelSQL, ModelView):
|
|
'Contracts'
|
|
__name__ = 'optical_equipment.contract'
|
|
_rec_name = 'number'
|
|
_order_name = 'number'
|
|
|
|
|
|
company = fields.Many2One(
|
|
'company.company', "Company", required=True, select=True,
|
|
states={
|
|
'readonly': (Eval('state') != 'draft') | Eval('party', True),
|
|
},help="Make the subscription belong to the company.")
|
|
number = fields.Char(
|
|
"Number", readonly=True, select=True,
|
|
help="The main identification of the subscription.")
|
|
reference = fields.Char(
|
|
"Reference", select=True,
|
|
help="The identification of an external origin.")
|
|
description = fields.Char("Description",
|
|
states={
|
|
'readonly': Eval('state') != 'draft',
|
|
})
|
|
party = fields.Many2One(
|
|
'party.party', "Party", required=True,
|
|
states={
|
|
'readonly': (Eval('state') != 'draft') | Eval('party', True),
|
|
},help="The party who subscribes.")
|
|
contact = fields.Many2One('party.contact_mechanism', "Contact", required=True)
|
|
invoice_address = fields.Many2One('party.address', 'Invoice Address',
|
|
required=True, domain=[('party', '=', Eval('party'))],
|
|
states={
|
|
'readonly': (Eval('state') != 'draft') | Eval('party', True),
|
|
})
|
|
start_date = fields.Date("Start Date", required=True,)
|
|
end_date = fields.Date("End Date",
|
|
domain=['OR',
|
|
('end_date', '>=', If(
|
|
Bool(Eval('start_date')),
|
|
Eval('start_date', datetime.date.min),
|
|
datetime.date.min)),
|
|
('end_date', '=', None),
|
|
],
|
|
states={
|
|
'readonly': Eval('state') != 'draft',
|
|
})
|
|
maintenance_services = fields.Many2Many('optical_equipment_maintenance.service-equipment.contract',
|
|
'contract', 'maintenance_services', "Prorogues")
|
|
equipments = fields.One2Many('optical_equipment.equipment', 'contract', "Equipments")
|
|
#equipments = fields.Many2Many('optical_equipment.contract-optical_equipment.equipment', 'contract', 'equipment')
|
|
price_contract = Monetary("Price Contract", digits=price_digits, currency='currency', required=True,
|
|
states={'readonly': Eval('state') != 'draft'})
|
|
state = fields.Selection([
|
|
('draft', "Draft"),
|
|
('running', "Running"),
|
|
('closed', "Closed"),
|
|
('cancelled', "Cancelled"),
|
|
], "State", readonly=True, required=False, sort=False,
|
|
help="The current state of the subscription.")
|
|
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(Contract, cls).__setup__()
|
|
cls._order = [
|
|
('number', 'DESC NULLS FIRST'),
|
|
('id', 'DESC'),
|
|
]
|
|
cls._transitions = ({
|
|
('draft', 'running'),
|
|
('running', 'closed'),
|
|
('running', 'cancelled'),
|
|
})
|
|
cls._buttons.update({
|
|
'running': {'invisible': Eval('state').in_(['cancelled', 'running'])},
|
|
'cancelled': {'invisible': Eval('state').in_(['draft', 'cancelled'])}
|
|
})
|
|
|
|
|
|
@staticmethod
|
|
def default_company():
|
|
return Transaction().context.get('company')
|
|
|
|
@staticmethod
|
|
def default_state():
|
|
return 'draft'
|
|
|
|
@classmethod
|
|
def set_number(cls, contracts):
|
|
pool = Pool()
|
|
Config = pool.get('optical_equipment.configuration')
|
|
config = Config(4)
|
|
|
|
if config.contract_sequence != None:
|
|
if not contracts[0].number:
|
|
try:
|
|
contracts[0].number = config.contract_sequence.get()
|
|
cls.save(contracts)
|
|
except UserError:
|
|
raise UserError(str('Validation Error'))
|
|
else:
|
|
raise UserError(gettext('optical_equipment.msg_not_sequence_equipment'))
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('running')
|
|
def running(cls, contracts):
|
|
contract = contracts[0]
|
|
for equipment in contract.equipments:
|
|
equipment.state = "contrated"
|
|
equipment.contract_history += (contract.id,)
|
|
equipment.save()
|
|
|
|
cls.set_number(contracts)
|
|
contract.state='running'
|
|
contract.save()
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('cancelled')
|
|
def cancelled(cls, contracts):
|
|
pass
|
|
|
|
|
|
|
|
class ContractMaintenanceServices(ModelSQL):
|
|
'Contract - Maintenance Services'
|
|
__name__ = 'optical_equipment_maintenance.service-equipment.contract'
|
|
|
|
maintenance_services = fields.Many2One('optical_equipment_maintenance.service', "Maintenance Service", select=True)
|
|
contract = fields.Many2One('optical_equipment.contract', "Contract")
|
|
|
|
class ContractEquipment(ModelSQL):
|
|
'Optical Equipment - Contract'
|
|
__name__ = 'optical_equipment.contract-optical_equipment.equipment'
|
|
|
|
equipment = fields.Many2One('optical_equipment.equipment', 'Equipment', select=True)
|
|
contract = fields.Many2One('optical_equipment.contract', 'Contract', select=True)
|