Funding balance sheet changed.
authorRadek Czajka <rczajka@rczajka.pl>
Wed, 21 Feb 2024 14:14:57 +0000 (15:14 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Wed, 21 Feb 2024 14:14:57 +0000 (15:14 +0100)
src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py [new file with mode: 0644]
src/funding/migrations/0013_missing_spent.py [new file with mode: 0644]
src/funding/models.py
src/funding/templates/funding/wlfund.html
src/funding/views.py

diff --git a/src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py b/src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py
new file mode 100644 (file)
index 0000000..4277fb7
--- /dev/null
@@ -0,0 +1,30 @@
+# Generated by Django 4.0.8 on 2024-02-21 11:52
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0046_alter_book_options_alter_bookmedia_options_and_more'),
+        ('funding', '0011_alter_funding_customer_ip_alter_funding_notify_key_and_more'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='spent',
+            name='annotation',
+            field=models.CharField(blank=True, help_text="np. 'audiobook'", max_length=255, verbose_name='adnotacja'),
+        ),
+        migrations.AddField(
+            model_name='spent',
+            name='link',
+            field=models.URLField(blank=True, help_text='zamiast książki, np. kolekcja'),
+        ),
+        migrations.AlterField(
+            model_name='spent',
+            name='book',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='catalogue.book'),
+        ),
+    ]
diff --git a/src/funding/migrations/0013_missing_spent.py b/src/funding/migrations/0013_missing_spent.py
new file mode 100644 (file)
index 0000000..d7da1bf
--- /dev/null
@@ -0,0 +1,41 @@
+# Generated by Django 4.0.8 on 2024-02-21 11:55
+
+from django.db import migrations, models
+
+
+def populate_spent(apps, schema_editor):
+    Offer = apps.get_model('funding', 'Offer')
+    Spent = apps.get_model('funding', 'Spent')
+    Book = apps.get_model('catalogue', 'Book')
+    for o in Offer.objects.all():
+        if Spent.objects.filter(book__slug=o.slug).exists():
+            continue
+        s = o.funding_set.exclude(completed_at=None).aggregate(s=models.Sum('amount'))['s'] or 0
+        if s >= o.target:
+            try:
+                book = Book.objects.get(slug=o.slug)
+                link = ''
+            except Book.DoesNotExist:
+                book = None
+                link = o.slug
+            Spent.objects.create(
+                book=book,
+                link=link,
+                amount=o.target,
+                timestamp=o.end,
+                annotation='auto'
+            )
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('funding', '0012_spent_annotation_spent_link_alter_spent_book'),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            populate_spent,
+            migrations.RunPython.noop
+        )
+    ]
index 23afa3c..12fa0ca 100644 (file)
@@ -372,9 +372,11 @@ class PayUNotification(club.payu.models.Notification):
 
 class Spent(models.Model):
     """ Some of the remaining money spent on a book. """
-    book = models.ForeignKey(Book, models.PROTECT)
+    book = models.ForeignKey(Book, models.PROTECT, null=True, blank=True)
+    link = models.URLField(blank=True, help_text='zamiast książki, np. kolekcja')
     amount = models.DecimalField('kwota', decimal_places=2, max_digits=10)
     timestamp = models.DateField('kiedy')
+    annotation = models.CharField('adnotacja', max_length=255, blank=True, help_text="np. 'audiobook'")
 
     class Meta:
         verbose_name = 'pieniądze wydane na książkę'
index 5a1ba61..bff1092 100644 (file)
@@ -26,7 +26,8 @@
     <tr>
       <td>{% trans "Data" %}:</td>
       <td>{% trans "Rozdysponowanie środków" %}:</td>
-      <td>{% trans "Kwota" %}:</td>
+      <td>{% trans "Zebrane" %}:</td>
+      <td>{% trans "Wydane" %}:</td>
       <td>{% trans "Bilans" %}:</td>
     </tr>
 
         <tr class="funding-minus">
           <td class="oneline">{{ entry.timestamp }}</td>
           <td>
-            {% trans "Pieniądze przeznaczone na opublikowanie książki" %}:
-            <a href="{{ entry.book.get_absolute_url }}">{{ entry.book }}</a>
+            {% trans "Pieniądze przeznaczone na publikację" %}:
+            {% if entry.book %}
+              <a href="{{ entry.book.get_absolute_url }}">{{ entry.book }}</a>
+              {% if entry.annotation %}
+                ({{ entry.annotation }})
+              {% endif %}
+            {% else %}
+              <a href="{{ entry.link }}">{{ entry.annotation }}</a>
+            {% endif %}
           </td>
-          <td>-{{ entry.amount }}&nbsp;zł</td>
+          <td></td>
+          <td>{{ entry.amount }}&nbsp;zł</td>
           <td>{{ entry.total }}&nbsp;zł</td>
         </tr>
       {% else %}
         <tr class="funding-plus">
           <td class="oneline">{{ entry.end }}</td>
           <td>
-            {% trans "Pieniądze pozostałe ze zbiórki na" %}:
+            {% trans "Zbiórka" %}:
             <a href="{{ entry.get_absolute_url }}">{{ entry }}</a>
           </td>
-          <td>+{{ entry.wlfund }}&nbsp;zł</td>
+          <td>{{ entry.wlfund }}&nbsp;zł</td>
+          <td></td>
           <td>{{ entry.total }}&nbsp;zł</td>
         </tr>
       {% endif %}
index 9b43780..5be8c4b 100644 (file)
@@ -51,20 +51,15 @@ class WLFundView(TemplateView):
         offers = []
 
         for o in Offer.past():
-            if o.is_win():
-                o.wlfund = o.sum() - o.target
-                if o.wlfund > 0:
-                    offers.append(o)
-            else:
-                o.wlfund = o.sum()
-                if o.wlfund > 0:
-                    offers.append(o)
+            o.wlfund = o.sum()
+            if o.wlfund > 0:
+                offers.append(o)
         amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
 
         ctx['amount'] = amount
         ctx['log'] = add_total(amount, mix(
-            (offers, lambda x: x.end, 'offer'),
             (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'),
+            (offers, lambda x: x.end, 'offer'),
         ))
         return ctx