예제 #1
0
    def test_delete_purchase(self):
        """
        for admin operations we need to invoice to be updated
        when a purchase is deleted.
        """
        p1 = Purchase(customer=self.customer,
                      article=self.articles[0],
                      quantity=2,
                      price=self.articles[0].price)
        p1.save()
        p2 = Purchase(customer=self.customer,
                      article=self.articles[1],
                      quantity=2,
                      price=self.articles[1].price)
        p2.save()

        # create invoice with p1 and ps
        invoice = create_invoice(self.customer, self.invoice_datetime,
                                 [p1, p2])
        self.assertEquals(
            Decimal(
                f'{2 * self.articles[0].price + 2 * self.articles[1].price:.2f}'
            ), invoice.amount)

        # delete p1
        p1.delete()

        self.assertEquals(Decimal(f'{2 * self.articles[1].price:.2f}'),
                          invoice.amount)
예제 #2
0
    def test_modify_invoice(self):
        """
        add an additional item to an invoice.
        this should change the invoice amount.
        """
        # create 2 item purchases
        p1 = Purchase(customer=self.customer,
                      article=self.articles[0],
                      quantity=2,
                      price=self.articles[0].price)
        p1.save()
        p2 = Purchase(customer=self.customer,
                      article=self.articles[1],
                      quantity=2,
                      price=self.articles[1].price)
        p2.save()

        # create invoice with p1
        invoice = create_invoice(self.customer, self.invoice_datetime, [p1])

        self.assertEquals(Decimal(f'{2 * self.articles[0].price}'),
                          invoice.amount)

        # add purchase p2
        p2.invoice = invoice
        p2.save()
        invoice.save()

        self.assertEquals(
            Decimal(
                f'{2 * self.articles[0].price + 2 * self.articles[1].price:.2f}'
            ), invoice.amount)
예제 #3
0
    def test_create_invoice(self):
        invoice = create_invoice(self.customer, self.invoice_datetime,
                                 self.purchases1 + self.purchases2)

        self.assertEquals(Decimal('6.9'), invoice.amount)
        self.assertEquals(self.invoice_datetime, invoice.date)
        # purchase1 has 2 items, purchase2 1
        self.assertEquals(3, len(invoice.purchases.all()))
예제 #4
0
    def test_create_invoice(self):
        # create 2 purchases
        p1 = Purchase(customer=self.customer,
                      article=self.articles[0],
                      quantity=2,
                      price=self.articles[0].price)
        p1.save()
        p2 = Purchase(customer=self.customer,
                      article=self.articles[1],
                      quantity=1,
                      price=self.articles[1].price)
        p2.save()

        invoice = create_invoice(self.customer, self.invoice_datetime,
                                 [p1, p2])

        self.assertEquals(self.customer, invoice.customer)
        self.assertEquals(2, len(invoice.purchases.all()))
        expected_amount = Decimal(
            f'{2 * self.articles[0].price + self.articles[1].price:.2f}')
        self.assertEquals(expected_amount, invoice.amount)
예제 #5
0
    def test_validate_purchase(self):
        """
        add purchase to an invoice
        make sure that customer and price are set automatically
        by validation logic (clean())
        """
        # create empty invoice
        invoice = create_invoice(self.customer, self.invoice_datetime, [])

        # add purchase without specifying price or customer
        p1 = Purchase(
            invoice=invoice,
            article=self.articles[0],
            quantity=2,
        )
        p1.clean()

        self.assertEquals(self.customer, p1.customer)
        self.assertEquals(self.articles[0].price, p1.price)

        p1.save()

        self.assertEquals(Decimal(f'{2 * p1.price:0.2f}'), invoice.amount)
예제 #6
0
    def test_generate_invoice_pdf(self):

        total_amount = 1.20 + 2 * 4.5

        # 1x item0, 2x item1
        ip1 = Purchase(article=self.articles[0],
                       quantity=1,
                       price=self.articles[0].price,
                       customer=self.customer)
        ip1.save()

        ip2 = Purchase(article=self.articles[1],
                       quantity=2,
                       price=self.articles[1].price,
                       customer=self.customer)
        ip2.save()

        invoice = create_invoice(self.customer, self.invoice_datetime,
                                 [ip1, ip2])
        # test pdf rendering
        # outstream = BytesIO()
        with open('test_invoice.pdf', 'wb') as outstream:
            renderer = InvoicePdfRenderer()
            renderer.render(invoice, outstream)