if (!defined('sugarEntry')) {
    define('sugarEntry', true);
}
chdir('..');
require_once 'include/entryPoint.php';
require_once 'modules/oqc_ExternalContract/oqc_ExternalContract.php';
require_once 'modules/oqc_ExternalContractCosts/oqc_ExternalContractCosts.php';
$expenses = array();
$e = new oqc_ExternalContract();
$result = $e->get_list('', 'deleted=0');
$externalContracts = $result['list'];
foreach ($externalContracts as $externalContract) {
    $costIds = explode(' ', $externalContract->costs);
    $c = new oqc_ExternalContractCosts();
    foreach ($costIds as $id) {
        if ($c->retrieve($id)) {
            if (!array_key_exists($c->year, $expenses)) {
                $expenses[$c->year] = array();
            }
            $expenses[$c->year][$c->category] += $c->price;
        }
    }
}
$chartData = array();
foreach ($expenses as $year => $categories) {
    $chartData[] = array_merge(array('year' => $year), $categories);
}
$json = getJSONobj();
$encoded = $json->encode($chartData);
echo $encoded;
 private function saveCosts($isDuplicate)
 {
     $this->auditDeletedCosts();
     if (isset($_REQUEST['costIds']) && is_array($_REQUEST['costIds'])) {
         $costIds = array();
         $numberOfCosts = count($_REQUEST['costIds']);
         for ($i = 0; $i < $numberOfCosts; $i++) {
             $id = $_REQUEST['costIds'][$i];
             $price = $_REQUEST['costPrices'][$i];
             $desc = $_REQUEST['costDescriptions'][$i];
             $year = $_REQUEST['costYears'][$i];
             $category = $_REQUEST['costCategories'][$i];
             $paymentInterval = $_REQUEST['costPayment'][$i];
             $cost = new oqc_ExternalContractCosts();
             $beforeText = null;
             if (!$isDuplicate && $cost->retrieve($id)) {
                 if ($cost->price != $price || $cost->description != $desc || $cost->year != $year || $cost->category != $category || $cost->paymentinterval != $paymentInterval) {
                     $beforeText = $cost->as_plain_text();
                 }
             } else {
                 $beforeText = '<n/a>';
             }
             $cost = new oqc_ExternalContractCosts();
             if (!$isDuplicate && $cost->retrieve($id)) {
                 // the cost already exists (and this is no duplicate)
                 // update the cost instead of creating a new row in the table
                 $cost->price = $price;
                 $cost->year = $year;
                 $cost->category = $category;
                 $cost->description = $desc;
                 $cost->paymentinterval = $paymentInterval;
             } else {
                 $cost = new oqc_ExternalContractCosts($price, $desc, $category, $year, $paymentInterval);
             }
             $cost->save();
             $costIds[] = $cost->id;
             $this->saveDetailedCost($isDuplicate, $id, $cost);
             if (isset($beforeText)) {
                 $changes = array('field_name' => $cost->get_name(), 'data_type' => 'text', 'before' => $beforeText, 'after' => $cost->as_plain_text());
                 global $sugar_version;
                 if (floatval(substr($sugar_version, 0, 3)) > 6.3) {
                     $this->bean->db->save_audit_records($this->bean, $changes);
                 } else {
                     $this->bean->dbManager->helper->save_audit_records($this->bean, $changes);
                 }
             }
         }
         if ($numberOfCosts > 0) {
             $this->bean->costs = implode(' ', $costIds);
         } else {
             $this->bean->costs = '';
         }
     } else {
         $this->bean->costs = '';
     }
 }
 function getCostsArray($withDetailedCosts = true)
 {
     $trimmedCosts = trim($this->costs);
     $costsArray = array();
     if (!empty($trimmedCosts)) {
         $ids = explode(' ', $this->costs);
         foreach ($ids as $id) {
             if (!array_key_exists($id, oqc_ExternalContractCosts::$cache)) {
                 $cost = new oqc_ExternalContractCosts();
                 if ($cost->retrieve($id)) {
                     oqc_ExternalContractCosts::$cache[$id] = $cost;
                     $costsArray[] = $this->getCostFromCache($id, $withDetailedCosts);
                 } else {
                     $GLOBALS['log']->warn("external contract references invalid cost entity '{$id}'.");
                 }
             } else {
                 $costsArray[] = $this->getCostFromCache($id, $withDetailedCosts);
             }
         }
     }
     return $costsArray;
 }