def get_data(self): data = [] # group products by id - que queryset must be ordered by id to make this work for key, groups in itertools.groupby(self.get_objects(), lambda pl: pl.product_id): quantity = 0 taxful_total = TaxfulPrice(0, self.shop.currency) taxless_total = TaxlessPrice(0, self.shop.currency) product = None for order_line in groups: quantity += order_line.quantity taxful_total += order_line.taxful_price taxless_total += order_line.taxless_price if not product: product = order_line.product data.append({ "product": product.name, "sku": product.sku, "quantity": quantity, "taxful_total": taxful_total.as_rounded().value, "taxless_total": taxless_total.as_rounded().value, }) order_by = self.options.get("order_by") if order_by: data = sorted(data, key=itemgetter(order_by), reverse=True) return self.get_return_data(data)
def get_data(self): orders = self.get_objects().order_by("-order_date") data = [] # TODO: maybe make raw sql query in future for order_date, orders_group in itertools.groupby(orders, key=self.extract_date): taxless_total = TaxlessPrice(0, currency=self.shop.currency) taxful_total = TaxfulPrice(0, currency=self.shop.currency) product_count = 0 order_count = 0 for order in orders_group: taxless_total += order.taxless_total_price taxful_total += order.taxful_total_price product_count += sum(order.get_product_ids_and_quantities().values()) order_count += 1 data.append({ "date": format_date(order_date, locale=get_current_babel_locale()), "order_count": order_count, "product_count": int(product_count), "taxless_total": taxless_total.as_rounded().value, "taxful_total": taxful_total.as_rounded().value, }) return self.get_return_data(data)