We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Shift product sales are not calculated correctly
### Describe the bug
For some reason, the product sales for shifts in the sales app are not calculated properly:

### How to reproduce
1. Check staging.thalia.nu, shift 1
2. 
<img width="453" alt="image" src="https://user-images.githubusercontent.com/7915741/123234193-06af2500-d4db-11eb-99b9-a8be74602c1a.png">
3. It should be waaaaay more as there are 200+ orders

### Expected behaviour
Correct calculation


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `website/sales/models/shift.py`
Content:
```
1 from django.core.exceptions import ValidationError
2 from django.db import models
3 from django.db.models import (
4     Sum,
5     Q,
6     Count,
7 )
8 from django.db.models.expressions import RawSQL
9 from django.utils import timezone
10 from django.utils.translation import gettext_lazy as _
11 from queryable_properties.managers import QueryablePropertiesManager
12 from queryable_properties.properties import (
13     RangeCheckProperty,
14     queryable_property,
15 )
16 
17 from activemembers.models import MemberGroup
18 from sales.models.product import ProductList
19 
20 
21 class Shift(models.Model):
22     class Meta:
23         permissions = [
24             ("override_manager", _("Can access all shifts as manager")),
25         ]
26 
27     objects = QueryablePropertiesManager()
28 
29     start = models.DateTimeField(verbose_name=_("start"), blank=False, null=False,)
30     end = models.DateTimeField(
31         verbose_name=_("end"),
32         blank=False,
33         null=False,
34         help_text=_(
35             "The end time is only indicative and does not prevent orders being created after the shift has ended. This only happens after locking the shift."
36         ),
37     )
38 
39     title = models.CharField(
40         verbose_name=_("title"), blank=True, null=True, max_length=100
41     )
42 
43     product_list = models.ForeignKey(
44         ProductList,
45         verbose_name=_("product list"),
46         blank=False,
47         null=False,
48         on_delete=models.PROTECT,
49     )
50 
51     managers = models.ManyToManyField(
52         MemberGroup, verbose_name=_("managers"), related_name="manager_shifts"
53     )
54 
55     locked = models.BooleanField(
56         verbose_name=_("locked"),
57         blank=False,
58         null=False,
59         default=False,
60         help_text=_(
61             "Prevent orders being changed or created for this shift. This will also clean up all unpaid orders in this shift."
62         ),
63     )
64 
65     def clean(self):
66         super().clean()
67         errors = {}
68 
69         if self.orders.filter(created_at__lt=self.start):
70             errors.update(
71                 {
72                     "start": _(
73                         "There are already orders created in this shift before this start time."
74                     )
75                 }
76             )
77 
78         if self.end and self.start and self.end <= self.start:
79             errors.update({"end": _("End cannot be before start.")})
80 
81         if errors:
82             raise ValidationError(errors)
83 
84     def save(
85         self, force_insert=False, force_update=False, using=None, update_fields=None
86     ):
87         if self.locked:
88             self.orders.filter(
89                 (Q(payment__isnull=True) & Q(total_amount__gt=0))
90                 | Q(order_items__isnull=True)
91             ).delete()
92 
93         return super(Shift, self).save(force_insert, force_update, using, update_fields)
94 
95     active = RangeCheckProperty("start", "end", timezone.now)
96 
97     @queryable_property(annotation_based=True)
98     @classmethod
99     def total_revenue(cls):
100         return RawSQL(
101             """(SELECT CAST(COALESCE(SUM("__orders"."total__"), 0) AS NUMERIC) AS "shift_revenue__"
102                 FROM (
103                     SELECT "sales_order"."id", "sales_order"."shift_id", "sales_order"."discount", "sales_order"."payment_id", CAST(SUM("sales_orderitem"."total") AS NUMERIC) AS "subtotal__", CAST((SUM("sales_orderitem"."total") - COALESCE("sales_order"."discount", 0)) AS NUMERIC) AS "total__", SUM("sales_orderitem"."amount") AS "num_items__"
104                     FROM "sales_order" LEFT JOIN "sales_orderitem" ON "sales_orderitem"."order_id" = "sales_order"."id"
105                     GROUP BY "sales_order"."id", "sales_order"."shift_id", "sales_order"."discount"
106                     ) AS "__orders"
107                 WHERE "__orders"."shift_id"="sales_shift"."id"
108                 )""",
109             [],
110         )
111 
112     @queryable_property(annotation_based=True)
113     @classmethod
114     def total_revenue_paid(cls):
115         return RawSQL(
116             """(SELECT CAST(COALESCE(SUM("__orders"."total__"), 0) AS NUMERIC) AS "shift_revenue__"
117                 FROM (
118                     SELECT "sales_order"."id", "sales_order"."shift_id", "sales_order"."discount", "sales_order"."payment_id", CAST(SUM("sales_orderitem"."total") AS NUMERIC) AS "subtotal__", CAST((SUM("sales_orderitem"."total") - COALESCE("sales_order"."discount", 0)) AS NUMERIC) AS "total__", SUM("sales_orderitem"."amount") AS "num_items__"
119                     FROM "sales_order" LEFT JOIN "sales_orderitem" ON "sales_orderitem"."order_id" = "sales_order"."id"
120                     GROUP BY "sales_order"."id", "sales_order"."shift_id", "sales_order"."discount"
121                     ) AS "__orders"
122                 WHERE "__orders"."shift_id"="sales_shift"."id"
123                 AND ("__orders"."payment_id" IS NOT NULL OR ("__orders"."payment_id" IS NULL AND "__orders"."total__"=0))
124                 )""",
125             [],
126         )
127 
128     @queryable_property(annotation_based=True)
129     @classmethod
130     def num_orders(cls):
131         return Count("orders")
132 
133     @queryable_property(annotation_based=True)
134     @classmethod
135     def num_orders_paid(cls):
136         return RawSQL(
137             """(SELECT COUNT(*) AS "num_orders__"
138                 FROM (
139                     SELECT "sales_order"."id", "sales_order"."shift_id", "sales_order"."discount", "sales_order"."payment_id", CAST(SUM("sales_orderitem"."total") AS NUMERIC) AS "subtotal__", CAST((SUM("sales_orderitem"."total") - COALESCE("sales_order"."discount", 0)) AS NUMERIC) AS "total__", SUM("sales_orderitem"."amount") AS "num_items__"
140                     FROM "sales_order" LEFT JOIN "sales_orderitem" ON "sales_orderitem"."order_id" = "sales_order"."id"
141                     GROUP BY "sales_order"."id", "sales_order"."shift_id", "sales_order"."discount"
142                     ) AS "__orders"
143                 WHERE "__orders"."shift_id"="sales_shift"."id"
144                 AND ("__orders"."payment_id" IS NOT NULL OR ("__orders"."payment_id" IS NULL AND "__orders"."total__"=0))
145                 )""",
146             [],
147         )
148 
149     @property
150     def product_sales(self):
151         qs = (
152             self.orders.exclude(order_items__isnull=True)
153             .values("order_items__product")
154             .distinct()
155             .annotate(sold=Sum("order_items__amount"))
156         )
157         return {
158             item[0]: item[1]
159             for item in qs.values_list("order_items__product__product__name", "sold")
160         }
161 
162     def __str__(self):
163         if self.title and self.title != "":
164             return f"Shift {self.pk} - {self.title}"
165         return f"Shift {self.pk}"
166
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/website/sales/models/shift.py b/website/sales/models/shift.py
--- a/website/sales/models/shift.py
+++ b/website/sales/models/shift.py
@@ -151,8 +151,8 @@
         qs = (
             self.orders.exclude(order_items__isnull=True)
             .values("order_items__product")
-            .distinct()
             .annotate(sold=Sum("order_items__amount"))
+            .order_by()
         )
         return {
             item[0]: item[1]

{"golden_diff": "diff --git a/website/sales/models/shift.py b/website/sales/models/shift.py\n--- a/website/sales/models/shift.py\n+++ b/website/sales/models/shift.py\n@@ -151,8 +151,8 @@\n         qs = (\n             self.orders.exclude(order_items__isnull=True)\n             .values(\"order_items__product\")\n-            .distinct()\n             .annotate(sold=Sum(\"order_items__amount\"))\n+            .order_by()\n         )\n         return {\n             item[0]: item[1]\n", "issue": "Shift product sales are not calculated correctly\n### Describe the bug\r\nFor some reason, the product sales for shifts in the sales app are not calculated properly:\r\n\r\n### How to reproduce\r\n1. Check staging.thalia.nu, shift 1\r\n2. \r\n<img width=\"453\" alt=\"image\" src=\"https://user-images.githubusercontent.com/7915741/123234193-06af2500-d4db-11eb-99b9-a8be74602c1a.png\">\r\n3. It should be waaaaay more as there are 200+ orders\r\n\r\n### Expected behaviour\r\nCorrect calculation\r\n\n", "before_files": [{"content": "from django.core.exceptions import ValidationError\nfrom django.db import models\nfrom django.db.models import (\n    Sum,\n    Q,\n    Count,\n)\nfrom django.db.models.expressions import RawSQL\nfrom django.utils import timezone\nfrom django.utils.translation import gettext_lazy as _\nfrom queryable_properties.managers import QueryablePropertiesManager\nfrom queryable_properties.properties import (\n    RangeCheckProperty,\n    queryable_property,\n)\n\nfrom activemembers.models import MemberGroup\nfrom sales.models.product import ProductList\n\n\nclass Shift(models.Model):\n    class Meta:\n        permissions = [\n            (\"override_manager\", _(\"Can access all shifts as manager\")),\n        ]\n\n    objects = QueryablePropertiesManager()\n\n    start = models.DateTimeField(verbose_name=_(\"start\"), blank=False, null=False,)\n    end = models.DateTimeField(\n        verbose_name=_(\"end\"),\n        blank=False,\n        null=False,\n        help_text=_(\n            \"The end time is only indicative and does not prevent orders being created after the shift has ended. This only happens after locking the shift.\"\n        ),\n    )\n\n    title = models.CharField(\n        verbose_name=_(\"title\"), blank=True, null=True, max_length=100\n    )\n\n    product_list = models.ForeignKey(\n        ProductList,\n        verbose_name=_(\"product list\"),\n        blank=False,\n        null=False,\n        on_delete=models.PROTECT,\n    )\n\n    managers = models.ManyToManyField(\n        MemberGroup, verbose_name=_(\"managers\"), related_name=\"manager_shifts\"\n    )\n\n    locked = models.BooleanField(\n        verbose_name=_(\"locked\"),\n        blank=False,\n        null=False,\n        default=False,\n        help_text=_(\n            \"Prevent orders being changed or created for this shift. This will also clean up all unpaid orders in this shift.\"\n        ),\n    )\n\n    def clean(self):\n        super().clean()\n        errors = {}\n\n        if self.orders.filter(created_at__lt=self.start):\n            errors.update(\n                {\n                    \"start\": _(\n                        \"There are already orders created in this shift before this start time.\"\n                    )\n                }\n            )\n\n        if self.end and self.start and self.end <= self.start:\n            errors.update({\"end\": _(\"End cannot be before start.\")})\n\n        if errors:\n            raise ValidationError(errors)\n\n    def save(\n        self, force_insert=False, force_update=False, using=None, update_fields=None\n    ):\n        if self.locked:\n            self.orders.filter(\n                (Q(payment__isnull=True) & Q(total_amount__gt=0))\n                | Q(order_items__isnull=True)\n            ).delete()\n\n        return super(Shift, self).save(force_insert, force_update, using, update_fields)\n\n    active = RangeCheckProperty(\"start\", \"end\", timezone.now)\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def total_revenue(cls):\n        return RawSQL(\n            \"\"\"(SELECT CAST(COALESCE(SUM(\"__orders\".\"total__\"), 0) AS NUMERIC) AS \"shift_revenue__\"\n                FROM (\n                    SELECT \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\", \"sales_order\".\"payment_id\", CAST(SUM(\"sales_orderitem\".\"total\") AS NUMERIC) AS \"subtotal__\", CAST((SUM(\"sales_orderitem\".\"total\") - COALESCE(\"sales_order\".\"discount\", 0)) AS NUMERIC) AS \"total__\", SUM(\"sales_orderitem\".\"amount\") AS \"num_items__\"\n                    FROM \"sales_order\" LEFT JOIN \"sales_orderitem\" ON \"sales_orderitem\".\"order_id\" = \"sales_order\".\"id\"\n                    GROUP BY \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\"\n                    ) AS \"__orders\"\n                WHERE \"__orders\".\"shift_id\"=\"sales_shift\".\"id\"\n                )\"\"\",\n            [],\n        )\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def total_revenue_paid(cls):\n        return RawSQL(\n            \"\"\"(SELECT CAST(COALESCE(SUM(\"__orders\".\"total__\"), 0) AS NUMERIC) AS \"shift_revenue__\"\n                FROM (\n                    SELECT \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\", \"sales_order\".\"payment_id\", CAST(SUM(\"sales_orderitem\".\"total\") AS NUMERIC) AS \"subtotal__\", CAST((SUM(\"sales_orderitem\".\"total\") - COALESCE(\"sales_order\".\"discount\", 0)) AS NUMERIC) AS \"total__\", SUM(\"sales_orderitem\".\"amount\") AS \"num_items__\"\n                    FROM \"sales_order\" LEFT JOIN \"sales_orderitem\" ON \"sales_orderitem\".\"order_id\" = \"sales_order\".\"id\"\n                    GROUP BY \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\"\n                    ) AS \"__orders\"\n                WHERE \"__orders\".\"shift_id\"=\"sales_shift\".\"id\"\n                AND (\"__orders\".\"payment_id\" IS NOT NULL OR (\"__orders\".\"payment_id\" IS NULL AND \"__orders\".\"total__\"=0))\n                )\"\"\",\n            [],\n        )\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def num_orders(cls):\n        return Count(\"orders\")\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def num_orders_paid(cls):\n        return RawSQL(\n            \"\"\"(SELECT COUNT(*) AS \"num_orders__\"\n                FROM (\n                    SELECT \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\", \"sales_order\".\"payment_id\", CAST(SUM(\"sales_orderitem\".\"total\") AS NUMERIC) AS \"subtotal__\", CAST((SUM(\"sales_orderitem\".\"total\") - COALESCE(\"sales_order\".\"discount\", 0)) AS NUMERIC) AS \"total__\", SUM(\"sales_orderitem\".\"amount\") AS \"num_items__\"\n                    FROM \"sales_order\" LEFT JOIN \"sales_orderitem\" ON \"sales_orderitem\".\"order_id\" = \"sales_order\".\"id\"\n                    GROUP BY \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\"\n                    ) AS \"__orders\"\n                WHERE \"__orders\".\"shift_id\"=\"sales_shift\".\"id\"\n                AND (\"__orders\".\"payment_id\" IS NOT NULL OR (\"__orders\".\"payment_id\" IS NULL AND \"__orders\".\"total__\"=0))\n                )\"\"\",\n            [],\n        )\n\n    @property\n    def product_sales(self):\n        qs = (\n            self.orders.exclude(order_items__isnull=True)\n            .values(\"order_items__product\")\n            .distinct()\n            .annotate(sold=Sum(\"order_items__amount\"))\n        )\n        return {\n            item[0]: item[1]\n            for item in qs.values_list(\"order_items__product__product__name\", \"sold\")\n        }\n\n    def __str__(self):\n        if self.title and self.title != \"\":\n            return f\"Shift {self.pk} - {self.title}\"\n        return f\"Shift {self.pk}\"\n", "path": "website/sales/models/shift.py"}], "after_files": [{"content": "from django.core.exceptions import ValidationError\nfrom django.db import models\nfrom django.db.models import (\n    Sum,\n    Q,\n    Count,\n)\nfrom django.db.models.expressions import RawSQL\nfrom django.utils import timezone\nfrom django.utils.translation import gettext_lazy as _\nfrom queryable_properties.managers import QueryablePropertiesManager\nfrom queryable_properties.properties import (\n    RangeCheckProperty,\n    queryable_property,\n)\n\nfrom activemembers.models import MemberGroup\nfrom sales.models.product import ProductList\n\n\nclass Shift(models.Model):\n    class Meta:\n        permissions = [\n            (\"override_manager\", _(\"Can access all shifts as manager\")),\n        ]\n\n    objects = QueryablePropertiesManager()\n\n    start = models.DateTimeField(verbose_name=_(\"start\"), blank=False, null=False,)\n    end = models.DateTimeField(\n        verbose_name=_(\"end\"),\n        blank=False,\n        null=False,\n        help_text=_(\n            \"The end time is only indicative and does not prevent orders being created after the shift has ended. This only happens after locking the shift.\"\n        ),\n    )\n\n    title = models.CharField(\n        verbose_name=_(\"title\"), blank=True, null=True, max_length=100\n    )\n\n    product_list = models.ForeignKey(\n        ProductList,\n        verbose_name=_(\"product list\"),\n        blank=False,\n        null=False,\n        on_delete=models.PROTECT,\n    )\n\n    managers = models.ManyToManyField(\n        MemberGroup, verbose_name=_(\"managers\"), related_name=\"manager_shifts\"\n    )\n\n    locked = models.BooleanField(\n        verbose_name=_(\"locked\"),\n        blank=False,\n        null=False,\n        default=False,\n        help_text=_(\n            \"Prevent orders being changed or created for this shift. This will also clean up all unpaid orders in this shift.\"\n        ),\n    )\n\n    def clean(self):\n        super().clean()\n        errors = {}\n\n        if self.orders.filter(created_at__lt=self.start):\n            errors.update(\n                {\n                    \"start\": _(\n                        \"There are already orders created in this shift before this start time.\"\n                    )\n                }\n            )\n\n        if self.end and self.start and self.end <= self.start:\n            errors.update({\"end\": _(\"End cannot be before start.\")})\n\n        if errors:\n            raise ValidationError(errors)\n\n    def save(\n        self, force_insert=False, force_update=False, using=None, update_fields=None\n    ):\n        if self.locked:\n            self.orders.filter(\n                (Q(payment__isnull=True) & Q(total_amount__gt=0))\n                | Q(order_items__isnull=True)\n            ).delete()\n\n        return super(Shift, self).save(force_insert, force_update, using, update_fields)\n\n    active = RangeCheckProperty(\"start\", \"end\", timezone.now)\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def total_revenue(cls):\n        return RawSQL(\n            \"\"\"(SELECT CAST(COALESCE(SUM(\"__orders\".\"total__\"), 0) AS NUMERIC) AS \"shift_revenue__\"\n                FROM (\n                    SELECT \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\", \"sales_order\".\"payment_id\", CAST(SUM(\"sales_orderitem\".\"total\") AS NUMERIC) AS \"subtotal__\", CAST((SUM(\"sales_orderitem\".\"total\") - COALESCE(\"sales_order\".\"discount\", 0)) AS NUMERIC) AS \"total__\", SUM(\"sales_orderitem\".\"amount\") AS \"num_items__\"\n                    FROM \"sales_order\" LEFT JOIN \"sales_orderitem\" ON \"sales_orderitem\".\"order_id\" = \"sales_order\".\"id\"\n                    GROUP BY \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\"\n                    ) AS \"__orders\"\n                WHERE \"__orders\".\"shift_id\"=\"sales_shift\".\"id\"\n                )\"\"\",\n            [],\n        )\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def total_revenue_paid(cls):\n        return RawSQL(\n            \"\"\"(SELECT CAST(COALESCE(SUM(\"__orders\".\"total__\"), 0) AS NUMERIC) AS \"shift_revenue__\"\n                FROM (\n                    SELECT \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\", \"sales_order\".\"payment_id\", CAST(SUM(\"sales_orderitem\".\"total\") AS NUMERIC) AS \"subtotal__\", CAST((SUM(\"sales_orderitem\".\"total\") - COALESCE(\"sales_order\".\"discount\", 0)) AS NUMERIC) AS \"total__\", SUM(\"sales_orderitem\".\"amount\") AS \"num_items__\"\n                    FROM \"sales_order\" LEFT JOIN \"sales_orderitem\" ON \"sales_orderitem\".\"order_id\" = \"sales_order\".\"id\"\n                    GROUP BY \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\"\n                    ) AS \"__orders\"\n                WHERE \"__orders\".\"shift_id\"=\"sales_shift\".\"id\"\n                AND (\"__orders\".\"payment_id\" IS NOT NULL OR (\"__orders\".\"payment_id\" IS NULL AND \"__orders\".\"total__\"=0))\n                )\"\"\",\n            [],\n        )\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def num_orders(cls):\n        return Count(\"orders\")\n\n    @queryable_property(annotation_based=True)\n    @classmethod\n    def num_orders_paid(cls):\n        return RawSQL(\n            \"\"\"(SELECT COUNT(*) AS \"num_orders__\"\n                FROM (\n                    SELECT \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\", \"sales_order\".\"payment_id\", CAST(SUM(\"sales_orderitem\".\"total\") AS NUMERIC) AS \"subtotal__\", CAST((SUM(\"sales_orderitem\".\"total\") - COALESCE(\"sales_order\".\"discount\", 0)) AS NUMERIC) AS \"total__\", SUM(\"sales_orderitem\".\"amount\") AS \"num_items__\"\n                    FROM \"sales_order\" LEFT JOIN \"sales_orderitem\" ON \"sales_orderitem\".\"order_id\" = \"sales_order\".\"id\"\n                    GROUP BY \"sales_order\".\"id\", \"sales_order\".\"shift_id\", \"sales_order\".\"discount\"\n                    ) AS \"__orders\"\n                WHERE \"__orders\".\"shift_id\"=\"sales_shift\".\"id\"\n                AND (\"__orders\".\"payment_id\" IS NOT NULL OR (\"__orders\".\"payment_id\" IS NULL AND \"__orders\".\"total__\"=0))\n                )\"\"\",\n            [],\n        )\n\n    @property\n    def product_sales(self):\n        qs = (\n            self.orders.exclude(order_items__isnull=True)\n            .values(\"order_items__product\")\n            .annotate(sold=Sum(\"order_items__amount\"))\n            .order_by()\n        )\n        return {\n            item[0]: item[1]\n            for item in qs.values_list(\"order_items__product__product__name\", \"sold\")\n        }\n\n    def __str__(self):\n        if self.title and self.title != \"\":\n            return f\"Shift {self.pk} - {self.title}\"\n        return f\"Shift {self.pk}\"\n", "path": "website/sales/models/shift.py"}]}