Esempio n. 1
0
    def get_item_line_data_using_amazon_data(cls, line_data):
        """
        Make data for an item line from the amazon data.

        :param order_items: Order items
        :return: List of data of order lines in required format
        """
        Uom = Pool().get('product.uom')
        Product = Pool().get('product.product')
        SaleLine = Pool().get('sale.line')

        unit, = Uom.search([('name', '=', 'Unit')])

        # Order lines are returned as dictionary for single record and as list
        # for mulitple reocrds.
        # So convert to list if its dictionary
        if isinstance(line_data, dict):
            # If its a single line order, then the array will be dict
            order_items = [line_data]
        else:
            # In case of multi line orders, the transaction array will be
            # a list of dictionaries
            order_items = line_data

        sale_lines = []
        for order_item in order_items:
            sale_lines.append(
                SaleLine(
                    description=order_item['Title']['value'],
                    unit_price=Decimal(
                        order_item['ItemPrice']['Amount']['value']
                    ),
                    unit=unit.id,
                    quantity=Decimal(order_item['QuantityOrdered']['value']),
                    product=Product.find_or_create_using_amazon_sku(
                        order_item['SellerSKU']['value'],
                    ).id
                )
            )

            if order_item['ShippingPrice']['Amount']['value']:
                sale_lines.append(
                    cls.get_shipping_line_data_using_amazon_data(order_item)
                )

        return sale_lines