<commit_before># -*- coding: utf-8 -*-
# © 2014-2016 <NAME> (http://www.barroux.org)
# © 2014-2016 Akretion France (<NAME> <<EMAIL>>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo.exceptions import ValidationError


class DonationDonation(models.Model):
    _inherit = 'donation.donation'

    recurring_template = fields.Selection([
        ('active', 'Active'),
        ('suspended', 'Suspended'),
        ], string='Recurring Template', copy=False, index=True)
    source_recurring_id = fields.Many2one(
        'donation.donation', string='Source Recurring Template',
        states={'done': [('readonly', True)]})
    recurring_donation_ids = fields.One2many(
        'donation.donation', 'source_recurring_id',
        string='Past Recurring Donations', readonly=True, copy=False)

    @api.multi
    @api.constrains(
        'recurring_template', 'source_recurring_id', 'state',
        'tax_receipt_option')
    def _check_recurring_donation(self):
        for donation in self:
            if donation.recurring_template and donation.state != 'draft':
                raise ValidationError(_(
                    "The recurring donation template of '%s' must stay in "
                    "draft state.") % donation.partner_id.name)
            if donation.source_recurring_id and donation.recurring_template:
                raise ValidationError(_(
                    "The recurring donation template of '%s' cannot have "
                    "a Source Recurring Template")
                    % donation.partner_id.name)
            if (
                    donation.recurring_template and
                    donation.tax_receipt_option == 'each'):
                raise ValidationError(_(
                    "The recurring donation of %s cannot have a tax "
                    "receipt option 'Each'.")
                    % donation.partner_id.name)

    @api.multi
    @api.depends('state', 'partner_id', 'move_id', 'recurring_template')
    def _compute_display_name(self):
        for donation in self:
            if donation.state == 'draft':
                if donation.recurring_template == 'active':
                    name = _('Recurring Donation of %s') % (
                        donation.partner_id.name)
                elif donation.recurring_template == 'suspended':
                    name = _('Suspended Recurring Donation of %s') % (
                        donation.partner_id.name)
                else:
                    name = _('Draft Donation of %s') % donation.partner_id.name
            elif donation.state == 'cancel':
                name = _('Cancelled Donation of %s') % donation.partner_id.name
            else:
                name = donation.number
            donation.display_name = name

    @api.onchange('recurring_template')
    def recurring_template_change(self):
        res = {'warning': {}}
        if self.recurring_template and self.tax_receipt_option == 'each':
            self.tax_receipt_option = 'annual'
            res['warning']['title'] = _('Update of Tax Receipt Option')
            res['warning']['message'] = _(<commit_msg>FIX warning about display_name field<commit_after># -*- coding: utf-8 -*-
# © 2014-2016 <NAME> (http://www.barroux.org)
# © 2014-2016 Akretion France (<NAME> <<EMAIL>>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo.exceptions import ValidationError


class DonationDonation(models.Model):
    _inherit = 'donation.donation'

    recurring_template = fields.Selection([
        ('active', 'Active'),
        ('suspended', 'Suspended'),
        ], string='Recurring Template', copy=False, index=True)
    source_recurring_id = fields.Many2one(
        'donation.donation', string='Source Recurring Template',
        states={'done': [('readonly', True)]})
    recurring_donation_ids = fields.One2many(
        'donation.donation', 'source_recurring_id',
        string='Past Recurring Donations', readonly=True, copy=False)

    @api.multi
    @api.constrains(
        'recurring_template', 'source_recurring_id', 'state',
        'tax_receipt_option')
    def _check_recurring_donation(self):
        for donation in self:
            if donation.recurring_template and donation.state != 'draft':
                raise ValidationError(_(
                    "The recurring donation template of '%s' must stay in "
                    "draft state.") % donation.partner_id.name)
            if donation.source_recurring_id and donation.recurring_template:
                raise ValidationError(_(
                    "The recurring donation template of '%s' cannot have "
                    "a Source Recurring Template")
                    % donation.partner_id.name)
            if (
                    donation.recurring_template and
                    donation.tax_receipt_option == 'each'):
                raise ValidationError(_(
                    "The recurring donation of %s cannot have a tax "
                    "receipt option 'Each'.")
                    % donation.partner_id.name)

    @api.multi
    @api.depends('state', 'partner_id', 'move_id', 'recurring_template')
    def _compute_display_name_field(self):
        for donation in self:
            if donation.state == 'draft':
                if donation.recurring_template == 'active':
                    name = _('Recurring Donation of %s') % (
                        donation.partner_id.name)
                elif donation.recurring_template == 'suspended':
                    name = _('Suspended Recurring Donation of %s') % (
                        donation.partner_id.name)
                else:
                    name = _('Draft Donation of %s') % donation.partner_id.name
            elif donation.state == 'cancel':
                name = _('Cancelled Donation of %s') % donation.partner_id.name
            else:
                name = donation.number
            donation.display_name = name

    @api.onchange('recurring_template')
    def recurring_template_change(self):
        res = {'warning': {}}
        if self.recurring_template and self.tax_receipt_option == 'each':
            self.tax_receipt_option = 'annual'
            res['warning']['title'] = _('Update of Tax Receipt Option')
            res['warning']['message'] = _(
{'fraction_non_alphanumeric': 0.11252154834665413, 'fraction_numerical': 0.006582040432534086, 'mean_word_length': 2.1086215294690698, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1196984', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1748, 'n_words': 414}