示例#1
0
function importMatching($importedTransaction, $accountId)
{
    global $us;
    global $badgerDb;
    static $dateDelta = null;
    static $amountDelta = null;
    static $textSimilarity = null;
    static $categories = null;
    if (is_null($dateDelta)) {
        try {
            $dateDelta = $us->getProperty('matchingDateDelta');
        } catch (BadgerException $ex) {
            $dateDelta = 5;
        }
        try {
            $amountDelta = $us->getProperty('matchingAmountDelta');
        } catch (BadgerException $ex) {
            $amountDelta = 0.1;
        }
        try {
            $textSimilarity = $us->getProperty('matchingTextSimilarity');
        } catch (BadgerException $ex) {
            $textSimilarity = 0.25;
        }
        $categoryManager = new CategoryManager($badgerDb);
        while ($currentCategory = $categoryManager->getNextCategory()) {
            $categories[$currentCategory->getId()] = preg_split('/[\\n]+/', $currentCategory->getKeywords(), -1, PREG_SPLIT_NO_EMPTY);
        }
    }
    if (!$importedTransaction['valutaDate']) {
        return $importedTransaction;
    }
    $minDate = new Date($importedTransaction['valutaDate']);
    $minDate->subtractSeconds($dateDelta * 24 * 60 * 60);
    $maxDate = new Date($importedTransaction['valutaDate']);
    $maxDate->addSeconds($dateDelta * 24 * 60 * 60);
    if (!$importedTransaction['amount']) {
        return $importedTransaction;
    }
    $minAmount = new Amount($importedTransaction['amount']);
    $minAmount->mul(1 - $amountDelta);
    $maxAmount = new Amount($importedTransaction['amount']);
    $maxAmount->mul(1 + $amountDelta);
    $accountManager = new AccountManager($badgerDb);
    $account = $accountManager->getAccountById($accountId);
    $account->setFilter(array(array('key' => 'valutaDate', 'op' => 'ge', 'val' => $minDate), array('key' => 'valutaDate', 'op' => 'le', 'val' => $maxDate), array('key' => 'amount', 'op' => 'ge', 'val' => $minAmount), array('key' => 'amount', 'op' => 'le', 'val' => $maxAmount)));
    $similarTransactions = array();
    while ($currentTransaction = $account->getNextTransaction()) {
        $titleSimilarity = getSimilarity($importedTransaction['title'], $currentTransaction->getTitle(), $textSimilarity);
        $descriptionSimilarity = getSimilarity($importedTransaction['description'], $currentTransaction->getDescription(), $textSimilarity);
        $transactionPartnerSimilarity = getSimilarity($importedTransaction['transactionPartner'], $currentTransaction->getTransactionPartner(), $textSimilarity);
        $currDate = $currentTransaction->getValutaDate();
        $impDate = $importedTransaction['valutaDate'];
        $dateSimilarity = 1 - abs(Date_Calc::dateDiff($currDate->getDay(), $currDate->getMonth(), $currDate->getYear(), $impDate->getDay(), $impDate->getMonth(), $impDate->getYear())) / $dateDelta;
        $cmpAmount = new Amount($currentTransaction->getAmount());
        $impAmount = new Amount($importedTransaction['amount']);
        $cmpAmount->sub($impAmount);
        $cmpAmount->abs();
        $impAmount->mul($amountDelta);
        $impAmount->abs();
        $amountSimilarity = 1 - $cmpAmount->div($impAmount)->get();
        $currentTextSimilarity = ($titleSimilarity + $descriptionSimilarity + $transactionPartnerSimilarity) / 3;
        //		if ($currentTextSimilarity >= $textSimilarity) {
        $overallSimilarity = ($titleSimilarity + $descriptionSimilarity + $transactionPartnerSimilarity + $dateSimilarity + $amountSimilarity) / 5;
        //$similarTransactions["$overallSimilarity t:$titleSimilarity d:$descriptionSimilarity tp:$transactionPartnerSimilarity vd:$dateSimilarity a:$amountSimilarity"] = $currentTransaction;
        $similarTransactions[$overallSimilarity] = $currentTransaction;
        //		}
    }
    krsort($similarTransactions);
    if (count($similarTransactions)) {
        $importedTransaction['similarTransactions'] = $similarTransactions;
        return $importedTransaction;
    }
    if ($importedTransaction['categoryId']) {
        return $importedTransaction;
    }
    $transactionStrings = array($importedTransaction['title'], $importedTransaction['description'], $importedTransaction['transactionPartner']);
    foreach ($transactionStrings as $currentTransactionString) {
        foreach ($categories as $currentCategoryId => $keywords) {
            foreach ($keywords as $keyword) {
                if (stripos($currentTransactionString, trim($keyword)) !== false) {
                    $importedTransaction['categoryId'] = $currentCategoryId;
                    break 3;
                }
                //if keyword found
            }
            //foreach keywords
        }
        //foreach categories
    }
    //foreach transactionStrings
    return $importedTransaction;
}
function getCategoryExpense()
{
    global $badgerDb;
    $categoryManager = new CategoryManager($badgerDb);
    $result = "<script type='text/javascript'> var categoryExpense = new Object();\n";
    while ($category = $categoryManager->getNextCategory()) {
        $result .= "categoryExpense[" . $category->getId() . "] = " . ($category->getExpense() ? 'true' : 'false') . ";\n";
    }
    $result .= "</script>";
    return $result;
}
/*
* ____          _____   _____ ______ _____  
*|  _ \   /\   |  __ \ / ____|  ____|  __ \ 
*| |_) | /  \  | |  | | |  __| |__  | |__) |
*|  _ < / /\ \ | |  | | | |_ |  __| |  _  / 
*| |_) / ____ \| |__| | |__| | |____| | \ \ 
*|____/_/    \_\_____/ \_____|______|_|  \_\
* Open Source Financial Management
* Visit http://www.badger-finance.org 
*
**/
define('BADGER_ROOT', '../..');
require_once BADGER_ROOT . '/includes/fileHeaderFrontEnd.inc.php';
require_once BADGER_ROOT . '/modules/account/CategoryManager.class.php';
header('Content-Type: text/plain');
define('endl', "\n");
$cm = new CategoryManager($badgerDb);
while ($cat = $cm->getNextCategory()) {
    echo 'Category Title: ' . $cat->getTitle() . endl;
}
$cat1 = $cm->getCategoryById(1);
echo 'Category Id: ' . $cat1->getId() . endl;
$cat2 = $cm->addCategory('Neue Kategorie ' . rand(0, 100), 'Bähschraipunk');
echo 'New Category Title: ' . $cat2->getTitle() . endl;
$cat3 = $cm->addCategory('Temporäre Kategorie');
$tmpId = $cat3->getId();
echo 'Temporary Category Id: ' . $tmpId . endl;
$cm->deleteCategory($tmpId);
$cat4 = $cm->getCategoryById($tmpId);
echo 'Temporary Category Title (never shown): ' . $cat4->getTitle() . endl;
function getCategorySelectArray()
{
    global $badgerDb;
    $cm = new CategoryManager($badgerDb);
    $order = array(array('key' => 'parentTitle', 'dir' => 'asc'), array('key' => 'title', 'dir' => 'asc'));
    $cm->setOrder($order);
    $parentCats = array();
    $parentCats['NULL'] = "";
    while ($cat = $cm->getNextCategory()) {
        $cat->getParent();
    }
    $cm->resetCategories();
    while ($cat = $cm->getNextCategory()) {
        if (is_null($cat->getParent())) {
            $parentCats[$cat->getId()] = $cat->getTitle();
            $children = $cat->getChildren();
            //echo "<pre>"; print_r($children); echo "</pre>";
            if ($children) {
                foreach ($children as $key => $value) {
                    $parentCats[$value->getId()] = " - " . $value->getTitle();
                }
            }
        }
    }
    return $parentCats;
}