77 lines
3.2 KiB
Python
Executable File
77 lines
3.2 KiB
Python
Executable File
from odoo import api, fields, models, _
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class DssContractsMoveFieldWizard(models.TransientModel):
|
|
_name = "dss.contracts.move.field.wizard"
|
|
_description = "Vertragsfelder auf Ausweichfeld umsetzen"
|
|
|
|
project_id = fields.Many2one(
|
|
'dss.projects',
|
|
string='Projekt',
|
|
required=True,
|
|
readonly=True,
|
|
)
|
|
target_field_id = fields.Many2one(
|
|
'dss.advertisefields',
|
|
string='Ausweichfeld',
|
|
required=True,
|
|
domain="[('project','=',project_id), '|', ('contract','=',False), ('isblocked','=',False)]",
|
|
)
|
|
|
|
@api.model
|
|
def default_get(self, fields_list):
|
|
values = super().default_get(fields_list)
|
|
active_ids = self.env.context.get('active_ids') or []
|
|
contracts = self.env['dss.contracts'].browse(active_ids).exists()
|
|
if contracts:
|
|
projects = contracts.mapped('project')
|
|
if len(projects) == 1:
|
|
values.setdefault('project_id', projects.id)
|
|
return values
|
|
|
|
def action_move_contracts_to_field(self):
|
|
self.ensure_one()
|
|
active_ids = self.env.context.get('active_ids') or []
|
|
contracts = self.env['dss.contracts'].browse(active_ids).exists()
|
|
if not contracts:
|
|
raise ValidationError(_("Bitte mindestens einen Vertrag auswaehlen."))
|
|
|
|
projects = contracts.mapped('project')
|
|
if len(projects) != 1:
|
|
raise ValidationError(_("Bitte nur Vertraege aus einem Projekt gleichzeitig auswaehlen."))
|
|
if projects.id != self.project_id.id:
|
|
raise ValidationError(_("Das gewaehlte Ausweichfeld passt nicht zum Projekt der Vertraege."))
|
|
if self.target_field_id.project.id != self.project_id.id:
|
|
raise ValidationError(_("Das Ausweichfeld muss zum gleichen Projekt gehoeren."))
|
|
|
|
contract_ids = contracts.ids
|
|
old_fields = contracts.mapped('werbe_feld_selected')
|
|
|
|
# Remove selected contracts from all old fields first, so they become available again.
|
|
for ad_field in old_fields:
|
|
remove_cmds = [(3, contract_id) for contract_id in contract_ids if contract_id in ad_field.contract.ids]
|
|
if remove_cmds:
|
|
ad_field.write({'contract': remove_cmds})
|
|
if ad_field.contract_save and ad_field.contract_save.id in contract_ids:
|
|
ad_field.contract_save = ad_field.contract[:1].id or False
|
|
|
|
add_cmds = [(4, contract_id) for contract_id in contract_ids if contract_id not in self.target_field_id.contract.ids]
|
|
if add_cmds:
|
|
self.target_field_id.write({'contract': add_cmds})
|
|
if not self.target_field_id.contract_save and contract_ids:
|
|
self.target_field_id.contract_save = contract_ids[0]
|
|
|
|
contracts.write({'werbe_feld_selected': [(6, 0, [self.target_field_id.id])]})
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('Ausweichfeld gesetzt'),
|
|
'message': _('%s Vertrag(e) wurden auf das Feld %s umgesetzt.') % (len(contracts), self.target_field_id.feldname),
|
|
'type': 'success',
|
|
'sticky': False,
|
|
}
|
|
}
|