Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#228 sponsorship categories #240

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
17 changes: 13 additions & 4 deletions website/events/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from events.helpers.notifications import email_notifier
from events.models import (
Event,
SponsorshipDiscounts,
EventOrganizer,
Invoice,
InvoiceAffect,
Expand All @@ -20,6 +21,11 @@
from reversion_compare.admin import CompareVersionAdmin


class SponsorshipDiscountsInline(admin.TabularInline):
model = SponsorshipDiscounts
exclude = ('created_by', 'changed_by',)


class EventOrganizerInline(admin.TabularInline):
model = EventOrganizer
exclude = ('created_by', 'changed_by',)
Expand All @@ -28,7 +34,7 @@ class EventOrganizerInline(admin.TabularInline):

class EventAdmin(CompareVersionAdmin):
fields = ('name', 'commission', 'category', 'start_date', 'place', 'close')
inlines = (EventOrganizerInline,)
inlines = (SponsorshipDiscountsInline, EventOrganizerInline)
list_display = ('name', 'start_date', 'place', 'category', 'close')
search_fields = ('start_date', 'place', )
list_filter = ('category', 'close')
Expand All @@ -43,9 +49,7 @@ def save_formset(self, request, form, formset, change):
event=instance.event, organizer=instance.organizer).exists()
if not exists_organizer:
notify_organizers.append(instance.organizer)

super(EventAdmin, self).save_formset(request, form, formset, change)

if len(notify_organizers):
current_site = get_current_site(request)
context = {
Expand Down Expand Up @@ -104,7 +108,7 @@ def sponsor(self, obj):


class SponsorCategoryAdmin(CompareVersionAdmin):
fields = ('name', 'amount', 'event')
fields = ('name', 'amount', 'event', 'sponsorship_discount')
list_display = ('name', 'amount', 'event', 'sponsoring_number')
search_fields = ('name', 'event__name')
list_select_related = (
Expand Down Expand Up @@ -292,6 +296,11 @@ class ProviderAdmin(CompareVersionAdmin):
list_display = ('organization_name', 'document_number')


class SponsorshipDiscountsAdmin(CompareVersionAdmin):
exclude = []


admin.site.register(SponsorshipDiscounts, SponsorshipDiscountsAdmin)
admin.site.register(Provider, ProviderAdmin)
admin.site.register(Payment, PaymentAdmin)
admin.site.register(ProviderExpense, ProviderExpenseAdmin)
Expand Down
20 changes: 19 additions & 1 deletion website/events/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
ProviderExpense,
Sponsor,
SponsorCategory,
Sponsoring
Sponsoring,
SponsorshipDiscounts,
)


Expand Down Expand Up @@ -117,6 +118,20 @@ class Meta:
fields = ['name', 'amount']


class SponsorshipDiscountForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SponsorshipDiscountForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.form_tag = False
self.helper.label_class = "col-sm-2"
self.helper.field_class = "col-sm-10"

class Meta:
model = SponsorshipDiscounts
fields = ['name', 'description', 'discount', 'event']


class BankAccountDataForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BankAccountDataForm, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -176,12 +191,15 @@ def __init__(self, event, *args, **kwargs):
# Pre-filter sponsorcategory by event
self.fields['sponsorcategory'].queryset = SponsorCategory.objects.filter(event=event)
self.fields['sponsor'].queryset = Sponsor.objects.filter(enabled=True)
self.fields['sponsorship_discount'].queryset = \
SponsorshipDiscounts.objects.filter(event=event)

class Meta:
model = Sponsoring
fields = [
'sponsorcategory',
'sponsor',
'sponsorship_discount',
'comments',
]
widgets = {
Expand Down
1 change: 1 addition & 0 deletions website/events/helpers/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class EmailNotification():
ORGANIZER_ASSOCIATED_TO_EVENT = 'organizer_associated_to_event'
SPONSOR_JUST_CREATED = 'sponsor_just_created'

INVOICE_JUST_CREATED = 'invoice_just_created'
INVOICE_AFFECT_JUST_CREATED = 'invoice_affect_just_created'
SPONSOR_JUST_ENABLED = 'sponsor_just_enabled'
Expand Down
32 changes: 32 additions & 0 deletions website/events/migrations/0013_auto_20211122_1838.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.1.13 on 2021-11-22 18:38

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('events', '0012_auto_20211122_1550'),
]

operations = [
migrations.CreateModel(
name='SponsorshipDiscounts',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=32, verbose_name='nombre')),
('description', models.TextField(blank=True, null=True, verbose_name='descripción')),
('discount', models.DecimalField(decimal_places=2, max_digits=18, verbose_name='descuento (%)')),
('event', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='discount', to='events.event')),
],
options={
'verbose_name_plural': 'Sponsorship Discounts',
},
),
migrations.AddField(
model_name='sponsoring',
name='sponsorship_discount',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='events.sponsorshipdiscounts', verbose_name='Descuento'),
),
]
35 changes: 34 additions & 1 deletion website/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ class Event(SaveReversionMixin, AuditUserTime):
verbose_name=_('organizadores'),
related_name='events'
)

