function getSpendingMoney($accountId, $startDate)
{
    global $badgerDb;
    $accountManager = new AccountManager($badgerDb);
    $account = $accountManager->getAccountById($accountId);
    $account->setType('finished');
    $account->setOrder(array(array('key' => 'valutaDate', 'dir' => 'asc')));
    $account->setFilter(array(array('key' => 'valutaDate', 'op' => 'ge', 'val' => $startDate), array('key' => 'periodical', 'op' => 'eq', 'val' => false), array('key' => 'exceptional', 'op' => 'eq', 'val' => false)));
    $sum = new Amount();
    $realStartDate = false;
    while ($currentTransaction = $account->getNextFinishedTransaction()) {
        if (!$realStartDate) {
            $realStartDate = $currentTransaction->getValutaDate();
        }
        $sum->add($currentTransaction->getAmount());
    }
    $span = new Date_Span($realStartDate, new Date());
    $count = $span->toDays();
    if ($count > 0) {
        $sum->div($count);
    }
    return $sum;
}
示例#2
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;
}
 foreach ($currentBalances as $balanceKey => $balanceVal) {
     if (isset($totals[$balanceKey])) {
         $totals[$balanceKey]->add($balanceVal);
     } else {
         $totals[$balanceKey] = $balanceVal;
     }
 }
 //calculate spending money, if saving target should be reached
 $countDay = count($totals) - 1;
 //get numbers of days between today & endDate
 $laststanding = new Amount($totals[$selectedDate->getDate()]);
 $endDateBalance = $laststanding;
 //get balance of end date
 $freeMoney = new Amount($endDateBalance->sub($savingTarget));
 //endDateBalance - saving target = free money to spend
 $dailyPocketMoney = new Amount($freeMoney->div($countDay));
 //calculate daily pocket money = free money / count of Days
 $day = 0;
 $pocketMoney1EndValue = "";
 $pocketMoney2EndValue = "";
 foreach ($totals as $key => $val) {
     $tmp = new Date($key);
     $PocketMoney1Loop = new Amount($pocketMoney1);
     $val2 = new Amount($val->get());
     if ($showPocketMoney1 == 1) {
         $pocketMoney1EndValue = $val2->sub($PocketMoney1Loop->mul($day))->get();
     }
     $PocketMoney2Loop = new Amount($pocketMoney2);
     $val3 = new Amount($val->get());
     if ($showPocketMoney2 == 1) {
         $pocketMoney2EndValue = $val3->sub($PocketMoney2Loop->mul($day))->get();
示例#4
0
        $i++;
        if ($i > MAX_CATEGORIES) {
            $other->add($currentAmount);
            unset($amounts[$currentId]);
            unset($labels[$currentId]);
        }
    }
}
$total = new Amount(0);
$total->add($other);
foreach ($amounts as $currentAmount) {
    $total->add($currentAmount);
}
foreach ($amounts as $currentId => $currentAmount) {
    $percentage = new Amount($currentAmount);
    $percentage->div($total);
    if ($percentage->compare(MIN_PERCENTAGE) < 0) {
        $other->add($currentAmount);
        unset($amounts[$currentId]);
        unset($labels[$currentId]);
    }
}
if ($other->compare(0) != 0) {
    $amounts['other'] = $other;
    $labels['other'] = getBadgerTranslation2('statistics2', 'miscCategories');
}
$data = array();
$dataNames = array();
foreach ($amounts as $currentAmount) {
    $data[] = $currentAmount->mul($type == 'i' ? 1 : -1)->get();
    $dataNames[] = $currentAmount->getFormatted();