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


class ResPartner(models.Model):
    """ This class upgrade the partners to match Compassion needs.
        It also synchronize all changes with the MySQL server of GP.
    """

    _inherit = 'res.partner'

    ##########################################################################
    #                           PUBLIC METHODS                               #
    ##########################################################################
    @api.model
    def gp_create(self, vals):
        """ Simple create method that skips MySQL insertion, since it is
            called from GP in order to export the addresses. """
        for key in vals.iterkeys():
            if key.endswith('_id'):
                vals[key] = int(vals[key])
        partner = super(ResPartner, self).create(vals)
        return partner.id

    @api.multi
    def gp_write(self, vals):
        """ Simple write method that skips MySQL insertion, since it is
        called from GP in order to export the addresses, and to cast
        integers which are not properly passed by GP. """
        for key in vals.iterkeys():
            if key.endswith('_id'):
                vals[key] = int(vals[key])
        return super(ResPartner, self).write(vals)

    @api.model
    def create(self, vals):
        """ We override the create method so that each partner creation will
            also propagate in the MySQL table used by GP. This method is also
            called by GP with XMLRPC, so that the Odoo holds all the logic
            to sync the two databases.
        """
        uid = self.env.user.id
        gp = gp_connector.GPConnect()
        fieldsUpdate = vals.keys()
        obj_partner = self.env['res.partner']
        # If the reference is not defined, we automatically set it.
        if 'ref' not in fieldsUpdate:
            # If the contact has a parent and uses his address, plus the parent
            # has a reference, we use the same reference.
            if vals.get('parent_id') and vals.get('use_parent_address'):
                vals['ref'] = obj_partner.browse(vals['parent_id']).ref
            # Otherwise we attribute a new reference number
            else:
                vals['ref'] = gp.nextRef()

        # Create the partner in Odoo, with original values, before exporting
        # to MySQL.
        partner = super(ResPartner, self).create(
            vals).with_context(lang='en_US')

        # Set some special fields that need a conversion before sending to
        # MySQL
        categories = partner._get_category_names()
        gp.upsert_partner(partner, categories, uid)

        return partner

    @api.multi
    def write(self, vals):
        """ We override the write method so that each update will also update
            the partner in the MySQL table used by GP. This method is also
            called by GP with XMLRPC, so that the update syncs the two
            databases. """
        uid = self.env.user.id
        gp = gp_connector.GPConnect()

        # We first call the original write method and update the records.
        result = super(ResPartner, self).write(vals)

        for partner in self.with_context(lang='en_US'):
            if partner.ref:
                # Handle the change of parent_id and use_parent_address
                if 'use_parent_address' in vals.keys() and partner.parent_id:
                    if vals['use_parent_address']:
                        # Change the MySQL partner so that it is linked to the
                        # contact
                        gp.linkContact(uid, partner.parent_id.ref, partner.id)
                        # Merge contact with parent
                        vals['ref'] = partner.parent_id.ref
                    else:
                        # Change the MySQL company partner so that it is no
                        # more linked to the contact of the company
                        gp.unlinkContact(
                            uid, partner.parent_id,
                            partner.parent_id._get_category_names())
                        # Link with contact in GP and sync or create new
                        # contact if it doesn't exist.
                        ref = gp.getRefContact(uid, partner.id)
                        if ref < 0:
                            ref = str(gp.nextRef())
                        vals['ref'] = ref
                    # Write the new reference in OpenERP
                    partner.ref = vals['ref']

                # Handle the change of the company status (weird update, but
                # could happen!)
                if 'is_company' in vals.keys():
                    if vals['is_company']:
                        gp.changeToCompany(uid, partner.id, partner.name)
                    else:
                        # Unlink the contact, if any exists.<commit_msg>Fix bug in partner sync<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __openerp__.py
#
##############################################################################
from openerp import api, models
from . import gp_connector


