def test_remove_time_lines_not_extra():
    """Remove all but one line.

    The extra line is not removed because it isn't linked to a time record.

    .. important:: This test will fail if you try to run it around midnight.
                   The end time will be something like ``time(0, 45)`` and
                   the start time will be something like ``time(23, 45)``.
                   Both are for the same date, and the end date will look as if
                   it is before the start date!

    """
    InvoiceSettingsFactory()
    VatSettingsFactory()
    tr = TimeRecordFactory()
    InvoiceContactFactory(contact=tr.ticket.contact)
    TimeRecordFactory(ticket=tr.ticket)
    invoice = InvoiceCreate().create(
        tr.user,
        tr.ticket.contact,
        date.today()
    )
    extra_line = InvoiceLineFactory(invoice=invoice)
    assert invoice.is_draft is True
    InvoicePrint().create_pdf(invoice, None)
    assert invoice.has_lines is True
    invoice.set_to_draft()
    invoice.remove_time_lines()
    assert [extra_line.pk] == [i.pk for i in invoice.invoiceline_set.all()]
def test_set_is_draft():
    InvoiceSettingsFactory()
    VatSettingsFactory()
    tr = TimeRecordFactory()
    InvoiceContactFactory(contact=tr.ticket.contact)
    invoice = InvoiceCreate().create(
        tr.user, tr.ticket.contact, date.today()
    )
    assert invoice.is_draft is True
    InvoicePrint().create_pdf(invoice, None)
    assert invoice.is_draft is False
    invoice.set_to_draft()
    assert invoice.is_draft is True
def test_remove_time_lines():
    """Remove all lines (because they are all linked to time records)."""
    InvoiceSettingsFactory()
    VatSettingsFactory()
    tr = TimeRecordFactory()
    InvoiceContactFactory(contact=tr.ticket.contact)
    TimeRecordFactory(ticket=tr.ticket)
    invoice = InvoiceCreate().create(
        tr.user, tr.ticket.contact, date.today()
    )
    assert invoice.is_draft is True
    InvoicePrint().create_pdf(invoice, None)
    assert invoice.has_lines is True
    invoice.set_to_draft()
    invoice.remove_time_lines()
    assert invoice.has_lines is False
def test_set_is_draft_too_late():
    """invoice can only be set back to draft on the day it is created."""
    InvoiceSettingsFactory()
    VatSettingsFactory()
    tr = TimeRecordFactory()
    InvoiceContactFactory(contact=tr.ticket.contact)
    TimeRecordFactory(ticket=tr.ticket)
    invoice = InvoiceCreate().create(
        tr.user, tr.ticket.contact, date.today()
    )
    invoice.invoice_date=date.today() + relativedelta(days=-1)
    invoice.save()
    assert invoice.is_draft is True
    InvoicePrint().create_pdf(invoice, None)
    assert invoice.is_draft is False
    with pytest.raises(InvoiceError) as e:
        invoice.set_to_draft()
    assert 'only set an invoice back to draft on the day' in str(e.value)