def addSearchString(searchString,categoryID): """Ajouter une nouvelle règle""" try: db = dbUtils.createConnexion() db.insert('searchstrings', categoryID=categoryID, searchString=searchString) return 0 except IndexError: return -1
def addCategory(category): """Ajouter une nouvelle catégorie""" try: db = dbUtils.createConnexion() db.insert('categories', category=category) return 0 except IndexError: return -1
def getAllCategories(): """Retourne toutes les catégories existantes dans la base""" db = dbUtils.createConnexion() allCategories = db.select('categories',order="category") categories = {} for category in allCategories: categories[category["id"]]=category["category"] return categories
def deleteSearchString(searchStringID): """Supprime la règle dans la base en mettant par défaut les transactions à l'ID=1""" db = dbUtils.createConnexion() try: db.query("DELETE FROM searchstrings WHERE id=$id", vars=dict(id=searchStringID)) return True except IndexError: print "La régle %s est introuvable!" % categoryID return False
def updateSearchString(searchStringID,searchString,categoryID): """Met à jour une règle dans la base""" db = dbUtils.createConnexion() try: db.query("UPDATE searchstrings SET searchString=$searchString,categoryID=$categoryID WHERE id=$id", vars=dict(searchString=searchString,categoryID=categoryID, id=searchStringID)) return True except IndexError: print "Règle %s est introuvable!" % transactionID return False
def updateCategory(categoryID,category): """Met à jour la catégorie dans la base""" db = dbUtils.createConnexion() try: db.query("UPDATE categories SET category=$category WHERE id=$id", vars=dict(category=category, id=categoryID)) return True except IndexError: print "La Categorie %s est introuvable!" % categoryID return False
def getCategory(categoryID): """Retourne la catégorie categoryID ou -1 si non existante""" db = dbUtils.createConnexion() myvar = {"id":categoryID} checkExisting = db.select('categories',myvar, where="id=$id") try: return checkExisting[0] except IndexError: return -1
def commitRules(): """Rejoue toutes les règles""" db = dbUtils.createConnexion() transactions = transaction.getAllTransactions() searchStrings = getAllSearchStrings() for myTransaction in transactions: categoryID = myTransaction["categoryID"] autoCategorize(myTransaction,searchStrings) if myTransaction["categoryID"] != categoryID: transaction.updateTransaction(myTransaction["id"],myTransaction["comment"],myTransaction["categoryID"],db) return True
def deleteCategory(categoryID): """Supprime la catégorie dans la base en mettant par défaut les transactions à l'ID=1""" db = dbUtils.createConnexion() try: db.query("UPDATE transactions SET categoryID=1 WHERE categoryID=$id", vars=dict(id=categoryID)) db.query("UPDATE searchStrings SET categoryID=1 WHERE categoryID=$id", vars=dict(id=categoryID)) db.query("DELETE FROM categories WHERE id=$id", vars=dict(id=categoryID)) return True except IndexError: print "La Categorie %s est introuvable!" % categoryID return False
def getSearchString(searchStringID): """Récupère une règle dans la base""" db = dbUtils.createConnexion() myvar = {"id":searchStringID} checkExisting = db.select('searchstrings',myvar, where="id=$id") try: searchString = checkExisting[0] categoryID = searchString["categoryID"] searchString["category"] = category.getCategory(categoryID)["category"] return searchString except IndexError: print "Règle %s est introuvable!" % searchStringID return -1
def getAllSearchStrings(): """Retourne tous les cas de catégorisation automatique""" db = dbUtils.createConnexion() allSearchStrings = db.select('searchStrings') searchStrings = {} for mySearch in allSearchStrings: searchString = {} searchString["id"]=mySearch["id"] searchString["searchString"]=mySearch["searchString"] searchString["categoryID"]=mySearch["categoryID"] searchString["category"]=category.getCategory(int(searchString["categoryID"]))["category"] searchStrings[searchString["id"]]=searchString return searchStrings
def getTransaction(transactionID): """Récupère une transaction dans la base""" db = dbUtils.createConnexion() myvar = {"id": transactionID} checkExisting = db.select("transactions", myvar, where="id=$id") try: transaction = checkExisting[0] categoryID = transaction["categoryID"] transaction["category"] = category.getCategory(categoryID)["category"] return transaction except IndexError: print "Transaction %s est introuvable!" % transactionID return -1
def updateTransaction(transactionID, comment, categoryID, db=0): """Met à jour une transaction dans la base""" if db == 0: db = dbUtils.createConnexion() myvars = {"id": transactionID, "comment": comment, "categoryID": categoryID} try: db.query( "UPDATE transactions SET comment=$comment,categoryID=$categoryID WHERE id=$id", vars=dict(comment=comment, categoryID=categoryID, id=transactionID), ) return True except IndexError: print "Transaction %s est introuvable!" % transactionID return False
def getAllTransactions(orderby="dated"): """Retourne tous les transactions présentes dans la base""" db = dbUtils.createConnexion() if orderby == "dated": orderby = "dated DESC" else: orderby = orderby + " ASC" allTransactions = db.select("transactions", order=orderby) categoriesList = category.getAllCategories() transactions = [] for transaction in allTransactions: myCategory = categoriesList[transaction["categoryID"]] transaction["category"] = myCategory transactions.append(transaction) return transactions
def insertTransactions(fichier): """Insère une transaction dans la base""" transactions = [] db = dbUtils.createConnexion() searchStrings = searchString.getAllSearchStrings() newTrans, existingTrans = 0, 0 # transformation liste pour enlever \n et champs non nécessaires newLines = [] for element in fichier: if element in ["\n", "^\n", "!Type:Bank\n"]: pass else: newLines.append(element.decode("iso-8859-1")) # boucle sur les lines par groupe de 4 for i in range(0, len(newLines) - 1, 4): # date: on enleve premier caractère + retour chariot dated = newLines[i][1:].replace("\n", "").strip() dated = "20" + dated[6:] + "-" + dated[3:5] + "-" + dated[0:2] # valeur de la transaction value = newLines[i + 1][1:].replace("\n", "").strip() # ref de la transaction ref1 = newLines[i + 2][1:].replace("\n", "").strip() # traitement des commentaires # TODO:penser à rajouter un test sur les index comment = newLines[i + 3][1:].replace("\n", "").strip() indexC = comment.find("CONTREVALEUR EN FRANCS ") typeTrans = comment[:indexC] # type de transaction comment = comment[indexC + 28 :].partition(" ")[2] # commentaire if indexC > 0: indexC2 = comment.find(" LE ") comment = comment[:indexC2].strip() if comment == "": comment = typeTrans comment = comment[:49] # on coupe à 49 caractères categoryID = 1 transaction = {"dated": dated, "ref1": ref1, "value": value, "comment": comment, "categoryID": categoryID} transaction = searchString.autoCategorize(transaction, searchStrings) transactions.append(transaction) myvar = {"ref1": ref1, "dated": dated, "value": value, "comment": comment} try: # si cela existe déjà alors on ne l'écrit pas dans la base checkExisting = db.select( "transactions", myvar, where="ref1=$ref1 and dated=$dated and value=$value and comment=$comment" ) if checkExisting[0] != "neant": print u"Transaction %s du %s existante" % (ref1, dated) existingTrans = existingTrans + 1 except IndexError: db.insert( "transactions", dated=transaction["dated"], ref1=transaction["ref1"], value=transaction["value"], comment=transaction["comment"], categoryID=transaction["categoryID"], ) newTrans = newTrans + 1 return transactions, newTrans, existingTrans
def getTransactionsByDate(dateDebut, dateFin): """Retourne toutes les transactione entre dateDebut et dateFin""" db = dbUtils.createConnexion() myvar = {"dateDebut": dateDebut, "dateFin": dateFin} transactions = db.select("transactions", myvar, where="dated between $dateDebut and $dateFin") return transactions