Created
December 20, 2017 01:52
-
-
Save elyssonmr/df63c36986d71a9732f2e834f74977ab to your computer and use it in GitHub Desktop.
Estoque Simples OP of Darkness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from alguma_coisa_do_django import models | |
TRANSACTION_KIND = ( | |
("in", "Entrada"), | |
("out", "Saida"), | |
("eaj", "Entrada de Ajuste"), | |
("saj", "Saída de Ajuste") | |
) | |
class Product(models.Model): | |
name = models.CharField(max_length=100) | |
#deleted = models.BooleanField(default=False) | |
def qtd_avaliable(self): | |
transactions = self.transactions.all().order_by("date_transactions") | |
total_avaliable = 0 | |
for transaction in transactions: | |
if transaction.transaction_kind == 'in' or transaction.transaction_kind == 'eaj': | |
total_avaliable += transaction.quantity | |
else: | |
total_avaliable -= transaction.quantity | |
return total_avaliable | |
def __unicode__(self): | |
return f"Produto {self.name} possuí {self.qtd_avaliable()} em estoque." | |
class Transaction(models.Model): | |
quantity = models.PositiveIntegerField() | |
transaction_kind = models.CharFild(max_length=3, choices=TRANSACTION_KIND) | |
date_transaction = models.DataTimeField(auto_now=True) | |
product = models.ForeignKey(Product, quando_deletar_produto_deleta_aqui=True, related_name="transactions") | |
def historico_vendas(self, product, **extra_filter): | |
pass | |
def historico_entradas(self, product, **extra_filter): | |
# transactions | |
# estrutura para montar algo \o/ | |
pass | |
def __unicode__(self): | |
trans_kind = "Entrada" | |
return f"{trans_kind} de {self.quantity} unidades do produto {self.product.name} no dia {self.date_transaction}." | |
# Passo 1 | |
papel = Product(name="Folha de Papel") | |
papel.save() | |
compra_papel = Transaction(quantity=300, transaction_kind="in", product=papel) | |
compra_papel.save() | |
papel.qtd_avaliable() # 300 | |
# Fim do Passo 1 | |
# Passo 2 | |
papel = Product.objects.get(name="Folha de Papel") | |
venda_papel = Transaction(quantity=100, transaction_kind="out", product=papel) | |
venda_papel.save() | |
papel.qtd_avaliable() # 200 | |
# Fim do Passo 2 | |
# Edit produto | |
pacote_papel = Product.objects.get(name="Folha de Papel") | |
pacote_papel.name = "Pacote de 500 folhas de papel" | |
pacote_papel.qtd_avaliable() # 200 | |
pacote_papel.save() | |
pacote_papel.qtd_avaliable() # 200 | |
ajuste = Transaction(quantity=190, transaction_kind="saj", product=pacote_papel) | |
ajuste.save() | |
pacote_papel.qtd_avaliable() # 10 | |
# Fim Edit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment