Exemplo n.º 1
0
    def sell_to_customer_create_invoice(self, cmID):
        """
        Add an invoice after adding a membership
        :param cmID: db.customers_memberships.id
        :return: db.invoices.id
        """
        from openstudio.os_customer_membership import CustomerMembership
        from openstudio.os_invoice import Invoice
        
        db = current.db
        T = current.T

        cm = CustomerMembership(cmID)

        # Check if price exists and > 0:
        if self.row.Price:
            igpt = db.invoices_groups_product_types(ProductType='membership')

            iID = db.invoices.insert(
                invoices_groups_id=igpt.invoices_groups_id,
                Description=cm.get_name(),
                Status='sent',
                payment_methods_id=cm.row.payment_methods_id
            )

            invoice = Invoice(iID)
            invoice.link_to_customer(cm.row.auth_customer_id)
            invoice.item_add_membership(
                cmID,
            )

            return iID
        else:
            return None
Exemplo n.º 2
0
def upgrade_to_201906():
    """
        Upgrade operations to 2019.06
    """
    from openstudio.os_invoice import Invoice

    query = (db.customers_notes.Processed == None)
    db(query).update(Processed=True)

    # Insert missing links to customers for some subscription invoices
    left = [
        db.invoices_items.on(db.invoices_items_customers_subscriptions.
                             invoices_items_id == db.invoices_items.id),
        db.invoices.on(db.invoices_items.invoices_id == db.invoices.id),
        db.invoices_customers.on(
            db.invoices_customers.invoices_id == db.invoices.id),
        db.customers_subscriptions.on(
            db.invoices_items_customers_subscriptions.
            customers_subscriptions_id == db.customers_subscriptions.id),
        db.auth_user.on(
            db.customers_subscriptions.auth_customer_id == db.auth_user.id)
    ]

    query = (db.invoices_customers.id == None)

    rows = db(query).select(db.invoices.id,
                            db.auth_user.id,
                            db.invoices_customers.id,
                            left=left)

    for row in rows:
        invoice = Invoice(row.invoices.id)
        invoice.link_to_customer(row.auth_user.id)
Exemplo n.º 3
0
    def sell_to_customer_create_invoice(self, cmID):
        """
        Add an invoice after adding a membership
        :param cmID: db.customers_memberships.id
        :return: db.invoices.id
        """
        from openstudio.os_customer_membership import CustomerMembership
        from openstudio.os_invoice import Invoice

        db = current.db
        T = current.T

        cm = CustomerMembership(cmID)

        # Check if price exists and > 0:
        if self.get_price_on_date(cm.row.Startdate):
            period_start = cm.row.Startdate
            period_end = cm.get_period_enddate(cm.row.Startdate)

            igpt = db.invoices_groups_product_types(ProductType='membership')

            iID = db.invoices.insert(
                invoices_groups_id=igpt.invoices_groups_id,
                Description=cm.get_name(),
                MembershipPeriodStart=period_start,
                MembershipPeriodEnd=period_end,
                Status='sent')

            invoice = Invoice(iID)
            invoice.link_to_customer(cm.row.auth_customer_id)
            invoice.item_add_membership(cmID, period_start, period_end)

            return iID
        else:
            return None
Exemplo n.º 4
0
    def add_get_form(self, cuID, csID=None, cmID=None, full_width=True):
        """
            Returns add form for an invoice
        """
        from os_customer import Customer
        from os_invoice import Invoice

        self._add_get_form_permissions_check()

        db = current.db
        T = current.T

        customer = Customer(cuID)
        self._add_get_form_set_default_values_customer(customer)
        self._add_get_form_enable_minimal_fields()
        if csID:
            self._add_get_form_enable_subscription_fields(csID)
        if cmID:
            self._add_get_form_enable_membership_fields(cmID)

        form = SQLFORM(db.invoices, formstyle='bootstrap3_stacked')

        elements = form.elements('input, select, textarea')
        for element in elements:
            element['_form'] = "invoice_add"

        form_element = form.element('form')
        form['_id'] = 'invoice_add'

        if form.process().accepted:
            iID = form.vars.id
            invoice = Invoice(
                iID
            )  # This sets due date and Invoice (calls invoice.on_create() #
            invoice.link_to_customer(cuID)
            self._add_reset_list_status_filter()

            if csID:
                invoice.link_to_customer_subscription(csID)
                invoice.item_add_subscription(form.vars.SubscriptionYear,
                                              form.vars.SubscriptionMonth)

            if cmID:
                invoice.item_add_membership(cmID,
                                            form.vars.MembershipPeriodStart,
                                            form.vars.MembershipPeriodEnd)

            redirect(URL('invoices', 'edit', vars={'iID': iID}))

        # So the grids display the fields normally
        for field in db.invoices:
            field.readable = True

        return form
Exemplo n.º 5
0
    def batch_generate_teachers_invoices(self, year, month):
        """
        :return: Int - number of generated invoices
        """
        from general_helpers import NRtoMonth
        from os_teacher import Teacher
        from os_teachers import Teachers
        from openstudio.os_invoice import Invoice
        import datetime

        db = current.db
        T = current.T

        # Get list of teachers
        teachers = Teachers()
        result = teachers.get_teachers_list_classes_in_month(year, month)

        #print teacher_classes
        invoices_created = 0

        for teID in result:
            teacher_classes = result[teID]['classes']
            number_of_classes = result[teID]['classes_count']
            if not number_of_classes:
                continue

            teacher = Teacher(teID)
            default_rate = teacher.get_payment_fixed_rate_default()

            if not default_rate:
                continue  # No default rate, not enough data to create invoice

            # Check if we have an invoice already, if so, skip
            query = (db.invoices_customers.auth_customer_id == teID) & \
                    (db.invoices.TeacherPayment == True) & \
                    (db.invoices.TeacherPaymentMonth == month) & \
                    (db.invoices.TeacherPaymentYear == year) & \
                    (db.invoices_customers.invoices_id == db.invoices.id)

            if db(query).count():
                continue

            # Create invoice
            invoices_created += 1
            igpt = db.invoices_groups_product_types(
                ProductType='teacher_payments')
            iID = db.invoices.insert(
                invoices_groups_id=igpt.invoices_groups_id,
                TeacherPayment=True,
                TeacherPaymentMonth=month,
                TeacherPaymentYear=year,
                Description=T('Classes') + ' ' + NRtoMonth(month) + ' ' +
                unicode(year),
                Status='sent')

            invoice = Invoice(iID)
            invoice.link_to_customer(teID)

            for date, rows in sorted(teacher_classes.iteritems()):
                prev_class_end = datetime.datetime(date.year, date.month,
                                                   date.day, 0, 0)
                for row in rows:
                    ## Check not adding travel allowance for consecutive classes
                    class_start = datetime.datetime(
                        date.year, date.month, date.day,
                        row.classes.Starttime.hour,
                        row.classes.Starttime.minute)

                    consecutive = class_start <= (
                        prev_class_end + datetime.timedelta(minutes=30))

                    if not consecutive:
                        invoice.item_add_teacher_class_credit_travel_allowance(
                            row.classes.id, date)

                    prev_class_end = datetime.datetime(
                        date.year, date.month, date.day,
                        row.classes.Endtime.hour, row.classes.Endtime.minute)

                    # Add class
                    invoice.item_add_teacher_class_credit_payment(
                        row.classes.id, date)

            invoice.set_amounts()

        return invoices_created