From 38770639bedc2ef9f8f9a91b5f40d77642d87f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Fri, 6 Jul 2018 20:23:31 +0200 Subject: [PATCH] Add multi month operation support : Normal operation with description "... (XX/YY)" will be automatically forwarded to next month --- ChangeLog | 13 ++++ src/controller/KissCount.cpp | 114 ++++++++++++++++++++++++++++++----- src/controller/KissCount.hpp | 3 + 3 files changed, 114 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index e5f63d6..f999986 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +v0.8 (06/08/2018) +** User ** + Add multi month operation support : Normal operation with description "... (XX/YY)" will be forwarded to next month +** Dev ** +** Bugs ** + + +v0.7.1 (07/05/2018) +** User ** +** Dev ** +** Bugs ** + web: start and end date only compared to current date and not selected one + v0.7 (25/03/2018) ** User ** Set background calendar color to red or yellow when one account is negative or less than 200€ (configurable) diff --git a/src/controller/KissCount.cpp b/src/controller/KissCount.cpp index ddbe0f9..f4fc774 100644 --- a/src/controller/KissCount.cpp +++ b/src/controller/KissCount.cpp @@ -24,7 +24,9 @@ #include "KissCount.hpp" #include - + +#include + std::vector * KissCount::_importEngines; std::vector * KissCount::_exportEngines; @@ -285,12 +287,62 @@ std::map > KissCount::GetAllOperations() return _db->GetAllOperations(_user); } +int KissCount::CopyGeneratedOperation(int monthTo, int yearTo, Operation& opOrig, Operation& opDest, + std::map &meta, QRegularExpression *regexp=0) +{ + opDest = opOrig; + opDest.month = monthTo; + opDest.year = yearTo; + opDest.checked = false; + opDest.id = AddOperation(opDest); + opDest.childs.clear(); + if (opOrig.meta) + meta[opOrig.id] = opDest.id; + + if (regexp) + { + QRegularExpressionMatch match; + int first, second; + + match = regexp->match(opOrig.description); + if (match.hasMatch()) + { + first = match.captured(1).toInt(); + second = match.captured(2).toInt(); + + if (first < second) + { + first++; + opDest.description = opOrig.description.left(match.capturedStart(1)); + opDest.description += QString::number(first) + QString("/") + QString::number(second); + opDest.description += opOrig.description.right(opOrig.description.length()-match.capturedEnd(2)); + } + } + } + + if (!QDate::isValid(opDest.year, opDest.month+1, opDest.day+1)) + { + QDate d(opDest.year, opDest.month+1, 1); + opDest.day = d.daysInMonth()-1; + return 1; + } + + return 0; +} + int KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearTo) { std::vector::iterator it, it2; + std::vector::iterator childIt, copyIt; std::map meta; - Operation op; - int nb_update_days = 0; + Operation op, op2, parentOp, childOp; + std::vector toCopy; + int nb_update_days = 0, first, second; + QRegularExpressionMatch match; + bool hasmatch; + /* Try to find something like XXX(1/5)XXX */ + QRegularExpression regexp(".+\\((\\d+)/(\\d+)\\).*"); + QString newDescription; _db->GenerateMonth(_user, monthFrom, yearFrom, monthTo, yearTo); @@ -305,26 +357,56 @@ int KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearT it != (*_user->_operations[yearFrom])[monthFrom].end(); it++) { - if (!it->fix_cost) continue; - op = *it; - op.month = monthTo; - op.year = yearTo; - op.checked = false; - op.id = AddOperation(op); - op.childs.clear(); - if (op.meta) - meta[it->id] = op.id; + hasmatch = false; - if (!QDate::isValid(op.year, op.month+1, op.day+1)) + if (!it->fix_cost) { - QDate d(op.year, op.month+1, 1); - op.day = d.daysInMonth()-1; - nb_update_days++; + match = regexp.match(it->description); + if (!match.hasMatch()) + continue; + hasmatch = true; } + if (hasmatch) + { + first = match.captured(1).toInt(); + second = match.captured(2).toInt(); + + if (first >= second) + continue; + + if (!std::count(toCopy.begin(), toCopy.end(), it->id)) + { + /* Copy only parent if has */ + if (it->parent) + { + if (std::count(toCopy.begin(), toCopy.end(), it->parent)) + continue; + + toCopy.push_back(it->parent); + } + else + toCopy.push_back(it->id); + } + } + else + nb_update_days += CopyGeneratedOperation(monthTo, yearTo, *it, op, meta); (*_user->_operations[yearTo])[monthTo].push_back(op); } + /* Restart search and filter by copy */ + for(it = (*_user->_operations[yearFrom])[monthFrom].begin(); + it != (*_user->_operations[yearFrom])[monthFrom].end(); + it++) + { + if (std::count(toCopy.begin(), toCopy.end(), it->id) || + (it->parent && std::count(toCopy.begin(), toCopy.end(), it->parent))) + { + nb_update_days += CopyGeneratedOperation(monthTo, yearTo, *it, childOp, meta, ®exp); + (*_user->_operations[yearTo])[monthTo].push_back(childOp); + } + } + // Re Generate parents for(it = (*_user->_operations[yearTo])[monthTo].begin(); it != (*_user->_operations[yearTo])[monthTo].end(); diff --git a/src/controller/KissCount.hpp b/src/controller/KissCount.hpp index f4fc800..61a2510 100644 --- a/src/controller/KissCount.hpp +++ b/src/controller/KissCount.hpp @@ -165,6 +165,9 @@ private: static std::vector *_importEngines; static std::vector *_exportEngines; + + int CopyGeneratedOperation(int monthTo, int yearTo, Operation& opOrig, Operation& opDest, + std::map &meta, QRegularExpression *regexp); }; #endif