Skip to content
Closed

dev #285

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion caja/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
class EventProductForm(ModelForm):
class Meta:
model = EventProduct
fields = ['name', 'price', 'is_active', 'ticket_type']
fields = ['name', 'price', 'image', 'is_active', 'ticket_type']
widgets = {
'name': forms.TextInput(attrs={'class': 'form-control'}),
'price': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01', 'min': '0'}),
'image': forms.ClearableFileInput(attrs={'class': 'form-control', 'accept': 'image/*'}),
'is_active': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
'ticket_type': forms.Select(attrs={'class': 'form-select'}),
}
Expand Down
36 changes: 36 additions & 0 deletions caja/migrations/0003_cajasale_cancellation_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
('caja', '0002_migrate_ticket_stock'),
]

operations = [
migrations.AddField(
model_name='cajasale',
name='notes',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='cajasale',
name='related_sale',
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name='cancellations',
to='caja.cajasale',
),
),
migrations.AddField(
model_name='cajasale',
name='sale_type',
field=models.CharField(
choices=[('SALE', 'Venta'), ('CANCELLATION', 'Cancelación')],
default='SALE',
max_length=20,
),
),
]
15 changes: 15 additions & 0 deletions caja/migrations/0004_eventproduct_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('caja', '0003_cajasale_cancellation_fields'),
]

operations = [
migrations.AddField(
model_name='eventproduct',
name='image',
field=models.ImageField(blank=True, null=True, upload_to='caja/products/'),
),
]
18 changes: 18 additions & 0 deletions caja/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EventProduct(BaseModel):
)
name = models.CharField(max_length=200, blank=True)
price = models.DecimalField(decimal_places=2, max_digits=10, null=True, blank=True)
image = models.ImageField(upload_to='caja/products/', blank=True, null=True)
is_active = models.BooleanField(default=True)

class Meta:
Expand Down Expand Up @@ -189,11 +190,28 @@ class Status(models.TextChoices):
CANCELLED = 'CANCELLED', 'Cancelada'
EXPIRED = 'EXPIRED', 'Expirada'

class SaleType(models.TextChoices):
SALE = 'SALE', 'Venta'
CANCELLATION = 'CANCELLATION', 'Cancelación'

event_caja = models.ForeignKey(EventCaja, on_delete=models.CASCADE, related_name='sales')
sold_by = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='caja_sales')
payment_method = models.CharField(max_length=20, choices=PaymentMethod.choices)
sale_type = models.CharField(
max_length=20,
choices=SaleType.choices,
default=SaleType.SALE,
)
status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
total_amount = models.DecimalField(decimal_places=2, max_digits=10, default=Decimal('0'))
notes = models.TextField(blank=True)
related_sale = models.ForeignKey(
'self',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='cancellations',
)
customer_email = models.EmailField(blank=True)
mark_as_used = models.BooleanField(default=False)
order = models.ForeignKey(
Expand Down
12 changes: 12 additions & 0 deletions caja/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from caja.views import (
api_cancel_sale,
api_cancel_paid_sale,
api_create_sale,
api_pay_mp_point,
api_pay_mp_qr,
Expand All @@ -10,6 +11,7 @@
caja_events_v2_view,
caja_sales_report_view,
caja_v2_operator_view,
caja_v2_summary_view,
cajas_list_view,
event_report_view,
product_edit_view,
Expand Down Expand Up @@ -57,6 +59,11 @@
caja_v2_operator_view,
name='caja_v2_operator',
),
path(
'mis-eventos/<slug:event_slug>/cajas-v2/<int:caja_id>/resumen/',
caja_v2_summary_view,
name='caja_v2_summary',
),
path(
'mis-eventos/<slug:event_slug>/cajas-v2/<int:caja_id>/api/sales/',
api_create_sale,
Expand All @@ -82,4 +89,9 @@
api_cancel_sale,
name='caja_v2_api_cancel_sale',
),
path(
'mis-eventos/<slug:event_slug>/cajas-v2/<int:caja_id>/api/sales/<int:sale_id>/cancel-paid/',
api_cancel_paid_sale,
name='caja_v2_api_cancel_paid_sale',
),
]
2 changes: 2 additions & 0 deletions caja/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
)
from caja.views.operator_views import (
api_cancel_sale,
api_cancel_paid_sale,
api_create_sale,
api_pay_mp_point,
api_pay_mp_qr,
api_sale_status,
caja_v2_operator_view,
caja_v2_summary_view,
)
71 changes: 43 additions & 28 deletions caja/views/admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,12 @@ def product_edit_view(request, event_slug, product_id):
event = get_event_for_admin(request.user, event_slug)
product = get_object_or_404(EventProduct, id=product_id, event=event)
stock = ensure_stock_row(product)
records = EventProductStockRecord.objects.filter(event_product=product)[:20]
is_unlimited = stock.quantity is None

