示例#1
0
 public function testAdd()
 {
     $amount100 = new Amount('100');
     $amount150 = $amount100->add(new Amount('50'));
     $this->assertSame('100', $amount100->getString(0));
     $this->assertSame('150', $amount150->getString(0));
 }
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;
}
function showCategoryData()
{
    global $badgerDb;
    global $logger;
    $logger->log('showCategoryData: REQUEST_URI: ' . $_SERVER['REQUEST_URI']);
    if (!isset($_GET['accounts']) || !isset($_GET['startDate']) || !isset($_GET['endDate']) || !isset($_GET['type']) || !isset($_GET['summarize'])) {
        throw new BadgerException('statistics', 'missingParameter');
    }
    $accounts = explode(';', $_GET['accounts']);
    foreach ($accounts as $key => $val) {
        settype($accounts[$key], 'integer');
    }
    $startDate = new Date($_GET['startDate']);
    $endDate = new Date($_GET['endDate']);
    $type = $_GET['type'];
    if ($type !== 'o') {
        $type = 'i';
    }
    if ($_GET['summarize'] !== 't') {
        $summarize = false;
    } else {
        $summarize = true;
    }
    $categories = gatherCategories($accounts, $startDate, $endDate, $type, $summarize);
    $sum = new Amount();
    foreach ($categories as $currentCategory) {
        $sum->add($currentCategory['amount']);
    }
    $chart = array();
    //for documentation for the following code see: http://www.maani.us/charts/index.php?menu=Reference
    $chart['chart_type'] = '3d pie';
    $chart['axis_category'] = array('skip' => 0, 'font' => "Arial", 'bold' => false, 'size' => 10, 'color' => "000000", 'alpha' => 100, 'orientation' => "horizontal");
    $chart['chart_rect'] = array('x' => 150, 'y' => 50, 'width' => 500, 'height' => 250, 'positive_color' => "ffffff", 'negative_color' => "000000", 'positive_alpha' => 0, 'negative_alpha' => 0);
    $chart['chart_value'] = array('prefix' => "", 'suffix' => "", 'decimals' => 0, 'decimal_char' => ".", 'separator' => "", 'position' => "outside", 'hide_zero' => true, 'as_percentage' => false, 'font' => "Arial", 'bold' => false, 'size' => 10, 'color' => "000000", 'alpha' => 90);
    $chart['chart_transition'] = array('type' => "none", 'delay' => 1, 'duration' => 1, 'order' => "all");
    $chart['legend_rect'] = array('x' => 700, 'y' => 400, 'width' => 0, 'height' => 0, 'margin' => 5, 'fill_color' => "FFFFFF", 'fill_alpha' => 100, 'line_color' => "000000", 'line_alpha' => 100, 'line_thickness' => 1);
    $chart['legend_label'] = array('layout' => "vertical", 'bullet' => "circle", 'font' => "Arial", 'bold' => false, 'size' => 11, 'color' => "000000", 'alpha' => 90);
    $chart['legend_transition'] = array('type' => "none", 'delay' => 1, 'duration' => 1);
    $chart['series_color'] = array('000070', 'FFFF99', '007000', 'FFCC99', '700070', 'CC99FF', '660000', '9999FF', '006666', 'CCFF99', 'A35200', 'FFCA7A');
    $chart['chart_data'] = array();
    $chart['chart_value_text'] = array();
    $chart['chart_data'][0][0] = '';
    $chart['chart_data'][1][0] = '';
    $chart['chart_value_text'][0][0] = '';
    $chart['chart_value_text'][1][0] = '';
    foreach ($categories as $key => $val) {
        $chart['chart_data'][0][] = utf8_encode($val['title']);
        $chart['chart_value_text'][0][] = null;
        if ($type == 'i') {
            $chart['chart_data'][1][] = $val['amount']->get();
        } else {
            $chart['chart_data'][1][] = $val['amount']->mul(-1)->get();
        }
        $chart['chart_value_text'][1][] = utf8_encode($val['title'] . "\n" . $val['amount']->getFormatted() . "\n" . round($val['amount']->div($sum)->mul($type == 'i' ? 100 : -100)->get(), 2) . ' %');
    }
    SendChartData($chart);
}
示例#4
0
 public function addCurrencyAmount(Amount $amt)
 {
     $this->currencyAmount = $amt->add($this->currencyAmount);
     return $this;
 }
