File indexing completed on 2024-05-12 05:06:04

0001 /*
0002     KMyMoney transaction importing module - searches for a matching scheduled transaction
0003 
0004     SPDX-FileCopyrightText: 2012 Lukasz Maszczynski <lukasz@maszczynski.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "scheduledtransactionmatchfinder.h"
0009 
0010 #include <QDebug>
0011 #include <QDate>
0012 
0013 #include "mymoneyfile.h"
0014 #include "mymoneyschedule.h"
0015 #include "kmymoneyutils.h"
0016 
0017 ScheduledTransactionMatchFinder::ScheduledTransactionMatchFinder(const MyMoneyAccount& account, int matchWindow)
0018     : TransactionMatchFinder(matchWindow), m_account(account)
0019 {
0020 }
0021 
0022 void ScheduledTransactionMatchFinder::createListOfMatchCandidates()
0023 {
0024     listOfMatchCandidates = MyMoneyFile::instance()->scheduleList(m_account.id());
0025     qDebug() << "Considering" << listOfMatchCandidates.size() << "schedule(s) for matching the transaction";
0026 }
0027 
0028 void ScheduledTransactionMatchFinder::findMatchInMatchCandidatesList()
0029 {
0030     for (const MyMoneySchedule& schedule : qAsConst(listOfMatchCandidates)) {
0031         QDate nextDueDate = schedule.nextDueDate();
0032         bool nextDueDateWithinMatchWindowRange = (nextDueDate >= importedTransaction.postDate().addDays(-m_matchWindow))
0033                 && (nextDueDate <= importedTransaction.postDate().addDays(m_matchWindow));
0034         if (schedule.isOverdue() || nextDueDateWithinMatchWindowRange) {
0035             MyMoneyTransaction scheduledTransaction = KMyMoneyUtils::scheduledTransaction(schedule);
0036 
0037             findMatchingSplit(scheduledTransaction, schedule.variation());
0038             if (matchResult != MatchNotFound) {
0039                 matchedSchedule.reset(new MyMoneySchedule(schedule));
0040                 return;
0041             }
0042         }
0043     }
0044 }