150 lines
4.0 KiB
Python
150 lines
4.0 KiB
Python
import datetime
|
|
|
|
import pytest
|
|
|
|
from lingo.invoicing.models import Campaign, DraftInvoice, DraftInvoiceLine, Invoice, InvoiceLine, Pool, Regie
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
@pytest.mark.parametrize('draft', [True, False])
|
|
def test_invoice_total_amount(draft):
|
|
regie = Regie.objects.create()
|
|
invoice_model = DraftInvoice if draft else Invoice
|
|
line_model = DraftInvoiceLine if draft else InvoiceLine
|
|
|
|
campaign = Campaign.objects.create(
|
|
date_start=datetime.date(2022, 9, 1),
|
|
date_end=datetime.date(2022, 10, 1),
|
|
date_issue=datetime.date(2022, 10, 31),
|
|
)
|
|
pool = Pool.objects.create(
|
|
campaign=campaign,
|
|
draft=draft,
|
|
)
|
|
|
|
invoice = invoice_model.objects.create(date_issue=datetime.date.today(), regie=regie, pool=pool)
|
|
assert invoice.total_amount == 0
|
|
invoice2 = invoice_model.objects.create(date_issue=datetime.date.today(), regie=regie, pool=pool)
|
|
assert invoice2.total_amount == 0
|
|
|
|
# line with error status, ignored
|
|
line = line_model.objects.create(
|
|
invoice=invoice, # with invoice
|
|
quantity=0,
|
|
unit_amount=0,
|
|
total_amount=0,
|
|
status='error',
|
|
pool=pool,
|
|
)
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 0
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# update line
|
|
line.unit_amount = 10
|
|
line.quantity = 1
|
|
line.total_amount = 10
|
|
line.save()
|
|
invoice.refresh_from_db()
|
|
# still in error
|
|
assert invoice.total_amount == 0
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# update line status
|
|
line.status = 'success'
|
|
line.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 10
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# update other field
|
|
line.unit_amount = 12 # total amount is wrong
|
|
line.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 10
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# update total_amount
|
|
line.total_amount = 12
|
|
line.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# create line with invoice, status success
|
|
line2 = line_model.objects.create(
|
|
invoice=invoice,
|
|
quantity=1,
|
|
unit_amount=20,
|
|
total_amount=20,
|
|
status='success',
|
|
pool=pool,
|
|
)
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 32
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# change invoice
|
|
line2.invoice = invoice2
|
|
line2.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 20
|
|
|
|
# delete line
|
|
line2.delete()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# create line without invoice, status success
|
|
line3 = line_model.objects.create(
|
|
quantity=1,
|
|
unit_amount=20,
|
|
total_amount=20,
|
|
status='success',
|
|
pool=pool,
|
|
)
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# set invoice
|
|
line3.invoice = invoice
|
|
line3.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 32
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# reset invoice
|
|
line3.invoice = None
|
|
line3.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
# no changes
|
|
line3.save()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|
|
|
|
# delete line
|
|
line3.delete()
|
|
invoice.refresh_from_db()
|
|
assert invoice.total_amount == 12
|
|
invoice2.refresh_from_db()
|
|
assert invoice2.total_amount == 0
|