def make_expense_nodes(conn, session):
    from autonomie.models.node import Node
    req = "select max(id) from node"
    result = conn.execute(req).fetchall()
    max_id = result[0][0]
    print("The new max_id is : %s" % max_id)

    request = "select id, month, year from expense_sheet"
    result = conn.execute(request)

    op.execute("SET FOREIGN_KEY_CHECKS=0;")

    from autonomie.models.treasury import get_expense_sheet_name

    for index, (id, month,year) in enumerate(result):
        max_id += 1
        new_id = max_id
        name = get_expense_sheet_name(month, year)
        node = Node(
            id=new_id,
            name=name,
            type_='expensesheet',
        )
        session.add(node)
        # Update relationships
        for key, table in ("sheet_id", "baseexpense_line"), ("expense_sheet_id", "communication"):
            op.execute("update {0} set {2}={1} where {2}={3}".format(table, new_id, key, id))
        op.execute("update expense_sheet set id={0} where id={1}".format(new_id, id))
        if index % 50 == 0:
            session.flush()
    op.execute("SET FOREIGN_KEY_CHECKS=1;")
Exemplo n.º 2
0
def get_new_expense_sheet(year, month, cid, uid):
    """
        Return a new expense sheet for the given 4-uple
    """
    expense = ExpenseSheet()
    expense.name = get_expense_sheet_name(month, year)
    expense.year = year
    expense.month = month
    expense.company_id = cid
    expense.user_id = uid
    query = ExpenseTelType.query()
    query = query.filter(ExpenseTelType.active==True)
    teltypes = query.filter(ExpenseTelType.initialize==True)
    for type_ in teltypes:
        line = ExpenseLine(type_id=type_.id, ht=0, tva=0,
                description=type_.label)
        expense.lines.append(line)
    return expense