if request.method == 'POST':
action = request.POST.get('action', 'save')

if action in ('add_stock', 'remove_stock'):
stock.refresh_from_db()
if stock.quantity is None:
messages.error(request, 'No podés ajustar stock mientras el producto es ilimitado.')
return redirect('caja_product_edit', event_slug=event.slug, product_id=product.id)

stock_form = StockQuantityForm(request.POST)
if stock_form.is_valid():
qty = stock_form.cleaned_data['quantity']
delta = qty if action == 'add_stock' else -qty
notes = stock_form.cleaned_data.get('notes', '')
adjust_stock(product, delta, request.user, notes=notes)
verb = 'agregado' if action == 'add_stock' else 'quitado'
messages.success(request, f'Stock {verb}: {qty} unidad(es).')
else:
for field, errors in stock_form.errors.items():
for error in errors:
messages.error(request, f'{field}: {error}')
return redirect('caja_product_edit', event_slug=event.slug, product_id=product.id)

form = EventProductEditForm(
request.POST,
request.FILES,
instance=product,
event=event,
stock_unlimited=is_unlimited,
Expand All @@ -119,18 +97,55 @@ def product_edit_view(request, event_slug, product_id):
context.update({
'product': product,
'form': form,
'stock': stock,
'stock_form': StockQuantityForm(),
'records': records,
'available': available(product),
'is_unlimited': is_unlimited,
})
return render(request, 'mi_fuego/caja_v2/product_edit.html', context)


@login_required
def product_stock_view(request, event_slug, product_id):
return redirect('caja_product_edit', event_slug=event_slug, product_id=product_id)
event = get_event_for_admin(request.user, event_slug)
product = get_object_or_404(EventProduct, id=product_id, event=event)
stock = ensure_stock_row(product)
records = EventProductStockRecord.objects.filter(event_product=product)[:20]
is_unlimited = stock.quantity is None

if request.method == 'POST':
action = request.POST.get('action')
if action not in ('add_stock', 'remove_stock'):
raise Http404('Acción inválida')

stock.refresh_from_db()
if stock.quantity is None:
messages.error(request, 'No podés ajustar stock mientras el producto es ilimitado.')
return redirect('caja_product_stock', event_slug=event.slug, product_id=product.id)

stock_form = StockQuantityForm(request.POST)
if stock_form.is_valid():
qty = stock_form.cleaned_data['quantity']
delta = qty if action == 'add_stock' else -qty
notes = stock_form.cleaned_data.get('notes', '')
adjust_stock(product, delta, request.user, notes=notes)
verb = 'agregado' if action == 'add_stock' else 'quitado'
messages.success(request, f'Stock {verb}: {qty} unidad(es).')
return redirect('caja_product_stock', event_slug=event.slug, product_id=product.id)

for field, errors in stock_form.errors.items():
for error in errors:
messages.error(request, f'{field}: {error}')
else:
stock_form = StockQuantityForm()

context = mi_fuego_admin_context(request, event, f'caja_products_{event.slug}')
context.update({
'product': product,
'stock': stock,
'stock_form': stock_form,
'records': records,
'available': available(product),
'is_unlimited': is_unlimited,
})
return render(request, 'mi_fuego/caja_v2/product_stock.html', context)


@login_required
Expand Down
Loading
Loading