Skip to content

Commit

Permalink
order new views and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckpy committed Oct 12, 2021
1 parent a916f51 commit 1318765
Show file tree
Hide file tree
Showing 22 changed files with 242 additions and 25 deletions.
Binary file modified order/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/actions.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/managers.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified order/__pycache__/views.cpython-38.pyc
Binary file not shown.
8 changes: 4 additions & 4 deletions order/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib import admin
from .models import Order, Sale, SaleItens
from .models import Order, Sale, SaleItem
from .actions import nfe_nao_emitida, nfe_emitida

class SaleItemInLine(admin.TabularInline):
model = SaleItens
model = SaleItem

class OrderAdmin(admin.ModelAdmin):
fieldsets = (
Expand All @@ -28,7 +28,7 @@ class SaleAdmin(admin.ModelAdmin):
# filter_horizontal = ('product',)
autocomplete_fields = ('customer',)

class SaleItensAdmin(admin.ModelAdmin):
class SaleItemAdmin(admin.ModelAdmin):
list_display = ('sale', 'product', 'quantity', 'discount')
search_fields = ('sale', 'product')
autocomplete_fields = ('sale', 'product')
Expand All @@ -37,4 +37,4 @@ class SaleItensAdmin(admin.ModelAdmin):
# Register your models here.
admin.site.register(Order, OrderAdmin)
admin.site.register(Sale, SaleAdmin)
admin.site.register(SaleItens, SaleItensAdmin)
admin.site.register(SaleItem, SaleItemAdmin)
11 changes: 11 additions & 0 deletions order/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django import forms

class SaleForm(forms.Form):
reference_code = forms.IntegerField(label='Código de Referência')
customer = forms.ChoiceField(label='Cliente')
discount = forms.DecimalField(label='Desconto', decimal_places=2, max_digits=5, )

class ProductForm(forms.Form):
product_id = forms.CharField(label='Produto')
quantity = forms.IntegerField(label='Quantidade')
discount = forms.DecimalField(label='Desconto', max_digits=5, decimal_places=2)
18 changes: 18 additions & 0 deletions order/migrations/0002_rename_saleitens_saleitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.3 on 2021-10-12 16:10

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('product', '0004_remove_product_weight'),
('order', '0001_initial'),
]

operations = [
migrations.RenameModel(
old_name='SaleItens',
new_name='SaleItem',
),
]
Binary file modified order/migrations/__pycache__/0001_initial.cpython-38.pyc
Binary file not shown.
Binary file modified order/migrations/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
10 changes: 4 additions & 6 deletions order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def get_absolute_url(self):
return reverse("order:detail", kwargs={"slug": self.slug})

