<commit_before>#########################################################################
#This module intergrates Open ERP with the magento core                 #
#Core settings are stored here                                          #
#########################################################################
#                                                                       #
# Copyright (C) 2009  <NAME>                                    #
#                                                                       #
#This program is free software: you can redistribute it and/or modify   #
#it under the terms of the GNU General Public License as published by   #
#the Free Software Foundation, either version 3 of the License, or      #
#(at your option) any later version.                                    #
#                                                                       #
#This program is distributed in the hope that it will be useful,        #
#but WITHOUT ANY WARRANTY; without even the implied warranty of         #
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
#GNU General Public License for more details.                           #
#                                                                       #
#You should have received a copy of the GNU General Public License      #
#along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#########################################################################

from osv import osv, fields
import magerp_osv
from base_external_referentials import external_osv

import tools

DEBUG = True
TIMEOUT = 2
        
class external_referential(magerp_osv.magerp_osv):
    #This class stores instances of magento to which the ERP will connect, so you can connect OpenERP to multiple Magento installations (eg different Magento databases)
    _inherit = "external.referential"

    _columns = {
        'attribute_sets':fields.one2many('magerp.product_attribute_set', 'referential_id', 'Attribute Sets'),
        'default_pro_cat':fields.many2one('product.category','Default Product Category',required=True, help="Products imported from magento may have many categories.\nOpenERP requires a specific category for a product to facilitate invoicing etc."),
        'default_lang_id':fields.many2one('res.lang', 'Default Language',required=True, help="Choose the language which will be used for the Default Value in Magento"),
        'active': fields.boolean('Active'),
    }

    _defaults = {
        'active': lambda *a: 1,
    }
             
    def connect(self, cr, uid, ids, context=None):
        #ids has to be a list
        if isinstance(ids, (list, tuple)) and len(ids) == 1:
            instance = self.browse(cr, uid, ids[0], context)
            if instance:
                core_imp_conn = self.external_connection(cr, uid, instance, DEBUG)
                if core_imp_conn.connect():
                    return core_imp_conn
                else:
                    raise osv.except_osv(_("Connection Error"), _("Could not connect to server\nCheck location, username & password."))

        return False

    def core_sync(self, cr, uid, ids, context=None):
        instances = self.browse(cr, uid, ids, context)
        filter = []
        for inst in instances:
            core_imp_conn = self.external_connection(cr, uid, inst, DEBUG)
            self.pool.get('external.shop.group').mage_import_base(cr, uid,core_imp_conn, inst.id, defaults={'referential_id':inst.id})
            self.pool.get('sale.shop').mage_import_base(cr, uid, core_imp_conn, inst.id, {'magento_shop':True, 'company_id':self.pool.get('res.users').browse(cr, uid, uid).company_id.id})
            self.pool.get('magerp.storeviews').mage_import_base(cr,uid,core_imp_conn, inst.id, defaults={})
        return True

    def sync_categs(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            pro_cat_conn = self.external_connection(cr, uid, inst, DEBUG)
            confirmation = pro_cat_conn.call('catalog_category.currentStore', [0])   #Set browse to root store
            if confirmation:
                categ_tree = pro_cat_conn.call('catalog_category.tree')             #Get the tree
                self.pool.get('product.category').record_entire_tree(cr, uid, inst.id, pro_cat_conn, categ_tree, DEBUG)
                #exp_ids = self.pool.get('product.category').search(cr,uid,[('exportable','=',True)])
                #self.pool.get('product.category').ext_export(cr,uid,exp_ids,[inst.id],{},{'conn_obj':pro_cat_conn})
        return True

    def sync_attribs(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            attrib_set_ids = self.pool.get('magerp.product_attribute_set').search(cr, uid, [('referential_id', '=', inst.id)])
            attrib_sets = self.pool.get('magerp.product_attribute_set').read(cr, uid, attrib_set_ids, ['magento_id'])
            #Get all attribute set ids to get all attributes in one go
            all_attr_set_ids = self.pool.get('magerp.product_attribute_set').get_all_mage_ids(cr, uid, [], inst.id)
            #Call magento for all attributes
            mage_inp = attr_conn.call('ol_catalog_product_attribute.list', [all_attr_set_ids])             #Get the tree
            #self.pool.get('magerp.product_attributes').sync_import(cr, uid, mage_inp, inst.id, DEBUG) #Last argument is extra mage2oe filter as same attribute ids
            self.pool.get('magerp.product_attributes').ext_import(cr, uid, mage_inp, inst.id, defaults={'referential_id':inst.id}, context={'referential_id':inst.id})
            #Relate attribute sets & attributes
            mage_inp = {}
            #Pass in {attribute_set_id:{attributes},attribute_set_id2:{attributes}}
            #print "Attribute sets are:", attrib_sets
            for each in attrib_sets:
                mage_inp[each['magento_id']] = attr_conn.call('ol_catalog_product_attribute.relations', [each['magento_id']])
            if mage_inp:
                self.pool.get('magerp.product_attribute_set').relate(cr, uid, mage_inp, inst.id, DEBUG)
        return True

    def sync_attrib_sets(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            self.pool.get('magerp.product_attribute_set').mage_import_base(cr, uid, attr_conn, inst.id,{'referential_id':inst.id},{'ids_or_filter':filter})
        return True

    def sync_attrib_groups(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            attrset_ids = self.pool.get('magerp.product_attribute_set').get_all_mage_ids(cr, uid, [], inst.id)
            filter = [{'attribute_set_id':{'in':attrset_ids}}]
            self.pool.get('magerp.product_attribute_groups').mage_import_base(cr, uid, attr_conn, inst.id, {'referential_id': inst.id}, {'ids_or_filter':filter})
        return True

    def sync_customer_groups(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            self.pool.get('res.partner.category').mage_import_base(cr, uid, attr_conn, inst.id, {}, {'ids_or_filter':filter})
        return True

    def sync_customer_addresses(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            #self.pool.get('res.partner').mage_import(cr, uid, filter, attr_conn, inst.id, DEBUG)
            #TODO fix by retrieving customer list first
            self.pool.get('res.partner.address').mage_import_base(cr, uid, attr_conn, inst.id, {}, {'ids_or_filter':filter})
        return True

    def sync_products(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            list_prods = attr_conn.call('catalog_product.list')
            #self.pool.get('product.product').mage_import(cr, uid, filter, attr_conn, inst.id, DEBUG)
            result = []
            for each in list_prods:
                each_product_info = attr_conn.call('catalog_product.info', [each['product_id']])
                result.append(each_product_info)
            self.pool.get('product.product').ext_import(cr, uid, result, inst.id, context={})
        return True

    def export_products(self, cr, uid, ids, context):
        shop_ids = self.pool.get('sale.shop').search(cr, uid, [])
        for inst in self.browse(cr, uid, ids, context):
            for shop in self.pool.get('sale.shop').browse(cr, uid, shop_ids, context):
                context['conn_obj'] = self.external_connection(cr, uid, inst)
                #shop.export_catalog
                tools.debug((cr, uid, shop, context,))
                shop.export_products(cr, uid, shop, context)
        return True

    def sync_partner(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)

        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            result = []
            result_address = []

            list_customer = attr_conn.call('customer.list')

            for each in list_customer:
                customer_id = int(each['customer_id'])

                each_customer_info = attr_conn.call('customer.info', [customer_id])
                result.append(each_customer_info)

                each_customer_address_info = attr_conn.call('customer_address.list', [customer_id])
                customer_address_info = each_customer_address_info[0]
                customer_address_info['customer_id'] = customer_id
                customer_address_info['email'] = each_customer_info['email']
                result_address.append(customer_address_info)
                print customer_address_info

            partner_ids = self.pool.get('res.partner').ext_import(cr, uid, result, inst.id, context={})
            partner_address_ids = self.pool.get('res.partner.address').ext_import(cr, uid, result_address, inst.id, context={})

        return True

    def sync_newsletter(self, cr, uid, ids, context):
        #update first all customer
        self.sync_partner(cr, uid, ids, context)

        instances = self.browse(cr, uid, ids, context)<commit_msg>[FIX] Customer Import without address<commit_after>#########################################################################
#This module intergrates Open ERP with the magento core                 #
#Core settings are stored here                                          #
#########################################################################
#                                                                       #
# Copyright (C) 2009  <NAME>                                    #
#                                                                       #
#This program is free software: you can redistribute it and/or modify   #
#it under the terms of the GNU General Public License as published by   #
#the Free Software Foundation, either version 3 of the License, or      #
#(at your option) any later version.                                    #
#                                                                       #
#This program is distributed in the hope that it will be useful,        #
#but WITHOUT ANY WARRANTY; without even the implied warranty of         #
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
#GNU General Public License for more details.                           #
#                                                                       #
#You should have received a copy of the GNU General Public License      #
#along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#########################################################################

from osv import osv, fields
import magerp_osv
from base_external_referentials import external_osv

import tools

DEBUG = True
TIMEOUT = 2
        
class external_referential(magerp_osv.magerp_osv):
    #This class stores instances of magento to which the ERP will connect, so you can connect OpenERP to multiple Magento installations (eg different Magento databases)
    _inherit = "external.referential"

    _columns = {
        'attribute_sets':fields.one2many('magerp.product_attribute_set', 'referential_id', 'Attribute Sets'),
        'default_pro_cat':fields.many2one('product.category','Default Product Category',required=True, help="Products imported from magento may have many categories.\nOpenERP requires a specific category for a product to facilitate invoicing etc."),
        'default_lang_id':fields.many2one('res.lang', 'Default Language',required=True, help="Choose the language which will be used for the Default Value in Magento"),
        'active': fields.boolean('Active'),
    }

    _defaults = {
        'active': lambda *a: 1,
    }
             
    def connect(self, cr, uid, ids, context=None):
        #ids has to be a list
        if isinstance(ids, (list, tuple)) and len(ids) == 1:
            instance = self.browse(cr, uid, ids[0], context)
            if instance:
                core_imp_conn = self.external_connection(cr, uid, instance, DEBUG)
                if core_imp_conn.connect():
                    return core_imp_conn
                else:
                    raise osv.except_osv(_("Connection Error"), _("Could not connect to server\nCheck location, username & password."))

        return False

    def core_sync(self, cr, uid, ids, context=None):
        instances = self.browse(cr, uid, ids, context)
        filter = []
        for inst in instances:
            core_imp_conn = self.external_connection(cr, uid, inst, DEBUG)
            self.pool.get('external.shop.group').mage_import_base(cr, uid,core_imp_conn, inst.id, defaults={'referential_id':inst.id})
            self.pool.get('sale.shop').mage_import_base(cr, uid, core_imp_conn, inst.id, {'magento_shop':True, 'company_id':self.pool.get('res.users').browse(cr, uid, uid).company_id.id})
            self.pool.get('magerp.storeviews').mage_import_base(cr,uid,core_imp_conn, inst.id, defaults={})
        return True

    def sync_categs(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            pro_cat_conn = self.external_connection(cr, uid, inst, DEBUG)
            confirmation = pro_cat_conn.call('catalog_category.currentStore', [0])   #Set browse to root store
            if confirmation:
                categ_tree = pro_cat_conn.call('catalog_category.tree')             #Get the tree
                self.pool.get('product.category').record_entire_tree(cr, uid, inst.id, pro_cat_conn, categ_tree, DEBUG)
                #exp_ids = self.pool.get('product.category').search(cr,uid,[('exportable','=',True)])
                #self.pool.get('product.category').ext_export(cr,uid,exp_ids,[inst.id],{},{'conn_obj':pro_cat_conn})
        return True

    def sync_attribs(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            attrib_set_ids = self.pool.get('magerp.product_attribute_set').search(cr, uid, [('referential_id', '=', inst.id)])
            attrib_sets = self.pool.get('magerp.product_attribute_set').read(cr, uid, attrib_set_ids, ['magento_id'])
            #Get all attribute set ids to get all attributes in one go
            all_attr_set_ids = self.pool.get('magerp.product_attribute_set').get_all_mage_ids(cr, uid, [], inst.id)
            #Call magento for all attributes
            mage_inp = attr_conn.call('ol_catalog_product_attribute.list', [all_attr_set_ids])             #Get the tree
            #self.pool.get('magerp.product_attributes').sync_import(cr, uid, mage_inp, inst.id, DEBUG) #Last argument is extra mage2oe filter as same attribute ids
            self.pool.get('magerp.product_attributes').ext_import(cr, uid, mage_inp, inst.id, defaults={'referential_id':inst.id}, context={'referential_id':inst.id})
            #Relate attribute sets & attributes
            mage_inp = {}
            #Pass in {attribute_set_id:{attributes},attribute_set_id2:{attributes}}
            #print "Attribute sets are:", attrib_sets
            for each in attrib_sets:
                mage_inp[each['magento_id']] = attr_conn.call('ol_catalog_product_attribute.relations', [each['magento_id']])
            if mage_inp:
                self.pool.get('magerp.product_attribute_set').relate(cr, uid, mage_inp, inst.id, DEBUG)
        return True

    def sync_attrib_sets(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            self.pool.get('magerp.product_attribute_set').mage_import_base(cr, uid, attr_conn, inst.id,{'referential_id':inst.id},{'ids_or_filter':filter})
        return True

    def sync_attrib_groups(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            attrset_ids = self.pool.get('magerp.product_attribute_set').get_all_mage_ids(cr, uid, [], inst.id)
            filter = [{'attribute_set_id':{'in':attrset_ids}}]
            self.pool.get('magerp.product_attribute_groups').mage_import_base(cr, uid, attr_conn, inst.id, {'referential_id': inst.id}, {'ids_or_filter':filter})
        return True

    def sync_customer_groups(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            self.pool.get('res.partner.category').mage_import_base(cr, uid, attr_conn, inst.id, {}, {'ids_or_filter':filter})
        return True

    def sync_customer_addresses(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            #self.pool.get('res.partner').mage_import(cr, uid, filter, attr_conn, inst.id, DEBUG)
            #TODO fix by retrieving customer list first
            self.pool.get('res.partner.address').mage_import_base(cr, uid, attr_conn, inst.id, {}, {'ids_or_filter':filter})
        return True

    def sync_products(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)
        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            filter = []
            list_prods = attr_conn.call('catalog_product.list')
            #self.pool.get('product.product').mage_import(cr, uid, filter, attr_conn, inst.id, DEBUG)
            result = []
            for each in list_prods:
                each_product_info = attr_conn.call('catalog_product.info', [each['product_id']])
                result.append(each_product_info)
            self.pool.get('product.product').ext_import(cr, uid, result, inst.id, context={})
        return True

    def export_products(self, cr, uid, ids, context):
        shop_ids = self.pool.get('sale.shop').search(cr, uid, [])
        for inst in self.browse(cr, uid, ids, context):
            for shop in self.pool.get('sale.shop').browse(cr, uid, shop_ids, context):
                context['conn_obj'] = self.external_connection(cr, uid, inst)
                #shop.export_catalog
                tools.debug((cr, uid, shop, context,))
                shop.export_products(cr, uid, shop, context)
        return True

    def sync_partner(self, cr, uid, ids, context):
        instances = self.browse(cr, uid, ids, context)

        for inst in instances:
            attr_conn = self.external_connection(cr, uid, inst, DEBUG)
            result = []
            result_address = []

            list_customer = attr_conn.call('customer.list')

            for each in list_customer:
                customer_id = int(each['customer_id'])

                each_customer_info = attr_conn.call('customer.info', [customer_id])
                result.append(each_customer_info)

                each_customer_address_info = attr_conn.call('customer_address.list', [customer_id])
                if len(each_customer_address_info):
                    customer_address_info = each_customer_address_info[0]
                    customer_address_info['customer_id'] = customer_id
                    customer_address_info['email'] = each_customer_info['email']
                    result_address.append(customer_address_info)
                    print customer_address_info

            partner_ids = self.pool.get('res.partner').ext_import(cr, uid, result, inst.id, context={})
            if result_address:
                partner_address_ids = self.pool.get('res.partner.address').ext_import(cr, uid, result_address, inst.id, context={})

        return True

    def sync_newsletter(self, cr, uid, ids, context):
        #update first all customer
        self.sync_partner(cr, uid, ids, context)

        instances = self.browse(cr, uid, ids, context)
{'fraction_non_alphanumeric': 0.1280095024898351, 'fraction_numerical': 0.001461921513088766, 'mean_word_length': 2.511952510829456, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7609062', 'n_tokens_mistral': 6241, 'n_tokens_neox': 6072, 'n_words': 1501}