Esempio n. 1
0
    def get_sale_line_using_magento_data(self, item):
        """
        Get sale.line data from magento data.
        """
        SaleLine = Pool().get('sale.line')
        ProductTemplate = Pool().get('product.template')
        MagentoException = Pool().get('magento.exception')
        Uom = Pool().get('product.uom')
        StoreView = Pool().get('magento.store.store_view')

        sale_line = None
        unit, = Uom.search([('name', '=', 'Unit')])
        if not item['parent_item_id']:
            # If its a top level product, create it
            try:
                product = ProductTemplate.find_or_create_using_magento_id(
                    item['product_id'],
                ).products[0]
            except xmlrpclib.Fault, exception:
                if exception.faultCode == 101:
                    # Case when product doesnot exist on magento
                    # create magento exception
                    MagentoException.create([{
                        'origin': '%s,%s' % (self.__name__, self.id),
                        'log': "Product #%s does not exist" %
                            item['product_id']
                    }])
                    product = None
                    self.has_magento_exception = True
                else:
                    raise
            sale_line = SaleLine(**{
                'sale': self.id,
                'magento_id': int(item['item_id']),
                'description': item['name'] or product.name,
                'unit_price': Decimal(item['price']),
                'unit': unit.id,
                'quantity': Decimal(item['qty_ordered']),
                'note': item.get('comments'),
                'product': product,
            })
            if item.get('tax_percent') and Decimal(item.get('tax_percent')):
                store_view = StoreView.get_current_store_view()
                taxes = store_view.get_taxes(
                    Decimal(item['tax_percent']) / 100
                )
                sale_line.taxes = taxes