def total_price(self):
total = self.saleitens_set.all().aggregate(
total = self.saleitem_set.all().aggregate(
total_sale=Sum((F('quantity')* F('product__price')) - F('discount'), output_field=FloatField())
)['total_sale'] or 0
total = total - float(self.discount) # TODO - self.imposto
self.price = total
Sale.objects.filter(id=self.id).update(price=total)

def products(self):
products = self.saleitens_set.all()
products = self.saleitem_set.all()
return products

class Meta :
Expand All @@ -52,7 +52,7 @@ class Meta :
verbose_name = "Vendas"
verbose_name_plural = "Vendas"

class SaleItens(models.Model):
class SaleItem(models.Model):
sale = models.ForeignKey(Sale, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
Expand All @@ -75,7 +75,6 @@ class Order(models.Model):
description = models.TextField('Descição 1',max_length=500, blank=True)
description_2 = models.TextField('Descição 2',max_length=500, blank=True)
date = models.DateField('Data',auto_now_add=True)


def __str__(self):
return f'Ordem - {self.customer} - {self.id}'
Expand All @@ -85,8 +84,7 @@ class Meta :
verbose_name_plural = "Ordens"



@receiver(post_save, sender=SaleItens)
@receiver(post_save, sender=SaleItem)
def update_sale_price(sender, instance,**kwargs):
instance.sale.total_price()

Expand Down
8 changes: 6 additions & 2 deletions order/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from .views import (SaleListView,
SaleDetailView,
SaleCreateView,
SaleProductAdd)
SaleProductAdd,
SaleEditView,
SaleDeleteView)

app_name= 'order'

urlpatterns = [
path('', SaleListView.as_view(), name='list'),
path('detail/<slug:slug>/', SaleDetailView.as_view(), name='detail'),
path('detalhe/<slug:slug>/', SaleDetailView.as_view(), name='detail'),
path('criar/', SaleCreateView.as_view(), name='new'),
path('add_product/<int:sale>/', SaleProductAdd.as_view(), name='add_product'),
path('editar/<int:sale>/', SaleEditView.as_view(), name='edit'),
path('excluir/<int:sale>/', SaleDeleteView.as_view(), name='delete'),
]
41 changes: 33 additions & 8 deletions order/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView, View

from user.models import User
from .forms import SaleForm,ProductForm
from .models import Sale, SaleItens, Product
from .forms import SaleForm, ProductForm
from .models import Sale, SaleItem, Product

class SaleListView(ListView):

model = Sale
paginate_by = 9
template_name= 'order/sale_list.html'
paginate_by = 15
template_name= 'order/sale_list.html'

def get_context_data(self, **kwargs):
context= super().get_context_data(**kwargs)
Expand Down Expand Up @@ -47,7 +47,7 @@ def post(self,request):
sale = Sale.objects.create(
reference_code=data['reference_code'],discount=data['discount'], customer= User.objects.get(id=data['customer'])
)
itens = sale.saleitens_set.all()
itens = sale.saleitem_set.all()
data['sale_obj']=sale
data['itens']=itens

Expand All @@ -61,7 +61,7 @@ def get(self, request):

def post(self, request, sale):
data={}
item = SaleItens.objects.create(
item = SaleItem.objects.create(
product=Product.objects.get(id=request.POST['product_id']),quantity=request.POST['quantity'],
discount=request.POST['discount'], sale=Sale.objects.get(id=sale)
)
Expand All @@ -71,6 +71,31 @@ def post(self, request, sale):
data['discount']=item.sale.discount
data['sale_id']=item.sale.id
data['sale_obj']=item.sale
data['itens']=item.sale.saleitens_set.all()
data['itens']=item.sale.saleitem_set.all()

return render(request, 'order/sale_form.html', data)

class SaleEditView(View):

def get(self, request, sale):
data= {}
sale= Sale.objects.get(id=sale)
data['form_item']= ProductForm()
data['reference_code']=sale.reference_code
data['discount']=sale.discount
data['sale_id']=sale.id
data['sale_obj']=sale
data['itens']=sale.saleitem_set.all()

return render(request, 'order/sale_edit.html', data)

class SaleDeleteView(View):

def get (self, request,sale):
sale = Sale.objects.get(id=sale)
return render(request, 'order/sale_delete.html', {'sale':sale})

def post (self, request,sale):
sale = Sale.objects.get(id=sale)
sale.delete()
return redirect('order:list')
33 changes: 33 additions & 0 deletions templates/order/sale_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends 'base.html' %}
{% block title %}Deletar Venda{% endblock %}

{% load crispy_forms_tags %}
{% block content %}


<div class="app-wrapper">
<div class="app-content pt-3 p-md-3 p-lg-4">
<div class="container-xl">
<div class="row col-10">
<div class="row my-4">
<div class="col-md-6 offset-md-3">
<div class="border p-3 bg-white">
<h3>Excluir Produto</h3>
<hr>
<form method="POST">
<div class="alert alert-danger">
<h4>Tem certeza que deseja excluir a venda "{{ sale.reference_code }}" ? <br>Lembre-se que essa ação é irreversível</h4>
</div>
{% csrf_token %}
{{ form|crispy }}
<a class="btn btn-secondary btn-sm" href="{% url 'order:list' %}">Cancelar</a>
<input class="btn btn-danger btn-sm" type="submit" value="Confirmar">
</form>
</div>
</div>
</div>
</div>
</div><!--//container-fluid-->
</div><!--//app-content-->
</div><!--//app-wrapper-->
{% endblock %}
4 changes: 4 additions & 0 deletions templates/order/sale_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

<div class="row g-3 mb-4 align-items-center justify-content-between">
<div class="app-page-title mb-0">Venda - {{sale.reference_code}} Resumo :</div>
<div class='row col-2'>
<a class='btn btn-primary 'href='{% url 'order:edit' sale.id%}'>Editar</a>
<a class='btn btn-danger' href='{% url 'order:delete' sale.id%}'>Excluir</a>
</div>
<div class="row g-4 mb-4">
<div class="col-6 col-lg-3">
<div class="app-card app-card-stat shadow-sm h-100">
Expand Down
62 changes: 62 additions & 0 deletions templates/order/sale_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends "base.html"%}
{% block title %}Nova Ordem de Venda {% endblock title%}

{% block content %}

{% load crispy_forms_tags %}

