# -*- coding: utf-8 -*- # This file is part of the cashbook-module from m-ds for Tryton. # The COPYRIGHT file at the top level of this repository contains the # full copyright notices and license terms. file_name = 'bargeld.qif' file_category_income = 'category_income.qif' file_category_expense = 'category_expense.qif' company_name = 'm-ds' from quiffen import Qif from proteus import config, Model from datetime import date import json cfg1 = config.set_trytond( 'postgresql://postgres:test1@localhost:5432/tr44/', user='admin', config_file='/home/trytproj/projekt/py3tr60/etc/trytond.conf') # check active modules need_modules = ['cashbook', 'party', 'company'] IrModule = Model.get('ir.module') mod_lst = IrModule.find([ ('state', '=', 'activated'), ('name', 'in', need_modules), ]) if not len(mod_lst) == len(need_modules): raise ValueError('einige module sind nicht aktiviert!') def add_category(category, parent, company, cattype): """ check or add category """ Category = Model.get('cashbook.category') query = [ ('name', '=', category.name), ('cattype', '=', cattype), ] if parent is None: query.append(('parent', '=', None)) else : query.append(('parent.id', '=', parent.id)) cat1 = Category.find(query) if len(cat1) == 1: cashbook_category = cat1[0] print('- found:', cashbook_category.rec_name) elif len(cat1) == 0: print('- new-go:', getattr(parent, 'rec_name', '-'), category.name, category.income, category.expense) cashbook_category, = Category.create([{ 'name': category.name, 'cattype': cattype, 'parent': getattr(parent, 'id', None), }], context={'company': company.id}) cashbook_category = Category(cashbook_category) print('- new-ok:', cashbook_category.rec_name) else : raise ValueError('invalid num of category') for subcat in category.children: add_category(subcat, cashbook_category, company, cattype) # end add_category def get_category_tree(categories): """ convert categories in dict-tree """ result = [] for category in categories: cat1 = { 'name': category.name, 'type': 'in' if category.income == True else 'out' if category.expense == True else 'ups', } cat1['childs'] = get_category_tree(category.children) result.append(cat1) return result # end get_category_tree Company = Model.get('company.company') company1, = Company.find([('rec_name', '=', company_name)]) # income-category qif = Qif.parse(file_category_income, day_first=True) cat_tree = [] for catname in qif.categories.keys(): category = qif.categories[catname] #cat_tree.extend(get_category_tree([category])) add_category(category, None, company1, 'in') # expense-category qif = Qif.parse(file_category_expense, day_first=True) cat_tree = [] for catname in qif.categories.keys(): category = qif.categories[catname] #cat_tree.extend(get_category_tree([category])) add_category(category, None, company1, 'out')