def test_grand_total(self): from seantisinvoice.models import Customer from seantisinvoice.models import CustomerContact from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem # Customer contact = CustomerContact() contact.customer = Customer() # Invoice invoice = Invoice() invoice.contact = contact self.assertEquals(0, invoice.grand_total()) item = InvoiceItem() item.amount = 100.0 invoice.items.append(item) self.assertEquals(Decimal('100.00'), invoice.grand_total()) invoice.tax = 7.6 self.assertEquals(Decimal('107.60'), invoice.grand_total()) item = InvoiceItem() item.amount = 60.0 invoice.items.append(item) # Value is rounded self.assertEquals(Decimal('172.15'), invoice.grand_total()) invoice.tax = None self.assertEquals(Decimal('160.00'), invoice.grand_total())
def test_grand_total(self): from seantisinvoice.models import Customer from seantisinvoice.models import CustomerContact from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem # Customer contact = CustomerContact() contact.customer = Customer() # Invoice invoice = Invoice() invoice.contact = contact self.assertEquals(0, invoice.grand_total()) item = InvoiceItem() item.amount = 100.0 invoice.items.append(item) self.assertEquals(Decimal("100.00"), invoice.grand_total()) invoice.tax = 7.6 self.assertEquals(Decimal("107.60"), invoice.grand_total()) item = InvoiceItem() item.amount = 60.0 invoice.items.append(item) # Value is rounded self.assertEquals(Decimal("172.15"), invoice.grand_total()) invoice.tax = None self.assertEquals(Decimal("160.00"), invoice.grand_total())
def handle_add(self, converted): session = DBSession() invoice = Invoice() invoice.company = session.query(Company).first() self._apply_data(invoice, converted) session.add(invoice) # Get and add unique invoice number if invoice.invoice_number is None: invoice.invoice_number = next_invoice_number() statusmessage.show(self.request, u"Invoice added.", "success") return HTTPFound(location=route_url('invoices', self.request))
def test_recurring(self): from seantisinvoice.models import DBSession from seantisinvoice.models import Invoice, InvoiceItem from seantisinvoice.recurring import copy_recurring session = DBSession() copy_recurring() self.assertEquals(0, session.query(Invoice).count()) # Add a new invoice invoice = Invoice() invoice_date = datetime.date.today() - datetime.timedelta(days=100) invoice.date = invoice_date invoice.due_date = datetime.date.today() - datetime.timedelta(days=70) invoice.project_description = u"Invoice project description" session.add(invoice) # Add an item to the invoice item = InvoiceItem() item.item_number = 0 item.amount = 1000 item.service_description = u"Item description" item.service_title = u"Invoice item" item.invoice = invoice session.merge(item) copy_recurring() # Invoice not cloned as it has no recurring_date self.assertEquals(1, session.query(Invoice).count()) invoice = session.query(Invoice).one() # Set the recurring date on the invoice recurring_date = datetime.date.today() - datetime.timedelta(days=70) invoice.recurring_date = recurring_date copy_recurring() # There are now two invoices self.assertEquals(2, session.query(Invoice).count()) invoice = session.query(Invoice).filter_by(date=invoice_date).one() # Recurring date of the original invoice has been reset self.failIf(invoice.recurring_date) cloned_invoice = session.query(Invoice).filter_by(date=recurring_date).one() # New invoice has the recurring date of the original one as date self.assertEquals(recurring_date, cloned_invoice.date) self.assertEquals(recurring_date + datetime.timedelta(days=30), cloned_invoice.recurring_date) # Check whether attributes on invoice and invoice item have been cloned self.assertEquals(invoice.project_description, cloned_invoice.project_description) cloned_item = cloned_invoice.items[0] self.assertEquals(item.amount, cloned_item.amount) self.assertEquals(item.service_description, cloned_item.service_description) self.assertEquals(item.service_title, cloned_item.service_title)
def test_total(self): from seantisinvoice.models import Company from seantisinvoice.models import Customer from seantisinvoice.models import CustomerContact from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem company = Company() # Customer customer = Customer() contact = CustomerContact() contact.customer = customer # Invoice invoice = Invoice() invoice.company = company invoice.contact = contact item = InvoiceItem() item.invoice = invoice item.amount = 3000.0 self.assertEquals(3000.0, item.total()) # Hourly rate is defined on the company company.hourly_rate = 120.0 item = InvoiceItem() item.invoice = invoice item.hours = 13 self.assertEquals(1560.0, item.total()) # Daily rate is also defined on the company company.daily_rate = 1300.0 item = InvoiceItem() item.invoice = invoice item.days = 2.5 self.assertEquals(3250.0, item.total()) # Special daily rate on customer customer.special_daily_rate = 1100.0 item = InvoiceItem() item.invoice = invoice item.days = 4.5 self.assertEquals(4950.0, item.total()) # Special hourly rate on customer customer.special_hourly_rate = 100.0 item = InvoiceItem() item.invoice = invoice item.hours = 10 self.assertEquals(1000.0, item.total())
def test_sub_total(self): from seantisinvoice.models import Customer from seantisinvoice.models import CustomerContact from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem # Customer contact = CustomerContact() contact.customer = Customer() # Invoice invoice = Invoice() invoice.contact = contact item = InvoiceItem() item.amount = 1250.50 invoice.items.append(item) self.assertEquals(1250.5, invoice.sub_total()) item = InvoiceItem() item.amount = 120.0 invoice.items.append(item) self.assertEquals(1370.5, invoice.sub_total())
def test_next_invoice_number_used(self): from seantisinvoice.models import DBSession from seantisinvoice.models import Company from seantisinvoice.models import Invoice from seantisinvoice.models import next_invoice_number session = DBSession() company = session.query(Company).one() company.invoice_start_number = 20 invoice = Invoice(invoice_number=20) session.add(invoice) session.flush() result = next_invoice_number() self.assertEqual(21, result) self.assertEqual(22, company.invoice_start_number)
def test_recurring(self): from seantisinvoice.models import DBSession from seantisinvoice.models import Invoice, InvoiceItem from seantisinvoice.recurring import copy_recurring session = DBSession() copy_recurring() self.assertEquals(0, session.query(Invoice).count()) # Add a new invoice invoice = Invoice() invoice_date = datetime.date.today() - datetime.timedelta(days=100) invoice.date = invoice_date invoice.due_date = datetime.date.today() - datetime.timedelta(days=70) invoice.project_description = u'Invoice project description' session.add(invoice) # Add an item to the invoice item = InvoiceItem() item.item_number = 0 item.amount = 1000 item.service_description = u'Item description' item.service_title = u'Invoice item' item.invoice = invoice session.merge(item) copy_recurring() # Invoice not cloned as it has no recurring_date self.assertEquals(1, session.query(Invoice).count()) invoice = session.query(Invoice).one() # Set the recurring date on the invoice recurring_date = datetime.date.today() - datetime.timedelta(days=70) invoice.recurring_date = recurring_date copy_recurring() # There are now two invoices self.assertEquals(2, session.query(Invoice).count()) invoice = session.query(Invoice).filter_by(date=invoice_date).one() # Recurring date of the original invoice has been reset self.failIf(invoice.recurring_date) cloned_invoice = session.query(Invoice).filter_by( date=recurring_date).one() # New invoice has the recurring date of the original one as date self.assertEquals(recurring_date, cloned_invoice.date) self.assertEquals(recurring_date + datetime.timedelta(days=30), cloned_invoice.recurring_date) # Check whether attributes on invoice and invoice item have been cloned self.assertEquals(invoice.project_description, cloned_invoice.project_description) cloned_item = cloned_invoice.items[0] self.assertEquals(item.amount, cloned_item.amount) self.assertEquals(item.service_description, cloned_item.service_description) self.assertEquals(item.service_title, cloned_item.service_title)
def test_tax_amount(self): from seantisinvoice.models import Customer from seantisinvoice.models import CustomerContact from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem # Customer contact = CustomerContact() contact.customer = Customer() # Invoice invoice = Invoice() invoice.contact = contact self.assertEquals(0, invoice.tax_amount()) invoice.tax = 7.6 self.assertEquals(0, invoice.tax_amount()) item = InvoiceItem() item.amount = 100.0 invoice.items.append(item) self.assertEquals(7.6, invoice.tax_amount())
def _add_invoice(self): from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem from seantisinvoice.models import DBSession session = DBSession() customer = self._add_customer() invoice = Invoice() invoice.project_description = u'Project' invoice.date = datetime.date.today() invoice.due_date = invoice.date + datetime.timedelta(days=30) invoice.currency = u'CHF' invoice.contact = customer.contacts[0] session.add(invoice) # Add invoice item to the invoice item = InvoiceItem() item.item_number = 0 item.service_title = u'Testing' item.service_description = u'A lot of work!' item.amount = 1000.0 item.invoice = invoice session.add(item) session.flush() return invoice
def _add_invoice(self): from seantisinvoice.models import Invoice from seantisinvoice.models import InvoiceItem from seantisinvoice.models import DBSession session = DBSession() customer = self._add_customer() invoice = Invoice() invoice.project_description = u"Project" invoice.date = datetime.date.today() invoice.due_date = invoice.date + datetime.timedelta(days=30) invoice.currency = u"CHF" invoice.contact = customer.contacts[0] session.add(invoice) # Add invoice item to the invoice item = InvoiceItem() item.item_number = 0 item.service_title = u"Testing" item.service_description = u"A lot of work!" item.amount = 1000.0 item.invoice = invoice session.add(item) session.flush() return invoice
def copy_recurring(): today = datetime.date.today() session = DBSession() query = session.query(Invoice) query = query.filter(Invoice.recurring_date <= today) query = query.filter( or_(Invoice.recurring_stop == None, Invoice.recurring_stop > today)) for invoice in query.all(): # Clone invoice and invoice items invoice_clone = Invoice() invoice_clone.company = invoice.company invoice_clone.contact = invoice.contact invoice_clone.project_description = invoice.project_description invoice_clone.currency = invoice.currency invoice_clone.tax = invoice.tax session.add(invoice_clone) for item in invoice.items: item_clone = InvoiceItem() item_clone.item_number = item.item_number item_clone.amount = item.amount item_clone.hours = item.hours item_clone.days = item.days item_clone.service_description = item.service_description item_clone.service_title = item.service_title item_clone.invoice = invoice_clone session.add(item_clone) # Get new invoice number invoice_clone.invoice_number = next_invoice_number() # Adjust dates on cloned invoice invoice_clone.date = invoice.recurring_date invoice_clone.due_date = invoice_clone.date + (invoice.due_date - invoice.date) invoice_clone.recurring_date = invoice_clone.date + ( invoice.recurring_date - invoice.date) invoice_clone.recurring_stop = invoice.recurring_stop # Old invoice is not recurring anymore invoice.recurring_date = None transaction.commit()
def copy_recurring(): today = datetime.date.today() session = DBSession() query = session.query(Invoice) query = query.filter(Invoice.recurring_date <= today) query = query.filter(or_(Invoice.recurring_stop == None, Invoice.recurring_stop > today)) for invoice in query.all(): # Clone invoice and invoice items invoice_clone = Invoice() invoice_clone.company = invoice.company invoice_clone.contact = invoice.contact invoice_clone.project_description = invoice.project_description invoice_clone.currency = invoice.currency invoice_clone.tax = invoice.tax session.add(invoice_clone) for item in invoice.items: item_clone = InvoiceItem() item_clone.item_number = item.item_number item_clone.amount = item.amount item_clone.hours = item.hours item_clone.days = item.days item_clone.service_description = item.service_description item_clone.service_title = item.service_title item_clone.invoice = invoice_clone session.add(item_clone) # Get new invoice number invoice_clone.invoice_number = next_invoice_number() # Adjust dates on cloned invoice invoice_clone.date = invoice.recurring_date invoice_clone.due_date = invoice_clone.date + (invoice.due_date - invoice.date) invoice_clone.recurring_date = invoice_clone.date + (invoice.recurring_date - invoice.date) invoice_clone.recurring_stop = invoice.recurring_stop # Old invoice is not recurring anymore invoice.recurring_date = None transaction.commit()