class ResPartner(models.Model):
    """ This class upgrade the partners to match Compassion needs.
        It also synchronize all changes with the MySQL server of GP.
    """

    _inherit = 'res.partner'

    ##########################################################################
    #                           PUBLIC METHODS                               #
    ##########################################################################
    @api.model
    def gp_create(self, vals):
        """ Simple create method that skips MySQL insertion, since it is
            called from GP in order to export the addresses. """
        for key in vals.iterkeys():
            if key.endswith('_id'):
                vals[key] = int(vals[key])
        partner = super(ResPartner, self).create(vals)
        return partner.id

    @api.multi
    def gp_write(self, vals):
        """ Simple write method that skips MySQL insertion, since it is
        called from GP in order to export the addresses, and to cast
        integers which are not properly passed by GP. """
        for key in vals.iterkeys():
            if key.endswith('_id'):
                vals[key] = int(vals[key])
        return super(ResPartner, self).write(vals)

    @api.model
    def create(self, vals):
        """ We override the create method so that each partner creation will
            also propagate in the MySQL table used by GP. This method is also
            called by GP with XMLRPC, so that the Odoo holds all the logic
            to sync the two databases.
        """
        uid = self.env.user.id
        gp = gp_connector.GPConnect()
        fieldsUpdate = vals.keys()
        obj_partner = self.env['res.partner']
        # If the reference is not defined, we automatically set it.
        if 'ref' not in fieldsUpdate:
            # If the contact has a parent and uses his address, plus the parent
            # has a reference, we use the same reference.
            if vals.get('parent_id') and vals.get('use_parent_address'):
                vals['ref'] = obj_partner.browse(vals['parent_id']).ref
            # Otherwise we attribute a new reference number
            else:
                vals['ref'] = gp.nextRef()

        # Create the partner in Odoo, with original values, before exporting
        # to MySQL.
        partner = super(ResPartner, self).create(
            vals).with_context(lang='en_US')

        # Set some special fields that need a conversion before sending to
        # MySQL
        categories = partner._get_category_names()
        gp.upsert_partner(partner, categories, uid)

        return partner

    @api.multi
    def write(self, vals):
        """ We override the write method so that each update will also update
            the partner in the MySQL table used by GP. This method is also
            called by GP with XMLRPC, so that the update syncs the two
            databases. """
        uid = self.env.user.id
        gp = gp_connector.GPConnect()

        # We first call the original write method and update the records.
        result = super(ResPartner, self).write(vals)

        for partner in self.with_context(lang='en_US'):
            if partner.ref:
                # Handle the change of parent_id and use_parent_address
                if 'use_parent_address' in vals.keys() and partner.parent_id:
                    if vals['use_parent_address']:
                        # Change the MySQL partner so that it is linked to the
                        # contact
                        gp.linkContact(uid, partner.parent_id.ref, partner.id)
                        # Merge contact with parent
                        vals['ref'] = partner.parent_id.ref
                    else:
                        # Change the MySQL company partner so that it is no
                        # more linked to the contact of the company
                        gp.unlinkContact(
                            uid, partner.parent_id,
                            partner.parent_id._get_category_names())
                        # Link with contact in GP and sync or create new
                        # contact if it doesn't exist.
                        ref = gp.getRefContact(partner.id)
                        if ref < 0:
                            ref = str(gp.nextRef())
                        vals['ref'] = ref
                    # Write the new reference in OpenERP
                    partner.ref = vals['ref']

                # Handle the change of the company status (weird update, but
                # could happen!)
                if 'is_company' in vals.keys():
                    if vals['is_company']:
                        gp.changeToCompany(uid, partner.id, partner.name)
                    else:
                        # Unlink the contact, if any exists.
{'fraction_non_alphanumeric': 0.12988603183573513, 'fraction_numerical': 0.0011302627860977678, 'mean_word_length': 1.7781266352694924, 'pattern_counts': {'":': 0, '<': 11, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7649052', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2506, 'n_words': 938}