function getSVNumbersArray()
 {
     $contract = new oqc_Contract();
     $svnumbersArray = array();
     $svnumbers = explode($this->svnumber_sep, $this->svnumbers);
     // iterate over svnumbers
     foreach ($svnumbers as $svnumber) {
         // check if svnumber is id of an existing contract
         if ($contract->retrieve($svnumber)) {
             // svnumber is the id of an existing contract
             $svnumbersArray[] = array('id' => $svnumber, 'name' => $contract->svnumber);
         } else {
             // svnumber is not the id of an existing contract
             $svnumbersArray[] = array('id' => false, 'name' => html_entity_decode($svnumber));
         }
     }
     return $svnumbersArray;
 }
function getLinkToContract($focus, $name, $value, $view)
{
    if (empty($focus->contractid)) {
        return "-";
    }
    $id = $focus->contractid;
    $module = 'oqc_Contract';
    $contract = new oqc_Contract();
    if ($contract->retrieve($id)) {
        if (!$contract->deleted) {
            $name = $contract->name;
            return "<a href='index.php?module={$module}&action=DetailView&record={$id}'>{$name}</a>";
        } else {
            return "-";
        }
    } else {
        // TODO handle exception
        return "-";
    }
}
function getProductUsage($id)
{
    $productCatalog = new oqc_ProductCatalog();
    if ($productCatalog->retrieve($id)) {
        $frequency = array();
        $json = getJSONobj();
        $c = new oqc_Contract();
        $result = $c->get_list('', 'deleted=0');
        $allContracts = $result['list'];
        foreach ($allContracts as $contract) {
            // services of this contract
            $services = $contract->get_linked_beans('oqc_service', 'oqc_Service');
            foreach ($services as $service) {
                $product = new oqc_Product();
                // if the service refers to an existing product that is defined in this product catalog ...
                if ($product->retrieve($service->product_id) && $product->catalog_id == $id) {
                    // increase the frequency of the appearance of this product
                    $frequency[$product->name]['rate'] += $service->quantity;
                    if (!array_key_exists('category', $frequency[$product->name])) {
                        $category = new oqc_Category();
                        if ($category->retrieve($product->relatedcategory_id)) {
                            $frequency[$product->name]['category'] = $category->name;
                        }
                    }
                }
            }
        }
        $chartData = array();
        foreach ($frequency as $name => $frequencyArray) {
            $chartData[] = array('name' => $name, 'frequency' => $frequencyArray['rate'], 'category' => $frequencyArray['category']);
        }
        $encoded = $json->encode($chartData);
        echo $encoded;
    } else {
        echo "No Product Catalog with id='" + $id + "' found.";
    }
}
 function action_save()
 {
     $old_id = null;
     $isLinked = false;
     if (isset($_POST['isLinked'])) {
         $isLinked = $_POST['isLinked'] == 'true' ? true : false;
     }
     // create new version of Addition and update Contract idsofadditions field	since we are creating new version of Addition
     if (!$isLinked && !empty($this->bean->contractid)) {
         if (!$this->bean->is_latest) {
             $latestVersion = $this->bean->getLatestRevision();
             $old_id = $latestVersion->id;
             $this->bean->version = intval($latestVersion->version + 1);
         } else {
             $old_id = $this->bean->id;
             $this->bean->version = intval($this->bean->version + 1);
         }
         //$GLOBALS['log']->fatal('going branch 1');
         unset($this->bean->id);
         unset($this->bean->{$this->bean->table_name . '_number'});
         $this->bean->deleted = 0;
         $this->bean->nextrevisions = '';
         $this->bean->is_latest = 1;
         //1.7.6
         $this->bean->previousrevision = $old_id;
         SugarController::action_save();
         //retrieve saved bean for oqc...number that is created during save
         $oqc_fld_number = $this->bean->table_name . '_number';
         $savedBean = new $this->bean->object_name();
         if ($savedBean->retrieve($this->bean->id)) {
             $this->bean->{$oqc_fld_number} = intval($savedBean->{$oqc_fld_number});
         }
         // 1.7.6 Keep generated svnumber for all future references
         if (empty($this->bean->svnumber)) {
             $this->bean->fill_in_svnumber();
         }
         $this->bean->oqc_delete_relationships($this->bean->id);
         // deleting documents and services- will be recreated during save
         //Recreate relationship to original contract
         $contract = 'oqc_contract';
         $this->bean->load_relationship($contract);
         $this->bean->oqc_contract->add($this->bean->contractid);
         //Update idsofadditions linked of contract
         $linkedContract = new oqc_Contract();
         if ($linkedContract->retrieve($this->bean->contractid)) {
             $linkedContract->idsofadditions = str_replace($old_id, $this->bean->id, $linkedContract->idsofadditions);
             $linkedContract->save();
         }
     } elseif ($isLinked) {
         if ($this->bean->deleted == 1) {
             $this->bean->mark_undeleted($this->bean->id);
         }
         $this->bean->deleted = 0;
         $this->bean->is_latest = 1;
         //$GLOBALS['log']->fatal('going branch 3');
         //retrieve saved bean for oqc...number that is created during save
         $oqc_fld_number = $this->bean->table_name . '_number';
         $savedBean = new $this->bean->object_name();
         if ($savedBean->retrieve($this->bean->id)) {
             $this->bean->{$oqc_fld_number} = intval($savedBean->{$oqc_fld_number});
         }
         // 1.7.6 Keep generated svnumber for all future references
         if (empty($this->bean->svnumber)) {
             $this->bean->fill_in_svnumber();
         }
         $this->bean->oqc_delete_relationships($this->bean->id);
         // deleting documents and services- will be recreated during save
         //Recreate relationship to original contract
         $contract = 'oqc_contract';
         $this->bean->load_relationship($contract);
         $this->bean->oqc_contract->add($this->bean->contractid);
         // Add new addition to the list of idsofadditions
         $linkedContract = new oqc_Contract();
         if ($linkedContract->retrieve($this->bean->contractid)) {
             $linkedContract->idsofadditions = $linkedContract->idsofadditions . " " . $this->bean->id;
             $linkedContract->save();
         }
     }
     if (isset($_POST['servicesVAT'])) {
         $this->bean->vat = $_POST['servicesVAT'];
     } else {
         if (isset($_POST['servicesOnceVAT'])) {
             $this->bean->vat = $_POST['servicesOnceVAT'];
         }
     }
     $this->saveAttachedDocuments();
     $this->saveTextblocks();
     $this->saveServices();
     if (!isset($_POST['assigned_user_id'])) {
         $this->bean->assigned_user_id = $this->bean->created_by;
     }
     //2.1 set this only if it is not in $_POST
     SugarController::action_save();
     // redirect to new version
     $this->return_id = $this->bean->id;
     $this->return_module = $this->module;
     // If previous version exist, hide it and update nextrevision field
     if ($old_id != '') {
         $oldBean = new $this->bean->object_name();
         if ($oldBean->retrieve($old_id)) {
             $oldBean->addNextRevisionId($this->bean->id);
             $oldBean->save();
             $this->bean->oqc_mark_deleted($old_id);
             //1.7.6
         }
     }
 }
