Commit 5baee415 authored by Claire Zuliani's avatar Claire Zuliani

update: automate htAmount calculation with model's methods #6

parent 53b02e87
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-14 16:30
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('djangoldp_invoices', '0002_auto_20181206_1856'),
]
operations = [
migrations.RemoveField(
model_name='batch',
name='htAmount',
),
migrations.RemoveField(
model_name='clientinvoice',
name='htAmount',
),
migrations.AlterField(
model_name='freelanceinvoice',
name='tvaRate',
field=models.DecimalField(choices=[(0.0, 'exonération de TVA'), (20.0, '20%')], decimal_places=2, default=20.0, max_digits=4),
),
]
# -*- coding: utf-8 -*-
from django.conf import settings
from django.db import models
from django.db.models import Sum
import datetime
from decimal import Decimal
class FreelanceInvoice(models.Model):
# customer = models.ForeignKey(User, on_delete=models.CASCADE)
......@@ -12,8 +14,8 @@ class FreelanceInvoice(models.Model):
('paid', 'payé')
)
TVA_RATES = (
(0.0, 0.0),
(20.0, 20.0)
(0.0, 'exonération de TVA'),
(20.0, '20%')
)
freelanceFullname = models.CharField(max_length=255)
identifier = models.CharField(max_length=255)
......@@ -40,8 +42,6 @@ class FreelanceInvoice(models.Model):
class ClientInvoice(models.Model):
# customer = models.ForeignKey('xxx.Client', on_delete=models.CASCADE)
# project = models.ForeignKey('xxx.Project', on_delete=models.CASCADE)
PENDING_STATE = 0
PAID_STATE = 1
STATES = (
('pending', 'en attente'),
('paid', 'réglée')
......@@ -49,15 +49,19 @@ class ClientInvoice(models.Model):
identifier = models.CharField(max_length=255)
title = models.CharField(max_length=255)
state = models.CharField(max_length=255, choices=STATES, default='pending')
htAmount = models.DecimalField(max_digits=11, decimal_places=2)
tvaRate = models.DecimalField(max_digits=4, decimal_places=2)
creationDate = models.DateField(auto_now_add=True)
modificationDate = models.DateField(auto_now=True)
invoicingDate = models.DateField(default = datetime.date.today)
@property
def htAmount(self):
return self.batches.aggregate(total=Sum('tasks__htAmount'))['total']
def tvaAmount(self):
return self.tvaRate * self.htAmount
return Decimal(self.tvaRate * self.htAmount() / Decimal(100))
def ttcAmount(self):
return Decimal(self.tvaAmount() + self.htAmount())
class Meta:
permissions = (
......@@ -66,13 +70,12 @@ class ClientInvoice(models.Model):
)
def __str__(self):
return '{} ({})'.format(self.identifier, self.title)
return '{} - {} ({})'.format(self.identifier, self.title, self.htAmount())
# Lot =========================================================
class Batch(models.Model):
invoice = models.ForeignKey(ClientInvoice, on_delete=models.CASCADE, related_name='batches')
title = models.CharField(max_length=255)
htAmount = models.DecimalField(max_digits=11, decimal_places=2)
creationDate = models.DateField(auto_now_add=True)
modificationDate = models.DateField(auto_now=True)
......@@ -83,7 +86,15 @@ class Batch(models.Model):
)
def __str__(self):
return '{} - {} ({} € HT)'.format(self.invoice.title, self.title, self.htAmount)
return '{} - {} ({} € HT)'.format(self.invoice.title, self.title, self.htAmount())
def htAmount(self):
amount = self.tasks.all().aggregate(total=Sum('htAmount'))['total']
if amount is None:
amount = Decimal(0.0)
return amount
Batch._meta.serializer_fields = ['@id', 'invoice', 'title', 'htAmount']
class Task(models.Model):
batch = models.ForeignKey(Batch, on_delete=models.CASCADE, related_name='tasks')
......@@ -99,4 +110,5 @@ class Task(models.Model):
)
def __str__(self):
return '{} ({} € HT)'.format(self.title, self.htAmount)
\ No newline at end of file
return '{} - {} ({} € HT)'.format(self.batch.title, self.title, self.htAmount)
......@@ -8,7 +8,14 @@ from .models import Task
urlpatterns = [
url(r'^freelance-invoices/', LDPViewSet.urls(model=FreelanceInvoice)),
url(r'^client-invoices/', LDPViewSet.urls(model=ClientInvoice, nested_fields=["batches"])),
url(r'^batches/', LDPViewSet.urls(model=Batch, nested_fields=["tasks"])),
url(r'^client-invoices/', LDPViewSet.urls(model=ClientInvoice,
nested_fields=["batches"],
fields=["@id", "identifier", "title", "state", "htAmount", "tvaRate", "invoicingDate",
"tvaAmount", "ttcAmount", "batches"]
)),
url(r'^batches/', LDPViewSet.urls(model=Batch,
nested_fields=["tasks"],
fields=["@id", "title", "htAmount", "tasks"]
)),
url(r'^tasks/', LDPViewSet.urls(model=Task)),
]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment