示例#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;
}
    $dailyPocketMoneyLoop = new Amount($dailyPocketMoney->get());
    $val1 = new Amount($val->get());
    if ($showSavingTarget == 1) {
        $chart['chart_data'][$numberOfGraph][] = $val1->sub($dailyPocketMoneyLoop->mul($day))->get();
        $numberOfGraph++;
    }
    $PocketMoney1Loop = new Amount($pocketMoney1->get());
    $val2 = new Amount($val->get());
    if ($showPocketMoney1 == 1) {
        $chart['chart_data'][$numberOfGraph][] = $val2->sub($PocketMoney1Loop->mul($day))->get();
        $numberOfGraph++;
    }
    $PocketMoney2Loop = new Amount($pocketMoney2->get());
    $val3 = new Amount($val->get());
    if ($showPocketMoney2 == 1) {
        $chart['chart_data'][$numberOfGraph][] = $val3->sub($PocketMoney2Loop->mul($day))->get();
        $numberOfGraph++;
    }
    $day++;
}
//for documentation for the following code see: http://www.maani.us/charts/index.php?menu=Reference
$chart['chart_type'] = "line";
$chart['axis_category'] = array('skip' => $countDay / 12, 'font' => "Arial", 'bold' => false, 'size' => 10, 'color' => "000000", 'alpha' => 100, 'orientation' => "horizontal");
$chart['axis_ticks'] = array('value_ticks' => true, 'category_ticks' => true, 'position' => "centered", 'major_thickness' => 2, 'major_color' => "000000", 'minor_thickness' => 1, 'minor_color' => "000000", 'minor_count' => 4);
$chart['axis_value'] = array('min' => 0, 'max' => 0, 'steps' => 10, 'prefix' => "", 'suffix' => "", 'decimals' => 0, 'decimal_char' => ".", 'separator' => "", 'show_min' => true, 'font' => "Arial", 'bold' => false, 'size' => 10, 'color' => "000000", 'alpha' => 75, 'orientation' => "horizontal");
$chart['chart_border'] = array('top_thickness' => 1, 'bottom_thickness' => 1, 'left_thickness' => 1, 'right_thickness' => 1, 'color' => "000000");
$chart['chart_pref'] = array('line_thickness' => 1, 'point_shape' => "none", 'fill_shape' => false);
$chart['chart_grid_h'] = array('thickness' => 1, 'color' => "000000", 'alpha' => 15, 'type' => "solid");
$chart['chart_grid_v'] = array('thickness' => 1, 'color' => "000000", 'alpha' => 5, 'type' => "dashed");
$chart['chart_rect'] = array('x' => 50, 'y' => 50, 'width' => 700, 'height' => 300, 'positive_color' => "ffffff", 'negative_color' => "000000", 'positive_alpha' => 100, 'negative_alpha' => 10);
$chart['chart_value'] = array('prefix' => "", 'suffix' => "", 'decimals' => 0, 'decimal_char' => ".", 'separator' => "", 'position' => "cursor", 'hide_zero' => true, 'as_percentage' => false, 'font' => "Arial", 'bold' => false, 'size' => 10, 'color' => "000000", 'alpha' => 90);
 $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();
     }
     $day++;
 }
 //foreach($totals as $key => $val) {
 echo '<forecastData>';
 $insertChart = urlencode($insertChart);
 echo "<insertChart>{$insertChart}</insertChart>";
 if ($showSavingTarget == 1) {
     $dailyPocketMoneyValue = $dailyPocketMoney->getFormatted();
     echo "<dailyPocketMoneyValue>{$dailyPocketMoneyValue}</dailyPocketMoneyValue>";
 }
 if ($pocketMoney1EndValue) {
     $printedPocketMoney1EndValue = new Amount($pocketMoney1EndValue);
     $balancedEndDate1 = $printedPocketMoney1EndValue->getFormatted();
     echo "<balancedEndDate1>{$balancedEndDate1}</balancedEndDate1>";
/**
 * Returns the Account balance for $account at the end of each day between $startDate and $endDate.
 * 
 * Considers the planned transactions of $account.
 * 
 * @param object $account The Account object for which the balance should be calculated. 
 * It should be 'fresh', i. e. no transactions of any type should have been fetched from it.
 * @param object $startDate The first date the balance should be calculated for as Date object.
 * @param object $endDate The last date the balance should be calculated for as Date object.
 * @return array Array of Amount objects corresponding to the balance of $account at each day between
 * $startDate and $endDate. The array keys are the dates as ISO-String (yyyy-mm-dd). 
 */
function getDailyAmount($account, $startDate, $endDate, $isoDates = true, $startWithBalance = false, $includePlannedTransactions = false)
{
    global $badgerDb;
    $account->setTargetFutureCalcDate($endDate);
    $account->setOrder(array(array('key' => 'valutaDate', 'dir' => 'asc')));
    if (!$includePlannedTransactions) {
        $account->setType('finished');
    }
    $result = array();
    $startDate->setHour(0);
    $startDate->setMinute(0);
    $startDate->setSecond(0);
    $endDate->setHour(0);
    $endDate->setMinute(0);
    $endDate->setSecond(0);
    $currentDate = new Date($startDate);
    $currentAmount = new Amount();
    $firstRun = true;
    //foreach transaction
    while ($currentTransaction = $account->getNextTransaction()) {
        if ($currentDate->after($endDate)) {
            //we reached $endDAte
            break;
        }
        if ($firstRun && $startWithBalance) {
            $currentAmount = new Amount($currentTransaction->getBalance());
            $currentAmount->sub($currentTransaction->getAmount());
            $firstRun = false;
        }
        //fill all dates between last and this transaction with the old amount
        while (is_null($tmp = $currentTransaction->getValutaDate()) ? false : $currentDate->before($tmp)) {
            if ($isoDates) {
                $key = $currentDate->getDate();
            } else {
                $key = $currentDate->getTime();
            }
            $result[$key] = new Amount($currentAmount);
            $currentDate->addSeconds(24 * 60 * 60);
            if ($currentDate->after($endDate)) {
                //we reached $endDAte
                break;
            }
        }
        $currentAmount->add($currentTransaction->getAmount());
    }
    if ($firstRun && $startWithBalance) {
        $newAccountManager = new AccountManager($badgerDb);
        $newAccount = $newAccountManager->getAccountById($account->getId());
        $newAccount->setOrder(array(array('key' => 'valutaDate', 'dir' => 'asc')));
        while ($newTransaction = $newAccount->getNextTransaction()) {
            $currentDate = $newTransaction->getValutaDate();
            if ($currentDate->after($startDate)) {
                //we reached $endDAte
                break;
            }
            $currentAmount = new Amount($newTransaction->getBalance());
        }
        $currentDate = new Date($startDate);
        if ($isoDates) {
            $key = $currentDate->getDate();
        } else {
            $key = $currentDate->getTime();
        }
        $result[$key] = new Amount($currentAmount);
    }
    //fill all dates after the last transaction with the newest amount
    while (Date::compare($currentDate, $endDate) <= 0) {
        if ($isoDates) {
            $key = $currentDate->getDate();
        } else {
            $key = $currentDate->getTime();
        }
        $result[$key] = new Amount($currentAmount);
        $currentDate->addSeconds(24 * 60 * 60);
    }
    return $result;
}