Example #1
0
    def import_category(self, channel_id):
        message = ""
        update_id = None
        create_id = None
        self.ensure_one()
        vals = EL(self.read(self.get_category_fields()))
        vals = self.get_channel_specific_categ_vals(channel_id, vals)
        store_id = vals.pop('store_id')
        match = channel_id.match_category_mappings(store_id)
        state = 'done'
        if not vals.get('name'):
            message += "<br/>Category without name can't evaluated"
            state = 'error'
        if not store_id:
            message += "<br/>Category without store ID can't evaluated"
            state = 'error'
        parent_id = vals.pop('parent_id')
        if parent_id:
            res = self.get_categ_id(parent_id, channel_id)
            res_parent_id = res.get('categ_id')
            if res_parent_id:
                vals['parent_id'] = res_parent_id
            else:
                _logger.error('#CategError1 %r' % res)
                state = 'error'
        vals.pop('description', None)
        vals.pop('website_message_ids', '')
        vals.pop('message_follower_ids', '')
        if match:
            if state == 'done':
                update_id = match
                try:
                    match.category_name.write(vals)
                    message += '<br/> Category %s successfully updated' % (
                        vals.get('name', ''))
                except Exception as e:
                    _logger.error('#CategError2 %r', e)
                    message += '<br/>%s' % (e)
                    state = 'error'
            elif state == 'error':
                message += '<br/>Error while category update.'

        else:
            if state == 'done':
                try:
                    erp_id = self.env['product.category'].create(vals)
                    create_id = channel_id.create_category_mapping(
                        erp_id, store_id, self.leaf_category)
                    message += '<br/> Category %s Successfully Evaluate' % (
                        vals.get('name', ''))
                except Exception as e:
                    _logger.error('#CategError3 %r', e)
                    message += '<br/>%s' % (e)
                    state = 'error'

        self.set_feed_state(state=state)
        self.message = "%s <br/> %s" % (self.message, message)
        return dict(create_id=create_id, update_id=update_id, message=message)
Example #2
0
    def import_item(self):
        self.ensure_one()
        message = ""
        mapping_id = None
        update_id = None
        create_id = None
        channel_domain = self.get_channel_domain()
        vals = EL(self.read(self.get_shipping_fields()))
        state = 'done'
        carrier_id = False
        shipping_carrier = vals.pop('shipping_carrier')
        map_domain = channel_domain + \
            [('shipping_service_id', '=', vals.get('store_id'))]
        match = self.env['channel.shipping.mappings'].search(
            map_domain, limit=1)
        if not vals.get('name'):
            message += "<br/>Shipping Method without name can't evaluated"
            state = 'error'
        if not vals.get('store_id'):
            message += "<br/>Shipping Method without store ID can't evaluated"
            state = 'error'
        if not shipping_carrier:
            message += "<br/>Shipping Method without shipping carrier can't evaluated"
            state = 'error'
        try:
            carrier_id = self.get_shiping_carrier(shipping_carrier).id
        except Exception as e:
            state = 'error'
            message += '<br/> Error in evaluating Shipping carrier %s' % (
                vals.get('name', ''))


        if state == 'done':
            if not match:
                mapping_id = self.create_shipping_mapping(
                    self.channel_id, carrier_id, vals)
                message += '<br/> Shipping carrier %s Successfully Evaluated' % (
                    vals.get('name', ''))
                create_id = mapping_id
            else:
                res = match.write(vals)
                mapping_id = match
                update_id = mapping_id
                if res:
                    message += 'Some Shipping carriers have been updated'

        self.set_feed_state(state=state)
        self.message = "%s <br/> %s" % (self.message, message)
        return dict(
            mapping_id=mapping_id,
            message=message,
            update_id=update_id,
            create_id=create_id
        )