<div class="app-wrapper">
<div class="app-content pt-3 p-md-3 p-lg-4">
<div class="container-xl">
<h1 class="app-page-title">Cabeçalho Venda {{ sale_obj.reference_code }} </h1>
<h5> Preço {{ sale_obj.price }}</h5>
<form method="POST" action='{% url 'order:new' %}'>
{% csrf_token %}
<label for='reference_code'>Código de Referência</label><br>
<input type='text' class='form-control'name='reference_code' value="{{reference_code}}" placeholder='9999'>
<label for='customer'>Cliente</label><br>
<input type='text' class='form-control'name='customer' value='{{customer}}' placeholder='Só números'>
<label for='discount'>Desconto</label><br>
<input type='number' class='form-control'name='discount' value='{{discount}}' placeholder='0,00'>
<input type="hidden" name="sale_id" value='{{sale_obj.id}}'>
<input class="btn btn-primary"type="submit" value="Criar">
</form>
{% if sale_obj %}
<h4>Adicionar Produtos </h4>
<form action='{% url 'order:add_product' sale_obj.id %}' method='POST'>
{% csrf_token %}
<label for='product_id'>Id do Produto</label><br>
<input type='text' class='form-control'name='product_id' value="{{product_id}}" placeholder='9999'>
<label for='quantity'>Quantidade</label><br>
<input type='number' class='form-control'name='quantity' value="{{quantity}}" placeholder='1~99'>
<label for='discount'>Desconto</label><br>
<input type='number' class='form-control'name='discount' value="{{discount}}" placeholder='0'>
<button class="btn btn-primary" type="submit">Incluir</button>
</form>
<h2>Listagem de Itens :</h2>
<table class="table">
<thread>
<tr>
<th scope="col">Id</th>
<th scope="col">Nome</th>
<th scope="col">Quantidade</th>
<th scope="col">Desconto</th>
</tr>
</thread>
<tbody>
{% for item in itens %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.product.name }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.discount }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div><!--//container-fluid-->
</div><!--//app-content-->
</div><!--//app-wrapper-->

{% endblock content%}
62 changes: 62 additions & 0 deletions templates/order/sale_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends "base.html"%}
{% block title %}Nova Ordem de Venda {% endblock title%}

{% block content %}

{% load crispy_forms_tags %}

<div class="app-wrapper">
<div class="app-content pt-3 p-md-3 p-lg-4">
<div class="container-xl">
<h1 class="app-page-title">Cabeçalho Venda {{ sale_obj.reference_code }} </h1>
<h5> Preço {{ sale_obj.price }}</h5>
<form method="POST" action='{% url 'order:new' %}'>
{% csrf_token %}
<label for='reference_code'>Código de Referência</label><br>
<input type='text' class='form-control'name='reference_code' value="{{reference_code}}" placeholder='9999'>
<label for='customer'>Cliente</label><br>
<input type='text' class='form-control'name='customer' value='{{customer}}' placeholder='Só números'>
<label for='discount'>Desconto</label><br>
<input type='number' class='form-control'name='discount' value='{{discount}}' placeholder='0,00'>
<input type="hidden" name="sale_id" value='{{sale_obj.id}}'>
<input class="btn btn-primary"type="submit" value="Criar">
</form>
{% if sale_obj %}
<h4>Adicionar Produtos </h4>
<form action='{% url 'order:add_product' sale_obj.id %}' method='POST'>
{% csrf_token %}
<label for='product_id'>Id do Produto</label><br>
<input type='text' class='form-control'name='product_id' value="{{product_id}}" placeholder='9999'>
<label for='quantity'>Quantidade</label><br>
<input type='number' class='form-control'name='quantity' value="{{quantity}}" placeholder='1~99'>
<label for='discount'>Desconto</label><br>
<input type='number' class='form-control'name='discount' value="{{discount}}" placeholder='0'>
<button class="btn btn-primary" type="submit">Incluir</button>
</form>
<h2>Listagem de Itens :</h2>
<table class="table">
<thread>
<tr>
<th scope="col">Id</th>
<th scope="col">Nome</th>
<th scope="col">Quantidade</th>
<th scope="col">Desconto</th>
</tr>
</thread>
<tbody>
{% for item in itens %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.product.name }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.discount }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div><!--//container-fluid-->
</div><!--//app-content-->
</div><!--//app-wrapper-->

{% endblock content%}
6 changes: 3 additions & 3 deletions templates/order/sale_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="row g-3 mb-4 align-items-center justify-content-between">
<div class="col-auto">
<h1 class="app-page-title mb-0">Ordens de Venda </h1>
<h1 class="app-page-title mb-0">Ordens de Venda<a class='btn btn-primary' href='{% url 'order:new'%}'>Nova Venda</a> </h1>
</div>
<div class="col-auto">
<div class="page-utilities">
Expand Down Expand Up @@ -54,9 +54,9 @@ <h1 class="app-page-title mb-0">Ordens de Venda </h1>
<td class="cell"><span>{{sale.date|date:"d D M"}}</span></td>
<td class="cell"><span class="badge bg-success">Pago</span></td>
<td class="cell">${{sale.price}}</td>
<td class="cell"><a class="btn-sm app-btn-secondary" href="{{sale.get_absolute_url}}">View</a></td>
<td class="cell"><a class="btn-sm app-btn-secondary" href="{{sale.get_absolute_url}}">Detalhes</a></td>
</tr>
{%empty %}
{% empty %}
<h1> Não existem ordens de venda ainda </h1>
{% endfor %}
</tbody>
Expand Down
Loading

0 comments on commit 1318765

Please sign in to comment.