def test_str(): obj = InvoiceSettingsFactory( name_and_address='Honeydown', phone_number='01837', time_record_product=None, ) assert 'Honeydown, Phone: 01837' == str(obj)
def test_invoice_create_time(perm_check): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) url = reverse('invoice.create.time', kwargs={'pk': contact.pk}) perm_check.staff(url)
def test_str_with_time_record_product(): obj = InvoiceSettingsFactory( name_and_address='Honeydown', phone_number='01837', time_record_product=ProductFactory(name='Apple'), ) assert 'Honeydown, Phone: 01837, Time record: Apple' == str(obj)
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_invoice_create_pdf_not_draft(self): """Cannot create a PDF if the invoice has already been printed""" InvoiceSettingsFactory() VatSettingsFactory() invoice = InvoiceFactory() InvoiceLineFactory(invoice=invoice) InvoicePrint().create_pdf(invoice, None) self.assertRaises(InvoiceError, InvoicePrint().create_pdf, invoice, None)
def test_user_can_edit_invoice(): InvoiceSettingsFactory() VatSettingsFactory() invoice = InvoiceFactory() line = InvoiceLineFactory(invoice=invoice) TimeRecordFactory(invoice_line=line) InvoicePrint().create_pdf(invoice, None) # refresh line = InvoiceLine.objects.get(pk=line.pk) assert line.user_can_edit is False
def test_invoice_create_pdf(self): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2013, 12, 1)) invoice = InvoiceCreate().create(UserFactory(), contact, date(2013, 12, 31)) InvoicePrint().create_pdf(invoice, None)
def test_is_not_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
def test_create_invoices_do_not_bill_twice(): """Check we can't include the time records more than once""" InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2012, 7, 1)) user = UserFactory() InvoiceCreateBatch().create(user, date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count() InvoiceCreateBatch().create(user, date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count()
def test_refresh_draft_only(): """Only draft invoices can be refreshed.""" InvoiceSettingsFactory() VatSettingsFactory() tr = TimeRecordFactory() InvoiceContactFactory(contact=tr.ticket.contact) invoice = InvoiceCreate().create( tr.user, tr.ticket.contact, date.today() ) assert 1 == invoice.invoiceline_set.count() InvoicePrint().create_pdf(invoice, None) with pytest.raises(InvoiceError) as e: InvoiceCreate().refresh(tr.user, invoice, date.today()) assert 'Time records can only be added to a draft invoice' in str(e.value)
def test_create_invoices_only_billable_time(): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2012, 7, 1)) InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) TimeRecordFactory( ticket=ticket, date_started=date(2012, 7, 1), billable=False, ) InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count()
def test_invoice_download(perm_check): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2013, 12, 1)) invoice = InvoiceCreate().create( UserFactory(), contact, date(2013, 12, 31) ) InvoicePrint().create_pdf(invoice, header_image=None) url = reverse('invoice.download', kwargs={'pk': invoice.pk}) perm_check.staff(url)
def test_invoice_with_time_records_no_end_time(): """One of the time records has no end time, so cannot be invoiced.""" InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) tr1 = TimeRecordFactory(ticket=ticket) TimeRecordFactory(ticket=ticket, end_time=None) TimeRecordFactory(ticket=ticket) with pytest.raises(InvoiceError) as ex: InvoiceCreate().create(tr1.user, contact, date.today()) message = str(ex.value) assert 'does not have a' in message assert 'end time' in message
def test_refresh(): """Create a draft invoice, and then add more time records to it.""" InvoiceSettingsFactory() VatSettingsFactory() tr = TimeRecordFactory() InvoiceContactFactory(contact=tr.ticket.contact) invoice = InvoiceCreate().create( tr.user, tr.ticket.contact, date.today() ) assert 1 == invoice.invoiceline_set.count() # create a couple more time records which can be added TimeRecordFactory(ticket=tr.ticket) TimeRecordFactory(ticket=tr.ticket) InvoiceCreate().refresh(tr.user, invoice, date.today()) assert 3 == invoice.invoiceline_set.count()
def test_create_invoices(): """Create an invoice""" InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) TimeRecordFactory(ticket=ticket, date_started=date(2012, 7, 1)) TimeRecordFactory(ticket=ticket, date_started=date(2012, 8, 1)) TimeRecordFactory(ticket=ticket) # action InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) invoices = Invoice.objects.filter(contact=contact) assert 1 == len(invoices) invoice = invoices[0] assert 2 == len(invoice.invoiceline_set.all())
def test_invoice_with_time_records(): """Invoice time records.""" InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) ticket = TicketFactory(contact=contact) tr1 = TimeRecordFactory(ticket=ticket) TimeRecordFactory(ticket=ticket) invoice = InvoiceCreate().create( tr1.user, contact, date.today() ) assert invoice.is_draft InvoicePrint().create_pdf(invoice, None) assert not invoice.is_draft assert Decimal('40.00') == invoice.net
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)
def test_report_total_by_user(): contact = ContactFactory() invoice = InvoiceFactory(contact=contact) InvoiceSettingsFactory() # no time records InvoiceLineFactory(invoice=invoice) # u1's time records invoice_line = InvoiceLineFactory(invoice=invoice) t1 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t1, user=UserFactory(username='******'), invoice_line=invoice_line) # u2's time records invoice_line = InvoiceLineFactory(invoice=invoice) u2 = UserFactory(username='******') t2 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t2, user=u2, invoice_line=invoice_line) invoice_line = InvoiceLineFactory(invoice=invoice) t3 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t3, user=u2, invoice_line=invoice_line) result = invoice.time_analysis() # invoice has a line with no time records assert '' in result # fred recorded time on one ticket assert 'u1' in result u1 = result['u1'] assert 1 == len(u1) assert t1.pk in u1 # sara recorded time on two tickets assert 'u2' in result u2 = result['u2'] assert 2 == len(u2) assert t2.pk in u2 assert t3.pk in u2 # web user added an invoice line, but didn't record time assert 'web' not in result # check net total matches invoice net = Decimal() for user, tickets in result.items(): for ticket_pk, totals in tickets.items(): net = net + totals['net'] assert invoice.net == net
def test_create_invoices_not_fixed_price(): InvoiceSettingsFactory() VatSettingsFactory() contact = ContactFactory() InvoiceContactFactory(contact=contact) # ticket 1 t1 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t1, title='t1', date_started=date(2012, 7, 1)) # ticket 2 is for fixed price work t2 = TicketFactory(contact=contact, fixed_price=True) TimeRecordFactory(ticket=t2, title='t2', date_started=date(2012, 7, 2)) # ticket 3 t3 = TicketFactory(contact=contact) TimeRecordFactory(ticket=t3, title='t3', date_started=date(2012, 7, 3)) # test InvoiceCreateBatch().create(UserFactory(), date(2012, 9, 30)) assert 1 == Invoice.objects.filter(contact=contact).count() invoice = Invoice.objects.get(contact=contact) assert ['t1', 't3'] == [ x.timerecord.title for x in InvoiceLine.objects.filter(invoice=invoice) ]