示例#1
0
    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())
示例#2
0
    def _apply_data(self, invoice, converted):
        changed = False
        # Apply schema fields to the customer object
        field_names = [p.key for p in class_mapper(Invoice).iterate_properties]
        for field_name in field_names:
            if field_name in converted.keys():
                if getattr(invoice, field_name) != converted[field_name]:
                    setattr(invoice, field_name, converted[field_name])
                    changed = True
        if invoice.due_date != invoice.date + datetime.timedelta(
                days=converted['payment_term']):
            invoice.due_date = invoice.date + datetime.timedelta(
                days=converted['payment_term'])
            changed = True
        if converted[
                'recurring_term'] and invoice.recurring_date != invoice.date + datetime.timedelta(
                    days=converted['recurring_term']):
            invoice.recurring_date = invoice.date + datetime.timedelta(
                days=converted['recurring_term'])
            changed = True

        # Apply data of the items subforms
        session = DBSession()
        item_map = {}
        for item in invoice.items:
            item_map[item.id] = item
        for index, item_data in enumerate(converted['item_list']):
            if item_data['item_id']:
                item_id = item_data['item_id']
                item = item_map[item_id]
                del item_map[item_id]
            else:
                item = InvoiceItem()
                item.invoice = invoice
                session.add(item)
                changed = True
            # Apply schema fields to the invoice item object
            field_names = [
                p.key for p in class_mapper(InvoiceItem).iterate_properties
            ]
            for field_name in field_names:
                if field_name in item_data.keys():
                    if getattr(item, field_name) != item_data[field_name]:
                        setattr(item, field_name, item_data[field_name])
                        changed = True
            if item.item_number != index:
                item.item_number = index
                changed = True
        # Remove invoice items that have been removed in the form
        for item in item_map.values():
            invoice.items.remove(item)
            changed = True

        return changed
示例#3
0
 def _apply_data(self, invoice, converted):
     changed = False
     # Apply schema fields to the customer object
     field_names = [ p.key for p in class_mapper(Invoice).iterate_properties ]
     for field_name in field_names:
         if field_name in converted.keys():
             if getattr(invoice, field_name) != converted[field_name]:
                 setattr(invoice, field_name, converted[field_name])
                 changed = True
     if invoice.due_date != invoice.date + datetime.timedelta(days=converted['payment_term']):
         invoice.due_date = invoice.date + datetime.timedelta(days=converted['payment_term'])
         changed = True
     if converted['recurring_term'] and invoice.recurring_date != invoice.date + datetime.timedelta(days=converted['recurring_term']):
         invoice.recurring_date = invoice.date + datetime.timedelta(days=converted['recurring_term'])
         changed = True
             
     # Apply data of the items subforms
     session = DBSession()
     item_map = {}
     for item in invoice.items:
         item_map[item.id] = item
     for index, item_data in enumerate(converted['item_list']):
         if item_data['item_id']:
             item_id = item_data['item_id']
             item = item_map[item_id]
             del item_map[item_id]
         else:
             item = InvoiceItem()
             item.invoice = invoice
             session.add(item)
             changed = True
         # Apply schema fields to the invoice item object
         field_names = [ p.key for p in class_mapper(InvoiceItem).iterate_properties ]
         for field_name in field_names:
             if field_name in item_data.keys():
                 if getattr(item, field_name) != item_data[field_name]:
                     setattr(item, field_name, item_data[field_name])
                     changed = True
         if item.item_number != index:
             item.item_number = index
             changed = True
     # Remove invoice items that have been removed in the form
     for item in item_map.values():
         invoice.items.remove(item)
         changed = True
         
     return changed
示例#4
0
 def test_unit(self):
     from seantisinvoice.models import InvoiceItem
     item = InvoiceItem()
     self.assertEquals(u'', item.unit())
     item.hours = 5.5
     self.assertEquals(u'h', item.unit())
     item.hours = None
     item.days = 12
     self.assertEquals(u'PT', item.unit())
示例#5
0
    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())
示例#6
0
    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())
示例#7
0
    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())
示例#8
0
    def test_unit(self):
        from seantisinvoice.models import InvoiceItem

        item = InvoiceItem()
        self.assertEquals(u"", item.unit())
        item.hours = 5.5
        self.assertEquals(u"h", item.unit())
        item.hours = None
        item.days = 12
        self.assertEquals(u"PT", item.unit())
示例#9
0
 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())
示例#10
0
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()
示例#11
0
 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)
示例#12
0
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()
示例#13
0
    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)
示例#14
0
 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())
示例#15
0
 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
示例#16
0
    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
示例#17
0
    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())
示例#18
0
    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())