eo_facture: add Facture.accounting_year (#85549)
This commit is contained in:
parent
e300aed21b
commit
5610e646f7
|
@ -399,6 +399,7 @@ class FactureAdmin(LookupAllowed, admin.ModelAdmin):
|
|||
fields = [
|
||||
'proforma',
|
||||
'ordre',
|
||||
'accounting_year',
|
||||
'client',
|
||||
'contrat',
|
||||
'intitule',
|
||||
|
@ -412,7 +413,6 @@ class FactureAdmin(LookupAllowed, admin.ModelAdmin):
|
|||
'solde',
|
||||
'montant',
|
||||
'montant_ttc',
|
||||
'account_on_previous_period',
|
||||
]
|
||||
readonly_fields = ['creator', 'solde', 'ordre', 'montant', 'montant_ttc', 'periode']
|
||||
date_hierarchy = 'emission'
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.2.23 on 2024-01-11 11:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('eo_facture', '0019_alter_contrat_client'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='facture',
|
||||
name='accounting_year',
|
||||
field=models.PositiveIntegerField(default=1, verbose_name='Exercice'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,21 @@
|
|||
# Generated by Django 3.2.23 on 2024-01-11 11:52
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def initialize_accounting_year(apps, schema_editor):
|
||||
Facture = apps.get_model('eo_facture', 'Facture')
|
||||
for facture in Facture.objects.filter(accounting_year=1):
|
||||
if facture.account_on_previous_period:
|
||||
facture.accounting_year = facture.emission.year - 1
|
||||
else:
|
||||
facture.accounting_year = facture.emission.year
|
||||
facture.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('eo_facture', '0020_facture_accounting_year'),
|
||||
]
|
||||
|
||||
operations = [migrations.RunPython(initialize_accounting_year, migrations.RunPython.noop)]
|
|
@ -379,6 +379,10 @@ def today_plus_delai():
|
|||
return today() + datetime.timedelta(days=DELAI_PAIEMENT)
|
||||
|
||||
|
||||
def current_year():
|
||||
return today().year
|
||||
|
||||
|
||||
echeance_verbose_name = 'Échéance (par défaut %d jours)' % getattr(settings, 'DELAI_PAIEMENT', 45)
|
||||
|
||||
|
||||
|
@ -390,10 +394,7 @@ class FactureQuerySet(QuerySet):
|
|||
return self.filter(proforma=False)
|
||||
|
||||
def for_year(self, year):
|
||||
return self.filter(
|
||||
Q(emission__year=year, account_on_previous_period=False)
|
||||
| Q(emission__year=year - 1, account_on_previous_period=True)
|
||||
)
|
||||
return self.filter(accounting_year=year)
|
||||
|
||||
|
||||
class Facture(models.Model):
|
||||
|
@ -431,6 +432,10 @@ class Facture(models.Model):
|
|||
account_on_previous_period = models.BooleanField(
|
||||
verbose_name='Mettre cette facture sur l\'exercice précédent', default=False
|
||||
)
|
||||
accounting_year = models.PositiveIntegerField(
|
||||
verbose_name='Exercice',
|
||||
default=current_year,
|
||||
)
|
||||
numero_engagement = models.CharField(
|
||||
max_length=128, verbose_name='Numéro d\'engagement', blank=True, default=''
|
||||
)
|
||||
|
@ -477,6 +482,8 @@ class Facture(models.Model):
|
|||
super().save(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
if not self.accounting_year:
|
||||
self.accounting_year = self.emission.year
|
||||
if not (self.contrat or self.client):
|
||||
raise ValidationError('La facture doit avoir un client ou un contrat')
|
||||
if self.contrat:
|
||||
|
@ -571,12 +578,6 @@ class Facture(models.Model):
|
|||
if save:
|
||||
self.save()
|
||||
|
||||
@property
|
||||
def accounting_year(self):
|
||||
if self.account_on_previous_period:
|
||||
return self.emission.year - 1
|
||||
return self.emission.year
|
||||
|
||||
DEFAULT_FACTURE_TEMPLATE = 'facture.html'
|
||||
|
||||
def html(self, template_name=None, base_uri=None):
|
||||
|
@ -630,7 +631,6 @@ class Facture(models.Model):
|
|||
facture_avoir.intitule = 'AVOIR POUR LA FACTURE ' + self.intitule
|
||||
facture_avoir.paid = False
|
||||
facture_avoir.creator = creator
|
||||
facture_avoir.account_on_previous_period = False
|
||||
facture_avoir.numero_d_echeance = None
|
||||
facture_avoir.save()
|
||||
for ligne in self.lignes.all():
|
||||
|
|
1074
tests/fixture.json
1074
tests/fixture.json
File diff suppressed because it is too large
Load Diff
|
@ -32,6 +32,7 @@ def test_facture_form(db, freezer):
|
|||
'proforma': 'true',
|
||||
'intitule': 'facture pour truc',
|
||||
'emission': '2019-01-01',
|
||||
'accounting_year': '2019',
|
||||
'echeance': '2019-01-30',
|
||||
'taux_tva': '20',
|
||||
'sous_traite': '0',
|
||||
|
|
Loading…
Reference in New Issue