Example #3
0
    def import_product(self, channel_id):
        self.ensure_one()
        message = ""
        create_id = None
        update_id = None
        context = dict(self._context)
        context.update({
            'pricelist': channel_id.pricelist_name.id,
            'lang': channel_id.language_id.code,
        })
        vals = EL(self.read(self.get_product_fields()))
        store_id = vals.pop('store_id')

        state = 'done'
        if not vals.get('name'):
            message += "<br/>Product without name can't evaluated"
            state = 'error'
        if not store_id:
            message += "<br/>Product without store ID can't evaluated"
            state = 'error'
        categ_id = channel_id.default_category_id.id
        if categ_id:
            vals['categ_id'] = categ_id
        weight_unit = vals.pop('weight_unit')
        if weight_unit:
            uom_id = channel_id.get_uom_id(name=weight_unit).id
            if uom_id:
                vals['uom_id'] = uom_id
                vals['uom_po_id'] = uom_id

        dimensions_unit = vals.pop('dimensions_unit')
        if dimensions_unit:
            vals['dimensions_uom_id'] = channel_id.get_uom_id(
                name=dimensions_unit).id
        if not vals.pop('wk_product_id_type'):
            vals['wk_product_id_type'] = 'wk_upc'
        variant_lines = vals.pop('feed_variants')
        feed_variants = self.feed_variants
        if variant_lines:
            check_attr = self.check_attribute_value(feed_variants)
            state = check_attr.get('state')
            message += check_attr.get('message')
        qty_available = vals.pop('qty_available')
        list_price = vals.get('list_price', '')
        if not self.channel_id.override_price:
            list_price = vals.pop('list_price')
        list_price = list_price and parse_float(list_price) or 0
        image_url = vals.pop('image_url')
        if not vals.get('barcode'):
            vals['barcode'] = False
        location_id = channel_id.location_id
        if not vals.get('image') and image_url and (image_url not in [
                'false', 'False', False
        ]):
            image_res = channel_id.read_website_image_url(image_url)
            if image_res: vals['image'] = image_res
        match = channel_id.match_template_mappings(store_id, **vals)
        template_exists_odoo = channel_id.match_odoo_template(
            vals, variant_lines=feed_variants)
        vals.pop('website_message_ids', '')
        vals.pop('message_follower_ids', '')

        if state == 'done':
            if match:
                extra_categ_ids = vals.pop('extra_categ_ids')
                if not template_exists_odoo:
                    template_id = match.template_name
                    template_id.with_context(context).write(vals)
                else:
                    template_exists_odoo.with_context(context).write(vals)
                    template_id = template_exists_odoo
                match.write({
                    'default_code': vals.get('default_code'),
                    'barcode': vals.get('barcode')
                })
                extra_categ = self.env['extra.categories'].search([
                    ('instance_id', '=', channel_id.id),
                    ('product_id', '=', template_id.id)
                ])
                if extra_categ_ids:
                    res = self.get_extra_categ_ids(extra_categ_ids, channel_id)
                    message += res.get('message', '')
                    categ_ids = res.get('categ_ids')
                    if categ_ids:
                        # code for instance wise category ids
                        data = {
                            'extra_category_ids': [(6, 0, categ_ids)],
                            'instance_id': channel_id.id,
                            'category_id': categ_id,
                        }
                        if extra_categ:
                            extra_categ.with_context(context).write(data)
                        else:
                            extra_categ = self.env[
                                'extra.categories'].with_context(
                                    context).create(data)
                        template_id.channel_category_ids = [(6, 0,
                                                             [extra_categ.id])]
                    else:
                        state = 'error'
                if len(variant_lines):
                    context['wk_qty_update'] = False
                    res = self.with_context(context).update_product_variants(
                        variant_lines, template_id, store_id, location_id,
                        channel_id)
                    if res:
                        message += res
                        state = 'error'
                else:
                    for variant_id in template_id.product_variant_ids:
                        variant_vals = variant_id.read(
                            ['default_code', 'barcode'])[0]
                        self.wk_change_product_price(product_id=variant_id,
                                                     price=list_price,
                                                     channel_id=channel_id)
                        # if qty_available and eval(qty_available) > 0:
                        #     self.wk_change_product_qty(
                        #         variant_id, qty_available, location_id)
                        match_product = channel_id.match_product_mappings(
                            store_id,
                            'No Variants',
                            default_code=variant_vals.get('default_code'),
                            barcode=variant_vals.get('barcode'))
                        if not match_product:
                            channel_id.create_product_mapping(
                                template_id, variant_id, store_id,
                                'No Variants', {
                                    'default_code':
                                    variant_vals.get('default_code'),
                                    'barcode':
                                    variant_vals.get('barcode')
                                })
                update_id = match

            else:
                template_id = None
                try:
                    vals['taxes_id'] = None
                    vals['supplier_taxes_id'] = None
                    if not template_exists_odoo:
                        if variant_lines:
                            context['create_product_product'] = 1
                        extra_categ_ids = vals.pop('extra_categ_ids')
                        if extra_categ_ids:
                            res = self.get_extra_categ_ids(
                                extra_categ_ids, channel_id)
                            message += res.get('message', '')
                            categ_ids = res.get('categ_ids')
                            if categ_ids:
                                # code for instance wise category ids
                                data = {
                                    'extra_category_ids': [(6, 0, categ_ids)],
                                    'instance_id': channel_id.id,
                                    'category_id': categ_id,
                                }
                                extra_categ = self.env[
                                    'extra.categories'].with_context(
                                        context).create(data)
                                vals['channel_category_ids'] = [
                                    (6, 0, [extra_categ.id])
                                ]
                                template_id = self.env[
                                    'product.template'].with_context(
                                        context).create(vals)
                            else:
                                state = 'error'
                        else:
                            template_id = self.env[
                                'product.template'].with_context(
                                    context).create(vals)
                    else:
                        # template_exists_odoo.with_context(context).write(vals)
                        template_id = template_exists_odoo
                        extra_categ_ids = vals.pop('extra_categ_ids')
                        if extra_categ_ids:
                            res = self.get_extra_categ_ids(
                                extra_categ_ids, channel_id)
                            message += res.get('message', '')
                            categ_ids = res.get('categ_ids')
                            if categ_ids:
                                # code for instance wise category ids
                                data = {
                                    'extra_category_ids': [(6, 0, categ_ids)],
                                    'instance_id': channel_id.id,
                                    'category_id': categ_id,
                                }
                                template_id.channel_category_ids = [(0, 0,
                                                                     data)]
                            else:
                                state = 'error'
                                template_id = None

                    if len(variant_lines) and template_id:
                        res = self._create_product_lines(
                            variant_lines, template_id, store_id, location_id,
                            channel_id)
                        if res:
                            res += message
                            state = 'error'

                    elif template_id:
                        for variant_id in template_id.product_variant_ids:
                            variant_vals = variant_id.read(
                                ['default_code', 'barcode'])[0]
                            self.wk_change_product_price(product_id=variant_id,
                                                         price=list_price,
                                                         channel_id=channel_id)
                            if qty_available and eval(qty_available) > 0:
                                self.wk_change_product_qty(
                                    variant_id, qty_available, location_id)
                            match = channel_id.match_product_mappings(
                                store_id,
                                'No Variants',
                                default_code=variant_vals.get('default_code'),
                                barcode=variant_vals.get('barcode'))
                            if not match:
                                channel_id.create_product_mapping(
                                    template_id, variant_id, store_id,
                                    'No Variants', {
                                        'default_code':
                                        variant_vals.get('default_code'),
                                        'barcode':
                                        variant_vals.get('barcode')
                                    })
                except Exception as e:
                    _logger.error('----------Exception------------%r', e)
                    message += '<br/>Error in variants %s' % (e)
                    state = 'error'
                if state == 'done':
                    template_id = template_id and template_id or template_exists_odoo
                    if template_id:
                        create_id = channel_id.create_template_mapping(
                            template_id, store_id, {
                                'default_code': vals.get('default_code'),
                                'barcode': vals.get('barcode')
                            })

            if state == 'done':
                message += '<br/> Product %s Successfully Evaluate' % (
                    vals.get('name', ''))
        self.set_feed_state(state=state)
        self.message = "%s <br/> %s" % (self.message, message)
        return dict(update_id=update_id, create_id=create_id, message=message)
Example #4
0
    def import_partner(self, channel_id):

        message = ""
        state = 'done'
        update_id = None
        create_id = None
        self.ensure_one()
        vals = EL(self.read(PartnerFields))
        _type = vals.get('type')
        store_id = vals.pop('store_id')
        vals.pop('website_message_ids', '')
        vals.pop('message_follower_ids', '')
        vals.pop('store_source', '')

        match = channel_id.match_partner_mappings(store_id, _type)
        name = vals.pop('name')
        if not name:
            message += "<br/>Partner without name can't evaluated."
            state = 'error'
        if not store_id:
            message += "<br/>Partner without store id can't evaluated."
            state = 'error'
        parent_store_id = vals['parent_id']
        if parent_store_id:
            partner_res = self.get_partner_id(parent_store_id,
                                              channel_id=channel_id)
            message += partner_res.get('message')
            partner_id = partner_res.get('partner_id')
            if partner_id:
                vals['parent_id'] = partner_id.id
            else:
                state = 'error'
        if state == 'done':
            country_id = vals.pop('country_id')
            if country_id:
                country_id = channel_id.get_country_id(country_id)
                if country_id:
                    vals['country_id'] = country_id.id
            state_id = vals.pop('state_id')
            state_name = vals.pop('state_name')

            if (state_id or state_name) and country_id:
                state_id = channel_id.get_state_id(state_id, country_id,
                                                   state_name)
                if state_id:
                    vals['state_id'] = state_id.id
            last_name = vals.pop('last_name', '')
            if last_name:
                vals['name'] = "%s %s" % (name, last_name)
            else:
                vals['name'] = name
        if match:
            if state == 'done':
                try:
                    match.odoo_partner.write(vals)
                    message += '<br/> Partner %s successfully updated' % (name)
                except Exception as e:
                    message += '<br/>%s' % (e)
                    state = 'error'
                update_id = match

            elif state == 'error':
                message += 'Error while partner updated.'

        else:
            if state == 'done':
                try:
                    erp_id = self.env['res.partner'].create(vals)
                    create_id = channel_id.create_partner_mapping(
                        erp_id, store_id, _type)
                    message += '<br/>Partner %s successfully evaluated.' % (
                        name)
                except Exception as e:
                    message += '<br/>%s' % (e)
                    state = 'error'
        self.set_feed_state(state=state)
        self.message = "%s <br/> %s" % (self.message, message)
        return dict(create_id=create_id, update_id=update_id, message=message)
Example #5
0
    def import_order(self, channel_id):
        message = ""
        update_id = None
        create_id = None
        self.ensure_one()
        vals = EL(self.read(self.get_order_fields()))
        store_id = vals.pop('store_id')

        store_source = vals.pop('store_source')
        match = channel_id.match_order_mappings(store_id)
        state = 'done'
        store_partner_id = vals.pop('partner_id')

        date_info = self.get_order_date_info(channel_id, vals)
        if date_info.get('date_order'):
            vals['date_order'] = date_info.get('date_order')
        date_invoice = date_info.get('date_invoice')
        confirmation_date = date_info.get('confirmation_date')

        if store_partner_id:
            if not match:
                res_partner = self.get_order_partner_id(
                    store_partner_id, channel_id)
                message += res_partner.get('message', '')
                partner_id = res_partner.get('partner_id')
                partner_invoice_id = res_partner.get('partner_invoice_id')
                partner_shipping_id = res_partner.get('partner_shipping_id')
                if partner_id and partner_invoice_id and partner_shipping_id:
                    vals['partner_id'] = partner_id.id
                    vals['partner_invoice_id'] = partner_invoice_id.id
                    vals['partner_shipping_id'] = partner_shipping_id.id
                else:
                    message += '<br/>Partner, Invoice, Shipping Address must present.'
                    state = 'error'
                    _logger.error('#OrderError1 %r' % message)
        else:
            message += '<br/>No partner in sale order data.'
            state = 'error'
            _logger.error('#OrderError2 %r' % message)

        if state == 'done':
            carrier_id = vals.pop('carrier_id', '')

            if carrier_id:
                carrier_res = self.get_carrier_id(carrier_id,
                                                  channel_id=channel_id)
                message += carrier_res.get('message')
                carrier_id = carrier_res.get('carrier_id')
                if carrier_id:
                    vals['carrier_id'] = carrier_id.id
            order_line_res = self._get_order_line_vals(vals, carrier_id,
                                                       channel_id)
            message += order_line_res.get('message', '')
            if not order_line_res.get('status'):
                state = 'error'
                _logger.error('#OrderError3 %r' % order_line_res)
            else:
                order_line = order_line_res.get('order_line')
                if len(order_line):
                    vals['order_line'] = order_line
                    state = 'done'
        currency = self.currency

        if state == 'done' and currency:
            currency_id = channel_id.get_currency_id(currency)
            if not currency_id:
                message += '<br/> Currency %s no active in Odoo' % (currency)
                state = 'error'
                _logger.error('#OrderError4 %r' % message)
            else:
                pricelist_id = channel_id.match_create_pricelist_id(
                    currency_id)
                vals['pricelist_id'] = pricelist_id.id

        vals.pop('name')
        vals.pop('id')
        vals.pop('website_message_ids', '')
        vals.pop('message_follower_ids', '')
        vals['team_id'] = channel_id.crm_team_id.id
        vals['warehouse_id'] = channel_id.warehouse_id.id

        if match and match.order_name:
            if state == 'done':
                try:

                    order_state = vals.pop('order_state')
                    if match.order_name.state == 'draft':
                        match.order_name.write(dict(order_line=[(5, 0)]))
                        match.order_name.write(vals)
                        message += '<br/> Order %s successfully updated' % (
                            vals.get('name', ''))
                    else:
                        message += 'Only order state can be update as order not in draft state.'
                    message += self.env[
                        'multi.channel.skeleton']._SetOdooOrderState(
                            match.order_name,
                            channel_id,
                            order_state,
                            self.payment_method,
                            date_invoice=date_invoice,
                            confirmation_date=confirmation_date)
                except Exception as e:
                    message += '<br/>%s' % (e)
                    _logger.error('#OrderError5  %r' % message)
                    state = 'error'
                update_id = match
            elif state == 'error':
                message += '<br/>Error while order update.'
        else:
            if state == 'done':
                try:
                    order_state = vals.pop('order_state')
                    erp_id = self.env['sale.order'].create(vals)
                    message += self.env[
                        'multi.channel.skeleton']._SetOdooOrderState(
                            erp_id,
                            channel_id,
                            order_state,
                            self.payment_method,
                            date_invoice=date_invoice,
                            confirmation_date=confirmation_date)
                    message += '<br/> Order %s successfully evaluated' % (
                        self.store_id)
                    create_id = channel_id.create_order_mapping(
                        erp_id, store_id, store_source)

                except Exception as e:
                    message += '<br/>%s' % (e)
                    _logger.error('#OrderError6 %r' % message)
                    state = 'error'
        self.set_feed_state(state=state)
        self.message = "%s <br/> %s" % (self.message, message)
        return dict(create_id=create_id, update_id=update_id, message=message)