close = models.BooleanField(_('cerrado'), default=False)

def get_absolute_url(self):
Expand All @@ -213,6 +212,25 @@ class Meta:
ordering = ['-start_date']


class SponsorshipDiscounts(models.Model):
"""SponsorShip Discount."""

class Meta:
verbose_name_plural = "Sponsorship Discounts"

name = models.CharField(_('nombre'), max_length=32)
description = models.TextField(_('descripción'), null=True, blank=True)
discount = models.DecimalField(_('descuento (%)'), max_digits=18, decimal_places=2)
event = models.ForeignKey(
Event,
related_name='discount',
on_delete=models.SET_NULL,
null=True)

def __str__(self):
return self.name


@reversion.register
class EventOrganizer(SaveReversionMixin, AuditUserTime):
"""Represents the many to many relationship between events and organizers. With TimeStamped
Expand Down Expand Up @@ -270,9 +288,24 @@ class Sponsoring(SaveReversionMixin, AuditUserTime):
related_name='sponsoring',
on_delete=models.CASCADE,
)
sponsorship_discount = models.ForeignKey(
'SponsorshipDiscounts',
verbose_name=_('Descuento'),
on_delete=models.SET_NULL,
null=True,
blank=True,
)
comments = models.TextField(_('comentarios'), blank=True)
close = models.BooleanField(_('cerrado'), default=False)

@property
def total_amount(self):
if self.sponsorship_discount:
amount = self.sponsorcategory.amount * (1 - self.sponsorship_discount.discount / 100)
else:
amount = self.sponsorcategory.amount
return format(amount, '.2f')

def __str__(self):
return (
f"{self.sponsor.organization_name} "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "events_base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
{% block route-navigator %}
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><i class="fas fa-home"></i><a href="{% url 'events_home' %}"> Inicio</a></li>
<li class="breadcrumb-item"><i class="fas fa-calendar-alt"></i><a href="{% url 'event_list' %}"> Eventos</a></li>
<li class="breadcrumb-item"><i class="fas fa-calendar-check"></i><a href="{% url 'event_detail' pk=event.pk %}"> {{event.name}}</a></li>
<li class="breadcrumb-item" aria-current="page"><i class="fas fa-wallet"></i> Crear</li>
</ol>
</nav>
{% endblock %}
<h3>Descuentos</h3>
<form method="POST">
{% csrf_token %}
{% crispy form %}
<button type="submit" class="btn btn-success col-2 float-right mx-1">
<i class="fas fa-save"></i>
Crear
</button>
<a class="btn btn-danger col-2 float-right mx-1" href="{{request.META.HTTP_REFERER}}"><i class="fas fa-undo"></i> Volver</a>
</form>
</div>
{% endblock %}
22 changes: 21 additions & 1 deletion website/events/templates/events/event_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,27 @@ <h3>Organizadores <small><span class="badge badge-dark">{{organizers.count}}</sp

{% endif %}
{% if perms.events.add_eventorganizer %}
<a class="btn btn-primary btn-block text-uppercase" href="{% url 'admin:events_event_change' event.pk %}" target="_blank"><i class="fas fa-user-plus"></i>Vincular Organizadores</a>
<a class="btn btn-primary btn-block text-uppercase" href="{% url 'admin:events_event_change' event.pk %}" target="_blank"><i class="fas fa-user-plus"></i> Vincular Organizadores</a>
{% endif %}
{% endif %}

{% if perms.events.view_event_organizers %}
<br>
<h3>Descuentos <small><span class="badge badge-dark">{{sponsorship_discount.count}}</span></small></h3>
{% if sponsorship_discount %}
<ul>
{% for discount in sponsorship_discount %}
<li>{{discount.name}} ({{discount.discount}}%)</li>
{% endfor %}
</ul>
{% else %}
<div class="alert alert-info" role="alert">
No hay descuentos asociados al evento!
</div>
{% endif %}

{% if perms.events.add_sponsorship_discount %}
<a class="btn btn-primary btn-block text-uppercase" href="{% url 'event_create_sponsor_discount' event.pk %}" target="_blank"><i class="fas fa-ticket-alt"></i> Crear Descuento</a>
{% endif %}
{% endif %}
</div>
Expand Down
18 changes: 15 additions & 3 deletions website/events/templates/events/sponsorings/sponsoring_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@
{% endblock %}

<h2>Sponsor {{sponsoring.sponsor.organization_name}}</h2>
<h2><small class="text-muted">Categoría: {{sponsoring.sponsorcategory.name}} (${{sponsoring.sponsorcategory.amount|intcomma}}) [{{sponsoring.state}}]</small></h2>
{% if sponsoring.sponsorship_discount.discount %}
<h2><small class="text-muted">Categoría: {{sponsoring.sponsorcategory.name}} (${{sponsoring.sponsorcategory.amount|intcomma}}) - Descuento: {{sponsoring.sponsorship_discount.discount}}% - Total: {{total_reminder}} ${{ sponsoring.total_amount|intcomma }}
{% else %}
<h2><small class="text-muted">Categoría: {{sponsoring.sponsorcategory.name}} (${{sponsoring.sponsorcategory.amount|intcomma}}) - Total: ${{ sponsoring.sponsorcategory.amount|intcomma }}
{% endif %}
<br>
{% if sponsoring.state == "no facturado" %}
<span class="text-danger">[{{sponsoring.state}}]</span>
{% else %}
<span class="text-success">[{{sponsoring.state}}]</span>
{% endif %}
</small></h2>

<h3><small class="text-muted">Última modificación: {{sponsoring.modified}}</small></h3>
<h3>Detalles <button class="btn btn-info col-2 float-right" data-toggle="collapse" href="#EventAndSponsorDetails" aria-expanded="false" aria-controls="collapseExample" id="detailButton"><i class="fa" aria-hidden="true"></i> Ver</button></h3>
{% if sponsoring.state == "no facturado" %}
Expand Down Expand Up @@ -263,7 +275,7 @@ <h5 class="modal-title" id="NewSponsoringInvoiceAffectLabel"><i class="fas fa-re
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="NewSponsoringInvoiceLabel"><i class="fas fa-file-invoice-dollar"></i> Factura <span class="text-muted">{{sponsoring.sponsor.organization_name}} ({{sponsoring.sponsorcategory.name}} <small>${{sponsoring.sponsorcategory.amount|intcomma}}</small>) @ {{event.name}} [#{{sponsoring.pk}}]</span></h5>
<h5 class="modal-title" id="NewSponsoringInvoiceLabel"><i class="fas fa-file-invoice-dollar"></i> Factura <span class="text-muted">{{sponsoring.sponsor.organization_name}} ({{sponsoring.sponsorcategory.name}} <small>${{sponsoring.total_amount|intcomma}}</small>) @ {{event.name}} [#{{sponsoring.pk}}]</span></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
Expand All @@ -274,7 +286,7 @@ <h5 class="modal-title" id="NewSponsoringInvoiceLabel"><i class="fas fa-file-inv
<div class="form-group row">
<label for="amount" class="font-weight-bold col-sm-2 col-form-label text-right">Monto</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id_amount" name="amount" placeholder="Valor de la factura" required>
<input type="text" class="form-control" id="id_amount" name="amount" value="{{sponsoring.total_amount}}" placeholder="Valor de la factura" required>
</div>
</div>
<div class="form-group row">
Expand Down
13 changes: 12 additions & 1 deletion website/events/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def admin_event_associate_organizers_post_data(event, organizers):
'event_organizers-__prefix__-id': [''],
'event_organizers-__prefix__-event': ['1'],
'event_organizers-__prefix__-organizer': [''],

'discount-TOTAL_FORMS': ['1'],
'discount-INITIAL_FORMS': ['0'],
'discount-MIN_NUM_FORMS': ['0'],
'discount-MAX_NUM_FORMS': ['1000'],

'discount-__prefix__-id': [''],
'discount-__prefix__-event': ['1'],
'discount-__prefix__-name': [''],
'discount-__prefix__-description': [''],
'discount-__prefix__-discount': [''],

'_save': ['Save']
}

Expand Down Expand Up @@ -353,7 +365,6 @@ def test_on_organizer_associate_to_event_call_mail_function(self, send_email_fun

url = reverse('admin:events_event_change', kwargs={'object_id': event.pk})
self.client.login(username='administrator', password='administrator')

organizers = []
for organizer in Organizer.objects.all():
organizers.append(organizer)
Expand Down
5 changes: 5 additions & 0 deletions website/events/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
views.event_create_sponsor_category,
name='event_create_sponsor_category'
),
path(
'eventos/<pk>/descuentos-sponsor/crear/',
views.event_create_sponsor_discount,
name='event_create_sponsor_discount'
),

# Sponsoring urls.
path('eventos/<event_pk>/patrocinios/', views.sponsoring_list, name='sponsoring_list'),
Expand Down
Loading