trytonpsk-surveillance/sale.py

140 lines
4.7 KiB
Python
Raw Permalink Normal View History

2021-10-22 06:00:56 +02:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2021-12-22 23:10:12 +01:00
from datetime import date
2021-10-22 06:00:56 +02:00
2023-03-30 04:40:52 +02:00
from trytond.model import fields, ModelSQL, ModelView
2021-11-10 21:54:08 +01:00
from trytond.pool import PoolMeta, Pool
from trytond.i18n import gettext
2023-03-30 04:40:52 +02:00
from trytond.pyson import Eval
2021-11-10 21:54:08 +01:00
from trytond.exceptions import UserError
2022-04-30 20:38:13 +02:00
from .schedule import PAYMENT_METHOD
2021-10-22 06:00:56 +02:00
2021-10-23 18:40:59 +02:00
class ContractProductLine(metaclass=PoolMeta):
__name__ = 'sale.contract.product_line'
2021-10-23 01:07:21 +02:00
shift_payment_method = fields.Selection(PAYMENT_METHOD,
2023-08-31 22:16:38 +02:00
'Shift Payment Method', states={'invisible': ~Eval('require_plan', False)})
2023-08-31 21:48:20 +02:00
location = fields.Many2One('surveillance.location', 'Location',
2023-08-31 22:16:38 +02:00
states={'invisible': ~Eval('require_plan', False)})
2023-08-31 21:48:20 +02:00
require_plan = fields.Boolean('Require Plan',
help='Check this option if product require surveillance plan')
2023-03-30 04:40:52 +02:00
positions_plan = fields.One2Many('sale.contract.product.position',
'product_line', 'Products Plan',
2023-08-31 22:16:38 +02:00
states={'invisible': ~Eval('require_plan', False)},
2023-03-30 04:40:52 +02:00
context={
'location': Eval('location', -1)
})
2021-11-10 21:54:08 +01:00
class SaleContract(metaclass=PoolMeta):
__name__ = 'sale.contract'
2021-12-22 23:10:12 +01:00
@classmethod
def _get_origin(cls):
2022-06-24 16:02:17 +02:00
return super(SaleContract, cls)._get_origin() + ['crm.opportunity']
2021-12-22 23:10:12 +01:00
2021-11-10 21:54:08 +01:00
@classmethod
def confirm(cls, contracts):
super(SaleContract, cls).confirm(contracts)
for contract in contracts:
contract.create_plan()
def create_plan(self):
pool = Pool()
Plan = pool.get('surveillance.plan')
plans = Plan.search([
2022-06-24 17:39:33 +02:00
('customer', '=', self.party.id),
2021-11-10 21:54:08 +01:00
])
if not plans:
plan, = Plan.create([{
'customer': self.party.id,
'company': self.company.id,
'state': 'draft',
}])
else:
plan = plans[0]
to_add = []
for li in self.product_lines:
2023-08-31 21:48:20 +02:00
if not li.require_plan:
continue
2021-11-10 21:54:08 +01:00
if not li.location:
raise UserError(gettext('surveillance.msg_missing_location'))
to_add.append({
'location': li.location.id,
'contract': li.contract.id,
'hired_service': li.id,
'start_date': li.start_date,
'end_date': li.end_date,
'shift_payment_method': li.shift_payment_method,
'state': 'preoperative',
'comment': li.description,
})
if to_add:
Plan.write([plan], {
'lines': [('create', to_add)]
})
2021-12-22 23:10:12 +01:00
class SaleOpportunity(metaclass=PoolMeta):
2022-06-24 16:02:17 +02:00
__name__ = "crm.opportunity"
2021-12-22 23:10:12 +01:00
contracts = fields.One2Many('sale.contract', 'origin', 'Contract')
@classmethod
def convert(cls, opportunities):
2023-03-30 04:40:52 +02:00
# Contract = Pool().get('sale.contract')
2021-12-22 23:10:12 +01:00
contracts = [o.create_contract() for o in opportunities if not o.contracts]
def create_contract(self):
'''
Create a sale contract for the opportunity and return the contract
'''
contract = self.get_contract_opportunity()
contract_lines = []
for line in self.lines:
contract_lines.append(self.get_contract_line(line))
2021-12-23 00:15:20 +01:00
contract.product_lines = contract_lines
contract.save()
2021-12-22 23:10:12 +01:00
return contract
def get_contract_opportunity(self):
'''
Return contract for an opportunity
'''
SaleContract = Pool().get('sale.contract')
contract = {
'description': self.description,
'party': self.party,
'payment_term': self.payment_term,
'company': self.company,
'currency': self.company.currency,
'comment': self.comment,
'contract_date': date.today(),
'start_date': date.today(),
'invoice_method': 'standard',
'origin': str(self),
}
2021-12-23 00:15:20 +01:00
contract, = SaleContract.create([contract])
return contract
2021-12-22 23:10:12 +01:00
def get_contract_line(self, line):
value = {
'product': line.product.id,
2021-12-23 00:15:20 +01:00
'unit_price': line.unit_price or 0,
'description': line.product.rec_name,
'type': 'line',
# 'quantity': line.quantity,
2021-12-22 23:10:12 +01:00
}
return value
2023-03-30 04:40:52 +02:00
class ContractProductPosition(ModelSQL, ModelView):
"Contract Product Position"
__name__ = "sale.contract.product.position"
product_line = fields.Many2One('sale.contract.product_line', 'Product Line',
required=True, ondelete='CASCADE')
position = fields.Many2One('surveillance.location.position', 'Position',
domain=[
('location', '=', Eval('_parent_product_line', {}).get('location'))
], required=True)