示例#5
0
            if ($krustomer->PromoCode) {
                $subscription_pretax = $krustomer->PromoCode->apply_discount($subscription_subtotal);
            } else {
                $subscription_pretax = new Amount($subscription_subtotal);
            }

            # Add delivery charge if necessary
            $zip = dbi()->q_1("SELECT * FROM ZipCode WHERE ZipCode = '{$krustomer->DeliveryAddress->Zip}'");
            if (!empty($zip) && $zip->Delivery !== '0.00') {
                $deliveryFee = new Amount($zip->Delivery);
            } else {
                $deliveryFee = new Amount(0);
            }

            $subscription_tax = new Amount($subscription_pretax->rounded * TAX_RATE);
            $total->add($subscription_pretax, $subscription_tax, $deliveryFee);
        }
        //weekly
        if (isset($allOrders)) {
            # Loop through each order to add the total
            foreach ($allOrders as $anOrder) {
                $total->add($anOrder->Amounts->Total->rounded);
            }

            // Remove duplicate delivery fees if needed
            if(count($allOrders > 1)) {
                $total->add($anOrder->Amounts->DeliveryFee->rounded * -1);
            }

        } else if ($order) {
            $total->add($order->Amounts->Total->rounded);
 /**
  * Returns all fields in an array.
  * 
  * The result has the following form:
  * array (
  *   array (
  *     'field name 0' => 'value of field 0',
  *     'field name 1' => 'value of field 1'
  *   )
  * );
  * 
  * The inner array is repeated for each row.
  * The fields need to be in the order returned by @link getFieldNames().
  * 
  * @return array A list of all fields.
  */
 public function getAll()
 {
     global $badgerDb;
     $us = new UserSettings($badgerDb);
     $tpl = new TemplateEngine($us, BADGER_ROOT);
     $widgets = new WidgetEngine($tpl);
     $currentLanguage = $us->getProperty('badgerLanguage');
     $result = array();
     switch ($this->type) {
         case 'transaction':
             $this->fetchTransactions();
             $sum = new Amount();
             foreach ($this->finishedTransactions as $currentTransaction) {
                 $sum->add($currentTransaction->getAmount());
                 $result[] = array('transactionId' => $currentTransaction->getId(), 'type' => $widgets->addImage($currentTransaction->getType() == 'FinishedTransaction' ? 'Account/finished_transaction.png' : 'Account/planned_transaction.png', 'title="' . getBadgerTranslation2('Account', $currentTransaction->getType()) . '"'), 'title' => $currentTransaction->getTitle(), 'description' => $currentTransaction->getDescription(), 'valutaDate' => ($tmp = $currentTransaction->getValutaDate()) ? $tmp->getFormatted() : '', 'amount' => $currentTransaction->getAmount()->getFormatted(), 'outsideCapital' => is_null($tmp = $currentTransaction->getOutsideCapital()) ? '' : $tmp, 'transactionPartner' => $currentTransaction->getTransactionPartner(), 'categoryId' => ($tmp = $currentTransaction->getCategory()) ? $tmp->getId() : '', 'categoryTitle' => ($tmp = $currentTransaction->getCategory()) ? $tmp->getTitle() : '', 'sum' => $sum->getFormatted());
             }
             break;
         case 'finished':
             while ($this->fetchNextFinishedTransaction()) {
             }
             foreach ($this->finishedTransactions as $currentTransaction) {
                 $result[] = array('finishedTransactionId' => $currentTransaction->getId(), 'title' => $currentTransaction->getTitle(), 'description' => $currentTransaction->getDescription(), 'valutaDate' => ($tmp = $currentTransaction->getValutaDate()) ? $tmp->getFormatted() : '', 'amount' => $currentTransaction->getAmount()->getFormatted(), 'outsideCapital' => is_null($tmp = $currentTransaction->getOutsideCapital()) ? '' : $tmp, 'transactionPartner' => $currentTransaction->getTransactionPartner(), 'categoryId' => ($tmp = $currentTransaction->getCategory()) ? $tmp->getId() : '', 'categoryTitle' => ($tmp = $currentTransaction->getCategory()) ? $tmp->getTitle() : '', 'exceptional' => is_null($tmp = $currentTransaction->getExceptional()) ? '' : $tmp, 'periodical' => is_null($tmp = $currentTransaction->getPeriodical()) ? '' : $tmp);
             }
             break;
         case 'planned':
             while ($this->fetchNextPlannedTransaction()) {
             }
             foreach ($this->plannedTransactions as $currentTransaction) {
                 $result[] = array('plannedTransactionId' => 'p' . $currentTransaction->getId() . '_X', 'title' => $currentTransaction->getTitle(), 'description' => $currentTransaction->getDescription(), 'amount' => $currentTransaction->getAmount()->getFormatted(), 'outsideCapital' => is_null($tmp = $currentTransaction->getOutsideCapital()) ? '' : $tmp, 'transactionPartner' => $currentTransaction->getTransactionPartner(), 'beginDate' => $currentTransaction->getBeginDate()->getFormatted(), 'endDate' => ($tmp = $currentTransaction->getEndDate()) ? $tmp->getFormatted() : '', 'repeatUnit' => getBadgerTranslation2('Account', $currentTransaction->getRepeatUnit()), 'repeatFrequency' => $currentTransaction->getRepeatFrequency(), 'categoryId' => ($tmp = $currentTransaction->getCategory()) ? $tmp->getId() : '', 'categoryTitle' => ($tmp = $currentTransaction->getCategory()) ? $tmp->getTitle() : '');
             }
             break;
     }
     return $result;
 }
示例#7
0
}
array_multisort($amounts, $labels);
$other = new Amount(0);
if (count($amounts) > MAX_CATEGORIES) {
    $i = 0;
    foreach ($amounts as $currentId => $currentAmount) {
        $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');
function getAllTransactions(&$finishedTransactions, $selectedFields, $order, $upperLimit, $lowerLimit)
{
    $result = array();
    $currResultIndex = 0;
    $sum = new Amount();
    $now = new Date();
    $now->setHour(0);
    $now->setMinute(0);
    $now->setSecond(0);
    if (isset($order[0]) && $order[0]['key'] == 'valutaDate') {
        $firstOrderValutaDate = true;
        $orderCompareNumber = $order[0]['dir'] == 'asc' ? -1 : 1;
    } else {
        $firstOrderValutaDate = false;
    }
    $todayMarkerSet = false;
    foreach ($finishedTransactions as $currentTransaction) {
        $sum->add($currentTransaction->getAmount());
        $classAmount = $currentTransaction->getAmount()->compare(0) >= 0 ? 'dgPositiveAmount' : 'dgNegativeAmount';
        if ($sum->compare(0) >= 0) {
            if ($upperLimit && $sum->compare($upperLimit) > 0) {
                $classSum = 'dgOverMaxAmount';
            } else {
                $classSum = 'dgPositiveAmount';
            }
        } else {
            if ($lowerLimit && $sum->compare($lowerLimit) < 0) {
                $classSum = 'dgUnderMinAmount';
            } else {
                $classSum = 'dgNegativeAmount';
            }
        }
        $balance = $currentTransaction->getBalance();
        if ($balance->compare(0) >= 0) {
            if ($upperLimit && $balance->compare($upperLimit) > 0) {
                $classBalance = 'dgOverMaxAmount';
            } else {
                $classBalance = 'dgPositiveAmount';
            }
        } else {
            if ($lowerLimit && $balance->compare($lowerLimit) < 0) {
                $classBalance = 'dgUnderMinAmount';
            } else {
                $classBalance = 'dgNegativeAmount';
            }
        }
        $category = $currentTransaction->getCategory();
        if (!is_null($category)) {
            $parentCategory = $category->getParent();
        } else {
            $parentCategory = null;
        }
        if ($parentCategory) {
            $concatCategoryTitle = $parentCategory->getTitle() . ' - ';
        } else {
            $concatCategoryTitle = '';
        }
        if ($category) {
            $concatCategoryTitle .= $category->getTitle();
        }
        if ($currentTransaction->getType() == 'FinishedTransaction' || $currentTransaction->getType() == 'FinishedTransferalTransaction') {
            $id = $currentTransaction->getId();
        } else {
            $id = 'p' . $currentTransaction->getPlannedTransaction()->getId() . '_' . $currentTransaction->getId();
        }
        switch ($currentTransaction->getType()) {
            case 'FinishedTransaction':
                $typeImg = 'Account/finished_transaction.png';
                $typeText = getBadgerTranslation2('Account', 'FinishedTransaction');
                break;
            case 'FinishedTransferalTransaction':
                if ($currentTransaction->getTransferalSource()) {
                    $typeImg = 'Account/finished_transferal_source_transaction.png';
                    $typeText = getBadgerTranslation2('Account', 'FinishedTransferalSourceTransaction');
                } else {
                    $typeImg = 'Account/finished_transferal_target_transaction.png';
                    $typeText = getBadgerTranslation2('Account', 'FinishedTransferalTargetTransaction');
                }
                break;
            case 'PlannedTransaction':
                $typeImg = 'Account/planned_transaction.png';
                $typeText = getBadgerTranslation2('Account', 'PlannedTransaction');
                break;
            case 'PlannedTransferalTransaction':
                if ($currentTransaction->getTransferalSource()) {
                    $typeImg = 'Account/planned_transferal_source_transaction.png';
                    $typeText = getBadgerTranslation2('Account', 'PlannedTransferalSourceTransaction');
                } else {
                    $typeImg = 'Account/planned_transferal_target_transaction.png';
                    $typeText = getBadgerTranslation2('Account', 'PlannedTransferalTargetTransaction');
                }
                break;
        }
        $result[$currResultIndex] = array();
        if ($firstOrderValutaDate && $todayMarkerSet === false && !is_null($currentTransaction->getValutaDate()) && Date::compare($now, $currentTransaction->getValutaDate()) == $orderCompareNumber) {
            $result[$currResultIndex]['transactionId'] = array('marker' => 'today', 'content' => $id);
            $todayMarkerSet = true;
        } else {
            $result[$currResultIndex]['transactionId'] = $id;
        }
        foreach ($selectedFields as $selectedField) {
            switch ($selectedField) {
                case 'type':
                    $result[$currResultIndex]['type'] = array('img' => getRelativeTplPath($typeImg), 'title' => $typeText);
                    break;
                case 'title':
                    $result[$currResultIndex]['title'] = $currentTransaction->getTitle();
                    break;
                case 'description':
                    $result[$currResultIndex]['description'] = $currentTransaction->getDescription();
                    break;
                case 'valutaDate':
                    $result[$currResultIndex]['valutaDate'] = ($tmp = $currentTransaction->getValutaDate()) ? $tmp->getFormatted() : '';
                    break;
                case 'amount':
                    $result[$currResultIndex]['amount'] = array('class' => $classAmount, 'content' => $currentTransaction->getAmount()->getFormatted());
                    break;
                case 'outsideCapital':
                    $result[$currResultIndex]['outsideCapital'] = is_null($tmp = $currentTransaction->getOutsideCapital()) ? '' : $tmp;
                    break;
                case 'transactionPartner':
                    $result[$currResultIndex]['transactionPartner'] = $currentTransaction->getTransactionPartner();
                    break;
                case 'categoryId':
                    $result[$currResultIndex]['categoryId'] = $category ? $category->getId() : '';
                    break;
                case 'categoryTitle':
                    $result[$currResultIndex]['categoryTitle'] = $category ? $category->getTitle() : '';
                    break;
                case 'parentCategoryId':
                    $result[$currResultIndex]['parentCategoryId'] = $parentCategory ? $parentCategory->getId() : '';
                    break;
                case 'parentCategoryTitle':
                    $result[$currResultIndex]['parentCategoryTitle'] = $parentCategory ? $parentCategory->getTitle() : '';
                    break;
                case 'concatCategoryTitle':
                    $result[$currResultIndex]['concatCategoryTitle'] = $concatCategoryTitle;
                    break;
                case 'sum':
                    $result[$currResultIndex]['sum'] = array('class' => $classSum, 'content' => $sum->getFormatted());
                    break;
                case 'balance':
                    $result[$currResultIndex]['balance'] = array('class' => $classBalance, 'content' => $currentTransaction->getBalance()->getFormatted());
                    break;
                case 'plannedTransactionId':
                    $result[$currResultIndex]['plannedTransactionId'] = is_null($tmp = $currentTransaction->getPlannedTransaction()) ? '' : $tmp->getId();
                    break;
                case 'exceptional':
                    $result[$currResultIndex]['exceptional'] = is_null($tmp = $currentTransaction->getExceptional()) ? '' : $tmp;
                    break;
                case 'periodical':
                    $result[$currResultIndex]['periodical'] = is_null($tmp = $currentTransaction->getPeriodical()) ? '' : $tmp;
                    break;
                case 'accountTitle':
                    $result[$currResultIndex]['accountTitle'] = $currentTransaction->getAccount()->getTitle();
                    break;
            }
            //switch
        }
        //foreach selectedFields
        $currResultIndex++;
    }
    //foreach finishedTransactions
    return $result;
}
示例#9
0
 /**
  * Get new Amount with the value of $amount added to instance
  *
  * @param  Amount  $amount
  * @param  integer $precision
  * @return Amount
  */
 public function add(Amount $amount, $precision = -1)
 {
     $this->validateCurrency($amount);
     return parent::add($amount, $precision);
 }