<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME>, <NAME>
#
#    The licence is in the file __openerp__.py
#
##############################################################################

from openerp.osv import orm, fields
from openerp import netsvc
from openerp.tools.translate import _
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF

from datetime import datetime
from dateutil.relativedelta import relativedelta
import logging

logger = logging.getLogger(__name__)


class recurring_contract(orm.Model):
    _inherit = "recurring.contract"

    ################################
    #        PRIVATE METHODS       #
    ################################
    def _active(self, cr, uid, ids, field_name, args, context=None):
        # Dummy function that sets the active flag.
        self._on_contract_active(cr, uid, ids, context=context)
        return {id: True for id in ids}

    def _get_contract_from_invoice(invoice_obj, cr, uid, invoice_ids,
                                   context=None):
        self = invoice_obj.pool.get('recurring.contract')
        res = []
        for invoice in invoice_obj.browse(cr, uid, invoice_ids,
                                          context=context):
            if invoice.state == 'paid':
                self._invoice_paid(cr, uid, invoice, context)
                last_pay_date = max([move_line.date
                                     for move_line in invoice.payment_ids
                                     if move_line.credit > 0] or [0])
                for invoice_line in invoice.invoice_line:
                    contract = invoice_line.contract_id
                    if contract.state == 'waiting' and last_pay_date:
                        # Activate the contract and set the
                        # first_payment_date
                        res.append(invoice_line.contract_id.id)
                        self.write(
                            cr, uid, contract.id, {
                                'first_payment_date':
                                    datetime.today().strftime(DF)},
                            context=context)

        return res

    def _on_contract_active(self, cr, uid, ids, context=None):
        """ Hook for doing something when contract is activated.
        Update child to mark it has been sponsored, and update partner
        to add the 'Sponsor' category.
        """
        wf_service = netsvc.LocalService('workflow')
        if not isinstance(ids, list):
            ids = [ids]
        sponsor_cat_id = self.pool.get('res.partner.category').search(
            cr, uid, [('name', '=', 'Sponsor')], context={'lang': 'en_US'})[0]
        for contract in self.browse(cr, uid, ids, context):
            if contract.child_id:
                contract.child_id.write({'has_been_sponsored': True})
                partner_categories = set(
                    [cat.id for cat in contract.partner_id.category_id])
                partner_categories.add(sponsor_cat_id)
                # Standard way in Odoo to set one2many fields
                contract.partner_id.write({
                    'category_id': [(6, 0, list(partner_categories))]})
                wf_service.trg_validate(
                    uid, 'recurring.contract', contract.id,
                    'contract_active', cr)
            logger.info("Contract " + str(contract.id) + " activated.")

    def _invoice_paid(self, cr, uid, invoice, context=None):
        """ Hook for doing something when an invoice is paid. """
        pass

    def _is_fully_managed(self, cr, uid, ids, field_name, arg, context):
        # Tells if the correspondent and the payer is the same person.
        res = {}
        for contract in self.browse(cr, uid, ids, context=context):
            res[contract.id] = contract.partner_id == contract.correspondant_id
        return res

    def get_ending_reasons(self, cr, uid, context=None):
        # Returns all the ending reasons of sponsorships
        return [
            ('1', _("Depart of child")),
            ('2', _("Mistake from our staff")),
            ('3', _("Death of sponsor")),
            ('4', _("Moved to foreign country")),<commit_msg>Fixed activation of contracts<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME>, <NAME>
#
#    The licence is in the file __openerp__.py
#
##############################################################################

from openerp.osv import orm, fields
from openerp import netsvc
from openerp.tools.translate import _
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF

from datetime import datetime
from dateutil.relativedelta import relativedelta
import logging

logger = logging.getLogger(__name__)


class recurring_contract(orm.Model):
    _inherit = "recurring.contract"

    ################################
    #        PRIVATE METHODS       #
    ################################
    def _active(self, cr, uid, ids, field_name, args, context=None):
        # Dummy function that sets the active flag.
        self._on_contract_active(cr, uid, ids, context=context)
        return {id: True for id in ids}

    def _get_contract_from_invoice(invoice_obj, cr, uid, invoice_ids,
                                   context=None):
        self = invoice_obj.pool.get('recurring.contract')
        res = []
        for invoice in invoice_obj.browse(cr, uid, invoice_ids,
                                          context=context):
            if invoice.state == 'paid':
                self._invoice_paid(cr, uid, invoice, context)
                last_pay_date = max([move_line.date
                                     for move_line in invoice.payment_ids
                                     if move_line.credit > 0] or [0])
                for invoice_line in invoice.invoice_line:
                    contract = invoice_line.contract_id
                    if contract.state == 'waiting' and last_pay_date:
                        # Activate the contract and set the
                        # first_payment_date
                        res.append(invoice_line.contract_id.id)
                        self.write(
                            cr, uid, contract.id, {
                                'first_payment_date':
                                    datetime.today().strftime(DF)},
                            context=context)

        return res

    def _on_contract_active(self, cr, uid, ids, context=None):
        """ Hook for doing something when contract is activated.
        Update child to mark it has been sponsored, and update partner
        to add the 'Sponsor' category.
        """
        wf_service = netsvc.LocalService('workflow')
        if not isinstance(ids, list):
            ids = [ids]
        sponsor_cat_id = self.pool.get('res.partner.category').search(
            cr, uid, [('name', '=', 'Sponsor')], context={'lang': 'en_US'})[0]
        for contract in self.browse(cr, uid, ids, context):
            if contract.child_id:
                contract.child_id.write({'has_been_sponsored': True})
                partner_categories = set(
                    [cat.id for cat in contract.partner_id.category_id])
                partner_categories.add(sponsor_cat_id)
                # Standard way in Odoo to set one2many fields
                contract.partner_id.write({
                    'category_id': [(6, 0, list(partner_categories))]})
            wf_service.trg_validate(
                uid, 'recurring.contract', contract.id,
                'contract_active', cr)
            logger.info("Contract " + str(contract.id) + " activated.")

    def _invoice_paid(self, cr, uid, invoice, context=None):
        """ Hook for doing something when an invoice is paid. """
        pass

    def _is_fully_managed(self, cr, uid, ids, field_name, arg, context):
        # Tells if the correspondent and the payer is the same person.
        res = {}
        for contract in self.browse(cr, uid, ids, context=context):
            res[contract.id] = contract.partner_id == contract.correspondant_id
        return res

    def get_ending_reasons(self, cr, uid, context=None):
        # Returns all the ending reasons of sponsorships
        return [
            ('1', _("Depart of child")),
            ('2', _("Mistake from our staff")),
            ('3', _("Death of sponsor")),
            ('4', _("Moved to foreign country")),
{'fraction_non_alphanumeric': 0.14191343963553532, 'fraction_numerical': 0.003416856492027335, 'mean_word_length': 2.0342087076710436, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3213075', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2255, 'n_words': 634}