<?php

if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}
require_once 'modules/oqc_Addition/oqc_Addition.php';
require_once 'modules/oqc_Contract/oqc_Contract.php';
require_once 'include/formbase.php';
// contains function handleRedirect()
define('CONTRACT_BEAN_NAME', 'oqc_Contract');
define('ADDITION_BEAN_NAME', 'oqc_Addition');
if (isset($_GET['record']) && isset($_GET['module']) && $_GET['module'] == CONTRACT_BEAN_NAME) {
    $contractId = $_GET['record'];
    $contract = new oqc_Contract();
    if ($contract->retrieve($contractId)) {
        $addition = new oqc_Addition();
        // copy all data from contract into the addition
        $addition->loadFromRow($contract->fetched_row);
        // unset id to make sure that we create a new contract
        unset($addition->id);
        $addition->document_id = '';
        //$addition->idoffreetextblock = '';
        $addition->svnumber = '';
        $addition->abbreviation = '';
        //1.7.6 Unset also abbreviation since addition has different options
        $addition->previousrevision = '';
        $addition->nextrevisions = '';
        $addition->version = 1;
        $addition->deleted = 1;
        $addition->contractid = $contractId;
        $addition->is_latest = 0;
     $offering->processed_dates_times = array();
     $offering->save();
     // redirect to contract in EditView; signal that temporary version should not be versioned by setting isLinked
     return header("Location: index.php?action=EditView&module=oqc_Contract&record={$contract->id}&isLinked=true");
 } else {
     $contract = new oqc_Contract();
     if ($contract->retrieve($previousContractId)) {
         if ($contract->deleted == 1) {
             //2.0 Contract is in deleted state because user pushed cancel instead of save; just redirect to it
             //$GLOBALS['log']->fatal("CreateContract:going branch 2");
             return header("Location: index.php?action=EditView&module=oqc_Contract&record={$contract->id}&isLinked=true");
         }
         if ($contract->version != $offering->version) {
             //$GLOBALS['log']->error("CreateContract:going branch 3");
             $oldsvnumber = $contract->svnumber;
             $newContract = new oqc_Contract();
             $newContract->loadFromRow($offering->fetched_row);
             unset($newContract->id);
             $newContract->document_id = '';
             $newContract->abbreviation = '';
             global $timedate;
             if (empty($newContract->startdate)) {
                 $newContract->startdate = date($timedate->get_date_format());
             }
             $newContract->svnumber = $oldsvnumber;
             // keep the same svnumber of contract and set the same version as quote
             $newContract->version = intval($offering->version);
             $newContract->deleted = 1;
             $newContract->is_latest = 0;
             $newContract->offeringid = $id;
             $newContract->previousrevision = $contract->id;
 private function saveSVNumbers()
 {
     $this->assembleSVNumbersIntoArray();
     $this->auditDeletedSVNumbers();
     // should not work since $svid is not id and we have separate entries.
     if (isset($_REQUEST['svnumberIds']) && is_array($_REQUEST['svnumberIds'])) {
         $svnumbers = array();
         $numberOfSVNumbers = count($_REQUEST['svnumberIds']);
         for ($i = 0; $i < $numberOfSVNumbers; $i++) {
             $contractId = $_REQUEST['svnumberIds'][$i];
             $svnumberName = $_REQUEST['svnumberNames'][$i];
             $contract = new oqc_Contract();
             if ($contract->retrieve($contractId)) {
                 // the svnumber references a valid contract. save the id of the contract instead of the svnumber name in the bean.
                 $svnumbers[] = $contractId;
             } else {
                 // the svnumber does not reference a valid contract. throw the contractId away and just store the svnumber name.
                 $svnumbers[] = $svnumberName;
             }
             // svnumber is new
             if (strpos($this->bean->svnumbers, end($svnumbers)) === false) {
                 $changes = array('field_name' => translate('LBL_SVNUMBERS'), 'data_type' => 'text', 'before' => '<n/a>', 'after' => $svnumberName);
                 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 ($numberOfSVNumbers > 0) {
             $this->bean->svnumbers = implode($this->bean->svnumber_sep, $svnumbers);
         } else {
             $this->bean->svnumbers = '';
         }
     } else {
         $this->bean->svnumbers = '';
     }
 }